#!/usr/bin/python # -*- coding: utf-8 -*- import logging import urllib log = logging.getLogger(__name__) class GroupList(object): def __init__(self): self.groups={} self.lastChange=False def load(self,data): if 'lastChange' in data: self.lastChange=data['lastChange'] if 'groups' in data: for g in data['groups']: self.groups[g]=Group() self.groups[g].load(data['groups'][g]) def export(self): groups={} for uuid in self.groups: groups[uuid]=self.groups[uuid].export() return { 'lastChange': self.lastChange, 'groups': groups } def toJSON(self,pretty=False): if pretty: return json.dumps(self.export(),indent=4, separators=(',', ': ')) else: return json.dumps(self.export()) def sync(self,groups): ret=GroupList() if groups.lastChangelastChange: lastChange=group.contributions[uuid].lastChange if self.deletedContributors[email].deletionTimelastChange: lastChange=ret.contributions[uuid].lastChange if group.deletedContributors[email].deletionTimeret.deletedContributions[uuid].lastChange: ret.contributions[uuid]=self.contributions[uuid] del ret.deletedContributions[uuid] for uuid in group.contributions: if uuid not in ret.contributions: if uuid not in ret.deletedContributions: ret.contributions[uuid]=group.contributions[uuid] elif group.contributions[uuid].lastChange>ret.deletedContributions[uuid].lastChange: ret.contributions[uuid]=group.contributions[uuid] del ret.deletedContributions[uuid] ## Categories for uuid in self.categories: if uuid in group.categories: ret.categories[uuid]=self.categories[uuid].sync(group.categories[uuid]) elif uuid not in ret.deletedCategories: ret.categories[uuid]=self.categories[uuid] elif self.categories[uuid].lastChange>ret.deletedCategories[uuid].lastChange: ret.categories[uuid]=self.categories[uuid] del ret.deletedCategories[uuid] for uuid in group.categories: if uuid not in ret.categories: if uuid not in ret.deletedCategories: ret.categories[uuid]=group.categories[uuid] elif group.categories[uuid].lastChange>ret.deletedCategories[uuid].lastChange: ret.categories[uuid]=group.categories[uuid] del ret.deletedCategories[uuid] return ret class Contribution(object): def __init__(self): self.uuid=None self.contributor=None self.title=None self.cost=None self.date=None self.category=None self.lastChange=None def load(self,data): self.uuid=data.get('uuid',None) self.contributor=data.get('contributor',None) self.title=data.get('title',None) self.cost=data.get('cost',None) self.date=data.get('date',None) self.category=data.get('category',None) self.lastChange=data.get('lastChange',None) def export(self): return { 'uuid': self.uuid, 'contributor': self.contributor, 'cost': self.cost, 'title': self.title, 'date': self.date, 'category': self.category, 'lastChange': self.lastChange } def sync(self, c): if c.lastChange>self.lastChange: return c else: return self class Contributor(object): def __init__(self): self.name=None self.email=None self.deletionTime=None def load(self,data): self.name=data.get('name',None) self.email=data.get('email',None) self.deletionTime=data.get('deletionTime',None) def export(self): ret={ 'name': self.name, 'email': self.email } if self.deletionTime is not None: ret['deletionTime']=self.deletionTime return ret class Category(object): def __init__(self): self.name=None self.color=None self.lastChange=None def load(self,data): self.name=data.get('name',None) self.color=data.get('color',None) self.lastChange=data.get('lastChange',None) def export(self): ret={ 'name': self.name, 'color': self.color, 'lastChange': self.lastChange } return ret def sync(self, c): if c.lastChange>self.lastChange: return c else: return self if __name__ == '__main__': import testdata import json data=json.loads(testdata.group_as_json) data2=json.loads(testdata.group_as_json2) gl=GroupList() gl.load(data2) print gl.toJSON(True) print 'Try sync groups' gl2=GroupList() gl2.load(data) gl_sync=gl.sync(gl2) print 'Sync group : %s' % gl_sync print gl_sync.toJSON(True)