Ejemplo n.º 1
0
 def get_extensions(self, extclass, **kwargs):
     # loads xslt extension classes
     extensions = {}
     extf_obj = extclass(self._basepath, self._username, self._project, **kwargs)
     exts = (n for n in dir(extclass) if not(n.startswith('_')))
     extensions.update(ET.Extension(extf_obj,exts,ns=extf_obj.ens))
     return extensions
Ejemplo n.º 2
0
def generateStereotypeReportFromDoc(processedArchive):

    # Extension function helpers
    helperModule = ExtFunctions()
    functions = {'unitIsCPPFile':'unitIsCPPFile', 'funcHasStereotype':'funcHasStereotype'}
    extensions = et.Extension(helperModule, functions)
    namespaces = {'src':'http://www.srcML.org/srcML/src', 'cpp':'http://www.srcML.org/srcML/cpp'}
    evaluator = et.XPathEvaluator(processedArchive, namespaces=namespaces, extensions=extensions)

    # Listing all C++ files within the archive.
    cppFileListing = evaluator("/src:unit/src:unit[unitIsCPPFile(.)]")

    functionSigAndStereotype = []
    for unit in cppFileListing:
        elementEvaluator = et.XPathElementEvaluator(unit, namespaces=namespaces, extensions=extensions)
        listOfFunctions = elementEvaluator(".//src:function[funcHasStereotype(.)]")

        for func in listOfFunctions:
            stereotypes = _getStereotype(func)
            stereotypes = sorted(stereotypes)

            stereotypeKey = " ".join(stereotypes)
            funcEvaluator = et.XPathElementEvaluator(func, namespaces=namespaces, extensions=extensions)
            functionSigAndStereotype.append( (_getNormalizedFunctionSig(func), stereotypeKey, unit.get("filename")) )

    return functionSigAndStereotype
Ejemplo n.º 3
0
def get_stylesheet(stylesheet_filename=None, locale_fn=None, doxy_db=None):
    """Get a XSLT stylesheet.

    If stylesheet_filename is not specified, the default sheet will be returned.

    If a file is given, it will be loaded with a special loader that exposes
    the default one under the "antidox:compound".

    Parameters
    ----------
    stylesheet_filename: XSL style sheet, or None to load the default value.
    locale_fn: A translation function mapping strings to strings. This will be
               to implement the "antidox:l" XPath function. If not given,
               the identity function will be used.
    """

    custom_extension = _XPathExtensions(locale_fn, doxy_db)

    ext = ET.Extension(custom_extension, ns="antidox")

    if stylesheet_filename:
        parser = ET.XMLParser()
        parser.resolvers.add(Resolver())
        xml_doc = ET.parse(stylesheet_filename, parser)
    else:
        xml_doc = ET.XML(_get_compound_xsl_text())

    return ET.XSLT(xml_doc, extensions=ext)
Ejemplo n.º 4
0
    def get_xsl(self, name, args):
        extclass = XSLExtensions
        exts = [n for n in dir(extclass) if not (n.startswith('_'))]
        extensions = ET.Extension(XSLExtensions(args=args),
                                  exts,
                                  ns="kolekti:migrate")

        return ET.XSLT(ET.parse(os.path.join(self._appdir, 'xsl', name)),
                       extensions=extensions)
Ejemplo n.º 5
0
 def __init__(self, docdb, sheetspec_el, xlpdocu, xlpelement):
     self.docdb = docdb
     self.sheetspec_el = sheetspec_el
     self.xlpdocu = xlpdocu
     self.xlpelement = xlpelement
     functions = ('href', 'sheetspec', 'numericunitsvalue', 'hrefxlink',
                  'xlpdoc', 'element')
     self.extensions = etree.Extension(
         self,
         functions,
         ns="http://limatix.org/processtrak/processinginstructions")
     pass
