MVC中的M已经介绍完了,C和V都是用户代码,先看几个工具类:
A)全局变量类,用户定义全局变量和一个用于产生普通对象的类型:
B)OsHelper.用于动态加载模块,获取类型
A)全局变量类,用户定义全局变量和一个用于产生普通对象的类型:
gl_A=1
gl_B='a'
class Obj:
def __init__(self):
self.Obj=self
如果需要,大家可以定义自己的全局性变量。
B)OsHelper.用于动态加载模块,获取类型
import imp
import inspect
import uuid
#加载控制模块和类,读取文件,获取GUID
class OsHelper:
@staticmethod
def LoadModule(controllerName):
return imp.load_source(controllerName,controllerName+'.py')
@staticmethod
def LoadClass(module,classname,ignorecase=True):
if ignorecase :
theMembers = inspect.getmembers(module)
for theM in theMembers:
if theM[0].upper()==classname.upper():
return theM[1]
else:
return getattr(module,classname)
@staticmethod
def GetGuid():
return uuid.uuid1().__str__().replace('-','')
@staticmethod
def ReadFile(filepath,AEncoding='utf-8'):
if AEncoding==None:
AEncoding='utf-8'
theFile = open(file=filepath,encoding=AEncoding)
try:
return theFile.read()
finally:
theFile.close()
C)HtmlHelper模块,主要提供html处理的类。这里提供了一个自动根据表单域给实体赋值的方法和Cookie处理的类。大家可以根据自己的需要增加自己的方法。
from os import environ
import cgi, cgitb
from ListAttr import ObjOpt
class HtmlTools:
#自动根据表单域给对象赋值
@staticmethod
def TryUpdate(obj,form):
theProperties = ObjOpt.GetPropertyNames(obj)
theObjName = ObjOpt.GetClassName(type(obj))
theRet={}
if theProperties != None:
for theP in theProperties:
try:
theVal =form.getvalue(theP)
if theVal!=None:
setattr(obj,theP,theVal)
else:
theVal =form.getvalue(theObjName+'.'+theP)
if theVal!=None:
setattr(obj,theP,theVal)
if theVal!=None:
theRet[theP] = theVal
except Exception:
return theRet
return theRet
#获取Cookie信息,字典方式返回.
@staticmethod
def GetCookies(Environ):
theRet={}
theCookieStr=Environ.get('HTTP_COOKIE')
if theCookieStr!=None:
for cookie in theCookieStr.split(";"):
(key, value ) = cookie.split('=')
theRet[key]=value
return theRet