Example #1
0
    def parseArgs(self):
        """
        Uses OptionParser to parse out input
        Jelly.py <stage> <protocol>
        """
        parser = OptionParser(USAGE)
        parser.remove_option("-h")
        parser.add_option("-h", "--help", action="store_true", default=False)
        
        parser.add_option("--debug",action="store_true",default=False)
        parser.add_option("-x", dest="extras", type="string", default="", 
                help="-x \"<options>\" are options to pass into the stage you're running")
        
        self.options, args = parser.parse_args()

        if self.options.help == True:
            if len(args) == 1:
                if args[0] in STAGES:
                    print exe(Stages.PRINT_HELPS[args[0]])[1]
                    sys.exit(0)
                #Else, this will drop down to the next parser.error
            else:
                print parser.format_help()
                sys.exit(0)
        if len(args) != 2 or args[0] not in STAGES:
            parser.error("Invalid Arguments. Expected one of\n'%s'" % "', '".join(STAGES))
            sys.exit(1)
        self.executeStage = args[0]
        self.protocolName = os.path.abspath(args[1])
Example #2
0
    def searchArgs(self, args):
        parser = OptionParser()

        parser.add_option('-a', '--artist', dest='artist')
        parser.add_option('-s', '--song', dest='song')
        parser.add_option('-b', '--album', dest='album')

        search_by = None

        for name in ('artist', 'song'):
            if args[0] == name:
                parser.remove_option('--%s' % name)
                search_by = args.pop(0)
                kwargs = {}
                kwargs[name] = args.pop(0)
                parser.set_defaults(**kwargs)
                break

        if not search_by:
            print "Don't understand"
            return

        (options, args) = parser.parse_args(args=args)

        lst = self.getList(search_by, options)
        self.cli.cur_list = lst
        print lst
Example #3
0
File: util.py Project: wadere/mrjob
def parse_and_save_options(option_parser, args):
    """Return a map from option name (``dest``) to a list of the arguments
    in *args* that correspond to that *dest*.

    This won't modify *option_parser*.
    """
    arg_map = {}

    def sim_callback(option, opt_str, value, parser):
        dest = option.dest
        arg_map.setdefault(dest, [])

        arg_map[dest].append(opt_str)
        if isinstance(value, string_types):
            arg_map[dest].append(value)
        elif value:
            arg_map[dest].extend(value)

    sim_parser = OptionParser()
    sim_parser.remove_option('-h')

    # optparse is no longer being maintained, so it's safe to access
    # hidden methods and attributes
    for option in option_parser._get_all_options():
        sim_parser.add_option(*(option._short_opts + option._long_opts),
                              dest=option.dest,
                              nargs=option.nargs,
                              action='callback',
                              type=('string' if option.type else None),
                              callback=sim_callback)

    sim_parser.parse_args(args)

    return arg_map
class TestOptionParser(BaseTest):
    def setUp(self):
        self.parser = OptionParser()
        self.parser.add_option("-v", "--verbose", "-n", "--noisy",
                          action="store_true", dest="verbose")
        self.parser.add_option("-q", "--quiet", "--silent",
                          action="store_false", dest="verbose")

    def test_add_option_no_Option(self):
        self.assertTypeError(self.parser.add_option,
                             "not an Option instance: None", None)

    def test_add_option_invalid_arguments(self):
        self.assertTypeError(self.parser.add_option,
                             "invalid arguments", None, None)

    def test_get_option(self):
        opt1 = self.parser.get_option("-v")
        self.assert_(isinstance(opt1, Option))
        self.assertEqual(opt1._short_opts, ["-v", "-n"])
        self.assertEqual(opt1._long_opts, ["--verbose", "--noisy"])
        self.assertEqual(opt1.action, "store_true")
        self.assertEqual(opt1.dest, "verbose")

    def test_get_option_equals(self):
        opt1 = self.parser.get_option("-v")
        opt2 = self.parser.get_option("--verbose")
        opt3 = self.parser.get_option("-n")
        opt4 = self.parser.get_option("--noisy")
        self.assert_(opt1 is opt2 is opt3 is opt4)

    def test_has_option(self):
        self.assert_(self.parser.has_option("-v"))
        self.assert_(self.parser.has_option("--verbose"))

    def assert_removed(self):
        self.assert_(self.parser.get_option("-v") is None)
        self.assert_(self.parser.get_option("--verbose") is None)
        self.assert_(self.parser.get_option("-n") is None)
        self.assert_(self.parser.get_option("--noisy") is None)

        self.failIf(self.parser.has_option("-v"))
        self.failIf(self.parser.has_option("--verbose"))
        self.failIf(self.parser.has_option("-n"))
        self.failIf(self.parser.has_option("--noisy"))

        self.assert_(self.parser.has_option("-q"))
        self.assert_(self.parser.has_option("--silent"))

    def test_remove_short_opt(self):
        self.parser.remove_option("-n")
        self.assert_removed()

    def test_remove_long_opt(self):
        self.parser.remove_option("--verbose")
        self.assert_removed()

    def test_remove_nonexistent(self):
        self.assertRaises(self.parser.remove_option, ValueError,
                          "no such option 'foo'", funcargs=['foo'])
Example #5
0
def parseargs( help=False ):
    parser = OptionParser()
    parser.remove_option('-h')

    parser.add_option( '-h', '--help', '-?', action='help', help='show this message and exit')
    # --complexity
    parser.add_option( '--complexity', metavar="<Number>", type=int, default=1,
        help='The complextiy number determines the number of operators in the query. With complexity 1, you get ~10 memory guzzling operators. With 2, the number of operators ~20 and so on.')
    # --concurrency
    parser.add_option( '--concurrency', metavar='<Number>', type=int, default=1,
        help='Number of concurrent session from which to execute the queries')
    # --dbname
    parser.add_option( '--dbname', metavar='<dbname>', default='gptest',
        help='Database where Tpch tables are created and data loaded.')
    # --username
    parser.add_option( '--username', metavar='<username>', default='gpadmin',
        help='Database username to use while executing the query')
    options, args = parser.parse_args()

    try:
        if sys.argv[1:].__len__() == 0:
            parser.print_help()
            raise Exception( "\nERROR: You did not provide any arguments \n\n" )
        if options.complexity <= 0:
            parser.print_help()
            raise Exception("\nERROR: You must provide positive value for complexity ! \n\n")
        if options.concurrency <= 0:
            parser.print_help()
            raise Exception("\nERROR: You must provide positive value for concurrency ! \n\n")
    except Exception, e:
        print e
        sys.exit(-1)
Example #6
0
    def parseArgs(self):
        """
        Uses OptionParser to parse out input
        Jelly.py <stage> <protocol>
        """
        parser = OptionParser(USAGE)
        parser.remove_option("-h")
        parser.add_option("-h", "--help", action="store_true", default=False)
        
        parser.add_option("--debug",action="store_true",default=False)
        parser.add_option("-x", dest="extras", type="string", default="", 
                help="-x \"<options>\" are options to pass into the stage you're running")
        
        self.options, args = parser.parse_args()

        if self.options.help == True:
            if len(args) == 1:
                if args[0] in STAGES:
                    print exe(Stages.PRINT_HELPS[args[0]])[1]
                    sys.exit(0)
                #Else, this will drop down to the next parser.error
            else:
                print parser.format_help()
                sys.exit(0)
        if len(args) != 2 or args[0] not in STAGES:
            parser.error("Invalid Arguments. Expected one of\n'%s'" % "', '".join(STAGES))
            sys.exit(1)
        self.executeStage = args[0]
        self.protocolName = os.path.abspath(args[1])
def main(args=sys.argv[1:], parser=None):
    """ Main entry point for the route53_dyndns command-line script """

    usage = "%prog [options] [args]\n\n"
    version = "%prog " + __version__

    usage += "%prog is the HTTP dynamic DNS server for Route53"

    if parser is None:  # pragma: no cover
        parser = OptionParser(usage=usage, version=version,
                              add_help_option=False)
    parser.remove_option("--version")  # Remove standard version option

    parser.add_option("-h", "--help", dest="help", action="store_true",
                      help="Show help and exit")
    parser.add_option("-V", "--version", dest="version", action="store_true",
                      help="Show version and exit")

    (options, parsed_args) = parser.parse_args(args)

    # Check if we should print help or version number
    if not parsed_args or (parsed_args and len(parsed_args) == 0):
        if options.version:
            parser.print_version()
            sys.exit(0)
        elif options.help:
            parser.print_help()
            sys.exit(0)

    configure_logging()

    app.run(debug=True)
Example #8
0
 def test_custom_progname(self):
     parser = OptionParser(prog="thingy", version="%prog 0.1", usage="%prog arg arg")
     parser.remove_option("-h")
     parser.remove_option("--version")
     expected_usage = "usage: thingy arg arg\n"
     self.assertUsage(parser, expected_usage)
     self.assertVersion(parser, "thingy 0.1")
     self.assertHelp(parser, expected_usage + "\n")
Example #9
0
def processArguments():
    '''init process arguments '''
    parser = OptionParser(usage="Usage: deepin-screenshot [options] [arg]", version="deepin-screenshot v2.1")
    parser.add_option("-f", "--full", action="store_true", dest="fullscreen", help=_("Take a screenshot of full screen"))
    parser.add_option("-w", "--window", action="store_true", dest="window", help=_("Take a screenshot of the window"))
    parser.add_option("-d", "--delay", dest="delay", type="int", help=_("Take a screenshot after NUM seconds"), metavar="NUM")
    parser.add_option("-s", "--save", dest="save_file", help=_("save screenshot to FILE"), metavar="FILE")
    parser.add_option("--sub", action="store_true", dest="sub", help=_("run as a subprocess"))
    parser.add_option("-n", "--new", action="store_true", dest="new", help=_("run a new process"))
    parser.add_option("-I", "--icon", action="store_true", dest="icon")
    #parser.add_option("-a", "--area", help="Grab an area of the screen instead of the entire screen", action="store_true")
    #parser.add_option("-e", "--border-effect", action="store_true", dest="border_effect", help="Effect to add to the border")
    #parser.add_option("-i", "--interactive", action="store_true", help="Interactively set options")
    #parser.add_option("-b", "--include-border", action="store_true", help="Include the window border with the screenshot")
    #parser.add_option("-B", "--remove-border", action="store_true", help="Remove the window border from the screenshot")
    #parser.add_option("-c", "--clipboard", help="Send the grab directly to the clipboard", action="store_true")
    parser.get_option('-h').help = _("show this help message and exit")
    parser.get_option('--version').help = _("show program's version number and exit")
    
    import sys
    if '-h' in sys.argv or '--help' in sys.argv:
        parser.remove_option("-I")
    (options, args) = parser.parse_args()

    if not options.new and IS_EXISTS:
        print "deepint-screenshot has run"
        exit(1)
    if options.fullscreen and options.window:
        parser.error("options -f and -w are mutually exclusive")
    config.OPTION_ICON = options.icon
    config.OPTION_FULLSCREEN = options.fullscreen
    config.OPTION_WINDOWN = options.window
    config.OPTION_NEW = options.new
    config.OPTION_FILE = options.save_file
    config.OPTION_SUB = options.sub
    if options.delay:
        notify("Deepin Screenshot", 0, summary=_("DScreenshot"),
               body=_("Deepin Screenshot will start in %d seconds.") % options.delay, timeout=(options.delay-0.5)*1000)
        loop = gobject.MainLoop()
        gobject.timeout_add_seconds(options.delay, loop.quit)
        loop.run()
    if options.save_file:
        parserFile = parser_path(str(options.save_file))
        if options.fullscreen:
            pixbuf = get_screenshot_pixbuf(True)
            pixbuf.save(parserFile[0], parserFile[1])
        elif options.window:
            pixbuf = get_screenshot_pixbuf(False)
            pixbuf.save(parserFile[0], parserFile[1])
        else:    
            main()
    elif options.fullscreen:
        open_file_dialog()
    elif options.window:
        open_file_dialog(False)
    else:
        main()
Example #10
0
def parseargs(help=False):
    parser = OptionParser()
    parser.remove_option('-h')

    parser.add_option('-h',
                      '--help',
                      '-?',
                      action='help',
                      help='show this message and exit')
    # --complexity
    parser.add_option(
        '--complexity',
        metavar="<Number>",
        type=int,
        default=1,
        help=
        'The complexity number determines the number of operators in the query. With complexity 1, you get ~10 memory guzzling operators. With 2, the number of operators ~20 and so on.'
    )
    # --concurrency
    parser.add_option(
        '--concurrency',
        metavar='<Number>',
        type=int,
        default=1,
        help='Number of concurrent sessions from which to execute the queries')
    # --dbname
    parser.add_option(
        '--dbname',
        metavar='<dbname>',
        default='gptest',
        help='Database where mpph heap_ tables are created and data loaded.')
    # --username
    parser.add_option(
        '--username',
        metavar='<username>',
        default='gpadmin',
        help='Database username to use while executing the query')
    options, args = parser.parse_args()

    try:
        if sys.argv[1:].__len__() == 0:
            parser.print_help()
            raise Exception("\nERROR: You did not provide any arguments\n\n")
        if options.complexity <= 0:
            parser.print_help()
            raise Exception(
                "\nERROR: You must provide a positive integer for complexity\n\n"
            )
        if options.concurrency <= 0:
            parser.print_help()
            raise Exception(
                "\nERROR: You must provide a positive integer for concurrency\n\n"
            )
    except Exception, e:
        print e
        sys.exit(-1)
Example #11
0
    def test_callback_help(self):
        # This test was prompted by SF bug #960515 -- the point is
        # not to inspect the help text, just to make sure that
        # format_help() doesn't crash.
        parser = OptionParser(usage=SUPPRESS_USAGE)
        parser.remove_option("-h")
        parser.add_option("-t", "--test", action="callback", callback=lambda: None, type="string", help="foo")

        expected_help = "options:\n" "  -t TEST, --test=TEST  foo\n"
        self.assertHelp(parser, expected_help)
Example #12
0
def agent_parse_args():
    usage = "usage: %prog [options]"
    parser = OptionParser(usage=usage)
    parser.remove_option("-h")
    parser.add_option(
        "-p", dest="port", help="choose a port to connect", metavar="port", type="int")
    parser.add_option("-h", dest="host", help="specific the host",
                      metavar="host", type="string")
    options, args = parser.parse_args()
    return options, args
Example #13
0
def CLI(args=argv[1:]):
    parser = OptionParser(
        version='cassandra-cli/%s (using pycassa/%s, Python/%s)' % (
            ccli_version, pycassa_version, '.'.join(map(str, version_info[:3]))
            ),
        usage='Usage: %prog [OPTIONS] [keyspace]',
        description='Cassanda Command-Line Interface',
        add_help_option=False
    )

    parser.remove_option('--version')
    parser.add_option('-?', '--help', dest='show_help', help='Display this help and exit.', action='store_true', default=False)
    parser.add_option('-I', dest='show_help', help='Synonym for -?', action='store_true')
    parser.add_option('-V', '--version', dest='show_version', help='Output version information and exit.', action='store_true', default=False)
    parser.add_option('-v', '--verbose', dest='verbose', help='Write more.', metavar='verbose', action='count', default=0)

    parser.add_option('-h', '--host', dest='host', help='Connect to host.', metavar='name', default='localhost')
    parser.add_option('-P', '--port', dest='port', help='Port number to use for connection (default: %default).', metavar='#', default=9160, type='int')

    parser.add_option('-u', '--user', dest='username', help='User for login', metavar='name', default=None)
    parser.add_option('-p', '--password', dest='password', help="Password to use when connecting to server. If password is not given it's asked from the tty.", metavar='name', default=None, action='callback', callback=process_password)

    parser.add_option('-k', '--keyspace', dest='keyspace', help='Keyspace to use.', metavar='name', default=None)
    parser.add_option('-d', '--database', dest='keyspace', help='Synonym for -k (mysql client compatibility).', metavar='name')

    parser.add_option('--connect-timeout', dest='timeout', help='Number of seconds before connection timeout.', metavar='#', default=0.5, type='float')

    options, args = parser.parse_args(args=args)
    if len(args) == 1:
        options.keyspace = args[0]

    if options.show_help or len(args) > 1:
        parser.print_version()
        parser.print_help()
    elif options.show_version:
        parser.print_version()
    else:
        try:
            GUI(
                host=options.host, port=options.port, keyspace=options.keyspace,
                username=options.username, password=options.password, timeout=options.timeout,
                verbose=options.verbose
            ).run()
        except Exception as e:
            if options.verbose >= 2:
                raise

            print 'CCLI ERROR: %s' % str(e)
