Example #1
0
def _addDatabankEntries_py27(dbname, dict_entries, verbose=True):
    ''' Add database dbname with entries from dict_entries. If database 
    already exists in ~/.radis, raises an error
    '''

    # Get ~/.radis if exists, else create it
    try:
        dbnames = getDatabankList()
    except FileNotFoundError:
        # generate ~/.radis:
        dbnames = []
        open(CONFIG_PATH, 'a').close()
        if verbose:
            print('Created ~/.radis in {0}'.format(dirname(CONFIG_PATH)))

    # Check database doesnt exist
    if dbname in dbnames:
        raise ValueError('Database already exists: {0}'.format(dbname) +
                         '. Cant add it')

    # Add entries to parser
    config = configparser.ConfigParser()
    config.add_section(dbname)  # = {}

    if 'info' in dict_entries:  # optional
        config.set(dbname, 'info', dict_entries.pop('info'))

    # ... Parse paths correctly
    if dict_entries['path'] in string_types:
        config.set(dbname, 'path', dict_entries.pop('path'))
    else:  # list
        config.set(dbname, 'path', '\n       '.join(dict_entries.pop('path')))

    config.set(dbname, 'format', dict_entries.pop('format'))
    config.set(dbname, 'parfuncfmt', dict_entries.pop('parfuncfmt'))

    # Optional:
    # ... Split all isotopes in separate keys
    levels_dict = dict_entries.pop('levels', {})
    for iso, levels_iso in levels_dict.items():
        dict_entries['levels_iso{0}'.format(iso)] = levels_iso

    if 'levelsfmt' in dict_entries:
        config.set(dbname, 'levelsfmt', dict_entries.pop('levelsfmt'))

    # Check nothing is left
    if dict_entries != {}:
        raise ValueError('Unexpected keys: {0}'.format(
            list(dict_entries.keys())))

    # Write to ~/.radis
    # ... Note: what if there is a PermissionError here? Try/except pass?
    with open(CONFIG_PATH, 'a') as configfile:
        configfile.write('\n')
        config.write(configfile)
    if verbose:
        print("Added {0} database in {1}".format(dbname, CONFIG_PATH))

    return
Example #2
0
def _addDatabankEntries_py27(dbname, dict_entries, verbose=True):
    """ Add database dbname with entries from dict_entries. If database 
    already exists in ~/.radis, raises an error
    """

    # Get ~/.radis if exists, else create it
    try:
        dbnames = getDatabankList()
    except FileNotFoundError:
        # generate ~/.radis:
        dbnames = []
        open(CONFIG_PATH, "a").close()
        if verbose:
            print("Created ~/.radis in {0}".format(dirname(CONFIG_PATH)))

    # Check database doesnt exist
    if dbname in dbnames:
        raise ValueError(
            "Database already exists: {0}".format(dbname) + ". Cant add it"
        )

    # Add entries to parser
    config = configparser.ConfigParser()
    config.add_section(dbname)  # = {}

    if "info" in dict_entries:  # optional
        config.set(dbname, "info", dict_entries.pop("info"))

    # ... Parse paths correctly
    if dict_entries["path"] in string_types:
        config.set(dbname, "path", dict_entries.pop("path"))
    else:  # list
        config.set(dbname, "path", "\n       ".join(dict_entries.pop("path")))

    config.set(dbname, "format", dict_entries.pop("format"))
    config.set(dbname, "parfuncfmt", dict_entries.pop("parfuncfmt"))

    # Optional:
    # ... Split all isotopes in separate keys
    levels_dict = dict_entries.pop("levels", {})
    for iso, levels_iso in levels_dict.items():
        dict_entries["levels_iso{0}".format(iso)] = levels_iso

    if "levelsfmt" in dict_entries:
        config.set(dbname, "levelsfmt", dict_entries.pop("levelsfmt"))

    # Check nothing is left
    if dict_entries != {}:
        raise ValueError("Unexpected keys: {0}".format(list(dict_entries.keys())))

    # Write to ~/.radis
    # ... Note: what if there is a PermissionError here? Try/except pass?
    with open(CONFIG_PATH, "a") as configfile:
        configfile.write("\n")
        config.write(configfile)
    if verbose:
        print("Added {0} database in {1}".format(dbname, CONFIG_PATH))

    return
Example #3
0
def getConfig():
    ''' Read config file and returns it

    Config file name is harcoded: `~/.radis`
    '''

    config = configparser.ConfigParser()
    configpath = CONFIG_PATH

    # Test ~/.radis exists
    if not exists(configpath):

        raise FileNotFoundError(
            "Create a `.radis` file in {0} to store links to ".format(
                dirname(configpath)) +
            "your local databanks. Format must be:\n {0}".format(DBFORMAT) +
            "\n(it can be empty too)")
    config.read(configpath)

    return config