예제 #1
0
def _get_bugzilla_bug(bug_id):
    """Fetch bug ``bug_id``.

    :param int bug_id: The ID of a bug in the Bugzilla database.
    :return: A FRIGGIN UNDOCUMENTED python-bugzilla THING.
    :raises BugFetchError: If an error occurs while fetching the bug. For
        example, a network timeout occurs or the bug does not exist.

    """
    # Is bug ``bug_id`` in the cache?
    if bug_id in _bugzilla:
        LOGGER.debug('Bugzilla bug {0} found in cache.'.format(bug_id))
    else:
        LOGGER.info('Bugzilla bug {0} not in cache. Fetching.'.format(bug_id))
        # Make a network connection to the Bugzilla server.
        try:
            bz_conn = bugzilla.RHBugzilla()
            bz_conn.connect(BUGZILLA_URL)
        except (TypeError, ValueError):
            raise BugFetchError(
                'Could not connect to {0}'.format(BUGZILLA_URL))
        # Fetch the bug and place it in the cache.
        try:
            _bugzilla[bug_id] = bz_conn.getbugsimple(bug_id)
        except Fault as err:
            raise BugFetchError('Could not fetch bug. Error: {0}'.format(
                err.faultString))
        except ExpatError as err:
            raise BugFetchError('Could not interpret bug. Error: {0}'.format(
                ErrorString(err.code)))

    return _bugzilla[bug_id]
예제 #2
0
    def _render_xml(self, xml_document, **kwargs):
        # Prepare the xml object to be processed by jinja2
        self.log.debug('Rendering XML object')
        template_string = ""

        try:
            self.template_images = dict()
            self._prepare_document_tags(xml_document)
            xml_source = xml_document.toxml()
            xml_source = xml_source.encode('ascii', 'xmlcharrefreplace')
            jinja_template = self.environment.from_string(
                self._unescape_entities(xml_source.decode('utf-8'))
            )

            result = jinja_template.render(**kwargs)
            result = self._encode_escape_chars(result)

            final_xml = parseString(result.encode('ascii', 'xmlcharrefreplace'))
            if self.template_images:
                self.replace_images(final_xml)

            return final_xml
        except ExpatError as e:
            near = result.split('\n')[e.lineno -1][e.offset-200:e.offset+200]
            raise ExpatError('ExpatError "%s" at line %d, column %d\nNear of: "[...]%s[...]"' % \
                             (ErrorString(e.code), e.lineno, e.offset, near))
        except:
            self.log.error('Error rendering template:\n%s',
                           xml_document.toprettyxml(), exc_info=True)

            self.log.error('Unescaped template was:\n{0}'.format(template_string))
            raise
        finally:
            self.log.debug('Rendering xml object finished')
예제 #3
0
    def get_bug_data(self, bug_id):
        """Get data for a single bug_id"""
        bug_data = self._cache.get(str(bug_id))
        if not bug_data:
            bz_conn = self._get_connection()
            try:
                bug = bz_conn.getbug(bug_id,
                                     include_fields=self.include_fields)
                if bug is not None:
                    bug_clones_data = []
                    if self.follow_clones:
                        bug_clones_data = [
                            self.set_bug_data_fields(clone_bug,
                                                     base_data_only=True)
                            for clone_bug in self._get_clones([bug_id])
                        ]
                    bug_data = self.set_bug_data_fields(
                        bug, bugs_clones_data=bug_clones_data)

            except ErrorString as error:
                logger.warning(
                    'Could not interpret bug. Error: {0}'.format(error))
            except Fault as error:
                logger.warning('Could not fetch bug. Error: {0}'.format(
                    error.faultString))
            except ExpatError as error:
                logger.warning('Could not interpret bug. Error: {0}'.format(
                    ErrorString(error.code)))
        return bug_data
예제 #4
0
 def parse_result(self, url_handler):
     try:
         goodreads_dom = minidom.parse(url_handler)
         return goodreads_dom
     except ExpatError, e:
         logging.error("XML Error: %s line: %d offset: %d" %
                       (ErrorString(e.code), e.lineno, e.offset))
         return None
