def make_url(u: t.Union[Url, dict], **kwargs) -> str: p = gws.to_dict(u) p.update(kwargs) s = '' if p.get('scheme'): s += p['scheme'] + ':' s += '//' if p.get('username'): s += quote_param(p['username']) + ':' + quote_param( p.get('password', '')) + '@' s += p['hostname'] if p.get('port'): s += ':' + str(p['port']) if p.get('path'): s += '/' + quote_path(p['path'].lstrip('/')) if p.get('params'): s += '?' + make_qs(p['params']) if p.get('fragment'): s += '#' + p['fragment'].lstrip('#') return s
def make_qs(x) -> str: """Convert a dict/list to a query string. For each item in x, if it's a list, join it with a comma, stringify and in utf8. Args: x: Value, which can be a dict'able or a list of key,value pairs. Returns: The query string. """ p = [] items = x if isinstance(x, (list, tuple)) else gws.to_dict(x).items() def _value(v): if isinstance(v, (bytes, bytearray)): return v if isinstance(v, str): return v.encode('utf8') if v is True: return b'true' if v is False: return b'false' try: return b','.join(_value(y) for y in v) except TypeError: return str(v).encode('utf8') for k, v in items: k = urllib.parse.quote_from_bytes(_value(k)) v = urllib.parse.quote_from_bytes(_value(v)) p.append(k + '=' + v) return '&'.join(p)
def from_dict(d: dict, trusted=False, with_strict_mode=True) -> 'Style': vals = {} s = d.get('text') if s: vals.update(parser.parse_text(s, trusted, with_strict_mode)) s = d.get('values') if s: vals.update( parser.parse_dict(gws.to_dict(s), trusted, with_strict_mode)) return Style( name=d.get('name', ''), values=gws.StyleValues(vals), selector=d.get('selector', ''), text=d.get('text', ''), )
def extend(self, *others): vs = vars(self.values) for other in others: if not other: continue if isinstance(other, Metadata): other = other.values for k, new in gws.to_dict(other).items(): if k in _NON_EXTENDABLE_PROPS or gws.is_empty(new): continue old = vs.get(k) if old is None: vs[k] = new elif k in _EXTENDABLE_LIST_PROPS: vs[k] = sorted(set(old + new)) self._ensure_consistency() return self
def from_props(props: gws.Props) -> 'Metadata': return from_dict(gws.to_dict(props))
def from_config(cfg: gws.Config) -> 'Metadata': return from_dict(gws.to_dict(cfg))
def from_props(props: gws.Props) -> 'Style': d = gws.to_dict(props) return from_dict(d, trusted=False, with_strict_mode=False)
def from_config(cfg: gws.Config) -> 'Style': d = gws.to_dict(cfg) return from_dict(d, trusted=True, with_strict_mode=True)