Skip to content

Commit

Permalink
rename the modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
liwei-lai committed Oct 14, 2012
1 parent e03fa9c commit 7f4a5b5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
29 changes: 27 additions & 2 deletions pypw/handler.py → papyrus.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ def add_record(self, group, item, value, note=None):
return True
except Exception, err:
self.log.error('Error occur in adding record - %s', err)
raise
return False

def update_record(self, group, item, value, note=None):
if self._records.has_key(group) and self._records[group].has_key(item):
try:
record = self._records[group][item]
record['value'] = value
record['updated'] = datetime.today().isoformat('_')
if note:
record['note'] = note
self.write()
Expand All @@ -108,6 +108,29 @@ def update_record(self, group, item, value, note=None):
else:
return False

def delete_record(self, group, item):
if self._records.has_key(group) and self._records[group].has_key(item):
try:
record = self._records[group][item]
rid, gid = record['id'], record['gid']
del self._records['_rid'][rid]
del self._records[group][item]
if len(self._records['_gid'][gid]) == 1:
del self._records['_gid'][gid]
del self._records[group]

for i in range(len(self.data['records'])):
if self.data['records'][i]['id'] == rid:
del self.data['records'][i]
break
self.write()
return True
except Exception, err:
self.log.error('Error occur in deleting record - %s', err)
return False
else:
return False

@property
def records(self):
pass
Expand Down Expand Up @@ -140,6 +163,7 @@ def _adjust_structure(self, record):

def _compose_record(self, group, item, value, note=None):
created = datetime.today().isoformat('_')
# handle some state about group id
if group in ('_rid', '_gid', '_gidmap'):
group = 'Invalid Group Name'
gid = float('nan') # Not a number
Expand All @@ -150,14 +174,15 @@ def _compose_record(self, group, item, value, note=None):
self.data['currentGID'] += 1

record = {
# the `id` is increase use the currentID field
'id': self.data['currentID'],
'gid': gid,
'group': group,
'itemname': item,
'value': value,
'note': note,
'created': created,
'updated': created,
'updated': created,
}
self.data['currentID'] += 1
self.data['records'].append(record)
Expand Down
40 changes: 40 additions & 0 deletions pypw/handler_test.py → papyrus_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,44 @@ def setUp(self):
def tearDown(self):
self.tmpfile.close()


def test_update_delete(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'))

# update a record that `id` equals to 1, `gid` equals to 1
updated = self.handler._records[u'web'][u'google']['updated']
self.assertTrue(
self.handler.update_record(u'web', u'google', 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.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'],
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.assertTrue(self.handler.delete_record(u'bank', u'boa'))
self.assertEqual(len(self.handler._records['_gid']), 1)
self.assertTrue(self.handler.delete_record(u'web', u'facebook'))
self.assertEqual(len(self.handler._records['_gid']), 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'))


def test_data_persistance(self):
self.assertTrue(self.handler.add_record(u'web', u'facebook', u'lol2012'))
self.assertTrue(self.handler.add_record(u'web', u'google', u'answer42'))
Expand Down Expand Up @@ -52,13 +90,15 @@ def test_data_persistance(self):
self.assertEqual(self.handler.data['records'][i][key],
handler2.data['records'][i][key])


def test_32byte_key_generate(self):
key1 = AESHandler.figure_32Byte_key('not enough 32 bytes')
key2 = AESHandler.figure_32Byte_key('exceed 32 bytes' * 3)
self.assertTrue(key1.startswith('not enough 32 bytes'))
self.assertEqual(len(key1), 32)
self.assertEqual(len(key2), 32)


def test_encrypt_and_decrypt(self):
key = AESHandler.figure_32Byte_key('provide a key')
text = 'test encrypt and decrypt.'
Expand Down
Empty file removed pypw/__init__.py~
Empty file.
File renamed without changes.

0 comments on commit 7f4a5b5

Please sign in to comment.