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:
  • heading (str) – Keyboard heading.
  • default (str) – [opt] Default text.
  • hidden (bool) – [opt] True for hidden text entry.
Returns:

Returns the user input as unicode.

Return type:

str

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. A True value indicates that blanks should be retained as a blank string. The default False 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. If False (the default), errors are silently ignored. If True, errors raise a “ValueError” exception.

Parameters:
  • qs (str) – Percent-encoded “query string” to be parsed, or a URL with a “query string”.
  • keep_blank_values (bool) – True to keep blank values, else discard.
  • strict_parsing (bool) – True to raise “ValueError” if there are parsing errors, else silently ignore.
Returns:

Returns a dictionary of key/value pairs, with all keys and values as “Unicode”.

Return type:

dict

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 to urlparse.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"
codequick.utils.strip_tags(html)[source]

Strips out HTML tags and return plain text.

Parameters:

html (str) – HTML with text to extract.

Returns:

Html with tags striped out

Return type:

str

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:

str

codequick.utils.ensure_unicode(data, encoding='utf8')[source]

Ensures that the given string is return as type unicode.

Parameters:
  • data (str or bytes) – String to convert if needed.
  • encoding (str) – [opt] The encoding to use if needed..
Returns:

The given string as type unicode.

Return type:

str

codequick.utils.bold(text)[source]

Return Bolded text.

Parameters:text (str) – Text to bold.
Returns:Bolded text.
Return type:str
codequick.utils.italic(text)[source]

Return Italic text.

Parameters:text (str) – Text to italic.
Returns:Italic text.
Return type:str
codequick.utils.color(text, color_code)[source]

Return Colorized text of givin color.

Parameters:
  • text (str) – Text to italic.
  • color_code (str) – Color to change text to.
Returns:

Colorized text.

Return type:

str