def compute_menus(ctx): """ Cache cli:cli or cli:menu list. @param ctx (Cli2xxx) Execution context. """ modlog.log(ctx.MODLOG_FILTER, modlog.TRACE, "Caching menu list...") # ctx.cache.menus already computed. See read_all_nodes(). #ctx.cache.menus.extend(ctx.xml.xpath_set(None, "/cli:cli")) #ctx.cache.menus.extend(ctx.xml.xpath_set(None, "//cli:menu[@name]")) # For each cli:menu[@ref], find out the respective menu. for _xml_menu_ref in ctx.cache.menu_refs: _menu_ref_name = ctx.xml.attr_value(_xml_menu_ref, "@ref") if _menu_ref_name is None: # If _xml_menu_ref, it must be because it has an @ref attribute! ctx.Utils.abort(ctx, _xml_menu_ref, "Internal error") for _xml_menu in ctx.cache.menus: _menu_name = ctx.xml.attr_value(_xml_menu, "@name") if _menu_name is None: ctx.Utils.abort(ctx, _xml_menu, "Missing '@name' attribute for menu") if _menu_name == _menu_ref_name: # Menu reference found _xml_menu_ref.cli_Cli2xxx_menu = _xml_menu # Eventually check the menu reference has been resolved if getattr(_xml_menu_ref, "cli_Cli2xxx_menu", None) is None: ctx.Utils.abort(ctx, _xml_menu_ref, "No such menu '%s'" % _menu_ref_name)
def warn(ctx, xml_node, message): """ Display warning. @param ctx (Cli2xxx) Execution context. @param xml_node (XML node) Context node. @param message (str) Warning message. """ modlog.log(ctx.MODLOG_FILTER, modlog.WARNING, ctx.Utils.node_location(ctx, xml_node) + ": " + message)
def set_encoding(self, encoding): """ Automatic encoding setter. @param encoding (str) Encoding name. """ try: self._codec = codecs.lookup(encoding) except LookupError: modlog.log(self.MODLOG_FILTER, modlog.WARNING, "Unknown encoding '%s'" % encoding)
def abort(ctx, xml_node, message): """ Abort current execution. @param ctx (Cli2xxx) Execution context. @param xml_node (XML node) Context node. @param message (str) Error message. @throw (...) """ modlog.log(ctx.MODLOG_FILTER, modlog.ERROR, ctx.Utils.node_location(ctx, xml_node) + ": " + message) raise ctx.Utils.ExecutionStoppedException()
def compute_main_infos(ctx): """ Cache main information. @param ctx (Cli2xxx) Execution context. """ if ctx.__class__.__name__ == "Cli2Help": modlog.log(ctx.MODLOG_FILTER, modlog.TRACE, "Caching main infos...") ctx.cache.cli_name = ctx.xml.attr_value(ctx.xml.root_node(), "@name") ctx.cache.page_title = ( "%s - %s" % (ctx.cache.cli_name, ctx.Utils.translate( ctx, "Command Line Interface documentation")))
def compute_command_lines(ctx): """ Cache command line informations. One cli:endl by command line. Command lines are attached to their respective menu. @param ctx (Cli2xxx) Execution context. """ modlog.log(ctx.MODLOG_FILTER, modlog.TRACE, "Caching command lines...") # For each menu, create a command list. for _xml_menu in ctx.cache.menus: _xml_menu.cli_Cli2xxx_commands = [] # For each cli:tag[@id], find-out the respective menu. for _xml_endl in ctx.cache.endls: _xml_menu = ctx.cache.owner_menu(ctx, _xml_endl) _xml_menu.cli_Cli2xxx_commands.append(_xml_endl)
def xpath_node(self, xml_focus, xpath): """ XML node XPath query. @param xml_focus (XML node) Current XML node, None for an absolute XPath expression. @param xpath (str) XPath expression to evaluate. @return (XML node) XML node if found, first one if several found, None if nothing found. """ _xml_node = None _set = self.xpath_set(xml_focus, xpath) if len(_set) > 0: if len(_set) > 1: modlog.log( self.MODLOG_FILTER, modlog.WARNING, "xpath_node('%s'): several node found, but only one is returned" % xpath) _xml_node = _set[0] return _xml_node
def load_file(self, path): """ Load an XML file. @param path (str) XML file path. @return (boolean) True for success, False otherwise. """ #modlog.log(self.MODLOG_FILTER, modlog.TRACE, "load_file(%s)" % path) try: self._doc = xml.parse(path) self._path = path self._doc.getroot().cli_XmlInDoc_parent = None for _parent in self._doc.getiterator(): for _child in _parent: _child.cli_XmlInDoc_parent = _parent return True except xml.ParseError, _pe: modlog.log(self.MODLOG_FILTER, modlog.ERROR, str(_pe)) return False
def compute_tags(ctx): """ Cache tag informations. @param ctx (Cli2xxx) Execution context. """ modlog.log(ctx.MODLOG_FILTER, modlog.TRACE, "Caching tag informations...") # For each menu, create tag caches. for _xml_menu in ctx.cache.menus: _xml_menu.cli_Cli2xxx_tag_ids = {} _xml_menu.cli_Cli2xxx_tag_refs = [] # For each cli:tag[@id] nodes, find out the respective menu. for _xml_tag in ctx.cache.tag_ids: _xml_menu = ctx.cache.owner_menu(ctx, _xml_tag) _tag_id = ctx.xml.attr_value(_xml_tag, "@id") _xml_menu.cli_Cli2xxx_tag_ids[_tag_id] = _xml_tag _xml_tag.cli_Cli2xxx_tag_refs = [] if ctx.xml.attr_value(_xml_tag, "@hollow") != "yes": # Self reference anticipation. _xml_tag.cli_Cli2xxx_tag_refs.append(_xml_tag) # For each cli:tag[@ref], find out the respective menu and cli:tag[@id]. for _xml_tag in ctx.cache.tag_refs: _xml_menu = ctx.cache.owner_menu(ctx, _xml_tag) _xml_menu.cli_Cli2xxx_tag_refs.append(_xml_tag) _tag_id = ctx.xml.attr_value(_xml_tag, "@ref") if _xml_menu.cli_Cli2xxx_tag_ids.has_key(_tag_id): _xml_tag.cli_Cli2xxx_tag_id = _xml_menu.cli_Cli2xxx_tag_ids[ _tag_id] _xml_tag.cli_Cli2xxx_tag_id.cli_Cli2xxx_tag_refs.append( _xml_tag) else: ctx.Utils.abort(ctx, _xml_tag, "No such tag identifier '%s'" % _tag_id) # For each tag identifier, find out the latest common node. for _xml_menu in ctx.cache.menus: for _tag_id in _xml_menu.cli_Cli2xxx_tag_ids.keys(): _xml_tag_id = _xml_menu.cli_Cli2xxx_tag_ids[_tag_id] _xml_common_parents = None for _xml_tag_ref in _xml_tag_id.cli_Cli2xxx_tag_refs: # Compute list of parents. _xml_parents = [] _xml_parent = _xml_tag_ref while _xml_parent is not None: _xml_parents.append(_xml_parent) if _xml_parent != _xml_menu: _xml_parent = ctx.xml.parent_node(_xml_parent) else: _xml_parent = None _xml_parents.reverse() # Compute the intersection with common parents computed until then. if _xml_common_parents is None: _xml_common_parents = _xml_parents else: _xml_common_parents2 = [] for _xml_parent in _xml_common_parents: if _xml_parent in _xml_parents: _xml_common_parents2.append(_xml_parent) else: break _xml_common_parents = _xml_common_parents2 # Determine whether this is a backward tag[@ref] _xml_tag_ref.cli_Cli2xxx_is_backward = ctx.xml.is_ancestor_or_self( _xml_tag_id, _xml_tag_ref) # Store the latest common parent computed if _xml_common_parents is None: modlog.log(ctx.MODLOG_FILTER, modlog.WARNING, "No tag reference for tag '%s'" % _tag_id) _xml_tag_id.cli_Cli2xxx_common_parent = None elif len(_xml_common_parents) > 0: _xml_tag_id.cli_Cli2xxx_common_parent = ctx.cache.node( _xml_common_parents[-1])
def main(): """ Main function. """ # Get the file content input_file = open(CLI_LOG, "rb") arstr_lines = input_file.readlines() input_file.close() # Analyze each line i_line = 0 while i_line < len(arstr_lines): modlog.log(MODLOG_FILTER, modlog.DEBUG, "Line %d:" % (i_line)) str_line = arstr_lines[i_line] debug_line(i_line, str_line) # Remove end of lines modlog.log(MODLOG_FILTER, modlog.DEBUG, " Removing end of line:") arstr_lines[i_line] = str_line = str_line.replace("\r", "").replace( "\n", "") debug_line(i_line, str_line) # Telnet special characters modlog.log(MODLOG_FILTER, modlog.DEBUG, " Processing telnet special characters:") arstr_lines[i_line] = str_line = str_line.replace("\xa7", "ç").replace( "\xc3ç", "ç") arstr_lines[i_line] = str_line = str_line.replace("\xa9", "é").replace( "\xc3é", "é") debug_line(i_line, str_line) # Remove traces menu (debug mode) b_remove_line = False if re.search(r"traces Traces menu", str_line): b_remove_line = True if re.search(r"traces Menu de configuration de traces", str_line): b_remove_line = True # Remove check menu (debug mode) if re.search(r"check Check CLI stuff", str_line): b_remove_line = True if re.search(r"check Vérifications du CLI", str_line): b_remove_line = True if b_remove_line: modlog.log(MODLOG_FILTER, modlog.DEBUG, " Removing debug line!") del arstr_lines[i_line] continue # Backspaces while re.search(r".[\b][\b]* *[\b][\b]*", str_line): modlog.log(MODLOG_FILTER, modlog.DEBUG, " Backspaces found!") arstr_lines[i_line] = str_line = re.sub( r"(.*)[^\b][\b]([\b]*) ( *)[\b]([\b]*)(.*)", r"\1\2\3\4\5", str_line) debug_line(i_line, str_line) i_line += 1 # Eventually write the file over output_file = open(CLI_LOG, "wb") for str_line in arstr_lines: modlog.log(MODLOG_FILTER, modlog.DEBUG, "Writing line") debug_line(-1, str_line) output_file.write(str_line) output_file.write("\n") output_file.close()
def debug_line(i_line, str_line): """ Print out the given line for debug. """ modlog.log( MODLOG_FILTER, modlog.DEBUG, " line[%d] = '%s'" % (i_line, str_line.replace("\b", "\\b").replace( "\r", "\\r").replace("\n", "\\n")))