| @@ -218,6 +218,13 @@ class Persona(object): | |||||
| return self._identity | return self._identity | ||||
| def new_version(self, *args): | |||||
| '''Update the Persona's Identity object.''' | |||||
| self._identity = self.sign(self._identity.new_version(*args)) | |||||
| return self._identity | |||||
| def store(self, fname): | def store(self, fname): | ||||
| '''Store the Persona to a file. If there is a private | '''Store the Persona to a file. If there is a private | ||||
| key associated w/ the Persona, it will be saved as well.''' | key associated w/ the Persona, it will be saved as well.''' | ||||
| @@ -457,17 +464,36 @@ def enumeratedir(_dir, created_by_ref): | |||||
| def main(): | def main(): | ||||
| from optparse import OptionParser | from optparse import OptionParser | ||||
| import sys | |||||
| parser = OptionParser() | parser = OptionParser() | ||||
| parser.add_option('-a', action='append', dest='add', | parser.add_option('-a', action='append', dest='add', | ||||
| default=[], help='add the arg as metadata for files, tag=value') | default=[], help='add the arg as metadata for files, tag=value') | ||||
| parser.add_option('-d', action='append', dest='delete', | parser.add_option('-d', action='append', dest='delete', | ||||
| default=[], help='delete the arg as metadata from files. Either specify tag, and all tags are removed, or specify tag=value and that specific tag will be removed.') | default=[], help='delete the arg as metadata from files. Either specify tag, and all tags are removed, or specify tag=value and that specific tag will be removed.') | ||||
| parser.add_option('-g', action='store_true', dest='generateident', | |||||
| default=False, help='generate an identity') | |||||
| parser.add_option('-l', action='store_true', dest='list', | parser.add_option('-l', action='store_true', dest='list', | ||||
| default=False, help='list metadata') | default=False, help='list metadata') | ||||
| options, args = parser.parse_args() | options, args = parser.parse_args() | ||||
| # this is shared between generateident and add | |||||
| addprops = map(lambda x: x.split('=', 1), options.add) | |||||
| if options.generateident: | |||||
| identfname = os.path.expanduser('~/.medashare_identity.pasn1') | |||||
| if os.path.exists(identfname): | |||||
| print >>sys.stderr, 'Error: Identity already created.' | |||||
| sys.exit(1) | |||||
| persona = Persona() | |||||
| persona.generate_key() | |||||
| persona.new_version(*addprops) | |||||
| persona.store(identfname) | |||||
| return | |||||
| storefname = os.path.expanduser('~/.medashare_store.pasn1') | storefname = os.path.expanduser('~/.medashare_store.pasn1') | ||||
| import sys | import sys | ||||
| #print >>sys.stderr, `storefname` | #print >>sys.stderr, `storefname` | ||||
| @@ -480,7 +506,6 @@ def main(): | |||||
| for k, v in _iterdictlist(j): | for k, v in _iterdictlist(j): | ||||
| print '%s:\t%s' % (k, v) | print '%s:\t%s' % (k, v) | ||||
| elif options.add: | elif options.add: | ||||
| addprops = map(lambda x: x.split('=', 1), options.add) | |||||
| for i in args: | for i in args: | ||||
| for j in objstr.by_file(i): | for j in objstr.by_file(i): | ||||
| nobj = j.new_version(*addprops) | nobj = j.new_version(*addprops) | ||||
| @@ -805,6 +830,24 @@ class _TestCases(unittest.TestCase): | |||||
| with mock.patch('os.path.expanduser', side_effect=expandusermock) \ | with mock.patch('os.path.expanduser', side_effect=expandusermock) \ | ||||
| as eu: | as eu: | ||||
| with nested(mock.patch('sys.stdout', | |||||
| StringIO.StringIO()), mock.patch('sys.argv', | |||||
| [ 'progname', '-g', '-a', 'name=A Test User' ])) as (stdout, argv): | |||||
| main() | |||||
| self.assertEqual(stdout.getvalue(), | |||||
| '') | |||||
| eu.assert_called_with('~/.medashare_identity.pasn1') | |||||
| persona = Persona.load(identfname) | |||||
| self.assertEqual(persona.get_identity().name, 'A Test User') | |||||
| with nested(mock.patch('sys.stderr', | |||||
| StringIO.StringIO()), mock.patch('sys.argv', | |||||
| [ 'progname', '-g', '-a', 'name=A Test User' ])) as (stderr, argv): | |||||
| self.assertRaises(SystemExit, main) | |||||
| self.assertEqual(stderr.getvalue(), | |||||
| 'Error: Identity already created.\n') | |||||
| eu.assert_called_with('~/.medashare_identity.pasn1') | |||||
| with nested(mock.patch('sys.stdout', | with nested(mock.patch('sys.stdout', | ||||
| StringIO.StringIO()), mock.patch('sys.argv', | StringIO.StringIO()), mock.patch('sys.argv', | ||||
| [ 'progname', '-l', testfname ])) as (stdout, argv): | [ 'progname', '-l', testfname ])) as (stdout, argv): | ||||