micropy.utils

micropy.utils

This module provides utility functions that are used within MicropyCli.

Functions

create_dir_link(source, target)

Creates a platform appropriate directory link.

ensure_existing_dir(path)

Ensure path exists and is a directory.

ensure_valid_url(url)

Ensure a url is valid.

extract_tarbytes(file_bytes, path)

Extract tarfile as bytes.

generate_stub(path[, log_func])

Create Stub from local .py file.

get_package_meta(name, url)

Retrieve package metadata from PyPi.

get_url_filename(url)

Parse filename from url.

is_dir_link(path)

Test if path is either a symlink or directory junction.

is_downloadable(url)

Checks if the url can be downloaded from.

is_existing_dir(path)

Check if path is an existing directory.

is_url(url)

Check if provided string is a url.

iter_requirements(path)

Iterate requirements from a requirements.txt file.

search_xml(url, node)

Search xml from url by node.

stream_download(url, **kwargs)

Stream download with tqdm progress bar.

is_update_available()

Check if micropy-cli update is available.

get_cached_data(url)

Wrap requests with a short cache.

get_class_that_defined_method(meth)

Determines Class that defined a given method.

Classes

PyboardWrapper(port[, connect, verbose])

Wrapper for rshell/pyboard.

Validator(schema_path)

“jsonschema wrapper for file validation.

class micropy.utils.Validator(schema_path)

Bases: object

“jsonschema wrapper for file validation.

Returns

Validator Instance

Return type

object

validate(path)

Validates json file against a schema.

Parameters

path (str) – path to json file to validate

Returns

jsonschema.validate

class micropy.utils.PyboardWrapper(port, connect=True, verbose=False)

Bases: object

Wrapper for rshell/pyboard.

Exposes the basic run/copy functionality Micropy needs

Parameters

port (str) – Port of Pyboard

Kwargs:

connect (bool): Connect on init. Defaults to True

connect()

connect to pyboard.

copy_dir(path, dest, rsync={})

Copy directory from pyboard to machine.

Parameters
  • path (str) – path to directory

  • dest (str) – destination to copy to

  • rsync (dict, optional) – additonal args to pass to rsync call. Defaults to {}

copy_file(source, dest=None)

Copies file to pyboard.

Parameters
  • source (str) – path to file

  • dest (str, optional) – dest on pyboard. Defaults to None. If None, file is copied to pyboard root.

Returns

path to dest on pyboard

Return type

str

list_dir(path)

List directory on pyboard.

Parameters

path (str) – path to directory

property pyb_root

pyboard root dirname.

property pyboard

rshell pyboard instance.

repl()

Pyboard raw repl context manager.

run(file, format_output=None)

Execute a local script on the pyboard.

Parameters
  • file (str) – path to file or string to run

  • format_output (callable, optional) – Callback to format output. Defaults to None. If none, uses print.

micropy.utils.is_url(url)

Check if provided string is a url.

Parameters

url (str) – url to check

Returns

True if arg url is a valid url

Return type

bool

micropy.utils.get_url_filename(url)

Parse filename from url.

Parameters

url (str) – url to parse

Returns

filename of url

Return type

str

micropy.utils.ensure_existing_dir(path)

Ensure path exists and is a directory.

If path does exist, it will be returned as a pathlib.PurePath object

Parameters

path (str) – path to validate and return

Raises
  • NotADirectoryError – path does not exist

  • NotADirectoryError – path is not a directory

Returns

pathlib.PurePath object

Return type

object

micropy.utils.ensure_valid_url(url)

Ensure a url is valid.

Parameters

url (str) – URL to validate

Raises
  • InvalidURL – URL is not a valid url

  • ConnectionError – Failed to connect to url

  • HTTPError – Reponse was not 200 <OK>

Returns

valid url

Return type

str

micropy.utils.is_downloadable(url)

Checks if the url can be downloaded from.

Parameters

url (str) – url to check

Returns

True if contains a downloadable resource

Return type

bool

micropy.utils.is_existing_dir(path)

Check if path is an existing directory.

Parameters

path (str) – path to check

Returns

True if path exists and is a directory

Return type

bool

micropy.utils.stream_download(url, **kwargs)

Stream download with tqdm progress bar.

Parameters

url (str) – url to file

Returns

bytearray of content

Return type

bytearray

micropy.utils.search_xml(url, node)

Search xml from url by node.

Parameters
  • url (str) – url to xml

  • node (str) – node to search for

Returns

matching nodes

Return type

[str]

micropy.utils.generate_stub(path, log_func=None)

Create Stub from local .py file.

Parameters
  • path (str) – Path to file

  • log_func (func, optional) – Callback function for logging. Defaults to None.

Returns

Tuple of file path and generated stub path.

Return type

tuple

micropy.utils.get_package_meta(name, url)

Retrieve package metadata from PyPi.

Parameters
  • name (str) – Name of package with specs.

  • url (str) – Url to package.

Returns

Dictionary of Metadata

Return type

dict

micropy.utils.extract_tarbytes(file_bytes, path)

Extract tarfile as bytes.

Parameters
  • file_bytes (bytearray) – Bytes of file to extract

  • path (str) – Path to extract it to

Returns

destination path

Return type

path

micropy.utils.iter_requirements(path)

Iterate requirements from a requirements.txt file.

Parameters

path (str) – path to file

Creates a platform appropriate directory link.

On POSIX systems it will create a symlink. On Windows it will fallback on a directory junction if needed

Parameters
  • source (os.Pathlike) – Path to create link at.

  • target (os.Pathlike) – Path to link to.

Raises
  • OSError – Symlink Creation Failed

  • OSError – Symlink and Directory Junction Fallback Failed

Test if path is either a symlink or directory junction.

Parameters

path (os.Pathlike) – Path to test.

Returns

True if path is a type of link.

Return type

bool

micropy.utils.is_update_available()

Check if micropy-cli update is available.

Returns

True if update available, else False.

Return type

bool

micropy.utils.get_cached_data(url)

Wrap requests with a short cache.

micropy.utils.get_class_that_defined_method(meth)

Determines Class that defined a given method.

See - https://stackoverflow.com/a/25959545

Parameters

meth (Callable) – Method to determine class from

Returns

Class that defined method

Return type

Callable