Example #14
0
class RepositoryAction(Action):
    description = "Set/modify the current MDiG repository and location"

    def __init__(self):
        super(RepositoryAction, self).__init__()
        self.check_model = False
        self.preload = False
        self.init_repository = False
        self.init_grass = False
        self.parser = OptionParser(
            version=mdig.version_string,
            description=self.description,
            usage="%prog repository [options] GISDBASE/LOCATION")
        self.add_options()
        self.parser.remove_option('-k')
        self.parser.remove_option('-r')

    def parse_options(self, argv):
        (self.options, args) = self.parser.parse_args(argv[1:])
        if len(args) == 1:
            self.repo_dir = args[0]
            self.log.debug("Repo dir specified is " + self.repo_dir)
        else:
            c = config.get_config()
            print "Current repository: " + c['GRASS']['GISDBASE']
            sys.exit(0)

    def do_me(self, mdig_model):
        c = config.get_config()
        gisdbase, location = os.path.split(os.path.abspath(self.repo_dir))
        if not os.path.isdir(gisdbase):
            self.log.error("GISDBASE '%s' is not a directory", gisdbase)
            sys.exit(1)
        if not os.path.isdir(self.repo_dir):
            self.log.error("LOCATION '%s' is not a directory", location)
            sys.exit(1)
        if not os.path.isdir(os.path.join(self.repo_dir, 'PERMANENT')):
            self.log.error("LOCATION '%s' doesn't have a PERMANENT mapset." + \
                    " Is this path a proper location within a GRASS database?", location)
            sys.exit(1)
        c['GRASS']['GISDBASE'] = gisdbase
        c['GRASS']['LOCATION_NAME'] = location
        c.write()
        print "Set repository to: " + c['GRASS']['GISDBASE']
        print "Set default location to: " + c['GRASS']['LOCATION_NAME']
Example #15
0
def option_parser():
    '''option parser'''
    parser = OptionParser()
    parser.remove_option('-h')
    parser.add_option('-?', '--help', action='help')
    parser.add_option('-h', '--host', dest="host", help='host of the ranger server', \
                      default='localhost')
    parser.add_option('-p', '--port', dest="port", \
                      help='port of the ranger server', type='int', default=6080)
    parser.add_option('-U', '--rangeruser', dest="rangerusername", default='admin', \
                      help='ranger username')
    parser.add_option('-w', '--rangerpassword', dest="rangerpassword", \
                      default='admin', help='ranger password')
    parser.add_option('-d', '--detelepolicy', dest="deletedpolicyname",\
                      default= '', help='delete a policy in ranger')
    parser.add_option('-a', '--addpolicy', dest="newpolicyfilename", \
                      default = '', help='add a policy in ranger by json file')
    return parser
Example #16
0
def option_parser():
    '''option parser'''
    parser = OptionParser()
    parser.remove_option('-h')
    parser.add_option('-?', '--help', action='help')
    parser.add_option('-h', '--host', dest="host", help='host of the ranger server', \
                      default='localhost')
    parser.add_option('-p', '--port', dest="port", \
                      help='port of the ranger server', type='int', default=6080)
    parser.add_option('-U', '--rangeruser', dest="rangerusername", default='admin', \
                      help='ranger username')
    parser.add_option('-w', '--rangerpassword', dest="rangerpassword", \
                      default='admin', help='ranger password')
    parser.add_option('-d', '--detelepolicy', dest="deletedpolicyname",\
                      default= '', help='delete a policy in ranger')
    parser.add_option('-a', '--addpolicy', dest="newpolicyfilename", \
                      default = '', help='add a policy in ranger by json file')
    return parser
Example #17
0
class RepositoryAction(Action):
    description = "Set/modify the current MDiG repository and location"

    def __init__(self):
        super(RepositoryAction, self).__init__()
        self.check_model = False
        self.preload = False
        self.init_repository = False
        self.init_grass = False
        self.parser = OptionParser(version=mdig.version_string,
                description = self.description,
                usage = "%prog repository [options] GISDBASE/LOCATION" )
        self.add_options()
        self.parser.remove_option('-k')
        self.parser.remove_option('-r')

    def parse_options(self,argv):
        (self.options, args) = self.parser.parse_args(argv[1:])
        if len(args) == 1:
            self.repo_dir = args[0]
            self.log.debug("Repo dir specified is " + self.repo_dir)
        else:
            c = config.get_config()
            print "Current repository: " + c['GRASS']['GISDBASE']
            sys.exit(0)

    def do_me(self, mdig_model):
        c = config.get_config()
        gisdbase, location = os.path.split(os.path.abspath(self.repo_dir))
        if not os.path.isdir(gisdbase):
            self.log.error("GISDBASE '%s' is not a directory", gisdbase)
            sys.exit(1)
        if not os.path.isdir(self.repo_dir):
            self.log.error("LOCATION '%s' is not a directory", location)
            sys.exit(1)
        if not os.path.isdir(os.path.join(self.repo_dir,'PERMANENT')):
            self.log.error("LOCATION '%s' doesn't have a PERMANENT mapset." + \
                    " Is this path a proper location within a GRASS database?", location)
            sys.exit(1)
        c['GRASS']['GISDBASE'] = gisdbase
        c['GRASS']['LOCATION_NAME'] = location
        c.write()
        print "Set repository to: " + c['GRASS']['GISDBASE']
        print "Set default location to: " + c['GRASS']['LOCATION_NAME']
Example #18
0
class TestDefaultValues(BaseTest):
    def setUp(self):
        self.parser = OptionParser()
        self.parser.add_option("-v", "--verbose", default=True)
        self.parser.add_option("-q", "--quiet", dest="verbose")
        self.parser.add_option("-n", type="int", default=37)
        self.parser.add_option("-m", type="int")
        self.parser.add_option("-s", default="foo")
        self.parser.add_option("-t")
        self.parser.add_option("-u", default=None)
        self.expected = {"verbose": True, "n": 37, "m": None, "s": "foo", "t": None, "u": None}

    def test_basic_defaults(self):
        self.assertEqual(self.parser.get_default_values(), self.expected)

    def test_mixed_defaults_post(self):
        self.parser.set_defaults(n=42, m=-100)
        self.expected.update({"n": 42, "m": -100})
        self.assertEqual(self.parser.get_default_values(), self.expected)

    def test_mixed_defaults_pre(self):
        self.parser.set_defaults(x="barf", y="blah")
        self.parser.add_option("-x", default="frob")
        self.parser.add_option("-y")

        self.expected.update({"x": "frob", "y": "blah"})
        self.assertEqual(self.parser.get_default_values(), self.expected)

        self.parser.remove_option("-y")
        self.parser.add_option("-y", default=None)
        self.expected.update({"y": None})
        self.assertEqual(self.parser.get_default_values(), self.expected)

    def test_process_default(self):
        self.parser.option_class = DurationOption
        self.parser.add_option("-d", type="duration", default=300)
        self.parser.add_option("-e", type="duration", default="6m")
        self.parser.set_defaults(n="42")
        self.expected.update({"d": 300, "e": 360, "n": 42})
        self.assertEqual(self.parser.get_default_values(), self.expected)

        self.parser.set_process_default_values(False)
        self.expected.update({"d": 300, "e": "6m", "n": "42"})
        self.assertEqual(self.parser.get_default_values(), self.expected)
Example #19
0
def option_parser():
    '''option parser'''
    parser = OptionParser()
    parser.remove_option('-h')
    parser.add_option('-?', '--help', action='help')
    parser.add_option('-h', '--host', dest="host", help='host of the ranger server', \
                      default='localhost')
    parser.add_option('-p', '--port', dest="port", \
                      help='port of the ranger server', type='int', default=6080)
    parser.add_option('-U', '--rangeruser', dest="rangerusername", default='admin', \
                      help='ranger username')
    parser.add_option('-w', '--rangerpassword', dest="rangerpassword", \
                      default='admin', help='ranger password')
    parser.add_option('-f', '--full', action="store_true", dest="fullprivilege", \
                      default=False, help='also add full privilege for user')
    parser.add_option('-u', '--user', dest="users", type='string',\
                      action='callback', callback=foo_callback, \
                      help='the ranger user list to be added')
    parser.add_option('-d', '--deteleuser', dest="deleteduserame",\
                      type='string', action='callback', \
                      callback=foo_callback, help='delete a user in ranger')
    return parser
Example #20
0
def option_parser():
    '''option parser'''
    parser = OptionParser()
    parser.remove_option('-h')
    parser.add_option('-?', '--help', action='help')
    parser.add_option('-h', '--host', dest="host", help='host of the ranger server', \
                      default='localhost')
    parser.add_option('-p', '--port', dest="port", \
                      help='port of the ranger server', type='int', default=6080)
    parser.add_option('-U', '--rangeruser', dest="rangerusername", default='admin', \
                      help='ranger username')
    parser.add_option('-w', '--rangerpassword', dest="rangerpassword", \
                      default='admin', help='ranger password')
    parser.add_option('-f', '--full', action="store_true", dest="fullprivilege", \
                      default=False, help='also add full privilege for user')
    parser.add_option('-u', '--user', dest="users", type='string',\
                      action='callback', callback=foo_callback, \
                      help='the ranger user list to be added')
    parser.add_option('-d', '--deteleuser', dest="deleteduserame",\
                      type='string', action='callback', \
                      callback=foo_callback, help='delete a user in ranger')
    return parser
Example #21
0
def create_option_parser(usage, epilog=None):
    err_choices = [ 'ERROR', 'WARN', 'INFO', 'DEBUG' ]
    parser = OptionParser(usage=usage, version="%prog " + VERSION,
                          add_help_option=False, epilog=epilog)
    parser.remove_option("--version")
    group = OptionGroup(parser, "General Options")
    group.add_option("-h", "--host", help="Tomcat server host",
                     default='localhost', dest='host')
    group.add_option("-P", "--port", help="Tomcat server port",
                     default='8080', dest='port')
    group.add_option("-u", "--user", help="Tomcat server username",
                     default='admin', dest='user')
    group.add_option("-p", "--password", help="Tomcat server password",
                     default='admin', dest='passwd')
    group.add_option("--loglevel", help="Log level ({0})".format(', '.join(err_choices)),
                     type="string", action="callback", metavar='LEVEL',
                     callback=lambda a, b, v, o: setup_logging(v))
    group.add_option("--help", help="show this help message and exit",
                     action="help")
    group.add_option("--version", help="show program's version number and exit",
                     action="version")
    parser.add_option_group(group)
    return parser
Example #22
0
def check_server(conf):
    parser = OptionParser()
    while True:
        msg_body = ""
        for section in conf.sections():
            if section != 'general':
                for key in conf[section]:
                    if is_int(conf[section][key]):
                        parser.add_option("-p",
                                          "--port",
                                          dest="port",
                                          type="int",
                                          default=conf[section][key],
                                          help="Server Port")
                    else:
                        parser.add_option("-a",
                                          "--address",
                                          dest="address",
                                          default=conf[section][key],
                                          help="Server Address")
                (options, args) = parser.parse_args()
                parser.remove_option("-p")
                parser.remove_option("-a")
                server_connected, error_message = connect_server(
                    options.address, options.port)
                if not server_connected:
                    d = {'section': section}
                    err_msg = "'{}' is not listening on port '{}' due to: {}".format(
                        options.address, options.port, error_message)
                    msg_body += err_msg + "\n"
                    logger.error(err_msg, extra=d)

        if msg_body:
            send_email(conf, msg_body)

        time.sleep(int(conf['general']['interval']))
Example #23
0
def main(argv):
    main_cmd = os.path.basename(argv[0])

    # the main optparser
    usage = "%s [--version] [--help] [--verbose] <command> [<args>]" % main_cmd
    parser = OptionParser(usage=usage)

    parser.remove_option("--help")
    parser.add_option("-h", "--help", action="store_true")
    parser.add_option("--version", action="store_true", help="print version")
    parser.add_option("-v",
                      "--verbose",
                      action="store_true",
                      help="verbose output")

    # no args, print help (might change in the future)
    if len(argv) <= 1:
        _print_help(main_cmd, parser, file=sys.stderr)
        return 1

    # collect options for the main command and get the command offset
    offset = -1
    pre_command = []
    for i, a in enumerate(argv):
        if i == 0:
            continue
        elif a.startswith("-"):
            pre_command.append(a)
        else:
            offset = i
            break

    # parse the global options
    options = parser.parse_args(pre_command)[0]

    # --help somewhere
    if options.help:
        _print_help(main_cmd, parser)
        return 0

    # --version somewhere
    if options.version:
        print_("%s version %s" % (main_cmd, const.VERSION))
        return 0

    # no sub command followed, help to stderr
    if offset == -1:
        _print_help(main_cmd, parser, file=sys.stderr)
        return 1
    arg = argv[offset]

    # special case help and list all commands
    if arg == "help":
        # no command, list all commands
        if len(argv) == 2:
            _print_help(main_cmd, parser)
            return 0

    # get the right sub command and pass the remaining args
    for command in Command.COMMANDS:
        if command.NAME == arg:
            cmd = command(main_cmd, options)
            try:
                cmd.execute(argv[offset + 1:])
            except CommandError as e:
                print_(u"%s: %s" % (command.NAME, e), file=sys.stderr)
                return 1
            break
    else:
        print_(u"Unknown command '%s'. See '%s help'." % (arg, main_cmd),
               file=sys.stderr)
        return 1

    return 0
Example #24
0
                example.plot_s_smith(draw_labels=True, m=1, n=0, label='S12')
            elif i == 1:
                example.plot_z_re(m=0, n=0, label='Z11')
                example.plot_z_re(m=1, n=0, label='Z12')
            elif i == 2:
                example.plot_z_im(m=0, n=0, label='Z11')
                example.plot_z_im(m=1, n=0, label='Z12')
            elif i == 3:
                example.plot_s_db(m=0, n=0, label='S11')  # 10
                example.plot_s_db(m=1, n=0, label='S12')
            elif i == 4:
                example.plot_s_db_time(
                    m=0, n=0, label='S11'
                )  # employs windowing before plotting to enhance impluse resolution.
                example.plot_s_db_time(m=1, n=0, label='S12')
            elif i == 5:
                example.plot_z_time_db(m=0, n=0, label='Z11')  #plot_z_re_time
                example.plot_z_time_db(m=1, n=0, label='Z12')

        parser.remove_option('--basename')
        parser.remove_option('--directory')

        fig.savefig(dir_in + cable.replace(".vna.txt", "") + '_rf.png')

        IDchecker += 1

        if IDchecker > (len(Pre_list) - 1):
            break

