def relative(self, path, encoding='utf-8'): '''Evaluate the new path relative to the current url''' if isinstance(path, byte_string): path = path.decode(encoding) path = unicodenormalize('NFC', path) if sys.version_info[0] == 3: newurl = urljoin(self.unicode(), path) else: newurl = urljoin(self.utf8(), path.encode('utf-8')) return URL.parse(newurl, 'utf-8')
def parse(cls, url, encoding): '''Parse the provided url, and return a URL instance''' if isinstance(url, byte_string): url = url.decode(encoding) url = unicodenormalize('NFC', url) if sys.version_info[0] == 2: url = url.encode('utf-8') parsed = urlparse(url) try: port = parsed.port except ValueError: port = None userinfo = parsed.username if userinfo and parsed.password: userinfo += ':%s' % parsed.password return cls(parsed.scheme, parsed.hostname, port, parsed.path, parsed.params, parsed.query, parsed.fragment, userinfo)
def _unicodenormalize(ustr, method='NFC'): if isinstance(ustr, bytes): return unicodenormalize(method, ustr.decode('utf-8')).encode('utf-8') else: return unicodenormalize(method, ustr)