Skip to content

Commit

Permalink
#8 Rename and add Bases classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarizza committed May 20, 2019
1 parent 6277328 commit 6b025f8
Showing 1 changed file with 38 additions and 14 deletions.
52 changes: 38 additions & 14 deletions src/drosh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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')))

Expand Down

0 comments on commit 6b025f8

Please sign in to comment.