confini

Parse and merge multiple ini files in python3
git clone git://git.defalsify.org/python-confini.git
Log | Files | Refs | README | LICENSE

README.md (2473B)


      1 # CONFINI
      2 
      3 Configuration parser that process all sections and values in all `ini` files in a directory.
      4 
      5 ## Usage
      6 
      7 ``` 
      8 import confini
      9 
     10 c = confini.Config('/path/to/config/dir')
     11 c.process()
     12 
     13 print(c.get('FOO_BAR_BAZ'))
     14 
     15 ```
     16 
     17 ### Value storage
     18 
     19 The values are stored in a single key/value dictionary, with section and name separated by _underscore_ and all letters transformed to uppercase.
     20 
     21 Consider this value in an ini section:
     22 
     23 ```
     24 [foO]
     25 bar_baz = 42
     26 ```
     27 
     28 This will be stored in the `confini` store with `FOO_BAR_BAZ` as the key.
     29 
     30 ### Environment overrides
     31 
     32 By default, the value of any environment variable matching a store key will overwrite the corresponding value in the store.
     33 
     34 A prefix can be provided on instantiation to define a separate namespace for environment variable overrides:
     35 
     36 ```
     37 >>> os.environ.set('FOO_BAZ_BAZ', 666)
     38 >>> c = config.Config('/path/to/config/dir')
     39 >>> c.process()
     40 >>> print(c.get('FOO_BAR_BAZ'))
     41 666
     42 >>> c = config.Config('/path/to/config/dir', 'XXX')
     43 >>> c.process()
     44 >>> print(c.get('FOO_BAR_BAZ'))
     45 42
     46 >>> os.environ.set('XXX_FOO_BAZ_BAZ', 13)
     47 >>> c = config.Config('/path/to/config/dir', 'XXX')
     48 >>> c.process()
     49 >>> print(c.get('FOO_BAR_BAZ'))
     50 13
     51 ```
     52 
     53 ### Required values
     54 
     55 Keys can be set as required, and after processing independently validated:
     56 
     57 ```
     58 >>> c = config.Config('/path/to/config/dir')
     59 >>> c.require('BAR_BAZ', 'FOO')
     60 >>> c.process()
     61 >>> c.validate()
     62 True
     63 >>> c = config.Config('/path/to/config/dir')
     64 >>> c.require('BAR_BAZ_BAZ', 'FOO')
     65 >>> c.process()
     66 >>> c.validate()
     67 False
     68 ```
     69 
     70 ### Censoring logs
     71 
     72 The string representation of the confini object is a list of all stored values, one on each line.
     73 
     74 Display of individual values can be suppressed:
     75 
     76 ```
     77 >>> c = config.Config('/path/to/config/dir')
     78 >>> c.process()
     79 >>> print(c)
     80 FOO_BAR_BAZ = 666
     81 >>> c.censor('BAR_BAZ', 'FOO')
     82 >>> print(c)
     83 ***
     84 ```
     85 
     86 ### Encryption
     87 
     88 Values can be **GNUPG** encrypted by saving them in individual encrypted files providing the filename as value argument wrapped in a gpg directve:
     89 
     90 ```
     91 [foo]
     92 BAR_BAZ = !gpg(foo_bar_baz.asc)
     93 ```
     94 
     95 Decryption mode is on by default, and can be deactivated on instantiation:
     96 
     97 ```
     98 >>> c = config.Config('/path/to/config/dir')
     99 >>> c.process()
    100 >>> c.get()
    101 666
    102 >>> c = config.Config('/path/to/config/dir', decrypt=False)
    103 >>> c.process()
    104 >>> c.get()
    105 !gpg(foo_bar_baz.asc)
    106 ```
    107 
    108 The user keyring in the default location is used for decryption, which may be overridden as usual with the `GNUPGHOME` environment variable.
    109 
    110