-
Notifications
You must be signed in to change notification settings - Fork 9
/
cli.py
98 lines (86 loc) · 3.52 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2015 jaidev <jaidev@newton>
#
# Distributed under terms of the BSD 3 clause license.
"""semantic
Usage:
semantic list [--project=<PROJECT_NAME>]
semantic add PROJECT_NAME PROJECT_SPECFILE
semantic remove PROJECT_NAME [--dataset=<dname>]
semantic set-schema PROJECT_NAME SCHEMA_FPATH
semantic set-specs PROJECT_NAME --dataset=<dname> [--path=<pth>] [--dlm=<sep>]
semantic add-dataset DATASET_NAME --project=<pname> --path=<pth> --dlm=<sep>
semantic export PROJECT_NAME [--dataset=<dname>] OUTPATH
Options:
-h --help Show this screen
-d --dataset=<dname> Name of the dataset to modify
--path=<pth> Path to a dataset
--dlm=<sep> Declare the delimiter for a dataset
-p --project=<pname> Name of the project to modify
-v --version Print the version of PySemantic
"""
import os.path as op
from docopt import docopt
from pysemantic import project as pr
from pysemantic.errors import MissingProject
def cli(arguments):
"""cli - The main CLI argument parser.
:param arguments: command line arguments, as parsed by docopt
:type arguments: dict
:return: None
"""
if arguments.get("list", False):
if arguments['--project'] is None:
pr.view_projects()
else:
proj_name = arguments.get('--project')
dataset_names = pr.get_datasets(proj_name)
for name in dataset_names:
print name
elif arguments.get("add", False):
proj_name = arguments.get("PROJECT_NAME")
proj_spec = arguments.get("PROJECT_SPECFILE")
proj_spec = op.abspath(proj_spec)
pr.add_project(proj_name, proj_spec)
elif arguments.get("remove", False):
proj_name = arguments.get("PROJECT_NAME")
if arguments['--dataset'] is None:
if not pr.remove_project(proj_name):
print "The project {0} doesn't exist.".format(proj_name)
else:
pr.remove_dataset(proj_name, arguments['--dataset'])
elif arguments.get("set-schema", False):
try:
proj_name = arguments.get("PROJECT_NAME")
proj_spec = arguments.get("SCHEMA_FPATH")
proj_spec = op.abspath(proj_spec)
pr.set_schema_fpath(proj_name, proj_spec)
except MissingProject:
msg = """Project {} not found in the configuration. Please use
$ semantic add
to register the project.""".format(arguments.get("PROJECT_NAME"))
print msg
elif arguments.get("set-specs", False):
proj_name = arguments.get("PROJECT_NAME")
dataset_name = arguments.get("--dataset")
newspecs = {}
if arguments.get("--path", False):
newspecs['path'] = arguments.get("--path")
if arguments.get("--dlm", False):
newspecs['delimiter'] = arguments.get("--dlm")
pr.set_schema_specs(proj_name, dataset_name, **newspecs)
elif arguments.get("add-dataset", False):
proj_name = arguments.get('--project')
dataset_name = arguments.get("DATASET_NAME")
specs = dict(path=arguments["--path"], delimiter=arguments["--dlm"])
pr.add_dataset(proj_name, dataset_name, specs)
elif arguments.get("export", False):
project = pr.Project(arguments.get("PROJECT_NAME"))
project.export_dataset(arguments.get("--dataset"),
outpath=arguments.get("OUTPATH"))
def main():
arguments = docopt(__doc__, version="semantic v0.1.1")
cli(arguments)