Skip to content

Commit

Permalink
add new feature - mv - support to the program and upgrade the unittes…
Browse files Browse the repository at this point in the history
…t file.
  • Loading branch information
liwei-lai committed Nov 2, 2014
1 parent d861eb9 commit fc071b6
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 33 deletions.
10 changes: 10 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ Support Commands:

Delete a record to the program.

- mv::
Usage: mv record_id group_id

args::
- record_id: the id of the record, `ls` is a useful command for
lookup the record id.
- group_id: the id of the group.

Move a record to the specify group.

Example
-------

Expand Down
74 changes: 64 additions & 10 deletions papyrus.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ def initialize(self, cipher, filepath='records.dat'):
"""
self.filepath = filepath
self.cipher = self.figure_32Byte_key(cipher)

# first initial the program
if not os.path.exists(self.filepath) or \
not os.path.getsize(self.filepath):
# initiali the self.data and self._records
# initiali the empty self.data and self._records
self._init_data()
self.initialized = True
return self.initialized
Expand Down Expand Up @@ -111,6 +111,38 @@ def update_record(self, record_id, value, note=None):
else:
return False

def move_record(self, record_id, group_id):
record_id, group_id = int(record_id), int(group_id)
if record_id in self._records['_rid'] and group_id in self._records['_gid']:
try:
record = self._records['_rid'][record_id]
if record['gid'] == group_id:
print 'The group_id already is the record gid, no need to move.'
return False
if len(self._records['_gid'][group_id]) == 0:
return False

old_gid = record['gid']
old_group = record['group']
new_group = self._records['_gid'][group_id][0]['group']
record['gid'] = group_id
record['group'] = new_group
record['updated'] = datetime.today().isoformat('_')

# delete the old data
self._del_record_in_gid(record_id, old_gid)
item = record['itemname']
del self._records[old_group][item]

self._adjust_structure(record)
self.write()
return True
except Exception, err:
self.log.error('Error occur in updating record - %s', err)
return False
else:
return False

def delete_record(self, record_id):
record_id = int(record_id)
if self._records['_rid'].has_key(record_id):
Expand All @@ -124,11 +156,7 @@ def delete_record(self, record_id):
del self._records['_gid'][gid]
del self._records[group]
else:
# delete the record in the _records['_gid']
for i in range(len(self._records['_gid'][gid])):
if self._records['_gid'][gid][i]['id'] == rid:
del self._records['_gid'][gid][i]
break
self._del_record_in_gid(rid, gid)

# delete the record in the data['records']
for i in range(len(self.data['records'])):
Expand All @@ -155,7 +183,7 @@ def _init_data(self):
'currentID': 0,
'currentGID': 0,
}
self.data = structure
self.data = structure
self._setup_structure()

def _setup_structure(self):
Expand All @@ -174,6 +202,17 @@ def _adjust_structure(self, record):
if not self._records['_gidmap'].has_key(group):
self._records['_gidmap'][group] = record['gid']

def _del_record_in_gid(self, rid, gid):
"""delete the record in the _records['_gid'].
:param rid: the id of the record.
:param gid: the id of the group.
"""
for i in range(len(self._records['_gid'][gid])):
if self._records['_gid'][gid][i]['id'] == rid:
del self._records['_gid'][gid][i]
break

def _compose_record(self, group, item, value, note=None):
created = datetime.today().isoformat('_')
# handle some state about group id
Expand Down Expand Up @@ -338,14 +377,14 @@ def do_ls(self, line):
- `records`: literal key word, show all the records
- group_name: group name, show all the records in the specific group
- group_id: group id, show all the records in the specific group
List all the groups or records existing in the current program.
"""
# single `ls` command, default to show all groups
if line == '':
self._ls_case_groups('groups')
return

args = self._validate_line(line, lengths=(1, 2), cmd='ls')
target = args[0]
if target.isdigit():
Expand Down Expand Up @@ -445,6 +484,21 @@ def do_delete(self, line):
if not self.handler.delete_record(*args):
raise PapyrusException(u"Fail to delete record to the program.")

