[SQL-CVS] SQLObject/SQLObject Style.py,NONE,1.1
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: <ian...@us...> - 2003-04-21 22:50:41
|
Update of /cvsroot/sqlobject/SQLObject/SQLObject In directory sc8-pr-cvs1:/tmp/cvs-serv5788/SQLObject Added Files: Style.py Log Message: New Style module --- NEW FILE: Style.py --- import re __all__ = ["Style", "MixedCaseUnderscoreStyle", "DefaultStyle"] class Style(object): """ The base Style class, and also the simplest implementation. No translation occurs -- column names and attribute names match, as do class names and table names (when using auto class or schema generation). """ def __init__(self, pythonAttrToDBColumn=None, dbColumnToPythonAttr=None, pythonClassToDBTable=None, dbTableToPythonClass=None, idForTable=None): if pythonAttrToDBColumn: self.pythonAttrToDBColumn = lambda a, s=self: pythonAttrToDBColumn(s, a) if dbColumnToPythonAttr: self.dbColumnToPythonAttr = lambda a, s=self: dbColumnToPythonAttr(s, a) if pythonClassToDBTable: self.pythonClassToDBTable = lambda a, s=self: pythonClassToDBTable(s, a) if dbTableToPythonClass: self.dbTableToPythonClass = lambda a, s=self: dbTableToPythonClass(s, a) if idForTable: self.idForTable = lambda a, s=self: idForTable(s, a) def pythonAttrToDBColumn(self, attr): return attr def dbColumnToPythonAttr(self, col): return col def pythonClassToDBTable(self, className): return className def dbTableToPythonClass(self, table): return table def idForTable(self, table): return 'id' class MixedCaseUnderscoreStyle(Style): """ This is the default style. Python attributes use mixedCase, while database columns use underscore_separated. """ def pythonAttrToDBColumn(self, attr): return mixedToUnder(attr) def dbColumnToPythonAttr(self, col): return underToMixed(col) def pythonClassToDBTable(self, className): return className[0].lower() \ + mixedToUnder(className[1:]) def dbTableToPythonClass(self, table): return table[0].upper() \ + underToMixed(table[1:]) def pythonClassToDBTableReference(self, className): return self.tableReference(self.pythonClassToDBTable(className)) def tableReference(self, table): return table + "_id" DefaultStyle = MixedCaseUnderscoreStyle def longID(self, table): """ Use SomeStyle(idForTable=longID) to use this. This will make table IDs use the full table, like person_id. """ return defaultStyle = DefaultStyle() def getStyle(soClass, dbConnection=None): if dbConnection is None: if hasattr(soClass, '_connection'): dbConnection = soClass._connection if hasattr(soClass, '_style') and soClass._style: return soClass._style elif dbConnection and dbConnection.style: return dbConnection.style else: return defaultStyle ############################################################ ## Text utilities ############################################################ _mixedToUnderRE = re.compile(r'[A-Z]+') def mixedToUnder(s): if s.endswith('ID'): return mixedToUnder(s[:-2] + "_id") trans = _mixedToUnderRE.sub(lambda x: '_%s' % x.group(0).lower(), s) if trans.startswith('_'): trans = trans[1:] return trans _underToMixedRE = re.compile('_.') def underToMixed(name): if name.endswith('_id'): return underToMixed(name[:-3] + "ID") return _underToMixedRE.sub(lambda m: m.group(0)[1].upper(), name) |