Skip to main content

aridity

DRY config and template system, easily extensible with Python

The Arid Manifesto

  • Keys are paths to avoid concatenation
  • It's never necessary to repeat a value
  • Minimal syntax for surprise-free authoring
  • Evaluation lazy and influenced by context
  • Strongly (dynamically) typed values
  • Central defaulting rather than at call sites
  • Templating using same syntax as expressions
  • Easy to correctly quote/escape values in templates
  • Extensibility via user-defined functions
  • Easy tasks are easy, hard tasks are possible
  • Many applications can share one user config
  • Principle of least astonishment driven design
  • Don't make users jump through hoops

Motivation

  • Environment variables are too crude to configure non-trivial apps, and maybe even trivial apps in the cloud
    • They do not support nested data or lists, without some encoding scheme implemented in app code or a lib
    • Multiple bespoke encoding schemes in the system are an error-prone maintenance burden worth avoiding
  • Testing code that queries the environment directly comes with a big risk of leaking state between tests
  • Often tools/libraries must be configured using config files
    • Support for config file interpolation is not enough to stay DRY, and comes with a different set of gotchas per tool
    • In particular Helm/Terraform have their own ways of sharing config between envs
  • aridity is a general purpose solution for all the above, also see soak

Config API

  • Normally you pass around a Config object, and application code can get data out via attribute access e.g. config.foo.bar
    • Here config.foo is also a Config object, a child scope of config named foo
    • The passing around can be taken care of by a dependency injection container such as diapyr
  • Every Config has an associated ConfigCtrl on which Python API such as processtemplate is available
    • Use negation to get ConfigCtrl when you have a Config e.g. (-config).processtemplate(...)
    • Use the node attribute to get Config when you have a ConfigCtrl, this is a rare situation in practice
  • When unit testing a class or function that expects a Config object, you can use SimpleNamespace to mock one

Guidelines

  • Config files have the extension .arid and templates .aridt
  • A template is simply a file-sized aridity expression
    • Conventionally the template processor sets " to the appropriate quote function for the file format, e.g. jsonquote for JSON/YAML
  • Instead of adding Python objects to the config in main, it's tidier to use aridity's pyref function to achieve this
  • When some value needs to be constructed using concatenation, consider whether it would be more tasteful to do this in the config

Feature switch

  • Sometimes we want to deploy a change, but something in production isn't ready for that change
  • A feature switch allows deployment to production in this case
  • Add a boolean to the base config (conventionally root.arid) e.g. foo enabled = true
    • This value should be the configuration that we eventually want in all environments
  • In production config, override with foo enabled = false
  • In the code, read config.foo.enabled and enable the change based on this boolean
  • The above can now be deployed to all environments, and is not a blocker for other changes
  • Later when production is ready for it, it's a 1 line change to remove the override from production config

Install

These are generic installation instructions.

To use, permanently

The quickest way to get started is to install the current release from PyPI:

pip3 install --user aridity

To use, temporarily

If you prefer to keep .local clean, install to a virtualenv:

python3 -m venv venvname
venvname/bin/pip install aridity
. venvname/bin/activate

Config file syntax

: Until aridity gets support for comments, you can use the colon directive to ignore data.
: Directives MUST be separated from data by whitespace, and are typically punctuation.

: Here's the equals directive:
foo = bar
: This does what you'd expect - assign the string value bar to foo.
: Observe that bar isn't quoted, values in aridity are normally barewords.
: foo is actually a path of length 1, path components are whitespace-separated:
this is a path = this is a value
: Any existing assignment can be overridden:
foo = baz
this is a path = this is different

: Internal whitespace in values is preserved (leading and trailing whitespace is not):
two sentences = Some like 2 spaces.  After a full stop.

: You can use indentation to avoid typing a common path prefix multiple times:
app1 feature1
    data1 = value1
    data2 = value2
app2
    feature1 data = value3
    feature2
        data1 = value4
        data2 = value5
: Exactly the same effect without using indentation:
app1 feature1 data1 = value1
app1 feature1 data2 = value2
app2 feature1 data = value3
app2 feature2 data1 = value4
app2 feature2 data2 = value5

: The right hand side of an equals is actually an expression.
: In an expression, a dollar sign with brackets can be used to refer to another path:
has value
    bar = $(foo)
    value3 = $(app2 feature1 data)
: Round brackets and square brackets have exactly the same effect:
also has value bar = $[foo]
: Values can be concatenated:
two bars
    without spacing = $(foo)$(foo)
    with one space  = $(foo) $(foo)
    with 2 spaces   = $(foo)  $(foo)
: A few paths are predefined in every new context, such as:
home directory = $(~)

: To get a literal dollar there is a special form for quoting:
financial report = $'(We lost $100 on Friday.)
: Unlike in older versions, nested brackets (if any) do not end the special form early:
behaviour
    expected = $'[Lunch cost $20 (worth it though).]
    same     = $'(Lunch cost $20 (worth it though).)
: Consequently, unbalanced brackets of the same kind as used by the special form must be avoided:
interval
    lower = $'[The interval ][$'[0, 1) includes 0 but not 1.]
    upper = $'(The interval )($'(0, 1] includes 1 but not 0.)

: Another special form can be used to preserve leading/trailing whitespace:
padded bars = $.( $(foo) $(foo) )
: Brackets can span multiple lines:
bar per line
    without final newline = $.($(foo)
$(foo))
    with final newline = $.($(foo)
$(foo)
)

