Пример #1
0
    def content(self):
        """
		Create response
		"""
        global svg_stylesheet
        global defs_stylesheet

        ns = etree.FunctionNamespace("http://www.praterm.com.pl/ISL/pxslt")
        ns.prefix = 'pxslt'
        ns['isl_vu'] = self.isl_vu

        fc = util.FieldStorage(self.req)
        req_type = fc.getfirst('type')
        if req_type == 'defs':
            stylesheet = XSLT(defs_stylesheet)
        else:
            stylesheet = XSLT(svg_stylesheet)

        # this should be set in Apache configuration file using
        # PythonOption directive, for example:
        #   PythonOption szarp.paramd.uri "http://localhost:8081/"
        paramd_uri = self.req.get_options()['szarp.paramd.uri']

        fname = 'file:///etc/szarp/default/config' + self.req.parsed_uri[
            apache.URI_PATH]

        doc = etree.parse(fname, parser)

        r = 'test'
        try:
            r = stylesheet(doc, uri="'" + paramd_uri + "'")
        except Exception, e:
            self.req.log_error('content: stylesheet failed %s' % str(e),
                               apache.APLOG_ERR)
            r = stylesheet.error_log
    def export(self, settings=None):
        from Products.Silva.silvaxml import xmlexport

        # export context to xml
        if settings == None:
            settings = xmlexport.ExportSettings()
        exporter = xmlexport.theXMLExporter
        info = xmlexport.ExportInfo()
        export_root = xmlexport.SilvaExportRoot(self.context)
        xml_export = exporter.exportToString(export_root, settings, info)

        document = ODFDocument('odt_template.zip')
        # Usefull for debug, include the silva export
        #document.add('silva.xml', xml_export)

        for path, id in info.getAssetPaths():
            asset = self.context.restrictedTraverse(path)
            if not silva_interfaces.IImage.providedBy(asset):
                continue
            adapter = interfaces.IAssetData(asset)
            if adapter is not None:
                document.add('Pictures/%s' % asset.id,
                             adapter.getData(),
                             content_type=asset.content_type())

        # now transform the XML
        xml_export = ElementTree(XML(xml_export))
        style_content = XSLT(self._getXSLT())

        content = style_content.apply(xml_export)
        document.add('content.xml', tostring(content))
        return document.download()
Пример #3
0
def process_saml_md_about_sps(saml_md: bytes):
    saml_md_tree = XML(saml_md)
    localparser = XMLParser(
        remove_blank_text=True, resolve_entities=False, remove_comments=False)
    ref = files('SPF_SAML_metadata_processor').joinpath(REMOVE_NAMESPACE_PREFIXES_XSL_FILE_PATH)
    with ref.open('rb') as xslt_root1_file:
        xslt_root1 = parse(xslt_root1_file, parser=localparser)

        transform1 = XSLT(xslt_root1)
        saml_md_tree_1 = transform1(saml_md_tree)

    ref = files('SPF_SAML_metadata_processor').joinpath(REMOVE_KEY_WHITESPACE_XSL_FILE_PATH)
    with ref.open('rb') as xslt_root2_file:
        xslt_root2 = parse(xslt_root2_file, parser=localparser)

    transform2 = XSLT(xslt_root2)
    saml_md_2 = transform2(saml_md_tree_1)

    canonicalized_saml_md_2 = BytesIO()
    saml_md_2.write_c14n(
        canonicalized_saml_md_2, exclusive=True, with_comments=False)

    saml_md_tree_3 = XML(canonicalized_saml_md_2.getvalue(),
                         localparser).getroottree()

    return saml_md_tree_3
Пример #4
0
 def transform(self, indata, config=None, parameters={}):
     strparams = {}
     if config:
         # paths to be used with the document() function
         # must use unix path separators
         if os.sep == "\\":
             config = config.replace(os.sep, "/")
         # print("Tranform: Using config %s. Contents:" % config)
         # print(util.readfile(config))
         config_fullpath = os.path.abspath(config)
         strparams['configurationfile'] = XSLT.strparam(config_fullpath)
     removefiles = []
     for key, value in parameters.items():
         if key.endswith("file") and value:
             if all(ord(c) < 128 and c != " " for c in value):
                 # IF the file name contains ONLY ascii chars and
                 # no spaces, we can use it directly. However, we
                 # need to relativize path of file relative to the
                 # XSL file we'll be using. The mechanism could be
                 # clearer...
                 value = os.path.relpath(value, self.templdir)
             else:
                 # If the filename contains non-ascii characters or
                 # space, any attempt to eg
                 # "document($annotationfile)" in the XSLT document
                 # will silently fail. Seriously, f**k lxml's error
                 # handling. In this case, copy it to a temp file
                 # (in the temporary templdir, with ascii filename)
                 # and use that.
                 contents = util.readfile(value)
                 value = os.path.basename(value)
                 value = "".join(c for c in value
                                 if ord(c) < 128 and c != " ")
                 removefiles.append(self.templdir + os.sep + value)
                 util.writefile(self.templdir + os.sep + value, contents)
             if os.sep == "\\":
                 value = value.replace(os.sep, "/")
         strparams[key] = XSLT.strparam(value)
     try:
         return self._transformer(indata, **strparams)
     except etree.XSLTApplyError as e:
         # the exception will only contain the last error. Errors
         # emanting from the xhtml file will not have file/line
         # number information. Errors emanting from the xslt file
         # do have file/line number info, and is probably more
         # useful to deal with.
         for error in self._transformer.error_log:
             if error.line:
                 log.error("%s: %s (line %s)" %
                           (error.filename, error.message, error.line))
         raise errors.TransformError(str(e))
     finally:
         for f in removefiles:
             util.robust_remove(f)
     # FIXME: This can never be reached, if _transformer() does not
     # raise an error, the above returns immediately.
     if len(self._transformer.error_log) > 0:
         raise errors.TransformError(str(_transformer.error_log))
Пример #5
0
 def transform(self, indata, config=None, parameters={}):
     strparams = {}
     if config:
         # paths to be used with the document() function
         # must use unix path separators
         if os.sep == "\\":
             config = config.replace(os.sep, "/")
         # print("Tranform: Using config %s. Contents:" % config)
         # print(util.readfile(config))
         config_fullpath = os.path.abspath(config)
         strparams['configurationfile'] = XSLT.strparam(config_fullpath)
     removefiles = []
     for key, value in parameters.items():
         if key.endswith("file") and value:
             if all(ord(c) < 128 and c != " " for c in value):
                 # IF the file name contains ONLY ascii chars and
                 # no spaces, we can use it directly. However, we
                 # need to relativize path of file relative to the
                 # XSL file we'll be using. The mechanism could be
                 # clearer...
                 value = os.path.relpath(value, self.templdir)
             else:
                 # If the filename contains non-ascii characters or
                 # space, any attempt to eg
                 # "document($annotationfile)" in the XSLT document
                 # will silently fail. Seriously, f**k lxml's error
                 # handling. In this case, copy it to a temp file
                 # (in the temporary templdir, with ascii filename)
                 # and use that.
                 contents = util.readfile(value)
                 value = os.path.basename(value)
                 value = "".join(c for c in value if ord(c) < 128 and c != " ")
                 removefiles.append(self.templdir+os.sep+value)
                 util.writefile(self.templdir+os.sep+value, contents)
             if os.sep == "\\":
                 value = value.replace(os.sep, "/")
         strparams[key] = XSLT.strparam(value)
     try:
         return self._transformer(indata, **strparams)
     except etree.XSLTApplyError as e:
         # the exception will only contain the last error. Errors
         # emanting from the xhtml file will not have file/line
         # number information. Errors emanting from the xslt file
         # do have file/line number info, and is probably more
         # useful to deal with.
         for error in self._transformer.error_log:
             if error.line:
                 log.error("%s: %s (line %s)" % (error.filename, error.message, error.line))
         raise errors.TransformError(str(e))
     finally:
         for f in removefiles:
             util.robust_remove(f)
     # FIXME: This can never be reached, if _transformer() does not
     # raise an error, the above returns immediately.
     if len(self._transformer.error_log) > 0:
         raise errors.TransformError(str(_transformer.error_log))
