mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 07:37:08 +00:00
Initial commit
This commit is contained in:
59
bin/configuration.py
Normal file
59
bin/configuration.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import os
|
||||
from glob import glob
|
||||
from yaml import full_load as _load
|
||||
|
||||
"""
|
||||
Interface to the instance configuration.
|
||||
|
||||
The instance configuration is expected to be found under
|
||||
$HOME/etc/config.yaml and the configuration for the individual
|
||||
surveys should be under $HOME/etc/surveys/*.yaml. In both cases,
|
||||
$HOME is the home directory of the user running this script.
|
||||
"""
|
||||
|
||||
prefix = os.environ.get("HOME", ".")
|
||||
|
||||
def read (file = None):
|
||||
if file is None:
|
||||
file = prefix+"/etc/config.yaml"
|
||||
with open(file, "r") as fd:
|
||||
return _load(fd)
|
||||
|
||||
def files (globspec = None, include_archived = False):
|
||||
"""
|
||||
Read and parse survey configuration files.
|
||||
|
||||
Arguments:
|
||||
|
||||
globspec (string): a glob spec matching the selection of files to read.
|
||||
If not provided, a default value will be used.
|
||||
|
||||
include_archived (bool): whether to include surveys marked as archived or
|
||||
not. Defaults to ignoring archived surveys.
|
||||
|
||||
Note that file names starting with `.` or `_` (dot or underscore) will
|
||||
never be read (and there is no option to override this). Intended for
|
||||
quickly and temporarily “disabling” a survey configuration by renaming
|
||||
the relevant file.
|
||||
"""
|
||||
tuples = []
|
||||
|
||||
if globspec is None:
|
||||
globspec = prefix+'/etc/surveys/*.yaml'
|
||||
|
||||
for filepath in glob(globspec):
|
||||
filepath = os.path.abspath(filepath)
|
||||
if os.path.basename(filepath).startswith("_") or os.path.basename(filepath).startswith("."):
|
||||
continue
|
||||
|
||||
survey = read(filepath)
|
||||
|
||||
if not include_archived and "archived" in survey and survey["archived"] is not False:
|
||||
continue
|
||||
|
||||
tuples.append((filepath, survey))
|
||||
|
||||
return tuples
|
||||
|
||||
def surveys (globspec = None, include_archived = False):
|
||||
return [i[1] for i in files(globspec, include_archived)]
|
||||
Reference in New Issue
Block a user