Skip to content
Snippets Groups Projects
Commit 796b75c0 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

main_attachment changed to data

parent 893c29d2
No related branches found
No related tags found
No related merge requests found
...@@ -38,7 +38,7 @@ class LogEntry(object): ...@@ -38,7 +38,7 @@ class LogEntry(object):
self.tags = set(tags) self.tags = set(tags)
self.attachments = {} #: dict(attachment name::str => attachment) self.attachments = {} #: dict(attachment name::str => attachment)
self.main_attachment = None #: main attachment self.data = None #: extra data
def attach(self, *args): def attach(self, *args):
""" """
...@@ -47,7 +47,7 @@ class LogEntry(object): ...@@ -47,7 +47,7 @@ class LogEntry(object):
(first of them will be a str, name of the entry, second one - the data to attach). (first of them will be a str, name of the entry, second one - the data to attach).
""" """
if len(args) == 1: # Attach an attachment without a name if len(args) == 1: # Attach an attachment without a name
self.main_attachment = args[0] self.data = args[0]
elif len(args) == 2: # Attach a named attachment elif len(args) == 2: # Attach a named attachment
self.attachments[args[0]] = args[1] self.attachments[args[0]] = args[1]
else: else:
...@@ -59,15 +59,15 @@ class LogEntry(object): ...@@ -59,15 +59,15 @@ class LogEntry(object):
def to_compact(self): def to_compact(self):
"""Serializes this as Python-specific string""" """Serializes this as Python-specific string"""
return pickle.dumps( return pickle.dumps(
(self.when, self.who, self.tags, self.main_attachment, self.attachments), (self.when, self.who, self.tags, self.data, self.attachments),
pickle.HIGHEST_PROTOCOL pickle.HIGHEST_PROTOCOL
) )
@staticmethod @staticmethod
def from_compact(p): def from_compact(p):
"""@param p: str""" """@param p: str"""
when, who, tags, main_attachment, attachments = pickle.loads(p) when, who, tags, data, attachments = pickle.loads(p)
le = LogEntry(who, tags, when).attach(main_attachment) le = LogEntry(who, tags, when).attach(data)
for k, v in attachments.iteritems(): for k, v in attachments.iteritems():
le.attach(k, v) le.attach(k, v)
return le return le
...@@ -78,7 +78,7 @@ class LogEntry(object): ...@@ -78,7 +78,7 @@ class LogEntry(object):
'when': self.when, 'when': self.when,
'who': self.who, 'who': self.who,
'tags': sorted(self.tags), 'tags': sorted(self.tags),
'main': self.main_attachment, 'data': self.data,
'attachments': dict(( 'attachments': dict((
(name, base64.b64encode(pickle.dumps(value, pickle.HIGHEST_PROTOCOL))) (name, base64.b64encode(pickle.dumps(value, pickle.HIGHEST_PROTOCOL)))
for name, value in self.attachments.iteritems() for name, value in self.attachments.iteritems()
...@@ -93,7 +93,7 @@ class LogEntry(object): ...@@ -93,7 +93,7 @@ class LogEntry(object):
Know that main_attachment's str's will get converted to Unicode, due to how JSON works. Know that main_attachment's str's will get converted to Unicode, due to how JSON works.
@type jsonstr: str""" @type jsonstr: str"""
jo = json.loads(jsonstr) jo = json.loads(jsonstr)
le = LogEntry(str(jo['who']), map(str, jo['tags']), jo['when']).attach(jo['main']) le = LogEntry(str(jo['who']), map(str, jo['tags']), jo['when']).attach(jo['data'])
for aname, avs in jo['attachments'].iteritems(): for aname, avs in jo['attachments'].iteritems():
le.attach(aname, pickle.loads(base64.b64decode(avs))) le.attach(aname, pickle.loads(base64.b64decode(avs)))
return le return le
...@@ -58,7 +58,7 @@ class LoggingTest(unittest.TestCase): ...@@ -58,7 +58,7 @@ class LoggingTest(unittest.TestCase):
le.attach('test string', 'hello world') le.attach('test string', 'hello world')
self.assertEquals(len(le.attachments), 1) self.assertEquals(len(le.attachments), 1)
self.assertEquals(le.main_attachment, 'hello world') self.assertEquals(le.data, 'hello world')
self.assertEquals(set(le.tags), set(('satella', 'test'))) self.assertEquals(set(le.tags), set(('satella', 'test')))
# test fluid interface of .attach() # test fluid interface of .attach()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment