blob: ef2730b762699f23b43c0ae7b1615f4a8d2dea97 [file] [log] [blame]
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -05001#!/usr/bin/python
2
3import os
4import pdb
Sapan Bhatiacdd90b72014-01-28 20:03:13 -05005import copy
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -05006import sys
7import json
8import re
9from django.template import Context, Template
10
Sapan Bhatiadf2b49e2014-01-28 19:41:07 -050011blacklist = ['SingletonModel','PlCoreBase']
12
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050013# Django set up
14
15sys.path.append('.')
16os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings")
Sapan Bhatiaeb62ad62014-01-28 14:29:08 -050017from django.db.models.fields.related import ForeignKey, ManyToManyField
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050018from core.models import *
19
Sapan Bhatiaeb62ad62014-01-28 14:29:08 -050020def singular(foo, keys):
21 for k in keys:
22 if (foo==k+'es'):
23 return k
24 elif (foo==k+'s'):
25 return k
26 raise Exception('Plural to singular error for %s'%foo)
27
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050028g = globals()
29
30def enum_classes():
31 model_classes = []
32 for c in g.values():
Sapan Bhatiadf2b49e2014-01-28 19:41:07 -050033 if type(c)==type(PlCoreBase) and c.__name__ not in blacklist:
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050034 model_classes.append(c)
35 return model_classes
36
37
38class GenObj(object):
39 def __str__(self):
40 return str(self.model.__name__.lower())
41
42 def __init__(self, m):
43 self.model = m
44 self.props = []
45 self.refs = []
Sapan Bhatiabe42fba2014-01-28 23:53:49 -050046 self.plural_name = None
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050047
48 def plural(self):
Sapan Bhatiabe42fba2014-01-28 23:53:49 -050049 if (self.plural_name):
50 return self.plural_name
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050051 else:
Sapan Bhatiabe42fba2014-01-28 23:53:49 -050052 name = str(self)
53 if (name.endswith('s')):
54 return name+'es'
55 else:
56 return name+'s'
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050057
58 def camel(self):
59 name = str(self.model.__name__)
Sapan Bhatiadf2b49e2014-01-28 19:41:07 -050060 return name
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050061
62class Generator(dict):
63 def all(self):
64 return self.values()
65
66 def regex(self, r):
67 filtered = filter(lambda o:re.match(r,str(o)), self.values())
68 return filtered
69
70 def add_object(self, o):
71 obj = GenObj(o)
72 fields = o._meta.fields
73 self[str(obj).lower()]=obj
74
75 def compute_links(self):
76 for obj in self.values():
Sapan Bhatiab5885402014-01-29 10:32:09 -050077 #if (str(obj)=='network'):
Sapan Bhatiacdd90b72014-01-28 20:03:13 -050078 # pdb.set_trace()
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050079 fields = obj.model._meta.fields
80 for f in fields:
Sapan Bhatiab5885402014-01-29 10:32:09 -050081 if (f and f.rel):
82 to_name = str(f.rel.to)
83 else:
84 to_name = None
85
86 if type(f)==ForeignKey and to_name and to_name in self.keys():
87 refobj = self[f.to_name]
88
89 if (str(obj)=='slice' and f.to_name=='networks'):
90 obj.refs.append(refobj)
Sapan Bhatiabe42fba2014-01-28 23:53:49 -050091 related_name = f.related_query_name()
Sapan Bhatiab5885402014-01-29 10:32:09 -050092 if (related_name!='+' and related_name.lower()!=str(obj).lower()):
Sapan Bhatiabe42fba2014-01-28 23:53:49 -050093 cobj = copy.deepcopy(obj)
94 cobj.multi = True
95 cobj.plural_name = related_name
Sapan Bhatiab5885402014-01-29 10:32:09 -050096 #if (str(refobj)=='slice' and related_name=='networks'):
97 # pdb.set_trace()
Sapan Bhatiabe42fba2014-01-28 23:53:49 -050098 refobj.refs.append(cobj)
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050099 else:
100 obj.props.append(f.name)
Sapan Bhatiab5885402014-01-29 10:32:09 -0500101
Sapan Bhatiaeb62ad62014-01-28 14:29:08 -0500102 m2m = obj.model._meta.many_to_many
103 for f in m2m:
104 try:
105 related_model_name = f.m2m_reverse_field_name()
106 except:
107 related_model_name = f.m2m_db_table().rsplit('_',1)[-1]
108
Sapan Bhatiab5885402014-01-29 10:32:09 -0500109 related_name = f.related_query_name()
Sapan Bhatiaeb62ad62014-01-28 14:29:08 -0500110 if related_model_name in self.keys():
Sapan Bhatiacdd90b72014-01-28 20:03:13 -0500111 # pdb.set_trace()
Sapan Bhatiaeb62ad62014-01-28 14:29:08 -0500112 refobj = self[related_model_name]
Sapan Bhatiacdd90b72014-01-28 20:03:13 -0500113 cobj = copy.deepcopy(obj)
114 cobj.multi=True
115 refobj.refs.append(cobj)
Sapan Bhatiabe42fba2014-01-28 23:53:49 -0500116
Sapan Bhatiab5885402014-01-29 10:32:09 -0500117 if (related_name!='+' and related_name.lower()!=str(obj).lower()):
Sapan Bhatiabe42fba2014-01-28 23:53:49 -0500118 cobj = copy.deepcopy(obj)
119 cobj.multi = True
120 cobj.plural_name = related_name
Sapan Bhatiab5885402014-01-29 10:32:09 -0500121
122 #if (str(refobj)=='slice' and related_name=='networks'):
123 # pdb.set_trace()
Sapan Bhatia4efd0d92014-01-29 00:20:06 -0500124 refobj.refs.append(cobj)
Sapan Bhatiabe42fba2014-01-28 23:53:49 -0500125
Sapan Bhatiab5885402014-01-29 10:32:09 -0500126 #if (related_name=='networks'):
127 #pdb.set_trace()
128 #print str(refobj)
129
Sapan Bhatiabe42fba2014-01-28 23:53:49 -0500130
Sapan Bhatiaeb62ad62014-01-28 14:29:08 -0500131
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -0500132
133def main():
134 try:
135 output = sys.argv[1]
136 except:
137 print 'Usage: modelgen <output template>'
138 exit(1)
139
140 generator = Generator()
141
142 models = enum_classes()
143
144 for m in models:
145 generator.add_object(m)
146
147 generator.compute_links()
148 template_contents = open(output).read()
149 template = Template(template_contents)
150 context = Context({'generator':generator})
151 print template.render(context)
152
153
154if (__name__=='__main__'):
155 main()