Пример #1
0
def renderController(model=None , baseBlock=None,outputStream=stdout, magicType='magic0', *args):
    MvcTemplateDirs= mvcPaths.getTemplatesDirs(magicType)
    MvcTemplateFiles = mvcPaths.getTemplateFiles(magicType)
    methods = None
    m = model
    if m:
        methodTemplates = []
        for k, v in settings.DEFAULT_OPERATIONS.iteritems():
            if isinstance(v['method'], str):
                if exists(pjoin(MvcTemplateDirs['OPRTMPL_DIR'],v['method']+'.txt')):
                    methodTemplates.append(pjoin(MvcTemplateDirs['OPRTMPL_DIR'],v['method']+'.txt'))
            else:
                if exists(pjoin(MvcTemplateDirs['OPRTMPL_DIR'],v['method'].__name__+'.txt')):
                    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)
        baseBlock['imports'].appendLines([classImport, formImport])
    baseBlock.appendText(render(MvcTemplateFiles['CTPath'], {'m':m, 'methods':methods}))
    outputStream.write(str(baseBlock))
Пример #2
0
def renderController(model=None,
                     baseBlock=None,
                     outputStream=stdout,
                     magicType='magic0',
                     *args):
    MvcTemplateDirs = mvcPaths.getTemplatesDirs(magicType)
    MvcTemplateFiles = mvcPaths.getTemplateFiles(magicType)
    methods = None
    m = model
    if m:
        methodTemplates = []
        for k, v in settings.DEFAULT_OPERATIONS.iteritems():
            if isinstance(v['method'], str):
                if exists(
                        pjoin(MvcTemplateDirs['OPRTMPL_DIR'],
                              v['method'] + '.txt')):
                    methodTemplates.append(
                        pjoin(MvcTemplateDirs['OPRTMPL_DIR'],
                              v['method'] + '.txt'))
            else:
                if exists(
                        pjoin(MvcTemplateDirs['OPRTMPL_DIR'],
                              v['method'].__name__ + '.txt')):
                    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)
        baseBlock['imports'].appendLines([classImport, formImport])
    baseBlock.appendText(
        render(MvcTemplateFiles['CTPath'], {
            'm': m,
            'methods': methods
        }))
    outputStream.write(str(baseBlock))
Пример #3
0
def makeMvc(args):
    arg = args[0]
    package = ''
    name = ''
    magicLevel = settings.MagicLevel
    modelList = []
    #TODO: set the MagicLevel
    #TODO: make try catch and display proper output if wrong sintax is entered
    if len(args) > 1:
        if args[1][:len('path=')] == 'path=':
            modelList = makeModelFromFile(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 = 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 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, cblPy)
                else:
                    baseBlock = Block.loadFromFile(mvcTemplateFiles["MBTPath"],
                                                   cblPy, render, {'m': m})
                stream = open(modelFile, 'w')
                renderModel(m, baseBlock, stream, magicType=magicType, arg)
                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, cblPy)
                else:
                    baseBlock = Block.loadFromFile(mvcTemplateFiles['FBTPath'],
                                                   cblPy, render, {'m': m})
                stream = open(mfPath, 'w')
                renderModelForm(m, baseBlock, stream, magicType, arg)
                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"],
                                                   cblPy, render, {'m': m})
                stream = open(controllerPath, 'w')
                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:
                    #if we dont want to use magic but to generate the default code instead
                    if not magicLevel:
                        for k, v in operations:
                            templateName = isinstance(
                                v['method'],
                                str) and v['method'] or v['method'].__name__
                            formTemplatePath = pjoin(
                                mvcTemplateDirs['FRMTMPL_DIR'],
                                templateName + '.txt')
                            viewPath = pjoin(
                                viewFolder, m.Name + '_' +
                                (k == 'default' and [''] or [k])[0] + '.html')
                            baseBlock = Block.loadFromFile(
                                mvcTemplateFiles['VTPath'], cblHal, 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, cblHal, render, {'m': m})
                            baseBlock.saveToFile(formPath)
                    else:  #here the magic will be used and we dont need some special view
                        pass
            if 'h' in arg:
                #HandlerMap Setup
                baseBlock = Block.loadFromFile(
                    config.proj_settings.HANDLER_MAP_FILE, cblPy)
                stream = open(config.proj_settings.HANDLER_MAP_FILE, 'w')
                renderHandlerMap(m, baseBlock, stream, magicLevel, arg)
                stream.close()
Пример #4
0
def makeMvc(args):
    arg = args[0]
    package = ''
    name =''
    magicLevel = settings.MagicLevel
    modelList = []
    #TODO: set the MagicLevel
    #TODO: make try catch and display proper output if wrong sintax is entered
    if len(args)>1:
        if args[1][:len('path=')]=='path=':
            modelList = makeModelFromFile(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 = 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 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, cblPy)
                else:
                    baseBlock =Block.loadFromFile(mvcTemplateFiles["MBTPath"], cblPy, render, {'m':m})
                stream = open(modelFile, 'w')
                renderModel(m, baseBlock, stream, magicType=magicType, arg)
                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, cblPy)
                else:
                    baseBlock = Block.loadFromFile(mvcTemplateFiles['FBTPath'], cblPy, render, {'m':m})
                stream = open(mfPath, 'w')
                renderModelForm(m, baseBlock,stream,magicType, arg)
                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"], cblPy, render, {'m':m})
                stream = open(controllerPath, 'w')
                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:
                    #if we dont want to use magic but to generate the default code instead
                    if not magicLevel:
                        for k, v in operations:
                            templateName = isinstance(v['method'],str) and v['method'] or v['method'].__name__
                            formTemplatePath = pjoin(mvcTemplateDirs['FRMTMPL_DIR'], templateName+'.txt')
                            viewPath = pjoin(viewFolder, m.Name+'_'+(k=='default' and [''] or [k])[0] +'.html')
                            baseBlock  = Block.loadFromFile(mvcTemplateFiles['VTPath'], cblHal, 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, cblHal, render,{'m':m})
                            baseBlock.saveToFile(formPath)
                    else: #here the magic will be used and we dont need some special view
                        pass
            if 'h' in arg:
                #HandlerMap Setup
                baseBlock = Block.loadFromFile(config.proj_settings.HANDLER_MAP_FILE, cblPy)
                stream = open(config.proj_settings.HANDLER_MAP_FILE, 'w')
                renderHandlerMap(m, baseBlock, stream, magicLevel, arg)
                stream.close()