def do_mv(self, line):
"""Help message:
Usage: mv record_id group_id
args::
- record_id: the id of the record, `ls` is a useful command for
lookup the record id.
- group_id: the id of the group.
Move a record to the specify group.
"""
args = self._validate_line(line, lengths=(2,), cmd='mv')
if not self.handler.move_record(*args):
raise PapyrusException(u"Fail to move record to the program.")

# def complete_update(self, text, line, begidx, endidx):
# clist = []
# for record in self.handler.records['_rid'].values():
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding:utf-8 -*-

__version__ = "0.4"
__version__ = "0.5"
__author__ = [
"Jason Lai <[email protected]>"
]
Expand Down
104 changes: 82 additions & 22 deletions test_papyrus.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import unittest
import tempfile
from pprint import pprint
import math

from papyrus import AESHandler


class TestAESHandler(unittest.TestCase):

def setUp(self):
self.tmpfile = tempfile.NamedTemporaryFile()
self.handler = AESHandler()
Expand All @@ -24,38 +25,97 @@ def test_update_delete(self):
self.assertTrue(self.handler.add_record(u'web', u'facebook', u'lol2012'))

# update a record that `id` equals to 1, `gid` equals to 1
updated = self.handler._records[u'web'][u'google']['updated']
updated = self.handler.records[u'web'][u'google']['updated']
self.assertTrue(
self.handler.update_record(1, u'google42', u'a note')
)
self.assertEqual(self.handler.data['records'][1]['value'], u'google42')
self.assertEqual(self.handler.data['records'][0]['note'], None)
self.assertEqual(self.handler.data['records'][1]['note'], u'a note')
self.assertEqual(self.handler.data['records'][1]['value'],
self.handler._records[u'web'][u'google']['value'])
self.handler.records[u'web'][u'google']['value'])
self.assertEqual(self.handler.data['records'][1]['note'],
self.handler._records[u'web'][u'google']['note'])
self.assertNotEqual(self.handler._records[u'web'][u'google']['updated'],
self.handler.records[u'web'][u'google']['note'])
self.assertNotEqual(self.handler.records[u'web'][u'google']['updated'],
updated)

# delete a record that `id` equals to 0 and 2, `gid` equals to 0 and 1
self.assertEqual(len(self.handler._records['_gid']), 2)
self.assertEqual(len(self.handler.records['_gid']), 2)
self.assertTrue(self.handler.delete_record(0))
self.assertEqual(len(self.handler._records['_gid']), 1)
self.assertEqual(len(self.handler._records['_gid'][1]), 2)
self.assertEqual(len(self.handler.records['_gid']), 1)
self.assertEqual(len(self.handler.records['_gid'][1]), 2)
self.assertTrue(self.handler.delete_record(2))
self.assertEqual(len(self.handler._records['_gid']), 1)
self.assertEqual(len(self.handler._records['_gid'][1]), 1)
self.assertEqual(len(self.handler.records['_gid']), 1)
self.assertEqual(len(self.handler.records['_gid'][1]), 1)

self.assertEqual(len(self.handler.data['records']), 1)
self.assertFalse(self.handler._records['_rid'].has_key(0))
self.assertFalse(self.handler._records['_rid'].has_key(2))
self.assertFalse(self.handler._records['_gid'].has_key(0))
self.assertTrue(self.handler._records['_gid'].has_key(1))
self.assertFalse(self.handler._records.has_key(u'bank'))
self.assertTrue(self.handler._records.has_key(u'web'))
self.assertFalse(self.handler._records[u'web'].has_key(u'facebook'))
self.assertFalse(self.handler.records['_rid'].has_key(0))
self.assertFalse(self.handler.records['_rid'].has_key(2))
self.assertFalse(self.handler.records['_gid'].has_key(0))
self.assertTrue(self.handler.records['_gid'].has_key(1))
self.assertFalse(self.handler.records.has_key(u'bank'))
self.assertTrue(self.handler.records.has_key(u'web'))
self.assertFalse(self.handler.records[u'web'].has_key(u'facebook'))


def test_move(self):
self.assertTrue(self.handler.add_record(u'bank', u'boa', u'kkk3000'))
self.assertTrue(self.handler.add_record(u'web', u'google', u'answer42'))
self.assertTrue(self.handler.add_record(u'web', u'facebook', u'lol2012'))

