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 |
Scott Baker | f303990 | 2015-10-23 12:20:15 -0700 | [diff] [blame] | 10 | from optparse import OptionParser |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 11 | |
| 12 | # Django set up |
| 13 | |
| 14 | sys.path.append('.') |
Scott Baker | 8fc378d | 2015-02-16 22:43:01 -0800 | [diff] [blame] | 15 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings") |
Sapan Bhatia | eb62ad6 | 2014-01-28 14:29:08 -0500 | [diff] [blame] | 16 | from django.db.models.fields.related import ForeignKey, ManyToManyField |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 17 | |
Scott Baker | f303990 | 2015-10-23 12:20:15 -0700 | [diff] [blame] | 18 | options = None |
| 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 | |
Scott Baker | b2a8cc3 | 2015-10-20 11:13:50 -0700 | [diff] [blame] | 30 | def enum_classes(apps): |
| 31 | model_classes = [] |
| 32 | for app in apps: |
| 33 | app = app + ".models" |
| 34 | models_module = __import__(app) |
| 35 | for part in app.split(".")[1:]: |
| 36 | if hasattr(models_module, "PlCoreBase"): |
| 37 | break |
| 38 | models_module = getattr(models_module,part) |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 39 | |
Scott Baker | b2a8cc3 | 2015-10-20 11:13:50 -0700 | [diff] [blame] | 40 | PlCoreBase = getattr(models_module,"PlCoreBase") |
| 41 | |
| 42 | for classname in dir(models_module): |
| 43 | c = getattr(models_module, classname, None) |
Scott Baker | f303990 | 2015-10-23 12:20:15 -0700 | [diff] [blame] | 44 | if type(c)==type(PlCoreBase) and c.__name__ not in options.blacklist: |
Scott Baker | b2a8cc3 | 2015-10-20 11:13:50 -0700 | [diff] [blame] | 45 | model_classes.append(c) |
| 46 | |
| 47 | return model_classes |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 48 | |
| 49 | class GenObj(object): |
| 50 | def __str__(self): |
| 51 | return str(self.model.__name__.lower()) |
| 52 | |
| 53 | def __init__(self, m): |
| 54 | self.model = m |
| 55 | self.props = [] |
| 56 | self.refs = [] |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame] | 57 | self.plural_name = None |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 58 | |
| 59 | def plural(self): |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame] | 60 | if (self.plural_name): |
| 61 | return self.plural_name |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 62 | else: |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame] | 63 | name = str(self) |
| 64 | if (name.endswith('s')): |
| 65 | return name+'es' |
| 66 | else: |
| 67 | return name+'s' |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 68 | |
Scott Baker | f556b92 | 2014-11-10 15:58:58 -0800 | [diff] [blame] | 69 | def singular(self): |
| 70 | return str(self) |
| 71 | |
| 72 | def rest_name(self): |
| 73 | # These are things that either for historic reasons or due to incorrect naming, |
| 74 | # got called something different than the autogen thinks they should be |
| 75 | # called. |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 76 | REST_FIXUP = {'controllernetworkses': 'controllernetworks', |
| 77 | 'controllerimageses': 'controllerimages', |
| 78 | 'controllersliceses': 'controllerslices', |
| 79 | 'controlleruserses': 'controllerusers', |
Scott Baker | f556b92 | 2014-11-10 15:58:58 -0800 | [diff] [blame] | 80 | 'sitedeploymentses': 'sitedeployments', |
| 81 | 'siteroles': 'site_roles', |
| 82 | 'sliceprivileges': 'slice_privileges', |
| 83 | 'sliceroles': 'slice_roles', |
| 84 | } |
| 85 | return REST_FIXUP.get(self.plural(), self.plural()) |
| 86 | |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 87 | def camel(self): |
| 88 | name = str(self.model.__name__) |
Sapan Bhatia | df2b49e | 2014-01-28 19:41:07 -0500 | [diff] [blame] | 89 | return name |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 90 | |
| 91 | class Generator(dict): |
| 92 | def all(self): |
| 93 | return self.values() |
Scott Baker | 853060e | 2015-10-23 13:09:49 -0700 | [diff] [blame] | 94 | |
| 95 | def rest_models(self): |
| 96 | norest = [x.lower() for x in options.norest] |
| 97 | return [v for v in self.values() if not (str(v) in norest)] |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 98 | |
| 99 | def regex(self, r): |
| 100 | filtered = filter(lambda o:re.match(r,str(o)), self.values()) |
| 101 | return filtered |
| 102 | |
| 103 | def add_object(self, o): |
| 104 | obj = GenObj(o) |
| 105 | fields = o._meta.fields |
| 106 | self[str(obj).lower()]=obj |
| 107 | |
| 108 | def compute_links(self): |
| 109 | for obj in self.values(): |
Sapan Bhatia | b588540 | 2014-01-29 10:32:09 -0500 | [diff] [blame] | 110 | #if (str(obj)=='network'): |
Sapan Bhatia | cdd90b7 | 2014-01-28 20:03:13 -0500 | [diff] [blame] | 111 | # pdb.set_trace() |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 112 | fields = obj.model._meta.fields |
| 113 | for f in fields: |
Sapan Bhatia | b588540 | 2014-01-29 10:32:09 -0500 | [diff] [blame] | 114 | if (f and f.rel): |
| 115 | to_name = str(f.rel.to) |
| 116 | else: |
| 117 | to_name = None |
| 118 | |
| 119 | if type(f)==ForeignKey and to_name and to_name in self.keys(): |
| 120 | refobj = self[f.to_name] |
| 121 | |
| 122 | if (str(obj)=='slice' and f.to_name=='networks'): |
| 123 | obj.refs.append(refobj) |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame] | 124 | related_name = f.related_query_name() |
Sapan Bhatia | b588540 | 2014-01-29 10:32:09 -0500 | [diff] [blame] | 125 | if (related_name!='+' and related_name.lower()!=str(obj).lower()): |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame] | 126 | cobj = copy.deepcopy(obj) |
| 127 | cobj.multi = True |
| 128 | cobj.plural_name = related_name |
| 129 | refobj.refs.append(cobj) |
Scott Baker | 225184c | 2015-10-30 09:42:34 -0700 | [diff] [blame] | 130 | elif f.name.endswith("_ptr"): |
| 131 | # django inherited model, for example HPCService |
| 132 | # cause swagger and REST to break |
| 133 | pass |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 134 | else: |
| 135 | obj.props.append(f.name) |
Sapan Bhatia | b588540 | 2014-01-29 10:32:09 -0500 | [diff] [blame] | 136 | |
Sapan Bhatia | eb62ad6 | 2014-01-28 14:29:08 -0500 | [diff] [blame] | 137 | m2m = obj.model._meta.many_to_many |
| 138 | for f in m2m: |
| 139 | try: |
| 140 | related_model_name = f.m2m_reverse_field_name() |
| 141 | except: |
| 142 | related_model_name = f.m2m_db_table().rsplit('_',1)[-1] |
| 143 | |
Sapan Bhatia | b588540 | 2014-01-29 10:32:09 -0500 | [diff] [blame] | 144 | related_name = f.related_query_name() |
Sapan Bhatia | eb62ad6 | 2014-01-28 14:29:08 -0500 | [diff] [blame] | 145 | if related_model_name in self.keys(): |
Scott Baker | 015e13d | 2014-12-15 16:12:43 -0800 | [diff] [blame] | 146 | #print "XXX1", obj, f, related_name, related_model_name |
Sapan Bhatia | eb62ad6 | 2014-01-28 14:29:08 -0500 | [diff] [blame] | 147 | refobj = self[related_model_name] |
Sapan Bhatia | cdd90b7 | 2014-01-28 20:03:13 -0500 | [diff] [blame] | 148 | cobj = copy.deepcopy(obj) |
| 149 | cobj.multi=True |
| 150 | refobj.refs.append(cobj) |
Scott Baker | 1e67bb4 | 2014-07-03 17:58:10 -0700 | [diff] [blame] | 151 | |
| 152 | # deal with upgradeFrom_rel_+ |
| 153 | if (related_name.endswith("+")): |
| 154 | continue |
| 155 | |
Scott Baker | 853060e | 2015-10-23 13:09:49 -0700 | [diff] [blame] | 156 | if (related_name!='+') and related_model_name in self: # and related_name.lower()!=str(obj).lower()): |
Scott Baker | 015e13d | 2014-12-15 16:12:43 -0800 | [diff] [blame] | 157 | refobj = self[related_model_name] |
Scott Baker | 853060e | 2015-10-23 13:09:49 -0700 | [diff] [blame] | 158 | #print "XXX2", obj, f, related_name, related_model_name, refobj.plural_name |
Scott Baker | 015e13d | 2014-12-15 16:12:43 -0800 | [diff] [blame] | 159 | cobj = copy.deepcopy(refobj) |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame] | 160 | cobj.multi = True |
Sapan Bhatia | b588540 | 2014-01-29 10:32:09 -0500 | [diff] [blame] | 161 | |
Scott Baker | 015e13d | 2014-12-15 16:12:43 -0800 | [diff] [blame] | 162 | obj.refs.append(cobj) |
Sapan Bhatia | b588540 | 2014-01-29 10:32:09 -0500 | [diff] [blame] | 163 | |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame] | 164 | |
Sapan Bhatia | eb62ad6 | 2014-01-28 14:29:08 -0500 | [diff] [blame] | 165 | |
Scott Baker | f303990 | 2015-10-23 12:20:15 -0700 | [diff] [blame] | 166 | |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 167 | def main(): |
Scott Baker | f303990 | 2015-10-23 12:20:15 -0700 | [diff] [blame] | 168 | global options |
| 169 | parser = OptionParser(usage="modelgen [options] <template_fn>", ) |
| 170 | |
| 171 | parser.add_option("-a", "--app", dest="apps", |
| 172 | help="list of applications to parse", metavar="APP", default=[], action="append") |
| 173 | parser.add_option("-b", "--blacklist", dest="blacklist", |
| 174 | help="add model name to blacklist", metavar="MODEL", default=["SingletonModel", "PlCoreBase"], action="append") |
Scott Baker | 853060e | 2015-10-23 13:09:49 -0700 | [diff] [blame] | 175 | parser.add_option("-n", "--no-rest", dest="norest", |
| 176 | help="do not generate rest api for model", metavar="MODEL", default=["SingletonModel", "PlCoreBase"], action="append") |
Scott Baker | f303990 | 2015-10-23 12:20:15 -0700 | [diff] [blame] | 177 | |
| 178 | (options, args) = parser.parse_args(sys.argv[1:]) |
| 179 | |
| 180 | if not options.apps: |
| 181 | options.apps = ["core"] |
| 182 | |
| 183 | if len(args)!=1: |
| 184 | print 'Usage: modelgen [options] <template_fn>' |
| 185 | exit(1) |
| 186 | |
| 187 | output = args[0] |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 188 | |
| 189 | generator = Generator() |
| 190 | |
Scott Baker | f303990 | 2015-10-23 12:20:15 -0700 | [diff] [blame] | 191 | models = enum_classes(options.apps) |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 192 | |
| 193 | for m in models: |
| 194 | generator.add_object(m) |
| 195 | |
| 196 | generator.compute_links() |
| 197 | template_contents = open(output).read() |
| 198 | template = Template(template_contents) |
| 199 | context = Context({'generator':generator}) |
| 200 | print template.render(context) |
| 201 | |
| 202 | |
| 203 | if (__name__=='__main__'): |
| 204 | main() |