annotate ikweb/tools/lazygen_model.py @ 283:34fd5bcd4cf8

add some explan in README
author billy3321@f3svr.f3.csu.edu.tw.f3.csu.edu.tw
date Fri, 05 Dec 2008 15:39:49 +0800
parents 7747bbe5b68e
children
rev   line source
247
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
1 #!/usr/bin/env python
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
2 import re
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
3 import os
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
4 import sys
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
5
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
6 from sqlalchemy import *
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
7 # dirty import XD
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
8 gaeo_home = os.getenv('GAEO_HOME').replace('~',os.getenv('HOME'))+'/bin'
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
9 try:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
10 sys.path.append(gaeo_home)
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
11 from gaeogen import *
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
12 except ImportError, e:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
13 print "can not import gaeogen, the gaeogen path maybe wrong, got path:%s" % gaeo_home
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
14 sys.exit()
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
15
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
16
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
17 def sqlite_column_mapper(model_class, table_obj):
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
18 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
19 map sqlite columns of the table to GAEO Model
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
20
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
21 >>> metadata = MetaData()
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
22 >>> users_table = Table('users', metadata,
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
23 ... Column('id', Integer, primary_key=True),
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
24 ... Column('name', String),
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
25 ... Column('fullname', String),
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
26 ... Column('password', String)
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
27 ... )
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
28 >>> model = sqlite_column_mapper(GenModel, users_table)
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
29 >>> model.generate_content()
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
30 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
31 def convertor(col_type):
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
32 lowertype = col_type.lower()
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
33
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
34 if re.match('char', lowertype): col_type = "String"
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
35 if re.match('(int|integer)', lowertype): col_type = 'Integer'
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
36 return col_type
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
37
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
38 model = model_class(table_obj.name)
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
39
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
40 for column in table_obj.columns:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
41 model.add_property( "%s" % GenProperty(column.name,
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
42 column,
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
43 convertor))
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
44 return model
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
45
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
46 class GenProperty(object):
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
47 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
48 GAE Property Generator.
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
49
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
50 class Property(verbose_name=None, name=None, default=None, required=False, validator=None, choices=None)
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
51 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
52 db_prefix = "^(SL|PG|Max|MS|FB|Ac|Info|Oracle)"
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
53
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
54 def __init__(self, name, column=False, convertor=False):
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
55 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
56 >>> col = Column('summary', String(2000))
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
57 >>> p = GenProperty('summary', col)
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
58 >>> "%s" % p
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
59 "summary:StringProperty(verbose_name='summary',name='summary')"
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
60 >>> meta = MetaData()
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
61 >>> meta.bind = create_engine("sqlite:///dummy.sqlite")
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
62 >>> t = Table('ally', meta, autoload=True)
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
63 >>> p = GenProperty('name', t.columns.get('name'))
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
64 >>> "%s" % p
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
65 "name:TextProperty(verbose_name='name',name='name')"
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
66 >>> p = GenProperty('name', t.columns.get('time'))
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
67 >>> "%s" % p
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
68 "name:DatetimeProperty(verbose_name='name',name='name')"
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
69 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
70 self.name = name
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
71 self.column = column
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
72
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
73 # very dirty ...
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
74 if column:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
75 self.type_name = re.sub( self.db_prefix,'',
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
76 column.type.__class__.__name__)
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
77 if convertor:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
78 self.type_name = convertor(self.type_name)
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
79
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
80 def __str__(self):
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
81 return self.gen()
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
82
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
83 def gen(self):
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
84 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
85 generate gaeo property code.
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
86 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
87 clause= self.create_clause(self.type_name, self.column)
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
88 return gae_property_code(self.name, self.type_name, clause)
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
89
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
90 def create_clause(self, type, column):
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
91 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
92 create property cluase.
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
93
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
94 >>> s = Column('summary', String(2000))
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
95 >>> code = GenProperty(s.name).create_clause('string', s)
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
96 >>> code
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
97 {'verbose_name': 'summary', 'name': 'summary'}
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
98 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
99 clause= self.create_basic_clause(column)
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
100
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
101 try:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
102 other_caluse = getattr(self, 'create_%s_clause' % type.lower())(column)
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
103 clause.update(other_caluse)
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
104 except:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
105 pass
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
106
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
107 return clause
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
108
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
109 def create_basic_clause(self, column):
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
110 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
111 create basic property cluase.
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
112 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
113 clause = {'verbose_name':self.name, 'name':self.name}
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
114
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
115 if column.default:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
116 clause.setdefault('default', column.default)
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
117 elif column.server_default:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
118 clause.setdefault('default', column.server_default)
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
119
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
120 return clause
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
121
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
122 def gae_property_code(name, type, clause):
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
123 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
124 generate google app engine property code.
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
125
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
126 >>> gae_property_code('name', 'string', {'default':'hychen','name':'hi'})
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
127 "name:StringProperty(default='hychen',name='hi')"
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
128 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
129 argv = []
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
130
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
131 def _quote(v):
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
132 try:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
133 return "'"+v+"'"
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
134 except TypeError:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
135 pass
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
136
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
137 for k,v in clause.items():
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
138 argv.append("%s=%s" % (k, _quote(v) ) )
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
139
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
140 return "%s:%sProperty(%s)" % (name,
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
141 type.capitalize(),
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
142 ','.join(argv))
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
143
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
144 class ModelMaker:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
145
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
146 is_build = False
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
147
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
148 def __init__(self, **kwds):
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
149 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
150 to create GAEO Model
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
151
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
152 >>> maker = ModelMaker(schema="sqlite:///dummy.sqlite", autoload=True)
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
153 >>> maker.build()
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
154 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
155 self.models = []
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
156 self.set_engine(kwds['schema'])
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
157
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
158 try:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
159 if True == kwds['autoload']:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
160 self.table_names = self.db_engine.table_names()
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
161 else:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
162 self.table_names = kwds['include_tables']
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
163 except KeyError:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
164 print "input wrong argv."
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
165
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
166 def set_engine(self, schema):
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
167 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
168 set db engine
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
169 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
170 self.db_engine = create_engine(schema)
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
171 self.meta = MetaData()
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
172 self.meta.bind = self.db_engine
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
173
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
174 def build(self):
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
175 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
176 build models by talbse in database.
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
177 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
178 mapper_func = '%s_column_mapper' % self.db_engine.name.lower()
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
179
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
180 for table_name in self.table_names:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
181 table = Table(table_name, self.meta, autoload=True)
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
182 self.models.append( eval(mapper_func)( GenModel, table) )
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
183
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
184 self.is_build = True
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
185
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
186 def save(self):
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
187 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
188 save model
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
189 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
190 if self.is_build:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
191 application_dir = os.path.join(os.getcwd(), 'application')
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
192 model_dir = os.path.join(application_dir, 'model')
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
193
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
194 # check if the model directory had been created
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
195 if not os.path.exists(os.path.join(model_dir, '__init__.py')):
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
196 create_file(os.path.join(model_dir, '__init__.py'), [])
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
197
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
198 for model in self.models:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
199 print 'Creating Model %s ...' % model.name
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
200 model.save(os.path.join(model_dir, '%s.py' % model.name))
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
201 else:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
202 print "not build yet."
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
203
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
204 def gen_models_from_db(db_schema, include_tables=False):
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
205 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
206 generate models form database.
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
207
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
208 gen_models_from_db("sqlite://dumy.sqlite")
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
209
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
210 # spefiy tables
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
211 gen_models_from_db("sqlite://dumy.sqlite", include_tables=['tb1','tb2'])
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
212 """
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
213 if not include_tables:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
214 maker = ModelMaker(schema=db_schema, autoload=True)
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
215 else:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
216 maker = ModelMaker(schema=db_schema, include_tables=include_tables)
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
217
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
218 maker.build()
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
219 maker.save()
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
220
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
221 if '__main__' == __name__:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
222 import sys
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
223
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
224 if 'test' == sys.argv[1]:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
225 import doctest
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
226 doctest.testmod()
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
227 else:
7747bbe5b68e start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff changeset
228 gen_models_from_db(sys.argv[1])