A micro token interpretor/manipulator for Python
Project description
tokensjar
tokensjar is a pure Python module dedicated to generate, manipulate and interpret expressions following environment variables creation rules. This module is really useful if you need to develop a tool that can prepare the session environment variables for a specific usage.
Installation
tokensjar is available on PyPi
python -m pip install tokensjar
Concept
A token must be considered as a simple variable where we can attach one value (a raw value) or multiple values (append or prepend). The main goal is to manipulate tokens with the same strategies than environment variables.
Examples
Add values to the tokens
There are three methods to prepare the TokensJar:
add_raw_value: Define the value for the token. Only the last value added will be kept.add_prepend_value: Allow to prepend a string to the token value. Each value will be separated by the OS standard separator (':' on Unix, ';' on Windows).add_append_value: Allow to append a string to the token value. Each value will be separated by the OS standard separator (':' on Unix, ';' on Windows).
import os
from tokensjar import TokensJar
jar = TokensJar(init_tokens={
'PATH': os.environ['PATH'],
'LD_LIBRARY_PATH': os.environ['LD_LIBRARY_PATH']})
jar.add_raw_value('HELLO', 'Hello World!')
print(jar.tokens_interpreted['HELLO'])
# Output: HelloWorld!
jar.add_prepend_value('PATH', 'MyPrependPath')
print(jar.tokens_interpreted['PATH'])
# Output: MyPrependPath:[...environment PATHs...]
jar.add_append_value('LD_LIBRARY_PATH', 'MyAppendPath')
print(jar.tokens_interpreted['LD_LIBRARY_PATH'])
# Output: [...environment LD_LIBRARY_PATHs...]:MyAppendPath
Interpret expressions
The strength of the TokensJar is the interpretation engine behind. You can add values that refers to other tokens as much as you want. An interpret method and tokens_interpreted property are exposed to compute/retrieve values.
from tokensjar import TokensJar
jar = TokensJar()
jar.add_raw_value('T1', 'Token1')
jar.add_raw_value('T2', 'Token2')
jar.add_raw_value('T12', '$(T1)/$(T2)') # Note the usage of the syntax $()
jar.add_raw_value('HELLO': 'Hello: ')
print(jar.tokens_interpreted['T12'])
# Output: Token1/Token2
result = jar.interpret('Joe said: $(HELLO) $(T12)')
# Output: Joe said: Hello: Token1/Token2
:warning: Take care to not introduce cyclic dependencies between tokens!
from tokensjar import TokensJar
jar = TokensJar()
jar.add_raw_value('T1', '$(T2)')
jar.add_raw_value('T2', '$(T1)')
jar.interpret('$(T1)')
# Raise toposort EXCEPTION: Cyclic dependency detected!
Contributions
Feel free to open feature requests, issues or pull requests. Your help and vision are welcome!
Tests
Tests are written following unittest framework. Some dependencies are needed (test-requirements.txt). If you want to run tests, enter the following command at the root level of the package:
python -m unittest discover
Build the project
To perform the build, you need to install dist-requirements.txt list of packages, then run the following command. All the files will be located in dist directory.
python -m build
Upload the project
The project can be uploaded using twine (listed in dist-requirements.txt) by running the following command:
python -m twine upload dist/*
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tokensjar-1.1.0.tar.gz.
File metadata
- Download URL: tokensjar-1.1.0.tar.gz
- Upload date:
- Size: 17.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
239560ded9dfe0556d7f4c09f56e70655e3a428525c3fb9c248d71305fa12549
|
|
| MD5 |
19d5cb9d65d726793c301eeebf3b185c
|
|
| BLAKE2b-256 |
b95e29c4291dcecfabb0fe8c3e532aff6c3f5607a44d04a51a767201c1514e3b
|
File details
Details for the file tokensjar-1.1.0-py3-none-any.whl.
File metadata
- Download URL: tokensjar-1.1.0-py3-none-any.whl
- Upload date:
- Size: 16.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c66317730b2f0af9747bc0c82850618f004a21d150912e65b964ca21d52206ae
|
|
| MD5 |
9b3fa6610740be1756616ce8dfb22a48
|
|
| BLAKE2b-256 |
c763bc6b129917cf80b0e118c29ef449ffbc558493b147ee82919863f5403e67
|