PREFS

PREFS is a Python library that facilitate the process of store and manage user preferences, simple but useful.

How does PREFS work?

In a nutshell what PREFS does is search for a PREFS file with the given name and if not found create it with the default PREFS.

Installation:

On windows: pip install PREFS

On MacOS and Linux: pip3 install PREFS

Syntax:

Each PREFS file is an instance of the PREFS class:
PREFS class has one required parameter, which is a dictionary with the default preferences, default means the preferences that all users will have at first time.

UserPrefs = PREFS.PREFS(prefs = {
  "theme": "light", 
  "lang": "en", 
  "keybindings": {"Ctrl+C": "Copy", "Ctrl+V": "Paste", "Ctrl+X": "Cut"}
})

This code will create a file like this:

#PREFS
theme=light
lang=en
keybindings=>
  Ctrl+C=Copy
  Ctrl+V=Paste
  Ctrl+X=Cut

A total human readable file that supports cascade/tree in nested dictionaries.


The main methods to manage the preferences are are:

  • ReadPrefs(): Returns a dictionary reading the PREFS file.
  • WritePrefs(): Requires two arguments, first the name of the pref that you want to change (if pref exists) or create if it doesn't, and second argument is the value that you want to assign to the pref. If using nested dictionaries pass in key parameter the keys path separated by a forward slash, e.g.: WritePrefs("keybindings/Ctr+C", "Ctrl+D")
  • ConvertToJson(): Converts the PREFS file into a json one.

Methods outside PREFS class:

  • ReadJsonFile(): Requires a the filename of the json file to read and returns it's value.
  • ReadPREFSFile(): Given the filename of a PREFS file reads it and returns it's value.

GitHub

https://github.com/Patitotective/PREFS