def write_file(self, file_path, content, callback=None): """write contents to the file""" if callback: RESTClient.set_callback(callback) return self.db_client.put_file(file_path, content.encode('utf-8'), overwrite=True, )
def read_file(self, file_path, callback): """read contents of the file""" if not callback: raise ValueError("read_file: need callback !") yield_key = object() RESTClient.set_callback((yield gen.Callback(yield_key))) self.db_client.get_file(file_path) f = yield gen.Wait(yield_key) content = f.body if not content: l.warn("%s has no content ?!" % (file_path, )) content = u'' # Convert to utf-8 ! elif type(content) != unicode: # Find out file encoding from REST headers db_enc = f.headers['Content-Type'] l.info("read %s from dropbox. encoding '%s'" %\ (file_path, db_enc)) if 'charset' in db_enc: # Text file, decode to unicode as early as possible py_enc = db_enc[db_enc.find('charset=')+8:] # TODO: maybe leave errors='strict' and act on exception ? content = content.decode(py_enc) # Doesn't work in python 2.6 !, errors='ignore') else: # Otherwise we treat the file as binary and keep it as it is if os.path.splitext(file_path)[1] in settings.SUPPORTED_EXTS: # Shouldn't get here l.error("%s doesn't have text encoding (%s) ?!" % \ (file_path, db_enc)) callback(content)
def get_account_info(self, callback): """returns user account information""" if not callback: raise ValueError("get_account_info: need callback !") yield_key = object() RESTClient.set_callback((yield gen.Callback(yield_key))) self.db_client.account_info() resp = yield gen.Wait(yield_key) callback(resp)
def create_folder(self, path, callback): """rename file or move directory on dropbox""" if not callback: raise ValueError("create_folder: need callback !") yield_key = object() RESTClient.set_callback((yield gen.Callback(yield_key))) self.db_client.file_create_folder(path) resp = yield gen.Wait(yield_key) callback(resp)
def move_file(self, from_path, to_path, callback): """rename file or move directory on dropbox""" if not callback: raise ValueError("move_file: need callback !") yield_key = object() RESTClient.set_callback((yield gen.Callback(yield_key))) self.db_client.file_move(from_path, to_path) resp = yield gen.Wait(yield_key) callback(resp)
def delete_file(self, file_path, callback): """remove file or directory from dropbox""" if not callback: raise ValueError("delete_file: need callback !") yield_key = object() RESTClient.set_callback((yield gen.Callback(yield_key))) self.db_client.file_delete(file_path) resp = yield gen.Wait(yield_key) callback(resp)
def write_file(self, file_path, content, callback): """write contents to the file""" if not callback: raise ValueError("write_file: need callback !") yield_key = object() RESTClient.set_callback((yield gen.Callback(yield_key))) self.db_client.put_file(file_path, content.encode('utf-8'), overwrite=True) resp = yield gen.Wait(yield_key) callback(resp)
def get_files_as_dicts(self, dir_path, callback=None): """ Get list files in dropbox directory as a list of dicts """ if callback: RESTClient.set_callback(callback) resp = None try: resp = self.db_client.metadata(dir_path) except rest.ErrorResponse, e: l.error("get_files_as_dicts: metadata error for %s: %s" % \ (dir_path, e))
def dir_changed(self, dir_name, hash, callback=None): """ Returns True and new_hash if dir in dropbox has new hash (was modified) False, None otherwise """ if callback: RESTClient.set_callback(callback) resp = None try: resp = self.db_client.metadata(dir_name, hash=hash) except rest.ErrorResponse, e: # No changes in dir if e.status == 304: return None
def get_files_as_dicts(self, dir_path, callback): """ Get list files in dropbox directory as a list of dicts """ if not callback: raise ValueError("get_files_as_dicts: need callback !") yield_key = object() RESTClient.set_callback((yield gen.Callback(yield_key))) resp = None try: self.db_client.metadata(dir_path) resp = yield gen.Wait(yield_key) except rest.ErrorResponse, e: l.error("get_files_as_dicts: metadata error for %s: %s" % \ (dir_path, e))
def dir_changed(self, dir_name, hash, callback): """ Returns True and new_hash if dir in dropbox has new hash (was modified) False, None otherwise """ if not callback: raise ValueError("dir_changed: need callback !") yield_key = object() RESTClient.set_callback((yield gen.Callback(yield_key))) resp = None try: self.db_client.metadata(dir_name, hash=hash) resp = yield gen.Wait(yield_key) except ErrorResponse, e: # No changes in dir if e.status == 304: callback(None)
def read_file(self, file_path, callback=None): """read contents of the file""" if callback: RESTClient.set_callback(callback) f = self.db_client.get_file(file_path) content = f.read() # Convert to utf-8 ! if type(content) != unicode: # Find out file encoding from REST headers db_enc = f.msg['content-type'] l.info("read %s from dropbox. encoding '%s'" %\ (file_path, db_enc)) if 'charset' in db_enc: # Text file, decode to unicode as early as possible py_enc = db_enc[db_enc.find('charset=')+8:] # TODO: maybe leave errors='strict' and act on exception ? content = content.decode(py_enc) # Doesn't work in python 2.6 !, errors='ignore') else: # Otherwise we treat the file as binary and keep it as it is if os.path.splitext(file_path)[1] in settings.SUPPORTED_EXTS: # Shouldn't get here l.error("%s doesn't have text encoding (%s) ?!" % \ (file_path, db_enc)) return content
def get_account_info(self, callback=None): """returns user account information""" if callback: RESTClient.set_callback(callback) return self.db_client.account_info()