-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmwcli.py
76 lines (63 loc) · 2.15 KB
/
smwcli.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
# -*- coding: utf-8 -*-
import argparse
import pysemanticmw as pysmw
config = {
"apiPoint": "https://semantic-mediawiki.org/w/api.php",
}
def runQuery(smw, action, query, indent):
if query is not None:
query = args.query
else:
query = input("Insert the query: ")
if action == "ask":
if indent is not None:
ask = smw.ask(query, indent)
print(ask)
else:
ask = smw.ask(query)
print(ask)
elif action == "askargs":
if indent is not None:
ask = smw.askArgs(query, indent)
print(ask)
else:
ask = smw.askArgs(query)
print(ask)
def main():
cli = {
"desc": """Command-line interface to access to the API of a Semantic
MediaWiki installation. Pending to document...""",
"api": "access url to the API (e.g. https://semantic-mediawiki.org/w/api.php)",
"ask": "make a query in ask-language",
"askArgs": "TO DO",
"query": "TO DO",
"indent": "number of indents to prettify the JSON result"
}
parser = argparse.ArgumentParser(
description=cli["desc"]
)
parser.add_argument("-A", "--api", help=cli["api"])
parser.add_argument("ask", nargs="?", help=cli["ask"])
parser.add_argument("askArgs", nargs="?", help=cli["ask"])
parser.add_argument("-q", "--query", help=cli["query"])
parser.add_argument("-i", "--indent", help=cli["indent"])
args = parser.parse_args()
indent = args.indent
query = args.query
if args.api:
smw = pysmw.SemanticMediaWiki(args.api)
elif config["apiPoint"] is not "":
smw = pysmw.SemanticMediaWiki(config["apiPoint"])
else:
apiPoint = input("Insert the API URL of the Semantic MediaWiki you want to read: ")
smw = pysmw.SemanticMediaWiki(apiPoint)
if args.ask:
action = "ask"
runQuery(smw, action, query, indent)
elif args.askArgs:
action = "askargs"
runquery(smw, action, query, indent)
else:
print("You must choose an action. Type -h to check the positional arguments")
if __name__ == '__main__':
main()