print("Plots can be now found in the Plots folder of the Cable\n")
Example #25
0
def sorted(x):
    all = [i for i in x]
    all.sort()
    return all

if __name__ == "__main__":
    from optparse import OptionParser
    parser = OptionParser(usage="%prog [options] mca.txt process cuts%%d.txt")
    addMCAnalysisOptions(parser)
    parser.add_option("--jets",         dest="jets",     type="int",    default=0,            help="Jet bin");
    parser.add_option("--masses",       dest="massfile", type="string", default="masses",     help="File containing masses to consider");
    parser.add_option("--scale",        dest="scale",    type="float",  default=(0.0, 0.0),   nargs=2, help="Scale factor and _absolute_ uncertainty on it");
    parser.add_option("--gamma",        dest="gamma",    type="int",    default=0,           help="Extract out from the scale uncertainty a gamma factor with the specified number of observed events");
    parser.add_option("-v",             dest="verbose",  action="store_true", help="Print out report")
    parser.remove_option("--out");
    parser.add_option("-o", "--out",    dest="out",     default="{process}Card_{ll}_{jets}j.txt",  help="Output file pattern; ll = (ee,mm,...); channel = (elel, mumu,...)");
    (options, args) = parser.parse_args()
    if not options.verbose: options.final = True
#     (mcafile, process, cutspattern) = args
    mcafile = args[0]
    process = args[1]
    cutspattern = args[2:]
    print cutspattern

    tty = MCAnalysis(mcafile,options)
    if options.scale[0] != 0: tty.scaleProcess(process, 1.0)

    if options.gamma:
        if options.scale[0] == 0: raise RuntimeError, "Can't use gamma with no scale"
        if options.scale[1] == 0: raise RuntimeError, "Can't use gamma with no scale uncertainty"
Example #26
0
class ReduceAction(Action):
    description = "Take a series of CSV output files and calculate average and std."

    def __init__(self):
        # TODO: refactor this and RepositoryAction to share a non-model loading
        # parent
        super(ReduceAction, self).__init__()
        self.check_model = False
        self.preload = False
        self.init_repository = False
        self.init_grass = False
        # Default is to run analysis on all timesteps with maps available.
        self.parser = OptionParser(version=mdig.version_string,
                                   description=self.description,
                                   usage="%prog reduce [options] model_name")
        self.add_options()
        self.model_limit = None
        self.parser.remove_option('-k')
        self.parser.remove_option('-r')

    def add_options(self):
        super(ReduceAction, self).add_options()
        self.parser.add_option("-n",
                               "--no-header",
                               help="Are the CSVs missing headers?",
                               action="store_true",
                               dest="no_header")
        self.parser.add_option(
            "-c",
            "--column",
            help="Use this column for the timeseries value we are reducing.",
            action="store",
            type="int",
            dest="column")
        self.parser.add_option("-m",
                               "--match",
                               help="Match these columns in each file",
                               action="append",
                               default=[0],
                               type="int",
                               dest="match_columns")
        self.parser.add_option("-f",
                               "--out-file",
                               help="File to put the results in",
                               action="store",
                               dest="outfile")

    def do_me(self, mdig_model):
        import csv
        from mdig.utils import mean_std_dev
        the_files = self.model_names  # extra args are put in self.model_names
        row_keys = {}
        headers = None
        for fn in the_files:
            with open(fn, 'r') as f:
                csv_reader = csv.reader(f,
                                        delimiter=',',
                                        quotechar='"',
                                        skipinitialspace=True)
                if not self.options.no_header:
                    headers = next(csv_reader, None)
                for row in csv_reader:
                    k = tuple([row[i] for i in self.options.match_columns])
                    row_keys.setdefault(k, [])
                    try:
                        row_keys[k].append(float(row[self.options.column]))
                    except ValueError:
                        row_keys[k].append(0.0)
                    except Exception, e:
                        print str(e)
                        print row
                        import pdb
                        pdb.set_trace()
        # Reduce them
        reduced = {}
        for k in row_keys:
            reduced[k] = mean_std_dev(row_keys[k])

        from operator import itemgetter
        with open(self.options.outfile, 'w') as f:
            out_file = csv.writer(f, delimiter=',', quotechar='"')
            if headers:
                header = [headers[i] for i in self.options.match_columns] + [
                    'mean %s' % headers[self.options.column],
                    'std %s' % headers[self.options.column],
                ]
                out_file.writerow(header)
            for k, val in sorted(reduced.iteritems(), key=itemgetter(0)):
                out_file.writerow(list(k) + list(val))
Example #27
0
def parse_opts(tmpcmdline, silent=False):
	myaction=None
	myopts = {}
	myfiles=[]

	global options, shortmapping

	actions = frozenset([
		"clean", "config", "depclean", "help",
		"info", "list-sets", "metadata",
		"prune", "regen",  "search",
		"sync",  "unmerge", "version",
	])

	longopt_aliases = {"--cols":"--columns", "--skip-first":"--skipfirst"}
	argument_options = {
		"--accept-properties": {
			"help":"temporarily override ACCEPT_PROPERTIES",
			"action":"store"
		},

		"--backtrack": {

			"help"   : "Specifies how many times to backtrack if dependency " + \
				"calculation fails ",

			"action" : "store"
		},

		"--config-root": {
			"help":"specify the location for portage configuration files",
			"action":"store"
		},
		"--color": {
			"help":"enable or disable color output",
			"type":"choice",
			"choices":("y", "n")
		},

		"--complete-graph": {
			"help"    : "completely account for all known dependencies",
			"type"    : "choice",
			"choices" : ("True", "n")
		},

		"--deep": {

			"shortopt" : "-D",

			"help"   : "Specifies how deep to recurse into dependencies " + \
				"of packages given as arguments. If no argument is given, " + \
				"depth is unlimited. Default behavior is to skip " + \
				"dependencies of installed packages.",

			"action" : "store"
		},

		"--deselect": {
			"help"    : "remove atoms/sets from the world file",
			"type"    : "choice",
			"choices" : ("True", "n")
		},

		"--exclude": {
			"help"   :"A space separated list of package names or slot atoms. " + \
				"Emerge won't  install any ebuild or binary package that " + \
				"matches any of the given package atoms.",

			"action" : "append"
		},

		"--fail-clean": {
			"help"    : "clean temp files after build failure",
			"type"    : "choice",
			"choices" : ("True", "n")
		},

		"--jobs": {

			"shortopt" : "-j",

			"help"   : "Specifies the number of packages to build " + \
				"simultaneously.",

			"action" : "store"
		},

		"--keep-going": {
			"help"    : "continue as much as possible after an error",
			"type"    : "choice",
			"choices" : ("True", "n")
		},

		"--load-average": {

			"help"   :"Specifies that no new builds should be started " + \
				"if there are other builds running and the load average " + \
				"is at least LOAD (a floating-point number).",

			"action" : "store"
		},

		"--with-bdeps": {
			"help":"include unnecessary build time dependencies",
			"type":"choice",
			"choices":("y", "n")
		},
		"--reinstall": {
			"help":"specify conditions to trigger package reinstallation",
			"type":"choice",
			"choices":["changed-use"]
		},

		"--binpkg-respect-use": {
			"help"    : "discard binary packages if their use flags \
				don't match the current configuration",
			"type"    : "choice",
			"choices" : ("True", "y", "n")
		},

		"--getbinpkg": {
			"shortopt" : "-g",
			"help"     : "fetch binary packages",
			"type"     : "choice",
			"choices"  : ("True", "n")
		},

		"--getbinpkgonly": {
			"shortopt" : "-G",
			"help"     : "fetch binary packages only",
			"type"     : "choice",
			"choices"  : ("True", "n")
		},

		"--rebuilt-binaries": {
			"help"     : "replace installed packages with binary " + \
			             "packages that have been rebuilt",
			"type"     : "choice",
			"choices"  : ("True", "n")
		},
		
		"--rebuilt-binaries-timestamp": {
			"help"   : "use only binaries that are newer than this " + \
			           "timestamp for --rebuilt-binaries",
			"action" : "store"
		},

		"--root": {
		 "help"   : "specify the target root filesystem for merging packages",
		 "action" : "store"
		},

		"--root-deps": {
			"help"    : "modify interpretation of depedencies",
			"type"    : "choice",
			"choices" :("True", "rdeps")
		},

		"--select": {
			"help"    : "add specified packages to the world set " + \
			            "(inverse of --oneshot)",
			"type"    : "choice",
			"choices" : ("True", "n")
		},

		"--selective": {
			"help"    : "similar to the --noreplace but does not take " + \
			            "precedence over options such as --newuse",
			"type"    : "choice",
			"choices" : ("True", "n")
		},

		"--use-ebuild-visibility": {
			"help"     : "use unbuilt ebuild metadata for visibility checks on built packages",
			"type"     : "choice",
			"choices"  : ("True", "n")
		},

		"--usepkg": {
			"shortopt" : "-k",
			"help"     : "use binary packages",
			"type"     : "choice",
			"choices"  : ("True", "n")
		},

		"--usepkgonly": {
			"shortopt" : "-K",
			"help"     : "use only binary packages",
			"type"     : "choice",
			"choices"  : ("True", "n")
		},

	}

	from optparse import OptionParser
	parser = OptionParser()
	if parser.has_option("--help"):
		parser.remove_option("--help")

	for action_opt in actions:
		parser.add_option("--" + action_opt, action="store_true",
			dest=action_opt.replace("-", "_"), default=False)
	for myopt in options:
		parser.add_option(myopt, action="store_true",
			dest=myopt.lstrip("--").replace("-", "_"), default=False)
	for shortopt, longopt in shortmapping.items():
		parser.add_option("-" + shortopt, action="store_true",
			dest=longopt.lstrip("--").replace("-", "_"), default=False)
	for myalias, myopt in longopt_aliases.items():
		parser.add_option(myalias, action="store_true",
			dest=myopt.lstrip("--").replace("-", "_"), default=False)

	for myopt, kwargs in argument_options.items():
		shortopt = kwargs.pop("shortopt", None)
		args = [myopt]
		if shortopt is not None:
			args.append(shortopt)
		parser.add_option(dest=myopt.lstrip("--").replace("-", "_"),
			*args, **kwargs)

	tmpcmdline = insert_optional_args(tmpcmdline)

	myoptions, myargs = parser.parse_args(args=tmpcmdline)

	if myoptions.changed_use is not False:
		myoptions.reinstall = "changed-use"
		myoptions.changed_use = False

	if myoptions.deselect == "True":
		myoptions.deselect = True

	if myoptions.binpkg_respect_use in ("y", "True",):
		myoptions.binpkg_respect_use = True
	else:
		myoptions.binpkg_respect_use = None

	if myoptions.complete_graph in ("y", "True",):
		myoptions.complete_graph = True
	else:
		myoptions.complete_graph = None

	if myoptions.exclude:
		exclude = []
		bad_atoms = []
		for x in ' '.join(myoptions.exclude).split():
			bad_atom = False
			try:
				atom = portage.dep.Atom(x)
			except portage.exception.InvalidAtom:
				try:
					atom = portage.dep.Atom("null/"+x)
				except portage.exception.InvalidAtom:
					bad_atom = True
			
			if bad_atom:
				bad_atoms.append(x)
			else:
				if atom.operator or atom.blocker or atom.use:
					bad_atoms.append(x)
				else:
					exclude.append(atom)

		if bad_atoms and not silent:
			parser.error("Invalid Atom(s) in --exclude parameter: '%s' (only package names and slot atoms allowed)\n" % \
				(",".join(bad_atoms),))

	if myoptions.fail_clean == "True":
		myoptions.fail_clean = True

	if myoptions.getbinpkg in ("True",):
		myoptions.getbinpkg = True
	else:
		myoptions.getbinpkg = None

	if myoptions.getbinpkgonly in ("True",):
		myoptions.getbinpkgonly = True
	else:
		myoptions.getbinpkgonly = None

	if myoptions.keep_going in ("True",):
		myoptions.keep_going = True
	else:
		myoptions.keep_going = None

	if myoptions.rebuilt_binaries in ("True",):
		myoptions.rebuilt_binaries = True

	if myoptions.root_deps == "True":
		myoptions.root_deps = True

	if myoptions.select == "True":
		myoptions.select = True
		myoptions.oneshot = False
	elif myoptions.select == "n":
		myoptions.oneshot = True

	if myoptions.selective == "True":
		myoptions.selective = True

	if myoptions.backtrack is not None:

		try:
			backtrack = int(myoptions.backtrack)
		except (OverflowError, ValueError):
			backtrack = -1

		if backtrack < 0:
			backtrack = None
			if not silent:
				parser.error("Invalid --backtrack parameter: '%s'\n" % \
					(myoptions.backtrack,))

		myoptions.backtrack = backtrack

	if myoptions.deep is not None:
		deep = None
		if myoptions.deep == "True":
			deep = True
		else:
			try:
				deep = int(myoptions.deep)
			except (OverflowError, ValueError):
				deep = -1

		if deep is not True and deep < 0:
			deep = None
			if not silent:
				parser.error("Invalid --deep parameter: '%s'\n" % \
					(myoptions.deep,))

		myoptions.deep = deep

	if myoptions.jobs:
		jobs = None
		if myoptions.jobs == "True":
			jobs = True
		else:
			try:
				jobs = int(myoptions.jobs)
			except ValueError:
				jobs = -1

		if jobs is not True and \
			jobs < 1:
			jobs = None
			if not silent:
				parser.error("Invalid --jobs parameter: '%s'\n" % \
					(myoptions.jobs,))

		myoptions.jobs = jobs

	if myoptions.load_average:
		try:
			load_average = float(myoptions.load_average)
		except ValueError:
			load_average = 0.0

		if load_average <= 0.0:
			load_average = None
			if not silent:
				parser.error("Invalid --load-average parameter: '%s'\n" % \
					(myoptions.load_average,))

		myoptions.load_average = load_average
	
	if myoptions.rebuilt_binaries_timestamp:
		try:
			rebuilt_binaries_timestamp = int(myoptions.rebuilt_binaries_timestamp)
		except ValueError:
			rebuilt_binaries_timestamp = -1

		if rebuilt_binaries_timestamp < 0:
			rebuilt_binaries_timestamp = 0
			if not silent:
				parser.error("Invalid --rebuilt-binaries-timestamp parameter: '%s'\n" % \
					(myoptions.rebuilt_binaries_timestamp,))

		myoptions.rebuilt_binaries_timestamp = rebuilt_binaries_timestamp

	if myoptions.use_ebuild_visibility in ("True",):
		myoptions.use_ebuild_visibility = True
	else:
		myoptions.use_ebuild_visibility = None

	if myoptions.usepkg in ("True",):
		myoptions.usepkg = True
	else:
		myoptions.usepkg = None

	if myoptions.usepkgonly in ("True",):
		myoptions.usepkgonly = True
	else:
		myoptions.usepkgonly = None

	for myopt in options:
		v = getattr(myoptions, myopt.lstrip("--").replace("-", "_"))
		if v:
			myopts[myopt] = True

	for myopt in argument_options:
		v = getattr(myoptions, myopt.lstrip("--").replace("-", "_"), None)
		if v is not None:
			myopts[myopt] = v

	if myoptions.searchdesc:
		myoptions.search = True

	for action_opt in actions:
		v = getattr(myoptions, action_opt.replace("-", "_"))
		if v:
			if myaction:
				multiple_actions(myaction, action_opt)
				sys.exit(1)
			myaction = action_opt

	if myaction is None and myoptions.deselect is True:
		myaction = 'deselect'

	if myargs and sys.hexversion < 0x3000000 and \
		not isinstance(myargs[0], unicode):
		for i in range(len(myargs)):
			myargs[i] = portage._unicode_decode(myargs[i])

	myfiles += myargs

	return myaction, myopts, myfiles
