Esempio n. 1
0
def create_config():
    """Initiates process to generate configuration file."""
    file_exists = exists(CONFIG_FILE)
    if not file_exists:
        # Set directories
        config.add_section('Directories')
        for key, value in DEFAULT_PATHS.items():
            config.set('Directories', key, value)
        # Set authentication
        config.add_section('Authentication')
        prompt_key = "Please enter your API Key(s), obtained from "\
                     "http://dev.elsevier.com/myapikey.html.  Separate "\
                     "multple keys using a comma:\n"
        key = input(prompt_key)
        config.set('Authentication', 'APIKey', key)
        prompt_token = "API Keys are sufficient for most users.  If you "\
                       "have to use Authtoken authentication, please enter "\
                       "the token, otherwise press Enter: \n"
        token = input(prompt_token)
        if token:
            config.set('Authentication', 'InstToken', token)
        # Write out
        try:
            makedirs(expanduser('~/.scopus/'))
        except FileExistsError:
            pass
        with open(CONFIG_FILE, 'w') as f:
            config.write(f)
        print(f"Configuration file successfully created at {CONFIG_FILE}")
    else:
        text = f"Configuration file already exists at {CONFIG_FILE}; process "\
               "to create the file aborted.  Please open the file and edit "\
               "the entries manually."
        raise FileExistsError(text)
Esempio n. 2
0
def get_folder(api, view):
    """Auxiliary function to get the cache folder belonging to an API,
    eventually create the folder.
    """
    from configparser import NoOptionError
    from pathlib import Path

    from pybliometrics.scopus.utils import CONFIG_FILE, DEFAULT_PATHS
    from pybliometrics.scopus.utils.create_config import create_config

    if not config.has_section('Directories'):
        create_config()
    try:
        parent = Path(config.get('Directories', api))
    except NoOptionError:
        parent = DEFAULT_PATHS[api]
        config.set('Directories', api, str(parent))
        with open(CONFIG_FILE, 'w') as ouf:
            config.write(ouf)
    try:
        folder = parent/view
    except TypeError:
        folder = parent
    folder.mkdir(parents=True, exist_ok=True)
    return folder