- :param logDir str: the directory to parse
- :param application str: the name of the application if there is any
- '''
- # Create an instance of xmlLogFile class to create hat.xml file
- xmlHatFilePath = os.path.join(logDir, 'hat.xml')
- xmlHat = xmlLogFile(xmlHatFilePath, "LOGlist", {"application" : application})
- # parse the log directory to find all the command logs, then add it to the xml file
- for fileName in os.listdir(logDir):
- # YYYYMMDD_HHMMSS_namecmd.xml
- sExpr = "^[0-9]{8}_+[0-9]{6}_+.*\.xml$"
- oExpr = re.compile(sExpr)
- if oExpr.search(fileName):
- # get date and hour and format it
- date_hour_cmd = fileName.split('_')
- date_not_formated = date_hour_cmd[0]
- date = "%s/%s/%s" % (date_not_formated[6:8], date_not_formated[4:6], date_not_formated[0:4] )
- hour_not_formated = date_hour_cmd[1]
- hour = "%s:%s:%s" % (hour_not_formated[0:2], hour_not_formated[2:4], hour_not_formated[4:6])
- cmd = date_hour_cmd[2][:-len('.xml')]
- showLog, cmdAppli = showcommandLog(os.path.join(logDir, fileName), cmd, application, notShownCommands)
- #if cmd not in notShownCommands:
- if showLog:
- # add a node to the hat.xml file
- xmlHat.add_simple_node("LogCommand", text=fileName, attrib = {"date" : date, "hour" : hour, "cmd" : cmd, "application" : cmdAppli})
+ :param filename str: The path to the file to create
+ :param xmlroot etree.Element: the Etree element to write to the file
+ :param stylesheet str: The stylesheet to add to the begin of the file
+ """
+ dirname = os.path.dirname(filename)
+ if not os.path.exists(dirname):
+ os.makedirs(dirname)
+ if len(stylesheet) > 0:
+ styleName = stylesheet
+ else:
+ styleName = None
+
+ with open(filename, "w") as f:
+ f.write("<?xml version='1.0' encoding='utf-8'?>\n")
+ if styleName is not None:
+ f.write("<?xml-stylesheet type='text/xsl' href='%s'?>\n" % styleName)
+ res = etree.tostring(xmlroot, encoding='utf-8')
+ # print("********** etree.tostring %s" % res)
+ f.write(res)
+
+ # create fileStyle in dirname if not existing
+ if styleName is not None:
+ styleFile = os.path.join(dirname, styleName)
+ if not os.path.exists(styleFile):
+ # copy if from "salomeTools/src/xsl"
+ srcdir = os.path.dirname(src.__file__)
+ srcFile = os.path.join(srcdir, "xsl", styleName)
+ if verbose: print("write_report %s style %s" % (srcFile, styleFile))
+ shutil.copy(srcFile, dirname)
+
+def escapeSequence(aStr):
+ """
+ See xml specification:
+ The ampersand character(&) and the left angle bracket(<) MUST NOT appear in their
+ literal form, except when used as markup delimiters, or within a comment, a processing
+ instruction, or a CDATA section.
+ If they are needed elsewhere, they MUST be escaped using either numeric character references
+ or the strings '&' and '<' respectively.
+ The right angle bracket(>) may be
+ represented using the string '>', and MUST,
+ for compatibility, be escaped using either '>' or a character reference
+ when it appears in the string " ]]> " in content,
+ when that string is not marking the end of a CDATA section.
+ You can use these escape sequences:
+ < (less - than) as < or <
+ > (greater - than) as > or >
+ & (ampersand) as &
+ ' (apostrophe or single quote) as '
+ " (double-quote) as "
+ """
+ replaces = [ ('&', '&'),
+ ('>', '>'),
+ ('<', '<'),
+ ("'", '''),
+ ('"', '"'),
+ ]
+ res = aStr
+ for ini, fin in replaces: # order matters
+ res = res.replace(ini, fin)
+ return res
+