Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import os |
| 4 | import pdb |
Sapan Bhatia | cdd90b7 | 2014-01-28 20:03:13 -0500 | [diff] [blame] | 5 | import copy |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 6 | import sys |
| 7 | import json |
| 8 | import re |
| 9 | from django.template import Context, Template |
| 10 | |
Sapan Bhatia | df2b49e | 2014-01-28 19:41:07 -0500 | [diff] [blame] | 11 | blacklist = ['SingletonModel','PlCoreBase'] |
| 12 | |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 13 | # Django set up |
| 14 | |
| 15 | sys.path.append('.') |
| 16 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings") |
Sapan Bhatia | eb62ad6 | 2014-01-28 14:29:08 -0500 | [diff] [blame] | 17 | from django.db.models.fields.related import ForeignKey, ManyToManyField |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 18 | from core.models import * |
| 19 | |
Sapan Bhatia | eb62ad6 | 2014-01-28 14:29:08 -0500 | [diff] [blame] | 20 | def 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 Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 28 | g = globals() |
| 29 | |
| 30 | def enum_classes(): |
| 31 | model_classes = [] |
| 32 | for c in g.values(): |
Sapan Bhatia | df2b49e | 2014-01-28 19:41:07 -0500 | [diff] [blame] | 33 | if type(c)==type(PlCoreBase) and c.__name__ not in blacklist: |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 34 | model_classes.append(c) |
| 35 | return model_classes |
| 36 | |
| 37 | |
| 38 | class 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 Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame] | 46 | self.plural_name = None |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 47 | |
| 48 | def plural(self): |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame] | 49 | if (self.plural_name): |
| 50 | return self.plural_name |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 51 | else: |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame] | 52 | name = str(self) |
| 53 | if (name.endswith('s')): |
| 54 | return name+'es' |
| 55 | else: |
| 56 | return name+'s' |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 57 | |
Scott Baker | 8ffd7d7 | 2014-11-10 15:58:58 -0800 | [diff] [blame] | 58 | def singular(self): |
| 59 | return str(self) |
| 60 | |
| 61 | def rest_name(self): |
| 62 | # These are things that either for historic reasons or due to incorrect naming, |
| 63 | # got called something different than the autogen thinks they should be |
| 64 | # called. |
| 65 | REST_FIXUP = {'networkdeploymentses': 'networkdeployments', |
| 66 | 'imagedeploymentses': 'imagedeployments', |
Scott Baker | 72bcf35 | 2014-11-21 11:36:19 -0800 | [diff] [blame^] | 67 | 'slicedeploymentses': 'slicedeployments', |
| 68 | 'userdeploymentses': 'userdeployments', |
Scott Baker | 8ffd7d7 | 2014-11-10 15:58:58 -0800 | [diff] [blame] | 69 | 'sitedeploymentses': 'sitedeployments', |
| 70 | 'siteroles': 'site_roles', |
| 71 | 'sliceprivileges': 'slice_privileges', |
| 72 | 'sliceroles': 'slice_roles', |
| 73 | } |
| 74 | return REST_FIXUP.get(self.plural(), self.plural()) |
| 75 | |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 76 | def camel(self): |
| 77 | name = str(self.model.__name__) |
Sapan Bhatia | df2b49e | 2014-01-28 19:41:07 -0500 | [diff] [blame] | 78 | return name |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 79 | |
| 80 | class Generator(dict): |
| 81 | def all(self): |
| 82 | return self.values() |
| 83 | |
| 84 | def regex(self, r): |
| 85 | filtered = filter(lambda o:re.match(r,str(o)), self.values()) |
| 86 | return filtered |
| 87 | |
| 88 | def add_object(self, o): |
| 89 | obj = GenObj(o) |
| 90 | fields = o._meta.fields |
| 91 | self[str(obj).lower()]=obj |
| 92 | |
| 93 | def compute_links(self): |
| 94 | for obj in self.values(): |
Sapan Bhatia | b588540 | 2014-01-29 10:32:09 -0500 | [diff] [blame] | 95 | #if (str(obj)=='network'): |
Sapan Bhatia | cdd90b7 | 2014-01-28 20:03:13 -0500 | [diff] [blame] | 96 | # pdb.set_trace() |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 97 | fields = obj.model._meta.fields |
| 98 | for f in fields: |
Sapan Bhatia | b588540 | 2014-01-29 10:32:09 -0500 | [diff] [blame] | 99 | if (f and f.rel): |
| 100 | to_name = str(f.rel.to) |
| 101 | else: |
| 102 | to_name = None |
| 103 | |
| 104 | if type(f)==ForeignKey and to_name and to_name in self.keys(): |
| 105 | refobj = self[f.to_name] |
| 106 | |
| 107 | if (str(obj)=='slice' and f.to_name=='networks'): |
| 108 | obj.refs.append(refobj) |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame] | 109 | related_name = f.related_query_name() |
Sapan Bhatia | b588540 | 2014-01-29 10:32:09 -0500 | [diff] [blame] | 110 | if (related_name!='+' and related_name.lower()!=str(obj).lower()): |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame] | 111 | cobj = copy.deepcopy(obj) |
| 112 | cobj.multi = True |
| 113 | cobj.plural_name = related_name |
Sapan Bhatia | b588540 | 2014-01-29 10:32:09 -0500 | [diff] [blame] | 114 | #if (str(refobj)=='slice' and related_name=='networks'): |
| 115 | # pdb.set_trace() |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame] | 116 | refobj.refs.append(cobj) |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 117 | else: |
| 118 | obj.props.append(f.name) |
Sapan Bhatia | b588540 | 2014-01-29 10:32:09 -0500 | [diff] [blame] | 119 | |
Sapan Bhatia | eb62ad6 | 2014-01-28 14:29:08 -0500 | [diff] [blame] | 120 | m2m = obj.model._meta.many_to_many |
| 121 | for f in m2m: |
| 122 | try: |
| 123 | related_model_name = f.m2m_reverse_field_name() |
| 124 | except: |
| 125 | related_model_name = f.m2m_db_table().rsplit('_',1)[-1] |
| 126 | |
Sapan Bhatia | b588540 | 2014-01-29 10:32:09 -0500 | [diff] [blame] | 127 | related_name = f.related_query_name() |
Sapan Bhatia | eb62ad6 | 2014-01-28 14:29:08 -0500 | [diff] [blame] | 128 | if related_model_name in self.keys(): |
Sapan Bhatia | cdd90b7 | 2014-01-28 20:03:13 -0500 | [diff] [blame] | 129 | # pdb.set_trace() |
Sapan Bhatia | eb62ad6 | 2014-01-28 14:29:08 -0500 | [diff] [blame] | 130 | refobj = self[related_model_name] |
Sapan Bhatia | cdd90b7 | 2014-01-28 20:03:13 -0500 | [diff] [blame] | 131 | cobj = copy.deepcopy(obj) |
| 132 | cobj.multi=True |
| 133 | refobj.refs.append(cobj) |
Scott Baker | 1e67bb4 | 2014-07-03 17:58:10 -0700 | [diff] [blame] | 134 | |
| 135 | # deal with upgradeFrom_rel_+ |
| 136 | if (related_name.endswith("+")): |
| 137 | continue |
| 138 | |
Sapan Bhatia | b588540 | 2014-01-29 10:32:09 -0500 | [diff] [blame] | 139 | if (related_name!='+' and related_name.lower()!=str(obj).lower()): |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame] | 140 | cobj = copy.deepcopy(obj) |
| 141 | cobj.multi = True |
| 142 | cobj.plural_name = related_name |
Sapan Bhatia | b588540 | 2014-01-29 10:32:09 -0500 | [diff] [blame] | 143 | |
| 144 | #if (str(refobj)=='slice' and related_name=='networks'): |
| 145 | # pdb.set_trace() |
Sapan Bhatia | 4efd0d9 | 2014-01-29 00:20:06 -0500 | [diff] [blame] | 146 | refobj.refs.append(cobj) |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame] | 147 | |
Sapan Bhatia | b588540 | 2014-01-29 10:32:09 -0500 | [diff] [blame] | 148 | #if (related_name=='networks'): |
| 149 | #pdb.set_trace() |
| 150 | #print str(refobj) |
| 151 | |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame] | 152 | |
Sapan Bhatia | eb62ad6 | 2014-01-28 14:29:08 -0500 | [diff] [blame] | 153 | |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 154 | |
| 155 | def main(): |
| 156 | try: |
| 157 | output = sys.argv[1] |
| 158 | except: |
| 159 | print 'Usage: modelgen <output template>' |
| 160 | exit(1) |
| 161 | |
| 162 | generator = Generator() |
| 163 | |
| 164 | models = enum_classes() |
| 165 | |
| 166 | for m in models: |
| 167 | generator.add_object(m) |
| 168 | |
| 169 | generator.compute_links() |
| 170 | template_contents = open(output).read() |
| 171 | template = Template(template_contents) |
| 172 | context = Context({'generator':generator}) |
| 173 | print template.render(context) |
| 174 | |
| 175 | |
| 176 | if (__name__=='__main__'): |
| 177 | main() |