|
| 1 | +from __future__ import print_function |
| 2 | +import sys |
| 3 | + |
| 4 | +import logging |
| 5 | +import os |
| 6 | +from pprint import pprint |
| 7 | +import traceback |
| 8 | + |
| 9 | +try: |
| 10 | + import bs4 |
| 11 | + from bs4 import BeautifulSoup |
| 12 | +except ImportError: |
| 13 | + raise ImportError('Error: ' |
| 14 | + 'Install BeautifulSoup (bs4) for adding' |
| 15 | + ' Python & Java signatures documentation') |
| 16 | + |
| 17 | +def load_html_file(file_dir): |
| 18 | + """ Uses BeautifulSoup to load an html """ |
| 19 | + with open(file_dir, 'rb') as fp: |
| 20 | + soup = BeautifulSoup(fp, 'html.parser') |
| 21 | + return soup |
| 22 | + |
| 23 | +def update_html(file, soup): |
| 24 | + s = str(soup) |
| 25 | + if os.name == 'nt' or sys.version_info[0] == 3: # if Windows |
| 26 | + s = s.encode('utf-8', 'ignore') |
| 27 | + with open(file, 'wb') as f: |
| 28 | + f.write(s) |
| 29 | + |
| 30 | + |
| 31 | +def insert_python_signatures(python_signatures, symbols_dict, filepath): |
| 32 | + soup = load_html_file(filepath) |
| 33 | + entries = soup.find_all(lambda tag: tag.name == "a" and tag.has_attr('id')) |
| 34 | + for e in entries: |
| 35 | + anchor = e['id'] |
| 36 | + if anchor in symbols_dict: |
| 37 | + s = symbols_dict[anchor] |
| 38 | + logging.info('Process: %r' % s) |
| 39 | + if s.type == 'fn' or s.type == 'method': |
| 40 | + process_fn(soup, e, python_signatures[s.cppname], s) |
| 41 | + elif s.type == 'const': |
| 42 | + process_const(soup, e, python_signatures[s.cppname], s) |
| 43 | + else: |
| 44 | + logging.error('unsupported type: %s' % s); |
| 45 | + |
| 46 | + update_html(filepath, soup) |
| 47 | + |
| 48 | + |
| 49 | +def process_fn(soup, anchor, python_signature, symbol): |
| 50 | + try: |
| 51 | + r = anchor.find_next_sibling(class_='memitem').find(class_='memproto').find('table') |
| 52 | + insert_python_fn_signature(soup, r, python_signature, symbol) |
| 53 | + except: |
| 54 | + logging.error("Can't process: %s" % symbol) |
| 55 | + traceback.print_exc() |
| 56 | + pprint(anchor) |
| 57 | + |
| 58 | + |
| 59 | +def process_const(soup, anchor, python_signature, symbol): |
| 60 | + try: |
| 61 | + #pprint(anchor.parent) |
| 62 | + description = append(soup.new_tag('div', **{'class' : ['python_language']}), |
| 63 | + 'Python: ' + python_signature[0]['name']) |
| 64 | + old = anchor.find_next_sibling('div', class_='python_language') |
| 65 | + if old is None: |
| 66 | + anchor.parent.append(description) |
| 67 | + else: |
| 68 | + old.replace_with(description) |
| 69 | + #pprint(anchor.parent) |
| 70 | + except: |
| 71 | + logging.error("Can't process: %s" % symbol) |
| 72 | + traceback.print_exc() |
| 73 | + pprint(anchor) |
| 74 | + |
| 75 | + |
| 76 | +def insert_python_fn_signature(soup, table, variants, symbol): |
| 77 | + description = create_python_fn_description(soup, variants) |
| 78 | + description['class'] = 'python_language' |
| 79 | + soup = insert_or_replace(table, description, 'table', 'python_language') |
| 80 | + return soup |
| 81 | + |
| 82 | + |
| 83 | +def create_python_fn_description(soup, variants): |
| 84 | + language = 'Python:' |
| 85 | + table = soup.new_tag('table') |
| 86 | + heading_row = soup.new_tag('th') |
| 87 | + table.append( |
| 88 | + append(soup.new_tag('tr'), |
| 89 | + append(soup.new_tag('th', colspan=999, style="text-align:left"), language))) |
| 90 | + for v in variants: |
| 91 | + #logging.debug(v) |
| 92 | + add_signature_to_table(soup, table, v, language, type) |
| 93 | + #print(table) |
| 94 | + return table |
| 95 | + |
| 96 | + |
| 97 | +def add_signature_to_table(soup, table, signature, language, type): |
| 98 | + """ Add a signature to an html table""" |
| 99 | + row = soup.new_tag('tr') |
| 100 | + row.append(soup.new_tag('td', style='width: 20px;')) |
| 101 | + |
| 102 | + if 'ret' in signature: |
| 103 | + row.append(append(soup.new_tag('td'), signature['ret'])) |
| 104 | + row.append(append(soup.new_tag('td'), '=')) |
| 105 | + else: |
| 106 | + row.append(soup.new_tag('td')) # return values |
| 107 | + row.append(soup.new_tag('td')) # '=' |
| 108 | + |
| 109 | + row.append(append(soup.new_tag('td'), signature['name'] + '(')) |
| 110 | + row.append(append(soup.new_tag('td', **{'class': 'paramname'}), signature['arg'])) |
| 111 | + row.append(append(soup.new_tag('td'), ')')) |
| 112 | + table.append(row) |
| 113 | + |
| 114 | + |
| 115 | +def append(target, obj): |
| 116 | + target.append(obj) |
| 117 | + return target |
| 118 | + |
| 119 | + |
| 120 | +def insert_or_replace(element_before, new_element, tag, tag_class): |
| 121 | + old = element_before.find_next_sibling(tag, class_=tag_class) |
| 122 | + if old is None: |
| 123 | + element_before.insert_after(new_element) |
| 124 | + else: |
| 125 | + old.replace_with(new_element) |
0 commit comments