Skip to content

Commit

Permalink
[Issue #19] Use exception
Browse files Browse the repository at this point in the history
  • Loading branch information
llaumgui committed May 12, 2018
1 parent 1f68c8e commit be161a1
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 61 deletions.
39 changes: 38 additions & 1 deletion seedboxsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,26 @@

"""
Start CLI interface.
Exit code:
- 0: All is good
- 1:
- 2: Logging error
- 3: Lock error
- 4: Transfert error
- 5: Configuration error
- 6: Unsupported protocole
- 8: Dependency error
"""

from seedboxsync import CLI
try:
from seedboxsync.exceptions import (ConnectionException, ConfigurationException, DependencyException,
IsLockedException, LockException, LogException, TransportProtocoleException)
from seedboxsync.cli import CLI
from seedboxsync.helper import Helper
except DependencyException as exc:
print(str(exc))
exit(8)
import os
import sys

Expand All @@ -22,5 +39,25 @@
try:
if __name__ == '__main__':
cli = CLI()
except LogException as exc:
exit(2)
print(str(exc))
except LockException as exc:
exit(2)
print(str(exc))
except ConnectionException as exc:
Helper.log_print(str(exc), msg_type='error')
exit(4)
except ConfigurationException as exc:
Helper.log_print(str(exc), msg_type='error')
exit(5)
except TransportProtocoleException as exc:
print(str(exc))
exit(6)
except DependencyException as exc:
print(str(exc))
exit(8)
except IsLockedException:
exit(0)
except KeyboardInterrupt:
exit(0)
22 changes: 0 additions & 22 deletions seedboxsync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,3 @@
# For the full copyright and license information, please view the LICENSE
# file that was distributed with this source code.
#
# flake8: noqa

"""
The seedboxsync package with all SeedBoxSync modules.
Exit code:
- 0: All is good
- 1:
- 2: Logging error
- 3: Lock error
- 4: Transfert error
- 5: Configuration error
- 6: Unsupported protocole
- 7: Unsupported protocole module
- 8: Dependency error
"""

from .exceptions import DependencyException
from .helper import Helper, SeedboxDbHelper
from .seedboxsync import BlackHoleSync, DownloadSync, GetInfos
from .cli import CLI
from .transport import SeedboxAbstractClient
24 changes: 8 additions & 16 deletions seedboxsync/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
CLI module.
"""

from seedboxsync import (BlackHoleSync, DownloadSync, GetInfos, Helper)
from seedboxsync.seedboxsync import (BlackHoleSync, DownloadSync, GetInfos)
from seedboxsync.exceptions import (ConfigurationException, IsLockedException)
import argparse
import os
import configparser
Expand Down Expand Up @@ -48,15 +49,13 @@ def __init__(self):
# Optionnal arguments
self.__parser.add_argument('-q', '--quiet', action='store_true')

# Big try / except.
# Start but try / catch.
try:
self.__start()
except configparser.NoSectionError as exc:
Helper.log_print(str(exc), msg_type='error')
exit(5)
raise ConfigurationException(str(exc))
except configparser.NoOptionError as exc:
Helper.log_print(str(exc), msg_type='error')
exit(5)
raise ConfigurationException(str(exc))

def __start(self):
"""
Expand All @@ -66,38 +65,31 @@ def __start(self):
self.__args = self.__parser.parse_args()

# Set if quiet
os.environ["SEEDBOXSYNC_IS_QUIET"] = str(self.__args .quiet)
os.environ["SEEDBOXSYNC_IS_QUIET"] = str(self.__args.quiet)

if self.__args.blackhole:
self.__call_blackhole_sync()

elif self.__args.download:
self.__call_download_sync()

elif self.__args.lasts_torrents:
info = GetInfos()
print(info.get_lasts_torrents(self.__args.lasts_torrents))

elif self.__args.lasts_downloads:
info = GetInfos()
print(info.get_lasts_downloads(self.__args.lasts_downloads))

elif self.__args.unfinished_downloads:
info = GetInfos()
print(info.get_unfinished_downloads())

else:
self.__parser.print_help()

exit(0)

def __call_download_sync(self):
"""
Call DownloadSync method
"""
sync = DownloadSync()
if sync.is_locked():
exit(0)
raise IsLockedException("SeedboxSync is locked")
else:
sync.do_sync()

Expand All @@ -108,6 +100,6 @@ def __call_blackhole_sync(self):

sync = BlackHoleSync()
if sync.is_locked():
exit(0)
raise IsLockedException("SeedboxSync is locked")
else:
sync.do_sync()
24 changes: 24 additions & 0 deletions seedboxsync/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,29 @@
"""


class ConnectionException(Exception):
pass


class ConfigurationException(Exception):
pass


class DependencyException(Exception):
pass


class IsLockedException(Exception):
pass


class LockException(Exception):
pass


class LogException(Exception):
pass


class TransportProtocoleException(Exception):
pass
2 changes: 1 addition & 1 deletion seedboxsync/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Helper module with helper classes.
"""

from seedboxsync import DependencyException
from seedboxsync.exceptions import DependencyException
import logging
import os
import errno
Expand Down
31 changes: 11 additions & 20 deletions seedboxsync/seedboxsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
Main module of used by seedboxseed CLI.
"""

from seedboxsync import (Helper, SeedboxDbHelper, DependencyException)
from seedboxsync.exceptions import (ConnectionException, ConfigurationException, DependencyException, LockException, LogException, TransportProtocoleException)
from seedboxsync.helper import (Helper, SeedboxDbHelper)
from importlib import import_module
import configparser
import datetime
Expand Down Expand Up @@ -80,12 +81,11 @@ def __get_config_file(self):
if os.path.isfile(seedbox_ini):
config_file = seedbox_ini
break
except:
except Exception as exc:
pass

if config_file is None:
print('No configuration file found !')
exit(5)
raise ConfigurationException('No configuration file found !')

return config_file

Expand All @@ -102,8 +102,7 @@ def __setup_logging(self):
logging.debug('Start')
Helper.log_print('Load config from "' + self.__config_file + '"', msg_type='debug')
except Exception as exc:
Helper.log_print(str(exc), msg_type='error')
exit(2)
raise LogException('Log error: ' + exc)

def _lock(self):
"""
Expand All @@ -115,8 +114,7 @@ def _lock(self):
lock.write(str(os.getpid()))
lock.close()
except Exception as exc:
Helper.log_print(str(exc), msg_type='error')
exit(3)
raise LockException('Log error: ' + str(exc))

def _unlock(self):
"""
Expand All @@ -126,7 +124,7 @@ def _unlock(self):
try:
os.remove(self.__lock_file)
except Exception as exc:
Helper.log_print(str(exc), msg_type='error')
raise LockException('Log error: ' + str(exc))

def _check_pid(self, pid):
"""
Expand Down Expand Up @@ -165,31 +163,24 @@ def _get_transport_client(self):
try:
client_module = import_module('seedboxsync.transport_' + transfer_protocol)
except ImportError as exc:
Helper.log_print('Unsupported protocole: ' + transfer_protocol + ' (' + str(exc) + ')', msg_type='error')
self._unlock()
exit(6)
except DependencyException as exc:
Helper.log_print(str(exc), msg_type='error')
self._unlock()
exit(8)
raise TransportProtocoleException('Unsupported protocole: ' + transfer_protocol + ' (' + str(exc) + ')')

try:
transfer_client = getattr(client_module, client_class)
except AttributeError:
Helper.log_print('Unsupported protocole module ! No class "' +
client_class + '" in module ' + 'seedboxsync.transport_' + transfer_protocol, msg_type='error')
self._unlock()
exit(7)
raise TransportProtocoleException('Unsupported protocole module ! No class "' + client_class +
'" in module ' + 'seedboxsync.transport_' + transfer_protocol)

try:
return transfer_client(host=self._config.get('Seedbox', 'transfer_host'),
port=int(self._config.get('Seedbox', 'transfer_port')),
login=self._config.get('Seedbox', 'transfer_login'),
password=self._config.get('Seedbox', 'transfer_password'))
except Exception as exc:
Helper.log_print('Connection fail: ' + str(exc), msg_type='error')
self._unlock()
exit(4)
raise ConnectionException('Connection fail: ' + str(exc))

def _store_torrent_infos(self, torrent_path):
"""
Expand Down
3 changes: 2 additions & 1 deletion seedboxsync/transport_sftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
Transport client using sFTP protocol.
"""

from seedboxsync import SeedboxAbstractClient, DependencyException
from seedboxsync.transport import SeedboxAbstractClient
from seedboxsync.exceptions import DependencyException
from stat import S_ISDIR
import logging
import os
Expand Down

0 comments on commit be161a1

Please sign in to comment.