self.assertEqual(len(self.handler.records['_gid']), 2)
self.assertEqual(len(self.handler.records['_rid']), 3)

# ensure the relationship between the added records
self.assertEqual(len(self.handler.records['_gid'][0]), 1)
self.assertEqual(len(self.handler.records['_gid'][1]), 2)
self.assertTrue( 'boa' in self.handler.records['bank'] )
self.assertEqual(self.handler.records['_rid'][0]['group'], 'bank')
self.assertEqual(self.handler.records['_rid'][0]['gid'], 0)
self.assertTrue( 'google' in self.handler.records['web'] )
self.assertEqual(self.handler.records['_rid'][1]['group'], 'web')
self.assertEqual(self.handler.records['_rid'][1]['gid'], 1)
self.assertTrue( 'facebook' in self.handler.records['web'] )
self.assertEqual(self.handler.records['_rid'][2]['group'], 'web')
self.assertEqual(self.handler.records['_rid'][2]['gid'], 1)

# First operation
updated = self.handler.records[u'web'][u'google']['updated']
self.assertTrue( self.handler.move_record(1, 0) )

self.assertEqual(len(self.handler.records['_gid'][0]), 2)
self.assertEqual(len(self.handler.records['_gid'][1]), 1)
self.assertTrue( 'boa' in self.handler.records['bank'] )
self.assertEqual(self.handler.records['_rid'][0]['group'], 'bank')
self.assertEqual(self.handler.records['_rid'][0]['gid'], 0)
self.assertTrue( 'google' in self.handler.records['bank'] )
self.assertEqual(self.handler.records['_rid'][1]['group'], 'bank')
self.assertEqual(self.handler.records['_rid'][1]['gid'], 0)
self.assertTrue( 'facebook' in self.handler.records['web'] )
self.assertEqual(self.handler.records['_rid'][2]['group'], 'web')
self.assertEqual(self.handler.records['_rid'][2]['gid'], 1)

self.assertNotEqual(self.handler.records[u'bank'][u'google']['updated'],
updated)

# Second operation
updated = self.handler.records[u'bank'][u'google']['updated']
self.assertTrue( self.handler.move_record(1, 1) )

self.assertEqual(len(self.handler.records['_gid'][0]), 1)
self.assertEqual(len(self.handler.records['_gid'][1]), 2)
self.assertTrue( 'boa' in self.handler.records['bank'] )
self.assertEqual(self.handler.records['_rid'][0]['group'], 'bank')
self.assertEqual(self.handler.records['_rid'][0]['gid'], 0)
self.assertTrue( 'google' in self.handler.records['web'] )
self.assertEqual(self.handler.records['_rid'][1]['group'], 'web')
self.assertEqual(self.handler.records['_rid'][1]['gid'], 1)
self.assertTrue( 'facebook' in self.handler.records['web'] )
self.assertEqual(self.handler.records['_rid'][2]['group'], 'web')
self.assertEqual(self.handler.records['_rid'][2]['gid'], 1)

self.assertNotEqual(self.handler.records[u'web'][u'google']['updated'],
updated)

def test_data_persistance(self):
self.assertTrue(self.handler.add_record(u'web', u'facebook', u'lol2012'))
Expand All @@ -67,13 +127,13 @@ def test_data_persistance(self):
self.assertEqual(len(self.handler.data['records']), 4)
self.assertEqual(self.handler.data['currentID'], 4)
self.assertEqual(self.handler.data['currentGID'], 2)
self.assertEqual(len(self.handler._records['_rid']), 4)
self.assertEqual(len(self.handler._records['_gid']), 3)
self.assertEqual(len(self.handler._records['_gidmap']), 3)
self.assertEqual(len(self.handler.records['_rid']), 4)
self.assertEqual(len(self.handler.records['_gid']), 3)
self.assertEqual(len(self.handler.records['_gidmap']), 3)
self.assertTrue(
self.handler._records['_gidmap'].has_key('Invalid Group Name')
self.handler.records['_gidmap'].has_key('Invalid Group Name')
)

handler2 = AESHandler()
handler2.initialize('provide a key', self.tmpfile.name)

Expand Down

0 comments on commit fc071b6

Please sign in to comment.