Пример #6
0
 def parse_formex(self, doc, source):
     parser = etree.XMLParser(remove_blank_text=True)
     sourcetree = etree.parse(source, parser).getroot()
     fp = self.resourceloader.openfp("xsl/formex.xsl")
     xslttree = etree.parse(fp, parser)
     transformer = etree.XSLT(xslttree)
     params = etree.XSLT
     resulttree = transformer(sourcetree,
                              about=XSLT.strparam(doc.uri),
                              rdftype=XSLT.strparam(str(doc.meta.value(URIRef(doc.uri), RDF.type))))
     return resulttree
     # re-parse to fix whitespace
     buffer = BytesIO(etree.tostring(resulttree, encoding="utf-8"))
     return etree.parse(buffer, parser)
Пример #7
0
    def create_report_templates(self, config):
        """
        This object takes the full configuration element and the path
        to an XSLT and does the transforms necessary to create templates
        for use in BARD reporting
        """
        xslt_path = settings.BARD_TRANSFORM
        template_dir = '%s/django/publicmapping/redistricting/templates' % config.xpath('//Project')[0].get('root')

        # Open up the XSLT file and create a transform
        f = file(xslt_path)
        xml = parse(f)
        transform = XSLT(xml)

        # For each legislative body, create the reporting step HTML 
        # template. If there is no config for a body, the XSLT transform 
        # should create a "Sorry, no reports" template
        bodies = config.xpath('//DistrictBuilder/LegislativeBodies/LegislativeBody')
        for body in bodies:
            # Name  the template after the body's name
            body_id = body.get('id')
            body_name = body.get('name')[:256]

            logging.info("Creating BARD reporting template for %s", body_name)

            body_name = body_name.lower()
            template_path = '%s/bard_%s.html' % (template_dir, body_name)

            # Pass the body's identifier in as a parameter
            xslt_param = XSLT.strparam(body_id)
            result = transform(config, legislativebody = xslt_param) 

            f = open(template_path, 'w')
            f.write(str(result))
            f.close()
Пример #8
0
    def transform(
        self,
        data,
        metamorphic_schema,
        from_structure=None,
        to_structure=None,
        validate_data=False,
        validate_schemas=False,
    ):
        """
        Transform provided data with a metamorphic schema (xslt)

        :param str data: data to transform
        :param str metamorphic_schema: unique_key
        :param from_structure: Structural schema unique_key to check
          data. Check is not performed if None.
        :type from_structure: str or None
        :param to_structure: Structural schema unique_key
          to check transformed data. Check is not performed if None.
        :type to_structure: str or None
        :param bool validate_data: Structural schemas are used if True,
          they are ignored otherwise. This option is suited for better
          perfs. Use it at your own risk.
        :param bool validate_schemas: Any provided schema will be
          valided before use if set to True. They are used as is
          otherwise. This option is suited for better perfs. Use it at
          your own risk.
        :return: transformed data
        :rtype: str (xml)
        """

        if validate_schemas is True:
            if validate_data is True:

                if from_structure is not None:
                    self.assert_structural_schema(from_structure)

                if to_structure is not None:
                    self.assert_structural_schema(to_structure)

            self.assert_metamorphic_schema(metamorphic_schema)

        if validate_data is True and from_structure is not None:
            if self.validate_data(data, from_structure) is False:
                raise ValueError('Original data has not been validated '
                                 'according to \'{}\''.format(from_structure))

        xsl_xml = self.get_cached_schema(metamorphic_schema)
        xslt = XSLT(xsl_xml)

        xml = parse(StringIO(data))

        transformed_data = xslt(xml)

        if validate_data is True and to_structure is not None:
            if self.validate_data(transformed_data, to_structure) is False:
                raise ValueError('Transformed data has not been validated '
                                 'according to \'{}\''.format(to_structure))

        return transformed_data
Пример #9
0
    def validate_schema(self, schema):
        """
        Make sure provided schema's syntax/grammar are correct

        :param str schema: xml (schema itself) or unique_key
        :return: [True, <schema_type>] if schema is correct and
          [False, None] otherwise
        :rtype: list

        .. note:: <schema_type> can either be 'XMLSchema' or 'XSLT'
        """

        if schema in self.get_existing_unique_keys():
            xschema = self.get_cached_schema(schema)
        else:
            try:
                xschema = parse(StringIO(schema))
            except Exception:
                return [False, None]

        try:
            XMLSchema(xschema)
            return [True, 'XMLSchema']
        except Exception:
            pass

        try:
            XSLT(xschema)
            return [True, 'XSLT']
        except Exception:
            pass

        return [False, None]
Пример #10
0
 def __init__(self, xslFileList, name=None, fromKwarg=None, toKwarg=None):
     Converter.__init__(self,
                        name=name,
                        fromKwarg=fromKwarg,
                        toKwarg=toKwarg)
     self._xsltFilelist = xslFileList
     self._xslts = [XSLT(parse(open(s))) for s in self._xsltFilelist]
Пример #11
0
def get_dox(dox_file):
    """Use XSLT to parse certain documentation out of doxygen xml."""
    dox = parse(dox_file)
    with open("dox.xsl", "r") as transform_file:
        transform = XSLT(parse(transform_file))

    result = transform(dox)
    return result
Пример #12
0
 def __init__(self):
     dict.__init__(self)
     self.BROKEN = {}
     for filename in xslt_files:
         name = path.splitext(filename)[0]
         try:
             self[name] = XSLT(parse(path.join(xslt_dir, filename)))
         except Exception, e:
             self.BROKEN[name] = e
Пример #13
0
def parse_xml(stream):

    with resource_stream("crmprtd", "data/ec_xform.xsl") as xsl:
        xsl = parse(xsl)

    # Parse and transform the xml
    et = parse(stream)
    transform = XSLT(xsl)
    return transform(et)
Пример #14
0
def test_xsl_transform_wind_direction(x, expected):
    # Apply the transform
    xsl = resource_filename("crmprtd", "data/ec_xform.xsl")
    transform = XSLT(parse(xsl))
    et = transform(x)

    # Locate changed element
    e = et.xpath(".//mpo:element", namespaces=ns)

    assert e[0].attrib["value"] == expected
Пример #15
0
def test_var_transforms(moti_sawr7110_xml):
    xsl = resource_filename("crmprtd", "data/moti.xsl")
    transform = XSLT(parse(xsl))
    et = transform(moti_sawr7110_xml)
    # Make sure that we can find some of the things to which we transformed
    assert et.xpath(("/cmml/data/observation-series/observation/"
                     "temperature[@type='CURRENT_AIR_TEMPERATURE1']"))
    assert et.xpath("/cmml/data/observation-series/observation/"
                    "temperature/value[@units='celsius']")
    assert et.xpath("/cmml/data/observation-series/observation/"
                    "pressure[@type='ATMOSPHERIC_PRESSURE']")
