Skip to content

Commit

Permalink
adding cli
Browse files Browse the repository at this point in the history
  • Loading branch information
jdagdelen committed Jul 23, 2019
1 parent 2011fb9 commit 57b7bcd
Show file tree
Hide file tree
Showing 9 changed files with 471 additions and 24 deletions.
21 changes: 0 additions & 21 deletions __init__.py

This file was deleted.

20 changes: 20 additions & 0 deletions matscholar/__init__.py
@@ -1 +1,21 @@
import os
import yaml
from matscholar.rest import Rester

SETTINGS_FILE = os.path.join(os.path.expanduser("~"), ".msclirc.yaml")

def _load_settings():
try:
with open(SETTINGS_FILE, "rt") as f:
d = yaml.safe_load(f)
except IOError:
# If there are any errors, default to using environment variables
# if present.
d = {}
for k, v in os.environ.items():
if k.startswith("MATSCHOLAR_"):
d[k] = v
return d


SETTINGS = _load_settings()
8 changes: 8 additions & 0 deletions matscholar/cli/__init__.py
@@ -0,0 +1,8 @@
# coding: utf-8
# Copyright (c) Matscholar Development Team.
# Distributed under the terms of the MIT License.

"""
This package contains the matscholar command line interface. Its entry point is defined in setup.py.
"""

124 changes: 124 additions & 0 deletions matscholar/cli/mscli.py
@@ -0,0 +1,124 @@
#!/usr/bin/env python
# Copyright (c) Matscholar Development Team.
# Distributed under the terms of the MIT License.

from __future__ import print_function, unicode_literals

import click
from PyInquirer import prompt
from matscholar.cli.mscli_config import set_config
from matscholar.collect import ScopusCollector

@click.group()
def cli():
pass

@click.command("configure")
def configure():
"""Used to configure Matscholar configuration settings."""

questions = [
{
'type': 'input',
'name': 'MATSCHOLAR_NAME',
'message': 'Whate is your full name?',
},
{
'type': 'input',
'name': 'MATSCHOLAR_TEXT_MINING_KEY',
'message': 'Enter your Scopus API text mining key '
'(obtained at https://dev.elsevier.com/apikey/manage) ',
},
{
'type': 'input',
'name': 'MATSCHOLAR_USER',
'message': 'Enter your Matscholar username',
},
{
'type': 'password',
'name': 'MATSCHOLAR_PASSWORD',
'message': 'Enter your Matscholar password',
},
]

answers = prompt(questions)
set_config(answers)

@click.command("contribute")
@click.option('--count', default=1, help='number of blocks')
def contribute(count):
"""Used to contribute data to Matscholar database."""
collector = ScopusCollector()
collector.contribute(num_blocks=count)

cli.add_command(configure)
cli.add_command(contribute)

def main():
cli()

# from matscholar.cli.mscli_config import configure_mscli
# from matscholar.cli.mscli_query import do_query
#
# def main():
# parser = argparse.ArgumentParser(description="""
# Welcome to mscli (The materials scholar command line interface)
# This script works based on several sub-commands with their own options.
# To see the options for the sub-commands, type "mscli sub-command -h".""")
#
# subparsers = parser.add_subparsers()
#
# parser_config = subparsers.add_parser(
# "set", help="Tool for modifying config variables in .msclirc.yaml configuration file.")
# groups = parser_config.add_mutually_exclusive_group(required=True)
# groups.add_argument("-a", "--add", dest="var_spec", nargs="+",
# help="Variables to add in the form of space "
# "separated key value pairs. E.g., "
# "MATSCHOLAR_TEXT_MINING_KEY <api key>")
# parser_config.set_defaults(func=configure_mscli)
#
# #TODO: add query on Rester to CLI
#
#
# parser_collect = subparsers.add_parser(
# "collect",
# help="Collect data from the Scopus API and add to the Materials Scholar Database. "
# "(For internal use by Matscholar collaborators.)")
#
# group = parser_collect.add_mutually_exclusive_group(required=False)
# group.add_argument(
# "-k", "--key", dest="api_key", metavar="format",
# choices=["poscar", "cif", "cssr"], type=str.lower,
# help="Get structures from Materials Project and write them to a "
# "specified format.")
# group.add_argument(
# "-e", "--entries", dest="entries", metavar="filename",
# help="Get entries from Materials Project and write them to "
# "serialization file. JSON and YAML supported.")
# group.add_argument(
# "-d", "--data", dest="data", metavar="fields", nargs="*",
# help="Print a summary of entries in the Materials Project satisfying "
# "the criteria. Supply field names to include additional data. "
# "By default, the Materials Project id, formula, spacegroup, "
# "energy per atom, energy above hull are shown.")
# parser_query.set_defaults(func=do_query)
#
# try:
# import argcomplete
# argcomplete.autocomplete(parser)
# except ImportError:
# # argcomplete not present.
# pass
#
# args = parser.parse_args()
#
# try:
# getattr(args, "func")
# except AttributeError:
# parser.print_help()
# sys.exit(0)
# args.func(args)


if __name__ == "__main__":
main()
42 changes: 42 additions & 0 deletions matscholar/cli/mscli_config.py
@@ -0,0 +1,42 @@
#!/usr/bin/env python
# coding: utf-8
# Copyright (c) Matscholar Development Team.
# Distributed under the terms of the MIT License.

import os
import sys
import shutil
from monty.serialization import loadfn, dumpfn

from matscholar import SETTINGS_FILE


def add_config_var(args):
d = {}
if os.path.exists(SETTINGS_FILE):
shutil.copy(SETTINGS_FILE, SETTINGS_FILE + ".bak")
print("Existing %s backed up to %s"
% (SETTINGS_FILE, SETTINGS_FILE + ".bak"))
d = loadfn(SETTINGS_FILE)
toks = args.var_spec
if len(toks) % 2 != 0:
print("Bad variable specification!")
sys.exit(-1)
for i in range(int(len(toks) / 2)):
d[toks[2 * i]] = toks[2 * i + 1]
dumpfn(d, SETTINGS_FILE, default_flow_style=False)
print("New %s written!" % (SETTINGS_FILE))


def set_config(config):
if os.path.exists(SETTINGS_FILE):
shutil.copy(SETTINGS_FILE, SETTINGS_FILE + ".bak")
print("Existing %s backed up to %s"
% (SETTINGS_FILE, SETTINGS_FILE + ".bak"))
dumpfn(config, SETTINGS_FILE, default_flow_style=False)
print("New %s written!" % (SETTINGS_FILE))


def configure_mscli(args):
if args.var_spec:
add_config_var(args)
19 changes: 19 additions & 0 deletions matscholar/cli/mscli_query.py
@@ -0,0 +1,19 @@
# coding: utf-8
# Copyright (c) Materials Intelligence.
# Distributed under the terms of the BSD License.


"""
Run a query on the Materials Scholar Rest API
"""

from matscholar import Rester
import json
from monty.serialization import dumpfn
import re
from tabulate import tabulate


def do_query(args):
#TODO: Implement this.
pass

0 comments on commit 57b7bcd

Please sign in to comment.