Example #28
0
def main(argv):
    main_cmd = os.path.basename(argv[0])

    # the main optparser
    usage = "%s [--version] [--help] [--verbose] <command> [<args>]" % main_cmd
    parser = OptionParser(usage=usage)

    parser.remove_option("--help")
    parser.add_option("-h", "--help", action="store_true")
    parser.add_option("--version", action="store_true",
                      help="print version")
    parser.add_option("-v", "--verbose", action="store_true",
                      help="verbose output")

    # no args, print help (might change in the future)
    if len(argv) <= 1:
        _print_help(main_cmd, parser, file=sys.stderr)
        return 1

    # collect options for the main command and get the command offset
    offset = -1
    pre_command = []
    for i, a in enumerate(argv):
        if i == 0:
            continue
        elif a.startswith("-"):
            pre_command.append(a)
        else:
            offset = i
            break

    # parse the global options
    options = parser.parse_args(pre_command)[0]

    # --help somewhere
    if options.help:
        _print_help(main_cmd, parser)
        return 0

    # --version somewhere
    if options.version:
        print_("%s version %s" % (main_cmd, const.VERSION))
        return 0

    # no sub command followed, help to stderr
    if offset == -1:
        _print_help(main_cmd, parser, file=sys.stderr)
        return 1
    arg = argv[offset]

    # special case help and list all commands
    if arg == "help":
        # no command, list all commands
        if len(argv) == 2:
            _print_help(main_cmd, parser)
            return 0

    # get the right sub command and pass the remaining args
    for command in Command.COMMANDS:
        if command.NAME == arg:
            cmd = command(main_cmd, options)
            try:
                cmd.execute(argv[offset + 1:])
            except CommandError as e:
                print_(u"%s: %s" % (command.NAME, e), sys.stderr)
                return 1
            break
    else:
        print_(u"Unknown command '%s'. See '%s help'." % (arg, main_cmd),
               sys.stderr)
        return 1

    return 0
Example #29
0
class WSRTrecipe(object):
    """Base class for recipes, pipelines are created by calling the cook_*
    methods.  Most subclasses should only need to reimplement go() and add
    inputs and outputs.  Some might need to addlogger() to messages or
    override main_results."""
    def __init__(self):
        ## List of inputs, self.inputs[key] != True is considered valid input
        self.inputs = ingredient.WSRTingredient()
        ## List of outputs, should only be filled on succesful execution
        self.outputs = ingredient.WSRTingredient()
        ## All of these should do something sensible in their __str__ for
        ## simple print output

        ## Try using the standard Python system for handling options
        self.optionparser = OptionParser(usage="usage: %prog [options]")
        self.optionparser.remove_option('-h')
        self.optionparser.add_option('-h', '--help', action="store_true")
        self.optionparser.add_option('-v',
                                     '--verbose',
                                     action="callback",
                                     callback=self.__setloglevel,
                                     help="verbose [Default: %default]")
        self.optionparser.add_option('-d',
                                     '--debug',
                                     action="callback",
                                     callback=self.__setloglevel,
                                     help="debug [Default: %default]")

        self.helptext = """LOFAR/WSRT pipeline framework"""
        self.recipe_path = ['.']

    def _log_error(self, e):
        # Sometimes, an exception will be thrown before we have any loggers
        # defined that can handle it. Check if that's the case, and, if so,
        # dump it to stderr.
        handled = len(self.logger.handlers) > 0
        my_logger = self.logger
        while my_logger.parent:
            my_logger = my_logger.parent
            if len(my_logger.handlers) > 0 and my_logger is not my_logger.root:
                handled = True
        if handled:
            self.logger.exception('Exception caught: ' + str(e))
        else:
            print >> sys.stderr, "***** Exception occurred with no log handlers"
            print >> sys.stderr, "*****", str(e)
            print_exc()

    def help(self):
        """Shows helptext and inputs and outputs of the recipe"""
        print self.helptext
        self.optionparser.print_help()
        print '\nOutputs:'
        for k in self._outfields.keys():
            print '  ' + k

    def main_init(self):
        """Main initialization for stand alone execution, reading input from
        the command line"""
        # The root logger has a null handler; we'll override in recipes.
        logging.getLogger().addHandler(NullLogHandler())
        self.logger = getSearchingLogger(self.name)
        opts = sys.argv[1:]
        try:
            myParset = parset.Parset(self.name + ".parset")
            for p in myParset.keys():
                opts[0:0] = "--" + p, myParset.getString(p)
        except IOError:
            logging.debug("Unable to open parset")
        (options, args) = self.optionparser.parse_args(opts)
        if options.help:
            return 1
        else:
            for key, value in vars(options).iteritems():
                if value is not None:
                    self.inputs[key] = value
            self.inputs['args'] = args
            return 0

    def main(self):
        """This function is to be run in standalone mode."""
        import os.path
        self.name = os.path.splitext(os.path.basename(sys.argv[0]))[0]
        status = self.main_init()
        if not status:
            status = self.run(self.name)
            self.main_result()
        else:
            self.help()
        logging.shutdown()
        return status

    def run(self, name):
        """This code will run if all inputs are valid, and wraps the actual
        functionality in self.go() with some exception handling, might need
        another name, like try_execute, because it's to similar to go()."""
        self.name = name
        self.logger.info('recipe ' + name + ' started')
        try:
            status = self.go()
            if not self.outputs.complete():
                self.logger.warn("Note: recipe outputs are not complete")
        except Exception, e:
            self._log_error(e)
            self.outputs = None  ## We're not generating any results we have
            ## confidence in
            return 1
        else:
Example #30
0
def main(argv=sys.argv, Progname=None):
	from optparse import OptionParser       # aka Optik

	# set up commandline arguments
	if not Progname:
		Progname=os.path.basename(argv[0])
	Usage="%prog usage: [-n num|-N|-P]\n" \
	      "%prog usage: -l\n" \
	      "%prog usage: -h\n" \
	      "%prog usage: -V" 
	optparser = OptionParser(usage=Usage, version="%prog: $Id:$" )
	optparser.remove_option("--version")    # we add our own that knows -V
	optparser.add_option("-V", "--version", action="version",
	  help="show program's version number and exit")
	optparser.add_option("-d", "--debug", dest = "debug", 
	  action="store_true", help="log debugging messages")
	optparser.add_option("-n", "--num", dest="workspace_num",
	  action="store", type="int", help="go to specified workspace")
	optparser.add_option("-N", "--next", dest="next_workspace",
	  action="store_true", help="go to next workspace")
	optparser.add_option("-P", "--prior", dest="prior_workspace",
	  action="store_true", help="go to prior workspace")
	optparser.add_option("-W", dest="wrap", default=True,
	  action="store_false", help="turn off workspace wrapping")
	optparser.add_option("-l", dest="list_workspaces",
	  action="store_true", help="list workspaces")
	(options, params) = optparser.parse_args(argv[1:])

	# set up logging environment
	root_log = logging.getLogger()          # grab the root logger
	root_log.setLevel((logging.INFO, logging.DEBUG)[options.debug == True])
	handler = logging.StreamHandler()
	logformat = "%(name)s: %(levelname)s: %(message)s"
	handler.setFormatter(logging.Formatter(logformat))
	root_log.addHandler(handler)
	log = logging.getLogger(Progname)

	if (bool(options.prior_workspace) + bool(options.next_workspace) + \
	    bool(options.workspace_num is not None)) > 1:
		log.critical("Only one of -N, -P, or -n options may be specified")
		sys.exit(1)
		

	screen = Screen()
	if options.list_workspaces:
		cur = screen.current_workspace()
		for count, workspace in enumerate(screen.list_workspaces()):
			print "%d: %s %s" % (count+1, [" ", "*"][cur == count],
			                     workspace)
		sys.exit(0)

	if options.prior_workspace:
		screen.activate_prior_workspace(wrap=options.wrap)
	elif options.next_workspace: 
		screen.activate_next_workspace(wrap=options.wrap)
	elif options.workspace_num is not None:
		try:
			screen.activate_workspace(options.workspace_num + 1)
		except:
			exc_type, exc_val, exc_tb = sys.exc_info()
			log.critical("Couldn't activate workspace %d: %s",
			  options.workspace_num, exc_val)
			sys.exit(2)
	sys.exit(0)
Example #31
0
def main():
    usage = """usage: %prog --calc=expression --outfile=out_filename [-A filename]
                    [--A_band=n] [-B...-Z filename] [other_options]"""
    parser = OptionParser(usage)

    # define options
    parser.add_option("--calc", dest="calc", help="calculation in gdalnumeric syntax using +-/* or any numpy array functions (i.e. log10())", metavar="expression")
    add_alpha_args(parser, sys.argv)

    parser.add_option("--outfile", dest="outF", help="output file to generate or fill", metavar="filename")
    parser.add_option("--NoDataValue", dest="NoDataValue", type=float, help="output nodata value (default datatype specific value)", metavar="value")
    parser.add_option("--type", dest="type", help="output datatype, must be one of %s" % list(DefaultNDVLookup.keys()), metavar="datatype")
    parser.add_option("--format", dest="format", help="GDAL format for output file", metavar="gdal_format")
    parser.add_option(
        "--creation-option", "--co", dest="creation_options", default=[], action="append",
        help="Passes a creation option to the output format driver. Multiple "
        "options may be listed. See format specific documentation for legal "
        "creation options for each format.", metavar="option")
    parser.add_option("--allBands", dest="allBands", default="", help="process all bands of given raster (A-Z)", metavar="[A-Z]")
    parser.add_option("--overwrite", dest="overwrite", action="store_true", help="overwrite output file if it already exists")
    parser.add_option("--debug", dest="debug", action="store_true", help="print debugging information")
    parser.add_option("--quiet", dest="quiet", action="store_true", help="suppress progress messages")
    parser.add_option("--optfile", dest="optfile", metavar="optfile", help="Read the named file and substitute the contents into the command line options list.")

    (opts, args) = parser.parse_args()

    if not hasattr(opts, "input_files"):
        opts.input_files = {}

    if opts.optfile:
        with open(opts.optfile, 'r') as f:
            ofargv = [x for line in f for x in shlex.split(line, comments=True)]
        # Avoid potential recursion.
        parser.remove_option('--optfile')
        add_alpha_args(parser, ofargv)
        ofopts, ofargs = parser.parse_args(ofargv)
        # Let options given directly override the optfile.
        input_files = getattr(ofopts, 'input_files', {})
        input_files.update(opts.input_files)
        ofopts.__dict__.update({k: v for k, v in vars(opts).items() if v})
        opts = ofopts
        opts.input_files = input_files
        args = args + ofargs

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)
    elif not opts.calc:
        print("No calculation provided. Nothing to do!")
        parser.print_help()
        sys.exit(1)
    elif not opts.outF:
        print("No output file provided. Cannot proceed.")
        parser.print_help()
        sys.exit(1)
    else:
        try:
            doit(opts, args)
        except IOError as e:
            print(e)
            sys.exit(1)
Example #32
0
#!/usr/bin/env python
import re, sys
import os.path
from math import *
from HiggsAnalysis.CombinedLimit.DatacardParser import parseCard, addDatacardParserOptions
from collections import defaultdict
from optparse import OptionParser
try:
    from vv import v
except:
    v = {}

parser = OptionParser()
addDatacardParserOptions(parser)
parser.remove_option("--fix-pars")
parser.remove_option("--ascii")
parser.remove_option("--stat")
parser.add_option("-f",
                  "--format",
                  type="string",
                  dest="format",
                  default="html",
                  help="Format for output number (choose html or brief)")
