#!/usr/bin/env python # Copyright (c) 2009 Andreas Balogh # See LICENSE for details. """ new module template """ # system imports import logging import sys import os import getopt # local imports # constants # globals LOG = logging.getLogger() logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(levelname).3s %(process)d:%(thread)d %(message)s", datefmt="%H:%M:%S") # definitions class Usage(Exception): pass class Error(Exception): pass def main(argv = [__name__]): try: # check for parameters LOG.debug("starting '%s %s'", argv[0], " ".join(argv[1:])) script_name = os.path.basename(argv[0]) try: opts, args = getopt.getopt(argv[1:], "hfgp", \ ["help", "force", "gui", "preview"]) except getopt.error, err: raise Usage(err) LOG.debug("opts: %s, args: %s", opts, args) o_overwrite = False o_gui = False o_preview = False for o, a in opts: if o in ("-h", "--help"): usage(script_name) return 0 elif o in ("-f", "--force"): o_overwrite = True elif o in ("-p", "--preview"): o_preview = True elif o in ("-g", "--gui"): o_gui = True if len(args) == 2: src_dir = args[0] dest_dir = args[1] elif len(args) == 1 : src_dir = args[0] dest_dir = args[0] elif len(args) == 0 : src_dir = None dest_dir = None o_gui = True else: raise Usage("more than two arguments provided") # call method with appropriate arguments if src_dir and not os.path.exists(src_dir): raise Error("Source directory not found [%s], aborting" % (src_dir, )) if dest_dir and not os.path.exists(dest_dir): LOG.warn("Destination directory not found [%s]", dest_dir) if not o_preview: LOG.info("Creating destination directory [%s]", dest_dir) os.makedirs(dest_dir) if o_gui: gui(src_dir, dest_dir, o_overwrite) else: cli(src_dir, dest_dir, o_preview, o_overwrite) LOG.debug("Done.") return 0 except Error, err: LOG.error(err) return 1 except Usage, err: LOG.error(err) LOG.info("for usage use -h or --help") return 2 def gui(src_dir, dest_dir, o_overwrite): """ graphical user interface """ print src_dir, dest_dir, o_overwrite def cli(src_dir, dest_dir, o_preview, o_overwrite): """ command line interface """ print src_dir, dest_dir, o_preview, o_overwrite def usage(script_name): print print "usage: %s [options] [src_dir [dest_dir]]" % (script_name,) print """ src_dir source directory to search for MOD/MOI dest_dir destination directory for MPG files options: -h, --help show this help message and exit -f, --force override files with same name in destination directory -g, --gui force interactive mode -p, --preview preview only, don't copy, don't create non-existent directories """ if __name__ == "__main__": sys.exit(main(sys.argv))