Skip to content

Commit c1428ed

Browse files
committed
Merge branch 'seldon-cli'
2 parents 480620b + 0f3e873 commit c1428ed

18 files changed

+3242
-4
lines changed

python/bin/seldon-cli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env python
2+
3+
import seldon.cli
4+
5+
seldon.cli.start_seldoncli()
6+

python/seldon/cli/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def start_seldoncli():
2+
import cli_main
3+
cli_main.main()
4+
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
import json
2+
import MySQLdb
3+
import getopt, argparse
4+
5+
import pprint
6+
7+
def pp(o):
8+
p = pprint.PrettyPrinter(indent=4)
9+
p.pprint(o)
10+
11+
valid_value_types = set(['double', 'string', 'date', 'text', 'int','boolean'])
12+
value_types_to_db_map = dict(double='DOUBLE', string='VARCHAR', date='DATETIME', int='INT', boolean='BOOLEAN',
13+
text='TEXT', enum='ENUM')
14+
15+
def addAttrsToDb(db, attrs, item_type):
16+
attrs.append({"name":"content_type", "value_type":["article"]})
17+
for attr in attrs:
18+
attrValType = attr['value_type']
19+
if type(attrValType) is list:
20+
attrValType = 'enum'
21+
cur = db.cursor()
22+
cur.execute("INSERT INTO ITEM_ATTR (name, type, item_type) "
23+
+ " VALUES (%s, %s, %s)", (attr['name'], value_types_to_db_map[attrValType], item_type))
24+
if attrValType is 'enum':
25+
for index,enum in enumerate(attr['value_type'], start=1):
26+
cur = db.cursor()
27+
cur.execute("SELECT attr_id FROM ITEM_ATTR WHERE NAME = %s and ITEM_TYPE = %s", (attr['name'],item_type))
28+
rows = cur.fetchall()
29+
attrId = rows[0][0]
30+
cur = db.cursor()
31+
cur.execute("INSERT INTO ITEM_ATTR_ENUM (attr_id, value_id, value_name) VALUES (%s, %s, %s)",(attrId, index, enum))
32+
cur = db.cursor()
33+
34+
cur.execute("SELECT attr_id, value_id, value_name FROM ITEM_ATTR_ENUM")
35+
rows = cur.fetchall()
36+
for row in rows:
37+
enum_attr_id = row[0]
38+
enum_value_id = row[1]
39+
enum_value_name = row[2]
40+
cur = db.cursor()
41+
cur.execute("INSERT INTO DIMENSION (item_type, attr_id, value_id) VALUES"
42+
+ " (%s, %s, %s)", (item_type, enum_attr_id, enum_value_id))
43+
44+
def doDbChecks(db):
45+
cur = db.cursor()
46+
cur.execute("SELECT COUNT(*) FROM ITEM_TYPE")
47+
rows = cur.fetchall()
48+
if rows[0][0] != 0:
49+
print "ITEM_TYPE table was not empty, it had", rows[0][0], 'rows'
50+
doExitBecauseDbNotEmpty()
51+
cur = db.cursor()
52+
cur.execute("SELECT COUNT(*) FROM ITEM_ATTR")
53+
rows = cur.fetchall()
54+
if rows[0][0] != 0:
55+
print "ITEM_ATTR table was not empty, it had", rows[0][0], 'rows'
56+
doExitBecauseDbNotEmpty()
57+
cur = db.cursor()
58+
cur.execute("SELECT COUNT(*) FROM ITEM_ATTR_ENUM")
59+
rows = cur.fetchall()
60+
if rows[0][0] !=0:
61+
print "ITEM_ATTR_ENUM table was not empty, it had", rows[0][0], 'rows'
62+
doExitBecauseDbNotEmpty()
63+
cur = db.cursor()
64+
cur.execute("SELECT COUNT(*) FROM DIMENSION")
65+
rows = cur.fetchall()
66+
if rows[0][0] !=0:
67+
print "DIMENSION table was not empty, it had", rows[0][0], 'rows'
68+
doExitBecauseDbNotEmpty()
69+
70+
71+
def doExitBecauseDbNotEmpty():
72+
print "To run this script, the relevant DB tables must be empty. Please rerun this script with the -clean option to delete these entries."
73+
exit(1)
74+
75+
def addToDb(db, types):
76+
with db:
77+
doDbChecks(db)
78+
for theType in types:
79+
cur= db.cursor()
80+
cur.execute("INSERT INTO ITEM_TYPE (type_id, name)"+
81+
" values (%s, %s)",(theType['type_id'],theType['type_name']))
82+
addAttrsToDb(db, theType['type_attrs'], theType['type_id'])
83+
84+
85+
def validateValueType(valType):
86+
theType = type(valType)
87+
if theType is list:
88+
for enum in valType:
89+
theEnumType = type(enum)
90+
if theEnumType is not unicode and theEnumType is not str:
91+
print "enum objects must be strings:", theEnumType
92+
exit(1)
93+
elif theType is unicode:
94+
if valType not in valid_value_types:
95+
print "the value type must be one of 'double', 'string', 'date' or an object"
96+
exit(1)
97+
else:
98+
print "the type of the field value_type must be a string or a list where as it was",theType
99+
exit(1)
100+
101+
def validateAttr(theAttr):
102+
if 'name' not in theAttr or 'value_type' not in theAttr:
103+
print "couldn't find one of (name, value_type) for attr "
104+
pp(theAttr)
105+
exit(1)
106+
else:
107+
validateValueType(theAttr['value_type']);
108+
109+
def validateType(theType):
110+
if 'type_id' not in theType or 'type_name' not in theType or 'type_attrs' not in theType:
111+
print "couldn't find one of (type_id, type_name, type_attrs) for object"
112+
pp(theType)
113+
exit(1)
114+
for theAttr in theType['type_attrs']:
115+
validateAttr(theAttr)
116+
117+
def validateNumbering(types):
118+
ids = set()
119+
for theType in types:
120+
if isinstance(theType['type_id'], int):
121+
if theType['type_id'] in ids:
122+
print "found a repeated type_id", theType['type_id']
123+
exit(1)
124+
else:
125+
ids.add(theType['type_id'])
126+
else:
127+
print "type_id s must be integers but one was","\"",theType['type_id'],"\""
128+
exit(1)
129+
130+
def outputDimensionsToFile(file, db):
131+
132+
with db:
133+
cur = db.cursor()
134+
cur.execute("SELECT d.dim_id, e.value_name from DIMENSION d, ITEM_ATTR_ENUM e where d.attr_id = e.attr_id and d.value_id = e.value_id and e.value_name != \'article\'")
135+
rows = cur.fetchall()
136+
json.dump(rows, file)
137+
138+
def readTypes(types):
139+
for theType in types:
140+
validateType(theType)
141+
validateNumbering(types)
142+
return types
143+
144+
def clearUp(db):
145+
with db:
146+
cur = db.cursor()
147+
cur.execute("TRUNCATE TABLE ITEMS")
148+
cur.execute("TRUNCATE TABLE DIMENSION")
149+
cur.execute("TRUNCATE TABLE ITEM_ATTR_ENUM")
150+
cur.execute("TRUNCATE TABLE ITEM_ATTR")
151+
cur.execute("TRUNCATE TABLE ITEM_TYPE")
152+
cur.execute('truncate table users')
153+
cur.execute('truncate table items')
154+
cur.execute('truncate table item_map_varchar')
155+
cur.execute('truncate table item_map_double')
156+
cur.execute('truncate table item_map_datetime')
157+
cur.execute('truncate table item_map_int')
158+
cur.execute('truncate table item_map_boolean')
159+
cur.execute('truncate table item_map_enum')
160+
cur.execute('truncate table item_map_text')
161+
162+
def create_schema(client_name, dbSettings, scheme_file_path, clean=False):
163+
164+
if clean != True:
165+
json_data=open(scheme_file_path)
166+
data = json.load(json_data)
167+
if 'types' not in data:
168+
print "couldn't find types object in json"
169+
return
170+
else:
171+
types = readTypes(data['types'])
172+
173+
db = MySQLdb.connect(user=dbSettings["user"],db=client_name,passwd=dbSettings["password"], host=dbSettings["host"])
174+
if clean == True:
175+
clearUp(db)
176+
print "Finished cleaning attributes successfully"
177+
else:
178+
addToDb(db, types)
179+
f = open('dimensions.json','w')
180+
outputDimensionsToFile(f,db)
181+
182+
print 'Finished applying attributes successfully'
183+
184+
json_data.close()
185+

0 commit comments

Comments
 (0)