예제 #5
0
def processReport(xmlFile):
    global reportData, tagStack

    guid = os.path.splitext(os.path.basename(xmlFile))[0]

    cursor = get_db().cursor()
    executeQuery(cursor, 'SELECT guid FROM #PFX#reports WHERE guid = %s', guid)
    report = cursor.fetchone()

    if report != None:
        os.remove(xmlFile)
        return

    source = open(xmlFile, 'rb')
    reportData = {
        'status': '',
        'usefulness': 0,
        'warnings': {},
        'requests': [],
        'filters': [],
        'subscriptions': [],
        'extensions': [],
        'errors': [],
        'time': time()
    }
    tagStack = []

    parser = ParserCreate()
    parser.StartElementHandler = processElementStart
    parser.EndElementHandler = processElementEnd
    parser.CharacterDataHandler = processText
    try:
        parser.ParseFile(source)
    except ExpatError as error:
        reportData['warnings'][
            '!parsing'] = 'Parsing error in the report: %s at line %i column %i' % (
                ErrorString(error.code), error.lineno, error.offset)

    source.seek(0)
    reportData['knownIssues'] = knownIssuesParser.findMatches(source, 'en-US')
    source.close()

    if 'screenshot' in reportData and not reportData['screenshot'].startswith(
            'data:image/'):
        del reportData['screenshot']
    if 'email' in reportData and reportData['email'].find(
            ' at ') < 0 and reportData['email'].find('@') < 0:
        del reportData['email']

    validateData(reportData)
    saveReport(guid, reportData, True)
    os.remove(xmlFile)
예제 #6
0
파일: tmtheme.py 프로젝트: robeady/CSScheme
def load(text, path, out):
    """Load a tmTheme property list and write errors to an output panel.

    :param text:
        The text of the file to be parsed.
    :param path:
        The path of the file, for error output purposes.
    :param out:
        OutputPanel instance.

    :return:
        `None` if errored, the parsed data otherwise (mostly a dict).
    """
    dirname = os.path.dirname(path)
    out.set_path(dirname, file_regex)
    if text.startswith('<?xml version="1.0" encoding="UTF-8"?>'):
        text = text[38:]

    try:
        from xml.parsers.expat import ExpatError, ErrorString
    except ImportError:
        # TODO: provide a compat plist parser as dependency
        # xml.parsers.expat is not available on certain Linux dists, try to use plist_parser then.
        # See https://github.com/SublimeText/AAAPackageDev/issues/19
        # Let's just hope AAAPackageDev is installed
        try:
            import plist_parser
        except ImportError:
            out.write_line(
                "Unable to load xml.parsers.expat or plist_parser modules.\n"
                "Please report to the package author.")
            return
        else:
            out.write_line(
                "Unable to load plistlib, using plist_parser instead\n")

        try:
            data = plist_parser.parse_string(text)
        except plist_parser.PropertyListParseError as e:
            out.write_line(debug_base_2 % (path, str(e)))
        else:
            return data
    else:
        import plistlib
        try:
            # This will try `from xml.parsers.expat import ParserCreate`
            # but since it is already tried above it should succeed.
            return plistlib.readPlistFromBytes(text.encode('utf-8'))
        except ExpatError as e:
            out.write_line(debug_base %
                           (path, ErrorString(e.code), e.lineno, e.offset + 1))