Ejemplo n.º 6
0
class XsltParser(docutils.parsers.Parser):
    """
    Parses XML input by XSLT and stores the result in the attribute
    `xslt_result` of the document. Works together with `XsltWriter`.
    """

    # TODO Accept additional XSLT sheets to create a transformation pipeline

    def __init__(self, xsltSource, extension=None):
        """
        :Parameters:

           xsltSource : file-like object
             The source containing the XSLT. This is an open file-like object.

             The XSLT program receives settings from Docutils as parameters.
             All settings whose value is a string, integer, float or boolean
             are transferred. This allows settings for the specific XSLT
             program. Please note that a string value may contain either ``'``
             or ``"`` but not both.

           extension : None | XPathExtension
             The extensions to use or None for no extensions.
        """
        self.xslt = None
        """
        :type: Return type of `etree.XSLT`\ ()

        The XSLT to use for parsing.
        """

        try:
            xsltDoc = etree.parse(xsltSource)
        except Exception, e:
            raise Exception("Error parsing XSLT: %s" % ( e, ))
        xsltSource.close()
        if extension is None:
            exts = None
        else:
            exts = etree.Extension(extension, ns=extension.namespace)
        self.xslt = etree.XSLT(xsltDoc, extensions=exts)
Ejemplo n.º 7
0
    def gettopic_mediawiki(self, _, *args):
        logger.debug('gettopic mediawiki')
        url = args[0]
        relspec = args[1]
        logger.debug('url %s', url)
        try:
            page = urllib2.urlopen(url)
            source = ET.HTML(page.read())
            xslpath = os.path.join(self._appdir, 'adapters', 'xsl',
                                   'mediawiki.xsl')
            xsldoc = ET.parse(xslpath)
            exts = [n for n in dir(self.__class__) if not (n.startswith('_'))]
            xsl = ET.XSLT(xsldoc,
                          extensions=ET.Extension(
                              self,
                              exts,
                              ns="kolekti:extensions:functions:publication"))
            res = xsl(source)
            try:
                wmfilter = relspec.split(':')[2]
            except IndexError:
                wmfilter = None
            if wmfilter is not None:
                try:
                    s = self.get_xsl(
                        wmfilter,
                        xsldir="/kolekti/import-stylesheets/mediawiki")
                    res = s(res)
                except:
                    logger.exception('could not apply mediawiki filter')
                    self.log_xsl(s.error_log)
        except:
            logger.exception('mediawiki failed')

        for img in res.xpath('//img'):
            newsrc = self.image_mediawiki(None, img.get('src'), url)
            img.set('src', newsrc)
        return res.getroot()
