diff --git a/src/drosh.py b/src/drosh.py index 8124412..26d2bd3 100755 --- a/src/drosh.py +++ b/src/drosh.py @@ -22,35 +22,61 @@ logger = logging.getLogger(__name__) -class Screenshot(object): +class NotImplementedError(Exception): + pass + + +class BaseNotifier(object): + def notify(self, message): + raise NotImplementedError() + + +class BaseUploader(object): def __init__(self, path): - file_on_dropbox = self.upload_file(path) - url = self.create_shared_link(file_on_dropbox) + self.notifier = self.configure_notifier() + file_on_server = self.upload_file(path) + url = self.create_shared_link(file_on_server) if url is None: - self.notify('Drosh', 'Error in create shared link') + self.notify('Drosh', 'Error to get link') else: self.notify('Drosh', url) + def configure_notifier(self): + return BasicNotifier() + + def notify(self, *args, **kwargs): + raise NotImplementedError() + + def upload_file(self, path): + raise NotImplementedError() + + def create_shared_link(self, path): + raise NotImplementedError() + +class BasicNotifier(BaseNotifier): def notify(self, title, body): - """ - Send a desktop message - """ try: subprocess.call(['notify-send', title, body]) except FileNotFoundError: # Try with kdialog subprocess.run(['kdialog', '--title', title, '--passivepopup', body, '5']) + +class DropboxHandler(BaseUploader): + + def __init__(self, *args, **kwargs): + super(DropboxHandler, self).__init__(*args, **kwargs) + self.dbox = DropboxAPI() + def upload_file(self, path): """ Upload file """ - dbox = Drosh() path_local = os.path.join(DROSH_SCREENSHOT_FOLDER, os.path.basename(path.decode('utf8'))) path_remote = os.path.join(DROSH_DROPBOX_FOLDER, os.path.basename(path.decode('utf8'))) - result = dbox.files_upload(path_local, path_remote) + result = self.dbox.files_upload(path_local, path_remote) if result is not None: logger.debug('File %r uploaded successfully', result.path_display) @@ -64,9 +90,7 @@ def create_shared_link(self, path): Create shared link """ - dbox = Drosh() - - result = dbox.create_shared_link(path) + result = self.dbox.create_shared_link(path) if result is not None: logger.debug('Shared link created %r for %r file', result.url, path) try: @@ -78,7 +102,7 @@ def create_shared_link(self, path): logger.debug('Error in create shared link for %r file', path) -class Drosh(object): +class DropboxAPI(object): def __init__(self): self.client = dropbox.dropbox.Dropbox(DROSH_DROPBOX_TOKEN, timeout=30.0) @@ -169,7 +193,7 @@ def main(): header.wd, header.mask, header.cookie, header.len, type_names, watch_path.decode('utf-8'), filename.decode('utf-8')) logger.info('Upload and create shared link for: %r', filename) - Screenshot(filename) + DropboxHandler(filename) finally: i.remove_watch(bytes(DROSH_SCREENSHOT_FOLDER.encode('utf8')))