Пример #16
0
 def transform(self, indata, config=None, parameters={}):
     strparams = {}
     if config:
         # paths to be used with the document() function
         # must use unix path separators
         if os.sep == "\\":
             config = config.replace(os.sep, "/")
         strparams['configurationfile'] = XSLT.strparam(config)
     for key, value in parameters.items():
         if key.endswith("file"):
             # relativize path of file relative to the XSL file
             # we'll be using. The mechanism could be clearer...
             value = os.path.relpath(value, self.templdir)
             if os.sep == "\\":
                 value = value.replace(os.sep, "/")
         strparams[key] = XSLT.strparam(value)
     try:
         return self._transformer(indata, **strparams)
     except etree.XSLTApplyError as e:
         raise errors.TransformError(str(e))
     if len(self._transformer.error_log) > 0:
         raise errors.TransformError(str(_transformer.error_log))
Пример #17
0
    def transform(self, xlm_path: str, file: str) -> str:
        dom = parse(xlm_path)
        try:
            xsl_parse = parse(self.xsl_path)
        except XMLSyntaxError as syntax_exc:
            return f"Error in file syntax {os.path.basename(file)}: {syntax_exc}"
        transform = XSLT(xsl_parse)
        result = transform(dom)

        xsl_file = open(file, "wb")
        xsl_file.write(result)
        xsl_file.close()
        return "Success"
Пример #18
0
    def create_report_templates(self, config):
        """
        This object takes the full configuration element and the path
        to an XSLT and does the transforms necessary to create templates
        for use in BARD reporting
        """
        xslt_path = settings.BARD_TRANSFORM
        template_dir = '%s/django/publicmapping/redistricting/templates' % config.xpath(
            '//Project')[0].get('root')

        # Open up the XSLT file and create a transform
        f = file(xslt_path)

        xml = parse(f)
        transform = XSLT(xml)

        # For each legislative body, create the reporting step HTML
        # template. If there is no config for a body, the XSLT transform
        # should create a "Sorry, no reports" template
        bodies = config.xpath(
            '//DistrictBuilder/LegislativeBodies/LegislativeBody')
        for body in bodies:
            # Name  the template after the body's name
            body_id = body.get('id')
            body_name = body.get('name')[:256]

            logger.info("Creating BARD reporting template for %s", body_name)

            body_name = body_name.lower()
            template_path = '%s/bard_%s.html' % (template_dir, body_name)

            # Pass the body's identifier in as a parameter
            xslt_param = XSLT.strparam(body_id)
            result = transform(config, legislativebody=xslt_param)

            f = open(template_path, 'w')
            f.write(str(result))
            f.close()
Пример #19
0
    def _transform_doc(self) -> bytes:
        """
        Transform original tree using stylesheet.

        This method will transform original xml using XSLT script into
        am ideally flatter xml document for easier parsing and migration
        to Data Frame.
        """
        from lxml.etree import XSLT

        transformer = XSLT(self.xsl_doc)
        new_doc = transformer(self.xml_doc)

        return bytes(new_doc)
Пример #20
0
class RelocatableRelaxNG(object):
    _relocate_xslt = XSLT(
        XML('''\
    <xsl:stylesheet version="1.0"
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns:rng="http://relaxng.org/ns/structure/1.0"
         xmlns="http://relaxng.org/ns/structure/1.0">
      <xsl:template match="/rng:*">
        <xsl:copy>
          <xsl:copy-of select="@*"/>
          <rng:start><rng:ref rng:name="{$newref}"/></rng:start>
          <xsl:apply-templates/>
        </xsl:copy>
      </xsl:template>
      <xsl:strip-space elements="*"/>
      <xsl:template match="rng:start"/>
      <xsl:template match="*">
        <xsl:copy><xsl:copy-of select="@*"/><xsl:apply-templates/></xsl:copy>
      </xsl:template>
    </xsl:stylesheet>
    '''))

    def __init__(self, tree, start=None):
        self._tree = tree
        self._start = start

    def validate(self, xml_tree):
        if self._start is None:
            rng_tree = self._tree
        else:
            rng_tree = self._relocate_xslt(self._tree,
                                           newref="'%s'" % self._start)
            # ugly hack to get around namespace (?) issues
            rng_tree = parse(StringIO(str(rng_tree)))
        rng = RelaxNG(rng_tree)
        self.validate = rng.validate  # replace the object method by the real thing
        return rng.validate(xml_tree)

    def copy(self, start=None):
        return self.__class__(self._tree, start)

    def relocate(self, start):
        self._start = start
        try:
            del self.validate
        except AttributeError:
            pass
Пример #21
0
    def transform(cls, xml_filename, xsl_filename, output_filepath):
        """

        Args:
            xml_filename:
            xsl_filename:
            output_filepath:

        Returns:

        """

        dom = etree.parse(xml_filename)
        xslt = etree.parse(xsl_filename)
        transform = XSLT(xslt)
        newdom = transform(dom)
        newdom.write_output(output_filepath)
        print(type(newdom))
        print(dir(newdom))
        print(newdom)
Пример #22
0
def convert_record(filename: str, xsl_filename: str):
    source_doc = parse(filename)

    with open('epitafium.rnc') as schema_file:
        schema = RelaxNG.from_rnc_string(schema_file.read())

    if schema.validate(source_doc):
        pass
        # print("VALID!")
    else:
        for err in schema.error_log:
            print(err)

    # Transform
    xslt = parse(xsl_filename)
    transform = XSLT(xslt)
    target_doc = transform(source_doc)

    # Output
    return target_doc.getroot()
Пример #23
0
    def to_html(self):
        """ Transforms XML to HTML using the docket XSLT """

        xml_string = self.to_xml()

        # Read the XSL file.
        xslfile = open(XSL_PATH)
        docket_xsl = parse(xslfile)
        xslfile.close()

        # Do the XML->HTML transform
        docket_xsl_transform = XSLT(docket_xsl)

        xmldoc = parse(StringIO.StringIO(xml_string))
        html = unicode(
            docket_xsl_transform(
                xmldoc,
                DEV_BUCKET_PREFIX="'%s'" % settings.DEV_BUCKET_PREFIX,
            )).encode("utf-8")

        return html
Пример #24
0
def test_var_transforms_all(moti_sawr7100_large):
    xsl = resource_filename("crmprtd", "data/moti.xsl")
    transform = XSLT(parse(xsl))
    et = transform(moti_sawr7100_large)
    assert et.xpath(("/cmml/data/observation-series/observation/"
                     "pressure[@type='ATMOSPHERIC_PRESSURE']"))
    assert et.xpath(("/cmml/data/observation-series/observation/"
                     "wind[@type='MEASURED_WIND_SPEED1']"))
    assert et.xpath(("/cmml/data/observation-series/observation/"
                     "wind[@type='MEASURED_WIND_DIRECTION1']"))
    assert et.xpath(("/cmml/data/observation-series/observation/"
                     "wind[@type='WIND_DIRECTION_STD_DEVIATION1']"))
    assert et.xpath(("/cmml/data/observation-series/observation/"
                     "temperature[@type='CURRENT_AIR_TEMPERATURE1']"))
    assert et.xpath(("/cmml/data/observation-series/observation/"
                     "temperature[@type='DEW_POINT']"))
    assert et.xpath(("/cmml/data/observation-series/observation/"
                     "precipitation[@type='HOURLY_PRECIPITATION']"))
    assert et.xpath(("/cmml/data/observation-series/observation/"
                     "humidity[@type='RELATIVE_HUMIDITY1']"))
    assert et.xpath(("/cmml/data/observation-series/observation/"
                     "snow[@type='HEIGHT_OF_SNOW']"))
