def add_option(self, *args, **kwargs): if 'action' in kwargs and kwargs['action'] == 'store_hive': if 'subopt' in kwargs: raise OptParseError( """option can't have a `subopt' attr and `action="store_hive"' at the same time""" ) if not 'type' in kwargs: kwargs['type'] = 'string' elif 'subopt' in kwargs: o = self.option_class(*args, **kwargs) oo = self.get_option(o.baseopt) if oo: if oo.action != "store_hive": raise OptionConflictError( "can't add subopt as option has already a handler that doesn't do `store_hive'", oo) else: self.add_option(o.baseopt, action='store_hive', metavar="sub1,[sub2,...]") oo = self.get_option(o.baseopt) oo.register_sub(o) args = (o, ) kwargs = {} return OptionParser.add_option(self, *args, **kwargs)
def add_option(self, *args, **kwargs): if isinstance(args[0], GroupingOption): raise OptParseError('use add_group to add GroupingOptions') try: group = kwargs['group'] except KeyError: return OptionParser.add_option(self, *args, **kwargs) opt_str = args[0] if group not in self.groups: raise OptParseError( 'GroupingOption must be added before GroupOptions can be') option = GroupOption(opt_str, **kwargs) option = self.groups[group].add_option(option) return option
def parseOptions(self): """Parse options for zenhubworker. Override parseOptions to capture the worklistId argument. """ super(ZenHubWorker, self).parseOptions() if len(self.args) == 0: raise OptParseError("ZenHub worklist name not specified") self.worklistId = self.args[0] self.instanceId = "%s_%s" % (self.worklistId, self.options.workerid)
def parse_args(self, args=None, values=None): try: return optparse.OptionParser.parse_args(self, args, values) except gobject.GError, error: if error.domain != gobject.OPTION_ERROR: raise if error.code == gobject.OPTION_ERROR_BAD_VALUE: raise OptionValueError(error.message) elif error.code == gobject.OPTION_ERROR_UNKNOWN_OPTION: raise BadOptionError(error.message) elif error.code == gobject.OPTION_ERROR_FAILED: raise OptParseError(error.message) else: raise
def postprocess_args(opts, args): """ Return inputfile, pointsfile, labelsfile, minutes, zoom, fonts after optparser.parse_args(). """ try: inputfiles = args[0:] except IndexError: raise OptParseError('Input filename is required.') for inputfile in inputfiles: if not exists(inputfile): raise OptParseError('Non-existent input filename: "%(inputfile)s".' % locals()) minutes = opts.minutes if minutes <= 0: raise OptParseError('Minutes must be greater than 0: "%(minutes).1f".' % locals()) fonts = {} fontfile, fontsize = opts.countryfont try: fontsize = int(fontsize) except ValueError: raise OptParseError('Bad font size for countries: "%(fontsize)s".' % locals()) if not exists(fontfile): raise OptParseError('Non-existent font filename for counties: "%(fontfile)s".' % locals()) fonts['country'] = truetype(fontfile, fontsize, encoding='unic') for opt in ('pop25mfont', 'pop250kfont', 'pop50kfont', 'popotherfont'): population = opt[3:-4] fontfile, fontsize = getattr(opts, opt) try: fontsize = int(fontsize) except ValueError: raise OptParseError('Bad font size for population %(population)s: "%(fontsize)s".' % locals()) if not exists(fontfile): raise OptParseError('Non-existent font filename for population %(population)s: "%(fontfile)s".' % locals()) fonts[population] = truetype(fontfile, fontsize, encoding='unic') zoom = opts.zoom countriesfile = opts.countries pointsfile = opts.points labelsfile = opts.labels return countriesfile, inputfiles, pointsfile, labelsfile, minutes, zoom, fonts
def _process_long_opt(self, args, values): for arg in args.pop(0).split(','): try: opt, value = arg.split('=') except ValueError: opt, value = arg, None try: option = self._long_opt[opt] except KeyError: raise BadOptionError(opt) if option.takes_value(): if option.nargs != 1: raise OptParseError( 'grouped options must only take a single argument') if value is None: self.error('option %s requires an argument' % opt) option.process(opt, value, values, self)
def parse_args(self, args=None, values=None): try: options, args = optparse.OptionParser.parse_args( self, args, values) except GError: error = sys.exc_info()[1] if error.domain != OPTION_CONTEXT_ERROR_QUARK: raise if error.code == GLib.OptionError.BAD_VALUE: raise OptionValueError(error.message) elif error.code == GLib.OptionError.UNKNOWN_OPTION: raise BadOptionError(error.message) elif error.code == GLib.OptionError.FAILED: raise OptParseError(error.message) else: raise for group in self.option_groups: for key, value in group.values.__dict__.items(): options.ensure_value(key, value) return options, args
def parse_args(self, args=None, values=None): old_args = args or [] try: options, args = optparse.OptionParser.parse_args( self, args, values) except _glib.GError: error = sys.exc_info()[1] if error.domain != _glib.OPTION_ERROR: raise if error.code == _glib.OPTION_ERROR_BAD_VALUE: raise OptionValueError(error.message) elif error.code == _glib.OPTION_ERROR_UNKNOWN_OPTION: raise BadOptionError(error.message) elif error.code == _glib.OPTION_ERROR_FAILED: raise OptParseError(error.message) else: raise for group in self.option_groups: for key, value in group.values.__dict__.items(): options.ensure_value(key, value) args = args[2:-len(old_args)] return options, args
def exit(self, status=0, msg=None): for l in msg.splitlines(): self.logger.error(l) raise OptParseError(msg)
def error(self, msg): raise OptParseError(msg)