Utils¶
A collection of useful functions.
-
codequick.utils.
string_map
= {}¶ Dict of localized string references used in conjunction with
Script.localize
. Allowing you to use the string as the localized string reference.Note
It is best if you set the string references at the top of your add-on python file.
Example: >>> Script.localize(30001) "Toutes les vidéos" >>> >>> # Add reference id for "All Videos" so you can use the string name instead. >>> utils.string_map["All Videos": 30001] >>> Script.localize("All Videos") "Toutes les vidéos"
-
codequick.utils.
keyboard
(heading, default='', hidden=False)[source]¶ Show a keyboard dialog.
Parameters: Returns: Returns the user input as unicode.
Return type:
-
codequick.utils.
parse_qs
(qs, keep_blank_values=False, strict_parsing=False)[source]¶ Parse a “urlencoded” query string, and return the data as a dictionary.
Parse a query string given as a string or unicode argument (data of type application/x-www-form- urlencoded). Data is returned as a dictionary. The dictionary keys are the “Unique” query variable names and the values are “Unicode” values for each name.
The optional argument
keep_blank_values
, is a flag indicating whether blank values in percent-encoded queries should be treated as a blank string. ATrue
value indicates that blanks should be retained as a blank string. The defaultFalse
value indicates that blank values are to be ignored and treated as if they were not included.The optional argument
strict_parsing
, is a flag indicating what to do with parsing errors. IfFalse
(the default), errors are silently ignored. IfTrue
, errors raise a “ValueError” exception.Parameters: Returns: Returns a dictionary of key/value pairs, with all keys and values as “Unicode”.
Return type: Raises: ValueError – If duplicate query field names exists or if there is a parsing error.
Example: >>> parse_qs("http://example.com/path?q=search&safe=no") {u"q": u"search", u"safe": u"no"} >>> parse_qs(u"q=search&safe=no") {u"q": u"search", u"safe": u"no"}
-
codequick.utils.
urljoin_partial
(base_url)[source]¶ Construct a full (absolute) URL by combining a base URL with another URL.
This is useful when parsing HTML, as the majority of links would be relative links.
Informally, this uses components of the base URL, in particular the addressing scheme, the network location and (part of) the path, to provide missing components in the relative URL.
Returns a new “partial” object which when called, will pass
base_url
tourlparse.urljoin()
along with the supplied relative URL.Parameters: base_url (str) – The absolute URL to use as the base.
Returns: A partial function that accepts a relative URL and returns a full absolute URL.
Example: >>> url_constructor = urljoin_partial("https://google.ie/") >>> url_constructor("/path/to/something") "https://google.ie/path/to/something" >>> url_constructor("/gmail") "https://google.ie/gmail"
Strips out HTML tags and return plain text.
Parameters: html (str) – HTML with text to extract.
Returns: Html with tags striped out
Return type: Example: >>> strip_tags('<a href="http://example.com/">I linked to <i>example.com</i></a>') "I linked to example.com"
-
codequick.utils.
ensure_native_str
(data, encoding='utf8')[source]¶ Ensures that the given string is returned as a native str type,
bytes
on Python 2,unicode
on Python 3.Parameters: - data – String to convert if needed.
- encoding (str) – [opt] The encoding to use if needed..
Returns: The given string as a native
str
type.Return type:
-
codequick.utils.
ensure_unicode
(data, encoding='utf8')[source]¶ Ensures that the given string is return as type
unicode
.Parameters: Returns: The given string as type
unicode
.Return type:
-
codequick.utils.
bold
(text)[source]¶ Return Bolded text.
Parameters: text (str) – Text to bold. Returns: Bolded text. Return type: str