| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2016 Emmanuel Saint-James <esj@rezo.net> |
|---|
| 4 | # |
|---|
| 5 | |
|---|
| 6 | import os |
|---|
| 7 | import re |
|---|
| 8 | import xml.sax |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | class DoxygenTracHandler(xml.sax.ContentHandler): |
|---|
| 12 | """ |
|---|
| 13 | A class using SAX for scanning the XML index produced by Doxygen. |
|---|
| 14 | """ |
|---|
| 15 | |
|---|
| 16 | to_where = '' |
|---|
| 17 | to_date = '' |
|---|
| 18 | to_multi = '' |
|---|
| 19 | last_field_name = '' |
|---|
| 20 | fields = {} |
|---|
| 21 | multi = [] |
|---|
| 22 | |
|---|
| 23 | def __init__(self, find, where, all, index): |
|---|
| 24 | self.to_where = where |
|---|
| 25 | self.to_date = os.path.getctime(index) |
|---|
| 26 | self.to_multi = all |
|---|
| 27 | self.to_find = find.replace('::', '\\') |
|---|
| 28 | if all: |
|---|
| 29 | self.to_find = re.compile(r'''%s''' % self.to_find) |
|---|
| 30 | |
|---|
| 31 | def characters(self, content): |
|---|
| 32 | if self.last_field_name != "": |
|---|
| 33 | self.fields[self.last_field_name] += content |
|---|
| 34 | |
|---|
| 35 | def startElement(self, name, attrs): |
|---|
| 36 | if name == 'field': |
|---|
| 37 | self.last_field_name = attrs['name'] |
|---|
| 38 | self.fields[self.last_field_name] = '' |
|---|
| 39 | else: |
|---|
| 40 | self.last_field_name = '' |
|---|
| 41 | |
|---|
| 42 | def endElement(self, name): |
|---|
| 43 | if name == "doc": |
|---|
| 44 | self.fields['occ'] = 0 |
|---|
| 45 | self.fields['target'] = '' |
|---|
| 46 | self.fields['date'] = self.to_date |
|---|
| 47 | for field in self.to_where: |
|---|
| 48 | if not self.to_multi: |
|---|
| 49 | p = self.to_find == self.fields[field] |
|---|
| 50 | else: |
|---|
| 51 | p = self.to_find.findall(self.fields[field]) |
|---|
| 52 | |
|---|
| 53 | if p: |
|---|
| 54 | if '#' in self.fields['url']: |
|---|
| 55 | url, target = self.fields['url'].split('#', 2) |
|---|
| 56 | self.fields['url'] = url |
|---|
| 57 | self.fields['target'] = target |
|---|
| 58 | if not self.to_multi: |
|---|
| 59 | raise IndexFound(self.fields) |
|---|
| 60 | else: |
|---|
| 61 | self.fields['occ'] += len(list(set(p))) |
|---|
| 62 | |
|---|
| 63 | if self.fields['occ']: |
|---|
| 64 | self.multi.append(self.fields) |
|---|
| 65 | self.fields = {} |
|---|
| 66 | elif name == "add" and self.to_multi: |
|---|
| 67 | raise IndexFound(self.multi) |
|---|
| 68 | elif self.last_field_name == 'keywords': |
|---|
| 69 | # Doxygen produces duplicates in this field ! |
|---|
| 70 | self.fields['keywords'] = ' '.join( |
|---|
| 71 | list(set(self.fields['keywords'].split(' ')))) |
|---|
| 72 | self.last_field_name = '' |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | class IndexFound(Exception): |
|---|
| 76 | def __init__(self, msg): |
|---|
| 77 | Exception.__init__(self, msg) |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | def search_in_doxygen(file, name, where, multi, log): |
|---|
| 81 | if not file: |
|---|
| 82 | return {} |
|---|
| 83 | parser = xml.sax.make_parser() |
|---|
| 84 | parser.setContentHandler(DoxygenTracHandler(name, where, multi, file)) |
|---|
| 85 | res = {} |
|---|
| 86 | try: |
|---|
| 87 | parser.parse(file) |
|---|
| 88 | except IndexFound as a: |
|---|
| 89 | res = a.args[0] |
|---|
| 90 | except xml.sax.SAXException as a: |
|---|
| 91 | log.debug("SAX %s", a) |
|---|
| 92 | return res |
|---|