예제 #7
0
    def parse(self, *args, **kwargs):
        # Note: I hate Plist and XML. And it doesn't help a bit that parsing
        # plist files is a REAL PITA.
        text = get_text(self.view)

        # Parsing will fail if `<?xml version="1.0" encoding="UTF-8"?>` encoding is in the first
        # line, so strip it.
        # XXX: Find a better way to fix this misbehaviour of xml stuff in Python
        #      (I mean, plistliv even "writes" that line)
        if text.startswith('<?xml version="1.0" encoding="UTF-8"?>'):
            text = text[38:]

        # See https://github.com/SublimeText/AAAPackageDev/issues/34
        if ST2 and isinstance(text, unicode):  # NOQA
            text = text.encode('utf-8')

        if use_plistlib:
            try:
                # This will try `from xml.parsers.expat import ParserCreate`
                # but since it is already tried above it should succeed.
                if ST2:
                    data = plistlib.readPlistFromString(text)
                else:
                    data = plistlib.readPlistFromBytes(text.encode('utf-8'))
            except ExpatError as e:
                self.output.write_line(
                    self.debug_base %
                    (self.file_path, ErrorString(e.code), e.lineno, e.offset))
            # except BaseException as e:
            #     # Whatever could happen here ...
            #     self.output.write_line(self.debug_base % (self.file_path, str(e), 0, 0))
            else:
                return data
        else:
            # falling back to plist_parser
            from xml.sax._exceptions import SAXReaderNotAvailable
            try:
                data = plist_parser.parse_string(text)
            except plist_parser.PropertyListParseError as e:
                self.output.write_line(self.debug_base %
                                       (self.file_path, str(e), 0, 0))
            except SAXReaderNotAvailable:
                # https://github.com/SublimeText/AAAPackageDev/issues/48
                self.output.write_line(
                    "Unable to parse Property List because of missing XML "
                    "parsers in your Python environment.\n"
                    "Please use Sublime Text 3 or reinstall Python 2.6 "
                    "on your system.")
            else:
                return data
예제 #8
0
    def parse(self, *args, **kwargs):
        # Note: I hate Plist and XML. And it doesn't help a bit that parsing
        # plist files is a REAL PITA.
        text = get_text(self.view)

        # Parsing will fail if `<?xml version="1.0" encoding="UTF-8"?>` encoding is in the first
        # line, so strip it.
        # XXX: Find a better way to fix this misbehaviour of xml stuff in Python
        #      (I mean, plistliv even "writes" that line)
        if text.startswith('<?xml version="1.0" encoding="UTF-8"?>'):
            text = text[38:]

        # See https://github.com/SublimeText/AAAPackageDev/issues/34
        if ST2 and isinstance(text, unicode):
            text = text.encode('utf-8')

        try:
            from xml.parsers.expat import ExpatError, ErrorString
        except ImportError:
            # xml.parsers.expat is not available on certain Linux dists, use plist_parser then.
            # See https://github.com/SublimeText/AAAPackageDev/issues/19
            import plist_parser
            print("[AAAPackageDev] Using plist_parser")

            try:
                data = plist_parser.parse_string(text)
            except plist_parser.PropertyListParseError as e:
                self.output.write_line(self.debug_base %
                                       (self.file_path, str(e), 0, 0))
            else:
                return data
        else:
            try:
                # This will try `from xml.parsers.expat import ParserCreate`
                # but since it is already tried above it should succeed.
                if ST2:
                    data = plistlib.readPlistFromString(text)
                else:
                    data = plistlib.readPlistFromBytes(text.encode('utf-8'))
            except ExpatError as e:
                self.output.write_line(
                    self.debug_base %
                    (self.file_path, ErrorString(e.code), e.lineno, e.offset))
            except BaseException as e:
                # Whatever could happen here ...
                self.output.write_line(self.debug_base %
                                       (self.file_path, str(e), 0, 0))
            else:
                return data
