Example #1
0
 def renderController(self, model=None , baseBlock=None,outputStream=stdout, magicType='magic0', *args):
     MvcTemplateDirs= mvcPaths.getTemplatesDirs(magicType)
     MvcTemplateFiles = mvcPaths.getTemplateFiles(magicType)
     methods = None
     m = model
     if m:
         ops=[]
         if os.path.exists(MvcTemplateDirs['OPRTMPL_DIR']):
             ops = [x[:x.rindex('.')] for x in os.listdir(MvcTemplateDirs['OPRTMPL_DIR'])]
         methodTemplates = []
         for k in ops:
             methodTemplates.append(pjoin(MvcTemplateDirs['OPRTMPL_DIR'],k+'.txt'))
 #NOTICE: We dont need this. most probably we will need to walk thru template files only
 #        and not to make them dependend on the operations
 #        for k, v in settings.DEFAULT_OPERATIONS.iteritems():
 #            if isinstance(v['method'], str):
 #                methodTemplates.append(pjoin(MvcTemplateDirs['OPRTMPL_DIR'],v['method']+'.txt'))
 #            else:
 #                methodTemplates.append(pjoin(MvcTemplateDirs['OPRTMPL_DIR'],v['method'].__name__+'.txt'))
         methodTemplates = list(set(methodTemplates))
 
         methods = map(lambda x: render(x, {'m':m}), methodTemplates)
         classImport = Template('from ${modelsPath}.${modelModule} import ${modelClass}')\
             .substitute(modelsPath=basename(settings.MODELS_DIR),
                         modelModule=m.Package+settings.MODEL_MODULE_SUFIX,
                         modelClass=m.Name)
         formImport = Template('from ${formsPath}.${formModule} import ${formClass}')\
             .substitute(formsPath=basename(settings.FORM_MODELS_DIR),
                     formModule=m.Package+settings.MODEL_FORM_MODULE_SUFIX,
                     formClass=m.Name+settings.MODEL_FORM_CLASS_SUFIX)
         if not baseBlock['imports']:
             baseBlock.createEmptyBlocks('imports', cblPy)
         baseBlock['imports'].appendLines([classImport, formImport])
     baseBlock.appendText(self.render(MvcTemplateFiles['CTPath'], {'m':m, 'methods':methods}))
     outputStream.write(str(baseBlock))
Example #2
0
 def makeMvc(self, args):
     arg = args[0]
     package = ''
     name =''
     magicLevel = settings.MagicLevel
     modelList = []
     #TODO: set the MagicLevel
     #TODO: make try catch and display proper output if wrong syntax is entered
     if len(args)>1:
         if args[1][:len('path=')]=='path=':
             modelList = self.makeModelListFromFile(args[1][len('path='):])
         else:
             name = args[1][args[1].rindex('.')+1:]
             package = args[1][:args[1].rindex('.')]
     if len(args)>2:
         magicLevel = int(args[2])
     magicType = 'magic'+str(magicLevel)
 
     mvcTemplateFiles = mvcPaths.getTemplateFiles(magicType)
     mvcTemplateDirs = mvcPaths.getTemplatesDirs(magicType)
     if not modelList:
         m = Model()
         #TODO: Validation needs to be added here
         if not (name and package):
             m.Package = ask('PackageName: ', '*')
             m.Name = ask('ModelName: ', '*')
         else:
             m.Package = package
             m.Name = name
 
         if 'm' in arg:
             #TODO: set to according to an argument
             m.InheritsFrom = self.inherits_from
             i = 0
             print '.'*14+'class '+m.FullName+'('+m.InheritsFrom+'):'
             p = True #Do-While
             while p:
                 p = raw_input('Property'+str(i)+'>'+'.'*(9-len(str(i))))
                 if self.setProperties(p, m): i+=1
         else:
             #Create a Model instance out of specific class if exists
             raise NotImplemented('This feature is not implemented yet')
         modelList.append(m)
 
     for m in modelList:
         if ask('Save '+m.FullName+'?'):
             if 'm' in arg:
                 #Model setup
                 modelFile = locators.LocateModelModule(m.Package)
                 baseBlock = None
                 if exists(modelFile):
                     baseBlock = Block.loadFromFile(modelFile, self.cblPy)
                 else:
                     baseBlock =Block.loadFromFile(mvcTemplateFiles["MBTPath"], self.cblPy, self.render, {'m':m})
                 stream = open(modelFile, 'w')
                 self.renderModel(m, baseBlock, stream, magicType=magicType)
                 stream.close()
                 #End Model Setup
             if 'f' in arg:
                 #ModelForm Setup
                 mfPath = locators.LocateFormModelModule(m.Package)
                 baseBlock = None
                 if exists(mfPath):
                     baseBlock = Block.loadFromFile(mfPath, self.cblPy)
                 else:
                     baseBlock = Block.loadFromFile(mvcTemplateFiles['FBTPath'], self.cblPy, self.render, {'m':m})
                 stream = open(mfPath, 'w')
                 self.renderModelForm(m, baseBlock,stream,magicType)
                 stream.close()
                 #End ModelForm Setup
             if 'c' in arg:
                 #Controller Setup
                 controllerPath = locators.LocateControllerModule(m.Package)
                 baseBlock = None
                 if exists(controllerPath):
                     baseBlock = Block.loadFromFile(controllerPath, cblPy)
                 else:
                     baseBlock = Block.loadFromFile(mvcTemplateFiles["CBTPath"], self.cblPy, self.render, {'m':m})
                 stream = open(controllerPath, 'w')
                 self.renderController(m, baseBlock, stream, magicType, arg)
                 stream.close()
                 #End Controller Setup
             if 'v' in arg:
                 viewFolder = locators.LocatePagesDir(m.Package)
                 formFolder =locators.LocateFormsDir(m.Package)
                 #if we only want an empty view
                 if not m:
                     pass
                 #if we want to generate the view by the model provided
                 else:
                     #Find the operations for the templates
                     ops=[]
                     if os.path.exists(mvcTemplateDirs['FRMTMPL_DIR']):
                         ops = [x[:x.rindex('.')] for x in os.listdir(mvcTemplateDirs['FRMTMPL_DIR'])]
                     if ops:
                         for k in ops:
                             templateName = k
                             formTemplatePath = pjoin(mvcTemplateDirs['FRMTMPL_DIR'], templateName+'.txt')
                             viewPath = pjoin(viewFolder, m.Name+'_'+(k=='default' and [''] or [k])[0] +'.html')
                             baseBlock  = Block.loadFromFile(mvcTemplateFiles['VTPath'], self.cblHal, self.render,{'m':m,'formTemplate': m.Name+'Form_'+k })
                             baseBlock.saveToFile(viewPath)
 
                             formPath = pjoin(formFolder, m.Name+settings.FORM_VIEW_SUFFIX+'_'+k+'.html')
                             baseBlock  = Block.loadFromFile(formTemplatePath, self.cblHal, self.render,{'m':m})
                             baseBlock.saveToFile(formPath)
                     else: #here the magic will be used and we dont need some special view
                         viewPath = pjoin(viewFolder, m.Name+'.html')
                         baseBlock  = Block.loadFromFile(mvcTemplateFiles['VTPath'], self.cblHal, self.render,{'m':m})
                         baseBlock.saveToFile(viewPath)