-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowlib.py
512 lines (467 loc) · 22.4 KB
/
powlib.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
import re
import os
from sqlalchemy import Table
import logging
import copy
from sqlalchemy import Column, Integer, String, Date, DateTime, Float
from sqlalchemy import Unicode, Text, Boolean, Numeric, BigInteger, LargeBinary
def make_logger(name, level, handler, format=None, logfile=None):
"""
get the given logger and configure it
with handler, and format
loglevels:
------------------------------
Level Numeric value
CRITICAL 50
ERROR 40
WARNING 30
INFO 20
DEBUG 10
NOTSET 0
"""
log_file_name = logfile
handler_log_level = logging.INFO
logger_log_level = logging.DEBUG
db_handler = logging.FileHandler(db_log_file_name)
db_handler.setLevel(db_handler_log_level)
db_logger = logging.getLogger('sqlalchemy')
db_logger.addHandler(db_handler)
db_logger.setLevel(db_logger_log_level)
return logger
def get_class_name(name):
"""
tries to return a CamelCased class name as good as poosible
capitalize
split at underscores "_" and capitelize the following letter
merge
this_is_Test => ThisIsTest
test => Test
testone => Testone
"""
#print("class_name: " + "".join([c.capitalize() for c in name.split("_")]))
return "".join([c.capitalize() for c in name.split("_")])
#see: http://stackoverflow.com/questions/38987/how-to-merge-two-python-dictionaries-in-a-single-expression
# but I need a deep copy here.
def merge_two_dicts(x, y):
'''Given two dicts, merge them into a new dict as a shallow copy.'''
z = copy.deepcopy(x)
#z = x.copy()
z.update(y)
return z
# (pattern, search, replace) regex english plural rules tuple
# taken from : http://www.daniweb.com/software-development/python/threads/70647
rule_tuple = (
('[ml]ouse$', '([ml])ouse$', 'ice'),
('child$', 'child$', 'children'),
('booth$', 'booth$', 'booths'),
('foot$', 'foot$', 'feet'),
('ooth$', 'ooth$', 'eeth'),
('l[eo]af$', 'l([eo])af$', 'laves'),
('sis$', 'sis$', 'ses'),
('man$', 'man$', 'men'),
('ife$', 'ife$', 'ives'),
('eau$', 'eau$', 'eaux'),
('lf$', 'lf$', 'lves'),
('[sxz]$', '$', 'es'),
('[^aeioudgkprt]h$', '$', 'es'),
('(qu|[^aeiou])y$', 'y$', 'ies'),
('$', '$', 's')
)
def regex_rules(rules=rule_tuple):
# also to pluralize
for line in rules:
pattern, search, replace = line
yield lambda word: re.search(pattern, word) and re.sub(search, replace, word)
def plural(noun):
#print noun
# the final pluralisation method.
for rule in regex_rules():
result = rule(noun)
#print result
if result:
return result
def pluralize(noun):
return plural(noun)
def singularize(word):
specials = {
"children" : "child",
"mice" : "mouse",
"lice" : "louse",
"men" : "man",
"feet" : "foot",
"women" : "woman"
}
if word in specials.keys():
return specials[word]
# taken from:http://codelog.blogial.com/2008/07/27/singular-form-of-a-word-in-python/
sing_rules = [lambda w: w[-3:] == 'ies' and w[:-3] + 'y',
lambda w: w[-4:] == 'ives' and w[:-4] + 'ife',
lambda w: w[-3:] == 'ves' and w[:-3] + 'f',
lambda w: w[-2:] == 'es' and w[:-2],
lambda w: w[-1:] == 's' and w[:-1],
lambda w: w,
]
word = word.strip()
singleword = [f(word) for f in sing_rules if f(word) is not False][0]
return singleword
def rel_dec(what, who):
# We're going to return this decorator
def simple_decorator(f):
# This is the new function we're going to return
# This function will be used in place of our original definition
def wrapper():
print(what)
f()
print(who)
return wrapper
return simple_decorator
#
# a class decorator that executes at import.
# ala flask app.route()
# does all the magic monkey patching stuff like:
# * has_many
# * setup the schema
# * one to one
# * many to many
#
# see: http://ains.co/blog/things-which-arent-magic-flask-part-1.html
#
# For the relation part see:
# http://docs.sqlalchemy.org/en/latest/orm/tutorial.html#building-a-relationship
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship
class powDecNew():
def __init__(self):
self.relations = {}
# below you can find the relation decorators
# These types of relations are implementd:
# Functional notion => sqlalchemy documentation notion
# --------------------------------------------------------------------
# has_many_and_belongs_to => One To Many with backref
# has_many => One To Many (without backref)
# belongs_to => Many To One (without backref)
# one_to_one => One To One
# many_to_many => Many To Many
# is_tree => Adjacence List
#
def has_many_and_belongs_to(self, child_as_str, backref=False):
##
#
#
return self.has_many( child_as_str, backref=True)
def has_many(self, child_as_str, backref=False):
# cls is the class that has many of the related models (e.g. User, Post)
# the "parent" class
# rel_as_str is the plueral name of the child class (adresses, comments)
# klass below is the real class instance of the child
def decorator(parent_class):
parent_name = parent_class.__name__.lower()
parent_class_name = parent_class.__name__
child_class_name = get_class_name(singularize(child_as_str))
child_module_name = singularize(child_as_str)
#print(sorted(locals().keys()))
#print(sorted(globals().keys()))
import sys
if "medium.models.sql." + child_module_name in sys.modules.keys():
#print(dir(sys.modules["medium.models.sql." + child_module_name]))
child_klass = getattr(sys.modules["medium.models.sql." + child_module_name], child_class_name)
else:
import importlib
mod = importlib.import_module('medium.models.sql.' + child_module_name)
#mod = __import__('medium.models.sql.'+rel_module_name, fromlist=[rel_class_name])
child_klass = getattr(mod, child_class_name)
if backref:
setattr(parent_class, child_as_str, relationship(child_class_name,
order_by=child_klass.id,
back_populates=parent_name))
else:
setattr(parent_class, child_as_str, relationship(child_class_name))
setattr(child_klass, parent_name + "_id", Column(Integer, ForeignKey(pluralize(parent_name)+".id")))
if backref:
setattr(child_klass, parent_name, relationship(parent_class_name, back_populates=child_as_str))
##print(dir(rel))
print("RELATION: I see a: " + parent_class_name + " has many: " + child_as_str )
if backref:
print( " .. and " + child_as_str + " belongs_to " + parent_name)
return parent_class
return decorator
def belongs_to(self, parent_as_str):
# cls is the class that has many of the related models (e.g. User, Post)
# the "parent" class
# rel_as_str is the plueral name of the child class (adresses, comments)
# klass below is the real class instance of the child
def decorator(child_class):
child_name = child_class.__name__.lower()
parent_class_name = parent_as_str.capitalize()
parent_module_name = parent_as_str
parent_class = None
import sys
if ("medium.models.sql."+parent_module_name) in sys.modules.keys():
parent_class = getattr(sys.modules["medium.models.sql."+parent_module_name], parent_class_name)
else:
import importlib
mod = importlib.import_module('medium.models.sql.' + parent_module_name)
#mod = __import__('medium.models.sql.'+rel_module_name, fromlist=[rel_class_name])
parent_class = getattr(mod, parent_class_name)
#print("parent_class: " + str(parent_class))
#print(dir(klass))
setattr(parent_class, child_name, relationship(child_class.__name__))
setattr(parent_class, child_name + "_id", Column(Integer, ForeignKey(pluralize(child_name)+".id")))
##print(dir(rel))
print("RELATION: I see a: " + child_name + " belongs_to: " + parent_as_str)
return child_class
return decorator
def one_to_one(self, child_as_str):
# cls is parent class (Post)
# child_as_str is the singular name of the child (related class)
# klass below is the real class instace of the child
# one-to-one
def decorator(parent):
# cls is the parent class of the relation
parent_name = parent.__name__.lower()
#print("cls_name: " + cls_name)
child_class_name = child_as_str.capitalize()
child_module_name = child_as_str
#print("child_class_name: " + child_class_name)
#print("child_module_name: " + child_module_name)
mod = __import__('medium.models.sql.'+child_module_name, fromlist=[child_class_name])
klass = getattr(mod, child_class_name)
#print("rel_class: " + str(klass))
#print(dir(klass))
setattr(parent, child_as_str, relationship(child_class_name,
uselist=False,
back_populates=parent_name))
setattr(klass, parent_name + "_id", Column(Integer, ForeignKey(parent_name+".id")))
setattr(klass, parent_name, relationship(parent_name.capitalize(), back_populates=child_as_str))
##print(dir(rel))
print("RELATION: I see a: " + parent_name.capitalize() + " has many: " + child_as_str)
return parent
return decorator
def many_to_many(self, children):
# cls is the class that has many of the related models (e.g. User, Post)
# the "parent" class
# rel_as_str is the plueral name of the child class (adresses, comments)
# klass below is the real class instance of the child
from medium.database.sqldblib import Base
def decorator(parent_class):
parent_name = parent_class.__name__.lower()
parent_class_name = parent_class.__name__
#
# create the new association Table and class
#
assoc_table = Table("association_" + parent_name + "_" + singularize(children),
Base.metadata,
Column(parent_name + "_id", Integer, ForeignKey(pluralize(parent_name) + ".id")),
Column(singularize(children)+"_id", Integer, ForeignKey(children + ".id"))
)
child_class_name = singularize(children).capitalize()
child_module_name = singularize(children)
#
# set the parent attribute
#
setattr(parent_class, children, relationship(child_class_name,
secondary=assoc_table,
back_populates=pluralize(parent_name)))
import sys
if "medium.models.sql." + child_module_name in sys.modules.keys():
#print(dir(sys.modules["medium.models.sql." + child_module_name]))
child_klass = getattr(sys.modules["medium.models.sql." + child_module_name], child_class_name)
else:
import importlib
mod = importlib.import_module('medium.models.sql.' + child_module_name)
#mod = __import__('medium.models.sql.'+rel_module_name, fromlist=[rel_class_name])
child_klass = getattr(mod, child_class_name)
#
# set the child attribute
#
setattr(child_klass, pluralize(parent_name),
relationship(parent_class_name,
secondary=assoc_table, back_populates=children ))
##print(dir(rel))
print("RELATION: I see a: " + parent_class_name + " has many-to-many: " + children)
return parent_class
return decorator
def is_tree(self):
# cls is the class that has many of the related models (e.g. User, Post)
# klass below is the real class instance of the child
def decorator(cls):
# parent is the parent class of the relation
cls_name = cls.__name__.lower()
#print(cls_name)
setattr(cls, "tree_parent_id", Column(Integer, ForeignKey(pluralize(cls_name)+".id")))
setattr(cls, "tree_children", relationship(cls_name.capitalize()))
##print(dir(rel))
print("RELATION: I see a tree: " + cls_name.capitalize() )
return cls
return decorator
def _many_to_one(self, child_as_str, backref=False):
# parent is the class that has many of the related models (e.g. User, Post)
# klass below is the real class instance of the child
def decorator(parent):
# parent is the parent class of the relation
parent_name = parent.__name__.lower()
#print("parent_name: " + parent_name)
child_class_name = singularize(child_as_str).capitalize()
child_module_name = singularize(child_as_str)
child_table_name = child_class_name.lower()
#print("child_class_name: " + child_class_name)
#print("child_module_name: " + child_module_name)
mod = __import__('medium.models.sql.'+child_module_name, fromlist=[child_class_name])
klass = getattr(mod, child_class_name)
#print("rel_class: " + str(klass))
#print(dir(klass))
setattr(parent, child_table_name + "_id", Column(Integer, ForeignKey(child_table_name + '.id')))
setattr(parent, child_table_name, relationship(child_class_name))
if backref:
setattr(klass, pluralize(parent_name), relationship(parent.__name__, back_populates=child_class_name))
##print(dir(rel))
print("RELATION: I see a: " + parent_name.capitalize() + " many to one: " + child_as_str)
return parent
return decorator
def _one_to_many(self, child_as_str):
# parent is the class that has many of the related models (e.g. User, Post)
# klass below is the real class instance of the child
def decorator(parent):
# parent is the parent class of the relation
parent_name = parent.__name__.lower()
#print("parent_name: " + parent_name)
child_class_name = singularize(child_as_str).capitalize()
child_module_name = singularize(child_as_str)
#print("child_class_name: " + child_class_name)
#print("child_module_name: " + child_module_name)
mod = __import__('medium.models.sql.'+child_module_name, fromlist=[child_class_name])
klass = getattr(mod, child_class_name)
#print("rel_class: " + str(klass))
#print(dir(klass))
setattr(parent, child_as_str, relationship(child_class_name))
setattr(klass, parent_name + "_id", Column(Integer, ForeignKey(pluralize(parent_name)+".id")))
##print(dir(rel))
print("RELATION: I see a: " + parent_name.capitalize() + " has many: " + child_as_str)
return parent
return decorator
#
# sets up a sqlqlchemy schema from a cerberus schema dict
# goal is to go seamlessly to NoSql AND to bring validation in
# the schema definition at once!
# ONE definition for SQL, NoSQL and Validation.
#
def setup_sql_schema(self, what=""):
def decorator(cls):
print("setup_schema:" + cls.__name__.lower())
#
# create a sqlalchemy model from the schema
#
# there are two special keys you can use additionally to the
# standard cerberus syntx:
# "sql" : add any Column __init__ kwargs here, they will be handed raw
# to the Column __init__
# "sqltype": specify a more precise SQL subtype.
# e.g. cerberus has integer. SQL has INTEGER, BIGINTEGER
# the two special keys will be removed from the schema at the end of this
# decorator.
#
colclass = None
from sqlalchemy import Column, Integer, String, Date, DateTime, Float
from sqlalchemy import Unicode, Text, Boolean, Numeric, BigInteger, LargeBinary
#
# now set the right sqlalchemy type for the column
#
for elem in cls.schema.keys():
#print(elem)
# the raw Column __init__ parameters dict
sql=cls.schema[elem].get("sql", {})
if cls.schema[elem]["type"] == "integer":
if "sqltype" in cls.schema[elem]:
if cls.schema[elem]["sqltype"].lower() == "biginteger":
setattr(cls, elem, Column(elem, BigInteger, **sql))
else:
setattr(cls, elem, Column(elem, Integer, **sql))
elif cls.schema[elem]["type"] == "float":
setattr(cls, elem, Column(elem, Float, **sql))
elif cls.schema[elem]["type"] == "string":
if "sqltype" in cls.schema[elem]:
if cls.schema[elem]["sqltype"].lower() == "text":
setattr(cls, elem, Column(elem, Text, **sql))
elif cls.schema[elem]["sqltype"].lower() == "unicode":
if "maxlength" in cls.schema[elem]:
setattr(cls, elem, Column(elem, Unicode(length=cls.schema[elem]["maxlength"]), **sql))
else:
setattr(cls, elem, Column(elem, Unicode, **sql))
else:
if "maxlength" in cls.schema[elem]:
setattr(cls, elem, Column(elem, String(length=cls.schema[elem]["maxlength"]), **sql))
else:
setattr(cls, elem, Column(elem, String, **sql))
elif cls.schema[elem]["type"] == "boolean":
setattr(cls, elem, Column(elem, Boolean, **sql))
elif cls.schema[elem]["type"] == "date":
setattr(cls, elem, Column(elem, Date, **sql))
elif cls.schema[elem]["type"] == "datetime":
setattr(cls, elem, Column(elem, DateTime, **sql))
elif cls.schema[elem]["type"] == "number":
setattr(cls, elem, Column(elem, Numeric, **sql))
elif cls.schema[elem]["type"] == "binary":
setattr(cls, elem, Column(elem, LargeBinary, **sql))
else:
raise Exception("Wrong Datatype in schema")
#print(" .. removing the schema (raw) sql key(s)")
cls.schema[elem].pop("sql", None)
cls.schema[elem].pop("sqltype", None)
return cls
return decorator
#
# sets up an elastic DSLschema from a cerberus schema dict
# goal is to go seamlessly to NoSql AND to bring validation in
# the schema definition at once!
# ONE definition for SQL, NoSQL and Validation.
# See: http://elasticsearch-dsl.readthedocs.io/en/latest/persistence.html
def setup_elastic_dsl_schema(self, what=""):
def decorator(cls):
print("setup_schema:" + cls.__name__.lower())
#
# create an elastic model from the schema
#
# there are two special keys you can use additionally to the
# standard cerberus syntx:
# "elastic" : add any Elastic DSL "Column" __init__ kwargs here, they will be handed raw
# to the Column __init__
# "elastictype" : add a more specific elasticserach_dsl type definition (Text instead of string)
# the two special keys will be removed from the schema at the end of this
# decorator.
#
#
# now set the right elastic types for the doc
#
from datetime import datetime
#from elasticsearch_dsl import DocType, String, Date, Nested, Boolean, Integer # Float, Byte, Text, analyzer, InnerObjectWrapper, Completion
import elasticsearch_dsl
for elem in cls.schema.keys():
#print(elem)
# the raw Column __init__ parameters dict
elastic=cls.schema[elem].get("elastic", {})
if cls.schema[elem]["type"] == "integer":
setattr(cls, elem, elasticsearch_dsl.Integer(**elastic))
elif cls.schema[elem]["type"] == "float":
setattr(cls, elem, elasticsearch_dsl.Float(**elastic))
elif cls.schema[elem]["type"] == "string":
setattr(cls, elem, elasticsearch_dsl.Text(**elastic))
elif cls.schema[elem]["type"] == "bool":
setattr(cls, elem, elasticsearch_dsl.Boolean(**elastic))
elif cls.schema[elem]["type"] == "date":
setattr(cls, elem, elasticsearch_dsl.Date(**elastic))
elif cls.schema[elem]["type"] == "datetime":
setattr(cls, elem, elasticsearch_dsl.Date(**elastic))
elif cls.schema[elem]["type"] == "number":
setattr(cls, elem, elasticsearch_dsl.Integer(**elastic))
elif cls.schema[elem]["type"] == "binary":
setattr(cls, elem, elasticsearch_dsl.Byte(**elastic))
elif cls.schema[elem]["type"] == "list":
setattr(cls, elem, elasticsearch_dsl.Keyword(**elastic))
else:
raise Exception("Wrong Datatype in schema")
#print(" .. removing the schema (raw) elastic key(s)")
cls.schema[elem].pop("elastic", None)
cls.schema[elem].pop("elastictype", None)
return cls
return decorator
relation = powDecNew()