parser.add_option(
    "-p",
    "--process",
    dest="process",
    default=None,
    type="string",
    help=
    "Higgs process to use. Will also be written in the Workspace as RooRealVar 'MH'."
from math import *
from array import *
from decimal import *
from optparse import OptionParser
from OSUT3Analysis.Configuration.configurationOptions import *
from OSUT3Analysis.Configuration.processingUtilities import *
from OSUT3Analysis.Configuration.formattingUtilities import *



### parse the command-line options

parser = OptionParser()
parser = set_commandline_arguments(parser)

parser.remove_option("-o")
parser.remove_option("-n")
parser.remove_option("-u")
parser.remove_option("-e")
parser.remove_option("-r")
parser.remove_option("-R")
parser.remove_option("-d")
parser.remove_option("-b")
parser.remove_option("--2D")
parser.remove_option("-y")
parser.remove_option("-p")

(arguments, args) = parser.parse_args()


if arguments.localConfig:
Example #34
0
    def _setupOptParser(self):
        p = OptionParser(usage="%prog [options] xml_source(s)",
                         version="v11r16",
                         description="Produce c++ source files and dictionary "
                         "files from xml descriptions.",
                         epilog="'xml_source(s)' can be either one or more "
                         "directories where all xml files will be parsed or "
                         "one or more xml-files which must have the extension "
                         ".xml")
        p.remove_option('--version')
        p.add_option('-v',
                     '--version',
                     action='version',
                     help='show version and exit')
        p.add_option(
            "-g",
            "--generate",
            action="store",
            type="choice",
            metavar='WHAT',
            choices=('src', 'dct'),
            help="produce only sources ('src') or only dictionaries ('dct')")
        p.add_option(
            "-i",
            "--info-file",
            action="append",
            help=
            "add additional file-package-information from './AddImports.txt'")
        p.add_option(
            '-s',
            '--src-output',
            action='store',
            metavar="DIR",
            help='define possible output destination of source code. '
            'Use the special value "env" to use the content of the environment '
            'variable GODDOTHOUT. [default: %default]')
        p.add_option(
            '-d',
            '--dict-output',
            action='store',
            metavar="DIR",
            help='define possible output destination of source code. '
            'Use the special value "env" to use the content of the environment '
            'variable GODDICTOUT. [default: %default]')
        p.add_option(
            '-x',
            '--classdb',
            action='store',
            metavar="CLASSDB",
            help='define location of "GODsClassDB.xml", which contains a '
            'dictionary of classes and their corresponding include files. '
            'Use the special value "env" to use the content of the environment '
            'variable GODXMLDB. [default: ${GAUDIOBJDESCROOT}/xml_files/GODsClassDB.xml]'
        )
        p.add_option('-r',
                     '--root',
                     action='store',
                     metavar='ROOT_DIR',
                     help='define the root path to the GOD tools. '
                     '[default: ${GAUDIOBJDESCROOT}]')
        p.add_option(
            '-n',
            '--namespace',
            action='store',
            help='define the default namespace to use if not given in XML. '
            '[default: %default]')
        p.add_option(
            '-t',
            '--dtd',
            action='store',
            help='path to the DTD file (gdd.dtd). '
            '[default: use the value of GODDTDPATH if defined, or find it in the '
            'same directory as the xml file]')
        p.add_option('-p',
                     '--package',
                     action='store',
                     help='override the package name defined in the XML files')
        p.add_option(
            '--allocator',
            action='store',
            type='choice',
            choices=('FROMXML', 'NO', 'BOOST', 'BOOST2', 'DEBUG', 'ORDERED',
                     'DEFAULT'),
            help='chose the type of allocator to use. Allowed values are: '
            'FROMXML (use what is specified in the XML, default), '
            'NO (never overload operators "new" and "delete"),'
            'BOOST (always use Boost singleton pool), '
            'BOOST2 (always use Boost singleton pool with a check on delete, slower), '
            'DEBUG (same as BOOST2 plus debug print-outs on std::cout), '
            'ORDERED (use "ordered_" malloc and free instead of plain ones '
            'and generate the static method to release the memory),'
            'DEFAULT (alias for BOOST)')

        deprecated = OptionGroup(
            p, "Deprecated Options",
            "These options are deprecated and will be removed "
            "in a future version.")
        deprecated.add_option('-l',
                              action='store',
                              dest='deprecated_l',
                              metavar="IGNORED_VALUE",
                              help='ignored')
        deprecated.add_option('-o',
                              action='store',
                              dest='deprecated_o',
                              metavar="OUTPUT_DIR",
                              help='output directory, use -s or -d instead')

        p.add_option_group(deprecated)

        p.set_defaults(info_file=[],
                       src_output=os.curdir,
                       dict_output=os.curdir,
                       classdb=None,
                       root=os.environ.get('GAUDIOBJDESCROOT'),
                       namespace='LHCb',
                       allocator='FROMXML',
                       dtd=os.environ.get("GODDTDPATH"))
        self._parser = p
Example #35
0
# <Tranlations stuff>

gettext.install(
                "gvdown",
                "po",
                unicode=True
               )

# </Translation stuff>

parser = OptionParser(
                      usage=_("Usage: %prog [options] URL1 [URL2] [...]")
                     )

parser.remove_option("-h")
parser.add_option("-h",
                  "--help",
                  action="help",
                  help=_("show this help message and exit")
                 )

parser.add_option(
                  "-d",
                  "--destination",
                  dest="destination",
                  help=_("save videos in DEST"),
                  metavar="DEST",
                  default="."
                 )
Example #36
0
def main(argv=sys.argv, Progname=None):
	from optparse import OptionParser, SUPPRESS_HELP       # aka Optik

	# set up commandline arguments
	if not Progname:
		Progname=os.path.basename(argv[0])
	Usage="%prog usage: [-a|-l|-L] [--uid|--pattern] PATTERN\n" \
	      "%prog usage: -h\n" \
	      "%prog usage: -V" 
	optparser = OptionParser(usage=Usage, version="%prog: $Id:$" )
	optparser.remove_option("--version")    # we add our own that knows -V
	optparser.add_option("-V", "--version", action="version",
	  help="show program's version number and exit")
	optparser.add_option("-d", "--debug", dest = "debug", 
	  action="store_true", help=SUPPRESS_HELP)
	optparser.add_option("-u", "--uid", dest = "query_userid",
	  action="store_true",
	  help="Query on userid (default is to query on common name)")
	optparser.add_option("--pattern", dest = "query_pattern",
	  action="store_true",
	  help="Query on an arbitrary search expression (e.g. building=*Austin*)")
	optparser.add_option("-a", dest = "return_all",
	  action="store_true",
	  help="Return all available information (ordinarily, only a subset is returned")
	optparser.add_option("-l", dest = "return_list_uid",
	  action="store_true",
	  help="Only print the matching userid")
	optparser.add_option("-L", dest = "return_list_dn",
	  action="store_true",
	  help="Only print the matching distinguished name")
	optparser.add_option("-r", dest = "resources",
	  action="store_true", default=False,
	  help="Query for defined resources (e.g. conference rooms)")
	optparser.add_option("-v", "--verbose", dest = "verbose",
	  action="store_true", help="be verbose")
	(options, params) = optparser.parse_args(argv[1:])

	# set up logging environment
	root_log = logging.getLogger()          # grab the root logger
	if options.debug:
		root_log.setLevel(logging.DEBUG)
	elif options.verbose:
		root_log.setLevel(logging.INFO)
	else:
		root_log.setLevel(logging.WARN)
	handler = logging.StreamHandler()
	logformat = "%(name)s: %(levelname)s: %(message)s"
	handler.setFormatter(logging.Formatter(logformat))
	root_log.addHandler(handler)
	log = logging.getLogger(Progname)


	if options.query_userid and options.query_pattern:
		log.error("Can only specify one of --pattern or --uid")
		sys.exit(1)
	if options.return_list_dn and options.return_list_uid:
		log.error("Can only specify one of -l or -L")
		sys.exit(1)
	
	if options.query_pattern:
		query = dict(sub_pattern.split("=") for sub_pattern in params[0].split(","))
	elif options.query_userid:
		query = {"uid": params[0]}
	else:
		query = {"cn": params[0]}
	
	ldap_engine = cisco_ldap(retrieve_all=options.return_all, resources=options.resources)
	for results in ldap_engine.query(**query):
		user = ldap_user(results)
		if options.return_list_dn:
			print user.dn
		elif options.return_list_uid:
			print user.uid
		else:
			pprint.pprint(results)
			print
Example #37
0
                  dest='autodir',
                  action="store_true",
                  default=False,
                  help="find install dir automatically")
parser.add_option("-s",
                  "--shared",
                  dest='shared',
                  action="store_true",
                  default=False,
                  help="build shared libs [default: static]")
parser.add_option("-p",
                  "--package",
                  dest="package",
                  default="all",
                  help="which package to build")
parser.remove_option("-h")
parser.add_option("-h",
                  "--help",
                  dest="helpflag",
                  action="store_true",
                  default=False,
                  help="help")
(opts, args) = parser.parse_args()

if opts.helpflag:
    printhelp()
    sys.exit(0)

for arg in args:
    opts.package = arg
Example #38
0
def main():
    """Parse options and analyze file."""

    usage = "usage: %prog [options] FILE|DIRECTORY"
    parser = OptionParser(
                     add_help_option = False,
                     version = "%prog " + get_release_string(),
                     usage = usage)
    parser.remove_option("--version")
    
    parser.add_option(
                     "--conf",
                     "-c",
                      action = "store",
                      default = "./mastiff.conf",
                      dest = "config_file",
                      help = "Use an alternate config file. The default is './mastiff.conf'.",
                      type = "string")
    parser.add_option(
                      "--help",
                      "-h",
                      action = "help",
                      help = "Show the help message and exit.")
    parser.add_option(
                      "--list",
                      "-l",
                      action = "store",
                      dest = "list_plugins",
                      help = "List all available plug-ins of the specified type and exit. Type must be one of 'analysis' or 'cat'.",
                      metavar = "PLUGIN_TYPE")
    parser.add_option(
                      "--option",
                      "-o",
                      action="append",
                      default = None,
                      dest = "override",
                      help = "Override a config file option. Configuration options should be specified as 'Section.Key=Value' and should be quoted if any whitespace is present. Multiple overrides can be specified by using multiple '-o' options.")
    parser.add_option(
                      "--plugin",
                      "-p",
                      action = "store",
                      default = None,
                      dest = "plugin_name",
                      help = "Only run the specified analysis plug-in. Name must be quoted if it contains whitespace.")
    parser.add_option(
                      "--quiet",
                      "-q",
                      action = "store_true",
                      default = False,
                      dest = "quiet",
                      help = "Only log errors.")
    parser.add_option(
                      "--type",
                      "-t",
                      action = "store",
                      default = None,
                      dest = "ftype",
                      help = "Force file to be analyzed with plug-ins from the specified category (e.g., EXE, PDF, etc.). Run with '-l cat' to list all available category plug-ins.",
                      type = "string")
    parser.add_option(
                      "--verbose",
                      "-V",
                      action = "store_true",
                      dest = "verbose",
                      default = False,
                      help = "Print verbose logs.")
    parser.add_option(
                      "--version",
                      "-v",
                      action = "version",
                      help = "Show program's version number and exit.")
    
    queue_group = OptionGroup(parser, "Queue Options")
    queue_group.add_option(
                      "--append-queue",
                      "",
                      action = "store_true",
                      dest = "append_queue",
                      default = False,
                      help = "Append file or directory to job queue and exit.")
    queue_group.add_option(
                      "--clear-queue",
                      "",
                      action = "store_true",
                      dest = "clear_queue",
                      default = False,
                      help = "Clear job queue and exit.")
    queue_group.add_option(
                      "--ignore-queue",
                      "",
                      action = "store_true",
                      dest = "ignore_queue",
                      default = False,
                      help = "Ignore the job queue and just process file.")   
    queue_group.add_option(
                      "--list-queue",
                      "",
                      action = "store_true",
                      dest = "list_queue",
                      default = False,
                      help = "List the contents of the job queue and exit.")
    queue_group.add_option(
                      "--resume-queue",
                      action = "store_true",
                      default = False,
                      dest = "resume_queue",
                      help = "Continue processing the queue.")
    parser.add_option_group(queue_group)

    (opts, args) = parser.parse_args()

    if (args is None or len(args) < 1) and opts.list_plugins is None \
    and opts.clear_queue is False and opts.resume_queue is False \
    and opts.list_queue is False:
        parser.print_help()
        sys.exit(1)

    if opts.verbose == True:
        loglevel = logging.DEBUG
    elif opts.quiet == True:
        loglevel = logging.ERROR
    else:
        loglevel = logging.INFO
        
    format_ = '[%(asctime)s] [%(levelname)s] [%(name)s] : %(message)s'        
    logging.basicConfig(format=format_)
    log = logging.getLogger("Mastiff")
    log.setLevel(loglevel)

    # check to see if we are running as root
    if os.geteuid() == 0:
        log.warning('You are running MASTIFF as ROOT! This may be DANGEROUS!')

    if opts.list_plugins is not None:
        plugs = Mastiff.Mastiff(opts.config_file)
        plugs.list_plugins(opts.list_plugins)
        sys.exit(0)

    # set up job queue
    job_queue = queue.MastiffQueue(opts.config_file)
    
    # process job queue specific options
    if opts.clear_queue is True:
        log.info('Clearing job queue and exiting.')
        job_queue.clear_queue()
        sys.exit(0)
    elif opts.list_queue is True:
        if len(job_queue) == 0:
            log.info("MASTIFF job queue is empty.")
        else:
            log.info("MASTIFF job queue has %d entries." % len(job_queue))
            print "\nFile Name\n---------\n%s" % (job_queue)            
        sys.exit(0)
        
    if len(args) > 0:
        fname = args[0]
    else:
        fname = None
        
    if opts.ignore_queue is True:        
        log.info('Ignoring job queue.')
        analyze_file(fname,  opts,  loglevel)
        sys.exit(0)

    # add file or directory to queue
    if fname is not None:
        add_to_queue(job_queue, fname)
        if opts.append_queue is True:
            sys.exit(0)    

    # Start analysis on the files in the queue until it is empty
    while len(job_queue) > 0:
        fname = job_queue.popleft()
        analyze_file(fname, opts, loglevel)        
        log.info('There are %d jobs in the queue.' % len(job_queue))
Example #39
0
class WSRTrecipe(object):
    """Base class for recipes, pipelines are created by calling the cook_*
    methods.  Most subclasses should only need to reimplement go() and add
    inputs and outputs.  Some might need to addlogger() to messages or
    override main_results."""
    def __init__(self):
        ## List of inputs, self.inputs[key] != True is considered valid input
        self.inputs = ingredient.WSRTingredient()
        ## List of outputs, should only be filled on succesful execution
        self.outputs = ingredient.WSRTingredient()
        ## All of these should do something sensible in their __str__ for
        ## simple print output

        ## Try using the standard Python system for handling options
        self.optionparser = OptionParser(
            usage = "usage: %prog [options]"
        )
        self.optionparser.remove_option('-h')
        self.optionparser.add_option(
            '-h', '--help', action = "store_true"
        )
        self.optionparser.add_option(
            '-v', '--verbose',
            action = "callback", callback = self.__setloglevel,
            help = "verbose [Default: %default]"
        )
        self.optionparser.add_option(
            '-d', '--debug',
            action = "callback", callback = self.__setloglevel,
            help = "debug [Default: %default]"
        )

        self.helptext = """LOFAR/WSRT pipeline framework"""
        self.recipe_path = ['.']

    def _log_error(self, e):
        # Sometimes, an exception will be thrown before we have any loggers
        # defined that can handle it. Check if that's the case, and, if so,
        # dump it to stderr.
        handled = len(self.logger.handlers) > 0
        my_logger = self.logger
        while my_logger.parent:
            my_logger = my_logger.parent
            if len(my_logger.handlers) > 0 and my_logger is not my_logger.root:
                handled = True
        if handled:
            self.logger.exception('Exception caught: ' + str(e))
        else:
            print >> sys.stderr, "***** Exception occurred with no log handlers"
            print >> sys.stderr, "*****", str(e)
            print_exc()

    def help(self):
        """Shows helptext and inputs and outputs of the recipe"""
        print self.helptext
        self.optionparser.print_help()
        print '\nOutputs:'
        for k in self._outfields.keys():
            print '  ' + k

    def main_init(self):
        """Main initialization for stand alone execution, reading input from
        the command line"""
        # The root logger has a null handler; we'll override in recipes.
        logging.getLogger().addHandler(NullLogHandler())
        self.logger = getSearchingLogger(self.name)
        opts = sys.argv[1:]
        try:
            myParset = parset.Parset(self.name + ".parset")
            for p in myParset.keys():
                opts[0:0] = "--" + p, myParset.getString(p)
        except IOError:
            logging.debug("Unable to open parset")
        (options, args) = self.optionparser.parse_args(opts)
        if options.help:
            return 1
        else:
            for key, value in vars(options).iteritems():
                if value is not None:
                    self.inputs[key] = value
            self.inputs['args'] = args
            return 0

    def main(self):
        """This function is to be run in standalone mode."""
        import os.path
        self.name = os.path.splitext(os.path.basename(sys.argv[0]))[0]
        status = self.main_init()
        if not status:
            status = self.run(self.name)
            self.main_result()
        else:
            self.help()
        logging.shutdown()
        return status

    def run(self, name):
        """This code will run if all inputs are valid, and wraps the actual
        functionality in self.go() with some exception handling, might need
        another name, like try_execute, because it's to similar to go()."""
        self.name = name
        self.logger.info('recipe ' + name + ' started')
        try:
            status = self.go()
            if not self.outputs.complete():
                self.logger.warn("Note: recipe outputs are not complete")
        except Exception, e:
            self._log_error(e)
            self.outputs = None ## We're not generating any results we have
                                ## confidence in
            return 1
        else:
from optparse import OptionParser
from array import *
from decimal import *

from OSUT3Analysis.Configuration.configurationOptions import *
from OSUT3Analysis.Configuration.processingUtilities import *
import ROOT

##set default plotting options
line_width = 2
plotting_options = ""

parser = OptionParser()
parser = set_commandline_arguments(parser)

parser.remove_option("-c")
parser.remove_option("-n")
parser.remove_option("-e")
parser.remove_option("-r")
parser.remove_option("-d")
parser.remove_option("--2D")

parser.add_option(
    "--hist",
    action="store_true",
    dest="plot_hist",
    default=False,
    help="plot as hollow histograms instead of error bar crosses")
parser.add_option("--line-width",
                  dest="line_width",
                  help="set line width (default is 2)")
Example #41
0
import sys
# FIX PATH

from os.path import basename
from optparse import OptionParser

from grammar import read_grammar
from automaton import Automaton
from helpers import open_executable
from version import VERSION

######################################################################
# command line options

getopt = OptionParser("usage: %prog [options] grammar")
getopt.remove_option("-h")
getopt.add_option("-d", "--debug", action="store", type="string",
                  dest="debug", default="",
                  help="enable debugging (p=parser)",
                  metavar="CHARS")