예제 #9
0
def _get_bugzilla_bug(bug_id):
    """Fetch bug ``bug_id``.

    :param int bug_id: The ID of a bug in the Bugzilla database.
    :return: A FRIGGIN UNDOCUMENTED python-bugzilla THING.
    :raises BugFetchError: If an error occurs while fetching the bug. For
        example, a network timeout occurs or the bug does not exist.

    """
    # Is bug ``bug_id`` in the cache?
    if bug_id in _bugzilla:
        LOGGER.debug('Bugzilla bug {0} found in cache.'.format(bug_id))
    else:
        LOGGER.info('Bugzilla bug {0} not in cache. Fetching.'.format(bug_id))
        # Make a network connection to the Bugzilla server.
        bz_credentials = {}
        if setting_is_set('bugzilla'):
            bz_credentials = settings.bugzilla.get_credentials()
        try:
            bz_conn = bugzilla.RHBugzilla(url=BUGZILLA_URL, **bz_credentials)
        except (TypeError, ValueError):  # pragma: no cover
            raise BugFetchError(
                'Could not connect to {0}'.format(BUGZILLA_URL)
            )
        # Fetch the bug and place it in the cache.
        try:
            _bugzilla[bug_id] = bz_conn.getbug(
                bug_id,
                include_fields=['id', 'status', 'whiteboard', 'flags']
            )
            if not bz_credentials:
                raise BZUnauthenticatedCall(
                    _bugzilla[bug_id],
                    'Unauthenticated call made to BZ API, no flags data will '
                    'be available'
                )
        except Fault as err:
            raise BugFetchError(
                'Could not fetch bug. Error: {0}'.format(err.faultString)
            )
        except ExpatError as err:
            raise BugFetchError(
                'Could not interpret bug. Error: {0}'
                .format(ErrorString(err.code))
            )

    return _bugzilla[bug_id]
예제 #10
0
    def get_bug_data(self, bug_id, include_fields=None, use_cache=True):
        """Get data for a single bug_id"""
        if not bug_id:
            return

        if self.root_bug_id is None:
            self.root_bug_id = bug_id

        self.processed_bugs.append(bug_id)

        include_fields = include_fields or self.include_fields[:]
        bug_data = self._cache.get(str(bug_id))
        if not bug_data or not use_cache:
            bz_conn = self._get_connection()
            try:
                bug = bz_conn.getbug(bug_id, include_fields=include_fields)

                if self.follow_duplicates and getattr(
                        bug, 'resolution') == 'DUPLICATE':  # noqa
                    # in case of a DUPLICATE, call again taking the dupes
                    # but only if self.follow_duplicates is True
                    inc_fields = include_fields[:]  # avoid shadowing
                    if DUPLICATES_FIELD not in inc_fields:
                        inc_fields.append(DUPLICATES_FIELD)
                    bug = bz_conn.getbug(bug_id, include_fields=inc_fields)

                if bug is not None:
                    bug_clones_data = []
                    if self.follow_clones:
                        bug_clones_data = [
                            self.set_bug_data_fields(clone_bug,
                                                     base_data_only=True)
                            for clone_bug in self._get_clones([bug_id])
                        ]
                    bug_data = self.set_bug_data_fields(
                        bug, bugs_clones_data=bug_clones_data)

            except ErrorString as error:
                logger.warning(
                    'Could not interpret bug. Error: {0}'.format(error))
            except Fault as error:
                logger.warning('Could not fetch bug. Error: {0}'.format(
                    error.faultString))
            except ExpatError as error:
                logger.warning('Could not interpret bug. Error: {0}'.format(
                    ErrorString(error.code)))
        return bug_data
예제 #11
0
 def check(self):
     """Check the syntax of the python code."""
     # Reconcile the text and Expat checker text requriements.
     if self.text == '':
         return
     parser = ElementTree.XMLParser()
     self.handle_namespaces(parser)
     parser.entity.update(entitydefs)
     offset = 0
     # The expat parser seems to be assuming ascii even when
     # XMLParser(encoding='utf-8') is used above.
     text = self.text.encode('utf-8').decode('ascii', 'ignore')
     if text.find('<!DOCTYPE') == -1:
         # Expat requires a doctype to honour parser.entity.
         offset = 1
         match = self.xml_decl_pattern.search(text)
         if match is None:
             text = self.xhtml_doctype + '\n' + text
         else:
             start, end = match.span(0)
             text = text[:start] + self.xhtml_doctype + '\n' + text[end:]
     elif text.find('<!DOCTYPE html>') != -1:
         text = text.replace('<!DOCTYPE html>', self.xhtml_doctype)
     try:
         ElementTree.parse(StringIO(text), parser)
     except (ExpatError, ParseError) as error:
         if hasattr(error, 'code'):
             error_message = ErrorString(error.code)
             if hasattr(error, 'position') and error.position:
                 error_lineno, error_charno = error.position
                 error_lineno = error_lineno - offset
             elif error.lineno:
                 # Python 2.6-
                 error_lineno = error.lineno - offset
             else:
                 error_lineno = 0
         else:
             error_message, location = str(error).rsplit(':')
             error_lineno = int(location.split(',')[0].split()[1]) - offset
         self.message(error_lineno, error_message, icon='error')
     self.check_text()
     self.check_windows_endlines()