Пример #25
0
    def transform_doc(self) -> bytes:
        """
        Parse stylesheet from file or buffer and run it.

        This method will parse stylesheet object into tree for parsing
        conditionally by its specific object type, then transforms
        original tree with XSLT script.
        """

        from lxml.etree import (
            XSLT,
            XMLParser,
            fromstring,
            parse,
        )

        style_doc = self.stylesheet

        handle_data = get_data_from_filepath(
            filepath_or_buffer=style_doc,
            encoding=self.encoding,
            compression=self.compression,
            storage_options=self.storage_options,
        )

        with preprocess_data(handle_data) as xml_data:
            curr_parser = XMLParser(encoding=self.encoding)

            if isinstance(xml_data, io.StringIO):
                xsl_doc = fromstring(
                    xml_data.getvalue().encode(self.encoding), parser=curr_parser
                )
            else:
                xsl_doc = parse(xml_data, parser=curr_parser)

        transformer = XSLT(xsl_doc)
        new_doc = transformer(self.root)

        return bytes(new_doc)
Пример #26
0
def diff_xml(fromFile, toFile, outputFile):
    global xslt_str
    xml = []
    files = []

    parser = XMLParser(remove_blank_text=True)
    xslt = fromstring(xslt_str)
    transform = XSLT(xslt)

    xml.append(parse(fromFile, parser))
    xml.append(parse(toFile, parser))

    for xfile in xml:
        files.append(
            tostring(transform(xfile)).replace(" ", "\n").replace(
                "><", ">\n<").replace("/>", "\n/>").replace("\">", "\"\n>"))

    for nfile in files:
        index = 0
        tab = "\t"
        newfile = ""
        for line in nfile.splitlines(1):
            if line.startswith("</"):
                index -= 1
            newfile += (tab * index) + line
            if line.startswith("</"):
                index -= 1
            if line.startswith("<"):
                index += 1
            if line.endswith("/>\n"):
                index -= 1
        nfile = newfile

    diff = difflib.unified_diff(files[0].splitlines(1), files[1].splitlines(1))

    if outputFile:
        with open(outputFile, "wb") as f:
            for d in diff:
                f.write(d)
Пример #27
0
def applyXSLTTransforms():
    for s in SCHEMAS:
        fp1 = "{}/{}.xslt".format(s, s)
        fp2 = "{}/html_examples".format(s)
        fps = glob.glob("{}/examples/*.xml".format(s))

        if os.path.isfile(fp1):
            xsltDocument = parse(fp1)

            if not os.path.isdir(fp2):
                os.mkdir(fp2)

            for fp in fps:
                xmlDocument = parse(fp)
                transform = XSLT(xsltDocument)
                htmlDocument = transform(xmlDocument)

                fileName = os.path.basename(fp)
                fileName = fileName[:fileName.find(".xml")]

                fp3 = os.path.join(fp2, fileName + ".html")

                with open(fp3, "wb") as fo1:
                    fo1.write(tostring(htmlDocument, pretty_print=True))
Пример #28
0
 def _parseXslt(filename):
     with open(filename) as fp:
         return XSLT(parse(fp))
Пример #29
0
def run(param):
   #read xml file
   scriptsDir = os.getcwd()
   os.chdir("../../")
   baseDir = os.getcwd()
   qaResultsDir = baseDir + "/QAResults/";
   xmlDir = baseDir + "/QAResults/xml"

   if not os.path.exists(xmlDir):
     print "Xml Directory not present"
     sys.exit(1)
    
   htmlDir = baseDir + "/QAResults/html"

   if not os.path.exists(htmlDir):
     os.makedirs(htmlDir)
   if not os.path.exists(htmlDir+ "/Styles"):  
     os.makedirs(htmlDir + "/Styles")

   if(os.path.exists(htmlDir + "/Styles")):
     shutil.rmtree(htmlDir + "/Styles")
     shutil.copytree(scriptsDir + "/Styles",htmlDir + "/Styles/")
     if(os.path.exists(htmlDir+ "/Styles/.svn")):
       for root, dirs, files in os.walk(htmlDir + "/Styles/.svn", topdown=False):
         for name in files:
             filename = os.path.join(root, name)
             os.chmod(filename, stat.S_IWUSR)
             os.remove(filename)
         for name in dirs:
             os.rmdir(os.path.join(root, name))
       os.chmod(htmlDir + "/Styles/.svn", stat.S_IWUSR)
       os.rmdir(htmlDir + "/Styles/.svn")
     
   xmlList=os.listdir(xmlDir)
   htmlList=[file.replace(".xml", ".html") for file in os.listdir(xmlDir)]

   ruleLinksString = ""
   for link in htmlList:
     if(link != "index.html" and link != "Styles"):
       ruleLinksString = ruleLinksString + "<li><a href=\"" + link + "\">" + link[:link.find(".")] + "</a></li>\n"

   xsltH = """<xsl:stylesheet version="2.0"
           xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output indent="yes"/>
   <xsl:template name="break">
           <xsl:param name="text"/>
           <xsl:choose>
                   <xsl:when test="contains($text, '&#xa;')">
                           <xsl:value-of select="substring-before($text, '&#xa;')"/>
                           <br/>
                           <xsl:call-template name="break">
                                   <xsl:with-param name="text" select="substring-after($text,'&#xa;')"/>
                           </xsl:call-template>
                   </xsl:when>
                   <xsl:otherwise>
                   <xsl:value-of select="$text"/>
                   </xsl:otherwise>
           </xsl:choose>
   </xsl:template>
   <xsl:template match="/">"""

   xsltT = """
   </xsl:template>
   </xsl:stylesheet>
   """

   headString = "".join(open(htmlDir + "/Styles/head.temp"))
   headString = headString.replace("@@@_PUBLISH_DATE_@@@", str( datetime.now().date()))
   centerString = "".join(open(htmlDir + "/Styles/center.temp"))
   tailString = "".join(open(htmlDir + "/Styles/tail.temp"))
   
   #check for external scripting
   if(param['LCOVCoverage']):
      #generateExternalLink
      
      externalScriptDirectory = scriptsDir + "/ExternalScripts"
      os.chdir(externalScriptDirectory)
      os.system("python " + externalScriptDirectory + "/LCOVCoveragePublish.py")
      pos = headString.find("@@@_EXTERNAL_TOOLS_REPORT_@@@")-1
      
      os.chdir(scriptsDir);
      headString = headString[:pos] + "<li><a href=\"../externalLCOVCoverage/index.html\">LCOV Coverage</a></li>" + headString[pos:]

   #remove placeholder for external scripting
   headString = headString.replace("@@@_EXTERNAL_TOOLS_REPORT_@@@", "")


   result = True
   for xmlFile in xmlList:
     try:
       filename = os.path.splitext(xmlFile)[0]
       print "Formatting in HTML " + filename
       #with lxml parse the file
       f = open(xmlDir + "/" + xmlFile,'r')
       xml = fromstring(str(f.read()))

       #with lxml create html
       searchF = filename  + ".xslt"
       absPathXslt = ""
       for top, dirs, files in os.walk('./'):
         for nm in files:
           if(nm == searchF):
             absPathXslt = os.path.join(top, nm)

       fileXslt = open(absPathXslt, 'r') 
       #print xsltH + headString + centerString + str(fileXslt.read()) + tailString + xsltT
       xsl=  fromstring(xsltH + headString + ruleLinksString + centerString + str(fileXslt.read()) + tailString + xsltT) 
       style = XSLT(xsl)
       result = style.apply(xml)
           
       #print htmlDir + filename + ".html"
       html = open(htmlDir + "/" + filename + ".html", 'w')
       print >> html , style.tostring(result)
     except Exception, e:
       result = False
       print "!!!!!!Bad Formatted XML on ", filename , "!!!!!!!"
       print e
       os.chdir(scriptsDir)