getopt.add_option("-e", "--example", action="store", dest="example_fname",
                  help="store example source code into NAME",
                  metavar="NAME")
getopt.add_option("-h", "--help", action="store_true", dest="help_flag",
                  help="show this message")
getopt.add_option("-o", "--output", action="store", dest="output_fname",
                  help="set the output file name (default is stdout)",
                  metavar="NAME")
getopt.add_option("-r", "--replace", action="store_true", dest="replace_flag",
                  help="replace nonterminals by numbers")
getopt.add_option("-V","--version",action="store_true",dest="version_flag",
Example #42
0
import os
import re
from math import *
from array import *
from decimal import *
from optparse import OptionParser
import copy
from operator import itemgetter

from OSUT3Analysis.Configuration.configurationOptions import *
from OSUT3Analysis.Configuration.processingUtilities import *
from OSUT3Analysis.Configuration.formattingUtilities import *

parser = OptionParser()
parser = set_commandline_arguments(parser)
parser.remove_option("-n")
parser.remove_option("-d")

parser.add_option("-n",
                  "--histToBeReWeighted",
                  dest="histToBeReWeighted",
                  help="histogram that will be reweighted")
parser.add_option("-i",
                  "--weightsHist",
                  dest="weightsHist",
                  help="histogram used to get weights")
parser.add_option("-f",
                  "--fileWithWtHist",
                  dest="fileWithWtHist",
                  help="file containing weights histogram")
parser.add_option("-s",
from math import *
from array import *
from decimal import *
from optparse import OptionParser
from OSUT3Analysis.Configuration.configurationOptions import *
from OSUT3Analysis.Configuration.processingUtilities import *
from OSUT3Analysis.Configuration.formattingUtilities import *



### parse the command-line options

parser = OptionParser()
parser = set_commandline_arguments(parser)

parser.remove_option("-c")
parser.remove_option("-e")
parser.remove_option("-n")
#parser.remove_option("--2D")
parser.remove_option("-y")

parser.add_option("-f", "--fancy", action="store_true", dest="makeFancy", default=False,
                  help="removes the title and replaces it with the official CMS plot heading")
parser.add_option("--dontRebinRatio", action="store_true", dest="dontRebinRatio", default=False,
                  help="don't do the rebinning of ratio plots")
parser.add_option("--noTGraph", action="store_true", dest="noTGraph", default=False,
                  help="don't make a TGraph; just make a TH1")
parser.add_option("--ylog", action="store_true", dest="setLogY", default=False,		 
                  help="Set logarithmic scale on vertical axis on all plots")	 
parser.add_option("--ymin", dest="setYMin", 
                  help="Minimum of y axis")	 
Example #44
0
class YamlOptions:
    "YamlOptions"

    def __init__(self, target):
        """
        Scan the class doc string of the given object, looking for the %YAML
        containing the option specification.  Parse the YAML and setup the
        corresponding OptionParser object.
        """
        # target - options object (input)
        # gname  - option group name

        self.y = get_yaml(target.__class__)
        self.parser = OptionParser(description=self.y['Description'], version='%prog version $Revision$')
        self.parser.remove_option('-h')
        self.parser.set_usage(self.y['Usage'])
        self.opty = self.y['Options']
        for gname in self.opty.get('Groups', []):
            self._register_group(gname)

    def _register_group(self, gname):
        """
        Register options for the specified option group name to the OptionParser
        using an OptionGroup unless the group name starts with 'Help' in which
        case we just register the options with the top level OptionParser object.
        """
        # gname    - option group name (input)
        # gy       - option group YAML object
        # grp      - option group object
        # tgt      - where to add options (parser or option group)
        # optkey   - comma separated list of option flags
        # optval   - help string or dict with detailed option settings
        # listargs - list of option flags (e.g. ['-h', '--help'])
        # dictargs - key/value arguments to add_option

        gy = self.opty.get(gname, None)
        if gname.startswith('Help'):
            grp = None
            tgt = self.parser
        else:
            grp = OptionGroup(self.parser, gname)
            tgt = grp

        for optkey, optval in gy.items():
            listargs = optkey.split(',')
            if type(optval) == type(''):
                # short form: optval is just a help string
                dictargs = {
                    'action': 'store_true',
                    'help': optval
                }
            else:
                # optval is the complete option specification
                dictargs = optval

            # hide hidden options
            if dictargs.get('help', '').startswith('hidden'):
                dictargs['help'] = SUPPRESS_HELP

            # print 'adding', listargs, dictargs
            tgt.add_option(*listargs, **dictargs)

        if grp is not None:
            self.parser.add_option_group(grp)
Example #45
0
from math import *
from array import *
from decimal import *
from optparse import OptionParser
from OSUT3Analysis.Configuration.configurationOptions import *
from OSUT3Analysis.Configuration.processingUtilities import *
from OSUT3Analysis.Configuration.formattingUtilities import *



### parse the command-line options

parser = OptionParser()
parser = set_commandline_arguments(parser)

parser.remove_option("-e")
parser.remove_option("-n")
parser.remove_option("--2D")
parser.remove_option("-y")

parser.add_option("-f", "--fancy", action="store_true", dest="makeFancy", default=False,
                  help="removes the title and replaces it with the official CMS plot heading")
parser.add_option("--dontRebinRatio", action="store_true", dest="dontRebinRatio", default=False,
                  help="don't do the rebinning of ratio plots")
parser.add_option("-A", "--addOneToRatio", action='store_true', dest="addOneToRatio",
                  help="add one to the ratio so that data/MC is plotted")
parser.add_option("-E", "--ratioRelErrMax", dest="ratioRelErrMax",
                  help="maximum error used in rebinning the ratio histogram")
parser.add_option("--ylog", action="store_true", dest="setLogY", default=False,
                  help="Set logarithmic scale on vertical axis on all plots")
parser.add_option("--ymin", dest="setYMin",
Example #46
0
class TestOptionParser(BaseTest):
    def setUp(self):
        self.parser = OptionParser()
        self.parser.add_option("-v",
                               "--verbose",
                               "-n",
                               "--noisy",
                               action="store_true",
                               dest="verbose")
        self.parser.add_option("-q",
                               "--quiet",
                               "--silent",
                               action="store_false",
                               dest="verbose")

    def test_add_option_no_Option(self):
        self.assertTypeError(self.parser.add_option,
                             "not an Option instance: None", None)

    def test_add_option_invalid_arguments(self):
        self.assertTypeError(self.parser.add_option, "invalid arguments", None,
                             None)

    def test_get_option(self):
        opt1 = self.parser.get_option("-v")
        self.assert_(isinstance(opt1, Option))
        self.assertEqual(opt1._short_opts, ["-v", "-n"])
        self.assertEqual(opt1._long_opts, ["--verbose", "--noisy"])
        self.assertEqual(opt1.action, "store_true")
        self.assertEqual(opt1.dest, "verbose")

    def test_get_option_equals(self):
        opt1 = self.parser.get_option("-v")
        opt2 = self.parser.get_option("--verbose")
        opt3 = self.parser.get_option("-n")
        opt4 = self.parser.get_option("--noisy")
        self.assert_(opt1 is opt2 is opt3 is opt4)

    def test_has_option(self):
        self.assert_(self.parser.has_option("-v"))
        self.assert_(self.parser.has_option("--verbose"))

    def assert_removed(self):
        self.assert_(self.parser.get_option("-v") is None)
        self.assert_(self.parser.get_option("--verbose") is None)
        self.assert_(self.parser.get_option("-n") is None)
        self.assert_(self.parser.get_option("--noisy") is None)

        self.failIf(self.parser.has_option("-v"))
        self.failIf(self.parser.has_option("--verbose"))
        self.failIf(self.parser.has_option("-n"))
        self.failIf(self.parser.has_option("--noisy"))

        self.assert_(self.parser.has_option("-q"))
        self.assert_(self.parser.has_option("--silent"))

    def test_remove_short_opt(self):
        self.parser.remove_option("-n")
        self.assert_removed()

    def test_remove_long_opt(self):
        self.parser.remove_option("--verbose")
        self.assert_removed()

    def test_remove_nonexistent(self):
        self.assertRaises(self.parser.remove_option,
                          ValueError,
                          "no such option 'foo'",
                          funcargs=['foo'])
Example #47
0
class LogrotateOptParser(object):
    '''
    Class for parsing commandline options of Python logrotating.

    @author: Frank Brehm
    @contact: [email protected]
    '''

    #-------------------------------------------------------
    def __init__(
        self,
        prog='%prog',
        version=None,
        local_dir=None,
    ):
        '''
        Constructor.

        @param prog:      The name of the calling process (e.g. sys.argv[0])
        @type prog:       str
        @param version:   The version string to use
        @type version:    str
        @param local_dir: The directory, where the i18n-files (*.mo)
                          are located. If None, then system default
                          (/usr/share/locale) is used.
        @type local_dir:  str or None

        @return: None
        '''

        self.prog = prog
        '''
        @ivar: The name of the calling process
        @type: str
        '''

        self.version = version
        '''
        @ivar: The version string to use
        @type: str
        '''

        self.local_dir = local_dir
        '''
        @ivar: The directory, where the i18n-files (*.mo) are located.
        @type: str or None
        '''

        self.t = gettext.translation('pylogrotate', local_dir, fallback=True)
        '''
        @ivar: a gettext translation object
        @type: gettext.translation
        '''

        _ = self.t.lgettext

        self.description = _('Rotates, compresses and mails system logs.')
        '''
        @ivar: description of the program
        @type: str
        '''

        msg = _("%s [options] <configfile>")
        self.usage = msg % (prog)
        '''
        @ivar: the usage string in getopt help output
        @type: str
        '''
        self.usage += ('       %s [-h|-?|--help]\n' % (prog))
        self.usage += ('       %s --usage\n' % (prog))
        self.usage += ('       %s --version' % (prog))

        self.options = None
        '''
        @ivar: a dict with all given commandline options
               after calling getOpts()
        @type: dict or None
        '''

        self.args = None
        '''
        @ivar: a list with all commandline parameters, what are not options
        @type: list or None
        '''

        self.parsed = False
        '''
        @ivar: flag, whether the parsing was done
        @type: bool
        '''

        if version:
            self.version = version

        self.parser = OptionParser(
            prog=self.prog,
            version=self.version,
            description=self.description,
            usage=self.usage,
            conflict_handler="resolve",
        )
        '''
        @ivar: the working OptionParser Object
        @type: optparse.OptionParser
        '''

        self._add_options()

    #-------------------------------------------------------
    def _add_options(self):
        '''
        Private function to add all necessary options
        to the OptionParser object
        '''

        #print ""
        #print "Default system encoding:     »%s«." \
        #   % (sys.getdefaultencoding())
        #print "Default filesystem encoding: »%s«." \
        #   % (sys.getfilesystemencoding())
        #print ""

        _ = self.t.lgettext

        if self.parser.has_option('--help'):
            self.parser.remove_option('--help')

        if self.parser.has_option('--version'):
            self.parser.remove_option('--version')

        msg = _('Set this do simulate commands')
        self.parser.add_option(
            '--simulate',
            '--test',
            '-T',
            default=False,
            action='store_true',
            dest='test',
            help=to_unicode_or_bust(msg),
        )

        msg = _('Set the verbosity level')
        self.parser.add_option(
            '--verbose',
            '-v',
            default=False,
            action='count',
            dest='verbose',
            help=to_unicode_or_bust(msg),
        )

        msg = _("Don't do anything, just test (implies -v and -T)")
        self.parser.add_option(
            '--debug',
            '-d',
            default=False,
            action='store_true',
            dest='debug',
            help=to_unicode_or_bust(msg),
        )

        msg = _("Force file rotation")
        self.parser.add_option(
            '--force',
            '-f',
            default=False,
            action='store_true',
            dest='force',
            help=to_unicode_or_bust(msg),
        )

        msg = _("Checks only the given configuration file and does " +
                "nothing. Conflicts with -f.")
        self.parser.add_option(
            '--config-check',
            '-c',
            default=False,
            action='store_true',
            dest='configcheck',
            help=to_unicode_or_bust(msg),
        )

        msg = _('Path of state file (different to configuration)')
        self.parser.add_option(
            '--state',
            '-s',
            dest="statefile",
            metavar='FILE',
            help=to_unicode_or_bust(msg),
        )

        msg = _('Path of PID file (different to configuration)')
        self.parser.add_option(
            '--pid-file',
            '-P',
            dest="pidfile",
            metavar='FILE',
            help=to_unicode_or_bust(msg),
        )

        msg = _("Command to send mail (instead of using SMTP or " +
                "the predefined sendmail command).")
        self.parser.add_option(
            '--mail',
            '-m',
            dest="mailcmd",
            metavar='CMD',
            help=to_unicode_or_bust(msg),
        )

        ######
        # Option group for common options

        group = OptionGroup(self.parser, _("Common options"))

        msg = _('Shows a help message and exit.')
        group.add_option(
            '-h',
            '-?',
            '--help',
            default=False,
            action='help',
            dest='help',
            help=to_unicode_or_bust(msg),
        )

        msg = _('Display brief usage message and exit.')
        group.add_option(
            '--usage',
            default=False,
            action='store_true',
            dest='usage',
            help=to_unicode_or_bust(msg),
        )

        msg = _('Shows the version number of the program and exit.')
        group.add_option(
            '-V',
            '--version',
            default=False,
            action='version',
            dest='version',
            help=to_unicode_or_bust(msg),
        )

        self.parser.add_option_group(group)

    #----------------------------------------------------------------------
    def getOpts(self):
        '''
        Wrapper function to OptionParser.parse_args().
        Sets self.options and self.args with the appropriate values.
        @return: None
        '''

        _ = self.t.lgettext

        if not self.parsed:
            self.options, self.args = self.parser.parse_args()
            self.parsed = True

        if self.options.usage:
            self.parser.print_usage()
            sys.exit(0)

        if self.options.force and self.options.configcheck:
            msg = _('Invalid usage of --force and --config-check.')
            raise LogrotateOptParserError(msg)

        if self.args is None or len(self.args) < 1:
            msg = _('No configuration file given.')
            raise LogrotateOptParserError(msg)

        if len(self.args) != 1:
            msg = _('Only one configuration file is allowed.')
            raise LogrotateOptParserError(msg)
Example #48
0
                      default=(0.0, 0.0),
                      nargs=2,
                      help="Scale factor and _absolute_ uncertainty on it")
    parser.add_option(
        "--gamma",
        dest="gamma",
        type="int",
        default=0,
        help=
        "Extract out from the scale uncertainty a gamma factor with the specified number of observed events"
    )
    parser.add_option("-v",
                      dest="verbose",
                      action="store_true",
                      help="Print out report")
    parser.remove_option("--out")
    parser.add_option(
        "-o",
        "--out",
        dest="out",
        default="{process}Card_{ll}_{jets}j.txt",
        help="Output file pattern; ll = (ee,mm,...); channel = (elel, mumu,...)"
    )
    (options, args) = parser.parse_args()
    if not options.verbose: options.final = True
    #     (mcafile, process, cutspattern) = args
    mcafile = args[0]
    process = args[1]
    cutspattern = args[2:]
    print cutspattern
Example #49
0
def main():

    global stats_array, count_array, time_array, n_rcvd
    global n_right, sync_status, mode, ch, data_packet_delivery_count
    global n_attempts
    data_packet_delivery_count_previous = 0
    n_rcvd = 0
    n_right = 0
    n_attempts = 5
    threshold = 0.01

    count_array = [ 0, 0, 0, 0, 0]
    time_array = [ 0, 0, 0, 0, 0]
    stats_array = [ 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,0]


    def send_pkt(self, payload='', eof=False):
        return self.txpath.send_pkt(payload, eof)

    def get_freq_rx():

        return 2.44*1e9

    def get_freq_tx():
#        return 8,900e6
        # Convert hop_freq to our unique channel list
        channel = int(random.choice([1,7]))
            

        hop_freq = float(1e6 * (850+(channel-1)*5))#setting the centre freq frequency for sending packets

        
        stats_array[channel] = stats_array[channel] + 1
        print "\nChannel DSA Selection Statistics (Channel #: Number times selected)"
        print "1: ", stats_array[1], " 7: ",stats_array[7]
        return channel,hop_freq #returning the channel number and hop frequency
    

    def rx_callback(ok, payload):
        
        global n_rcvd, n_right,sync_status,mode,ch,data_packet_delivery_count
        ########################## sync ####################################
        if mode == "sync":
            if ok:
                print "SYNC:GOT CHANNEL PACKET"
                (pktno,) = struct.unpack('!H', payload[0:2])
                (sync_signal,) = struct.unpack('!s', payload[2]) 
                (data_channel,) = struct.unpack('!H', payload[3:5])
                                  
                if str(sync_signal) == 'o' and str(data_channel) == str(ch):
                    sync_status = True
                    #tb.stop()
                    print "SYNC:RECEIVE CONFIRM PACKET...LINK ESTABLISHED"                           
                if str(sync_signal) == 's' and str(data_channel) == str(ch): 
                    print "SYNC:SEND CONFIRM PACKET"
                    sync_status = True
                    data = 'o'
                    pktno=0
                    ack_payload = struct.pack('!HsH', pktno & 0xffff,data,ch & 0xffff) #+ data
                    send_pkt(tb,ack_payload) #sending back the acknowledgement
        ###################################################################
            
        ######################### traffic #################################
        if mode == "traffic":
            if ok: 
                (data_header,) = struct.unpack('!s', payload[0])
                if data_header == 'd':
		    #print "TRAFFIC:SEND ACK"
                    data_packet_delivery_count = data_packet_delivery_count +1
                    comm = struct.unpack('!14s', payload[1:15])
                    data = 'dI am fine.....' #Sending this message
                    payload = struct.pack('!15s', data)
                    send_pkt(tb,payload)
                
        ##############################################################

        n_rcvd += 1
        if ok:
            n_right += 1

    mods = digital.modulation_utils.type_1_mods()
    demods = digital.modulation_utils.type_1_demods()


    #setting up the tx options parser

    parser_tx = OptionParser(option_class=eng_option, conflict_handler="resolve")
    
    parser_tx.add_option("-m", "--modulation", type="choice", choices=mods.keys(),
            default='gmsk',help="Select modulation from: %s [default=%%default]" 
            % (', '.join(mods.keys()),))
    parser_tx.add_option("-s", "--size", type="eng_float", default=1500,
                      help="set packet size [default=%default]")
    parser_tx.add_option("-M", "--megabytes", type="eng_float", default=1.0,
                      help="set megabytes to transmit [default=%default]")
    parser_tx.add_option("","--discontinuous", action="store_true", default=False,
                      help="enable discontinous transmission (bursts of 5 packets)") 
    parser_tx.add_option("","--from-file", default=None,
                          help="use file for packet contents")

 
    expert_grp_tx = parser_tx.add_option_group("Expert_tx")
    dsa_grp = parser_tx.add_option_group("DSA Options")

        
    dsa_grp.add_option("-T", "--threshold", type="eng_float", default=0.01,
                          help="set primary user sensing energy threshold [default=%default]")
  
    usrp_transmit_path.add_options(parser_tx, expert_grp_tx)
    parser_tx.remove_option('-f');
    #parser_tx.remove_option('--tx-freq');

    for mod in mods.values():
        mod.add_options(expert_grp_tx)


    (options_tx, args_tx) = parser_tx.parse_args ()

    if len(args_tx) != 0:
        parser_tx.print_help()
        sys.exit(1)
    
    ############# Setting some default values for tx side of the block
    options_tx.tx_freq = 900e6
    options_tx.samples_per_symbol =  2
    options_tx.modulation = 'gmsk'
    options_tx.fusb_block_size = 4096
    options_tx.fusb_nblocks = 16
    options_tx.bitrate = 0.0125e6
    #############

    parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve")
    expert_grp_rx = parser_rx.add_option_group("Expert_rx")
    usrp_receive_path.add_options(parser_rx, expert_grp_rx)
    
    parser_rx.remove_option('-f');
 
    (options_rx, args_rx) = parser_rx.parse_args ()

    ############# Setting some default values for rx side of the block
    options_rx.rx_freq = 2.44e9 #setting default rx_freq value
    options_rx.samples_per_symbol =  2
    options_rx.modulation = 'gmsk'
    options_rx.fusb_block_size = 4096
    options_rx.fusb_nblocks = 16
    options_rx.bitrate = 0.0125e6
    #############


    print "[[ Using the RANDOM channel selection algorithm ]]\n\n"
        
    # build the graph

    tb = my_top_block(mods[options_tx.modulation],
                      demods[options_rx.modulation],
                      rx_callback,options_tx,
                      options_rx)
    r = gr.enable_realtime_scheduling()
    if r != gr.RT_OK:
        print "Warning: failed to enable realtime scheduling"
    
    tb.start()

    #listening to random frequencies untill a match is found
    running = True

    # Scan all channels first for inital data
    #time.sleep(0.1)

    print "\n[[ Scanning channels for network nodes ]]\n"
    timeout = time.time() + timer
    while running:
	if time.time() > timeout:
	    break
        ################################################sync mode####################################
        if mode == "sync":
			if sync_status != True:
                    

                ch,hop_freq = get_freq_tx()
                hop_freq_rx = get_freq_rx()

                tb.txpath.sink.set_freq(hop_freq)
                tb.rxpath.source.set_freq(hop_freq_rx)
                print "RX_FREQ:",hop_freq_rx,"  TX_FREQ:",hop_freq
                ch_energy = level(ch) #check if primary user is present
                #print ch_energy,"*"*30
                if int(ch_energy) > threshold: #if primary user is there then dont transmit on this channel
		    time.sleep(1)
                    continue
                
                nbytes = 5 #int(1e6 * .0003)
                pkt_size = 5
                n = 0
                pktno = 0
                while n < nbytes:
                    if options_tx.from_file is None:
                        data = 's'
                    else:
                        data = source_file.read(pkt_size - 2)
                        if data == '':
                            break;

                    payload = struct.pack('!HsH', pktno & 0xffff,data,ch & 0xffff) #+ data
                            
                    send_pkt(tb,payload)
                    n += len(payload)
                    sys.stderr.write("SEND SYNC PACKET\n")
                    if options_tx.discontinuous and pktno % 5 == 4:
                        time.sleep(1)
                        pktno += 1
                time.sleep(1)
                    
            else:
                print "\n\n[[ Network Node Found: Commencing communications on CHANNEL ", ch, " ]]\n";
                n_attempts_counter = 0
                mode = "traffic"
                data_packet_delivery_count = 0
                sync_status="False"
                start_time = datetime.now() #measuring the time for which the primary user is away
    
        ################################################end of sync mode####################################

        ################################################Communications mode#################################
        if mode == "traffic":
            nbytes = 15
            pkt_size = 15
            data_pktno = 0
            n = 0
            while n < nbytes:
                if options_tx.from_file is None:
                    data = 'dHi how are you' #Sending this message
                else:
                    data = source_file.read(pkt_size - 2)
                    if data == '':
                        break;
    
            
                payload = struct.pack('!15s', data)
                                       
                send_pkt(tb,payload)
                n += len(payload)
                sys.stderr.write("SEND TRAFFIC PACKET\n")
                if options_tx.discontinuous and data_pktno % 5 == 4:
                    time.sleep(1)
                data_pktno += 1
                time.sleep(0.2 + 0.05*int(random.choice([0,1,2,3])))

                if data_packet_delivery_count == data_packet_delivery_count_previous: #checking if the data packet delivery has stagnated
                    n_attempts_counter += 1
                    if n_attempts_counter > n_attempts: #get out of the data channel as it seems that the other node is still trying to rendezvous
                        mode = "sync"
                        continue
		else:
		    data_packet_delivery_count_previous = 0
		    data_packet_delivery_count = 0

		data_packet_delivery_count_previous = data_packet_delivery_count
		ch_energy = level(ch) #check if primary user is present
		print "CHANNEL ENERGY:",ch_energy,"\n"
		if int(ch_energy) > threshold: #if primary user is there then dont transmit on this channel
		    stop_time = datetime.now()    
		    _elapsed_time  = start_time - stop_time
		    print "\n[[ Primary User Detected:  Evacuating Current Channel ]]\n"
		    print "\n[[ Scanning channels for network nodes ]]\n"
		    print "\nAbsent time:",_elapsed_time,"\n"
		    mode = "sync"
Example #50
0
def main():
    from calibre.utils.terminal import geometry
    cols = geometry()[0]

    parser = OptionParser(
        usage="usage: %prog [options] command args\n\ncommand " +
        "is one of: info, books, df, ls, cp, mkdir, touch, cat, rm, eject, test_file\n\n"
        + "For help on a particular command: %prog command",
        version=__appname__ + " version: " + __version__)
    parser.add_option(
        "--log-packets",
        help="print out packet stream to stdout. " +
        "The numbers in the left column are byte offsets that allow the packet size to be read off easily.",
        dest="log_packets",
        action="store_true",
        default=False)
    parser.remove_option("-h")
    parser.disable_interspersed_args()  # Allow unrecognized options
    options, args = parser.parse_args()

    if len(args) < 1:
        parser.print_help()
        return 1

    command = args[0]
    args = args[1:]
    dev = None
    scanner = DeviceScanner()
    scanner.scan()
    connected_devices = []

    for d in device_plugins():
        try:
            d.startup()
        except:
            print('Startup failed for device plugin: %s' % d)
        if d.MANAGES_DEVICE_PRESENCE:
            cd = d.detect_managed_devices(scanner.devices)
            if cd is not None:
                connected_devices.append((cd, d))
                dev = d
                break
            continue
        ok, det = scanner.is_device_connected(d)
        if ok:
            dev = d
            dev.reset(log_packets=options.log_packets, detected_device=det)
            connected_devices.append((det, dev))

    if dev is None:
        print('Unable to find a connected ebook reader.', file=sys.stderr)
        shutdown_plugins()
        return 1

    for det, d in connected_devices:
        try:
            d.open(det, None)
        except:
            continue
        else:
            dev = d
            d.specialize_global_preferences(device_prefs)
            break

    try:
        if command == "df":
            total = dev.total_space(end_session=False)
            free = dev.free_space()
            where = ("Memory", "Card A", "Card B")
            print("Filesystem\tSize \tUsed \tAvail \tUse%")
            for i in range(3):
                print("%-10s\t%s\t%s\t%s\t%s" %
                      (where[i], human_readable(
                          total[i]), human_readable(total[i] - free[i]),
                       human_readable(free[i]),
                       unicode_type(0 if total[i] ==
                                    0 else int(100 * (total[i] - free[i]) /
                                               (total[i] * 1.))) + "%"))
        elif command == 'eject':
            dev.eject()
        elif command == "books":
            print("Books in main memory:")
            for book in dev.books():
                print(book)
            print("\nBooks on storage carda:")
            for book in dev.books(oncard='carda'):
                print(book)
            print("\nBooks on storage cardb:")
            for book in dev.books(oncard='cardb'):
                print(book)
        elif command == "mkdir":
            parser = OptionParser(
                usage=
                "usage: %prog mkdir [options] path\nCreate a folder on the device\n\npath must begin with / or card:/"
            )
            if len(args) != 1:
                parser.print_help()
                sys.exit(1)
            dev.mkdir(args[0])
        elif command == "ls":
            parser = OptionParser(
                usage=
                "usage: %prog ls [options] path\nList files on the device\n\npath must begin with / or card:/"
            )
            parser.add_option(
                "-l",
                help=
                "In addition to the name of each file, print the file type, permissions, and  timestamp  (the  modification time, in the local timezone). Times are local.",  # noqa
                dest="ll",
                action="store_true",
                default=False)
            parser.add_option(
                "-R",
                help=
                "Recursively list subfolders encountered. /dev and /proc are omitted",
                dest="recurse",
                action="store_true",
                default=False)
            parser.remove_option("-h")
            parser.add_option("-h",
                              "--human-readable",
                              help="show sizes in human readable format",
                              dest="hrs",
                              action="store_true",
                              default=False)
            options, args = parser.parse_args(args)
            if len(args) != 1:
                parser.print_help()
                return 1
            print(ls(dev,
                     args[0],
                     recurse=options.recurse,
                     ll=options.ll,
                     human_readable_size=options.hrs,
                     cols=cols),
                  end=' ')
        elif command == "info":
            info(dev)
        elif command == "cp":
            usage="usage: %prog cp [options] source destination\nCopy files to/from the device\n\n"+\
            "One of source or destination must be a path on the device. \n\nDevice paths have the form\n"+\
            "dev:mountpoint/my/path\n"+\
            "where mountpoint is one of / or carda: or cardb:/\n\n"+\
            "source must point to a file for which you have read permissions\n"+\
            "destination must point to a file or folder for which you have write permissions"
            parser = OptionParser(usage=usage)
            parser.add_option(
                '-f',
                '--force',
                dest='force',
                action='store_true',
                default=False,
                help='Overwrite the destination file if it exists already.')
            options, args = parser.parse_args(args)
            if len(args) != 2:
                parser.print_help()
                return 1
            if args[0].startswith("dev:"):
                outfile = args[1]
                path = args[0][4:]
                if path.endswith("/"):
                    path = path[:-1]
                if os.path.isdir(outfile):
                    outfile = os.path.join(outfile, path[path.rfind("/") + 1:])
                try:
                    outfile = lopen(outfile, "wb")
                except IOError as e:
                    print(e, file=sys.stderr)
                    parser.print_help()
                    return 1
                dev.get_file(path, outfile)
                fsync(outfile)
                outfile.close()
            elif args[1].startswith("dev:"):
                try:
                    infile = lopen(args[0], "rb")
                except IOError as e:
                    print(e, file=sys.stderr)
                    parser.print_help()
                    return 1
                dev.put_file(infile, args[1][4:], replace_file=options.force)
                infile.close()
            else:
                parser.print_help()
                return 1
        elif command == "cat":
            outfile = sys.stdout
            parser = OptionParser(
                usage=
                "usage: %prog cat path\nShow file on the device\n\npath should point to a file on the device and must begin with /,a:/ or b:/"
            )
            options, args = parser.parse_args(args)
            if len(args) != 1:
                parser.print_help()
                return 1
            if args[0].endswith("/"):
                path = args[0][:-1]
            else:
                path = args[0]
            outfile = sys.stdout
            dev.get_file(path, outfile)
        elif command == "rm":
            parser = OptionParser(
                usage=
                "usage: %prog rm path\nDelete files from the device\n\npath should point to a file or empty folder on the device "
                + "and must begin with / or card:/\n\n" +
                "rm will DELETE the file. Be very CAREFUL")
            options, args = parser.parse_args(args)
            if len(args) != 1:
                parser.print_help()
                return 1
            dev.rm(args[0])
        elif command == "touch":
            parser = OptionParser(
                usage=
                "usage: %prog touch path\nCreate an empty file on the device\n\npath should point to a file on the device and must begin with /,a:/ or b:/\n\n"
                +  # noqa
                "Unfortunately, I cant figure out how to update file times on the device, so if path already exists, touch does nothing"
            )
            options, args = parser.parse_args(args)
            if len(args) != 1:
                parser.print_help()
                return 1
            dev.touch(args[0])
        elif command == 'test_file':
            parser = OptionParser(usage=(
                "usage: %prog test_file path\n"
                'Open device, copy file specified by path to device and '
                'then eject device.'))
            options, args = parser.parse_args(args)
            if len(args) != 1:
                parser.print_help()
                return 1
            path = args[0]
            from calibre.ebooks.metadata.meta import get_metadata
            mi = get_metadata(lopen(path, 'rb'),
                              path.rpartition('.')[-1].lower())
            print(
                dev.upload_books([args[0]], [os.path.basename(args[0])],
                                 end_session=False,
                                 metadata=[mi]))
            dev.eject()
        else:
            parser.print_help()
            if getattr(dev, 'handle', False):
                dev.close()
            return 1
    except DeviceLocked:
        print("The device is locked. Use the --unlock option", file=sys.stderr)
    except (ArgumentError, DeviceError) as e:
        print(e, file=sys.stderr)
        return 1
    finally:
        shutdown_plugins()

    return 0
Example #51
0
class YamlOptions:
    "YamlOptions"

    def __init__(self, target):
        """
        Scan the class doc string of the given object, looking for the %YAML
        containing the option specification.  Parse the YAML and setup the
        corresponding OptionParser object.
        """
        # target - options object (input)
        # gname  - option group name

        self.y = get_yaml(target.__class__)
        self.parser = OptionParser(description=self.y['Description'],
                                   version='%prog version $Revision$')
        self.parser.remove_option('-h')
        self.parser.set_usage(self.y['Usage'])
        self.opty = self.y['Options']
        for gname in self.opty.get('Groups', []):
            self._register_group(gname)

    def _register_group(self, gname):
        """
        Register options for the specified option group name to the OptionParser
        using an OptionGroup unless the group name starts with 'Help' in which
        case we just register the options with the top level OptionParser object.
        """
        # gname    - option group name (input)
        # gy       - option group YAML object
        # grp      - option group object
        # tgt      - where to add options (parser or option group)
        # optkey   - comma separated list of option flags
        # optval   - help string or dict with detailed option settings
        # listargs - list of option flags (e.g. ['-h', '--help'])
        # dictargs - key/value arguments to add_option

        gy = self.opty.get(gname, None)
        if gname.startswith('Help'):
            grp = None
            tgt = self.parser
        else:
            grp = OptionGroup(self.parser, gname)
            tgt = grp

        for optkey, optval in gy.items():
            listargs = optkey.split(',')
            if type(optval) == type(''):
                # short form: optval is just a help string
                dictargs = {'action': 'store_true', 'help': optval}
            else:
                # optval is the complete option specification
                dictargs = optval

            # hide hidden options
            if dictargs.get('help', '').startswith('hidden'):
                dictargs['help'] = SUPPRESS_HELP

            # print 'adding', listargs, dictargs
            tgt.add_option(*listargs, **dictargs)

        if grp is not None:
            self.parser.add_option_group(grp)
Example #52
0
def main(argv):
    usage = """usage: %prog --calc=expression --outfile=out_filename [-A filename]
                    [--A_band=n] [-B...-Z filename] [other_options]"""
    parser = OptionParser(usage)

    # define options
    parser.add_option("--calc", dest="calc", action="append", help="calculation in gdalnumeric syntax using +-/* or any numpy array functions (i.e. log10()). May appear multiple times to produce a multi-band file", metavar="expression")
    add_alpha_args(parser, argv)

    parser.add_option("--outfile", dest="outF", help="output file to generate or fill", metavar="filename")
    parser.add_option("--NoDataValue", dest="NoDataValue", type=float, help="output nodata value (default datatype specific value)", metavar="value")
    parser.add_option("--hideNoData", dest="hideNoData", action="store_true", help="ignores the NoDataValues of the input rasters")
    parser.add_option("--type", dest="type", help="output datatype", choices=GDALDataTypeNames, metavar="datatype")
    parser.add_option("--format", dest="format", help="GDAL format for output file", metavar="gdal_format")
    parser.add_option(
        "--creation-option", "--co", dest="creation_options", default=[], action="append",
        help="Passes a creation option to the output format driver. Multiple "
        "options may be listed. See format specific documentation for legal "
        "creation options for each format.", metavar="option")
    parser.add_option("--allBands", dest="allBands", default="", help="process all bands of given raster (a-z, A-Z)", metavar="[a-z, A-Z]")
    parser.add_option("--overwrite", dest="overwrite", action="store_true", help="overwrite output file if it already exists")
    parser.add_option("--debug", dest="debug", action="store_true", help="print debugging information")
    parser.add_option("--quiet", dest="quiet", action="store_true", help="suppress progress messages")
    parser.add_option("--optfile", dest="optfile", metavar="optfile", help="Read the named file and substitute the contents into the command line options list.")

    # parser.add_option("--color_table", dest="color_table", help="color table file name")
    parser.add_option("--extent", dest="extent",
                      choices=['ignore', 'fail'],
                      help="how to treat mixed geotrasnforms")
    parser.add_option("--projectionCheck", dest="projectionCheck", action="store_true",
                      help="check that all rasters share the same projection")

    (opts, args) = parser.parse_args(argv[1:])

    if not hasattr(opts, "input_files"):
        opts.input_files = {}
    if not hasattr(opts, "user_namespace"):
        opts.user_namespace = {}

    if opts.optfile:
        with open(opts.optfile, 'r') as f:
            ofargv = [x for line in f for x in shlex.split(line, comments=True)]
        # Avoid potential recursion.
        parser.remove_option('--optfile')
        add_alpha_args(parser, ofargv)
        ofopts, ofargs = parser.parse_args(ofargv)

        # Let options given directly override the optfile.
        input_files = getattr(ofopts, 'input_files', {})
        input_files.update(opts.input_files)
        user_namespace = getattr(ofopts, 'user_namespace', {})
        user_namespace.update(opts.user_namespace)
        ofopts.__dict__.update({k: v for k, v in vars(opts).items() if v})
        opts = ofopts
        opts.input_files = input_files
        opts.user_namespace = user_namespace
        args = args + ofargs

    if len(argv) == 1:
        parser.print_help()
        sys.exit(1)
    elif not opts.calc:
        print("No calculation provided. Nothing to do!")
        parser.print_help()
        sys.exit(1)
    elif not opts.outF:
        print("No output file provided. Cannot proceed.")
        parser.print_help()
        sys.exit(1)
    else:
        try:
            doit(opts, args)
        except IOError as e:
            print(e)
            sys.exit(1)
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA

import sys, os
from optparse import OptionParser, _

import MySQLdb



parser = OptionParser()

parser.remove_option('-h')

parser.add_option("", "--help", action="help",
                    help=_("show this help message and exit"))

parser.add_option("-h", "--host", dest="host", default="localhost",
                    help="Connect to host.",
                    metavar="HOST")

parser.add_option("-u", "--user", dest="user",
                    help="User login if not current user.",
                    metavar="USER")

parser.add_option("-p", "--password", dest="password", default='',
                    help="Password to use when connecting to server. If password is not given it's asked from the tty.",
                    metavar="PASSWORD")
Example #54
0
from optparse import OptionParser
from array import *
from decimal import *

from OSUT3Analysis.Configuration.configurationOptions import *
from OSUT3Analysis.Configuration.processingUtilities import *


##set default plotting options
line_width = 2
plotting_options = ""

parser = OptionParser()
parser = set_commandline_arguments(parser)

parser.remove_option("-c")
parser.remove_option("-n")
parser.remove_option("-e")
parser.remove_option("-d")
parser.remove_option("--2D")


parser.add_option("--hist", action="store_true", dest="plot_hist", default=False,
                  help="plot as hollow histograms instead of error bar crosses")
parser.add_option("--line-width", dest="line_width",
                  help="set line width (default is 2)")
parser.add_option("--pdf", action="store_true", dest="plot_savePdf", default=False,
                  help="save plot as pdf in addition")


(arguments, args) = parser.parse_args()
Example #55
0
import os
import re
import math
from array import *
from decimal import *
from optparse import OptionParser
from OSUT3Analysis.Configuration.configurationOptions import *
from OSUT3Analysis.Configuration.processingUtilities import *
from OSUT3Analysis.Configuration.formattingUtilities import *

### parse the command-line options

parser = OptionParser()
parser = set_commandline_arguments(parser)

parser.remove_option("-o")
parser.remove_option("-n")
parser.remove_option("-u")
parser.remove_option("-e")
parser.remove_option("-r")
parser.remove_option("-R")
parser.remove_option("-d")
parser.remove_option("-b")
parser.remove_option("--2D")
parser.remove_option("-y")
parser.remove_option("-p")
parser.remove_option("-c")

(arguments, args) = parser.parse_args()

if arguments.localConfig:
Example #56
0
def main():
    usage = """usage: %prog --calc=expression --outfile=out_filename [-A filename]
                    [--A_band=n] [-B...-Z filename] [other_options]"""
    parser = OptionParser(usage)

    # define options
    parser.add_option(
        "--calc",
        dest="calc",
        help=
        "calculation in gdalnumeric syntax using +-/* or any numpy array functions (i.e. log10())",
        metavar="expression")
    add_alpha_args(parser, sys.argv)

    parser.add_option("--outfile",
                      dest="outF",
                      help="output file to generate or fill",
                      metavar="filename")
    parser.add_option(
        "--NoDataValue",
        dest="NoDataValue",
        type=float,
        help="output nodata value (default datatype specific value)",
        metavar="value")
    parser.add_option("--type",
                      dest="type",
                      help="output datatype, must be one of %s" %
                      list(DefaultNDVLookup.keys()),
                      metavar="datatype")
    parser.add_option("--format",
                      dest="format",
                      help="GDAL format for output file",
                      metavar="gdal_format")
    parser.add_option(
        "--creation-option",
        "--co",
        dest="creation_options",
        default=[],
        action="append",
        help="Passes a creation option to the output format driver. Multiple "
        "options may be listed. See format specific documentation for legal "
        "creation options for each format.",
        metavar="option")
    parser.add_option("--allBands",
                      dest="allBands",
                      default="",
                      help="process all bands of given raster (A-Z)",
                      metavar="[A-Z]")
    parser.add_option("--overwrite",
                      dest="overwrite",
                      action="store_true",
                      help="overwrite output file if it already exists")
    parser.add_option("--debug",
                      dest="debug",
                      action="store_true",
                      help="print debugging information")
    parser.add_option("--quiet",
                      dest="quiet",
                      action="store_true",
                      help="suppress progress messages")
    parser.add_option(
        "--optfile",
        dest="optfile",
        metavar="optfile",
        help=
        "Read the named file and substitute the contents into the command line options list."
    )

    (opts, args) = parser.parse_args()

    if not hasattr(opts, "input_files"):
        opts.input_files = {}

    if opts.optfile:
        with open(opts.optfile, 'r') as f:
            ofargv = [
                x for line in f for x in line.strip().split(' ')
                if line[0] != '#'
            ]
        # Avoid potential recursion.
        parser.remove_option('--optfile')
        add_alpha_args(parser, ofargv)
        ofopts, ofargs = parser.parse_args(ofargv)
        # Let options given directly override the optfile.
        input_files = getattr(ofopts, 'input_files', {})
        input_files.update(opts.input_files)
        ofopts.__dict__.update({k: v for k, v in vars(opts).items() if v})
        opts = ofopts
        opts.input_files = input_files
        args = args + ofargs

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)
    elif not opts.calc:
        print("No calculation provided. Nothing to do!")
        parser.print_help()
        sys.exit(1)
    elif not opts.outF:
        print("No output file provided. Cannot proceed.")
        parser.print_help()
        sys.exit(1)
    else:
        try:
            doit(opts, args)
        except IOError as e:
            print(e)
            sys.exit(1)
Example #57
0
        if e < 0.2*sqrt(y+1): continue
        if verbose: print "\tbin %3d: yield %9.1f +- %9.1f (rel %.5f), rel err. %.4f, poisson %.1f" % (b, y, e, y/ytot, e/y if y else 1, sqrt(y+1))
        hi = ref.Clone(); hi.SetDirectory(None)
        lo = ref.Clone(); lo.SetDirectory(None)
        hi.SetBinContent(b, y+e)
        lo.SetBinContent(b, y*y/(y+e))
        tlist.Add(hi)
        tlist.Add(lo)
        ret.append("bin%d" % b)
    return ret

if __name__ == "__main__":
    from optparse import OptionParser
    parser = OptionParser(usage="%prog [options] mc.txt cuts.txt ids.txt xvars.txt")
    addMCEfficiencyOptions(parser)
    for dup in ["--showRatio","--rebin","--sP","--xP","--legendWidth"]: parser.remove_option(dup)
    addPlotMakerOptions(parser,  addAlsoMCAnalysis=False)
    parser.add_option("-i", "--in", dest="infile", default=None, help="Input file name");
    parser.add_option("--algo", dest="algo", default="fitND", help="Algorithm to use");
    parser.add_option("--bare", dest="bare", action="store_true", default=False, help="Just make the first histograms and stop");
    parser.add_option("--subSyst", dest="subSyst", type="float", default=0, help="Extra systematic on EWK subtraction");
    parser.add_option("--shapeSystSignal", dest="shapeSystSig", type="string", default="l", help="Shape systematic for signal: l = linear, q = quadratic, s = stretch");
    parser.add_option("--shapeSystBackground", dest="shapeSystBkg", type="string", default="l", help="Shape systematic for background: l = linear, q = quadratic, s = stretch");
    parser.add_option("--fcut", dest="fcut", default=None, nargs=2, type='float', help="Cut in the discriminating variable");
    parser.add_option("--fqcd-ranges", dest="fqcdRanges", default=(0.0, 20.0, 50.0, 120.0), nargs=4, type='float', help="Boundaries for the fqcd method");
    parser.add_option("--same-nd-templates", dest="sameNDTemplates", action="store_true", default=False, help="Just make the first histograms and stop");
    (options, args) = parser.parse_args()
    mca  = MCAnalysis(args[0],options)
    procs = mca.listProcesses()
    cut = CutsFile(args[1],options).allCuts()
    ids   = PlotFile(args[2],options).plots()
Example #58
0
            out.append(thing)
        else:
            yield out
            out = [thing]
    yield out

if __name__ == '__main__':
    from optparse import OptionParser
    import pymongo
    parser = OptionParser('%prog [options] action database filename')
    parser.description = 'action is either "dump" or "restore"'
    parser.set_defaults(
        hostname='localhost',
        port=27017,
    )
    parser.remove_option('-h') # we're going to override it
    parser.add_option('-h', '--host', action='store', dest='hostname', help='Host name to connect to')
    parser.add_option('-p', '--port', action='store', dest='port', help='Port number to connect to')
    parser.add_option('-d', '--database', action='store', dest='database', help='Database to operate on')
    parser.add_option('-?', '--help', action='help', help='Show this help message')

    options, args = parser.parse_args()

    if len(args) != 3:
        sys.exit(parser.error('action, database, and filename required'))

    action, dbname, filename = args
    action = action.lower()
    if action not in ('dump', 'restore'):
        sys.exit(parser.error('action must be "dump" or "restore"'))