def set_options(self): """ Set options to be used in this plugin according to the passed options dictionary. Dictionary values are all strings, since they were read from XML. Here we need to convert them to the needed types. We use default values to determine the type. """ # First we set options_dict values based on the saved options options = self.saved_option_list.get_options() docgen_names = self.option_list_collection.docgen_names for option_name, option_data in options.items(): if (option_name in self.options_dict and isinstance(option_data, list) and option_data and option_data[0] in docgen_names): try: converter = get_type_converter( self.options_dict[option_name]) self.options_dict[option_name] = converter(option_data[1]) except (TypeError, ValueError): pass
def set_options(self): """ Set options to be used in this plugin according to the passed options dictionary. Dictionary values are all strings, since they were read from XML. Here we need to convert them to the needed types. We use default values to determine the type. """ # First we set options_dict values based on the saved options options = self.saved_option_list.get_options() docgen_names = self.option_list_collection.docgen_names for option_name, option_data in options.items(): if ( option_name in self.options_dict and isinstance(option_data, list) and option_data and option_data[0] in docgen_names ): try: converter = get_type_converter( self.options_dict[option_name]) self.options_dict[option_name] = converter(option_data[1]) except (TypeError, ValueError): pass
def parse_args(self): """ Fill in lists with open, exports, imports, and actions options. Any errors are added to self.errors """ try: # Convert arguments to unicode, otherwise getopt will not work # if a non latin character is used as an option (by mistake). # getopt will try to treat the first char in an utf-8 sequence. Example: # -Ärik is '-\xc3\x84rik' and getopt will respond : # option -\xc3 not recognized for arg in range(len(self.args) - 1): self.args[arg + 1] = conv_to_unicode(self.args[arg + 1], sys.stdin.encoding) options, leftargs = getopt.getopt(self.args[1:], SHORTOPTS, LONGOPTS) except getopt.GetoptError as msg: # Extract the arguments in the list. # The % operator replaces the list elements with repr() of the list elemements # which is OK for latin characters, but not for non latin characters in list elements cliargs = "[ " for arg in range(len(self.args) - 1): cliargs += self.args[arg + 1] + " " cliargs += "]" # Must first do str() of the msg object. msg = str(msg) self.errors += [ (_('Error parsing the arguments'), msg + '\n' + _("Error parsing the arguments: %s \n" "Type gramps --help for an overview of commands, or " "read the manual pages.") % cliargs) ] return if leftargs: # if there were an argument without option, # use it as a file to open and return self.open_gui = leftargs[0] print(_("Trying to open: %s ...") % leftargs[0], file=sys.stderr) #see if force open is on for opt_ix in range(len(options)): option, value = options[opt_ix] if option in ('-u', '--force-unlock'): self.force_unlock = True break return # Go over all given option and place them into appropriate lists cleandbg = [] need_to_quit = False for opt_ix in range(len(options)): option, value = options[opt_ix] if option in ['-O', '--open']: self.open = value elif option in ['-C', '--create']: self.create = value elif option in ['-i', '--import']: family_tree_format = None if opt_ix < len(options) - 1 \ and options[opt_ix + 1][0] in ( '-f', '--format'): family_tree_format = options[opt_ix + 1][1] self.imports.append((value, family_tree_format)) elif option in ['-e', '--export']: family_tree_format = None if opt_ix < len(options) - 1 \ and options[opt_ix + 1][0] in ( '-f', '--format'): family_tree_format = options[opt_ix + 1][1] self.exports.append((value, family_tree_format)) elif option in ['-a', '--action']: action = value if action not in ('report', 'tool', 'book'): print(_("Unknown action: %s. Ignoring.") % action, file=sys.stderr) continue options_str = "" if opt_ix < len(options)-1 \ and options[opt_ix+1][0] in ( '-p', '--options' ): options_str = options[opt_ix + 1][1] self.actions.append((action, options_str)) elif option in ['-d', '--debug']: print(_('setup debugging'), value, file=sys.stderr) logger = logging.getLogger(value) logger.setLevel(logging.DEBUG) cleandbg += [opt_ix] elif option in ['-l']: self.list = True elif option in ['-L']: self.list_more = True elif option in ['-t']: self.list_table = True elif option in ['-s', '--show']: print(_("Gramps config settings from %s:") % config.filename) for section in config.data: for setting in config.data[section]: print("%s.%s=%s" % (section, setting, repr(config.data[section][setting]))) print() sys.exit(0) elif option in ['-c', '--config']: setting_name = value set_value = False if setting_name: if ":" in setting_name: setting_name, new_value = setting_name.split(":", 1) set_value = True if config.has_default(setting_name): setting_value = config.get(setting_name) print(_("Current Gramps config setting: " "%(name)s:%(value)s") % { 'name': setting_name, 'value': repr(setting_value) }, file=sys.stderr) if set_value: # does a user want the default config value? if new_value in ("DEFAULT", _("DEFAULT")): new_value = config.get_default(setting_name) else: converter = get_type_converter(setting_value) new_value = converter(new_value) config.set(setting_name, new_value) # translators: indent "New" to match "Current" print(_(" New Gramps config setting: " "%(name)s:%(value)s") % { 'name': setting_name, 'value': repr(config.get(setting_name)) }, file=sys.stderr) else: need_to_quit = True else: print(_("Gramps: no such config setting: '%s'") % setting_name, file=sys.stderr) need_to_quit = True cleandbg += [opt_ix] elif option in ['-h', '-?', '--help']: self.help = True elif option in ['-u', '--force-unlock']: self.force_unlock = True elif option in ['--usage']: self.usage = True elif option in ['--qml']: self.runqml = True elif option in ['-y', '--yes']: self.auto_accept = True elif option in ['-q', '--quiet']: self.quiet = True #clean options list cleandbg.reverse() for ind in cleandbg: del options[ind] if len(options) > 0 and self.open is None and self.imports == [] \ and not (self.list or self.list_more or self.list_table or self.help or self.runqml): # Extract and convert to unicode the arguments in the list. # The % operator replaces the list elements with repr() of # the list elements, which is OK for latin characters # but not for non-latin characters in list elements cliargs = "[ " for arg in range(len(self.args) - 1): cliargs += conv_to_unicode(self.args[arg + 1], sys.stdin.encoding) + ' ' cliargs += "]" self.errors += [(_('Error parsing the arguments'), _("Error parsing the arguments: %s \n" "To use in the command-line mode, supply at " "least one input file to process.") % cliargs)] if need_to_quit: sys.exit(0)
def parse_args(self): """ Fill in lists with open, exports, imports, and actions options. Any errors are added to self.errors """ try: options, leftargs = getopt.getopt(self.args[1:], SHORTOPTS, LONGOPTS) except getopt.GetoptError as msg: # Extract the arguments in the list. # The % operator replaces the list elements # with repr() of the list elements # which is OK for latin characters, # but not for non latin characters in list elements cliargs = "[ " for arg in range(len(self.args) - 1): cliargs += self.args[arg + 1] + " " cliargs += "]" # Must first do str() of the msg object. msg = str(msg) self.errors += [(_('Error parsing the arguments'), msg + '\n' + _("Error parsing the arguments: %s \n" "Type gramps --help for an overview of " "commands, or read the manual pages." ) % cliargs)] return # Some args can work on a list of databases: if leftargs: for opt_ix in range(len(options)): option, value = options[opt_ix] if option in ['-L', '-l', '-t']: self.database_names = leftargs leftargs = [] if leftargs: # if there were an argument without option, # use it as a file to open and return self.open_gui = leftargs[0] print(_("Trying to open: %s ..." ) % leftargs[0], file=sys.stderr) #see if force open is on for opt_ix in range(len(options)): option, value = options[opt_ix] if option in ('-u', '--force-unlock'): self.force_unlock = True break return # Go over all given option and place them into appropriate lists cleandbg = [] need_to_quit = False for opt_ix in range(len(options)): option, value = options[opt_ix] if option in ['-O', '--open']: self.open = value elif option in ['-C', '--create']: self.create = value elif option in ['-U', '--username']: self.username = value elif option in ['-P', '--password']: self.password = value elif option in ['-i', '--import']: family_tree_format = None if (opt_ix < len(options) - 1 and options[opt_ix + 1][0] in ('-f', '--format')): family_tree_format = options[opt_ix + 1][1] self.imports.append((value, family_tree_format)) elif option in ['-r', '--remove']: self.removes.append(value) elif option in ['-e', '--export']: family_tree_format = None if (opt_ix < len(options) - 1 and options[opt_ix + 1][0] in ('-f', '--format')): family_tree_format = options[opt_ix + 1][1] abs_name = os.path.abspath(os.path.expanduser(value)) if not os.path.exists(abs_name): # The file doesn't exists, try to create it. try: open(abs_name, 'w').close() os.unlink(abs_name) except OSError as e: message = _("WARNING: %(strerr)s " "(errno=%(errno)s):\n" "WARNING: %(name)s\n") % { 'strerr' : e.strerror, 'errno' : e.errno, 'name' : e.filename} print(message) sys.exit(1) self.exports.append((value, family_tree_format)) elif option in ['-a', '--action']: action = value if action not in ('report', 'tool', 'book'): print(_("Unknown action: %s. Ignoring." ) % action, file=sys.stderr) continue options_str = "" if (opt_ix < len(options)-1 and options[opt_ix+1][0] in ('-p', '--options')): options_str = options[opt_ix+1][1] self.actions.append((action, options_str)) elif option in ['-d', '--debug']: print(_('setup debugging'), value, file=sys.stderr) logger = logging.getLogger(value) logger.setLevel(logging.DEBUG) cleandbg += [opt_ix] elif option in ['-l']: self.list = True elif option in ['-L']: self.list_more = True elif option in ['-t']: self.list_table = True elif option in ['-s', '--show']: from gramps.gen.config import config print(_("Gramps config settings from %s:" ) % config.filename) for sect in config.data: for setting in config.data[sect]: print("%s.%s=%s" % (sect, setting, repr(config.data[sect][setting]))) print() sys.exit(0) elif option in ['-c', '--config']: from gramps.gen.config import config cfg_name = value set_value = False if cfg_name: if ":" in cfg_name: cfg_name, new_value = cfg_name.split(":", 1) set_value = True if config.has_default(cfg_name): setting_value = config.get(cfg_name) print(_("Current Gramps config setting: " "%(name)s:%(value)s" ) % {'name' : cfg_name, 'value' : repr(setting_value)}, file=sys.stderr) if set_value: # does a user want the default config value? if new_value in ("DEFAULT", _("DEFAULT")): new_value = config.get_default(cfg_name) else: converter = get_type_converter(setting_value) new_value = converter(new_value) config.set(cfg_name, new_value) # translators: indent "New" to match "Current" print(_(" New Gramps config setting: " "%(name)s:%(value)s" ) % {'name' : cfg_name, 'value' : repr(config.get(cfg_name))}, file=sys.stderr) else: need_to_quit = True else: print(_("Gramps: no such config setting: '%s'" ) % cfg_name, file=sys.stderr) need_to_quit = True cleandbg += [opt_ix] elif option in ['-h', '-?', '--help']: self.help = True elif option in ['-u', '--force-unlock']: self.force_unlock = True elif option in ['--usage']: self.usage = True elif option in ['-y', '--yes']: self.auto_accept = True elif option in ['-q', '--quiet']: self.quiet = True elif option in ['-S', '--safe']: cleandbg += [opt_ix] elif option in ['-D', '--default']: def rmtree(path): if os.path.isdir(path): shutil.rmtree(path, ignore_errors=True) if 'E' in value or 'A' in value: # clear addons rmtree(USER_PLUGINS) if 'E' in value or 'P' in value: # clear ini preferences for fil in glob(os.path.join(VERSION_DIR, "*.*")): if "custom_filters.xml" in fil: continue os.remove(fil) # create gramps.ini so config won't load the one from an # older version of Gramps. with open(os.path.join(VERSION_DIR, 'gramps.ini'), 'w'): pass if 'E' in value or 'F' in value: # clear filters fil = os.path.join(VERSION_DIR, "custom_filters.xml") if os.path.isfile(fil): os.remove(fil) if 'E' in value or 'X' in value: # clear xml reports/tools for fil in glob(os.path.join(HOME_DIR, "*.xml")): os.remove(fil) if 'E' in value or 'Z' in value: # clear upgrade zips for fil in glob(os.path.join(HOME_DIR, "*.zip")): os.remove(fil) if 'E' in value: # Everything else rmtree(TEMP_DIR) rmtree(THUMB_DIR) rmtree(USER_CSS) rmtree(ENV_DIR) rmtree(os.path.join(HOME_DIR, "maps")) for fil in glob(os.path.join(HOME_DIR, "*")): if os.path.isfile(fil): os.remove(fil) sys.exit(0) # Done with Default #clean options list cleandbg.reverse() for ind in cleandbg: del options[ind] if (len(options) > 0 and self.open is None and self.imports == [] and self.removes == [] and not (self.list or self.list_more or self.list_table or self.help)): # Extract and convert to unicode the arguments in the list. # The % operator replaces the list elements with repr() of # the list elements, which is OK for latin characters # but not for non-latin characters in list elements cliargs = "[ " for arg in range(len(self.args) - 1): cliargs += self.args[arg + 1] + ' ' cliargs += "]" self.errors += [(_('Error parsing the arguments'), _("Error parsing the arguments: %s \n" "To use in the command-line mode, supply at " "least one input file to process." ) % cliargs)] if need_to_quit: sys.exit(0)
def parse_args(self): """ Fill in lists with open, exports, imports, and actions options. Any errors are added to self.errors """ try: options, leftargs = getopt.getopt(self.args[1:], SHORTOPTS, LONGOPTS) except getopt.GetoptError as msg: # Extract the arguments in the list. # The % operator replaces the list elements # with repr() of the list elements # which is OK for latin characters, # but not for non latin characters in list elements cliargs = "[ " for arg in range(len(self.args) - 1): cliargs += self.args[arg + 1] + " " cliargs += "]" # Must first do str() of the msg object. msg = str(msg) self.errors += [(_('Error parsing the arguments'), msg + '\n' + _("Error parsing the arguments: %s \n" "Type gramps --help for an overview of " "commands, or read the manual pages." ) % cliargs)] return # Some args can work on a list of databases: if leftargs: for opt_ix in range(len(options)): option, value = options[opt_ix] if option in ['-L', '-l', '-t']: self.database_names = leftargs leftargs = [] if leftargs: # if there were an argument without option, # use it as a file to open and return self.open_gui = leftargs[0] print(_("Trying to open: %s ..." ) % leftargs[0], file=sys.stderr) #see if force open is on for opt_ix in range(len(options)): option, value = options[opt_ix] if option in ('-u', '--force-unlock'): self.force_unlock = True break return # Go over all given option and place them into appropriate lists cleandbg = [] need_to_quit = False for opt_ix in range(len(options)): option, value = options[opt_ix] if option in ['-O', '--open']: self.open = value elif option in ['-C', '--create']: self.create = value elif option in ['-U', '--username']: self.username = value elif option in ['-P', '--password']: self.password = value elif option in ['-i', '--import']: family_tree_format = None if (opt_ix < len(options) - 1 and options[opt_ix + 1][0] in ('-f', '--format')): family_tree_format = options[opt_ix + 1][1] self.imports.append((value, family_tree_format)) elif option in ['-r', '--remove']: self.removes.append(value) elif option in ['-e', '--export']: family_tree_format = None if (opt_ix < len(options) - 1 and options[opt_ix + 1][0] in ('-f', '--format')): family_tree_format = options[opt_ix + 1][1] abs_name = os.path.abspath(os.path.expanduser(value)) if not os.path.exists(abs_name): # The file doesn't exists, try to create it. try: open(abs_name, 'w').close() os.unlink(abs_name) except OSError as e: message = _("WARNING: %(strerr)s " "(errno=%(errno)s):\n" "WARNING: %(name)s\n") % { 'strerr' : e.strerror, 'errno' : e.errno, 'name' : e.filename} print(message) sys.exit(1) self.exports.append((value, family_tree_format)) elif option in ['-a', '--action']: action = value if action not in ('report', 'tool', 'book'): print(_("Unknown action: %s. Ignoring." ) % action, file=sys.stderr) continue options_str = "" if (opt_ix < len(options)-1 and options[opt_ix+1][0] in ('-p', '--options')): options_str = options[opt_ix+1][1] self.actions.append((action, options_str)) elif option in ['-d', '--debug']: print(_('setup debugging'), value, file=sys.stderr) logger = logging.getLogger(value) logger.setLevel(logging.DEBUG) cleandbg += [opt_ix] elif option in ['-l']: self.list = True elif option in ['-L']: self.list_more = True elif option in ['-t']: self.list_table = True elif option in ['-s', '--show']: print(_("Gramps config settings from %s:" ) % config.filename) for sect in config.data: for setting in config.data[sect]: print("%s.%s=%s" % (sect, setting, repr(config.data[sect][setting]))) print() sys.exit(0) elif option in ['-c', '--config']: cfg_name = value set_value = False if cfg_name: if ":" in cfg_name: cfg_name, new_value = cfg_name.split(":", 1) set_value = True if config.has_default(cfg_name): setting_value = config.get(cfg_name) print(_("Current Gramps config setting: " "%(name)s:%(value)s" ) % {'name' : cfg_name, 'value' : repr(setting_value)}, file=sys.stderr) if set_value: # does a user want the default config value? if new_value in ("DEFAULT", _("DEFAULT")): new_value = config.get_default(cfg_name) else: converter = get_type_converter(setting_value) new_value = converter(new_value) config.set(cfg_name, new_value) # translators: indent "New" to match "Current" print(_(" New Gramps config setting: " "%(name)s:%(value)s" ) % {'name' : cfg_name, 'value' : repr(config.get(cfg_name))}, file=sys.stderr) else: need_to_quit = True else: print(_("Gramps: no such config setting: '%s'" ) % cfg_name, file=sys.stderr) need_to_quit = True cleandbg += [opt_ix] elif option in ['-h', '-?', '--help']: self.help = True elif option in ['-u', '--force-unlock']: self.force_unlock = True elif option in ['--usage']: self.usage = True elif option in ['-y', '--yes']: self.auto_accept = True elif option in ['-q', '--quiet']: self.quiet = True #clean options list cleandbg.reverse() for ind in cleandbg: del options[ind] if (len(options) > 0 and self.open is None and self.imports == [] and self.removes == [] and not (self.list or self.list_more or self.list_table or self.help)): # Extract and convert to unicode the arguments in the list. # The % operator replaces the list elements with repr() of # the list elements, which is OK for latin characters # but not for non-latin characters in list elements cliargs = "[ " for arg in range(len(self.args) - 1): cliargs += self.args[arg + 1] + ' ' cliargs += "]" self.errors += [(_('Error parsing the arguments'), _("Error parsing the arguments: %s \n" "To use in the command-line mode, supply at " "least one input file to process." ) % cliargs)] if need_to_quit: sys.exit(0)
def parse_args(self): """ Fill in lists with open, exports, imports, and actions options. Any errors are added to self.errors """ try: options, leftargs = getopt.getopt(self.args[1:], SHORTOPTS, LONGOPTS) except getopt.GetoptError as msg: # Extract the arguments in the list. # The % operator replaces the list elements # with repr() of the list elements # which is OK for latin characters, # but not for non latin characters in list elements cliargs = "[ " for arg in range(len(self.args) - 1): cliargs += self.args[arg + 1] + " " cliargs += "]" # Must first do str() of the msg object. msg = str(msg) self.errors += [(_('Error parsing the arguments'), msg + '\n' + _("Error parsing the arguments: %s \n" "Type gramps --help for an overview of " "commands, or read the manual pages." ) % cliargs)] return # Some args can work on a list of databases: if leftargs: for opt_ix in range(len(options)): option, value = options[opt_ix] if option in ['-L', '-l', '-t']: self.database_names = leftargs leftargs = [] if leftargs: # if there were an argument without option, # use it as a file to open and return self.open_gui = leftargs[0] print(_("Trying to open: %s ..." ) % leftargs[0], file=sys.stderr) #see if force open is on for opt_ix in range(len(options)): option, value = options[opt_ix] if option in ('-u', '--force-unlock'): self.force_unlock = True break return # Go over all given option and place them into appropriate lists cleandbg = [] need_to_quit = False for opt_ix in range(len(options)): option, value = options[opt_ix] if option in ['-O', '--open']: self.open = value elif option in ['-C', '--create']: self.create = value elif option in ['-i', '--import']: family_tree_format = None if (opt_ix < len(options) - 1 and options[opt_ix + 1][0] in ('-f', '--format')): family_tree_format = options[opt_ix + 1][1] self.imports.append((value, family_tree_format)) elif option in ['-r', '--remove']: self.removes.append(value) elif option in ['-e', '--export']: family_tree_format = None if (opt_ix < len(options) - 1 and options[opt_ix + 1][0] in ('-f', '--format')): family_tree_format = options[opt_ix + 1][1] self.exports.append((value, family_tree_format)) elif option in ['-a', '--action']: action = value if action not in ('report', 'tool', 'book'): print(_("Unknown action: %s. Ignoring." ) % action, file=sys.stderr) continue options_str = "" if (opt_ix < len(options)-1 and options[opt_ix+1][0] in ('-p', '--options')): options_str = options[opt_ix+1][1] self.actions.append((action, options_str)) elif option in ['-d', '--debug']: print(_('setup debugging'), value, file=sys.stderr) logger = logging.getLogger(value) logger.setLevel(logging.DEBUG) cleandbg += [opt_ix] elif option in ['-l']: self.list = True elif option in ['-L']: self.list_more = True elif option in ['-t']: self.list_table = True elif option in ['-s', '--show']: print(_("Gramps config settings from %s:" ) % config.filename) for sect in config.data: for setting in config.data[sect]: print("%s.%s=%s" % (sect, setting, repr(config.data[sect][setting]))) print() sys.exit(0) elif option in ['-b', '--databases']: default = config.data["database"]["backend"] pmgr = BasePluginManager.get_instance() pmgr.reg_plugins(PLUGINS_DIR, self, None) pmgr.reg_plugins(USER_PLUGINS, self, None, load_on_reg=True) for plugin in pmgr.get_reg_databases(): pdata = pmgr.get_plugin(plugin.id) mod = pmgr.load_plugin(pdata) if mod: database = getattr(mod, pdata.databaseclass) summary = database.get_class_summary() print("Database backend ID:", pdata.id, "(default)" if pdata.id == default else "") for key in sorted(summary.keys()): print(" ", _("%s:") % key, summary[key]) sys.exit(0) elif option in ['-c', '--config']: cfg_name = value set_value = False if cfg_name: if ":" in cfg_name: cfg_name, new_value = cfg_name.split(":", 1) set_value = True if config.has_default(cfg_name): setting_value = config.get(cfg_name) print(_("Current Gramps config setting: " "%(name)s:%(value)s" ) % {'name' : cfg_name, 'value' : repr(setting_value)}, file=sys.stderr) if set_value: # does a user want the default config value? if new_value in ("DEFAULT", _("DEFAULT")): new_value = config.get_default(cfg_name) else: converter = get_type_converter(setting_value) new_value = converter(new_value) config.set(cfg_name, new_value) # translators: indent "New" to match "Current" print(_(" New Gramps config setting: " "%(name)s:%(value)s" ) % {'name' : cfg_name, 'value' : repr(config.get(cfg_name))}, file=sys.stderr) else: need_to_quit = True else: print(_("Gramps: no such config setting: '%s'" ) % cfg_name, file=sys.stderr) need_to_quit = True cleandbg += [opt_ix] elif option in ['-h', '-?', '--help']: self.help = True elif option in ['-u', '--force-unlock']: self.force_unlock = True elif option in ['--usage']: self.usage = True elif option in ['-y', '--yes']: self.auto_accept = True elif option in ['-q', '--quiet']: self.quiet = True #clean options list cleandbg.reverse() for ind in cleandbg: del options[ind] if (len(options) > 0 and self.open is None and self.imports == [] and self.removes == [] and not (self.list or self.list_more or self.list_table or self.help)): # Extract and convert to unicode the arguments in the list. # The % operator replaces the list elements with repr() of # the list elements, which is OK for latin characters # but not for non-latin characters in list elements cliargs = "[ " for arg in range(len(self.args) - 1): cliargs += self.args[arg + 1] + ' ' cliargs += "]" self.errors += [(_('Error parsing the arguments'), _("Error parsing the arguments: %s \n" "To use in the command-line mode, supply at " "least one input file to process." ) % cliargs)] if need_to_quit: sys.exit(0)
def parse_args(self): """ Fill in lists with open, exports, imports, and actions options. Any errors are added to self.errors """ try: options, leftargs = getopt.getopt(self.args[1:], SHORTOPTS, LONGOPTS) except getopt.GetoptError as msg: # Extract the arguments in the list. # The % operator replaces the list elements with repr() of the list elemements # which is OK for latin characters, but not for non latin characters in list elements cliargs = "[ " for arg in range(len(self.args) - 1): cliargs += self.args[arg + 1] + " " cliargs += "]" # Must first do str() of the msg object. msg = str(msg) self.errors += [ (_('Error parsing the arguments'), msg + '\n' + _("Error parsing the arguments: %s \n" "Type gramps --help for an overview of commands, or " "read the manual pages.") % cliargs) ] return # Some args can work on a list of databases: if leftargs: for opt_ix in range(len(options)): option, value = options[opt_ix] if option in ['-L', '-l', '-t']: self.database_names = leftargs leftargs = [] if leftargs: # if there were an argument without option, # use it as a file to open and return self.open_gui = leftargs[0] print(_("Trying to open: %s ...") % leftargs[0], file=sys.stderr) #see if force open is on for opt_ix in range(len(options)): option, value = options[opt_ix] if option in ('-u', '--force-unlock'): self.force_unlock = True break return # Go over all given option and place them into appropriate lists cleandbg = [] need_to_quit = False for opt_ix in range(len(options)): option, value = options[opt_ix] if option in ['-O', '--open']: self.open = value elif option in ['-C', '--create']: self.create = value elif option in ['-i', '--import']: family_tree_format = None if opt_ix < len(options) - 1 \ and options[opt_ix + 1][0] in ( '-f', '--format'): family_tree_format = options[opt_ix + 1][1] self.imports.append((value, family_tree_format)) elif option in ['-r', '--remove']: self.removes.append(value) elif option in ['-e', '--export']: family_tree_format = None if opt_ix < len(options) - 1 \ and options[opt_ix + 1][0] in ( '-f', '--format'): family_tree_format = options[opt_ix + 1][1] self.exports.append((value, family_tree_format)) elif option in ['-a', '--action']: action = value if action not in ('report', 'tool', 'book'): print(_("Unknown action: %s. Ignoring.") % action, file=sys.stderr) continue options_str = "" if opt_ix < len(options)-1 \ and options[opt_ix+1][0] in ( '-p', '--options' ): options_str = options[opt_ix + 1][1] self.actions.append((action, options_str)) elif option in ['-d', '--debug']: print(_('setup debugging'), value, file=sys.stderr) logger = logging.getLogger(value) logger.setLevel(logging.DEBUG) cleandbg += [opt_ix] elif option in ['-l']: self.list = True elif option in ['-L']: self.list_more = True elif option in ['-t']: self.list_table = True elif option in ['-s', '--show']: print(_("Gramps config settings from %s:") % config.filename) for section in config.data: for setting in config.data[section]: print("%s.%s=%s" % (section, setting, repr(config.data[section][setting]))) print() sys.exit(0) elif option in ['-b', '--databases']: default = config.data["behavior"]["database-backend"] pmgr = BasePluginManager.get_instance() pmgr.reg_plugins(PLUGINS_DIR, self, None) pmgr.reg_plugins(USER_PLUGINS, self, None, load_on_reg=True) for plugin in pmgr.get_reg_databases(): pdata = pmgr.get_plugin(plugin.id) mod = pmgr.load_plugin(pdata) if mod: database = getattr(mod, pdata.databaseclass) summary = database.get_class_summary() print("Database backend ID:", pdata.id, "(default)" if pdata.id == default else "") for key in sorted(summary.keys()): print(" ", "%s:" % key, summary[key]) sys.exit(0) elif option in ['-c', '--config']: setting_name = value set_value = False if setting_name: if ":" in setting_name: setting_name, new_value = setting_name.split(":", 1) set_value = True if config.has_default(setting_name): setting_value = config.get(setting_name) print(_("Current Gramps config setting: " "%(name)s:%(value)s") % { 'name': setting_name, 'value': repr(setting_value) }, file=sys.stderr) if set_value: # does a user want the default config value? if new_value in ("DEFAULT", _("DEFAULT")): new_value = config.get_default(setting_name) else: converter = get_type_converter(setting_value) new_value = converter(new_value) config.set(setting_name, new_value) # translators: indent "New" to match "Current" print(_(" New Gramps config setting: " "%(name)s:%(value)s") % { 'name': setting_name, 'value': repr(config.get(setting_name)) }, file=sys.stderr) else: need_to_quit = True else: print(_("Gramps: no such config setting: '%s'") % setting_name, file=sys.stderr) need_to_quit = True cleandbg += [opt_ix] elif option in ['-h', '-?', '--help']: self.help = True elif option in ['-u', '--force-unlock']: self.force_unlock = True elif option in ['--usage']: self.usage = True elif option in ['-y', '--yes']: self.auto_accept = True elif option in ['-q', '--quiet']: self.quiet = True #clean options list cleandbg.reverse() for ind in cleandbg: del options[ind] if (len(options) > 0 and self.open is None and self.imports == [] and self.removes == [] and not (self.list or self.list_more or self.list_table or self.help)): # Extract and convert to unicode the arguments in the list. # The % operator replaces the list elements with repr() of # the list elements, which is OK for latin characters # but not for non-latin characters in list elements cliargs = "[ " for arg in range(len(self.args) - 1): cliargs += self.args[arg + 1] + ' ' cliargs += "]" self.errors += [(_('Error parsing the arguments'), _("Error parsing the arguments: %s \n" "To use in the command-line mode, supply at " "least one input file to process.") % cliargs)] if need_to_quit: sys.exit(0)
def parse_args(self): """ Fill in lists with open, exports, imports, and actions options. Any errors are added to self.errors """ try: options, leftargs = getopt.getopt(self.args[1:], SHORTOPTS, LONGOPTS) except getopt.GetoptError as getopt_error: self.errors.append( self.construct_error( "Type gramps --help for an overview of " "commands, or read the manual pages.", error=getopt_error)) return # Some args can work on a list of databases: if leftargs: for option, value in options: if option in ['-L', '-l', '-t']: self.database_names = leftargs leftargs = [] if leftargs: # if there were an argument without option, # use it as a file to open and return self.open_gui = leftargs[0] print(_("Trying to open: %s ...") % leftargs[0], file=sys.stderr) #see if force open is on for option, value in options: if option in ('-u', '--force-unlock'): self.force_unlock = True break return # Go over all given option and place them into appropriate lists cleandbg = [] need_to_quit = False for opt_ix, (option, value) in enumerate(options): if option in ['-O', '--open']: self.open = value elif option in ['-C', '--create']: self.create = value elif option in ['-U', '--username']: self.username = value elif option in ['-P', '--password']: self.password = value elif option in ['-i', '--import']: family_tree_format = None if (opt_ix < len(options) - 1 and options[opt_ix + 1][0] in ('-f', '--format')): family_tree_format = options[opt_ix + 1][1] self.imports.append((value, family_tree_format)) elif option in ['-r', '--remove']: self.removes.append(value) elif option in ['-e', '--export']: family_tree_format = None if (opt_ix < len(options) - 1 and options[opt_ix + 1][0] in ('-f', '--format')): family_tree_format = options[opt_ix + 1][1] abs_name = os.path.abspath(os.path.expanduser(value)) if not os.path.exists(abs_name): # The file doesn't exists, try to create it. try: open(abs_name, 'w').close() os.unlink(abs_name) except OSError as e: message = _("WARNING: %(strerr)s " "(errno=%(errno)s):\n" "WARNING: %(name)s\n") % { 'strerr': e.strerror, 'errno': e.errno, 'name': e.filename } print(message) sys.exit(1) self.exports.append((value, family_tree_format)) elif option in ['-a', '--action']: action = value if action not in ('report', 'tool', 'book'): print(_("Unknown action: %s. Ignoring.") % action, file=sys.stderr) continue options_str = "" if (opt_ix < len(options) - 1 and options[opt_ix + 1][0] in ('-p', '--options')): options_str = options[opt_ix + 1][1] self.actions.append((action, options_str)) elif option in ['-d', '--debug']: print(_('setup debugging'), value, file=sys.stderr) logger = logging.getLogger(value) logger.setLevel(logging.DEBUG) cleandbg += [opt_ix] elif option in ['-l']: self.list = True elif option in ['-L']: self.list_more = True elif option in ['-t']: self.list_table = True elif option in ['-s', '--show']: from gramps.gen.config import config print(_("Gramps config settings from %s:") % config.filename) for sect, settings in config.data.items(): for settings_index, setting in settings.items(): print("%s.%s=%s" % (sect, settings_index, repr(value))) print() sys.exit(0) elif option in ['-c', '--config']: from gramps.gen.config import config cfg_name = value set_value = False if cfg_name: if ":" in cfg_name: cfg_name, new_value = cfg_name.split(":", 1) set_value = True if config.has_default(cfg_name): setting_value = config.get(cfg_name) print(_("Current Gramps config setting: " "%(name)s:%(value)s") % { 'name': cfg_name, 'value': repr(setting_value) }, file=sys.stderr) if set_value: # does a user want the default config value? if new_value in ("DEFAULT", _("DEFAULT")): new_value = config.get_default(cfg_name) else: converter = get_type_converter(setting_value) new_value = converter(new_value) config.set(cfg_name, new_value) # Translators: indent "New" to match "Current" print(_(" New Gramps config setting: " "%(name)s:%(value)s") % { 'name': cfg_name, 'value': repr(config.get(cfg_name)) }, file=sys.stderr) else: need_to_quit = True else: print(_("Gramps: no such config setting: '%s'") % cfg_name, file=sys.stderr) need_to_quit = True cleandbg += [opt_ix] elif option in ['-h', '-?', '--help']: self.help = True elif option in ['-u', '--force-unlock']: self.force_unlock = True elif option in ['--usage']: self.usage = True elif option in ['-y', '--yes']: self.auto_accept = True elif option in ['-q', '--quiet']: self.quiet = True elif option in ['-S', '--safe']: cleandbg += [opt_ix] elif option in ['-D', '--default']: def rmtree(path): if os.path.isdir(path): shutil.rmtree(path, ignore_errors=True) if 'E' in value or 'A' in value: # clear addons rmtree(USER_PLUGINS) if 'E' in value or 'P' in value: # clear ini preferences for fil in glob(os.path.join(VERSION_DIR, "*.*")): if "custom_filters.xml" in fil: continue os.remove(fil) # create gramps.ini so config won't load the one from an # older version of Gramps. with open(os.path.join(VERSION_DIR, 'gramps.ini'), 'w'): pass if 'E' in value or 'F' in value: # clear filters fil = os.path.join(VERSION_DIR, "custom_filters.xml") if os.path.isfile(fil): os.remove(fil) if 'E' in value or 'X' in value: # clear xml reports/tools for fil in glob(os.path.join(HOME_DIR, "*.xml")): os.remove(fil) if 'E' in value or 'Z' in value: # clear upgrade zips for fil in glob(os.path.join(HOME_DIR, "*.zip")): os.remove(fil) if 'E' in value: # Everything else rmtree(THUMB_DIR) rmtree(USER_CSS) rmtree(ENV_DIR) rmtree(os.path.join(HOME_DIR, "maps")) for fil in glob(os.path.join(HOME_DIR, "*")): if os.path.isfile(fil): os.remove(fil) sys.exit(0) # Done with Default #clean options list cleandbg.reverse() for ind in cleandbg: del options[ind] if (len(options) > 0 and self.open is None and self.imports == [] and self.removes == [] and not (self.list or self.list_more or self.list_table or self.help)): self.errors.append( self.construct_error( "To use in the command-line mode, supply at " "least one input file to process.")) if need_to_quit: sys.exit(0)
def parse_args(self): """ Fill in lists with open, exports, imports, and actions options. Any errors are added to self.errors """ try: # Convert arguments to unicode, otherwise getopt will not work # if a non latin character is used as an option (by mistake). # getopt will try to treat the first char in an utf-8 sequence. Example: # -Ärik is '-\xc3\x84rik' and getopt will respond : # option -\xc3 not recognized for arg in range(len(self.args) - 1): self.args[arg+1] = conv_to_unicode(self.args[arg + 1], sys.stdin.encoding) options, leftargs = getopt.getopt(self.args[1:], SHORTOPTS, LONGOPTS) except getopt.GetoptError as msg: # Extract the arguments in the list. # The % operator replaces the list elements with repr() of the list elemements # which is OK for latin characters, but not for non latin characters in list elements cliargs = "[ " for arg in range(len(self.args) - 1): cliargs += self.args[arg + 1] + " " cliargs += "]" # Must first do str() of the msg object. msg = str(msg) self.errors += [(_('Error parsing the arguments'), msg + '\n' + _("Error parsing the arguments: %s \n" "Type gramps --help for an overview of commands, or " "read the manual pages.") % cliargs)] return if leftargs: # if there were an argument without option, # use it as a file to open and return self.open_gui = leftargs[0] print(_("Trying to open: %s ...") % leftargs[0], file=sys.stderr) #see if force open is on for opt_ix in range(len(options)): option, value = options[opt_ix] if option in ('-u', '--force-unlock'): self.force_unlock = True break return # Go over all given option and place them into appropriate lists cleandbg = [] need_to_quit = False for opt_ix in range(len(options)): option, value = options[opt_ix] if option in ['-O', '--open']: self.open = value elif option in ['-C', '--create']: self.create = value elif option in ['-i', '--import']: family_tree_format = None if opt_ix < len(options) - 1 \ and options[opt_ix + 1][0] in ( '-f', '--format'): family_tree_format = options[opt_ix + 1][1] self.imports.append((value, family_tree_format)) elif option in ['-e', '--export']: family_tree_format = None if opt_ix < len(options) - 1 \ and options[opt_ix + 1][0] in ( '-f', '--format'): family_tree_format = options[opt_ix + 1][1] self.exports.append((value, family_tree_format)) elif option in ['-a', '--action']: action = value if action not in ('report', 'tool', 'book'): print(_("Unknown action: %s. Ignoring.") % action, file=sys.stderr) continue options_str = "" if opt_ix < len(options)-1 \ and options[opt_ix+1][0] in ( '-p', '--options' ): options_str = options[opt_ix+1][1] self.actions.append((action, options_str)) elif option in ['-d', '--debug']: print(_('setup debugging'), value, file=sys.stderr) logger = logging.getLogger(value) logger.setLevel(logging.DEBUG) cleandbg += [opt_ix] elif option in ['-l']: self.list = True elif option in ['-L']: self.list_more = True elif option in ['-t']: self.list_table = True elif option in ['-s','--show']: print(_("Gramps config settings from %s:") % config.filename) for section in config.data: for setting in config.data[section]: print("%s.%s=%s" % (section, setting, repr(config.data[section][setting]))) print() sys.exit(0) elif option in ['-c', '--config']: setting_name = value set_value = False if setting_name: if ":" in setting_name: setting_name, new_value = setting_name.split(":", 1) set_value = True if config.has_default(setting_name): setting_value = config.get(setting_name) print(_("Current Gramps config setting: " "%(name)s:%(value)s") % {'name' : setting_name, 'value' : repr(setting_value)}, file=sys.stderr) if set_value: # does a user want the default config value? if new_value in ("DEFAULT", _("DEFAULT")): new_value = config.get_default(setting_name) else: converter = get_type_converter(setting_value) new_value = converter(new_value) config.set(setting_name, new_value) # translators: indent "New" to match "Current" print(_(" New Gramps config setting: " "%(name)s:%(value)s") % {'name' : setting_name, 'value' : repr( config.get(setting_name))}, file=sys.stderr) else: need_to_quit = True else: print(_("Gramps: no such config setting: '%s'") % setting_name, file=sys.stderr) need_to_quit = True cleandbg += [opt_ix] elif option in ['-h', '-?', '--help']: self.help = True elif option in ['-u', '--force-unlock']: self.force_unlock = True elif option in ['--usage']: self.usage = True elif option in ['--qml']: self.runqml = True elif option in ['-y', '--yes']: self.auto_accept = True elif option in ['-q', '--quiet']: self.quiet = True #clean options list cleandbg.reverse() for ind in cleandbg: del options[ind] if len(options) > 0 and self.open is None and self.imports == [] \ and not (self.list or self.list_more or self.list_table or self.help or self.runqml): # Extract and convert to unicode the arguments in the list. # The % operator replaces the list elements with repr() of # the list elements, which is OK for latin characters # but not for non-latin characters in list elements cliargs = "[ " for arg in range(len(self.args) - 1): cliargs += conv_to_unicode(self.args[arg + 1], sys.stdin.encoding) + ' ' cliargs += "]" self.errors += [(_('Error parsing the arguments'), _("Error parsing the arguments: %s \n" "To use in the command-line mode, supply at " "least one input file to process.") % cliargs)] if need_to_quit: sys.exit(0)
def parse_args(self): """ Fill in lists with open, exports, imports, and actions options. Any errors are added to self.errors """ try: options, leftargs = getopt.getopt(self.args[1:], SHORTOPTS, LONGOPTS) except getopt.GetoptError as msg: # Extract the arguments in the list. # The % operator replaces the list elements with repr() of the list elemements # which is OK for latin characters, but not for non latin characters in list elements cliargs = "[ " for arg in range(len(self.args) - 1): cliargs += self.args[arg + 1] + " " cliargs += "]" # Must first do str() of the msg object. msg = str(msg) self.errors += [ ( _("Error parsing the arguments"), msg + "\n" + _( "Error parsing the arguments: %s \n" "Type gramps --help for an overview of commands, or " "read the manual pages." ) % cliargs, ) ] return if leftargs: # if there were an argument without option, # use it as a file to open and return self.open_gui = leftargs[0] print(_("Trying to open: %s ...") % leftargs[0], file=sys.stderr) # see if force open is on for opt_ix in range(len(options)): option, value = options[opt_ix] if option in ("-u", "--force-unlock"): self.force_unlock = True break return # Go over all given option and place them into appropriate lists cleandbg = [] need_to_quit = False for opt_ix in range(len(options)): option, value = options[opt_ix] if option in ["-O", "--open"]: self.open = value elif option in ["-C", "--create"]: self.create = value elif option in ["-i", "--import"]: family_tree_format = None if opt_ix < len(options) - 1 and options[opt_ix + 1][0] in ("-f", "--format"): family_tree_format = options[opt_ix + 1][1] self.imports.append((value, family_tree_format)) elif option in ["-e", "--export"]: family_tree_format = None if opt_ix < len(options) - 1 and options[opt_ix + 1][0] in ("-f", "--format"): family_tree_format = options[opt_ix + 1][1] self.exports.append((value, family_tree_format)) elif option in ["-a", "--action"]: action = value if action not in ("report", "tool", "book"): print(_("Unknown action: %s. Ignoring.") % action, file=sys.stderr) continue options_str = "" if opt_ix < len(options) - 1 and options[opt_ix + 1][0] in ("-p", "--options"): options_str = options[opt_ix + 1][1] self.actions.append((action, options_str)) elif option in ["-d", "--debug"]: print(_("setup debugging"), value, file=sys.stderr) logger = logging.getLogger(value) logger.setLevel(logging.DEBUG) cleandbg += [opt_ix] elif option in ["-l"]: self.list = True elif option in ["-L"]: self.list_more = True elif option in ["-t"]: self.list_table = True elif option in ["-s", "--show"]: print(_("Gramps config settings from %s:") % config.filename) for section in config.data: for setting in config.data[section]: print("%s.%s=%s" % (section, setting, repr(config.data[section][setting]))) print() sys.exit(0) elif option in ["-c", "--config"]: setting_name = value set_value = False if setting_name: if ":" in setting_name: setting_name, new_value = setting_name.split(":", 1) set_value = True if config.has_default(setting_name): setting_value = config.get(setting_name) print( _("Current Gramps config setting: " "%(name)s:%(value)s") % {"name": setting_name, "value": repr(setting_value)}, file=sys.stderr, ) if set_value: # does a user want the default config value? if new_value in ("DEFAULT", _("DEFAULT")): new_value = config.get_default(setting_name) else: converter = get_type_converter(setting_value) new_value = converter(new_value) config.set(setting_name, new_value) # translators: indent "New" to match "Current" print( _(" New Gramps config setting: " "%(name)s:%(value)s") % {"name": setting_name, "value": repr(config.get(setting_name))}, file=sys.stderr, ) else: need_to_quit = True else: print(_("Gramps: no such config setting: '%s'") % setting_name, file=sys.stderr) need_to_quit = True cleandbg += [opt_ix] elif option in ["-h", "-?", "--help"]: self.help = True elif option in ["-u", "--force-unlock"]: self.force_unlock = True elif option in ["--usage"]: self.usage = True elif option in ["--qml"]: self.runqml = True elif option in ["-y", "--yes"]: self.auto_accept = True elif option in ["-q", "--quiet"]: self.quiet = True # clean options list cleandbg.reverse() for ind in cleandbg: del options[ind] if ( len(options) > 0 and self.open is None and self.imports == [] and not (self.list or self.list_more or self.list_table or self.help or self.runqml) ): # Extract and convert to unicode the arguments in the list. # The % operator replaces the list elements with repr() of # the list elements, which is OK for latin characters # but not for non-latin characters in list elements cliargs = "[ " for arg in range(len(self.args) - 1): cliargs += self.args[arg + 1] + " " cliargs += "]" self.errors += [ ( _("Error parsing the arguments"), _( "Error parsing the arguments: %s \n" "To use in the command-line mode, supply at " "least one input file to process." ) % cliargs, ) ] if need_to_quit: sys.exit(0)