Coverage for appr/discovery.py: 24%

# Coverage for appr/discovery.py : 24%

## 51 statements 12 run 39 missing 0 excluded

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | from __future__ import absolute_import, division, print_function import re from html.parser import HTMLParser import requests package_regexp = "(.+?)/(.+)" class MetaHTMLParser(HTMLParser): def __init__(self, variables): self.meta = {} self.variables = variables HTMLParser.__init__(self) def replace_values(self, s): for k, v in self.variables.items(): s = s.replace("{%s}" % k, v) return s def handle_starttag(self, tag, attrs): if tag == "meta": d = dict(attrs) if 'name' in d and d['name'] == 'appr-package': name, source = d['content'].split(" ") name = self.replace_values(name) source = self.replace_values(source) if name not in self.meta: self.meta[name] = [] self.meta[name].append(source) def split_package_name(package): m = re.search(package_regexp, package) host, name = (m.group(1), m.group(2)) return (host, name) def ishosted(package): host, _ = split_package_name(package) if "." in host or 'localhost' in host: return True else: return False def discover_sources(package, version, media_type, secure=False): schemes = ["https://", "http://"] host, name = split_package_name(package) for scheme in schemes: url = scheme + host try: r = requests.get(url, params={"appr-discovery": 1}, timeout=2) except (requests.exceptions.Timeout, requests.ConnectionError) as e: if scheme == "https://" and not secure: continue else: raise e r.raise_for_status() variables = { 'name': name, 'version': version, "media_type": media_type, "mediatype": media_type} p = MetaHTMLParser(variables) p.feed(r.content.decode()) if package in p.meta: return p.meta[package] return None