From 745221f77c6994a9bd0e2d36c8fe8d49d02d736f Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Fri, 10 Aug 2012 16:23:18 -0400 Subject: [PATCH] cc-4105: added command line option parsing --- .../media-monitor2/media/monitor/events.py | 4 +-- .../media-monitor2/media/monitor/listeners.py | 30 +++++++++--------- python_apps/media-monitor2/mm1.99.sh | 4 +++ python_apps/media-monitor2/mm2.py | 31 ++++++++++++++----- 4 files changed, 46 insertions(+), 23 deletions(-) create mode 100755 python_apps/media-monitor2/mm1.99.sh diff --git a/python_apps/media-monitor2/media/monitor/events.py b/python_apps/media-monitor2/media/monitor/events.py index 5169ba086..9837b18e7 100644 --- a/python_apps/media-monitor2/media/monitor/events.py +++ b/python_apps/media-monitor2/media/monitor/events.py @@ -79,8 +79,8 @@ class BaseEvent(Loggable): # nothing to see here, please move along def morph_into(self, evt): """ - 'Morphing' should preserve the self.cookie invariant. I.e. either - None -> None or int -> int + 'Morphing' should preserve the self.cookie invariant. I.e. + either should either stay an integer or stay none. """ self.logger.info("Morphing '%s' into '%s'" % (self.__class__.__name__, evt.__class__.__name__)) diff --git a/python_apps/media-monitor2/media/monitor/listeners.py b/python_apps/media-monitor2/media/monitor/listeners.py index a0aecc443..fd490afba 100644 --- a/python_apps/media-monitor2/media/monitor/listeners.py +++ b/python_apps/media-monitor2/media/monitor/listeners.py @@ -10,23 +10,25 @@ from media.monitor.events import OrganizeFile, NewFile, MoveFile, DeleteFile, \ from media.monitor.log import Loggable, get_logger # We attempt to document a list of all special cases and hacks that the -# following classes should be able to handle. TODO : implement all of the -# following special cases +# following classes should be able to handle. TODO : implement all of +# the following special cases # -# properly as they only send a request for the dir and not for every file. Also -# more hacks are needed to check that the directory finished moving/copying? +# properly as they only send a request for the dir and not for every +# file. Also more hacks are needed to check that the directory finished +# moving/copying? # -# - In the case when a 'watched' directory's subdirectory is delete we should -# send a special request telling ApiController to delete a whole dir. This is -# done becasue pyinotify will not send an individual file delete event for -# every file in that directory +# - In the case when a 'watched' directory's subdirectory is delete we +# should send a special request telling ApiController to delete a whole +# dir. This is done becasue pyinotify will not send an individual file +# delete event for every file in that directory # -# - Special move events are required whenever a file is moved from a 'watched' -# directory into another 'watched' directory (or subdirectory). In this case we -# must identify the file by its md5 signature instead of it's filepath like we -# usually do. Maybe it's best to always identify a file based on its md5 -# signature?. Of course that's not possible for some modification events -# because the md5 signature will change... +# - Special move events are required whenever a file is moved +# from a 'watched' directory into another 'watched' directory (or +# subdirectory). In this case we must identify the file by its md5 +# signature instead of it's filepath like we usually do. Maybe it's +# best to always identify a file based on its md5 signature?. Of course +# that's not possible for some modification events because the md5 +# signature will change... # Note: Because of the way classes that inherit from pyinotify.ProcessEvent # interact with constructors. you should only instantiate objects from them diff --git a/python_apps/media-monitor2/mm1.99.sh b/python_apps/media-monitor2/mm1.99.sh new file mode 100755 index 000000000..7285bdf1b --- /dev/null +++ b/python_apps/media-monitor2/mm1.99.sh @@ -0,0 +1,4 @@ +#export PYTHONPATH="/home/rudi/Airtime/python_apps/:/home/rudi/Airtime/python_apps/media-monitor2/" +PYTHONPATH='/home/rudi/Airtime/python_apps/:/home/rudi/Airtime/python_apps/media-monitor2/' +export PYTHONPATH +python ./mm2.py --config="/home/rudi/Airtime/python_apps/media-monitor2/tests/live_client.cfg" --apiclient="/home/rudi/Airtime/python_apps/media-monitor2/tests/live_client.cfg" --log="/home/rudi/throwaway/mm2.log" diff --git a/python_apps/media-monitor2/mm2.py b/python_apps/media-monitor2/mm2.py index 82db9b42c..5d4e24b7a 100644 --- a/python_apps/media-monitor2/mm2.py +++ b/python_apps/media-monitor2/mm2.py @@ -36,7 +36,6 @@ def main(global_config, api_client_config): print(str(e)) logfile = unicode( config['logpath'] ) - setup_logging(logfile) log = get_logger() log.info("Attempting to set the locale...") @@ -96,10 +95,28 @@ def main(global_config, api_client_config): pyi = manager.pyinotify() pyi.loop() +__doc__ = """ +Usage: + mm2.py --config= --apiclient= --log= + +Options: + -h --help Show this screen + --config= path to mm2 config + --apiclient= path to apiclient config + --log= log at +""" + + #base_path = u'/home/rudi/Airtime/python_apps/media-monitor2/tests' + #global_config = os.path.join(base_path, u'live_client.cfg') + #api_client_config = global_config + if __name__ == '__main__': - # TODO : parse these from command line arguments - # TODO : add log config stuff - base_path = u'/home/rudi/Airtime/python_apps/media-monitor2/tests' - global_config = os.path.join(base_path, u'live_client.cfg') - api_client_config = global_config - main(global_config, api_client_config) + from docopt import docopt + args = docopt(__doc__,version="mm1.99") + for k in ['--apiclient','--config']: + if not os.path.exists(args[k]): + print("'%s' must exist" % args[k]) + sys.exit(0) + print("Running mm1.99") + setup_logging(args['--log']) + main(args['--config'],args['--apiclient'])