예제 #12
0
 def _context(self):
     self.p = ParserCreate(namespace_separator='}')
     self.p.StartElementHandler = self._start_element
     self.p.EndElementHandler = self._end_element
     self.p.CharacterDataHandler = self._char_data
     self._ancestors = []
     self._root = None
     try:
         yield
     except ExpatError as err:
         e = ExpatError(ErrorString(err.code))
         e.lineno = err.lineno
         e.offset = err.offset
         e.filename = self.filename
         raise e
     except ValueError as err:
         err.lineno = self.p.CurrentLineNumber
         err.offset = self.p.CurrentColumnNumber
         err.filename = self.filename
         raise err
     except self.Exit:
         pass
예제 #13
0
def processReport(xmlFile):
    global reportData, tagStack

    guid = os.path.splitext(os.path.basename(xmlFile))[0]

    cursor = get_db().cursor()
    executeQuery(cursor, '''SELECT guid FROM #PFX#reports WHERE guid = %s''',
                 (guid))
    report = cursor.fetchone()

    if report != None:
        os.remove(xmlFile)
        return

    source = open(xmlFile, 'rb')
    reportData = {
        'status': '',
        'usefulness': 0,
        'warnings': {},
        'requests': [],
        'filters': [],
        'subscriptions': [],
        'extensions': [],
        'errors': [],
        'time': time()
    }
    tagStack = []

    parser = ParserCreate()
    parser.StartElementHandler = processElementStart
    parser.EndElementHandler = processElementEnd
    parser.CharacterDataHandler = processText
    try:
        parser.ParseFile(source)
    except ExpatError, error:
        reportData['warnings'][
            '!parsing'] = 'Parsing error in the report: %s at line %i column %i' % (
                ErrorString(error.code), error.lineno, error.offset)
예제 #14
0
    def chargerXML(self, fichier):
        """ Vide le dictionnaire elements et le remplit avec les données du
            fichier XML """
        logI("Chargement du fichier " + str(fichier) + "...")
        self._elements.clear()
        try:
            arbre = parse(source=fichier)
        except FileNotFoundError as e:
            raise GrafcetError("Fichier : " + str(fichier) + " introuvable !")
        except ParseError as e:
            raise GrafcetError(
                "Erreur lors du chargement du fichier XML : " + fichier +
                "\nMessage du module XML : " + ErrorString(e.code))
        # TODO : vérifier intégrité du fichier xml
        etapes = arbre.find('Etapes')
        transitions = arbre.find('Transitions')
        liaisons = arbre.find('Liaisons')

        for etape_xml in etapes.findall('Etape'):
            etape = Etape()
            etape.deXml(etape_xml)
            logD(etape)
            self._elements['E' + str(etape.numero)] = etape

        for transition_xml in transitions.findall('Transition'):
            transition = Transition()
            transition.deXml(transition_xml)
            logD(transition)
            self._elements['T' + str(transition.numero)] = transition

        for liaison_xml in liaisons.findall('Liaison'):
            liaison = Liaison()
            liaison.deXml(liaison_xml, self._elements.values())
            logD(liaison)
            self._elements['L' + str(liaison.numero)] = liaison

        logI(str(len(self._elements)) + " éléments trouvés !")