Ejemplo n.º 8
0
    def getContent(self):
        if self._caller != "databrowse":
            return None
        else:
            if self._content_mode == "full" and self._style_mode in [
                    'old_log_view', 'old_tabular_view'
            ]:
                # Contents of File
                #f = open(self._fullpath)
                #xmlroot = etree.XML(f.read())
                p = etree.XMLParser(huge_tree=True)
                xmlroot = etree.parse(self._fullpath, parser=p).getroot()
                # Resolve URL to Files Directory
                try:
                    reldest = xmlroot.xpath(
                        'dc:summary/dc:reldest',
                        namespaces={'dc':
                                    'http://limatix.org/datacollect'})[0].text
                    reldesturl = self.getURL(
                        os.path.abspath(
                            os.path.join(os.path.dirname(self._relpath),
                                         reldest)))
                    xmlroot.set('reldesturl', reldesturl)
                except:
                    xmlroot.set('reldesturl', '')
                # Resolve URLs for Config Files
                configlist = xmlroot.xpath(
                    'dc:configstr',
                    namespaces={'dc': 'http://limatix.org/datacollect'})
                for item in configlist:
                    try:
                        fname = item.get('fname')
                        fnames = item.get('fnames')
                        if fname:
                            path = os.path.realpath(fname)
                            if path.startswith(
                                    os.path.normpath(
                                        self._web_support.dataroot)
                            ) and os.access(path,
                                            os.R_OK) and os.path.exists(path):
                                relpath = path.replace(
                                    self._web_support.dataroot, '')
                                url = self.getURL(relpath)
                                item.set('url', url)
                        elif fnames:
                            if fnames[0] == '[' and fnames[-1] == "]":
                                urls = []
                                fnamelist = fnames[1:-1].split(',')
                                for fname in fnamelist:
                                    fname = fname.replace("'", "").replace(
                                        '"', "").strip()
                                    path = os.path.realpath(fname)
                                    if path.startswith(
                                            os.path.normpath(
                                                self._web_support.dataroot)
                                    ) and os.access(
                                            path,
                                            os.R_OK) and os.path.exists(path):
                                        relpath = path.replace(
                                            self._web_support.dataroot, '')
                                        url = self.getURL(relpath)
                                        urls.append(url)
                                    else:
                                        urls.append("")
                                item.set('urls', repr(urls))
                    except:
                        pass
                # Resolve URLs for Specimen Database
                specimenlist = xmlroot.xpath(
                    '//dc:specimen',
                    namespaces={
                        "dc": 'http://limatix.org/datacollect',
                        "dcv": 'http://limatix.org/dcvalue'
                    })
                for item in specimenlist:
                    if item.text:
                        relpath = '/specimens/' + item.text + '.sdb'
                        if os.access(
                                os.path.abspath(self._web_support.dataroot +
                                                "/" + relpath), os.R_OK
                        ) and os.path.exists(
                                os.path.abspath(self._web_support.dataroot +
                                                "/" + relpath)):
                            url = self.getURL(relpath)
                            item.set('url', url)
                # Resolve URLs for Transducer Database
                transducerlist = xmlroot.xpath(
                    '//dc:xducer',
                    namespaces={
                        "dc": 'http://limatix.org/datacollect',
                        "dcv": 'http://limatix.org/dcvalue'
                    })
                for item in transducerlist:
                    if item.text:
                        relpath = '/transducers/' + item.text + '.tdb'
                        if os.access(
                                os.path.abspath(self._web_support.dataroot +
                                                "/" + relpath), os.R_OK
                        ) and os.path.exists(
                                os.path.abspath(self._web_support.dataroot +
                                                "/" + relpath)):
                            url = self.getURL(relpath)
                            item.set('url', url)
                return xmlroot
            elif self._content_mode == "full" and self._style_mode != 'limatix_custom_view':
                # Contents of File
                #f = open(self._fullpath)
                #xmlroot = etree.XML(f.read())
                p = etree.XMLParser(huge_tree=True)
                xmlroot = etree.parse(self._fullpath, parser=p).getroot()

                # TODO:  GET RID OF ALL OF THIS BELOW
                # Resolve URLs for Config Files
                configlist = xmlroot.xpath(
                    'dc:config/dc:configfile',
                    namespaces={'dc': 'http://limatix.org/datacollect'})
                for item in configlist:
                    try:
                        xlink = item.get('{http://www.w3.org/1999/xlink}href')
                        if xlink:
                            path = os.path.realpath(fname)
                            if path.startswith(
                                    os.path.normpath(
                                        self._web_support.dataroot)
                            ) and os.access(path,
                                            os.R_OK) and os.path.exists(path):
                                relpath = path.replace(
                                    self._web_support.dataroot, '')
                                url = self.getURL(relpath)
                                item.set('url', url)
                    except:
                        pass
                # Resolve URLs for Specimen Database
                specimenlist = xmlroot.xpath(
                    '//dc:specimen',
                    namespaces={
                        "dc": 'http://limatix.org/datacollect',
                        "dcv": 'http://limatix.org/dcvalue'
                    })
                for item in specimenlist:
                    if item.text:
                        relpath = '/specimens/' + item.text + '.sdb'
                        if os.access(
                                os.path.abspath(self._web_support.dataroot +
                                                "/" + relpath), os.R_OK
                        ) and os.path.exists(
                                os.path.abspath(self._web_support.dataroot +
                                                "/" + relpath)):
                            url = self.getURL(relpath)
                            item.set('url', url)
                # Resolve URLs for Transducer Database
                transducerlist = xmlroot.xpath(
                    '//dc:xducer',
                    namespaces={
                        "dc": 'http://limatix.org/datacollect',
                        "dcv": 'http://limatix.org/dcvalue'
                    })
                for item in transducerlist:
                    if item.text:
                        relpath = '/transducers/' + item.text + '.tdb'
                        if os.access(
                                os.path.abspath(self._web_support.dataroot +
                                                "/" + relpath), os.R_OK
                        ) and os.path.exists(
                                os.path.abspath(self._web_support.dataroot +
                                                "/" + relpath)):
                            url = self.getURL(relpath)
                            item.set('url', url)
                return xmlroot
            elif self._content_mode == "full" and self._style_mode == "limatix_custom_view":
                self._namespace_local = "dt"
                self._namespace_uri = "http://limatix.org/databrowse/datatable"
                if not 'custom_view' in self._web_support.req.form:
                    raise self.RendererException(
                        "Custom View Selection Required")
                    pass
                xml = etree.parse(
                    os.path.join(
                        os.path.dirname(self._fullpath),
                        self._web_support.req.form['custom_view'].value))
                namespaces = " ".join([
                    "xmlns:" + str(item) + '="' + str(value) + '"'
                    for item, value in xml.getroot().nsmap.iteritems()
                ])
                root = xml.getroot()
                root.set('filenamematch', os.path.basename(
                    self._fullpath))  # Force it to only operate on this file
                ext_module = db_data_table.MyExt(
                    os.path.join(
                        os.path.dirname(self._fullpath),
                        self._web_support.req.form['custom_view'].value))
                extensions = etree.Extension(
                    ext_module, ('data', 'xmlassert'),
                    ns='http://limatix.org/databrowse/datatable/functions')
                root = xml.xslt(etree.XML(
                    db_data_table._table_transform %
                    (namespaces, self._web_support.siteurl,
                     self.getURL(
                         os.path.join(
                             os.path.dirname(self._relpath),
                             self._web_support.req.form['custom_view'].value)),
                     self._web_support.req.form['custom_view'].value)),
                                extensions=extensions).getroot()
                root.set('custom_view',
                         self._web_support.req.form['custom_view'].value)
                return root
            elif self._content_mode == "raw":
                if 'filetype' in self._web_support.req.form:
                    self._namespace_local = "dt"
                    self._namespace_uri = "http://limatix.org/databrowse/datatable"
                    if not 'custom_view' in self._web_support.req.form:
                        raise self.RendererException(
                            "Custom View Selection Required")
                        pass
                    xml = etree.parse(
                        os.path.join(
                            os.path.dirname(self._fullpath),
                            self._web_support.req.form['custom_view'].value))
                    namespaces = " ".join([
                        "xmlns:" + str(item) + '="' + str(value) + '"'
                        for item, value in xml.getroot().nsmap.iteritems()
                    ])
                    root = xml.getroot()
                    root.set('filenamematch', os.path.basename(self._fullpath)
                             )  # Force it to only operate on this file
                    ext_module = db_data_table.MyExt(
                        os.path.join(
                            os.path.dirname(self._fullpath),
                            self._web_support.req.form['custom_view'].value))
                    extensions = etree.Extension(
                        ext_module, ('data', 'xmlassert'),
                        ns='http://limatix.org/databrowse/datatable/functions')
                    base = xml.xslt(etree.XML(
                        db_data_table._table_transform %
                        (namespaces, self._web_support.siteurl,
                         self.getURL(
                             os.path.join(
                                 os.path.dirname(self._relpath), self.
                                 _web_support.req.form['custom_view'].value)),
                         self._web_support.req.form['custom_view'].value)),
                                    extensions=extensions)
                    filename = str(base.xpath('//@title')[0])
                    if self._web_support.req.form['filetype'].value == 'ods':
                        result = etree.tostring(
                            base.xslt(etree.XML(db_data_table._ods_transform)))

                        # File Creation
                        f = tempfile.TemporaryFile()
                        if sys.version_info[0] <= 2 and sys.version_info[1] < 7:
                            zipfile_compression = zipfile.ZIP_STORED
                            pass
                        else:
                            zipfile_compression = zipfile.ZIP_DEFLATED
                            pass
                        zf = zipfile.ZipFile(f, "w", zipfile_compression)
                        if sys.version_info[0] <= 2 and sys.version_info[1] < 7:
                            zf.writestr(
                                "mimetype",
                                "application/vnd.oasis.opendocument.spreadsheet"
                            )
                            pass
                        else:
                            # on Python 2.7 can explicitly specify lack of compression for this file alone
                            zf.writestr(
                                "mimetype",
                                "application/vnd.oasis.opendocument.spreadsheet",
                                compress_type=zipfile.ZIP_STORED)
                            pass
                        zf.writestr(
                            "META-INF/manifest.xml",
                            r"""<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0">
    <manifest:file-entry manifest:media-type="application/vnd.oasis.opendocument.spreadsheet" manifest:full-path="/"/>
    <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="content.xml"/>
    <!-- manifest:file-entry manifest:media-type="text/xml" manifest:full-path="styles.xml"/-->
    <!-- manifest:file-entry manifest:media-type="text/xml" manifest:full-path="meta.xml"/-->
    <!-- manifest:file-entry manifest:media-type="text/xml" manifest:full-path="settings.xml"/-->
</manifest:manifest>
""")

                        zf.writestr("content.xml", result)
                        zf.close()

                        # File Streaming
                        self._web_support.req.response_headers[
                            'Content-Type'] = 'application/vnd.oasis.opendocument.spreadsheet'
                        self._web_support.req.response_headers[
                            'Content-Length'] = str(f.tell())
                        f.seek(0, 0)
                        self._web_support.req.response_headers[
                            'Content-Disposition'] = "attachment; filename=" + filename + ".ods"
                        self._web_support.req.start_response(
                            self._web_support.req.status,
                            self._web_support.req.response_headers.items())
                        self._web_support.req.output_done = True
                        if 'wsgi.file_wrapper' in self._web_support.req.environ:
                            return self._web_support.req.environ[
                                'wsgi.file_wrapper'](f, 1024)
                        else:
                            return iter(lambda: f.read(1024), '')
                        pass
                    elif self._web_support.req.form['filetype'].value == 'csv':
                        # File Creation
                        f = tempfile.TemporaryFile()
                        coldef = base.xpath(
                            'dt:header/dt:coldef',
                            namespaces={
                                'dt': 'http://limatix.org/databrowse/datatable'
                            })
                        f.write(",".join([x.text for x in coldef]) + '\n')
                        for row in base.xpath(
                                'dt:row',
                                namespaces={
                                    'dt':
                                    'http://limatix.org/databrowse/datatable'
                                }):
                            datadef = row.xpath(
                                'dt:data/.',
                                namespaces={
                                    'dt':
                                    'http://limatix.org/databrowse/datatable'
                                })
                            f.write(",".join([
                                x.text if x.text is not None else ''
                                for x in datadef
                            ]) + '\n')
                            pass
                        f.flush()
                        f.seek(0, 2)

                        # File Streaming
                        self._web_support.req.response_headers[
                            'Content-Type'] = 'text/csv'
                        self._web_support.req.response_headers[
                            'Content-Length'] = str(f.tell())
                        f.seek(0, 0)
                        self._web_support.req.response_headers[
                            'Content-Disposition'] = "attachment; filename=" + filename + ".csv"
                        self._web_support.req.start_response(
                            self._web_support.req.status,
                            self._web_support.req.response_headers.items())
                        self._web_support.req.output_done = True
                        if 'wsgi.file_wrapper' in self._web_support.req.environ:
                            return self._web_support.req.environ[
                                'wsgi.file_wrapper'](f, 1024)
                        else:
                            return iter(lambda: f.read(1024), '')
                        pass
                    else:
                        raise self.RendererException('Invalid File Type')
                    pass
                else:
                    size = os.path.getsize(self._fullpath)
                    try:
                        magicstore = magic.open(magic.MAGIC_MIME)
                        magicstore.load()
                        contenttype = magicstore.file(
                            os.path.realpath(self._fullpath)
                        )  # real path to resolve symbolic links outside of dataroot
                    except AttributeError:
                        contenttype = magic.from_file(os.path.realpath(
                            self._fullpath),
                                                      mime=True)
                    if contenttype is None:
                        contenttype = "text/plain"
                    f = open(self._fullpath, "rb")
                    self._web_support.req.response_headers[
                        'Content-Type'] = contenttype
                    self._web_support.req.response_headers[
                        'Content-Length'] = str(size)
                    self._web_support.req.response_headers[
                        'Content-Disposition'] = "attachment; filename=" + os.path.basename(
                            self._fullpath)
                    self._web_support.req.start_response(
                        self._web_support.req.status,
                        self._web_support.req.response_headers.items())
                    self._web_support.req.output_done = True
                    if 'wsgi.file_wrapper' in self._web_support.req.environ:
                        return self._web_support.req.environ[
                            'wsgi.file_wrapper'](f, 1024)
                    else:
                        return iter(lambda: f.read(1024), '')
            else:
                raise self.RendererException("Invalid Content Mode")
            pass
