def __init__(self, root, config, client): """ :param root: the root path for this provider :param config: the dict of the configuration of the object :param plugin_config: contains the plugin settings """ if len(root) > 0 and root[0] == '/': root = root[1:] self.root = root self.connection = client.get("box_com_connection") self.access_token = self.connection['access_token'] self.cache_enabled = config.get("cache_enabled") if self.cache_enabled: cache_file_name = hashlib.sha1(self.access_token.encode('utf-8')).hexdigest() else: cache_file_name = None auth = OAuth2( client_id="", client_secret="", access_token=self.access_token ) main_session = AuthorizedSession(auth, network_layer=LessVerboseLoggingNetwork()) self.client = Client(auth, main_session) self.user = self.client.user().get() self.box_item = BoxItem(cache_file_name, root, self.client) self.box_item.check_path_format(get_normalized_path(root))
def get_as_browse(self): return { 'fullPath': get_normalized_path(self.path), 'exists': self.exists(), 'directory': self.is_folder(), 'size': self.size, 'lastModified': self.get_last_modified() }
def get_stat(self): ret = { 'path': get_normalized_path(self.path), 'size': self.size if self.is_file() else 0, 'isDirectory': self.is_folder() } if self.modified_at is not None: ret["lastModified"] = self.modified_at return ret
def get_new_dir(title): """ Returns always new directory path. """ name = TEST_REPO_PREFIX if title: name = '-'.join((name, title)) hex = hashlib.sha1(str(time.time())).hexdigest() name = '-'.join((name, hex)) path = os.path.join(TEST_DIR, name) return get_normalized_path(path)
def browse(self, path): """ List the file or directory at the given path, and its children (if directory) """ normalized_path = get_normalized_path(path) full_path = get_full_path(self.root, path) item = self.box_item.get_by_path(get_rel_path(full_path)) if item.not_exists(): return {'fullPath' : normalized_path, 'exists' : False} if item.is_folder(): return {'fullPath' : normalized_path, 'exists' : True, 'directory' : True, 'children' : item.get_children(normalized_path), 'lastModified' : item.get_last_modified()} else: return item.get_as_browse()
def get_children(self, internal_path): full_path = get_full_path(self.root, self.path) intra_path = self.path.replace('/' + self.root, '') children = [] for sub in self.client.folder(self.id).get_items( fields=['modified_at', 'name', 'type', 'size']): sub_path = get_normalized_path( os.path.join(internal_path, sub.name)) ret = { 'fullPath': sub_path, 'exists': True, 'directory': sub.type == self.BOX_FOLDER, 'size': sub.size, 'lastModified': self.get_last_modified(sub) } children.append(ret) self.cache.add(get_rel_path(sub.name), sub.id, sub.type) return children
def enumerate(self, path, first_non_empty): """ Enumerate files recursively from prefix. If first_non_empty, stop at the first non-empty file. If the prefix doesn't denote a file or folder, return None """ full_path = get_full_path(self.root, path) normalized_path = get_normalized_path(path) item = self.box_item.get_by_path(full_path) if item.not_exists(): return None paths = [] if item.is_folder(): paths = self.list_recursive(normalized_path, item.id, first_non_empty) else: paths.append({'path':normalized_path.split("/")[-1], 'size':item.size, 'lastModified':int(0) * 1000}) return paths