mycroft.util package¶
The mycroft.util package includes functions for common operations such as playing audio files, parsting and creating natural text as well as many components used internally in Mycroft such as cache directory lookup, path resolution. etc.
Below _some_ of the functions that are of interest to skill developers are listed.
LOG¶
- mycroft.util.LOG(name)[source]¶
Custom logger class that acts like logging.Logger The logger name is automatically generated by the module of the caller
- Usage:
>>> LOG.debug('My message: %s', debug_str) 13:12:43.673 - :<module>:1 - DEBUG - My message: hi >>> LOG('custom_name').debug('Another message') 13:13:10.462 - custom_name - DEBUG - Another message
play_wav¶
- mycroft.util.play_wav(uri, environment=None)[source]¶
Play a wav-file.
This will use the application specified in the mycroft config and play the uri passed as argument. The function will return directly and play the file in the background.
- Parameters
uri – uri to play
environment (dict) – optional environment for the subprocess call
Returns: subprocess.Popen object or None if operation failed
play_mp3¶
- mycroft.util.play_mp3(uri, environment=None)[source]¶
Play a mp3-file.
This will use the application specified in the mycroft config and play the uri passed as argument. The function will return directly and play the file in the background.
- Parameters
uri – uri to play
environment (dict) – optional environment for the subprocess call
Returns: subprocess.Popen object or None if operation failed
play_ogg¶
- mycroft.util.play_ogg(uri, environment=None)[source]¶
Play an ogg-file.
This will use the application specified in the mycroft config and play the uri passed as argument. The function will return directly and play the file in the background.
- Parameters
uri – uri to play
environment (dict) – optional environment for the subprocess call
Returns: subprocess.Popen object, or None if operation failed
resolve_resource_file¶
- mycroft.util.resolve_resource_file(res_name)[source]¶
Convert a resource into an absolute filename.
Resource names are in the form: ‘filename.ext’ or ‘path/filename.ext’
The system wil look for $XDG_DATA_DIRS/mycroft/res_name first (defaults to ~/.local/share/mycroft/res_name), and if not found will look at /opt/mycroft/res_name, then finally it will look for res_name in the ‘mycroft/res’ folder of the source code package.
Example
With mycroft running as the user ‘bob’, if you called
resolve_resource_file('snd/beep.wav')
it would return either: ‘$XDG_DATA_DIRS/mycroft/beep.wav’, ‘/home/bob/.mycroft/snd/beep.wav’ or ‘/opt/mycroft/snd/beep.wav’ or ‘…/mycroft/res/snd/beep.wav’ where the ‘…’ is replaced by the path where the package has been installed.- Parameters
res_name (str) – a resource path/name
- Returns
(str) path to resource or None if no resource found
get_cache_directory¶
- mycroft.util.get_cache_directory(domain=None)[source]¶
Get a directory for caching data.
This directory can be used to hold temporary caches of data to speed up performance. This directory will likely be part of a small RAM disk and may be cleared at any time. So code that uses these cached files must be able to fallback and regenerate the file.
- Parameters
domain (str) – The cache domain. Basically just a subdirectory.
- Returns
(str) a path to the directory where you can cache data