Ejemplo n.º 9
0
 def _load_extensions(self):
     super(plugin, self)._load_extensions()
     extf_obj = EpubExtensionsFunctions()
     exts = (n for n in dir(EpubExtensionsFunctions)
             if not (n.startswith('_')))
     self.extensions.update(ET.Extension(extf_obj, exts, ns=extf_obj.ens))
Ejemplo n.º 10
0
    def getContent(self):
        if self._caller != "databrowse":
            return None
        else:
            if self._content_mode == "full":
                xml = etree.parse(self._fullpath)
                namespaces = " ".join([
                    "xmlns:" + str(item) + '="' + str(value) + '"'
                    for item, value in xml.getroot().nsmap.iteritems()
                ])
                ext_module = self.MyExt(self._fullpath)
                extensions = etree.Extension(
                    ext_module, ('data', 'xmlassert'),
                    ns=
                    'http://thermal.cnde.iastate.edu/databrowse/datatable/functions'
                )
                return xml.xslt(etree.XML(
                    self._table_transform %
                    (namespaces, self._web_support.siteurl,
                     self.getURL(
                         self._relpath), os.path.basename(self._fullpath))),
                                extensions=extensions).getroot()
            elif self._content_mode == "raw" and 'filetype' in self._web_support.req.form and self._web_support.req.form[
                    'filetype'].value == "ods":
                # File Generation
                xml = etree.parse(self._fullpath)
                namespaces = " ".join([
                    "xmlns:" + str(item) + '="' + str(value) + '"'
                    for item, value in xml.getroot().nsmap.iteritems()
                ])
                ext_module = self.MyExt(self._fullpath)
                extensions = etree.Extension(
                    ext_module, ('data', 'xmlassert'),
                    ns=
                    'http://thermal.cnde.iastate.edu/databrowse/datatable/functions'
                )
                base = xml.xslt(etree.XML(
                    self._table_transform %
                    (namespaces, self._web_support.siteurl,
                     self.getURL(
                         self._relpath), os.path.basename(self._fullpath))),
                                extensions=extensions)
                result = etree.tostring(
                    base.xslt(etree.XML(self._ods_transform)))
                filename = str(base.xpath('//@title')[0])

                # File Creation
                f = tempfile.TemporaryFile()
                if sys.version_info[0] <= 2 and sys.version_info[1] < 7:
                    zipfile_compression = zipfile.ZIP_STORED
                    pass
                else:
                    zipfile_compression = zipfile.ZIP_DEFLATED
                    pass
                zf = zipfile.ZipFile(f, "w", zipfile_compression)
                if sys.version_info[0] <= 2 and sys.version_info[1] < 7:
                    zf.writestr(
                        "mimetype",
                        "application/vnd.oasis.opendocument.spreadsheet")
                    pass
                else:
                    # on Python 2.7 can explicitly specify lack of compression for this file alone
                    zf.writestr(
                        "mimetype",
                        "application/vnd.oasis.opendocument.spreadsheet",
                        compress_type=zipfile.ZIP_STORED)
                    pass
                zf.writestr(
                    "META-INF/manifest.xml",
                    r"""<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0">
    <manifest:file-entry manifest:media-type="application/vnd.oasis.opendocument.spreadsheet" manifest:full-path="/"/>
    <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="content.xml"/>
    <!-- manifest:file-entry manifest:media-type="text/xml" manifest:full-path="styles.xml"/-->
    <!-- manifest:file-entry manifest:media-type="text/xml" manifest:full-path="meta.xml"/-->
    <!-- manifest:file-entry manifest:media-type="text/xml" manifest:full-path="settings.xml"/-->
</manifest:manifest>
""")

                zf.writestr("content.xml", result)
                zf.close()

                # File Streaming
                self._web_support.req.response_headers[
                    'Content-Type'] = 'application/vnd.oasis.opendocument.spreadsheet'
                self._web_support.req.response_headers['Content-Length'] = str(
                    f.tell())
                f.seek(0, 0)
                self._web_support.req.response_headers[
                    'Content-Disposition'] = "attachment; filename=" + filename + ".ods"
                self._web_support.req.start_response(
                    self._web_support.req.status,
                    self._web_support.req.response_headers.items())
                self._web_support.req.output_done = True
                if 'wsgi.file_wrapper' in self._web_support.req.environ:
                    return self._web_support.req.environ['wsgi.file_wrapper'](
                        f, 1024)
                else:
                    return iter(lambda: f.read(1024))
            elif self._content_mode == "raw" and 'filetype' in self._web_support.req.form and self._web_support.req.form[
                    'filetype'].value == "csv":
                # File Generation
                xml = etree.parse(self._fullpath)
                namespaces = " ".join([
                    "xmlns:" + str(item) + '="' + str(value) + '"'
                    for item, value in xml.getroot().nsmap.iteritems()
                ])
                ext_module = self.MyExt(self._fullpath)
                extensions = etree.Extension(
                    ext_module, ('data', 'xmlassert'),
                    ns=
                    'http://thermal.cnde.iastate.edu/databrowse/datatable/functions'
                )
                base = xml.xslt(etree.XML(
                    self._table_transform %
                    (namespaces, self._web_support.siteurl,
                     self.getURL(
                         self._relpath), os.path.basename(self._fullpath))),
                                extensions=extensions)

                # File Creation
                f = tempfile.TemporaryFile()
                filename = str(base.xpath('//@title')[0])
                coldef = base.xpath(
                    'dt:header/dt:coldef',
                    namespaces={
                        'dt':
                        'http://thermal.cnde.iastate.edu/databrowse/datatable'
                    })
                f.write(",".join([x.text for x in coldef]) + '\n')
                for row in base.xpath(
                        'dt:row',
                        namespaces=
                    {
                        'dt':
                        'http://thermal.cnde.iastate.edu/databrowse/datatable'
                    }):
                    datadef = row.xpath(
                        'dt:data/.',
                        namespaces={
                            'dt':
                            'http://thermal.cnde.iastate.edu/databrowse/datatable'
                        })
                    f.write(",".join([
                        x.text if x.text is not None else '' for x in datadef
                    ]) + '\n')
                    pass
                f.flush()
                f.seek(0, 2)

                # File Streaming
                self._web_support.req.response_headers[
                    'Content-Type'] = 'text/csv'
                self._web_support.req.response_headers['Content-Length'] = str(
                    f.tell())
                f.seek(0, 0)
                self._web_support.req.response_headers[
                    'Content-Disposition'] = "attachment; filename=" + filename + ".csv"
                self._web_support.req.start_response(
                    self._web_support.req.status,
                    self._web_support.req.response_headers.items())
                self._web_support.req.output_done = True
                if 'wsgi.file_wrapper' in self._web_support.req.environ:
                    return self._web_support.req.environ['wsgi.file_wrapper'](
                        f, 1024)
                else:
                    return iter(lambda: f.read(1024))
            else:
                raise self.RendererException("Invalid Content Mode")
        pass
