Improve modularity of jinja2 extensions

Change-Id: I86549a7a5002fa88008a4debfa4b5cf15a756c06
diff --git a/lib/xos-genx/MANIFEST.in b/lib/xos-genx/MANIFEST.in
index 3629bae..f6da5db 100644
--- a/lib/xos-genx/MANIFEST.in
+++ b/lib/xos-genx/MANIFEST.in
@@ -1 +1,2 @@
-include xosgenx/targets/*
\ No newline at end of file
+include xosgenx/targets/*
+include xosgenx/jinja2_extensions/*
diff --git a/lib/xos-genx/setup.py b/lib/xos-genx/setup.py
index cdb2b3a..bc1abc9 100644
--- a/lib/xos-genx/setup.py
+++ b/lib/xos-genx/setup.py
@@ -6,8 +6,8 @@
       version='1.0',
       description='XOS Generative Toolchain',
       author='Sapan Bhatia, Matteo Scandolo',
-      author_email='sapan@onlab.us, teo@onlab.us',
+      author_email='sapan@opennetworking.org, teo@opennetworking.org',
       packages=['xosgenx'],
       scripts=['bin/xosgenx'],
       include_package_data=True,
-     )
\ No newline at end of file
+     )
diff --git a/lib/xos-genx/xosgenx/jinja2_extensions/__init__.py b/lib/xos-genx/xosgenx/jinja2_extensions/__init__.py
new file mode 100644
index 0000000..659f440
--- /dev/null
+++ b/lib/xos-genx/xosgenx/jinja2_extensions/__init__.py
@@ -0,0 +1,2 @@
+from .django import *
+from .base import *
diff --git a/lib/xos-genx/xosgenx/jinja2_extensions.py b/lib/xos-genx/xosgenx/jinja2_extensions/base.py
similarity index 64%
rename from lib/xos-genx/xosgenx/jinja2_extensions.py
rename to lib/xos-genx/xosgenx/jinja2_extensions/base.py
index f07aa14..df57858 100644
--- a/lib/xos-genx/xosgenx/jinja2_extensions.py
+++ b/lib/xos-genx/xosgenx/jinja2_extensions/base.py
@@ -43,11 +43,6 @@
 
     return plural
 
-def xproto_unquote(s):
-    if (s.startswith('"') and s.endswith('"')):
-        s = s[1:-1]
-    return s
-
 def xproto_links_to_modeldef_relations(llst):
     outlist = []
     seen = []
@@ -63,38 +58,6 @@
     
     return outlist
 
-def django_content_type_string(xptags):
-    # Check possibility of KeyError in caller
-    content_type = xptags['content_type']
-
-    try:
-        content_type = eval(content_type)
-    except:
-        pass
-
-    if (content_type=='url'):
-        return 'URLField'
-    if (content_type=='date'):
-        return 'DateTimeField'
-    elif (content_type=='ip'):
-        return 'GenericIPAddressField'
-    elif (content_type=='stripped' or content_type=='"stripped"'):
-        return 'StrippedCharField'
-    else:
-        raise Exception('Unknown Type: %s'%content_type)
-
-def django_string_type(xptags):
-    try:
-        max_length = eval(xptags['max_length'])
-    except:
-        max_length = 1024 * 1024
-
-    if ('content_type' in xptags):
-        return django_content_type_string(xptags)
-    elif (max_length<1024*1024):
-        return 'CharField'
-    else:
-        return 'TextField'
 
 def xproto_base_def(model_name, base):
     if (model_name=='XOSBase'):
@@ -118,115 +81,6 @@
 
     return field['type']
 
-def xproto_django_type(xptype, xptags):
-    if (xptype=='string'):
-        return django_string_type(xptags)
-    elif (xptype=='float'):
-        return 'FloatField'
-    elif (xptype=='bool'):
-        return 'BooleanField'
-    elif (xptype=='uint32'):
-        return 'IntegerField'
-    elif (xptype=='int32'):
-        return 'IntegerField'
-    elif (xptype=='int64'):
-        return 'BigIntegerField'
-    else:
-        raise Exception('Unknown Type: %s'%xptype)
-
-
-
-def xproto_django_link_type(f):
-    if (f['link_type']=='manytoone'):
-        return 'ForeignKey'
-    elif (f['link_type']=='manytomany'):
-        if (f['dst_port']):
-            return 'ManyToManyField'
-        else:
-            return 'GenericRelation'
-
-def format_options_string(d):
-    if (not d):
-        return ''
-    else:
-
-        lst = []
-        for k,v in d.items():
-            if (type(v)==str and k=='default' and v.endswith('()"')):
-                lst.append('%s = %s'%(k,v[1:-3]))
-            elif (type(v)==str and v.startswith('"')): 
-                try:
-                    tup = eval(v[1:-1])
-                    if (type(tup)==tuple):
-                        lst.append('%s = %r'%(k,tup))
-                    else:
-                        lst.append('%s = %s'%(k,v))
-                except:
-                    lst.append('%s = %s'%(k,v))
-            elif (type(v)==bool):
-                lst.append('%s = %r'%(k,bool(v)))
-            else:
-                try:
-                    lst.append('%s = %r'%(k,int(v)))
-                except ValueError:
-                    lst.append('%s = %s'%(k,v))
-
-        return ', '.join(lst)
-
-def map_xproto_to_django(f):
-    allowed_keys=['help_text','default','max_length','modifier','blank','choices','db_index','null','editable','on_delete','verbose_name', 'auto_now_add']
-
-    m = {'modifier':{'optional':True, 'required':False, '_target':'null'}}
-    out = {}
-
-    for k,v in f['options'].items():
-        if (k in allowed_keys):
-            try:
-                kv2 = m[k]
-                out[kv2['_target']] = kv2[v]
-            except:
-                out[k] = v
-    return out
-
-
-def xproto_django_link_options_str(field, dport=None):
-    output_dict = map_xproto_to_django(field)
-
-    if (dport and (dport=='+' or '+' not in dport)):
-        output_dict['related_name'] = '%r'%dport
-
-    try:
-        if field['through']:
-            d = {}
-            if isinstance(field['through'], str):
-                split = field['through'].rsplit('.',1)
-                d['name'] = split[-1]
-                if len(split)==2:
-                    d['package'] = split[0]
-                    d['fqn'] = 'package' + '.' + d['name']
-                else:
-                    d['fqn'] = d['name']
-                    d['package'] = ''
-            else:
-                d = field['through']
-
-            if not d['name'].endswith('_'+field['name']):
-                output_dict['through'] = '%r'%d['fqn']
-    except KeyError:
-        pass
-
-    return format_options_string(output_dict)
-
-def xproto_django_options_str(field, dport=None):
-    output_dict = map_xproto_to_django(field)
-
-    if (dport=='_'):
-        dport = '+'
-
-    if (dport and (dport=='+' or '+' not in dport)):
-        output_dict['related_name'] = '%r'%dport
-
-    return format_options_string(output_dict)
 
 def xproto_base_name(n):
     # Hack - Refactor NetworkParameter* to make this go away
@@ -443,4 +297,3 @@
         return "boolean"
     else:
         return type
-
diff --git a/lib/xos-genx/xosgenx/jinja2_extensions/django.py b/lib/xos-genx/xosgenx/jinja2_extensions/django.py
new file mode 100644
index 0000000..9c52fbd
--- /dev/null
+++ b/lib/xos-genx/xosgenx/jinja2_extensions/django.py
@@ -0,0 +1,139 @@
+def django_content_type_string(xptags):
+    # Check possibility of KeyError in caller
+    content_type = xptags['content_type']
+
+    try:
+        content_type = eval(content_type)
+    except:
+        pass
+
+    if (content_type=='url'):
+        return 'URLField'
+    if (content_type=='date'):
+        return 'DateTimeField'
+    elif (content_type=='ip'):
+        return 'GenericIPAddressField'
+    elif (content_type=='stripped' or content_type=='"stripped"'):
+        return 'StrippedCharField'
+    else:
+        raise Exception('Unknown Type: %s'%content_type)
+
+def django_string_type(xptags):
+    try:
+        max_length = eval(xptags['max_length'])
+    except:
+        max_length = 1024 * 1024
+
+    if ('content_type' in xptags):
+        return django_content_type_string(xptags)
+    elif (max_length<1024*1024):
+        return 'CharField'
+    else:
+        return 'TextField'
+
+def xproto_django_type(xptype, xptags):
+    if (xptype=='string'):
+        return django_string_type(xptags)
+    elif (xptype=='float'):
+        return 'FloatField'
+    elif (xptype=='bool'):
+        return 'BooleanField'
+    elif (xptype=='uint32'):
+        return 'IntegerField'
+    elif (xptype=='int32'):
+        return 'IntegerField'
+    elif (xptype=='int64'):
+        return 'BigIntegerField'
+    else:
+        raise Exception('Unknown Type: %s'%xptype)
+
+def xproto_django_link_type(f):
+    if (f['link_type']=='manytoone'):
+        return 'ForeignKey'
+    elif (f['link_type']=='manytomany'):
+        if (f['dst_port']):
+            return 'ManyToManyField'
+        else:
+            return 'GenericRelation'
+
+def map_xproto_to_django(f):
+    allowed_keys=['help_text','default','max_length','modifier','blank','choices','db_index','null','editable','on_delete','verbose_name', 'auto_now_add']
+
+    m = {'modifier':{'optional':True, 'required':False, '_target':'null'}}
+    out = {}
+
+    for k,v in f['options'].items():
+        if (k in allowed_keys):
+            try:
+                kv2 = m[k]
+                out[kv2['_target']] = kv2[v]
+            except:
+                out[k] = v
+    return out
+
+def xproto_django_link_options_str(field, dport=None):
+    output_dict = map_xproto_to_django(field)
+
+    if (dport and (dport=='+' or '+' not in dport)):
+        output_dict['related_name'] = '%r'%dport
+
+    try:
+        if field['through']:
+            d = {}
+            if isinstance(field['through'], str):
+                split = field['through'].rsplit('.',1)
+                d['name'] = split[-1]
+                if len(split)==2:
+                    d['package'] = split[0]
+                    d['fqn'] = 'package' + '.' + d['name']
+                else:
+                    d['fqn'] = d['name']
+                    d['package'] = ''
+            else:
+                d = field['through']
+
+            if not d['name'].endswith('_'+field['name']):
+                output_dict['through'] = '%r'%d['fqn']
+    except KeyError:
+        pass
+
+    return format_options_string(output_dict)
+
+def format_options_string(d):
+    if (not d):
+        return ''
+    else:
+
+        lst = []
+        for k,v in d.items():
+            if (type(v)==str and k=='default' and v.endswith('()"')):
+                lst.append('%s = %s'%(k,v[1:-3]))
+            elif (type(v)==str and v.startswith('"')): 
+                try:
+                    tup = eval(v[1:-1])
+                    if (type(tup)==tuple):
+                        lst.append('%s = %r'%(k,tup))
+                    else:
+                        lst.append('%s = %s'%(k,v))
+                except:
+                    lst.append('%s = %s'%(k,v))
+            elif (type(v)==bool):
+                lst.append('%s = %r'%(k,bool(v)))
+            else:
+                try:
+                    lst.append('%s = %r'%(k,int(v)))
+                except ValueError:
+                    lst.append('%s = %s'%(k,v))
+
+        return ', '.join(lst)
+
+def xproto_django_options_str(field, dport=None):
+    output_dict = map_xproto_to_django(field)
+
+    if (dport=='_'):
+        dport = '+'
+
+    if (dport and (dport=='+' or '+' not in dport)):
+        output_dict['related_name'] = '%r'%dport
+
+    return format_options_string(output_dict)