The following two functions (py3_str() and py3_b()) are commonly used utility functions, and so they are exposed directly in the kaa namespace.
Convert (if necessary) the given value to a (unicode) string.
Parameters: |
|
---|---|
Returns: | the value as a (unicode) string, or the original value if coerce is False and the value was not a bytes or string. |
Note
The naming of str is relative Python 3’s notion of a str object, hence the py3_ prefix. On Python 2, the returned value is a unicode object while on Python 3, the returned value is a str object.
Convert (if necessary) the given value to a “string of bytes”, agnostic to any character encoding.
Parameters: |
|
---|---|
Returns: | the value as a string of bytes, or the original value if coerce is False and the value was not a bytes or string. |
Note
The notion of “bytes” was introduced in Python 3 (and included in Python 2.6 as an alias to str), hence the py3_ prefix. On Python 2, the returned value is a str object while on Python 3, the returned value is a bytes object.
Returns a string object native to the current Python version, converting between types if needed.
Parameters: | s – bytes or unicode object to convert |
---|
This is useful for functions like __str__ which expects a native string type.
Return an object appropriate to represent a filename for the current Python version.
Parameters: | s – the filename to decode if needed (Python 3) or encode if needed (Python 2) |
---|
Python 2 returns a non-unicode string, while Python 3 returns a unicode string using the surrogateescape handler. See PEP 383.
Note
This is a convenience function, equivalent to:
nativestr(s, fs=True, desperate=False)
Returns a UTF-8 string, converting from other character sets if necessary.
Return the current encoding.
Set default character encoding. This function also sets the global Python encoding.
Format a string and make sure all string or unicode arguments are converted to the correct type.
TODO: utc, local
TODO: property
Return a filename in the secure kaa tmp directory with the given name. Name can also be a relative path in the temp directory, directories will be created if missing. If unique is set, it will return a unique name based on the given name.
Does what which(1) does: searches the PATH in order for a given file name and returns the full path to first match.
Forks the process. May safely be called after the main loop has been started.
Does a double-fork to daemonize the current process using the technique described at http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16 .
If exit is True (default), parent exits immediately. If false, caller will receive the pid of the forked child.
Check if the program with the given name is running. The program must have called set_running itself. Returns the pid or 0.
Set this program as running with the given name. If modify is True, the process name is updated as described in set_process_name().
On Linux systems later than 2.6.9, this function sets the process name as it appears in ps, and so that it can be found with killall(1) and pidof(8).
Note
name will be truncated to the cumulative length of the original process name and all its arguments; once updated, passed arguments will no longer be visible.
This function currently only works properly with Python 2. With Python 3, the process will be found with killall(1), but ps(1) and pidof(8) will not see the new name.
Returns the number of processors on the system, or raises RuntimeError if that value cannot be determined.
Returns a unique (and hopefully persistent) identifier for the current machine.
This function will return the D-Bus UUID if it exists (which should be available on modern Linuxes), otherwise it will return the machine’s hostname.
Flexible plugin loader, supporting Python eggs as well as on-disk trees. All modules at the specified location or entry point group (for eggs) are loaded and returned as a dict mapping plugin names to plugin objects.
Parameters: |
|
---|---|
Returns: | a dict mapping plugin names to plugin objects. |
If a plugin raises an exception while it is being imported, the value in the returned dictionary for that plugin will be the Exception object that was raised.
Method 1 is the more traditional approach to plugins in Python. For example, you might have a directory structure like:
module/
plugins/
__init__.py
plugin1.py
plugin2.py
There is typically logic inside plugins/__init__.py to import all other files in the same directory (os.path.dirname(__file__)). It can pass location=__file__ to kaa.utils.get_plugins() to do this:
>>> kaa.utils.get_plugins(location=__file__)
{'plugin1': <module 'module.plugins.plugin1' from '.../module/plugins/plugin1.py'>,
'plugin2': <module 'module.plugins.plugin2' from '.../module/plugins/plugin2.py'>}
Note
Plugins will be imported relative to the caller’s module name. So if the caller is module module.plugins, plugin1 will be imported as module.plugins.plugin1.
This also works when plugins/__init__.py is inside a zipped egg. However, if you reference __file__ as above, setuptools (assuming it’s available on the system), when it examines the source code during installation, will think it’s not safe to zip the tree into an egg file, and so it will install it as an on-disk directory tree instead. You can override this by passing zip_safe=True to setup() in the setup script for the application that loads plugins.
Method 2 is the approach to take when setuptools is available. Plugin modules register themselves with an entry point group name. For example, a plugin module’s setup.py may call setup() with:
setup(
module='myplugin',
# ...
plugins = {'someapplication.plugins': 'src/submodule'},
entry_points = {'someapplication.plugins': 'plugname = myplugin.submodule:SomeClass'},
)
When SomeApplication wants to load all registered modules, it can do:
>>> kaa.utils.get_plugins(group='someapplication.plugins')
{'plugname': <class myplugin.submodule.SomeClass at 0xdeadbeef>}
The entry_points kwarg specified in the plugin module can specify multiple plugin names, and SomeClass could be any python object, as long as it is exposed within myplugin.submodule. For more information on entry points, refer to setuptools documentation.
It is possible and in fact recommended to mix both methods. (If your project makes setuptools a mandatory dependency, then you can use entry point groups exclusively.) If group is not specified, then it becomes impossible for either the application or the plugin to be installed as eggs.
The attr kwarg makes it more convenient to combine both methods. Plugins loaded from entry points will be SomeClass objects. If (assuming now the application is not installed as a zipped egg but rather an on-disk source tree) some plugins were installed to the applications source tree, while others installed as eggs registered with the entry point group, you would want to pull SomeClass from those modules loaded from location. So:
>>> kaa.utils.get_plugins(group='someapplication.plugins', location=__file__, attr='SomeClass')
{'plugin1': <class module.plugins.plugin1.SomeClass at 0xdeadbeef>,
'plugin2': <class module.plugins.plugin2.SomeClass at 0xcafebabe>,
'plugname': <class myplugin.submodule.SomeClass at 0xbaadf00d>}
Decorator factory: used to create a decorator that assumes the same attributes (name, docstring, signature) as its decorated function when sphinx has been imported. This is necessary because sphinx uses introspection to construct the documentation.
This logic is inspired from Michele Simionato’s decorator module.
>>> def decorator(func):
... @wraps(func)
... def newfunc(*args, **kwargs):
... # custom logic here ...
... return func(*args, **kwargs)
... return newfunc
Parameters: |
|
---|---|
Returns: | a decorator which has the attributes of the decorated function. |
A utility class for decorators that sets or gets a value to/from a decorated function. Attributes of instances of this class can be get, set, or deleted, and those attributes are associated with the decorated function.
The object to which the data is attached is either the function itself for non-method, or the instance object for methods.
There are two possible perspectives of using the data store: from inside the decorator, and from outside the decorator. This allows, for example, a method to access data stored by one of its decorators.
This class represents a weak reference based on the python module weakref. The difference between weakref.ref and this class is that you can access the ref without calling it first. E.g.: foo = weak(bar) and bar has the attribute x. With a normal weakref.ref you need to call foo().x to get, this class makes it possible to just use foo.x.
All functions are passed to the real object behind the weakref. To check if the weakref is alive or not, you can compare the object with None. Do not use a simple if, because an object still alive can also be False (e.g. an empty list).
The kaa.ioctl module provides functions for the C-level ioctl macros that are defined in /usr/include/asm-generic/ioctl.h used for creating and decoding ioctl numbers.