Skip to content

Simple application configuration management using a SQLite database for key-value storage

License

Notifications You must be signed in to change notification settings

TeacupTerrapin/EZConfig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EZConfig

Quick and easy key-value store for your Configuration or State

  • EZConfig uses compact, intuitive syntax.
  • The source code should be readable and easy to follow: It was written with simplicity in mind, rather than comprehensive features.
  • You can use it in place of modules like Configparser, or manually keeping information in YAML/text files.
  • Extend it for your own use, or import it as a small library whenever you need to store State or Configuration (or both).
  • Keys can be read individually and assigned to variables, or they can be read all-at-once as a dictionary.
  • The database is SQLite. One file per EZConfig instance. (Store state and config easily)
  • Backup() method saves the current config snapshot.

Usage Examples

Create the config

mycfg = EZConfig('database_file_name.db')

The file extension db is optional, you can use no extension (e.g. 'appconfig') or another extension such as '.sqlite'

Write a config item

mycfg.write('server_name', 'localhost')

The write() method also returns the value:

server = mycfg.write('server_name','localhost')
# Variable 'server' is assigned 'localhost', so you can assign and write the config all at once

Read a config item

server = mycfg.read('server_name')
# returns the value

port = mycfg.read('port_number', value_if_null='error')
# Returns exception if the key is missing/null.

update_interval = mycfg.read('update_interval',60)
# Returns 60 as a default interval if none exists

By default a Null key will return None.

Read the entire config into a dictionary

mycfg_dict = {}
mycfg_dict = mycfg.read_all()

Delete a config item: entire key-value pair, or only value

mycfg.delete('server_name', 'value_only')
# deletes _only_ the value 'localhost' and sets the value of the key 'server_name' to Null

mycfg.delete('mykey')
# deletes the entire key, removing the row from the database entirely

Querying the config database directly with SQL

mycfg.query("SELECT key, value, modified FROM config WHERE key='server_name';")
# returns tuple ('server_name', 'localhost', 'yyyy-mm-dd HH:MM:SS')

Backup the entire config database file

mycfg.backup('mycfg_backup.db')
# saves the current config into a sqlite database called 'mycfg_backup.db'

Database file name property

If you need the name of the config database file, the property 'dbname' can be used

mydb = mycfg.dbname

Summary of methods in the EZConfig class

Method Comments
write(key, value) Value can be None
Returns the value assigned
read(key, value_if_null=None) See notes about value_if_null
read_all(output=None) This will by default return a dictionary
For debug, this can print to the console
delete(key, delete_level='row') By default will delete the row from the database
query(sql_query) DROP is not supported and will raise an exception
backup(filename) Config can be in use; uses sqlite native backup

About the Database file

The database is only one table (config). You can use the SQLite client tool sqlite3 to use SQL directly on the database:

SELECT * FROM config;

SELECT * FROM sqlite_master; # Shows the schema, including the trigger to update the 'modified' timestamp.

Timestamps & accessing the database file:

A column in the database called modified is stored in the database when the key-value pair is created, and updated each time the key is updated.

About

Simple application configuration management using a SQLite database for key-value storage

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages