Add the commands

This commit is contained in:
Marcin Kulik
2013-09-30 22:45:58 +02:00
parent 43eb8e8bb5
commit e8ea723ea7
11 changed files with 261 additions and 0 deletions

0
src/commands/__init__.py Normal file
View File

10
src/commands/auth.py Normal file
View File

@@ -0,0 +1,10 @@
class AuthCommand(object):
def __init__(self, api_url, user_token):
self.api_url = api_url
self.user_token = user_token
def execute(self):
url = '%s/connect/%s' % (self.api_url, self.user_token)
print 'Open following URL in your browser to authenticate and/or ' \
'claim recorded asciicasts:\n%s' % url

11
src/commands/error.py Normal file
View File

@@ -0,0 +1,11 @@
import sys
class ErrorCommand(object):
def __init__(self, message):
self.message = message
def execute(self):
print "asciinema: %s. See 'asciinema --help'." % self.message
sys.exit(1)

20
src/commands/help.py Normal file
View File

@@ -0,0 +1,20 @@
class HelpCommand(object):
def execute(self):
print HELP_TEXT
HELP_TEXT = '''usage: asciinema [-h] [-i] [-y] [-c <command>] [-t <title>] [action]
Asciicast recorder+uploader.
Actions:
rec record asciicast (this is the default when no action given)
auth authenticate and/or claim recorded asciicasts
Optional arguments:
-c command run specified command instead of shell ($SHELL)
-t title specify title of recorded asciicast
-y don't prompt for confirmation
-h, --help show this help message and exit
-v, --version show version information'''

59
src/commands/record.py Normal file
View File

@@ -0,0 +1,59 @@
import subprocess
from recorder import Recorder
from uploader import Uploader
from confirmator import Confirmator
class RecordCommand(object):
def __init__(self, api_url, user_token, cmd, title, skip_confirmation,
recorder=Recorder(), uploader=Uploader(),
confirmator=Confirmator()):
self.api_url = api_url
self.user_token = user_token
self.cmd = cmd
self.title = title
self.skip_confirmation = skip_confirmation
self.recorder = recorder
self.uploader = uploader
self.confirmator = confirmator
def execute(self):
asciicast = self._record_asciicast()
self._upload_asciicast(asciicast)
def _record_asciicast(self):
self._reset_terminal()
print '~ Asciicast recording started.'
if not self.cmd:
print '~ Hit ctrl+d or type "exit" to finish.'
print ''
asciicast = self.recorder.record(self.cmd, self.title)
self._reset_terminal()
print '~ Asciicast recording finished.'
return asciicast
def _upload_asciicast(self, asciicast):
if self._upload_confirmed():
print '~ Uploading...'
try:
url = self.uploader.upload(self.api_url, self.user_token, asciicast)
print url
except SystemExit:
print '~ Error during upload. Try again in a minute.'
def _upload_confirmed(self):
if self.skip_confirmation:
return True
return self.confirmator.confirm("~ Do you want to upload it? [Y/n] ")
def _reset_terminal(self):
# subprocess.call(["reset"])
pass

7
src/commands/version.py Normal file
View File

@@ -0,0 +1,7 @@
from common import VERSION
class VersionCommand(object):
def execute(self):
print 'asciinema %s' % VERSION

View File

@@ -0,0 +1,14 @@
import re
from commands.auth import AuthCommand
from test_helper import assert_printed, Test
class TestAuthCommand(Test):
def test_execute(self):
command = AuthCommand('http://the/url', 'a1b2c3')
command.execute()
assert_printed('http://the/url/connect/a1b2c3')

View File

@@ -0,0 +1,13 @@
from nose.tools import assert_raises
from commands.error import ErrorCommand
from test_helper import assert_printed, Test
class TestErrorCommand(Test):
def test_execute(self):
command = ErrorCommand('foo')
assert_raises(SystemExit, command.execute)
assert_printed('foo')

View File

@@ -0,0 +1,15 @@
from commands.help import HelpCommand
from test_helper import assert_printed, Test
class TestHelpCommand(Test):
def test_execute(self):
command = HelpCommand()
command.execute()
assert_printed('asciinema')
assert_printed('usage')
assert_printed('rec')
assert_printed('auth')

View File

@@ -0,0 +1,99 @@
import sys
from nose.tools import assert_equal
from commands.record import RecordCommand
from test_helper import assert_printed, assert_not_printed, Test
class FakeAsciicast(object):
def __init__(self, cmd, title):
self.cmd = cmd
self.title = title
class FakeRecorder(object):
def __init__(self):
self.asciicast = None
def record(self, cmd, title):
self.asciicast = FakeAsciicast(cmd, title)
return self.asciicast
class FakeUploader(object):
def __init__(self):
self.uploaded = None
def upload(self, api_url, user_token, asciicast):
self.uploaded = [api_url, user_token, asciicast]
return 'http://asciicast/url'
class FakeConfirmator(object):
def __init__(self):
self.text = ''
self.success = True
def confirm(self, text):
self.text = text
return self.success
class TestRecordCommand(Test):
def setUp(self):
Test.setUp(self)
self.recorder = FakeRecorder()
self.uploader = FakeUploader()
self.confirmator = FakeConfirmator()
def create_command(self, skip_confirmation):
return RecordCommand('http://the/url', 'a1b2c3', 'ls -l', 'the title',
skip_confirmation, self.recorder, self.uploader,
self.confirmator)
def test_execute_when_upload_confirmation_skipped(self):
command = self.create_command(True)
self.confirmator.success = False
command.execute()
assert 'Do you want to upload' not in self.confirmator.text
self.assert_recorded_and_uploaded()
def test_execute_when_upload_confirmed(self):
command = self.create_command(False)
self.confirmator.success = True
command.execute()
assert 'Do you want to upload' in self.confirmator.text
self.assert_recorded_and_uploaded()
def test_execute_when_upload_rejected(self):
command = self.create_command(False)
self.confirmator.success = False
command.execute()
assert 'Do you want to upload' in self.confirmator.text
self.assert_recorded_but_not_uploaded()
def assert_recorded_but_not_uploaded(self):
asciicast = self.recorder.asciicast
assert asciicast, 'asciicast not recorded'
assert_not_printed('Uploading...')
assert_equal(None, self.uploader.uploaded)
def assert_recorded_and_uploaded(self):
asciicast = self.recorder.asciicast
assert asciicast, 'asciicast not recorded'
assert_equal('ls -l', asciicast.cmd)
assert_equal('the title', asciicast.title)
assert_printed('Uploading...')
assert_equal(['http://the/url', 'a1b2c3', asciicast], self.uploader.uploaded)
assert_printed('http://asciicast/url')

View File

@@ -0,0 +1,13 @@
from commands.version import VersionCommand
from common import VERSION
from test_helper import assert_printed, Test
class TestVersionCommand(Test):
def test_execute(self):
command = VersionCommand()
command.execute()
assert_printed('asciinema %s' % VERSION)