-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load settings from a configuration file
This commit allows settings to be loaded from a configuration file. If one exists at `$XDG_CONFIG_HOME/screenplain/screenplain.cfg` (by default), it will be loaded. A new `-c`/`--config` command line flag allows loading a configuration file explicitly. Command line options take precedence over the settings loaded from the configuration file. The format of the file is INI, with no interpolations, options without values are allowed, and double-bracketed "sub-sections" (aesthetics only, the hierarchy of such sections in relation with "root-level" sections is not enforced). An example of a configuration file would be: ```ini [export] format: pdf [[pdf]] strong: no [[html]] base: no css ```
- Loading branch information
Showing
2 changed files
with
98 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import os | ||
import re | ||
import json | ||
import configparser | ||
|
||
|
||
class Defaults: | ||
FILENAME_CONFIG = 'screenplain.cfg' | ||
PATH_CONFIG = os.path.join( | ||
os.getenv('XDG_CONFIG_HOME') | ||
or os.path.join(os.getenv('HOME'), '.config'), | ||
'screenplain', | ||
FILENAME_CONFIG, | ||
) | ||
|
||
|
||
class ConfigurationFileError(Exception): pass | ||
|
||
|
||
class ConfigurationFile(configparser.ConfigParser): | ||
def __init__(self, path=Defaults.PATH_CONFIG): | ||
super().__init__(interpolation=None, | ||
allow_no_value=True, | ||
converters={ | ||
'list': self.__getlist, | ||
}) | ||
|
||
# Allow brackets in section names | ||
self.SECTCRE = re.compile(r'^[ \t\r\f\v]*\[(?P<header>.+?)\][ \t\r\f\v]*$') | ||
|
||
# Initialize sections and their expected values | ||
self.read_string(""" | ||
[export] | ||
format | ||
[[pdf]] | ||
strong: no | ||
font | ||
[[html]] | ||
base: no | ||
css | ||
[font] | ||
""") | ||
|
||
try: | ||
self.read(path) | ||
except configparser.Error as e: | ||
raise ConfigurationFileError('unable to load configuration file: %s' % e) | ||
|
||
def __getlist(self, v): | ||
try: | ||
v = json.loads(v) | ||
except json.JSONDecodeError as e: | ||
raise ConfigurationFileError('unable to decode JSON value: %s' % e) | ||
|
||
if not isinstance(v, list): | ||
raise ConfigurationFileError('value is not a list: %s' % type(v)) | ||
|
||
return v |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters