Commits

Anonymous committed f0281ed

Allow the Tracklr objects to be used like a readonly array

  • Participants
  • Parent commits ec43192

Comments (0)

Files changed (1)

         self.type = obj_type
         self.tid = tid
         self.value = value
+        self.children = None
         # Should this be a weak ref for Pythons GC?
         self.parent = parent
 
     def __str__(self):
         return '%s: %s' % (self.type, self.value)
 
+    def __getitem__(self, key):
+        self._get_children()
+        return self.children[key]
+
     def _find_parent(self, obj_type):
         obj = self
         while obj != None and obj.type != obj_type:
             obj = obj.parent
         return obj
 
+    def _get_children_list(self):
+        self._get_children()
+        return self.children.itervalues()
+
+    def keys(self):
+        self._get_children()
+        return self.children.keys()
+
 class Tracklr(TracklrBase):
     def __init__(self, user, passwd):
         TracklrBase.__init__(self, 'Tracklr', '0', 'Tracklr', None)
         _connect(user, passwd)
 
-    def _get_companies(self):
-        ret = []
-        companies = _get('/companies/')
-        for cmpy in companies:
-            ret.append(Company(self, cmpy['id'], cmpy['name']))
-        return ret
-    companies = property(_get_companies)
+    def _get_children(self):
+        if self.children is None:
+            self.children = {}
+            companies = _get('/companies/')
+            for cmpy in companies:
+                self.children[cmpy['id']] = Company(self, cmpy['id'],
+                  cmpy['name'])
+
+    companies = property(lambda self: self._get_children_list())
 
 class Company(TracklrBase):
     def __init__(self, parent, cid, name):
         TracklrBase.__init__(self, 'Company', cid, name, parent)
         self.name = name
 
-    def _get_clients(self):
-        ret = []
-        clients = _get('/%s/clients/' % self.tid)
-        for client in clients:
-            ret.append(Client(self, client['id'], client['name'],
-              client['active']))
-        return ret
-    clients = property(_get_clients)
+    def _get_children(self):
+        if self.children is None:
+            self.children = {}
+            clients = _get('/%s/clients/' % self.tid)
+            for client in clients:
+                self.children[client['id']] = Client(self, client['id'],
+                  client['name'], client['active'])
+
+    clients = property(lambda self: self._get_children_list())
 
 class Client(TracklrBase):
     def __init__(self, parent, cid, name, active):
         self.name = name
         self.active = active
 
-    def _get_projects(self):
-        ret = []
-        projects = _get('/%s/%s/projects/' %
-          (self._find_parent('Company').tid, self.tid))
-        for project in projects:
-            ret.append(Project(self, project['id'], project['name'],
-              project['active'], project['billable']))
-        return ret
-    projects = property(_get_projects)
+    def _get_children(self):
+        if self.children is None:
+            self.children = {}
+            projects = _get('/%s/%s/projects/' %
+              (self._find_parent('Company').tid, self.tid))
+            for project in projects:
+                self.children[project['id']] = Project(self, project['id'],
+                  project['name'], project['active'], project['billable'])
+
+    projects = property(lambda self: self._get_children_list())
 
 class Project(TracklrBase):
     def __init__(self, parent, pid, name, active, billable):
         self.active = active
         self.billable = billable
 
-    def _get_tasks(self):
-        ret = []
-        tasks = _get('/%s/%s/%s/tasks/' % (self._find_parent('Company').tid,
-            self._find_parent('Client').tid, self.tid))
-        for task in tasks:
-            ret.append(Task(self, task['id'], task['name'], task['active'],
-              task['billable']))
-        return ret
-    tasks = property(_get_tasks)
+    def _get_children(self):
+        if self.children is None:
+            self.children = {}
+            tasks = _get('/%s/%s/%s/tasks/' % (self._find_parent('Company').tid,
+                self._find_parent('Client').tid, self.tid))
+            for task in tasks:
+                self.children[task['id']] = Task(self, task['id'], task['name'],
+                  task['active'], task['billable'])
+
+    tasks = property(lambda self: self._get_children_list())
 
 class Task(TracklrBase):
     def __init__(self, parent, tid, name, active, billable):
         self.active = active
         self.billable = billable
 
-    def _get_events(self):
-        ret = []
-        events = _get('/%s/%s/%s/%s/events/' %
-          (self._find_parent('Company').tid,
-           self._find_parent('Client').tid, self._find_parent('Project').tid,
-           self.tid))
-        for event in events:
-            ret.append(Event(self, event['id'], event['log'], event['start'],
-              event['stop'], event['billable'], event['in_progress']))
-        return ret
-    events = property(_get_events)
+    def _get_children(self):
+        if self.children is None:
+            self.children = {}
+            events = _get('/%s/%s/%s/%s/events/' %
+              (self._find_parent('Company').tid,
+               self._find_parent('Client').tid,
+               self._find_parent('Project').tid, self.tid))
+            for event in events:
+                self.children[event['id']] = Event(self, event['id'],
+                  event['log'], event['start'], event['stop'],
+                  event['billable'], event['in_progress'])
+
+    events = property(lambda self: self._get_children_list())
 
 class Event(TracklrBase):
     def __init__(self, parent, eid, log, start, stop, billable, in_progress):
                           EVENT.stop)
                         print '   ', EVENT.length
 
+    # You can also use pytraclkr like a dict, e.g.
+    # TRACKLR['company']['client']['project']['task']['event']
+