Utility functions and classes.
| Class | |
Subclass of Thread that captures exceptions from the target function. |
| Class | |
A BaseModel containing the information about the server version. |
| Function | make |
Replace unsafe characters in a filename or identifier with underscores. |
| Function | make |
Replace unsafe characters in a file path with underscores, preserving separators. |
| Function | requires |
Decorate a class method so that it requires the class lock to run. |
| Function | robust |
Return a version string and information on its source. |
| Constant | COMMIT |
Undocumented |
| Constant | LOGGER |
Undocumented |
| Constant | P |
Undocumented |
| Constant | REF |
Undocumented |
| Constant | REPO |
Undocumented |
| Type Variable | T |
Undocumented |
| Function | _get |
Get the commit hash from a .git directory directly. |
| Function | _get |
Get the commit hash from a ref in the .git directory. |
| Function | _get |
Get the version string from a pyproject.toml file. |
| Function | _is |
Check if an object is a lock. |
| Function | _wrap |
Run target function in a try-except block. |
| Constant | _NAME |
Undocumented |
| Constant | _POSIX |
Undocumented |
| Constant | _WINDOWS |
Undocumented |
Replace unsafe characters in a filename or identifier with underscores.
This excludes all path separators, ensuring the result is safe to use as a standalone filename or identifier component.
| Parameters | |
unsafestr | The original name string to sanitise. |
| Returns | |
str | A version of the input string safe to use as a file name or identifier. |
Replace unsafe characters in a file path with underscores, preserving separators.
This can be used to check that user inputs have not been concatenated into an unsafe filepath.
This ensures compatibility across platforms by preserving only valid characters, including path separators (forward slash on POSIX, and forward slash/backslash/colon on Windows).
| Parameters | |
unsafestr | The original path string to sanitise. |
| Returns | |
str | A version of the input string safe to use as a file path. |
Decorate a class method so that it requires the class lock to run.
The class should have a reentrant lock with the name self._lock.
Return a version string and information on its source.
Returning a python version string is not without problems. If the package is installed in an editable way, often METADATA is never updated. This provides 4 ways to provide a version string:
- If both a Git directory and a pyproject.toml are available. Return the version
- string from the toml file and the git hash from the .git directory as its source.
- If a pyproject.toml is available but no Git directory then this is likely an
- installation from source. Return the version string from the toml file and return the literal string "TOML" as its source.
- If a Git directory is available but no pyproject.toml then something is very
- weird - log an error. Then return "Undefined" as the version string, but still return the Git hash as the source.
- Finally if neither a Git directory nor a pyproject.toml are available then the server has been installed from a distribution package (i.e. a wheel). In this case use the standard library function importlib.metadata.version for the version string (prepending a "v") and return "Dist" as its source.
| Returns | |
VersionData | a VersionData instance with the version string and source. |
Get the commit hash from a .git directory directly.
This function doesn't rely on GIT being on the PATH or even installed. It doesn't require any packages outside the Python standard library. It directly inspects the .git directory to get the commit hash:
- If the repository is on a detached HEAD then the hash will be in the file
- .git/HEAD. A detached HEAD is could be from checking out a tag or checking out a commit directly.
- In normal operation .git/HEAD points to a reference or "ref". This ref file
- contains the hash.
| Parameters | |
gitstr | The path to the .git directory. |
| Returns | |
str | The hash, or "Undefined" if there is any error, errors are logged not raised. |
Get the commit hash from a ref in the .git directory.
For more detail see _get_hash_from_git_dir
| Parameters | |
gitstr | The full path to the ref file in the .git directory. |
| Returns | |
str | The hash, or "Undefined" if there is any error, errors are logged not raised. |
Get the version string from a pyproject.toml file.
| Parameters | |
tomlstr | The path to the toml file. |
| Returns | |
str | The version string directly as reported in the toml file, or "Undefined" if there is any error, errors are logged not raised. |
Check if an object is a lock.
Cannot use isinstance(obj, threading.RLock), may be possible to use isinstance(obj, threading._RLock). But this uses a private method and may break. Instead making the check that both "acquire" and "release" exist.
Run target function in a try-except block.
This function is designed only to be used by ErrorCapturingThread.
It will run a target function in a try-except block catching any exception. If an exception is caught it is added to error_buffer.
| Parameters | |
| target | The target function to call |
| error | An empty list that is used for returning the exception from the thread. It is a list to ensure it is passed by reference. |
| *args | The arguments for the target function |
| **kwargs | The Keyword arguments for the target function |