Skip to content

Commit

Permalink
Refactored handlers and provided a base class
Browse files Browse the repository at this point in the history
  • Loading branch information
AadityaNair committed Jul 1, 2018
1 parent cf7b709 commit 92d248e
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 65 deletions.
4 changes: 4 additions & 0 deletions ProjectNephos/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ class OAuthFailure(NephosException):

class FileNotFound(NephosException):
pass


class SubCommandNotFound(NephosException):
pass
70 changes: 70 additions & 0 deletions ProjectNephos/handlers/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from argparse import _SubParsersAction, Namespace

from ProjectNephos.config import Configuration
from ProjectNephos.exceptions import SubCommandNotFound

from logging import getLogger


logger = getLogger(__name__)


class BaseHandler(object):
"""
This is the base class for all handlers. All common
functions are defined here.
"""

def __init__(self, subcommand: str = None, config: Configuration = None):
"""
Initialise the handler. This generally takes the subcommand name when calling
nephos from the command line. Config can also be provided in which case it will
initialize config as well.
"""
self.subcommand = subcommand
self.config = config

if self.config:
self.init_with_config(self.config)

def init_with_config(self, config):
"""
Config is generally not available during the initialisation phase. This command is
to be used in such a case.
"""
if self.config != config and self.config is not None:
logger.warning(
"The class is already initalised with an config. Reinitializing with another one"
)
self.config = config

def init_args(self, subparser: _SubParsersAction):
"""
This command takes the subparser from the calling code and initialises the
command line arguments for this function. This function will not be called when
the handler isn't called from the command line.
"""
if self.subcommand is None:
raise SubCommandNotFound(
"Subcommand has not been supplied. Cannot init_args without a subcommand"
)
return subparser.add_parser(self.subcommand)

def execute_command(self):
"""
Actually execute the functionality of the handler. The signature of the function
will be different for each handler. This will be the function called when handler
is not initiated from the command line.
"""
raise NotImplementedError

def run(self, args: Namespace):
"""
This command is only used in the case of command line invocation. This will recieve
the parsed command-line arguments and perform logic on them. For the most part, it
will use the arguments to directly call the execute_command function and pretty print
its output.
Its signature will not change across handlers.
"""
raise NotImplementedError
15 changes: 6 additions & 9 deletions ProjectNephos/handlers/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@

from ProjectNephos.config import Configuration
from ProjectNephos.backends import DBStorage
from ProjectNephos.handlers.base import BaseHandler

logger = getLogger(__name__)


class ChannelHandler(object):
class ChannelHandler(BaseHandler):
"""
Handles creating channels.
"""

def __init__(self, subcommand):
self.subcommand = subcommand

def init_with_config(self, config: Configuration):
self.config = config
super().init_with_config(config)

self.db = DBStorage(config)

def init_args(self, subparser: _SubParsersAction):
parser = subparser.add_parser(self.subcommand)
parser = super().init_args(subparser)

parser.add_argument(
"action", help="Define what action to take.", choices=["list", "add"]
)
Expand All @@ -36,9 +36,6 @@ def init_args(self, subparser: _SubParsersAction):
parser.add_argument("--language", action="store", help="Language of the stream")
parser.add_argument("--source", action="store", help="Source of the video.")

def execute_command(self):
pass

def run(self, args: Namespace):
if args.action == "add":
if not args.name:
Expand Down
15 changes: 4 additions & 11 deletions ProjectNephos/handlers/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
BASE_FOLDER,
default_values,
)
from ProjectNephos.handlers.base import BaseHandler
from ProjectNephos.orchestration import Server
from argparse import _SubParsersAction, Namespace
from logging import getLogger
Expand All @@ -16,27 +17,19 @@
logger = getLogger(__name__)


class InitHandler(object):
class InitHandler(BaseHandler):
"""
Handles startup of the project. This probably needs to be run just once.
This performs the following tasks:
1. Creates all the config files and stuff.
2. Performs OAuth with google.
3. Starts an orchestration server that ensures that the recorder is always running.
and other stuff not currently decided.
TODO: Passing config to this handler should override create config option.
NOTE: 1 and 2 are being handled separately as of now. They will be moved here later.
NOTE: 2 is being handled separately as of now. It will be moved here later.
"""

def __init__(self, subcommand: str):
self.subcommand = subcommand

def init_with_config(self, config: Configuration):
raise NotImplementedError

def init_args(self, subparser: _SubParsersAction):
subparser.add_parser(self.subcommand)

def _create_config(self, config_path):
if config_path == CONFIG_FULL_PATH_DEFAULT:
logger.debug("Default config path is being used")
Expand Down
16 changes: 7 additions & 9 deletions ProjectNephos/handlers/permissions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ProjectNephos.backends import DriveStorage, DBStorage
from ProjectNephos.config import Configuration
from ProjectNephos.handlers.base import BaseHandler
from ProjectNephos.handlers.search import SearchHandler

from argparse import _SubParsersAction, Namespace
Expand All @@ -8,7 +9,7 @@
logger = getLogger(__name__)


class PermissionHandler(object):
class PermissionHandler(BaseHandler):
"""
This class handles adding permissions to uploaded files.
Expand All @@ -19,19 +20,16 @@ class PermissionHandler(object):
NOTE: Mechanism to tag file while uploading has not yet been implemented.
"""

def __init__(self, subcommand: str):
self.subcommand = subcommand

def init_with_config(self, config: Configuration):
self.config = config
super().init_with_config(config)