: Evaluation is lazy, the expression is what is actually (and eagerly) assigned to the path:
no problem = $(this path will get a value later)
: If your use-case demands it, you can force eager evaluation:
bar even if foo changes later := $(foo)

: When evaluating a path the local scope is examined first, then its parents if path not found:
host
    short path = nope
    guest short path = yep
    should be nope = $(short path)
    guest should be yep = $(short path)
does not work = $(short path)

: Use the dot directive to include config from another file:
. /path/to/other/config.arid
: Thus you can factor out any config that's common to multiple deployments, and override as needed.
: It's possible (but maybe not so useful) to include under a non-trivial path:
other stuff . /path/to/other/config.arid
: There is no default context for relative paths, you must set cwd up-front as inclusion is not lazy:
cwd = /path/to
. other/config.arid

: Text between dollar and open bracket (that isn't a special form) is a function name.
: A useful function predefined in every new context is the platform slash:
path = $/($(~) Desktop report.txt)
: Unlike most functions, / can also be used (less legibly) as a value:
path = $(~)$(/)Desktop$(/)report.txt
: All functions are first class objects that can be assigned and overridden in the usual ways:
slash := $(/)
/ = something else
path = $slash($(~) Desktop report.txt)

: Simple lists can be created using the plus equals convenience directive.
: Indentation means you don't have to repeat the directive for every list element:
years +=
    2018
    2019
years += 2020
: A predefined join function takes a list and a separator and does what you'd expect:
copyright = $join($(years) $.(, ))
: Observe that functions typically take values not identifiers, so you have to 'get' explicitly.
: Lists are just a special case of nested scopes, which are much more powerful:
person
    $.(The Guardians) year = 2018
    Greta year = 2019
summary = Person of the Year was $join($map($(person) $.($label() in $(year))) $.(, )).
: Here the predefined label function gives you access to the last path component of a list element.

Templates

  • A template is simply an expression in a file, that may be quite large
  • These are typically used to create config files for other languages e.g. YAML, HCL
    • Note that literal dollar signs must be quoted as above, everything else is safe
  • A processtemplate script is provided for basic processing
processtemplate app.json.aridt <config.arid >app.json
  • Conventionally the " path is set to the most useful escape function for the target format
    • Brackets can be elided in function composition e.g. $"$(key) is the same as $"($(key))

Commands

arid-config

Print given config (with optional path in config) as shell snippet.

aridity

Interactive REPL.

processtemplate

Process the given template to stdout using config from stdin.

Metadata

Release files for aridity 70

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aridity 70
File Size Uploaded
aridity-70.tar.gz 20.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aridity 70
File Interpreter ABI Platform
aridity-70-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 45.0 kB

Release files / aridity-70.tar.gz

Download URL aridity-70.tar.gz
Size 20.6 kB
Tags Source
SHA-256 checksum
How to use checksums
29acd4b89610bfaae5ffb973b23389205b31771a1643b1e5475bbb58a5bf2b65
BLAKE2b-256 checksum
How to use checksums
11858552e3a70eed18d2f861179f114f56ad3222e3916cd46454bc40ab2ead98
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.9.6 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.10.1 urllib3/1.26.15 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.9

Release files / aridity-70-py2.py3-none-any.whl

Download URL aridity-70-py2.py3-none-any.whl
Size 24.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
d07453b4f1a1cb7e4f6fb08f349af2decffdacd983e022ca6152986ac3824648
BLAKE2b-256 checksum
How to use checksums
2a8d9b51b37e08549ad65131d6bf4415fb9b9b0e330f365992d445155b57b41b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.9.6 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.10.1 urllib3/1.26.15 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.9

Release history Release notifications | RSS feed

102

2 release files

101

2 release files

100

2 release files

99

2 release files

98

2 release files

97

2 release files

96

2 release files

95

2 release files

94

2 release files

93

2 release files

92

2 release files

91

2 release files

90

2 release files

89

2 release files

88

2 release files

87

2 release files

86

2 release files

85

2 release files

84

2 release files

83

2 release files

82

2 release files

81

2 release files

80

2 release files

79

2 release files

78

2 release files

77

2 release files

76

2 release files

75

2 release files

74

2 release files

73

2 release files

72

2 release files

71

2 release files

This release

70 This release

2 release files

69

2 release files

68

2 release files

67

2 release files

66

2 release files

65

2 release files

64

2 release files

63

2 release files

62

2 release files

61

2 release files

60

2 release files

59

2 release files

58

2 release files

57

2 release files

56

2 release files

55

2 release files

54

2 release files

53

2 release files

52

2 release files

51

2 release files

50

2 release files

49

2 release files

48

2 release files

47

2 release files

46

2 release files

45

2 release files

44

2 release files

43

2 release files

42

2 release files

41

2 release files

40

2 release files

39

2 release files

38

2 release files

37

2 release files

36

2 release files

35

2 release files

34

2 release files

33

2 release files

32

2 release files

31

2 release files

30

2 release files

29

2 release files

28

2 release files

27

2 release files

26

2 release files

25

2 release files

24

2 release files

23

2 release files

22

2 release files

21

2 release files

20

2 release files

19

2 release files

18

2 release files

17

2 release files

16

2 release files

15

2 release files

14

2 release files

13

2 release files

12

2 release files

11

2 release files

10

2 release files

9

2 release files

8

2 release files

7

2 release files

6

2 release files

5

2 release files

4

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page