Commits

paulc  committed d1475e7

Update

  • Participants
  • Parent commits 0480e1a

Comments (0)

Files changed (1)

             t += [ ('params[]',x) for x in args ]
         r = self.action('get_account_tree','listing_ok',*t)
         if r.find('tree/folder'):
-            return BoxFolder(self,r.find('tree/folder'))
+            return BoxFolder.FromAccountTree(self,r.find('tree/folder'))
         else:
             # Extract ZIP
             s = StringIO.StringIO(r.findtext('tree').decode('base64'))
             z = zipfile.ZipFile(s)
             z.read(z.infolist()[0])
-            return BoxFolder(self,ET.fromstring(z.read(z.infolist()[0])))
-
-    def create_folder(self,name,parent_id=0,share=0):
-        r = self.action('create_folder','create_ok',name=name,parent_id=parent_id,share=share)
-        return r
+            return BoxFolder.FromAccountTree(self,ET.fromstring(z.read(z.infolist()[0])))
 
     def download(self,id):
         try:
 
 class BoxFolder(object):
 
-    def __init__(self,connection,root):
+    def __init__(self,connection,id,name,path=None,user_id=None,description=None,shared=0,
+                      shared_link=None,permissions=None,tags=None,files=None,folders=None):
+        self.connection = connection
+        self.tags = tags or []
+        self.files = files or []
+        self.folders = folders or []
+        self.id = id
+        self.name = name 
+        self.path = path
+        self.user_id = user_id
+        self.description = description
+        self.shared = shared
+        self.shared_link = shared_link
+        self.permissions = permissions
+
+    @classmethod
+    def ParseCreateFolder(cls,root):
+        """ Parse output of create_folder api call - why is this different ?? """
         attrib = root.attrib
-        self.connection = connection
-        self.tags = []
-        self.files = []
-        self.folders = []
-        self.id = int(attrib['id'])
-        self.name = attrib['name']
+        id = int(attrib['folder_id'])
+        name = attrib['folder_name']
+        user_id = int(attrib['user_id'])
+        if attrib["shared"]:
+            shared = attrib['shared'] == "1"
+        else:
+            shared = False
+        return id,name,user_id,None,shared,None,None
+
+    @classmethod
+    def ParseAccountTree(cls,root):
+        attrib = root.attrib
+        id = int(attrib['id'])
+        name = attrib['name']
         try:
-            self.simple = False
-            self.user_id = int(attrib['user_id'])
-            self.description = attrib['description']
+            user_id = int(attrib['user_id'])
+            description = attrib['description']
             if attrib["shared"]:
-                self.shared = attrib['shared'] == "1"
+                shared = attrib['shared'] == "1"
             else:
-                self.shared = False
-            self.shared_link = attrib['shared_link']
-            self.permissions = attrib['permissions']
+                shared = False
+            shared_link = attrib['shared_link']
+            permissions = attrib['permissions']
         except KeyError:
             # 'Simple' listing
-            self.simple = True
-            pass
+            user_id = description = shared = shared_link = permissions = None
+        return id,name,user_id,description,shared,shared_link,permissions
+
+    @classmethod
+    def FromAccountTree(cls,connection,root):
+        params = BoxFolder.ParseAccountTree(root)
+        root_folder = cls(connection,*params)
         if root.find('folders') is not None:
             for folder in root.find('folders'):
-                self.folders.append(BoxFolder(connection,folder))
+                root_folder.folders.append(BoxFolder.FromAccountTree(connection,folder))
         if root.find('files') is not None:
             for file in root.find('files'):
-                self.files.append(BoxFile(connection,file))
+                root_folder.files.append(BoxFile(connection,file))
         if root.find('tags') is not None:
             for tag in root.find('tags'):
-                print "Folder:", self.name, "--", "Tag: ",tag
+                print "Folder:", params[1], "--", "Tag: ",tag
+        return root_folder
+
+    def create_folder(self,name,share=0):
+        r = self.connection.action('create_folder','create_ok',
+                            name=name,parent_id=self.id,share=share)
+        return BoxFolder(self.connection,r.find('folder'))
 
     def __str__(self):
         return "<BoxFolder: id=%d name='%s' files=%d folders=%d>" % (