예제 #15
0
            try:
                data = plist_parser.parse_string(text)
            except plist_parser.PropertyListParseError, e:
                self.output.write_line(self.debug_base % (self.file_path, str(e), 0, 0))
            else:
                return data
        else:
            try:
                # This will try `from xml.parsers.expat import ParserCreate`
                # but since it is already tried above it should succeed.
                data = plistlib.readPlistFromString(text)
            except ExpatError, e:
                self.output.write_line(self.debug_base
                                       % (self.file_path,
                                          ErrorString(e.code),
                                          e.lineno,
                                          e.offset)
                                       )
            except BaseException, e:
                # Whatever could happen here ...
                self.output.write_line(self.debug_base % (self.file_path, str(e), 0, 0))
            else:
                return data


class YAMLLoader(LoaderProto):
    name    = "YAML"
    ext     = "yaml"
    comment = "#"
    scope   = "source.yaml"
예제 #16
0
            try:
                data = plist_parser.parse_string(text)
            except plist_parser.PropertyListParseError, e:
                self.output.write_line(self.debug_base %
                                       (self.file_path, str(e), 0, 0))
            else:
                return data
        else:
            try:
                # This will try `from xml.parsers.expat import ParserCreate`
                # but since it is already tried above it should succeed.
                data = plistlib.readPlistFromString(text)
            except ExpatError, e:
                self.output.write_line(
                    self.debug_base %
                    (self.file_path, ErrorString(e.code), e.lineno, e.offset))
            except BaseException, e:
                # Whatever could happen here ...
                self.output.write_line(self.debug_base %
                                       (self.file_path, str(e), 0, 0))
            else:
                return data


class YAMLLoader(LoaderProto):
    name = "YAML"
    ext = "yaml"
    comment = "#"
    scope = "source.yaml"
    debug_base = "Error parsing YAML: %s"
    file_regex = r'^ +in "(.*?)", line (\d+), column (\d+)'
예제 #17
0
    def _render_xml(self, xml_document, **kwargs):
        # Prepare the xml object to be processed by jinja2
        self.log.debug('Rendering XML object')
        template_string = ""

        try:
            self.template_images = dict()
            self._prepare_document_tags(xml_document)
            xml_source = xml_document.toxml()
            xml_source = xml_source.encode('ascii', 'xmlcharrefreplace')
            jinja_template = self.environment.from_string(
                self._unescape_entities(xml_source.decode('utf-8')))

            result = jinja_template.render(**kwargs)

            final_xml = parseString(result.encode('ascii',
                                                  'xmlcharrefreplace'))

            # float convert
            if self.FLOAT_CONVERT != FLOAT_CONVERT_NONE:
                paras = final_xml.getElementsByTagName('text:p')
                for p in paras:
                    for ch in p.childNodes:
                        if ch.nodeType == ch.TEXT_NODE:
                            try:
                                if self.FLOAT_CONVERT == FLOAT_CONVERT_ONLY_FLOAT_STYLE:
                                    style = p.parentNode.getAttribute(
                                        'table:style-name')
                                    if 'float' not in style.lower():
                                        continue

                                float(ch.data)
                                p.parentNode.attributes[
                                    'office:value-type'] = 'float'
                                p.parentNode.attributes[
                                    'calcext:value-type'] = 'float'
                                p.parentNode.attributes[
                                    'office:value'] = ch.data
                            except Exception as e:
                                continue

            if self.template_images:
                self.replace_images(final_xml)

            return final_xml
        except ExpatError as e:
            if not 'result' in locals():
                result = xml_source
            near = result.split('\n')[e.lineno - 1][e.offset - 200:e.offset +
                                                    200]

            raise ExpatError('ExpatError "%s" at line %d, column %d\nNear of: "[...]%s[...]"' % \
                             (ErrorString(e.code), e.lineno, e.offset, near))
        except:
            self.log.error('Error rendering template:\n%s',
                           xml_document.toprettyxml(),
                           exc_info=True)

            self.log.error(
                'Unescaped template was:\n{0}'.format(template_string))
            raise
        finally:
            self.log.debug('Rendering xml object finished')