Skip to main content

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

Take care to not introduce cyclic dependencies between tokens! The topological sort is handled by Eric V. Smith toposort Python package (Link).

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tokensjar-0.2.0.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tokensjar-0.2.0-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file tokensjar-0.2.0.tar.gz.

File metadata

  • Download URL: tokensjar-0.2.0.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.8

File hashes

Hashes for tokensjar-0.2.0.tar.gz
Algorithm Hash digest
SHA256 467cf1527614e90c86da487560ca510d675e0ffe8392c39a1da9575678089e10
MD5 f4d784f541a6f13bc2814310296fa624
BLAKE2b-256 50265d6cf464f298301750f857b3cf964a211d591286c01089c232cb9a4025b7

See more details on using hashes here.

File details

Details for the file tokensjar-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: tokensjar-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 16.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.8

File hashes

Hashes for tokensjar-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ec8e8c14b14a03f1f1e99cc8ce6734d2f701edb6d58f7e17ad58011928a68871
MD5 f2200db908fcce07cb17c76ce4f6f769
BLAKE2b-256 e2e4becf8d173263c61ad17472d38d6bed0515c4a6856923f7e8f6167f96a547

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page