"""
This file handles all things configuration i.e even the parts of the configuration we are interested in
"""
import os
import json

def get (path) :
    #
    # Opens the manifest file and returns the configuration
    if os.path.exists(path) :
        f = open(path) 
        _conf = json.loads(f.read())
        f.close()
    else:
        _conf = {}
    return _conf
def get_manifest(manifest):
    #
    # returns the manifest file (absolute path) for a site
    if not manifest.endswith('json') and os.path.isdir(manifest):
        manifest = manifest if not manifest.endswith(os.sep) else os.sep.join(manifest.split(os.sep)[:-1])
        return os.sep.join([manifest,'qcms-manifest.json'])
    else:
        return manifest

def _isvalid(_allowed,**_args):
    
    if not list(set(_allowed) - set(_args.keys())) :
        _pargs = {}
        for key in _allowed :
            _pargs [key] = _args[key]
        return _pargs
    return False
 
def write(config, path):
    f = open(path,'w')
    f.write( json.dumps(config,indent=4)) ;
    f.close()

