一个简单的Python MVC框架(4)

来源:互联网 发布:2017耽美网络剧百度云 编辑:程序博客网 时间:2024/04/28 00:47

前面都是准备,这里是整个Web Mvc框架的核心地方,稍微多介绍一下。核心类是一个叫Dspth的模块。这里我没有处理路由,一个是不太熟,另外一个是主要是体会架构。我用的路由规则如下:
1)用URL的地址参数进行路由,有两个参数,一个是ctr,表示控制类,一个是act表示执行的方法
2)所有执行方法都约定参数格式如下
self,Environ,CGI,CGITB,Form,Cookies,SessionId,*Args
当然,真正做框架的时候,可以把Environ,CGI,CGITB,Form,Cookies,SessionId这些参数用一个对象包含,通过全局性变量传递给控制类。或者放在控制类的基类中,也可以直接赋给控制对象(脚本语言这样处理非常方便)
下面是路由模块:

from os import environimport cgi, cgitbfrom OsHelper import OsHelperimport glfrom HtmlHelper import HtmlToolstheCGI = cgi;theCgitb = cgitb;theForm = cgi.FieldStorage()theEnviron = environ;theControllerName = theForm.getvalue('ctr')theAction = theForm.getvalue('act')theCookies=HtmlTools.GetCookies(theEnviron)theSessionId = theCookies.get('SESSIONID')theCanDo=True#写入响应头信息.if theSessionId==None:    theSessionId = OsHelper.GetGuid()print('Content-type:text/html;')print("Set-Cookie:SESSIONID=%s;" % theSessionId)print("Set-Cookie:UserID=XYZ;")print("Set-Cookie:Password=XYZ123;")print("Set-Cookie:Expires=Tuesday, 31-Dec-2007 23:12:40 GMT\";")print("Set-Cookie:Domain=www.w3cschool.cc;")print("Set-Cookie:Path=/perl;")print("\r\n\r\n")if theControllerName=='' or theControllerName==None:    print("<html><head></head><body>控制名不存在</body></html>")    theCanDo = False    exitif theAction=='' or theAction==None:    print("<html><head></head><body>动作不存在</body></html>")    theCanDo = False    exit#导入控制类模块theModule = OsHelper.LoadModule(theControllerName)if theModule==None:    print("<html><head></head><body>控制模块不存在</body></html>")    theCanDo = False    exit#导入控制类类型theClass = OsHelper.LoadClass(theModule,theControllerName)if theClass==None:    print("<html><head></head><body>控制类不存在</body></html>")    theCanDo = False    exit#实例化控制类theInst = theClass()
#导入控制的Action方法theMethod = OsHelper.LoadClass(theInst,theAction)if theMethod==None:    print("<html><head></head><body>响应不存在</body></html>")    theCanDo = False    exit#执行控制类的目标方法if theCanDo:    theMethod(theEnviron,theCGI,theCgitb,theForm,theCookies,theSessionId)

下面是一个简单的控制类:

import glfrom OsHelper import OsHelperfrom HtmlHelper import HtmlToolsclass Person:    def __init__(self):        self.first_name=''        self.last_name=''class Test:    Count=1    def __init__(self):        self.aaa='a'    def Execute(self,Environ,CGI,CGITB,Form,Cookies,SessionId,*Args):        ViewBag=gl.Obj()        Test.Count+=1        theM = Person()        HtmlTools.TryUpdate(theM,Form)        ViewBag.first_name=theM.first_name        ViewBag.last_name=theM.last_name        ViewBag.SessionId=SessionId        theViewText = OsHelper.ReadFile('testview.py')        exec(theViewText)

大家注意,View视图,我这里并没有进行特殊处理,仅仅是直接执行里面的代码,实际上可以很容易的实现类似Asp的那种标记性视图。要注意的,ViewBag这个对象是传递给视图层的模型数据。因为采用的是直接执行视图代码的方法,整个视图执行的变量作用域都在Execute方法里,ViewBag是可以直接访问的。下面是视图类:

print('<html>')print('<head>')print('<title>Hello Word - First CGI Program</title>')print('</head>')print('<body>')print('<h2>Hello Word! This is my first CGI program</h2>')print("<h2>HelloX %s %s</h2>" % (ViewBag.first_name, ViewBag.last_name))print('''<form method="post" action="Dspth.py?ctr=test&act=execute" >          <a>姓</a><input type="text" name="first_name" value="" /><br />          <a>名</a><input type="text" name="last_name" value="" />          <input type="submit" value="提交X"/>          </form>        ''')print("<b>SessionId:"+ViewBag.SessionId+"</b>")print('</body>')print('</html>')


到这里,MVC框架就算完成了,简单吧,其实哪些鼎鼎大名的框架也基本是这种模式,只是细节处理方面要好很多。特别是路由的处置方面。但哪些只是技术细节,架构上,并无本质上的区别。我上面的框架如果把视图和路由再处理一下,做一般的大小系统,足够了。

因为初涉python,不妥地方难免,还望赐教。大家也可以交流。

2 0