Пример #30
0
def ec_xml_single_obs():
    x = fromstring(b"""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<om:ObservationCollection xmlns="http://dms.ec.gc.ca/schema/point-observation/2.1" xmlns:gml="http://www.opengis.net/gml" xmlns:om="http://www.opengis.net/om/1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <om:member>
    <om:Observation>
      <om:metadata>
        <set>
          <general>
            <author build="build.4063" name="MSC-DMS-PG-WXO-Summary" version="2.4"/>
            <dataset name="mscobservation/atmospheric/surface_weather/wxo_dd_hour_summary-1.0-ascii/"/>
            <phase name="product-wxo_xml-1.0/"/>
            <id xlink:href="/data/msc/observation/atmospheric/surface_weather/wxo_dd_hour_summary-1.0-ascii/product-wxo_xml-1.0/20160528024500000/bc/intermediate/en"/>
            <parent xlink:href="/data/msc/observation/atmospheric/surface_weather/wxo_dd_hour_summary-1.0-ascii/product-wxo_xml-1.0/20160528024500000/bc/intermediate/en"/>
          </general>
          <identification-elements>
            <element name="station_name" uom="unitless" value="Abbotsford Airport"/>
            <element name="latitude" uom="degree" value="49.025278"/>
            <element name="longitude" uom="degree" value="-122.36"/>
            <element name="transport_canada_id" uom="unitless" value="YXX"/>
            <element name="observation_date_utc" uom="unitless" value="2016-05-28T02:00:00.000Z"/>
            <element name="observation_date_local_time" uom="unitless" value="2016-05-27T19:00:00.000 PDT"/>
            <element name="climate_station_number" uom="unitless" value="1100031"/>
            <element name="wmo_station_number" uom="unitless" value="71108"/>
          </identification-elements>
        </set>
      </om:metadata>
      <om:samplingTime>
        <gml:TimeInstant>
          <gml:timePosition>2016-05-28T02:00:00.000Z</gml:timePosition>
        </gml:TimeInstant>
      </om:samplingTime>
      <om:resultTime>
        <gml:TimeInstant>
          <gml:timePosition>2016-05-28T02:00:00.000Z</gml:timePosition>
        </gml:TimeInstant>
      </om:resultTime>
      <om:procedure xlink:href="msc/observation/atmospheric/surface_weather/wxo_dd_hour_summary-1.0-ascii/product-wxo_xml-1.0/20160528024500000/bc/intermediate/en"/>
      <om:observedProperty gml:remoteSchema="/schema/point-observation/2.0.xsd"/>
      <om:featureOfInterest>
        <gml:FeatureCollection>
          <gml:location>
            <gml:Point>
              <gml:pos>49.025278 -122.36</gml:pos>
            </gml:Point>
          </gml:location>
        </gml:FeatureCollection>
      </om:featureOfInterest>
      <om:result>
        <elements>
          <element name="present_weather" uom="code" value="Mostly Cloudy"/>
          <element name="mean_sea_level" uom="kPa" value="101.9"/>
          <element name="tendency_amount" uom="kPa" value="0.12"/>
          <element name="tendency_characteristic" uom="code" value="falling"/>
          <element name="horizontal_visibility" uom="km" value="40.2"/>
          <element name="air_temperature" uom="Celsius" value="13.7"/>
          <element name="dew_point" uom="Celsius" value="5.7"/>
          <element name="relative_humidity" uom="percent" value="58"/>
          <element name="wind_speed" uom="km/h" value="18"/>
          <element name="wind_direction" uom="code" value="S"/>
          <element name="wind_gust_speed" uom="km/h" value="29"/>
          <element name="total_cloud_cover" uom="code" value="8"/>
          <element name="wind_chill" uom="unitless" value=""/>
          <element name="humidex" uom="unitless" value=""/>
        </elements>
      </om:result>
    </om:Observation>
  </om:member>
</om:ObservationCollection>""")  # noqa
    xsl = resource_filename("crmprtd", "data/ec_xform.xsl")
    transform = XSLT(parse(xsl))
    return transform(x)
Пример #31
0
def publishReport():
    extScriptDir = os.getcwd()
    os.chdir(qaDir)
    baseDir = os.getcwd()
    qaResultsDir = os.path.join(baseDir, "QAResults")
    htmlDir = os.path.join(baseDir, "QAResults", "html")
    xmlDir = os.path.join(baseDir, "QAResults", "xml")

    CPPCheckStaticAnalisysDir = os.path.join(
        qaResultsDir, "externalcppcheck")  #need to parametrize this?
    reportFile = os.path.join(CPPCheckStaticAnalisysDir, "report.xml")

    try:
        filename = reportFile
        print "Formatting in HTML " + filename
        #with lxml parse the file
        f = open(filename)
        xml = fromstring(str(f.read()))

        #with lxml create html
        absPathXslt = os.path.join(currentPathScript, "cppcheckPlugin.xslt")

        fileXslt = open(absPathXslt, 'r')
        #print xsltH + headString + centerString + str(fileXslt.read()) + tailString + xsltT
        xsl = fromstring(fileXslt.read().replace("@@@_PUBLISH_DATE_@@@",
                                                 str(datetime.now().date())))

        style = XSLT(xsl)
        result = style.apply(xml)

        #print htmlDir + filename + ".html"
        html = open(os.path.join(CPPCheckStaticAnalisysDir, "report.html"),
                    'w')
        print >> html, style.tostring(result)

        #create introduction for cppcheck
        headString = "".join(open(os.path.join(htmlDir, "Styles",
                                               "head.temp")))
        headString = headString.replace("@@@_PUBLISH_DATE_@@@",
                                        str(datetime.now().date()))
        centerString = "".join(
            open(os.path.join(htmlDir, "Styles", "center.temp")))
        tailString = "".join(open(os.path.join(htmlDir, "Styles",
                                               "tail.temp")))

        #modify headstring adding cppcheck stylesheet, and substitute relative path
        pos = headString.find("</head>") - 1
        headStringNew = headString

        headStringNew = headString[:
                                   pos] + "<link rel=\"stylesheet\" type=\"text/css\" href=\"../html/Styles/gcov.css\">\n" + headString[
                                       pos:]
        headStringNew = headStringNew.replace("href=\"Styles/",
                                              "href=\"../html/Styles/")
        headStringNew = headStringNew.replace(
            "<li><a href=\"index.html\">Introduction</a></li>",
            "<li><a href=\"../html/index.html\">Introduction</a></li>")
        headStringNew = headStringNew.replace("@@@_EXTERNAL_TOOLS_REPORT_@@@",
                                              "")  #remove placeholder
        headStringNew = headStringNew.replace("Rule Scripts",
                                              "Static Analysis")
        fileIndex = open(os.path.join(CPPCheckStaticAnalisysDir, "index.html"),
                         'w')
        fileIndex.write(
            "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">"
        )

        fileIndex.write(headStringNew)
        ruleLinksStringIndex = "<li><a href=\"" + "./report.html\">" + "Cppcheck Report" + "</a></li>\n"

        fileIndex.write(ruleLinksStringIndex)
        fileIndex.write(centerString)
        fileIndex.write("""
       <h1> Static Analysis Using CPPCHECK</h1>
       Cppcheck is an analysis tool for C/C++ code. <br>
       Unlike C/C++ compilers and many other analysis tools, it don't detect syntax errors. <br>
       Cppcheck only detects the types of bugs that the compilers normally fail to detect. The goal is no false positives.<br>
       Currently the CPPCHECK version used is the 1.4.7, and the check has been executed on the root directory of the MAF framework.<br>
       <br>Some features:<br>
        
       <ul><li>Out of bounds checking
        <li>Check the code for each class</li>
        <li>Checking exception safety</li>
        <li>Memory leaks checking</li>
        <li>Warn if obsolete functions are used</li>
        <li>Check for invalid usage of STL</li>
        <li>Check for uninitialized variables and unused functions</li>
       </ul>
       <br>
       <h2> <a href="./report.html">Go to Report table</a> </h2>
       <br>
       """)
        fileIndex.write(tailString)
        fileIndex.close()

    except Exception, e:
        print "Problem when publishing cppcheck, error:", e
