forked from phorward/viur-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
csvimport.py
192 lines (139 loc) · 5.61 KB
/
csvimport.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# -*- coding: utf-8 -*-
import re, json, csv, requests, sys, codecs, argparse, logging, os, logics
root = logging.getLogger()
root.setLevel(logging.INFO)
sys.stdout = codecs.getwriter("utf8")(sys.stdout)
class Importer(requests.Session):
def __init__(self, host, username=None, password=None, loginKey=None, render="json"):
super(Importer, self).__init__()
self.render = render
self.host = host
self.username = username
self.password = password
self.loginKey = loginKey
assert (self.username and self.password) or self.loginKey
if not self.login():
raise IOError("Unable to logon to '%s'" % self.host)
def login(self):
if self.username and self.password:
answ = self.post(
"/user/auth_userpassword/login",
data={
"name": self.username,
"password": self.password,
"skey": self.skey()
},
timeout=30)
else:
answ = self.post(
"/user/auth_loginkey/login",
data={
"key": self.loginKey,
"skey": self.skey()
},
timeout=30)
if not answ.status_code == 200:
logging.error("Unable to logon to '%s'" % self.host)
return False
res = re.search(r"JSON\(\((.*)\)\)", answ.text)
if res:
res = json.loads(res.group(1))
else:
res = answ.json()
assert res == "OKAY"
logging.debug("HELLO %s!" % self.host)
return True
def logout(self):
return self.get("/user/logout", params={"skey": self.skey()}, timeout=10).json()
def skey(self):
return self.get("/skey", timeout=15).json()
def get(self, url, *args, **kwargs):
if url.startswith("/"):
url = url[1:]
logging.debug("GET %s %s" % ("/".join([self.host, self.render, url]), kwargs))
return super(Importer, self).get("/".join([self.host, self.render, url]), *args, **kwargs)
def post(self, url, *args, **kwargs):
if url.startswith("/"):
url = url[1:]
logging.debug("POST %s %s" % ("/".join([self.host, self.render, url]), kwargs))
return super(Importer, self).post("/".join([self.host, self.render, url]), *args, **kwargs)
def list(self, module, *args, **kwargs):
req = self.get("/%s/list" % module, params=kwargs, timeout=60)
if not req.status_code == 200:
logging.error("Error %d, unable to fetch items" % req.status_code)
return None
return req.json()
def logic_lookup(self, module, field, value, result = "key"):
ret = self.list(module, **{field: value, "amount": 1})
if ret["skellist"]:
return ret["skellist"][0][result]
return None
if __name__ == "__main__":
ap = argparse.ArgumentParser(description="csv2viur - Generic CSV importer for ViUR.")
ap.add_argument("filename", type=str, help="The CSV-File to be imported")
ap.add_argument("-D", "--debug", action="store_true", help="Set debug log level")
ap.add_argument("-d", "--delimiter", default=";", type=str, help="Value delimiter in CSV-file")
ap.add_argument("-m", "--module", type=str, help="Module to import to (is retrieved by filename else)")
ap.add_argument("-U", "--update", action="store_true", dest="updateEntry", help="Update entries which already exist")
ap.add_argument("-k", "--key-column", metavar="key-column", dest="keyColumn", type=str, help="Name of the key column")
ap.add_argument("-c", "--connect", required=True, metavar="HOST", type=str, help="URL to ViUR application host")
ap.add_argument("-u", "--username", type=str, help="Username")
ap.add_argument("-p", "--password", type=str, help="Password")
ap.add_argument("-l", "--loginkey", type=str, help="LoginKey")
ap.add_argument("-e", "--expression", metavar=("column", "expression"), action="append", nargs=2, type=str,
help="Define additional field expression")
args = ap.parse_args()
#print(args)
imp = Importer(args.connect, args.username, args.password, args.loginkey, render="vi")
if not args.module:
args.module = os.path.splitext(args.filename)[0]
reader = csv.DictReader(open(args.filename, "r"), delimiter=args.delimiter)
added = 0
updated = 0
vil = logics.Interpreter()
#vil.functions["lookup"] = logics.Function(imp.logic_lookup, None)
#vil.functions["csvlookup"] = logics.Function(imp.logic_lookup, None)
rules = {}
for row in reader:
key = None
if args.expression:
for i, expr in enumerate(args.expression):
#print(expr[0], expr[1], row.get(expr[0]))
field = expr[0]
if field not in rules:
rules[(field, i)] = vil.compile(expr[1])
row[field] = vil.execute(rules[(field, i)], row)
#print("=", row[field])
if args.keyColumn:
if args.keyColumn not in reader.fieldnames:
logging.error("Key field '%s' does not exist in %s." % (args.keyColumn, args.filename))
sys.exit(1)
answ = imp.list(args.module, **{args.keyColumn: row[args.keyColumn]})
if len(answ["skellist"]) == 1:
key = answ["skellist"][0]["key"]
if not args.updateEntry:
logging.info("Entry %s exists and will not be updated." % row[args.keyColumn])
continue
elif len(answ["skellist"]) > 1:
logging.error("Multiple matches on '%s'? IMPOSSIBLE!!" % row[args.keyColumn])
continue
row["skey"] = imp.skey()
if key:
row["key"] = key
answ = imp.post(args.module + "/edit", data=row).json()
if answ["action"] != "editSuccess":
logging.error(
"%s/edit/%s failed with errors: %s", args.module, key,
", ".join([("%s=%s: %s" % (bone, answ["values"][bone], struct["error"]))
for bone, struct in answ["structure"] if struct["error"]])
)
continue
logging.info("%r updated successfully", key)
updated += 1
else:
answ = imp.post(args.module + "/add", data=row).json()
if answ["action"] == "addSuccess":
logging.info("%r created successfully", answ["values"]["key"])
added += 1
#break
logging.info("%d added, %d updated", added, updated)