def tac_file(options): """Generates the Twisted tac file from temnplate""" mgr=TemplateManager() t=mgr.get_sys_domain().get_template('tpl/tac.evoque') return t.evoque( app_name=options['app_name'], app_config=options['app_config'] )
def init_file(installPath, options): """Return the correct Init file for the current distribution.""" mgr = TemplateManager() t = mgr.get_sys_domain().get_template( 'rc.scripts/{0}.evoque'.format(distro)) return t.evoque(app_name=options['app_name'], app_desc=options['app_desc'], app_file=options['app_file'], app_log=options['app_log'], app_share=installPath['share'])
def project_file(options): """Generates the Goliat project file from template""" mgr = TemplateManager() t = mgr.get_sys_domain().get_template('tpl/project.evoque') return t.evoque(goliat_ver=options['goliat_ver'], project_ver=options['app_version'], app_name=options['app_name'], app_desc=options['app_desc'], app_layout=options['app_layout'], app_port=options['app_port'])
def main_js_file(options): """Generates the Goliat main application JavaScript file from template""" mgr = TemplateManager() t = mgr.get_sys_domain().get_template('tpl/mainJs.evoque') return t.evoque( app_name=options['app_name'].replace(' ', ''), app_version=options['app_version'], app_layout='Goliat.layout.'+''.join([p.capitalize() \ for p in options['app_layout'].split('_')]) )
def main_js_file(options): """Generates the Goliat main application JavaScript file from template""" mgr=TemplateManager() t=mgr.get_sys_domain().get_template('tpl/mainJs.evoque') return t.evoque( app_name=options['app_name'].replace(' ', ''), app_version=options['app_version'], app_layout='Goliat.layout.'+''.join([p.capitalize() \ for p in options['app_layout'].split('_')]) )
def init_file(installPath, options): """Return the correct Init file for the current distribution.""" mgr=TemplateManager() t=mgr.get_sys_domain().get_template('rc.scripts/{0}.evoque'.format(distro)) return t.evoque( app_name=options['app_name'], app_desc=options['app_desc'], app_file=options['app_file'], app_log=options['app_log'], app_share=installPath['share'] )
def project_file(options): """Generates the Goliat project file from template""" mgr=TemplateManager() t=mgr.get_sys_domain().get_template('tpl/project.evoque') return t.evoque( goliat_ver=options['goliat_ver'], project_ver=options['app_version'], app_name=options['app_name'], app_desc=options['app_desc'], app_layout=options['app_layout'], app_port=options['app_port'] )
class Generator(object): _schema = None _mgr = TemplateManager() def __init__(self, verbose=False): self._verbose = verbose self._schema = Schema('config/schema.yaml') if self._verbose: print bold('Fixing null values on schema...') (success, msg) = self._schema.fix_tables() if not success: raise SchemaException(msg) if self._verbose: print green('Schema fixed!') def generate_models(self): for table, columns in self._schema.get_tables().iteritems(): mod_name, tpl = self.generate_model_base(table, columns) self.write_base_model(mod_name, tpl) mod_name, tpl = self.generate_model(table, columns) self.write_model(mod_name, tpl) def create_b(self, table, columns): templates = { 'base': self.generate_model_base(table, columns), 'rel': self.generate_many_2_many(table, columns) } return templates def create_m(self, table, columns, path): return {'work': self.generate_model(table, columns, path)} def generate_model(self, table, columns, path): t = self._mgr.get_sys_domain().get_template('tpl/model.evoque') model_name = self._generate_model_name(table) return (model_name, t.evoque( model_name=model_name, module_register_path=path, model_creation_date=datetime.now(), model_file='application/model/{0}'.format(model_name), )) def generate_model_base(self, table, columns): t = self._mgr.get_sys_domain().get_template('tpl/modelbase.evoque') relation = columns.get('_relation') model_name = self._generate_model_name(table) _attributes = [] _relations = [] _model_primary_keys = self._check_composed_keys(columns) cfg = config.ConfigManager().look_at_cur_path() if cfg == None: print red('No goliat configuration file found.') sys.exit(-1) for col in self._schema.reorder_table_fields( self._schema.find_table(table)): if col[0] in [ '_config', '_indexes', '_relation', '_order', '_parent' ]: continue attr_name = col[0] if len(_model_primary_keys): attr_type = self._parse_column(col[1], True) else: attr_type = self._parse_column(col[1]) _attributes.append((attr_name, attr_type)) if relation != None: for field, rel in relation.iteritems(): if rel['type'] == 'one2one': _reference = 'Reference' _attributes.append((field, '{0}({1}_id, "{2}.{3}")' \ .format(_reference, field, self._generate_model_name(\ rel['foreignTable']), rel['foreignKey']))) _relations.append(('application.model.base.{0}Base' \ .format(self._generate_model_name( rel['foreignTable'])), '{0}Base'.format(self._generate_model_name( rel['foreignTable'])))) elif rel['type'] == 'many2one': _reference = 'ReferenceSet' _attributes.append( (field, '{0}'.format('{0}("{1}.{2}", "{3}.{4}")'.format( _reference, self._generate_model_name(table), rel['localKey'], self._generate_model_name(rel['foreignTable']), rel['foreignKey'])))) _relations.append(('application.model.base.{0}Base' \ .format(self._generate_model_name(rel['foreignTable'])), '{0}Base'.format(self._generate_model_name( rel['foreignTable'])))) elif rel['type'] == 'many2many': _reference = 'ReferenceSet' reference = _reference + '(' reference += self._parse_relation(rel, table) reference += ')' _attributes.append(('{0}'.format(field), reference)) #_relations.append(('application.model.base.{0}Base' \ #.format(self._generate_model_name(rel['foreignTable'])), # '{0}Base'.format(self._generate_model_name( # rel['foreignTable'])))) _relations.append(( 'application.model.relation.{0}'.format( self._generate_model_name(table)+\ self._generate_model_name(rel['foreignTable'])), self._generate_model_name(table)+\ self._generate_model_name(rel['foreignTable']) )) reverse = self._schema.find_reverse_reference(table) if reverse != None: reverse_reference = 'ReferenceSet(' reverse_reference += self._parse_relation(reverse, table, True) reverse_reference += ')' _attributes.append( ('{0}'.format(reverse['field']), reverse_reference)) _relations.append(( 'application.model.relation.{0}'.format( self._generate_model_name(reverse['foreignTable'])+\ self._generate_model_name(table)), self._generate_model_name(reverse['foreignTable'])+\ self._generate_model_name(table) )) return (model_name, t.evoque( model_name=model_name, model_creation_date=datetime.now(), model_file='application/model/base/{0}'.format(model_name), model_table='{0}'.format(table), model_primary_keys=_model_primary_keys, attributes=_attributes, relations=_relations)) def generate_many_2_many(self, table, columns): if columns.get('_relation') == None: return None models = [] for field, relation in columns['_relation'].iteritems(): if not self._analyze(relation): raise SchemaException('%s table has an invalid _relation ' \ 'section, please fix it!!!'%(table)) if relation['type'] != 'many2many': continue # Not a m2m relationship t=self._mgr.get_sys_domain().get_template(\ 'tpl/modelrelation.evoque') modelSuffix=''.join([ word.capitalize() \ for word in table.split('_') ]) modelPreffix=''.join([ word.capitalize() \ for word in relation['foreignTable'].split('_') ]) model_name = modelSuffix + modelPreffix _model_primary_keys = self._generate_primary_keys(relation['keys']) _attributes = [] for key in relation['keys']: attr_name = key if type(key) == str else key.keys()[0] attr_type = 'Int()' _attributes.append((attr_name, attr_type)) if relation.get('fields') != None: for field in relation['fields']: for fname, fvalue in field.iteritems(): attr_name = fname attr_type = self._parse_column(fvalue, True) _attributes.append((attr_name, attr_type)) models.append( (model_name, t.evoque(model_name=model_name, model_creation_date=datetime.now(), model_file='application/model/relation/{0}'.format( model_name), model_table='{0}'.format(table + '_' + relation['foreignTable']), model_primary_keys=_model_primary_keys, attributes=_attributes))) return models def write_base_model(self, mod_name, tpl): fp = file('application/model/base/{0}Base.py'.format(mod_name), 'w') fp.write(tpl.encode('utf8')) fp.close() def write_model(self, mod_name, tpl): fp = file('application/model/{0}.py'.format(mod_name), 'w') fp.write(tpl.encode('utf8')) fp.close() def write_relation(self, mod_name, tpl): fp = file('application/model/relation/{0}.py'.format(mod_name), 'w') fp.write(tpl.encode('utf8')) fp.close() def _generate_model_name(self, table): return ''.join([word.capitalize() for word in table.split('_')]) def _check_composed_keys(self, cols): keys = [] for name, col in cols.iteritems(): if col in ['_relation', '_config', '_indexes']: continue if 'primaryKey' in col: keys.append(name) if len(keys) > 1: return self._generate_primary_keys(keys) return '' def _generate_primary_keys(self, keys): _new_keys = [] for key in keys: if type(key) == str: _new_keys.append(key) elif type(key) == dict: _new_keys.append(key.keys()[0]) return '__storm_primary__ = '+','.join([ '"'+k+'"' \ for k in _new_keys ]) def _analyze(self, config): """Analyzes a table _config section""" if config.get('type') == None or config.get('foreignTable') == None: return False return True def _parse_column(self, col, special=False): _primary = 'False' _allow_none = 'True' _default_value = 'Undef' _foreign_keys = False if col.get('type') != None: if col.get('primaryKey') != None and col.get('primaryKey') == True: _primary = 'True' if col.get('required') != None and col.get('required') == True: _allow_none = 'False' if col.get('default') != None: _default_value = col.get('default') if not special: return '{0}(primary={1}, value={2}, allow_none={3})' \ .format(tr(col.get('type')), _primary, _default_value, _allow_none) else: return '{0}(value={1}, allow_none={2})' \ .format(tr(col.get('type')), _default_value, _allow_none) def _parse_relation(self, rel, table, reverse=False): """Parses a model table relation.""" _new_keys = [] for key in rel['keys']: if type(key) == str: _new_keys.append(key) elif type(key) == dict: _new_keys.append(key.keys()[0]) if not reverse: ret = '"{0}.{1}", '.format(self._generate_model_name(table), rel['localKey']) ret+='"{0}.{1}", '.format(self._generate_model_name(table)+\ self._generate_model_name(rel['foreignTable']), _new_keys[0]) ret+='"{0}.{1}", '.format(self._generate_model_name(table)+\ self._generate_model_name(rel['foreignTable']), _new_keys[1]) ret += '"{0}.{1}"'.format( self._generate_model_name(rel['foreignTable']), rel['foreignKey']) else: ret = '"{0}.{1}", '.format(self._generate_model_name(table), rel['localKey']) ret += '"{0}.{1}", '.format( self._generate_model_name(rel['foreignTable']) + self._generate_model_name(table), _new_keys[0]) ret+='"{0}.{1}", '.format(self._generate_model_name( rel['foreignTable'])+\ self._generate_model_name(table), _new_keys[1]) ret += '"{0}.{1}"'.format( self._generate_model_name(rel['foreignTable']), rel['foreignKey']) return ret
def __init__(self): self.tplmgr = TemplateManager() resource.Resource.__init__(self)
Returns true if the user is active. """ return self.userdata.is_active def __rpr__(self): """ Representation of a user. """ repr = dict() repr['sid'] = self.session.uid repr['uid'] = self.get_uid() repr['name'] = self.get_name() repr['groups'] = self.get_groups() if self.get_creation_date() != None: repr['creation_date'] = self.get_creation_date().isoformat() else: repr['creation_date'] = '' if self.get_last_login() != None: repr['last_login'] = self.get_last_login().isoformat() else: repr['last_login'] = '' repr['active'] = self.is_active() return repr registerAdapter(User, GoliatSession, IUser) user_sql_data = yaml.load(TemplateManager().get_sys_domain().get_template( 'tpl/usertable.evoque').evoque())
def tac_file(options): """Generates the Twisted tac file from temnplate""" mgr = TemplateManager() t = mgr.get_sys_domain().get_template('tpl/tac.evoque') return t.evoque(app_name=options['app_name'], app_config=options['app_config'])
def schema_file(): """Generates the Goliat database schema file template""" mgr = TemplateManager() t = mgr.get_sys_domain().get_template('tpl/schema.evoque') return t.evoque()
def schema_file(): """Generates the Goliat database schema file template""" mgr=TemplateManager() t=mgr.get_sys_domain().get_template('tpl/schema.evoque') return t.evoque()
def perform(self, args): cfg=self._look_at_cur_path() module_name, opts=self.parse_args(args) if not len(module_name): print self.long_help() sys.exit(-1) try: _schema=Schema('config/schema.yaml') _schema.fix_tables() except TypeError: if opts.get('model')!=None: print red('The schema is not defined.') sys.exit(-1) _module_model_import='' _module_get_schema_model='' if opts.get('path')==None: _module_register_path='"{0}"'.format(module_name.lower()) else: _module_register_path='"{0}"'.format(opts['path']) if opts.get('model')!=None: if not check_model(opts['model']): print red(\ '\n{0} model does not exist at the project schema.\nUse ' \ 'module -l or module --list to show a list of available ' \ 'models.'.format(\ opts['model'] if len(opts['model']) else 'Noname')) sys.exit(0) gen=Generator(opts['verbose']) _use_model=True _model_name=gen._generate_model_name(opts['model']) tmodel=gen.create_m(\ opts['model'], _schema.find_table(opts['model']), _module_register_path) _module_model_import='from application.model.{0} import {1}' \ .format(gen._generate_model_name(opts['model']), gen._generate_model_name(opts['model'])) _module_get_schema_model='''def get_schema_model(self, request, **kwargs): """Return the schema model %s architecture.""" model_schema, model_view = %s.get_model_info() if model_schema == None: return json.dumps({ "success" : False, "error" : "Unable to fetch a schema for model %s" }) return json.dumps({ "success" : True, "model" : model_schema, "view" : model_view })'''%(gen._generate_model_name(opts['model']), gen._generate_model_name(opts['model']), opts['model']) print '\n'+bold('Generating {0} module...'.format(module_name)) mgr=TemplateManager() t=mgr.get_sys_domain().get_template('tpl/module.evoque') module=t.evoque( module_file="application/controller/{0}".format(module_name), module_creation_date=datetime.now(), module_name=module_name, module_model_import=_module_model_import, module_register_path=_module_register_path, module_get_schema_model=_module_get_schema_model, use_model=_use_model, model_name=_model_name ) if opts['dump']: if opts.get('model')!=None: print '\napplication/model/{0}.py'.format(tmodel['work'][0]) print tmodel['work'][1] print '\napplication/controller/{0}.py'.format(module_name) print module else : if opts.get('model')!=None: gen.write_model(tmodel['work'][0], tmodel['work'][1]) fp=file('application/controller/{0}.py'.format(module_name), 'w') fp.write(module.encode('utf8')) fp.close() print bold('Module created successfully.')