Skip to content

Instantly share code, notes, and snippets.

@joe42
Last active December 28, 2015 23:59
Show Gist options
  • Save joe42/7583086 to your computer and use it in GitHub Desktop.
Save joe42/7583086 to your computer and use it in GitHub Desktop.
Add Feature Issue #10, make syncfolders available in root directory Add method get_syncfolders to class SugarsyncClient, to retrieve a dictionary with available directories as keys and the id used to address them in the sugarsync api as value Modify methods get_directory_listing, __init__, _translate_path, and get_metadata of class SugarsyncSto…
From 65b88450fbdab6d83365ab06163d1f57f2b95209 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Johannes=20M=C3=BCller?= <dna42@gmx.de>
Date: Thu, 21 Nov 2013 15:58:25 +0100
Subject: [PATCH] Add Feature Issue #10, make syncfolders available in root
directory Add method get_syncfolders to class
SugarsyncClient, to retrieve a dictionary with available
directories as keys and the id used to address them in the
sugarsync api as value Modify methods
get_directory_listing, __init__, _translate_path, and
get_metadata of class SugarsyncStore, to represent the
syncfolders inside the root directory
---
cloudfusion/store/sugarsync/client.py | 19 +++++++++++++++++++
cloudfusion/store/sugarsync/sugarsync_store.py | 19 +++++++++++++++++--
2 files changed, 36 insertions(+), 2 deletions(-)
diff --git a/cloudfusion/store/sugarsync/client.py b/cloudfusion/store/sugarsync/client.py
index 139214f..8261265 100644
--- a/cloudfusion/store/sugarsync/client.py
+++ b/cloudfusion/store/sugarsync/client.py
@@ -5,6 +5,7 @@ import httplib2, requests
from cloudfusion.util.xmlparser import DictXMLParser
from cloudfusion.util.string import *
import base64
+import xml.dom.minidom
#make thread safe by adding connection creation to every method call
@@ -43,6 +44,24 @@ class SugarsyncClient(object):
ret = HTTPResponse( response, content )
return ret
+ def get_syncfolders(self):
+ headers = {"Host": self.host, "Authorization: ": self.token}
+ conn = httplib2.Http(timeout=30)
+ response, content = conn.request("https://"+self.host+"/user/%s/folders/contents" % self.uid,"GET",None,headers)
+ http_response = HTTPResponse( response, content )
+ dom_response = xml.dom.minidom.parseString(http_response.data)
+ dom_collections = dom_response.getElementsByTagName('collection')
+ ret = {}
+ for dom_collection in dom_collections:
+ partial_tree = {"collection": {"displayName": "", "contents": ""}}
+ DictXMLParser().populate_dict_with_XML_leaf_textnodes(str(dom_collection.toxml()), partial_tree)
+ #https://api.sugarsync.com/folder/:sc:7585140:36733670_13234/contents
+ user_id = regSearchString(".*:sc:"+self.uid+":(.*)/.*", partial_tree['collection']['contents'])
+ displayname = partial_tree['collection']['displayName']
+ ret[displayname] = user_id
+ return ret
+
+
def user_info(self):
headers = {"Host": self.host, "Authorization: ": self.token}
conn = httplib2.Http(timeout=30)
diff --git a/cloudfusion/store/sugarsync/sugarsync_store.py b/cloudfusion/store/sugarsync/sugarsync_store.py
index 0b1c2d3..be5dff0 100644
--- a/cloudfusion/store/sugarsync/sugarsync_store.py
+++ b/cloudfusion/store/sugarsync/sugarsync_store.py
@@ -127,6 +127,10 @@ class SugarsyncStore(Store):
raise StoreAutorizationError(repr(e), 0)
self.time_difference = self._get_time_difference()
self.logger.debug("sugarsync store initialized")
+ self.root_folders = {}
+ syncfolders = self.client.get_syncfolders()
+ for folder in syncfolders.keys():
+ self.root_folders['/'+folder] = syncfolders[folder]
super(SugarsyncStore, self).__init__()
def get_name(self):
@@ -154,7 +158,9 @@ class SugarsyncStore(Store):
path = to_unicode( path, "utf8")
if path in self.path_cache:
return self.path_cache[path]
- if path == "/":
+ if path in self.root_folders.keys():
+ return self.root_folders[path]
+ elif path == "/":
return self.root
else:
parent_dir = to_unicode( os.path.dirname(path), "utf8")
@@ -356,6 +362,8 @@ class SugarsyncStore(Store):
def get_directory_listing(self, directory):
self.logger.debug("getting directory listing for %s", directory)
ret = []
+ if directory == "/":
+ return self.root_folders.keys()
translated_dir = self._translate_path(directory)
collection = self._parse_collection(translated_dir)
if directory[-1] != "/":
@@ -475,7 +483,14 @@ class SugarsyncStore(Store):
ret["modified"] = time.time()
ret["path"] = "/"
ret["is_dir"] = True
- return ret;
+ return ret
+ if path in self.root_folders.keys():
+ ret = {}
+ ret["bytes"] = 0
+ ret["modified"] = time.time()
+ ret["path"] = self.root_folders[path]
+ ret["is_dir"] = True
+ return ret
if path[-1] == "/":
path = path[0:-1]
is_file = True
--
1.7.9.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment