archivebox.misc.util
ο
Module Contentsο
Classesο
Extended json serializer that supports serializing several model fields and objects |
Functionsο
check that all parentheses in a string are balanced and nested properly |
|
cleanup a regex-parsed url that may contain dangling trailing parens from markdown link syntax helpful to fix URLs parsed from markdown e.g. input: https://wikipedia.org/en/some_article_(Disambiguation).html?abc=def).somemoretext result: https://wikipedia.org/en/some_article_(Disambiguation).html?abc=def |
|
Enforce function arg and kwarg types at runtime using its python3 type hints Simpler version of pydantic @validate_call decorator |
|
attach the given docstring to the decorated function |
|
( |
|
Parse unix timestamps, iso format, and human-readable strings |
|
Download the contents of a remote url and return the text |
|
Download the contents of a remote url and return the headers |
|
Based on: https://stackoverflow.com/questions/19212665/python-converting-ansi-color-codes-to-html Simple way to render colored CLI stdout/stderr in HTML properly, Textual/rich is probably better though. |
|
Deduplicates the given CLI args by key=value. Options that come later override earlier. |
Dataο
APIο
- archivebox.misc.util.parens_are_matched(string: str, open_char='(', close_char=')')[source]ο
check that all parentheses in a string are balanced and nested properly
- archivebox.misc.util.fix_url_from_markdown(url_str: str) str [source]ο
cleanup a regex-parsed url that may contain dangling trailing parens from markdown link syntax helpful to fix URLs parsed from markdown e.g. input: https://wikipedia.org/en/some_article_(Disambiguation).html?abc=def).somemoretext result: https://wikipedia.org/en/some_article_(Disambiguation).html?abc=def
IMPORTANT ASSUMPTION: valid urls wont have unbalanced or incorrectly nested parentheses e.g. this will fail the user actually wants to ingest a url like βhttps://example.com/some_wei)(rd_urlβ in that case it will return https://example.com/some_wei (truncated up to the first unbalanced paren) This assumption is true 99.9999% of the time, and for the rare edge case the user can use url_list parser.
- archivebox.misc.util.enforce_types(func)[source]ο
Enforce function arg and kwarg types at runtime using its python3 type hints Simpler version of pydantic @validate_call decorator
- archivebox.misc.util.docstring(text: Optional[str])[source]ο
attach the given docstring to the decorated function
- archivebox.misc.util.str_between(string: str, start: str, end: str = None) str [source]ο
(
12345, , ) -> 12345
- archivebox.misc.util.parse_date(date: Any) datetime.datetime [source]ο
Parse unix timestamps, iso format, and human-readable strings
- archivebox.misc.util.download_url(url: str, timeout: int = None) str [source]ο
Download the contents of a remote url and return the text
- archivebox.misc.util.get_headers(url: str, timeout: int | None = None) str [source]ο
Download the contents of a remote url and return the headers
- archivebox.misc.util.ansi_to_html(text: str) str [source]ο
Based on: https://stackoverflow.com/questions/19212665/python-converting-ansi-color-codes-to-html Simple way to render colored CLI stdout/stderr in HTML properly, Textual/rich is probably better though.
- archivebox.misc.util.dedupe(options: List[str]) List[str] [source]ο
Deduplicates the given CLI args by key=value. Options that come later override earlier.
- class archivebox.misc.util.ExtendedEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]ο
Bases:
json.JSONEncoder
Extended json serializer that supports serializing several model fields and objects
Initialization
Constructor for JSONEncoder, with sensible defaults.
If skipkeys is false, then it is a TypeError to attempt encoding of keys that are not str, int, float or None. If skipkeys is True, such items are simply skipped.
If ensure_ascii is true, the output is guaranteed to be str objects with all incoming non-ASCII characters escaped. If ensure_ascii is false, the output can contain non-ASCII characters.
If check_circular is true, then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause an RecursionError). Otherwise, no such check takes place.
If allow_nan is true, then NaN, Infinity, and -Infinity will be encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats.
If sort_keys is true, then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis.
If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation.
If specified, separators should be an (item_separator, key_separator) tuple. The default is (β, β, β: β) if indent is
None
and (β,β, β: β) otherwise. To get the most compact JSON representation, you should specify (β,β, β:β) to eliminate whitespace.If specified, default is a function that gets called for objects that canβt otherwise be serialized. It should return a JSON encodable version of the object or raise a
TypeError
.
- archivebox.misc.util.URL_REGEX_TESTS[source]ο
[(βhttps://example.comβ, [βhttps://example.comβ]), (βhttp://abc-file234example.com/abc?def=abc&23423β¦