def __getattr__(self, item: str) -> Union[str, List[str]]: """ All the magic happens here, this method should be invoked when trying to call (not declared) properties. For instance, calling self.charset should end up here and be replaced by self['charset']. """ item = unpack_protected_keyword(item) if item not in self._valued_attrs: raise AttributeError( "'{item}' attribute is not defined within '{header}' header.". format(item=item, header=self.name)) return self[item]
def __getattr__(self, item: str) -> Union[Header, List[Header]]: """ Where the magic happens, every header is accessible via the property notation. The result is either a single Header or a list of Header. eg. >>> headers = Header("Content-Type", "text/html; charset=UTF-8") + Header("Allow", "POST") + Header("From", "*****@*****.**") >>> headers.content_type Content-Type: text/html; charset=UTF-8 >>> headers.from_ From: [email protected] """ item = unpack_protected_keyword(item) if item not in self: raise AttributeError( "'{item}' header is not defined in headers.".format(item=item)) return self[item]
def __setattr__(self, key: str, value: str) -> None: """ Set attribute on header using the property notation. """ # Avoid conflict with __init__ sequence of Header if key in { "_name", "_normalized_name", "_pretty_name", "_content", "_members", "_not_valued_attrs", "_valued_attrs", "__class__", }: return super().__setattr__(key, value) key = unpack_protected_keyword(key) self[key] = value