|
| 1 | +import sys |
| 2 | +import inspect |
| 3 | +import warnings |
| 4 | +from collections import namedtuple |
| 5 | + |
| 6 | +import certifi |
| 7 | +import logging |
| 8 | +from bs4 import BeautifulSoup |
| 9 | + |
| 10 | +sys.path.append('.') |
| 11 | + |
| 12 | +from telegram.vendor.ptb_urllib3 import urllib3 |
| 13 | +import telegram |
| 14 | + |
| 15 | +IGNORED_OBJECTS = ('ResponseParameters', 'CallbackGame') |
| 16 | +IGNORED_PARAMETERS = {'self', 'args', 'kwargs', 'read_latency', 'network_delay', 'timeout', 'bot'} |
| 17 | + |
| 18 | +logger = logging.getLogger(__name__) |
| 19 | + |
| 20 | + |
| 21 | +def find_next_sibling_until(tag, name, until): |
| 22 | + for sibling in tag.next_siblings: |
| 23 | + if sibling is until: |
| 24 | + return |
| 25 | + if sibling.name == name: |
| 26 | + return sibling |
| 27 | + |
| 28 | + |
| 29 | +def parse_table(h4): |
| 30 | + table = find_next_sibling_until(h4, 'table', h4.find_next_sibling('h4')) |
| 31 | + if not table: |
| 32 | + return [] |
| 33 | + head = [td.text for td in table.tr.find_all('td')] |
| 34 | + row = namedtuple('{}TableRow'.format(h4.text), ','.join(head)) |
| 35 | + t = [] |
| 36 | + for tr in table.find_all('tr')[1:]: |
| 37 | + t.append(row(*[td.text for td in tr.find_all('td')])) |
| 38 | + return t |
| 39 | + |
| 40 | + |
| 41 | +def check_method(h4): |
| 42 | + name = h4.text |
| 43 | + method = getattr(telegram.Bot, name) |
| 44 | + table = parse_table(h4) |
| 45 | + |
| 46 | + # Check arguments based on source |
| 47 | + sig = inspect.signature(method, follow_wrapped=True) |
| 48 | + |
| 49 | + checked = [] |
| 50 | + for parameter in table: |
| 51 | + param = sig.parameters.get(parameter.Parameters) |
| 52 | + logger.debug(parameter) |
| 53 | + assert param is not None |
| 54 | + # TODO: Check type via docstring |
| 55 | + # TODO: Check if optional or required |
| 56 | + checked.append(parameter.Parameters) |
| 57 | + |
| 58 | + ignored = IGNORED_PARAMETERS.copy() |
| 59 | + if name == 'getUpdates': |
| 60 | + ignored -= {'timeout'} # Has it's own timeout parameter that we do wanna check for |
| 61 | + elif name == 'sendDocument': |
| 62 | + ignored |= {'filename'} # Undocumented |
| 63 | + elif name == 'setGameScore': |
| 64 | + ignored |= {'edit_message'} # TODO: Now deprecated, so no longer in telegrams docs |
| 65 | + |
| 66 | + logger.debug((sig.parameters.keys(), checked, ignored, |
| 67 | + sig.parameters.keys() - checked - ignored)) |
| 68 | + assert (sig.parameters.keys() ^ checked) - ignored == set() |
| 69 | + |
| 70 | + |
| 71 | +def check_object(h4): |
| 72 | + name = h4.text |
| 73 | + obj = getattr(telegram, name) |
| 74 | + table = parse_table(h4) |
| 75 | + |
| 76 | + # Check arguments based on source |
| 77 | + sig = inspect.signature(obj, follow_wrapped=True) |
| 78 | + |
| 79 | + checked = [] |
| 80 | + for parameter in table: |
| 81 | + field = parameter.Field |
| 82 | + if field == 'from': |
| 83 | + field = 'from_user' |
| 84 | + elif name.startswith('InlineQueryResult') and field == 'type': |
| 85 | + continue |
| 86 | + elif field == 'remove_keyboard': |
| 87 | + continue |
| 88 | + |
| 89 | + param = sig.parameters.get(field) |
| 90 | + logger.debug(parameter) |
| 91 | + assert param is not None |
| 92 | + # TODO: Check type via docstring |
| 93 | + # TODO: Check if optional or required |
| 94 | + checked.append(field) |
| 95 | + |
| 96 | + ignored = IGNORED_PARAMETERS.copy() |
| 97 | + if name == 'InputFile': |
| 98 | + ignored |= {'data'} |
| 99 | + elif name == 'InlineQueryResult': |
| 100 | + ignored |= {'id'} |
| 101 | + elif name == 'User': |
| 102 | + ignored |= {'type'} # TODO: Deprecation |
| 103 | + |
| 104 | + if name.startswith('InlineQueryResult'): |
| 105 | + ignored |= {'type'} |
| 106 | + |
| 107 | + logger.debug((sig.parameters.keys(), checked, ignored, |
| 108 | + sig.parameters.keys() - checked - ignored)) |
| 109 | + assert (sig.parameters.keys() ^ checked) - ignored == set() |
| 110 | + |
| 111 | + |
| 112 | +def test_official(): |
| 113 | + if not sys.version_info >= (3, 5): |
| 114 | + warnings.warn('Not running tests, since follow_wrapped is not supported on this platform' |
| 115 | + '(python version >= 3.5 required)') |
| 116 | + return |
| 117 | + |
| 118 | + http = urllib3.PoolManager( |
| 119 | + cert_reqs='CERT_REQUIRED', |
| 120 | + ca_certs=certifi.where()) |
| 121 | + request = http.request('GET', 'https://core.telegram.org/bots/api') |
| 122 | + soup = BeautifulSoup(request.data.decode('utf-8'), 'html.parser') |
| 123 | + |
| 124 | + for thing in soup.select('h4 > a.anchor'): |
| 125 | + # Methods and types don't have spaces in them, luckily all other sections of the docs do |
| 126 | + # TODO: don't depend on that |
| 127 | + if '-' not in thing['name']: |
| 128 | + h4 = thing.parent |
| 129 | + name = h4.text |
| 130 | + |
| 131 | + test = None |
| 132 | + # Is it a method |
| 133 | + if h4.text[0].lower() == h4.text[0]: |
| 134 | + test = check_method |
| 135 | + else: # Or a type/object |
| 136 | + if name not in IGNORED_OBJECTS: |
| 137 | + test = check_object |
| 138 | + |
| 139 | + if test: |
| 140 | + def fn(): |
| 141 | + return test(h4) |
| 142 | + fn.description = '{}({}) ({})'.format(test.__name__, h4.text, __name__) |
| 143 | + yield fn |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == '__main__': |
| 147 | + # Since we don't have the nice unittest asserts which show the comparison |
| 148 | + # We turn on debugging |
| 149 | + logging.basicConfig( |
| 150 | + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', |
| 151 | + level=logging.DEBUG) |
| 152 | + for f in test_official(): |
| 153 | + f() |
0 commit comments