Пример #32
0
def publishReport():
    extScriptDir = os.getcwd()
    os.chdir(qaDir)
    baseDir = os.getcwd()
    qaResultsDir = os.path.join(baseDir, "QAResults")
    htmlDir = os.path.join(baseDir, "QAResults","html")
    xmlDir = os.path.join(baseDir, "QAResults", "xml")
    
    xmlList=os.listdir(xmlDir)
    htmlList=[file.replace(".xml", ".html") for file in os.listdir(xmlDir)]
    
    ccccExternalDir = qaResultsDir + "/externalcccc" #need to parametrize this?
    covList = os.listdir(ccccExternalDir)
    
    if(os.path.exists(htmlDir) == False):
        print "Html Results Dir doesn't exist %s" % htmlDir
        exit()

    if(os.path.exists(qaResultsDir) == False):
        print "cccc directory results doesn't exist %s" % ccccExternalDir
        exit()


    """if(os.path.exists(os.path.join(ccccExternalDir , "index.html"))):
            print "************************* Warning ******************************"
            print "cccc Report already Formatted, please remove directory and rerun Publishing script"
            print "************************* ------- ******************************"
            return      
    """
    stylePath = os.path.join(htmlDir, "Styles")
    headString = "".join(open(stylePath + "/head.temp"))
    headString = headString.replace("@@@_PUBLISH_DATE_@@@", str( datetime.now().date()))
    centerString = "".join(open(stylePath + "/center.temp"))
    tailString = "".join(open(stylePath + "/tail.temp"))
        
    #write here format
    xmlReports = [item for item in os.listdir(ccccExternalDir)  if (os.path.splitext( item )[1] == ".xml")]
    ccccLinks = [(os.path.splitext(item)[0] + ".html") for item in xmlReports]
    
    ruleLinksStringIndex = ""
    for link in ccccLinks:
        ruleLinksStringIndex = ruleLinksStringIndex + "<li><a href=\"" + link + "\">" + link[:-9] + "</a></li>\n"
    
    try:
        for reportFile in xmlReports:
            #for each file apply stylesheet
            htmlName = os.path.join(ccccExternalDir, os.path.splitext( reportFile )[0] + ".html")
            print "Formatting in HTML " + htmlName
            #with lxml parse the file
            f = open(os.path.join(ccccExternalDir,reportFile))
            xml = fromstring(str(f.read()))

            #with lxml create html
            absPathXslt = os.path.join(currentPathScript,"ccccPlugin.xslt")
       
            fileXslt = open(absPathXslt, 'r') 
            #print xsltH + headString + centerString + str(fileXslt.read()) + tailString + xsltT
            xsl = fromstring(fileXslt.read().replace("@@@_PUBLISH_DATE_@@@", str( datetime.now().date())).replace(" @@@_EXTERNAL_TOOLS_LINKS_@@@",ruleLinksStringIndex).replace("@@@_EXTERNAL_TOOLS_REPORT_@@@", ""))

            style = XSLT(xsl)
            result = style.apply(xml)
           
            #print htmlDir + filename + ".html"
            html = open(os.path.join(ccccExternalDir, htmlName), 'w')
            print >> html , style.tostring(result)    
    
    except Exception, e:
        print "Problem when publishing cccc , error:" , e
Пример #33
0
def publishReport():
    extScriptDir = os.getcwd()
    os.chdir(qaDir)
    baseDir = os.getcwd()
    qaResultsDir = os.path.join(baseDir,"QAResults")
    htmlDir = os.path.join(baseDir,"QAResults","html")
    xmlDir = os.path.join(baseDir,"QAResults","xml")
    
    CPPCheckStaticAnalisysDir = os.path.join(qaResultsDir, "externalcppcheck") #need to parametrize this?
    reportFile = os.path.join(CPPCheckStaticAnalisysDir, "report.xml");
    
    try:
       filename = reportFile
       print "Formatting in HTML " + filename
       #with lxml parse the file
       f = open(filename)
       xml = fromstring(str(f.read()))

       #with lxml create html
       absPathXslt = os.path.join(currentPathScript,"cppcheckPlugin.xslt")
       
       fileXslt = open(absPathXslt, 'r') 
       #print xsltH + headString + centerString + str(fileXslt.read()) + tailString + xsltT
       xsl=  fromstring(fileXslt.read().replace("@@@_PUBLISH_DATE_@@@", str( datetime.now().date())))

       style = XSLT(xsl)
       result = style.apply(xml)
           
       #print htmlDir + filename + ".html"
       html = open(os.path.join(CPPCheckStaticAnalisysDir, "report.html"), 'w')
       print >> html , style.tostring(result)
       
       #create introduction for cppcheck
       headString = "".join(open(os.path.join(htmlDir,  "Styles", "head.temp")))
       headString = headString.replace("@@@_PUBLISH_DATE_@@@", str( datetime.now().date()))
       centerString = "".join(open(os.path.join(htmlDir,"Styles", "center.temp")))
       tailString = "".join(open(os.path.join(htmlDir, "Styles", "tail.temp")))
       
       #modify headstring adding cppcheck stylesheet, and substitute relative path
       pos = headString.find("</head>")-1
       headStringNew = headString
                    
                                
       headStringNew = headString[:pos] + "<link rel=\"stylesheet\" type=\"text/css\" href=\"../html/Styles/gcov.css\">\n" + headString[pos:]
       headStringNew = headStringNew.replace("href=\"Styles/", "href=\"../html/Styles/")
       headStringNew = headStringNew.replace("<li><a href=\"index.html\">Introduction</a></li>", "<li><a href=\"../html/index.html\">Introduction</a></li>")
       headStringNew = headStringNew.replace("@@@_EXTERNAL_TOOLS_REPORT_@@@", "") #remove placeholder
       headStringNew = headStringNew.replace("Rule Scripts", "Static Analysis")
       fileIndex = open(os.path.join(CPPCheckStaticAnalisysDir, "index.html"), 'w');
       fileIndex.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">")
        
       fileIndex.write(headStringNew)
       ruleLinksStringIndex = "<li><a href=\"" + "./report.html\">" + "Cppcheck Report" + "</a></li>\n"
        
       fileIndex.write(ruleLinksStringIndex)
       fileIndex.write(centerString)
       fileIndex.write("""
       <h1> Static Analysis Using CPPCHECK</h1>
       Cppcheck is an analysis tool for C/C++ code. <br>
       Unlike C/C++ compilers and many other analysis tools, it don't detect syntax errors. <br>
       Cppcheck only detects the types of bugs that the compilers normally fail to detect. The goal is no false positives.<br>
       Currently the CPPCHECK version used is the 1.4.7, and the check has been executed on the root directory of the MAF framework.<br>
       <br>Some features:<br>
        
       <ul><li>Out of bounds checking
        <li>Check the code for each class</li>
        <li>Checking exception safety</li>
        <li>Memory leaks checking</li>
        <li>Warn if obsolete functions are used</li>
        <li>Check for invalid usage of STL</li>
        <li>Check for uninitialized variables and unused functions</li>
       </ul>
       <br>
       <h2> <a href="./report.html">Go to Report table</a> </h2>
       <br>
       """)
       fileIndex.write(tailString)
       fileIndex.close()
         
    except Exception, e:
        print "Problem when publishing cppcheck, error:" , e
Пример #34
0
        <table>
            <thead>
                <tr>
                    <th>English Name</th>
                    <th>Latin Name</th>
                    <th>Cost</th>
                </tr>
            </thead>

            <xsl:for-each select="CATALOG/PLANT">
                <tr>
                    <td><xsl:value-of select="COMMON"/></td>
                    <td><xsl:value-of select="BOTANICAL"/></td>
                    <td><xsl:value-of select="PRICE"/></td>
                </tr>
            </xsl:for-each>

        </table>
    </html>