self.backend = DriveStorage(config)
self.db = DBStorage(config)

self.search = SearchHandler("search")
self.search.init_with_config(config)
self.search = SearchHandler(config=config)

def init_args(self, subparser: _SubParsersAction) -> None:
parser = subparser.add_parser(self.subcommand)
parser = super().init_args(subparser)

parser.add_argument(
"action", help="Define what action to take.", choices=["list", "add"]
)
Expand Down
19 changes: 6 additions & 13 deletions ProjectNephos/handlers/process.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from ProjectNephos.config import Configuration
from ProjectNephos.exceptions import FileNotFound
from ProjectNephos.handlers.base import BaseHandler

from argparse import _SubParsersAction, Namespace
from logging import getLogger
from os.path import isfile
import ffmpy


logger = getLogger(__name__)


class ProcessHandler(object):
class ProcessHandler(BaseHandler):
"""
This class handles conversion of downloaded data. The recorded data will most likely
be compressed to save space. The only functionality this class provides as of now is to
Expand All @@ -20,14 +21,8 @@ class ProcessHandler(object):
sure to name it correctly
"""

def __init__(self, subcommand: str):
self.subcommand = subcommand

def init_with_config(self, config: Configuration):
self.config = config

def init_args(self, subparser: _SubParsersAction):
parser = subparser.add_parser(self.subcommand)
parser = super().init_args(subparser)

parser.add_argument(
"input_file", action="store", help="The file that has to be transformed"
Expand All @@ -47,7 +42,5 @@ def run(self, args: Namespace):
ff.run()

def execute_command(self, input_file, output_file):
ff = ffmpy.FFmpeg(
inputs={input_file: None}, outputs={output_file: None}
)
ff.run()
ff = ffmpy.FFmpeg(inputs={input_file: None}, outputs={output_file: None})
ff.run()
13 changes: 7 additions & 6 deletions ProjectNephos/handlers/search.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from ProjectNephos.backends import DriveStorage
from ProjectNephos.config import Configuration
from ProjectNephos.handlers.base import BaseHandler

from argparse import _SubParsersAction, Namespace
from logging import getLogger


logger = getLogger(__name__)


class SearchHandler(object):
class SearchHandler(BaseHandler):
"""
This class does the searching. You provide it with search parameters and
it find all matching files and their corresponding ids.
Expand All @@ -26,15 +28,14 @@ class SearchHandler(object):
More fine-grained controls will be added later.
"""

def __init__(self, subcommand: str):
self.subcommand = subcommand

def init_with_config(self, config: Configuration):
self.config = config
super().init_with_config(config)

self.backend = DriveStorage(config)

def init_args(self, subparser: _SubParsersAction) -> None:
parser = subparser.add_parser(self.subcommand)
parser = super().init_args(subparser)

parser.add_argument("--name", action="store", help="Search for a name")
parser.add_argument(
"--tags", action="store", nargs="*", help="Tags you want to search for"
Expand Down
16 changes: 7 additions & 9 deletions ProjectNephos/handlers/tag.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ProjectNephos.backends import DriveStorage
from ProjectNephos.config import Configuration
from ProjectNephos.handlers.base import BaseHandler
from ProjectNephos.handlers.search import SearchHandler

from argparse import _SubParsersAction, Namespace
Expand All @@ -8,7 +9,7 @@
logger = getLogger(__name__)


class TagHandler(object):
class TagHandler(BaseHandler):
"""
This class is used to tag *uploaded* files. Tags currently are just strings
that are added in the file's description field.
Expand All @@ -21,18 +22,15 @@ class TagHandler(object):
NOTE: Mechanism to tag file while uploading has not yet been implemented.
"""

def __init__(self, subcommand: str):
self.subcommand = subcommand

def init_with_config(self, config: Configuration):
self.config = config
self.backend = DriveStorage(config)
super().init_with_config(config)

self.search = SearchHandler("search")
self.search.init_with_config(config)
self.backend = DriveStorage(config)
self.search = SearchHandler(config=config)

def init_args(self, subparser: _SubParsersAction) -> None:
parser = subparser.add_parser(self.subcommand)
parser = super().init_args(subparser)

parser.add_argument(
"--for_name",
action="store",
Expand Down
18 changes: 10 additions & 8 deletions ProjectNephos/handlers/upload.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
from ProjectNephos.backends import DriveStorage
from ProjectNephos.config import Configuration
from ProjectNephos.handlers.base import BaseHandler

from argparse import _SubParsersAction, Namespace
from logging import getLogger
from os import path


logger = getLogger(__name__)


class UploadHandler(object):
class UploadHandler(BaseHandler):
"""
Upload files to google drive. Really, that's it. You supply it a file or
a list of files and this will upload it for you. Raises errors on non-existant
files based on the --ignore-errors argument.
"""

def __init__(self, subcommand: str):
self.subcommand = subcommand
def init_with_config(self, config: Configuration):
super().init_with_config(config)

self.backend = DriveStorage(config)

def init_args(self, subparser: _SubParsersAction) -> None:
parser = subparser.add_parser(self.subcommand)
parser.add_argument("files", nargs="+", help="Files you want to upload.")
parser = super().init_args(subparser)

def init_with_config(self, config: Configuration):
self.config = config
self.backend = DriveStorage(config)
parser.add_argument("files", nargs="+", help="Files you want to upload.")

def execute_command(self, filepath):
return self.backend.write(filepath)
Expand Down

0 comments on commit 92d248e

Please sign in to comment.