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

: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


Download files

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

Source Distribution

tokensjar-1.0.0.tar.gz (16.9 kB view details)

Uploaded Source

Built Distribution

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

tokensjar-1.0.0-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tokensjar-1.0.0.tar.gz
  • Upload date:
  • Size: 16.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.6

File hashes

Hashes for tokensjar-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0003f37f2fbd025f12a45e720d790b9588225e11bd51419868759c7b89031ace
MD5 5467513992d4c402b219f740b472be71
BLAKE2b-256 cc0a5d4b2241d0a3c0bea5f1d8c1e3261d05a4c4ffc5cec5dd04480350c291e5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tokensjar-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 16.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.6

File hashes

Hashes for tokensjar-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 037a880c775f82182d6073dd45f9d2cb72b5a8ff8a476dea8b2d2c1e2515fe91
MD5 9c6afaccfe405a1cca4eb54bd065053f
BLAKE2b-256 d681fe130659ed048c14d36c428dd54d9c48124ac75f375b49019e83deadf916

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