def __init__(self, dbstate, parser, sessionmanager, errorfunc=None, gui=False): self.dbstate = dbstate self.sm = sessionmanager self.errorfunc = errorfunc self.gui = gui if self.gui: self.actions = [] self.list = False self.list_more = False self.open_gui = None else: self.actions = parser.actions self.list = parser.list self.list_more = parser.list_more self.list_table = parser.list_table self.open_gui = parser.open_gui self.imp_db_path = None self.dbman = CLIDbManager(self.dbstate) self.force_unlock = parser.force_unlock self.cl = 0 self.imports = [] self.exports = [] self.open = self.__handle_open_option(parser.open, parser.create) self.sanitize_args(parser.imports, parser.exports)
def __init__(self, dbstate, parser, sessionmanager, errorfunc=None, gui=False): self.dbstate = dbstate self.sm = sessionmanager self.errorfunc = errorfunc self.gui = gui if self.gui: self.actions = [] self.list = False self.list_more = False self.open_gui = None else: self.actions = parser.actions self.list = parser.list self.list_more = parser.list_more self.open_gui = parser.open_gui self.imp_db_path = None self.dbman = CLIDbManager(self.dbstate) self.force_unlock = parser.force_unlock self.cl = 0 self.imports = [] self.exports = [] self.open = self.__handle_open_option(parser.open) self.sanitize_args(parser.imports, parser.exports)
class ArgHandler(object): """ This class is responsible for the non GUI handling of commands. The handler is passed a parser object, sanitizes it, and can execute the actions requested working on a DbState. """ def __init__(self, dbstate, parser, sessionmanager, errorfunc=None, gui=False): self.dbstate = dbstate self.sm = sessionmanager self.errorfunc = errorfunc self.gui = gui if self.gui: self.actions = [] self.list = False self.list_more = False self.open_gui = None else: self.actions = parser.actions self.list = parser.list self.list_more = parser.list_more self.open_gui = parser.open_gui self.imp_db_path = None self.dbman = CLIDbManager(self.dbstate) self.force_unlock = parser.force_unlock self.cl = 0 self.imports = [] self.exports = [] self.open = self.__handle_open_option(parser.open) self.sanitize_args(parser.imports, parser.exports) def __error(self, string): """ Output an error. Uses errorfunc if given, otherwise a simple print. """ if self.errorfunc: self.errorfunc(string) else: # Need to convert to str before printing # For non latin characters in Windows path/file/user names print string.encode(sys.getfilesystemencoding()) #------------------------------------------------------------------------- # Argument parser: sorts out given arguments #------------------------------------------------------------------------- def sanitize_args(self, importlist, exportlist): """ Check the lists with open, exports, imports, and actions options. """ for (value, family_tree_format) in importlist: self.__handle_import_option(value, family_tree_format) for (value, family_tree_format) in exportlist: self.__handle_export_option(value, family_tree_format) def __handle_open_option(self, value): """ Handle the "-O" or "--open" option. Only Family trees or a dir with a family tree can be opened. """ if value is None: return None value = Utils.get_unicode_path_from_env_var(value) db_path = self.__deduce_db_path(value) if db_path: # We have a potential database path. # Check if it is good. if not self.check_db(db_path, self.force_unlock): sys.exit(0) return db_path else: self.__error( _('Error: Input family tree "%s" does not exist.\n' "If GEDCOM, Gramps-xml or grdb, use the -i option to " "import into a family tree instead.") % value) sys.exit(0) def __handle_import_option(self, value, family_tree_format): """ Handle the "-i" or "--import" option. Only Files supported by a plugin can be imported, so not Family Trees. """ # Need to convert path/filename to unicode before openingh # For non latin characters in Windows path/file/user names value = Utils.get_unicode_path_from_env_var(value) fname = value fullpath = os.path.abspath(os.path.expanduser(fname)) if not os.path.exists(fullpath): self.__error(_('Error: Import file %s not found.') % fname) sys.exit(0) if family_tree_format is None: # Guess the file format based on the file extension. # This will get the lower case extension without a period, # or an empty string. family_tree_format = os.path.splitext(fname)[-1][1:].lower() pmgr = BasePluginManager.get_instance() plugin_found = False for plugin in pmgr.get_import_plugins(): if family_tree_format == plugin.get_extension(): plugin_found = True if plugin_found: self.imports.append((fname, family_tree_format)) else: self.__error(_('Error: Unrecognized type: "%(format)s" for ' 'import file: %(filename)s') \ % {'format' : family_tree_format, 'filename' : fname}) sys.exit(0) def __handle_export_option(self, value, family_tree_format): """ Handle the "-e" or "--export" option. Note: this can only happen in the CLI version. """ if self.gui: return # Need to covert path/filename to unicode before openingh # For non latin characters in Windows path/file/user names value = Utils.get_unicode_path_from_env_var(value) fname = value fullpath = os.path.abspath(os.path.expanduser(fname)) if os.path.exists(fullpath): self.__error(_("WARNING: Output file already exists!\n" "WARNING: It will be overwritten:\n %(name)s") % \ {'name' : fullpath}) answer = None while not answer: answer = raw_input(_('OK to overwrite? (yes/no) ') \ .encode(sys.getfilesystemencoding())) if answer.upper() in ('Y', 'YES', _('YES').upper()): self.__error( _("Will overwrite the existing file: %s") % fullpath) else: sys.exit(0) if family_tree_format is None: # Guess the file format based on the file extension. # This will get the lower case extension without a period, # or an empty string. family_tree_format = os.path.splitext(fname)[-1][1:].lower() pmgr = BasePluginManager.get_instance() plugin_found = False for plugin in pmgr.get_export_plugins(): if family_tree_format == plugin.get_extension(): plugin_found = True if plugin_found: self.exports.append((fullpath, family_tree_format)) else: self.__error(_("ERROR: Unrecognized format for export file %s") % fname) sys.exit(0) def __deduce_db_path(self, db_name_or_path): """ Attempt to find a database path for the given parameter. @return: The path to a Gramps DB or None if a database can not be deduced. """ # First, check if this is the name of a family tree db_path = self.dbman.get_family_tree_path(db_name_or_path) if db_path is None: # This is not a known database name. # Check if the user provided a db path instead. fullpath = os.path.abspath(os.path.expanduser(db_name_or_path)) if os.path.isdir(fullpath): # The user provided a directory. Check if it is a valid tree. name_file_path = os.path.join(fullpath, NAME_FILE) if os.path.isfile(name_file_path): db_path = fullpath return db_path #------------------------------------------------------------------------- # Overall argument handler: # sorts out the sequence and details of operations #------------------------------------------------------------------------- def handle_args_gui(self): """ method to handle the arguments that can be given for a GUI session. Returns the filename of the family tree that should be opened if user just passed a famtree or a filename 1/no options: a family tree can be given, if so, this name is tested and returned. If a filename, it is imported in a new db and name of new db returned 2/an open and/or import option can have been given, if so, this is handled, and None is returned """ if self.open_gui: # First check if a Gramps database was provided # (either a database path or a database name) db_path = self.__deduce_db_path(self.open_gui) if not db_path: # Apparently it is not a database. See if it is a file that # can be imported. db_path, title = self.dbman.import_new_db(self.open_gui, None) if db_path: # Test if not locked or problematic if not self.check_db(db_path, self.force_unlock): sys.exit(0) # Add the file to the recent items path = os.path.join(db_path, "name.txt") try: ifile = open(path) title = ifile.readline().strip() ifile.close() except: title = db_path RecentFiles.recent_files(db_path, title) self.open = db_path self.__open_action() else: sys.exit(0) return db_path # if not open_gui, parse any command line args. We can only have one # open argument, and perhaps some import arguments self.__open_action() self.__import_action() return None def handle_args_cli(self, cleanup=True): """ Depending on the given arguments, import or open data, launch session, write files, and/or perform actions. @param: climan: the manager of a CLI session @type: CLIManager object """ if self.list: print 'List of known family trees in your database path\n' for name, dirname in self.dbman.family_tree_list(): print dirname, ', with name ', name.encode(sys.getfilesystemencoding()) sys.exit(0) if self.list_more: print 'Gramps Family Trees:' summary_list = self.dbman.family_tree_summary() for summary in summary_list: print "Family Tree \"%s\":" % summary["Family tree"] for item in summary: if item != "Family tree": print " %s: %s" % (item, summary[item]) sys.exit(0) self.__open_action() self.__import_action() for (action, options_str) in self.actions: print "Performing action: %s." % action if options_str: print "Using options string: %s" % options_str self.cl_action(action, options_str) for expt in self.exports: # Need to convert path/filename to str before printing # For non latin characters in Windows path/file/user names fn = expt[0].encode(sys.getfilesystemencoding()) fmt = str(expt[1]) print "Exporting: file %s, format %s." % (fn, fmt) self.cl_export(expt[0], expt[1]) if cleanup: self.cleanup() print "Exiting." sys.exit(0) def cleanup(self): print "Cleaning up." # remove files in import db subdir after use self.dbstate.db.close() if self.imp_db_path: Utils.rm_tempdir(self.imp_db_path) def __import_action(self): """ Take action for all given to import files. Note: Family trees are not supported. If a family tree is open, the import happens on top of it. If not open, a new family tree is created, and the import done. If this is CLI, the created tree is deleted at the end (as some action will have happened that is now finished), if this is GUI, it is opened. """ if self.imports: self.cl = bool(self.exports or self.actions or self.cl) if not self.open: # Create empty dir for imported database(s) if self.gui: self.imp_db_path, title = self.dbman.create_new_db_cli() else: self.imp_db_path = Utils.get_empty_tempdir("import_dbdir") newdb = gen.db.DbBsddb() newdb.write_version(self.imp_db_path) try: self.sm.open_activate(self.imp_db_path) print "Created empty family tree successfully" except: print "Error opening the file." print "Exiting..." sys.exit(0) for imp in self.imports: # Need to covert path/filename to str before printing # For non latin characters in Windows path/file/user names fn = imp[0].encode(sys.getfilesystemencoding()) fmt = str(imp[1]) print "Importing: file %s, format %s." % (fn , fmt) self.cl_import(imp[0], imp[1]) def __open_action(self): """ Take action on a family tree dir to open. It will be opened in the session manager """ if self.open: # Family Tree to open was given. Open it # Then go on and process the rest of the command line arguments. self.cl = bool(self.exports or self.actions) # we load this file for use try: self.sm.open_activate(self.open) print "Opened successfully!" except: print "Error opening the file." print "Exiting..." sys.exit(0) def check_db(self, dbpath, force_unlock = False): """ Test a given family tree path if it can be opened. """ # Test if not locked or problematic if force_unlock: self.dbman.break_lock(dbpath) if self.dbman.is_locked(dbpath): self.__error((_("Database is locked, cannot open it!") + '\n' + _(" Info: %s")) % find_locker_name(dbpath)) return False if self.dbman.needs_recovery(dbpath): self.__error( _("Database needs recovery, cannot open it!")) return False return True #------------------------------------------------------------------------- # # Import handler # #------------------------------------------------------------------------- def cl_import(self, filename, family_tree_format): """ Command-line import routine. Try to import filename using the family_tree_format. """ pmgr = BasePluginManager.get_instance() for plugin in pmgr.get_import_plugins(): if family_tree_format == plugin.get_extension(): import_function = plugin.get_import_function() import_function(self.dbstate.db, filename, None) if not self.cl: if self.imp_db_path: return self.sm.open_activate(self.imp_db_path) else: return self.sm.open_activate(self.open) #------------------------------------------------------------------------- # # Export handler # #------------------------------------------------------------------------- def cl_export(self, filename, family_tree_format): """ Command-line export routine. Try to write into filename using the family_tree_format. """ pmgr = BasePluginManager.get_instance() for plugin in pmgr.get_export_plugins(): if family_tree_format == plugin.get_extension(): export_function = plugin.get_export_function() export_function(self.dbstate.db, filename) #------------------------------------------------------------------------- # # Action handler # #------------------------------------------------------------------------- def cl_action(self, action, options_str): """ Command-line action routine. Try to perform specified action. """ pmgr = BasePluginManager.get_instance() if action == "report": try: options_str_dict = dict( [ tuple(chunk.split('=')) for chunk in options_str.split(',') ] ) except: options_str_dict = {} print "Ignoring invalid options string." name = options_str_dict.pop('name', None) _cl_list = pmgr.get_reg_reports(gui=False) if name: for pdata in _cl_list: if name == pdata.id: mod = pmgr.load_plugin(pdata) if not mod: #import of plugin failed return category = pdata.category report_class = eval('mod.' + pdata.reportclass) options_class = eval('mod.' + pdata.optionclass) if category in (CATEGORY_BOOK, CATEGORY_CODE): options_class(self.dbstate.db, name, category, options_str_dict) else: cl_report(self.dbstate.db, name, category, report_class, options_class, options_str_dict) return # name exists, but is not in the list of valid report names msg = "Unknown report name." else: msg = "Report name not given. Please use -p name=reportname." print "%s\n Available names are:" % msg for pdata in _cl_list: # Print cli report name ([item[0]) and GUI report name (item[4]) if len(pdata.id) <= 25: print " %s%s- %s" % ( pdata.id, " " * (26 - len(pdata.id)), pdata.name.encode(sys.getfilesystemencoding())) else: print " %s\t- %s" % (pdata.id, pdata.name.encode(sys.getfilesystemencoding()) ) elif action == "tool": try: options_str_dict = dict( [ tuple(chunk.split('=')) for chunk in options_str.split(',') ] ) except: options_str_dict = {} print "Ignoring invalid options string." name = options_str_dict.pop('name', None) _cli_tool_list = pmgr.get_reg_tools(gui=False) if name: for pdata in _cli_tool_list: if name == pdata.id: mod = pmgr.load_plugin(pdata) if not mod: #import of plugin failed return category = pdata.category tool_class = eval('mod.' + pdata.toolclass) options_class = eval('mod.' + pdata.optionclass) Tool.cli_tool(self.dbstate, name, category, tool_class, options_class, options_str_dict) return msg = "Unknown tool name." else: msg = "Tool name not given. Please use -p name=toolname." print "%s\n Available names are:" % msg for pdata in _cli_tool_list: # Print cli report name ([item[0]) and GUI report name (item[4]) if len(pdata.id) <= 25: print " %s%s- %s" % ( pdata.id, " " * (26 - len(pdata.id)), pdata.name.encode(sys.getfilesystemencoding())) else: print " %s\t- %s" % (pdata.id, pdata.name.encode(sys.getfilesystemencoding())) else: print "Unknown action: %s." % action sys.exit(0)
class ArgHandler(object): """ This class is responsible for the non GUI handling of commands. The handler is passed a parser object, sanitizes it, and can execute the actions requested working on a DbState. """ def __init__(self, dbstate, parser, sessionmanager, errorfunc=None, gui=False): self.dbstate = dbstate self.sm = sessionmanager self.errorfunc = errorfunc self.gui = gui if self.gui: self.actions = [] self.list = False self.list_more = False self.open_gui = None else: self.actions = parser.actions self.list = parser.list self.list_more = parser.list_more self.list_table = parser.list_table self.open_gui = parser.open_gui self.imp_db_path = None self.dbman = CLIDbManager(self.dbstate) self.force_unlock = parser.force_unlock self.cl = 0 self.imports = [] self.exports = [] self.open = self.__handle_open_option(parser.open, parser.create) self.sanitize_args(parser.imports, parser.exports) def __error(self, msg1, msg2=None): """ Output an error. Uses errorfunc if given, otherwise a simple print. """ if self.errorfunc: self.errorfunc(msg1) else: # Need to convert to system file encoding before printing # For non latin characters in path/file/user names print >> sys.stderr, msg1.encode(sys.getfilesystemencoding()) if msg2 is not None: print >> sys.stderr, msg2.encode(sys.getfilesystemencoding()) #------------------------------------------------------------------------- # Argument parser: sorts out given arguments #------------------------------------------------------------------------- def sanitize_args(self, importlist, exportlist): """ Check the lists with open, exports, imports, and actions options. """ for (value, family_tree_format) in importlist: self.__handle_import_option(value, family_tree_format) for (value, family_tree_format) in exportlist: self.__handle_export_option(value, family_tree_format) def __handle_open_option(self, value, create): """ Handle the "-O" or "--open" and "-C" or "--create" options. Only Family trees or a dir with a family tree can be opened. If create is True, then create the tree if it doesn't exist. """ if value is None: return None value = Utils.get_unicode_path_from_env_var(value) db_path = self.__deduce_db_path(value) if db_path: # We have a potential database path. # Check if it is good. if not self.check_db(db_path, self.force_unlock): sys.exit(0) if create: self.__error( _("Error: Family tree '%s' already exists.\n" "The '-C' option cannot be used.") % value) sys.exit(0) return db_path elif create: # create the tree here, and continue db_path, title = self.dbman.create_new_db_cli(title=value) return db_path else: self.__error( _('Error: Input family tree "%s" does not exist.\n' "If GEDCOM, Gramps-xml or grdb, use the -i option " "to import into a family tree instead.") % value) sys.exit(0) def __handle_import_option(self, value, family_tree_format): """ Handle the "-i" or "--import" option. Only Files supported by a plugin can be imported, so not Family Trees. """ # Need to convert path/filename to unicode before opening # For non latin characters in Windows path/file/user names value = Utils.get_unicode_path_from_env_var(value) fname = value fullpath = os.path.abspath(os.path.expanduser(fname)) if fname != '-' and not os.path.exists(fullpath): self.__error(_('Error: Import file %s not found.') % fname) sys.exit(0) if family_tree_format is None: # Guess the file format based on the file extension. # This will get the lower case extension without a period, # or an empty string. family_tree_format = os.path.splitext(fname)[-1][1:].lower() pmgr = BasePluginManager.get_instance() plugin_found = False for plugin in pmgr.get_import_plugins(): if family_tree_format == plugin.get_extension(): plugin_found = True if plugin_found: self.imports.append((fname, family_tree_format)) else: self.__error( _('Error: Unrecognized type: "%(format)s" for ' 'import file: %(filename)s') % { 'format': family_tree_format, 'filename': fname }) sys.exit(0) def __handle_export_option(self, value, family_tree_format): """ Handle the "-e" or "--export" option. Note: this can only happen in the CLI version. """ if self.gui: return # Need to convert path/filename to unicode before opening # For non latin characters in Windows path/file/user names value = Utils.get_unicode_path_from_env_var(value) fname = value if fname == '-': fullpath = '-' else: fullpath = os.path.abspath(os.path.expanduser(fname)) if os.path.exists(fullpath): self.__error( _("WARNING: Output file already exists!\n" "WARNING: It will be overwritten:\n %s") % fullpath) try: answer = raw_input(_('OK to overwrite? (yes/no) ') \ .encode(sys.getfilesystemencoding())) except EOFError: print sys.exit(0) if answer.upper() in ('Y', 'YES', _('YES').upper()): self.__error( _("Will overwrite the existing file: %s") % fullpath) else: sys.exit(0) if family_tree_format is None: # Guess the file format based on the file extension. # This will get the lower case extension without a period, # or an empty string. family_tree_format = os.path.splitext(fname)[-1][1:].lower() pmgr = BasePluginManager.get_instance() plugin_found = False for plugin in pmgr.get_export_plugins(): if family_tree_format == plugin.get_extension(): plugin_found = True if plugin_found: self.exports.append((fullpath, family_tree_format)) else: self.__error( _("ERROR: Unrecognized format for export file %s") % fname) sys.exit(0) def __deduce_db_path(self, db_name_or_path): """ Attempt to find a database path for the given parameter. @return: The path to a Gramps DB or None if a database can not be deduced. """ # First, check if this is the name of a family tree db_path = self.dbman.get_family_tree_path(db_name_or_path) if db_path is None: # This is not a known database name. # Check if the user provided a db path instead. fullpath = os.path.abspath(os.path.expanduser(db_name_or_path)) if os.path.isdir(fullpath): # The user provided a directory. Check if it is a valid tree. name_file_path = os.path.join(fullpath, NAME_FILE) if os.path.isfile(name_file_path): db_path = fullpath return db_path #------------------------------------------------------------------------- # Overall argument handler: # sorts out the sequence and details of operations #------------------------------------------------------------------------- def handle_args_gui(self): """ method to handle the arguments that can be given for a GUI session. Returns the filename of the family tree that should be opened if user just passed a famtree or a filename 1/no options: a family tree can be given, if so, this name is tested and returned. If a filename, it is imported in a new db and name of new db returned 2/an open and/or import option can have been given, if so, this is handled, and None is returned """ if self.open_gui: # First check if a Gramps database was provided # (either a database path or a database name) db_path = self.__deduce_db_path(self.open_gui) if not db_path: # Apparently it is not a database. See if it is a file that # can be imported. db_path, title = self.dbman.import_new_db(self.open_gui, None) if db_path: # Test if not locked or problematic if not self.check_db(db_path, self.force_unlock): sys.exit(0) # Add the file to the recent items title = self.dbstate.db.get_dbname() if not title: title = db_path RecentFiles.recent_files(db_path, title) self.open = db_path self.__open_action() else: sys.exit(0) return db_path # if not open_gui, parse any command line args. We can only have one # open argument, and perhaps some import arguments self.__open_action() self.__import_action() return None def handle_args_cli(self, cleanup=True): """ Depending on the given arguments, import or open data, launch session, write files, and/or perform actions. @param: climan: the manager of a CLI session @type: CLIManager object """ # Handle the "-l" List Family Trees option. if self.list: print _('List of known family trees in your database path\n').\ encode(sys.getfilesystemencoding()) for name, dirname in sorted(self.dbman.family_tree_list(), key=lambda pair: pair[0].lower()): print (_("%(full_DB_path)s with name \"%(f_t_name)s\"") % \ {'full_DB_path' : dirname.decode(sys.getfilesystemencoding()), 'f_t_name' : name}).encode(sys.getfilesystemencoding()) sys.exit(0) # Handle the "-L" List Family Trees in detail option. if self.list_more: print _('Gramps Family Trees:').encode(sys.getfilesystemencoding()) summary_list = self.dbman.family_tree_summary() for summary in sorted( summary_list, key=lambda sum: sum[_("Family tree")].lower()): print _("Family Tree \"%s\":").\ encode(sys.getfilesystemencoding()) % summary[_("Family tree")] for item in sorted(summary): if item == _("Path"): summary[item] = (summary[item]).decode( sys.getfilesystemencoding()) if item != _("Family tree"): print (" %s: %s" % (item, summary[item])).\ encode(sys.getfilesystemencoding()) sys.exit(0) # Handle the "-t" List Family Trees, tab delimited option. if self.list_table: print _('Gramps Family Trees:').encode(sys.getfilesystemencoding()) summary_list = self.dbman.family_tree_summary() if not summary_list: sys.exit(0) # We have to construct the line elements together, to avoid # insertion of blank spaces when print on the same line is used line_list = [_("Family Tree").encode(sys.getfilesystemencoding())] for key in sorted(summary_list[0]): if key != _("Family tree"): line_list += [key.encode(sys.getfilesystemencoding())] print "\t".join(line_list) for summary in sorted( summary_list, key=lambda sum: sum[_("Family tree")].lower()): line_list = [('"%s"' % summary[_("Family tree")]).\ encode(sys.getfilesystemencoding())] for item in sorted(summary): if item != _("Family tree"): line_list += [('"%s"' % summary[item]).\ encode(sys.getfilesystemencoding())] print "\t".join(line_list) sys.exit(0) self.__open_action() self.__import_action() for (action, op_string) in self.actions: print >> sys.stderr, (_("Performing action: %s.") % action).encode( sys.getfilesystemencoding()) if op_string: print >> sys.stderr, (_("Using options string: %s") % op_string).encode( sys.getfilesystemencoding()) self.cl_action(action, op_string) for expt in self.exports: # Need to convert path/filename to str before printing # For non latin characters in Windows path/file/user names fn = expt[0].encode(sys.getfilesystemencoding()) fmt = str(expt[1]) print >> sys.stderr, _("Exporting: file %(filename)s, " "format %(format)s.") % \ {'filename' : fn, 'format' : fmt} self.cl_export(expt[0], expt[1]) if cleanup: self.cleanup() print >> sys.stderr, _("Exiting.").encode( sys.getfilesystemencoding()) sys.exit(0) def cleanup(self): print >> sys.stderr, _("Cleaning up.").encode( sys.getfilesystemencoding()) # remove files in import db subdir after use self.dbstate.db.close() if self.imp_db_path: Utils.rm_tempdir(self.imp_db_path) def __import_action(self): """ Take action for all given import files. Note: Family trees are not supported. If a family tree is open, the import happens on top of it. If not open, a new family tree is created, and the import done. If this is CLI, the created tree is deleted at the end (as some action will have happened that is now finished), if this is GUI, it is opened. """ if self.imports: self.cl = bool(self.exports or self.actions or self.cl) if not self.open: # Create empty dir for imported database(s) if self.gui: self.imp_db_path, title = self.dbman.create_new_db_cli() else: self.imp_db_path = Utils.get_empty_tempdir("import_dbdir") \ .encode(sys.getfilesystemencoding()) newdb = gen.db.DbBsddb() newdb.write_version(self.imp_db_path) try: self.sm.open_activate(self.imp_db_path) msg = _("Created empty family tree successfully").encode( sys.getfilesystemencoding()) print >> sys.stderr, msg except: print >> sys.stderr, _("Error opening the file.").encode( sys.getfilesystemencoding()) print >> sys.stderr, _("Exiting...").encode( sys.getfilesystemencoding()) sys.exit(0) for imp in self.imports: fn = imp[0].encode(sys.getfilesystemencoding()) fmt = str(imp[1]) msg = _("Importing: file %(filename)s, format %(format)s.") % \ {'filename' : fn, 'format' : fmt} print >> sys.stderr, msg.encode(sys.getfilesystemencoding()) self.cl_import(imp[0], imp[1]) def __open_action(self): """ Take action on a family tree dir to open. It will be opened in the session manager """ if self.open: # Family Tree to open was given. Open it # Then go on and process the rest of the command line arguments. self.cl = bool(self.exports or self.actions) # we load this file for use try: self.sm.open_activate(self.open) print >> sys.stderr, _("Opened successfully!").encode( sys.getfilesystemencoding()) except: print >> sys.stderr, _("Error opening the file.").encode( sys.getfilesystemencoding()) print >> sys.stderr, _("Exiting...").encode( sys.getfilesystemencoding()) sys.exit(0) def check_db(self, dbpath, force_unlock=False): """ Test a given family tree path if it can be opened. """ # Test if not locked or problematic if force_unlock: self.dbman.break_lock(dbpath) if self.dbman.is_locked(dbpath): self.__error((_("Database is locked, cannot open it!") + '\n' + _(" Info: %s")) % find_locker_name(dbpath)) return False if self.dbman.needs_recovery(dbpath): self.__error(_("Database needs recovery, cannot open it!")) return False return True #------------------------------------------------------------------------- # # Import handler # #------------------------------------------------------------------------- def cl_import(self, filename, family_tree_format): """ Command-line import routine. Try to import filename using the family_tree_format. """ pmgr = BasePluginManager.get_instance() for plugin in pmgr.get_import_plugins(): if family_tree_format == plugin.get_extension(): import_function = plugin.get_import_function() import_function(self.dbstate.db, filename, None) #------------------------------------------------------------------------- # # Export handler # #------------------------------------------------------------------------- def cl_export(self, filename, family_tree_format): """ Command-line export routine. Try to write into filename using the family_tree_format. """ pmgr = BasePluginManager.get_instance() for plugin in pmgr.get_export_plugins(): if family_tree_format == plugin.get_extension(): export_function = plugin.get_export_function() export_function(self.dbstate.db, filename, self.__error) #------------------------------------------------------------------------- # # Action handler # #------------------------------------------------------------------------- def cl_action(self, action, options_str): """ Command-line action routine. Try to perform specified action. """ pmgr = BasePluginManager.get_instance() if action == "report": try: options_str_dict = _split_options(options_str) except: options_str_dict = {} print >> sys.stderr, _( "Ignoring invalid options string.").encode( sys.getfilesystemencoding()) name = options_str_dict.pop('name', None) _cl_list = pmgr.get_reg_reports(gui=False) if name: for pdata in _cl_list: if name == pdata.id: mod = pmgr.load_plugin(pdata) if not mod: #import of plugin failed return category = pdata.category report_class = eval('mod.' + pdata.reportclass) options_class = eval('mod.' + pdata.optionclass) if category in (CATEGORY_BOOK, CATEGORY_CODE): options_class(self.dbstate.db, name, category, options_str_dict) else: cl_report(self.dbstate.db, name, category, report_class, options_class, options_str_dict) return # name exists, but is not in the list of valid report names msg = _("Unknown report name.") else: msg = _("Report name not given. " "Please use one of %(donottranslate)s=reportname") % \ {'donottranslate' : '[-p|--options] name'} print >> sys.stderr, (_("%s\n Available names are:") % msg).encode( sys.getfilesystemencoding()) for pdata in sorted(_cl_list, key=lambda pdata: pdata.id.lower()): # Print cli report name ([item[0]), GUI report name (item[4]) if len(pdata.id) <= 25: print >> sys.stderr, \ " %s%s- %s" % ( pdata.id, " " * (26 - len(pdata.id)), pdata.name.encode(sys.getfilesystemencoding())) else: print >> sys.stderr, " %s\t- %s" % ( pdata.id, pdata.name.encode( sys.getfilesystemencoding())) elif action == "tool": from gui.plug import tool try: options_str_dict = dict([ tuple(chunk.split('=')) for chunk in options_str.split(',') ]) except: options_str_dict = {} print >> sys.stderr, _( "Ignoring invalid options string.").encode( sys.getfilesystemencoding()) name = options_str_dict.pop('name', None) _cli_tool_list = pmgr.get_reg_tools(gui=False) if name: for pdata in _cli_tool_list: if name == pdata.id: mod = pmgr.load_plugin(pdata) if not mod: #import of plugin failed return category = pdata.category tool_class = eval('mod.' + pdata.toolclass) options_class = eval('mod.' + pdata.optionclass) tool.cli_tool(self.dbstate, name, category, tool_class, options_class, options_str_dict) return msg = _("Unknown tool name.") else: msg = _("Tool name not given. " "Please use one of %(donottranslate)s=toolname.") % \ {'donottranslate' : '[-p|--options] name'} print >> sys.stderr, _("%s\n Available names are:") % msg for pdata in sorted(_cli_tool_list, key=lambda pdata: pdata.id.lower()): # Print cli report name ([item[0]), GUI report name (item[4]) if len(pdata.id) <= 25: print >> sys.stderr, \ " %s%s- %s" % ( pdata.id, " " * (26 - len(pdata.id)), pdata.name.encode(sys.getfilesystemencoding())) else: print >> sys.stderr, " %s\t- %s" % ( pdata.id, pdata.name.encode( sys.getfilesystemencoding())) else: print >> sys.stderr, _("Unknown action: %s.") % action sys.exit(0)