"""

transform = XSLT(XML(TEMPLATE))
data = parse(StringIO(DATA))
result = transform(data)

df = pd.read_html(str(result))[0]

df['Cost'] = df['Cost'].apply(lambda x: float(x.replace('$', '')))
df['Cost'].mean()
# 6.369166666666666
Пример #35
0
def publishReport():
    extScriptDir = os.getcwd()
    os.chdir(qaDir)
    baseDir = os.getcwd()
    qaResultsDir = os.path.join(baseDir, "QAResults")
    htmlDir = os.path.join(baseDir, "QAResults", "html")
    xmlDir = os.path.join(baseDir, "QAResults", "xml")

    xmlList = os.listdir(xmlDir)
    htmlList = [file.replace(".xml", ".html") for file in os.listdir(xmlDir)]

    ccccExternalDir = qaResultsDir + "/externalcccc"  #need to parametrize this?
    covList = os.listdir(ccccExternalDir)

    if (os.path.exists(htmlDir) == False):
        print "Html Results Dir doesn't exist %s" % htmlDir
        exit()

    if (os.path.exists(qaResultsDir) == False):
        print "cccc directory results doesn't exist %s" % ccccExternalDir
        exit()
    """if(os.path.exists(os.path.join(ccccExternalDir , "index.html"))):
            print "************************* Warning ******************************"
            print "cccc Report already Formatted, please remove directory and rerun Publishing script"
            print "************************* ------- ******************************"
            return      
    """
    stylePath = os.path.join(htmlDir, "Styles")
    headString = "".join(open(stylePath + "/head.temp"))
    headString = headString.replace("@@@_PUBLISH_DATE_@@@",
                                    str(datetime.now().date()))
    centerString = "".join(open(stylePath + "/center.temp"))
    tailString = "".join(open(stylePath + "/tail.temp"))

    #write here format
    xmlReports = [
        item for item in os.listdir(ccccExternalDir)
        if (os.path.splitext(item)[1] == ".xml")
    ]
    ccccLinks = [(os.path.splitext(item)[0] + ".html") for item in xmlReports]

    ruleLinksStringIndex = ""
    for link in ccccLinks:
        ruleLinksStringIndex = ruleLinksStringIndex + "<li><a href=\"" + link + "\">" + link[:
                                                                                             -9] + "</a></li>\n"

    try:
        for reportFile in xmlReports:
            #for each file apply stylesheet
            htmlName = os.path.join(ccccExternalDir,
                                    os.path.splitext(reportFile)[0] + ".html")
            print "Formatting in HTML " + htmlName
            #with lxml parse the file
            f = open(os.path.join(ccccExternalDir, reportFile))
            xml = fromstring(str(f.read()))

            #with lxml create html
            absPathXslt = os.path.join(currentPathScript, "cccc.xslt")

            fileXslt = open(absPathXslt, 'r')
            #print xsltH + headString + centerString + str(fileXslt.read()) + tailString + xsltT
            xsl = fromstring(fileXslt.read().replace(
                "@@@_PUBLISH_DATE_@@@", str(datetime.now().date())).replace(
                    " @@@_EXTERNAL_TOOLS_LINKS_@@@",
                    ruleLinksStringIndex).replace(
                        "@@@_EXTERNAL_TOOLS_REPORT_@@@", ""))

            style = XSLT(xsl)
            result = style.apply(xml)

            #print htmlDir + filename + ".html"
            html = open(os.path.join(ccccExternalDir, htmlName), 'w')
            print >> html, style.tostring(result)

    except Exception, e:
        print "Problem when publishing cccc , error:", e
Пример #36
0
def run(param):
    #read xml file
    scriptsDir = os.getcwd()
    os.chdir(mafPath.mafQADir)
    baseDir = os.getcwd()
    qaResultsDir = os.path.join(baseDir, "QAResults")
    xmlDir = os.path.join(baseDir, "QAResults", "xml")

    if not os.path.exists(xmlDir):
        print "Xml Directory not present: ", xmlDir
        sys.exit(1)

    htmlDir = os.path.join(baseDir, "QAResults", "html")

    if not os.path.exists(htmlDir):
        os.makedirs(htmlDir)
    if not os.path.exists(os.path.join(htmlDir, "Styles")):
        os.makedirs(os.path.join(htmlDir, "Styles"))

    if (os.path.exists(os.path.join(htmlDir, "Styles"))):
        origDir = os.path.join(scriptsDir, "Styles")
        destDir = os.path.join(htmlDir, "Styles")
        files = os.listdir(origDir)
        for item in files:
            if (item != 'CVS' and item != 'SVN' and item != '.cvs'
                    and item != '.svn'):
                shutil.copyfile(os.path.join(origDir, item),
                                os.path.join(destDir, item))

    xmlList = os.listdir(xmlDir)
    htmlList = [file.replace(".xml", ".html") for file in os.listdir(xmlDir)]

    ruleLinksString = ""
    for link in htmlList:
        if (link != "index.html" and link != "Styles"):
            ruleLinksString = ruleLinksString + "<li><a href=\"" + link + "\">" + link[:link.find(
                ".")] + "</a></li>\n"

    xsltH = """<xsl:stylesheet version="2.0"
           xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output indent="yes"/>
   <xsl:template name="break">
           <xsl:param name="text"/>
           <xsl:choose>
                   <xsl:when test="contains($text, '&#xa;')">
                           <xsl:value-of select="substring-before($text, '&#xa;')"/>
                           <br/>
                           <xsl:call-template name="break">
                                   <xsl:with-param name="text" select="substring-after($text,'&#xa;')"/>
                           </xsl:call-template>
                   </xsl:when>
                   <xsl:otherwise>
                   <xsl:value-of select="$text"/>
                   </xsl:otherwise>
           </xsl:choose>
   </xsl:template>
   <xsl:template match="/">"""

    xsltT = """
   </xsl:template>
   </xsl:stylesheet>
   """

    headString = "".join(open(os.path.join(htmlDir, "Styles", "head.temp")))
    headString = headString.replace("@@@_PUBLISH_DATE_@@@",
                                    str(datetime.now().date()))
    centerString = "".join(open(os.path.join(htmlDir, "Styles",
                                             "center.temp")))
    tailString = "".join(open(os.path.join(htmlDir, "Styles", "tail.temp")))

    #check for external scripting
    pos = 0
    pos = headString.find("@@@_EXTERNAL_TOOLS_REPORT_@@@") - 1
    if (param['LCOVCoverage']):
        #generateExternalLink
        externalScriptDirectory = os.path.join(scriptsDir, "ExternalScripts")
        #os.chdir(externalScriptDirectory)
        os.system(
            "python " +
            os.path.join(externalScriptDirectory, "LCOVCoveragePublish.py"))

        li = "<li><a href=\"../externalLCOVCoverage/index.html\">LCOV Coverage</a></li>"
        headString = headString[:pos] + li + headString[pos:]
        pos = pos + len(li)
        #os.chdir(scriptsDir)

    if (param['cppcheck']):
        #generateExternalLink
        externalScriptDirectory = os.path.join(scriptsDir, "ExternalScripts")
        #os.chdir(externalScriptDirectory)
        os.system("python " +
                  os.path.join(externalScriptDirectory, "cppcheckPublish.py"))
        li = "<li><a href=\"../externalcppcheck/index.html\">Static Analysis</a></li>"
        headString = headString[:pos] + li + headString[pos:]
        pos = pos + len(li)
        #os.chdir(scriptsDir)

    if (param['cccc']):
        #generateExternalLink
        externalScriptDirectory = os.path.join(scriptsDir, "ExternalScripts")
        #os.chdir(externalScriptDirectory)
        os.system("python " +
                  os.path.join(externalScriptDirectory, "ccccPublish.py"))
        li = "<li><a href=\"../externalcccc/index.html\">Code Complexity</a></li>"
        headString = headString[:pos] + li + headString[pos:]
        pos = pos + len(li)
        #os.chdir(scriptsDir)

    #remove placeholder for external scripting
    headString = headString.replace("@@@_EXTERNAL_TOOLS_REPORT_@@@", "")

    success = True
    for xmlFile in xmlList:
        try:
            filename = os.path.splitext(xmlFile)[0]
            print "Formatting in HTML " + filename
            #with lxml parse the file
            f = open(xmlDir + "/" + xmlFile, 'r')
            xml = fromstring(str(f.read()))

            #with lxml create html
            searchF = filename + ".xslt"
            absPathXslt = ""
            for top, dirs, files in os.walk('./'):
                for nm in files:
                    if (nm == searchF):
                        absPathXslt = os.path.join(top, nm)

            fileXslt = open(absPathXslt, 'r')
            #print xsltH + headString + centerString + str(fileXslt.read()) + tailString + xsltT
            xsl = fromstring(xsltH + headString + ruleLinksString +
                             centerString + str(fileXslt.read()) + tailString +
                             xsltT)
            style = XSLT(xsl)
            result = style.apply(xml)

            #print htmlDir + filename + ".html"
            html = open(os.path.join(htmlDir, filename + ".html"), 'w')
            print >> html, style.tostring(result)
        except Exception, e:
            success = False
            print "!!!!!!Bad Formatted XML on ", filename, "!!!!!!!"
            print e
            os.chdir(scriptsDir)
Пример #37
0
def run(param):
   #read xml file
   scriptsDir = os.getcwd()
   os.chdir(medPath.medQADir)
   baseDir = os.getcwd()
   qaResultsDir = os.path.join(baseDir,"QAResults")
   xmlDir = os.path.join(baseDir,"QAResults","xml")

   if not os.path.exists(xmlDir):
     print "Xml Directory not present: ", xmlDir
     sys.exit(1)
    
   htmlDir = os.path.join(baseDir,"QAResults","html")

   if not os.path.exists(htmlDir):
     os.makedirs(htmlDir)
   if not os.path.exists(os.path.join(htmlDir,"Styles")):  
     os.makedirs(os.path.join(htmlDir,"Styles"))

   if(os.path.exists(os.path.join(htmlDir,"Styles"))):
     origDir = os.path.join(scriptsDir, "Styles")
     destDir = os.path.join(htmlDir, "Styles")
     files = os.listdir(origDir)
     for item in files:
        if (item != 'CVS' and item != 'SVN' and item != '.cvs' and item != '.svn'):
            shutil.copyfile(os.path.join(origDir,item), os.path.join(destDir, item))
              
   xmlList=os.listdir(xmlDir)
   htmlList=[file.replace(".xml", ".html") for file in os.listdir(xmlDir)]

   ruleLinksString = ""
   for link in htmlList:
     if(link != "index.html" and link != "Styles"):
       ruleLinksString = ruleLinksString + "<li><a href=\"" + link + "\">" + link[:link.find(".")] + "</a></li>\n"

   xsltH = """<xsl:stylesheet version="2.0"
           xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output indent="yes"/>
   <xsl:template name="break">
           <xsl:param name="text"/>
           <xsl:choose>
                   <xsl:when test="contains($text, '&#xa;')">
                           <xsl:value-of select="substring-before($text, '&#xa;')"/>
                           <br/>
                           <xsl:call-template name="break">
                                   <xsl:with-param name="text" select="substring-after($text,'&#xa;')"/>
                           </xsl:call-template>
                   </xsl:when>
                   <xsl:otherwise>
                   <xsl:value-of select="$text"/>
                   </xsl:otherwise>
           </xsl:choose>
   </xsl:template>
   <xsl:template match="/">"""

   xsltT = """
   </xsl:template>
   </xsl:stylesheet>
   """

   headString = "".join(open(os.path.join(htmlDir,"Styles" ,"head.temp")))
   headString = headString.replace("@@@_PUBLISH_DATE_@@@", str( datetime.now().date()))
   centerString = "".join(open(os.path.join(htmlDir,"Styles","center.temp")))
   tailString = "".join(open(os.path.join(htmlDir, "Styles","tail.temp")))
   
   #check for external scripting
   pos = 0
   pos = headString.find("@@@_EXTERNAL_TOOLS_REPORT_@@@")-1
   if(param['LCOVCoverage']):
      #generateExternalLink
      externalScriptDirectory = os.path.join(scriptsDir, "ExternalScripts")
      #os.chdir(externalScriptDirectory)
      os.system("python " + os.path.join(externalScriptDirectory, "LCOVCoveragePublish.py"))
      
      li = "<li><a href=\"../externalLCOVCoverage/index.html\">LCOV Coverage</a></li>";
      headString = headString[:pos] + li + headString[pos:]
      pos = pos + len(li)
      #os.chdir(scriptsDir)

   if(param['cppcheck']):
      #generateExternalLink
      externalScriptDirectory = os.path.join(scriptsDir,"ExternalScripts")
      #os.chdir(externalScriptDirectory)
      os.system("python " + os.path.join(externalScriptDirectory,"cppcheckPublish.py"))
      li = "<li><a href=\"../externalcppcheck/index.html\">Static Analysis</a></li>"
      headString = headString[:pos] + li + headString[pos:]
      pos = pos + len(li)
      #os.chdir(scriptsDir)
   
   if(param['cccc']):
      #generateExternalLink
      externalScriptDirectory = os.path.join(scriptsDir,"ExternalScripts")
      #os.chdir(externalScriptDirectory)
      os.system("python " + os.path.join(externalScriptDirectory, "ccccPublish.py"))
      li = "<li><a href=\"../externalcccc/index.html\">Code Complexity</a></li>"
      headString = headString[:pos] + li + headString[pos:]
      pos = pos + len(li)
      #os.chdir(scriptsDir)

   #remove placeholder for external scripting
   headString = headString.replace("@@@_EXTERNAL_TOOLS_REPORT_@@@", "")


   success = True
   for xmlFile in xmlList:
     try:
       filename = os.path.splitext(xmlFile)[0]
       print "Formatting in HTML " + filename
       #with lxml parse the file
       f = open(xmlDir + "/" + xmlFile,'r')
       xml = fromstring(str(f.read()))

       #with lxml create html
       searchF = filename  + ".xslt"
       absPathXslt = ""
       for top, dirs, files in os.walk('./'):
         for nm in files:
           if(nm == searchF):
             absPathXslt = os.path.join(top, nm)

       fileXslt = open(absPathXslt, 'r') 
       #print xsltH + headString + centerString + str(fileXslt.read()) + tailString + xsltT
       xsl=  fromstring(xsltH + headString + ruleLinksString + centerString + str(fileXslt.read()) + tailString + xsltT) 
       style = XSLT(xsl)
       result = style.apply(xml)
           
       #print htmlDir + filename + ".html"
       html = open(os.path.join(htmlDir, filename + ".html"), 'w')
       print >> html , style.tostring(result)
     except Exception, e:
       success = False
       print "!!!!!!Bad Formatted XML on ", filename, "!!!!!!!"
       print e
       os.chdir(scriptsDir)
Пример #38
0
def html_building():
    dom = parse('xml_gen_update.xml')
    xslt = parse('components/style.xslt')
    transform = XSLT(xslt)
    newdom = transform(dom)
    return newdom