generate.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #!/usr/bin/env python
  2. import re
  3. import MySQLdb as mysql
  4. from singplur import singularize
  5. from itertools import combinations
  6. PRIMARY_KEY = 'id'
  7. TAB = '\t'
  8. MODEL_NAMESPACE = ''
  9. def php_value(data, indent=0):
  10. """
  11. Convert Python data types to PHP code. Tuples or lists are used for
  12. single-line arrays, sets for multiline arrays, dictionaries for associative
  13. arrays, and strings are encapsulated in single quotes.
  14. """
  15. if isinstance(data, str):
  16. return "'%s'" % data.replace("'", "\\'")
  17. tabs = (indent + 1) * TAB
  18. if type(data) in (list, tuple):
  19. return 'array(%s)' % ', '.join(map(php_value, data))
  20. elif isinstance(data, dict):
  21. if not data:
  22. return 'array()'
  23. lines = ["'%s' => %s" % (k, php_value(v, indent + 1))
  24. for k, v in data.iteritems()]
  25. elif isinstance(data, set):
  26. if not data:
  27. return 'array()'
  28. lines = [php_value(item, indent + 1) for item in data]
  29. else:
  30. return str(data)
  31. return 'array(\n%s\n%s)' % (',\n'.join([tabs + line for line in lines]),
  32. indent * TAB)
  33. def php_assoc((arg, kwargs)):
  34. assocs = ["'%s' => %s" % (k, php_value(v)) for k, v in kwargs.iteritems()]
  35. return 'array(%s)' % ', '.join([php_value(arg)] + assocs)
  36. def php_static_var(name, value):
  37. return 'static $%s = %s;' % (name, value)
  38. def php_static_array(name, values):
  39. array = 'array(%s)' if len(values) < 2 \
  40. else 'array(\n{0}{0}%s\n{0})'.format(TAB)
  41. lines = map(php_assoc, values)
  42. array_values = (',\n' + 2 * TAB).join(lines)
  43. return php_static_var(name, array % array_values)
  44. def php_block(php_code):
  45. return '<?php\n\n%s\n\n?>' % php_code
  46. class Model(object):
  47. def __init__(self, table, options):
  48. self.table = table
  49. self.options = options
  50. self.attributes = []
  51. self.primary_key = []
  52. self.foreign_keys = []
  53. self.has_many = []
  54. self.has_one = []
  55. self.belongs_to = []
  56. def __str__(self):
  57. return '<Model "%s" table=%s>' % (self.name(), self.table)
  58. def singular_name(self):
  59. return singularize(self.table)
  60. def classname(self):
  61. return singularize(re.sub('(?:^|_)([a-z])',
  62. lambda m: m.group(1).upper(), self.table))
  63. def read_attributes(self, conn):
  64. fields = read_fields(conn, self.table)
  65. is_protected = lambda field: field['Key'] in ('PRI', 'MUL')
  66. self.protected_fields, accessible = partition(is_protected, fields)
  67. self.accessible_attr = [field['Field'] for field in accessible]
  68. def process_keys(self, models):
  69. for field in self.protected_fields:
  70. name = field['Field']
  71. if field['Key'] == 'PRI':
  72. self.primary_key.append(name)
  73. elif field['Key'] == 'MUL':
  74. self.add_foreign_key(name, models)
  75. # Has-many through
  76. if len(self.belongs_to) > 1:
  77. relations = [find_model(name, models)
  78. for name, options in self.belongs_to]
  79. for a, b in combinations(relations, 2):
  80. a.add_has_relation(b, through=self.table)
  81. b.add_has_relation(a, through=self.table)
  82. def add_has_relation(self, model, **kwargs):
  83. options = self.relopts(model)
  84. options.update(kwargs)
  85. name = model.singular_name()
  86. if [self.singular_name(), name] in self.options.get('has_one', []):
  87. self.has_one.append((name, options))
  88. else:
  89. self.has_many.append((model.table, options))
  90. def relopts(self, model, **kwargs):
  91. relopts = kwargs
  92. if self.options.get('create_select', True):
  93. relopts['select'] = ', '.join(model.accessible_attr)
  94. return relopts
  95. def add_foreign_key(self, name, models):
  96. self.foreign_keys.append(name)
  97. singular = re.sub('_' + PRIMARY_KEY + '$', '', name)
  98. try:
  99. other_model = find_model(singular, models)
  100. except ValueError:
  101. return
  102. self.belongs_to.append((singular, self.relopts(other_model)))
  103. other_model.add_has_relation(self)
  104. def php_has_many(self):
  105. if self.has_many:
  106. return php_static_array('has_many', self.has_many)
  107. def php_has_one(self):
  108. if self.has_one:
  109. return php_static_array('has_one', self.has_one)
  110. def php_belongs_to(self):
  111. if self.belongs_to:
  112. return php_static_array('belongs_to', self.belongs_to)
  113. def php_has_many_and_belongs_to(self):
  114. if not self.has_many or not self.belongs_to:
  115. return
  116. def php_attr_accessible(self):
  117. # All attributes except primary/foreign keys are accessible
  118. if self.accessible_attr:
  119. return php_static_var('attr_accessible',
  120. php_value(self.accessible_attr))
  121. def generate_php(self, add_php_block=False):
  122. # PHP delimiters
  123. php = '<?php\n\n%s\n\n?>'
  124. # Class definition
  125. classname = self.classname()
  126. if self.options.get('namespace'):
  127. classname = options['namespace'] + '\\' + classname
  128. classdef = 'class %s extends ActiveRecord\\Model {\n%%s\n}' % classname
  129. # Indented lines within class definition
  130. lines = []
  131. if self.options.get('create_relations', True):
  132. lines += [
  133. self.php_has_many(),
  134. self.php_has_one(),
  135. self.php_belongs_to(),
  136. self.php_has_many_and_belongs_to(),
  137. ]
  138. if self.options.get('create_accessible', True):
  139. lines.append(self.php_attr_accessible())
  140. php = classdef % '\n'.join([TAB + line
  141. for line in lines if line is not None])
  142. return php_block(php) if add_php_block else php
  143. def filename(self):
  144. return self.classname().lower().replace('_', '') + '.php'
  145. def create_path_from_dir(self, dirname):
  146. return os.path.dirname(dirname + '//') + '/' + self.filename()
  147. def save_in_file(self, path):
  148. code = self.generate_php(True)
  149. f = open(path, 'w')
  150. f.write(code)
  151. f.close()
  152. def find_model(singular_name, models):
  153. for model in models:
  154. if model.singular_name() == singular_name:
  155. return model
  156. raise ValueError('No model found for "%s".' % singular_name)
  157. def flatten(iterable):
  158. return reduce(lambda a, b: a + b, map(list, iterable), [])
  159. def create_models(conn, options):
  160. models = [Model(table, options) for table in read_tables(conn)]
  161. for model in models:
  162. model.read_attributes(conn)
  163. for model in models:
  164. model.process_keys(models)
  165. return models
  166. def refine_model(conn, model, models):
  167. for field in read_fields(conn, model.table):
  168. model.add_attribute(field, models)
  169. def read_tables(conn):
  170. cur = conn.cursor()
  171. cur.execute('show tables')
  172. tables = flatten(cur.fetchall())
  173. cur.close()
  174. return tables
  175. def read_fields(conn, table):
  176. cur = conn.cursor(mysql.cursors.DictCursor)
  177. cur.execute('show columns from %s' % table)
  178. fields = cur.fetchall()
  179. cur.close()
  180. return fields
  181. def partition(callback, iterable):
  182. """
  183. Partition an iterable into two parts using a callback that returns a
  184. boolean.
  185. Example:
  186. >>> partition(lambda x: x & 1, range(6))
  187. ([1, 3, 5], [0, 2, 4])
  188. """
  189. a, b = [], []
  190. for item in iterable:
  191. (a if callback(item) else b).append(item)
  192. return a, b
  193. if __name__ == '__main__': # pragma: nocover
  194. import os
  195. from argparse import ArgumentParser
  196. parser = ArgumentParser(description='Generate PHPActiveRecord models.')
  197. parser.add_argument('dbname', help='database name')
  198. parser.add_argument('-H', '--host', metavar='ADDRESS', default='localhost',
  199. help='MySQL server address')
  200. parser.add_argument('-u', '--user', metavar='USERNAME', default='root',
  201. help='MySQL username')
  202. parser.add_argument('-p', '--password', default='mysql12#$',
  203. help='MySQL password')
  204. parser.add_argument('--has-one', nargs=2, dest='opt_has_one',
  205. help='one-to-one relationships of the form '
  206. '\'payment-receipt\', where one payment has one '
  207. 'receipt', action='append', default=[])
  208. parser.add_argument('--namespace', default='', dest='opt_namespace',
  209. help='PHP namespace to create models classes in')
  210. parser.add_argument('--create-select', action='store_true',
  211. help='whether to skip creation of the \'select\' '
  212. 'option in relations', dest='opt_create_select')
  213. parser.add_argument('--create-accessible', dest='opt_create_accessible',
  214. help='whether to skip creation of the '
  215. '\'$attr_accessible\' variable, containing all '
  216. 'attributes except primary or foreing keys',
  217. action='store_true')
  218. parser.add_argument('-a', '--create-all', action='store_true',
  219. help='create all available options and variables')
  220. parser.add_argument('-d', '--dir', help='directory to save model files in')
  221. parser.add_argument('-s', '--spaces', nargs='?', const=4, type=int,
  222. help='use spaces instead of tabs (4 by default)')
  223. args = parser.parse_args()
  224. options = {}
  225. for arg, value in args._get_kwargs():
  226. if arg[:4] == 'opt_':
  227. options[arg[4:]] = value
  228. if args.create_all:
  229. options['create_select'] = options['create_accessible'] = True
  230. if args.spaces:
  231. TAB = args.spaces * ' '
  232. conn = mysql.connect(args.host, args.user, args.password, args.dbname)
  233. models = create_models(conn, options)
  234. conn.close()
  235. if args.dir:
  236. if not os.path.exists(args.dir):
  237. os.makedirs(args.dir)
  238. for model in models:
  239. path = model.create_path_from_dir(args.dir)
  240. model.save_in_file(path)
  241. print 'Saved model %s in %s' % (model.classname(), path)
  242. else:
  243. for model in models:
  244. print model.generate_php()