def append_styles(selected_style, item): """ Append the styles for a book item to the stylesheet. """ # Set up default style default_style = StyleSheet() make_default_style = item.option_class.make_default_style make_default_style(default_style) # Read all style sheets available for this item style_file = item.option_class.handler.get_stylesheet_savefile() style_list = StyleSheetList(style_file, default_style) # Get the selected stylesheet style_name = item.get_style_name() style_sheet = style_list.get_style_sheet(style_name) for this_style_name in style_sheet.get_paragraph_style_names(): selected_style.add_paragraph_style( this_style_name, style_sheet.get_paragraph_style(this_style_name)) for this_style_name in style_sheet.get_draw_style_names(): selected_style.add_draw_style( this_style_name, style_sheet.get_draw_style(this_style_name)) for this_style_name in style_sheet.get_table_style_names(): selected_style.add_table_style( this_style_name, style_sheet.get_table_style(this_style_name)) for this_style_name in style_sheet.get_cell_style_names(): selected_style.add_cell_style( this_style_name, style_sheet.get_cell_style(this_style_name)) return selected_style
def __init__(self, label, default_style, module_name): """ @param label: A friendly label to be applied to this option. Example: "Style" @type label: string @param default_style: A docgen StyleSheet instance which provides the default styles. @type default_style: docgen StyleSheet @param module_name: The name of the module the style sheets belong to. Example: "web_cal" @type module_name: string @return: nothing """ EnumeratedListOption.__init__(self, label, "default") self.__default_style = default_style self.__default_style.set_name("default") self.__style_file = "%s_style.xml" % module_name style_list = StyleSheetList(self.__style_file, self.__default_style) for style_name in style_list.get_style_names(): self.add_item(style_name, style_name)
def setup_style_frame(self): """Set up the style frame of the dialog. This function relies on other routines create the default style for this report, and to read in any user defined styles for this report. It the builds a menu of all the available styles for the user to choose from.""" # Build the default style set for this report. self.default_style = StyleSheet() self.options.make_default_style(self.default_style) if self.default_style.is_empty(): # Don't display the option of no styles are used return # Styles Frame label = gtk.Label("%s:" % _("Style")) label.set_alignment(0.0, 0.5) self.style_menu = StyleComboBox() self.style_button = gtk.Button("%s..." % _("Style Editor")) self.style_button.connect('clicked', self.on_style_edit_clicked) self.tbl.attach(label, 1, 2, self.row, self.row+1, gtk.SHRINK|gtk.FILL) self.tbl.attach(self.style_menu, 2, 3, self.row, self.row+1, yoptions=gtk.SHRINK) self.tbl.attach(self.style_button, 3, 4, self.row, self.row+1, xoptions=gtk.SHRINK|gtk.FILL, yoptions=gtk.SHRINK) self.row += 1 # Build the initial list of available styles sets. This # includes the default style set and any style sets saved from # previous invocations of gramps. self.style_sheet_list = StyleSheetList( self.options.handler.get_stylesheet_savefile(), self.default_style) # Now build the actual menu. style = self.options.handler.get_default_stylesheet_name() self.build_style_menu(style)
def parse_options(self): """ Load the options that the user has entered. """ if not hasattr(self.option_class, "menu"): menu = None else: menu = self.option_class.menu menu_opt_names = menu.get_all_option_names() for opt in self.options_str_dict: if opt in self.options_dict: self.options_dict[opt] = \ _convert_str_to_match_type(self.options_str_dict[opt], self.options_dict[opt]) self.option_class.handler.options_dict[opt] = \ self.options_dict[opt] if menu and opt in menu_opt_names: option = menu.get_option_by_name(opt) option.set_value(self.options_dict[opt]) else: print(_("Ignoring unknown option: %s") % opt).encode( sys.getfilesystemencoding()) print( _(" Valid options are:") + ", ".join(self.options_dict.keys())).encode( sys.getfilesystemencoding()) print( _(" Use '%(donottranslate)s' to see description " "and acceptable values") % { 'donottranslate': "show=option" }).encode(sys.getfilesystemencoding()) self.option_class.handler.output = self.options_dict['of'] self.css_filename = None _chosen_format = None if self.category == CATEGORY_TEXT: for plugin in self.__textdoc_plugins: if plugin.get_extension() == self.options_dict['off']: self.format = plugin.get_basedoc() self.css_filename = self.options_dict['css'] if self.format is None: # Pick the first one as the default. self.format = self.__textdoc_plugins[0].get_basedoc() _chosen_format = self.__textdoc_plugins[0].get_extension() elif self.category == CATEGORY_DRAW: for plugin in self.__drawdoc_plugins: if plugin.get_extension() == self.options_dict['off']: self.format = plugin.get_basedoc() if self.format is None: # Pick the first one as the default. self.format = self.__drawdoc_plugins[0].get_basedoc() _chosen_format = self.__drawdoc_plugins[0].get_extension() elif self.category == CATEGORY_BOOK: for plugin in self.__bookdoc_plugins: if plugin.get_extension() == self.options_dict['off']: self.format = plugin.get_basedoc() if self.format is None: # Pick the first one as the default. self.format = self.__bookdoc_plugins[0].get_basedoc() _chosen_format = self.__bookdoc_plugins[0].get_extension() elif self.category == CATEGORY_GRAPHVIZ: for graph_format in graphdoc.FORMATS: if graph_format['type'] == self.options_dict['off']: if not self.format: # choose the first one, not the last self.format = graph_format["class"] if self.format is None: # Pick the first one as the default. self.format = graphdoc.FORMATS[0]["class"] _chosen_format = graphdoc.FORMATS[0]["type"] else: self.format = None if _chosen_format and self.options_str_dict.has_key('off'): print( _("Ignoring '%(notranslate1)s=%(notranslate2)s' " "and using '%(notranslate1)s=%(notranslate3)s'.") % { 'notranslate1': "off", 'notranslate2': self.options_dict['off'], 'notranslate3': _chosen_format }).encode(sys.getfilesystemencoding()) print( _("Use '%(notranslate)s' to see valid values.") % { 'notranslate': "show=off" }).encode(sys.getfilesystemencoding()) self.paper = paper_sizes[0] # make sure one exists for paper in paper_sizes: if paper.get_name() == self.options_dict['papers']: self.paper = paper self.option_class.handler.set_paper(self.paper) self.orien = self.options_dict['papero'] self.marginl = self.options_dict['paperml'] self.marginr = self.options_dict['papermr'] self.margint = self.options_dict['papermt'] self.marginb = self.options_dict['papermb'] if self.category in (CATEGORY_TEXT, CATEGORY_DRAW): default_style = StyleSheet() self.option_class.make_default_style(default_style) # Read all style sheets available for this item style_file = self.option_class.handler.get_stylesheet_savefile() self.style_list = StyleSheetList(style_file, default_style) # Get the selected stylesheet style_name = self.option_class.handler.get_default_stylesheet_name( ) self.selected_style = self.style_list.get_style_sheet(style_name)
def init_standard_options(self, noopt): """ Initialize the options that are hard-coded into the report system. """ self.options_dict = { 'of' : self.option_class.handler.module_name, 'off' : self.option_class.handler.get_format_name(), 'style' : \ self.option_class.handler.get_default_stylesheet_name(), 'papers' : self.option_class.handler.get_paper_name(), 'papero' : self.option_class.handler.get_orientation(), 'paperml' : self.option_class.handler.get_margins()[0], 'papermr' : self.option_class.handler.get_margins()[1], 'papermt' : self.option_class.handler.get_margins()[2], 'papermb' : self.option_class.handler.get_margins()[3], 'css' : self.option_class.handler.get_css_filename(), } self.options_help = { 'of': ["=filename", "Output file name. MANDATORY", ""], 'off': ["=format", "Output file format.", []], 'style': ["=name", "Style name.", ""], 'papers': ["=name", "Paper size name.", ""], 'papero': ["=num", "Paper orientation number.", ""], 'paperml': ["=num", "Left paper margin", "Size in cm"], 'papermr': ["=num", "Right paper margin", "Size in cm"], 'papermt': ["=num", "Top paper margin", "Size in cm"], 'papermb': ["=num", "Bottom paper margin", "Size in cm"], 'css': ["=css filename", "CSS filename to use, html format" " only", ""], } if noopt: return self.options_help['of'][2] = os.path.join(const.USER_HOME, "whatever_name") if self.category == CATEGORY_TEXT: for plugin in self.__textdoc_plugins: self.options_help['off'][2].append(plugin.get_extension() + "\t" + plugin.get_description()) elif self.category == CATEGORY_DRAW: for plugin in self.__drawdoc_plugins: self.options_help['off'][2].append(plugin.get_extension() + "\t" + plugin.get_description()) elif self.category == CATEGORY_BOOK: for plugin in self.__bookdoc_plugins: self.options_help['off'][2].append(plugin.get_extension() + "\t" + plugin.get_description()) elif self.category == CATEGORY_GRAPHVIZ: for graph_format in graphdoc.FORMATS: self.options_help['off'][2].append(graph_format["type"] + "\t" + graph_format["descr"]) else: self.options_help['off'][2] = "NA" self.options_help['papers'][2] = \ [ paper.get_name() for paper in paper_sizes if paper.get_name() != 'Custom Size' ] self.options_help['papero'][2] = [ "%d\tPortrait" % PAPER_PORTRAIT, "%d\tLandscape" % PAPER_LANDSCAPE ] self.options_help['css'][2] = os.path.join(const.USER_HOME, "whatever_name.css") if self.category in (CATEGORY_TEXT, CATEGORY_DRAW): default_style = StyleSheet() self.option_class.make_default_style(default_style) # Read all style sheets available for this item style_file = self.option_class.handler.get_stylesheet_savefile() self.style_list = StyleSheetList(style_file, default_style) self.options_help['style'][2] = self.style_list.get_style_names()
class CommandLineReport(object): """ Provide a way to generate report from the command line. """ def __init__(self, database, name, category, option_class, options_str_dict, noopt=False): pmgr = BasePluginManager.get_instance() self.__textdoc_plugins = [] self.__drawdoc_plugins = [] self.__bookdoc_plugins = [] for plugin in pmgr.get_docgen_plugins(): if plugin.get_text_support() and plugin.get_extension(): self.__textdoc_plugins.append(plugin) if plugin.get_draw_support() and plugin.get_extension(): self.__drawdoc_plugins.append(plugin) if plugin.get_text_support() and \ plugin.get_draw_support() and \ plugin.get_extension(): self.__bookdoc_plugins.append(plugin) self.database = database self.category = category self.format = None self.option_class = option_class(name, database) if category == CATEGORY_GRAPHVIZ: # Need to include GraphViz options self.__gvoptions = graphdoc.GVOptions() menu = self.option_class.menu self.__gvoptions.add_menu_options(menu) for name in menu.get_all_option_names(): if name not in self.option_class.options_dict: self.option_class.options_dict[name] = \ menu.get_option_by_name(name).get_value() self.option_class.load_previous_values() _validate_options(self.option_class, database) self.show = options_str_dict.pop('show', None) self.options_str_dict = options_str_dict self.init_standard_options(noopt) self.init_report_options() self.parse_options() self.show_options() def init_standard_options(self, noopt): """ Initialize the options that are hard-coded into the report system. """ self.options_dict = { 'of' : self.option_class.handler.module_name, 'off' : self.option_class.handler.get_format_name(), 'style' : \ self.option_class.handler.get_default_stylesheet_name(), 'papers' : self.option_class.handler.get_paper_name(), 'papero' : self.option_class.handler.get_orientation(), 'paperml' : self.option_class.handler.get_margins()[0], 'papermr' : self.option_class.handler.get_margins()[1], 'papermt' : self.option_class.handler.get_margins()[2], 'papermb' : self.option_class.handler.get_margins()[3], 'css' : self.option_class.handler.get_css_filename(), } self.options_help = { 'of': ["=filename", "Output file name. MANDATORY", ""], 'off': ["=format", "Output file format.", []], 'style': ["=name", "Style name.", ""], 'papers': ["=name", "Paper size name.", ""], 'papero': ["=num", "Paper orientation number.", ""], 'paperml': ["=num", "Left paper margin", "Size in cm"], 'papermr': ["=num", "Right paper margin", "Size in cm"], 'papermt': ["=num", "Top paper margin", "Size in cm"], 'papermb': ["=num", "Bottom paper margin", "Size in cm"], 'css': ["=css filename", "CSS filename to use, html format" " only", ""], } if noopt: return self.options_help['of'][2] = os.path.join(const.USER_HOME, "whatever_name") if self.category == CATEGORY_TEXT: for plugin in self.__textdoc_plugins: self.options_help['off'][2].append(plugin.get_extension() + "\t" + plugin.get_description()) elif self.category == CATEGORY_DRAW: for plugin in self.__drawdoc_plugins: self.options_help['off'][2].append(plugin.get_extension() + "\t" + plugin.get_description()) elif self.category == CATEGORY_BOOK: for plugin in self.__bookdoc_plugins: self.options_help['off'][2].append(plugin.get_extension() + "\t" + plugin.get_description()) elif self.category == CATEGORY_GRAPHVIZ: for graph_format in graphdoc.FORMATS: self.options_help['off'][2].append(graph_format["type"] + "\t" + graph_format["descr"]) else: self.options_help['off'][2] = "NA" self.options_help['papers'][2] = \ [ paper.get_name() for paper in paper_sizes if paper.get_name() != 'Custom Size' ] self.options_help['papero'][2] = [ "%d\tPortrait" % PAPER_PORTRAIT, "%d\tLandscape" % PAPER_LANDSCAPE ] self.options_help['css'][2] = os.path.join(const.USER_HOME, "whatever_name.css") if self.category in (CATEGORY_TEXT, CATEGORY_DRAW): default_style = StyleSheet() self.option_class.make_default_style(default_style) # Read all style sheets available for this item style_file = self.option_class.handler.get_stylesheet_savefile() self.style_list = StyleSheetList(style_file, default_style) self.options_help['style'][2] = self.style_list.get_style_names() def init_report_options(self): """ Initialize the options that are defined by each report. """ if self.category == CATEGORY_BOOK: # a Book Report has no "menu" for key in self.option_class.options_dict: self.options_dict[key] = self.option_class.options_dict[key] self.options_help[key] = \ self.option_class.options_help[key][:3] # a Book Report can't have HTML output so "css" is meaningless self.options_dict.pop('css') if not hasattr(self.option_class, "menu"): return menu = self.option_class.menu for name in menu.get_all_option_names(): option = menu.get_option_by_name(name) self.options_dict[name] = option.get_value() self.options_help[name] = ["", option.get_help()] if isinstance(option, PersonOption): id_list = [] for person_handle in self.database.get_person_handles(True): person = self.database.get_person_from_handle( person_handle) id_list.append("%s\t%s" % (person.get_gramps_id(), name_displayer.display(person))) self.options_help[name].append(id_list) elif isinstance(option, FamilyOption): id_list = [] for family in self.database.iter_families(): mname = "" fname = "" mhandle = family.get_mother_handle() if mhandle: mother = self.database.get_person_from_handle(mhandle) if mother: mname = name_displayer.display(mother) fhandle = family.get_father_handle() if fhandle: father = self.database.get_person_from_handle(fhandle) if father: fname = name_displayer.display(father) text = "%s:\t%s, %s" % \ (family.get_gramps_id(), fname, mname) id_list.append(text) self.options_help[name].append(id_list) elif isinstance(option, NoteOption): id_list = [] for nhandle in self.database.get_note_handles(): note = self.database.get_note_from_handle(nhandle) id_list.append(note.get_gramps_id()) self.options_help[name].append(id_list) elif isinstance(option, MediaOption): id_list = [] for mhandle in self.database.get_media_object_handles(): mobject = self.database.get_object_from_handle(mhandle) id_list.append(mobject.get_gramps_id()) self.options_help[name].append(id_list) elif isinstance(option, PersonListOption): self.options_help[name].append("") elif isinstance(option, NumberOption): self.options_help[name].append("A number") elif isinstance(option, BooleanOption): self.options_help[name].append(["False", "True"]) elif isinstance(option, DestinationOption): self.options_help[name].append("A file system path") elif isinstance(option, StringOption): self.options_help[name].append("Any text") elif isinstance(option, TextOption): self.options_help[name].append( "A list of text values. Each entry in the list " "represents one line of text.") elif isinstance(option, EnumeratedListOption): ilist = [] for (value, description) in option.get_items(): ilist.append("%s\t%s" % (value, description)) self.options_help[name].append(ilist) elif isinstance(option, Option): self.options_help[name].append(option.get_help()) else: print(_("Unknown option: %s") % option).encode( sys.getfilesystemencoding()) print(_(" Valid options are:"), ", ".join(self.options_dict.keys())).encode( sys.getfilesystemencoding()) print( _(" Use '%(donottranslate)s' to see description " "and acceptable values") % { 'donottranslate': "show=option" }).encode(sys.getfilesystemencoding()) def parse_options(self): """ Load the options that the user has entered. """ if not hasattr(self.option_class, "menu"): menu = None else: menu = self.option_class.menu menu_opt_names = menu.get_all_option_names() for opt in self.options_str_dict: if opt in self.options_dict: self.options_dict[opt] = \ _convert_str_to_match_type(self.options_str_dict[opt], self.options_dict[opt]) self.option_class.handler.options_dict[opt] = \ self.options_dict[opt] if menu and opt in menu_opt_names: option = menu.get_option_by_name(opt) option.set_value(self.options_dict[opt]) else: print(_("Ignoring unknown option: %s") % opt).encode( sys.getfilesystemencoding()) print( _(" Valid options are:") + ", ".join(self.options_dict.keys())).encode( sys.getfilesystemencoding()) print( _(" Use '%(donottranslate)s' to see description " "and acceptable values") % { 'donottranslate': "show=option" }).encode(sys.getfilesystemencoding()) self.option_class.handler.output = self.options_dict['of'] self.css_filename = None _chosen_format = None if self.category == CATEGORY_TEXT: for plugin in self.__textdoc_plugins: if plugin.get_extension() == self.options_dict['off']: self.format = plugin.get_basedoc() self.css_filename = self.options_dict['css'] if self.format is None: # Pick the first one as the default. self.format = self.__textdoc_plugins[0].get_basedoc() _chosen_format = self.__textdoc_plugins[0].get_extension() elif self.category == CATEGORY_DRAW: for plugin in self.__drawdoc_plugins: if plugin.get_extension() == self.options_dict['off']: self.format = plugin.get_basedoc() if self.format is None: # Pick the first one as the default. self.format = self.__drawdoc_plugins[0].get_basedoc() _chosen_format = self.__drawdoc_plugins[0].get_extension() elif self.category == CATEGORY_BOOK: for plugin in self.__bookdoc_plugins: if plugin.get_extension() == self.options_dict['off']: self.format = plugin.get_basedoc() if self.format is None: # Pick the first one as the default. self.format = self.__bookdoc_plugins[0].get_basedoc() _chosen_format = self.__bookdoc_plugins[0].get_extension() elif self.category == CATEGORY_GRAPHVIZ: for graph_format in graphdoc.FORMATS: if graph_format['type'] == self.options_dict['off']: if not self.format: # choose the first one, not the last self.format = graph_format["class"] if self.format is None: # Pick the first one as the default. self.format = graphdoc.FORMATS[0]["class"] _chosen_format = graphdoc.FORMATS[0]["type"] else: self.format = None if _chosen_format and self.options_str_dict.has_key('off'): print( _("Ignoring '%(notranslate1)s=%(notranslate2)s' " "and using '%(notranslate1)s=%(notranslate3)s'.") % { 'notranslate1': "off", 'notranslate2': self.options_dict['off'], 'notranslate3': _chosen_format }).encode(sys.getfilesystemencoding()) print( _("Use '%(notranslate)s' to see valid values.") % { 'notranslate': "show=off" }).encode(sys.getfilesystemencoding()) self.paper = paper_sizes[0] # make sure one exists for paper in paper_sizes: if paper.get_name() == self.options_dict['papers']: self.paper = paper self.option_class.handler.set_paper(self.paper) self.orien = self.options_dict['papero'] self.marginl = self.options_dict['paperml'] self.marginr = self.options_dict['papermr'] self.margint = self.options_dict['papermt'] self.marginb = self.options_dict['papermb'] if self.category in (CATEGORY_TEXT, CATEGORY_DRAW): default_style = StyleSheet() self.option_class.make_default_style(default_style) # Read all style sheets available for this item style_file = self.option_class.handler.get_stylesheet_savefile() self.style_list = StyleSheetList(style_file, default_style) # Get the selected stylesheet style_name = self.option_class.handler.get_default_stylesheet_name( ) self.selected_style = self.style_list.get_style_sheet(style_name) def show_options(self): """ Print available options on the CLI. """ if not self.show: return elif self.show == 'all': print _(" Available options:").encode( sys.getfilesystemencoding()) for key in sorted(self.options_dict.keys()): if key in self.options_help: opt = self.options_help[key] # Make the output nicer to read, assume a tab has 8 spaces tabs = '\t\t' if len(key) < 10 else '\t' optmsg = " %s%s%s (%s)" % (key, tabs, opt[1], opt[0]) print optmsg.encode(sys.getfilesystemencoding()) else: optmsg = " %s" % key print optmsg.encode(sys.getfilesystemencoding()) print( _(" Use '%(donottranslate)s' to see description " "and acceptable values") % { 'donottranslate': "show=option" }).encode(sys.getfilesystemencoding()) elif self.show in self.options_help: opt = self.options_help[self.show] tabs = '\t\t' if len(self.show) < 10 else '\t' print ' %s%s%s (%s)' % (self.show, tabs, opt[1], opt[0]) print _(" Available values are:").encode( sys.getfilesystemencoding()) vals = opt[2] if isinstance(vals, (list, tuple)): for val in vals: optmsg = " %s" % val print optmsg.encode(sys.getfilesystemencoding()) else: optmsg = " %s" % opt[2] print optmsg.encode(sys.getfilesystemencoding()) else: #there was a show option given, but the option is invalid print( _("option '%(optionname)s' not valid. " "Use '%(donottranslate)s' to see all valid options.") % { 'optionname': self.show, 'donottranslate': "show=all" }).encode(sys.getfilesystemencoding())
def get_style(self): """ Get the selected style """ style_list = StyleSheetList(self.__style_file, self.__default_style) return style_list.get_style_sheet(self.get_value())