Ejemplo n.º 11
0
def generateTestReport(processedArchive, reportFolder):

    # Extension function helpers
    # ns = et.FunctionNamespace(None)

    helperModule = ExtFunctions()
    functions = {'unitIsCPPFile':'unitIsCPPFile', 'funcHasStereotype':'funcHasStereotype'}
    extensions = et.Extension(helperModule, functions)
    namespaces = {'src':'http://www.srcML.org/srcML/src', 'cpp':'http://www.srcML.org/srcML/cpp'}
    evaluator = et.XPathEvaluator(processedArchive, namespaces=namespaces, extensions=extensions)


    # ns = et.FunctionNamespace(None)
    # ns['funcHasStereotype'] =  funcHasStereotype

    if os.path.exists(reportFolder):
        shutil.rmtree(reportFolder)
    os.mkdir(reportFolder)


    # The things to report about this archive.
    totalFileCount = 0
    CPPFileCount = 0
    classCount = 0 
    functionDeclCount = 0
    functionDefnCount = 0
    annotatedFunctionDeclCount = 0
    annotatedFunctionDefnCount = 0
    functionDeclStereotypeHistogram = dict()
    functionDefnStereotypeHistogram = dict()
    functionSigAndStereotype = []

    # Listing all C++ files within the archive.
    cppFileListing = evaluator("/src:unit/src:unit[unitIsCPPFile(.)]")
    CPPFileCount = len(cppFileListing)
    print >> sys.stderr, "CPP File Count: ", CPPFileCount
    sys.stdout.flush()

    # Getting the # of units within the archive.
    totalFileCount = int(evaluator("count(/src:unit/src:unit)"))
    print >> sys.stderr, "Total File Count: ", totalFileCount
    sys.stdout.flush()

    # Counting the # of structs + classes within all C++ files.
    classCount = int(evaluator("count(/src:unit/src:unit[unitIsCPPFile(.)]//src:class)"))
    structCount = int(evaluator("count(/src:unit/src:unit[unitIsCPPFile(.)]//src:struct)"))
    print >> sys.stderr, "Class Count: ", classCount
    print >> sys.stderr, "Struct Count: ", structCount
    sys.stdout.flush()

    # Counting function decl/def within files that CPP files.
    functionDeclCount = int(evaluator("count(/src:unit/src:unit[unitIsCPPFile(.)]//src:function_decl)"))
    print >> sys.stderr, "Function Declaration Count: ", functionDeclCount
    sys.stdout.flush()

    functionDefnCount = int(evaluator("count(/src:unit/src:unit[unitIsCPPFile(.)]//src:function)"))
    print >> sys.stderr, "Function Definition Count: ", functionDefnCount
    sys.stdout.flush()

    # # Counting annotated function decls & defs.
    print >> sys.stderr, "Attempting to count the # of annotated functions"
    sys.stdout.flush()

    collectedFunctionDefns = []
    functionSigAndStereotype = []
    for unit in cppFileListing:
        elementEvaluator = et.XPathElementEvaluator(unit, namespaces=namespaces, extensions=extensions)
        listOfFunctions = elementEvaluator(".//src:function[funcHasStereotype(.)]")
        collectedFunctionDefns += listOfFunctions 

        for func in listOfFunctions:
            stereotypes = _getStereotype(func)
            stereotypes = sorted(stereotypes)

            stereotypeKey = " ".join(stereotypes)
            funcEvaluator = et.XPathElementEvaluator(func, namespaces=namespaces, extensions=extensions)
            functionSigAndStereotype.append( (_getNormalizedFunctionSig(func), stereotypeKey, unit.get("filename")) )
            if stereotypeKey in functionDefnStereotypeHistogram:
                functionDefnStereotypeHistogram[stereotypeKey] += 1
            else:
                functionDefnStereotypeHistogram[stereotypeKey] = 1
    # annotatedFuncDecls = evaluator("/src:unit/src:unit[unitIsCPPFile(.)]//src:function_decl[funcHasStereotype(.)]") 
    # print "Annotated Function declarations: ", len(collectedFunctionsDecls)
    print >> sys.stderr, "# Of Annotated Function definitions: ", len(collectedFunctionDefns)


    temp = list(functionDefnStereotypeHistogram.items())
    temp = sorted(temp, key=lambda x: x[1])

    

    print >> sys.stderr, "\n".join([str(x) for x in temp])

    reportOutputPath = os.path.join(reportFolder, "functionStereotypeReport.txt")
    reportOutputStrm = open(reportOutputPath, "w")
    currentFile = ""
    for reportEntry in functionSigAndStereotype:
        if currentFile != reportEntry[2]:
            currentFile = reportEntry[2]
            reportOutputStrm.write("\n{0}\n".format(reportEntry[2]))
        reportOutputStrm.write( reportEntry[0])
        reportOutputStrm.write(", "  + reportEntry[1] + "\n")
    reportOutputStrm.close()