Package orm2 :: Package util :: Module datatypes
[hide private]
[frames] | no frames]

Source Code for Module orm2.util.datatypes

  1  #!/usr/bin/env python 
  2  # -*- coding: iso-8859-1 -*- 
  3   
  4  ##  This file is part of orm, The Object Relational Membrane Version 2. 
  5  ## 
  6  ##  Copyright 2002-2006 by Diedrich Vorberg <diedrich@tux4web.de> 
  7  ## 
  8  ##  All Rights Reserved 
  9  ## 
 10  ##  For more Information on orm see the README file. 
 11  ## 
 12  ##  This program is free software; you can redistribute it and/or modify 
 13  ##  it under the terms of the GNU General Public License as published by 
 14  ##  the Free Software Foundation; either version 2 of the License, or 
 15  ##  (at your option) any later version. 
 16  ## 
 17  ##  This program is distributed in the hope that it will be useful, 
 18  ##  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 19  ##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 20  ##  GNU General Public License for more details. 
 21  ## 
 22  ##  You should have received a copy of the GNU General Public License 
 23  ##  along with this program; if not, write to the Free Software 
 24  ##  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 25  ## 
 26  ##  I have added a copy of the GPL in the file gpl.txt. 
 27   
 28  # 
 29  # $Log: datatypes.py,v $ 
 30  # Revision 1.5  2007/05/14 19:48:06  diedrich 
 31  # Convert PEMail values to unicode instead of complaining. 
 32  # 
 33  # Revision 1.4  2007/01/22 19:46:14  diedrich 
 34  # Fixed __sql__() methods to use runner paradigm instead of the old ds. 
 35  # 
 36  # Revision 1.3  2006/07/04 22:51:49  diedrich 
 37  # Fixed validator handling. 
 38  # 
 39  # Revision 1.2  2006/05/13 14:56:36  diedrich 
 40  # Added pickle 
 41  # 
 42  # Revision 1.1  2006/04/28 08:45:50  diedrich 
 43  # Initial commit. 
 44  # 
 45  # 
 46   
 47  """ 
 48  This module defines a number of datatypes (dbattributes) for 
 49  particular purposes. 
 50  """ 
 51   
 52  from cPickle import loads, dumps, HIGHEST_PROTOCOL 
 53  from string import * 
 54   
 55  from orm2.datatypes import datatype, Unicode 
 56  from orm2.validators import * 
 57  from orm2 import sql 
 58   
59 -class idna_literal(sql.unicode_literal):
60 """ 61 SQL literal class for <b>valid</b> Unicode (idna) domain names and 62 email addresses. 63 """
64 - def __sql__(self, runner):
65 if "@" in self.content: # e-Mail address 66 local, remote = split(self.content, "@") 67 local = local.encode("ascii") 68 remote = remote.encode("idna") 69 70 s = "%s@%s" % ( local, remote, ) 71 s = runner.ds.escape_string(s) 72 sql = runner.ds.string_quotes(s) 73 else: 74 s = self.content.encode("idna") 75 s = runner.ds.escape_string(s) 76 sql = runner.ds.string_quotes(s) 77 78 return sql
79 80 81
82 -class PDomain(Unicode):
83 """ 84 Just like orm2.datatypes.Unicode, except that it doesn't use the 85 backend's charset to convert the Unicode string, but Python's idna 86 (Internationalized Domain Names in Applications) codec which takes 87 care of lowercasing and punicode representation and so on. 88 """ 89 sql_literal_class = idna_literal 90
91 - def __init__(self, column=None, title=None, validators=(), 92 widget_specs=(), has_default=False):
93 94 if isinstance(validators, validator): validators = [ validators, ] 95 validators = list(validators) 96 validators.append(idna_fqdn_validator()) 97 98 Unicode.__init__(self, column, title, validators, 99 widget_specs, has_default)
100
101 - def __convert__(self, value):
102 if type(value) is not UnicodeType: 103 raise TypeError( 104 "You must set a PDomain property to a unicode value!") 105 else: 106 return value
107 108
109 -class PEMail(Unicode):
110 """ 111 Like PDomain above, but for e-Mail addresses. The local part will be 112 checked against a regular expression, the remote part will be treated 113 like a domain name by the PDomain class above. 114 """ 115 sql_literal_class = idna_literal 116
117 - def __init__(self, column=None, title=None, validators=(), 118 has_default=False):
119 120 if isinstance(validators, validator): validators = [ validators, ] 121 validators = list(validators) 122 validators.append(idna_email_validator()) 123 124 Unicode.__init__(self, column, title, validators, 125 has_default)
126 127
128 - def __convert__(self, value):
129 if type(value) is not UnicodeType: 130 return unicode(value) 131 #raise TypeError( 132 # "You must set a PEMail property to a unicode value!") 133 else: 134 return value
135 136
137 -class pickle(datatype):
138 """ 139 This datatype uses Python's pickle module to serialize (nearly) 140 arbitrary Python objects into a string representation that is then 141 stored in a regular database column. See U{http://localhost/Documentation/Python/Main/lib/module-pickle.html} for details on pickling. 142 """ 143
144 - def __init__(self, pickle_protocol=HIGHEST_PROTOCOL, 145 column=None, title=None, 146 validators=(), has_default=False):
147 """ 148 @param pickle_protocol: Version number of the protocol being used by 149 the pickle functions. See U{http://localhost/Documentation/Python/Main/lib/module-pickle.html} for details. 150 """ 151 self.pickle_protocol = pickle_protocol 152 datatype.__init__(self, column, title, validators, 153 has_default)
154 155
156 - def __set_from_result__(self, ds, dbobj, value):
157 """ 158 This method takes care of un-pickling the value stored in the datbase. 159 """ 160 value = loads(value) 161 setattr(dbobj, self.data_attribute_name(), value)
162
163 - def __convert__(self, value):
164 """ 165 Since we store the Python object 'as is', convert does nothing. 166 """ 167 return value
168
169 - def sql_literal(self, dbobj):
170 """ 171 This function takes care of converting the Python object into a 172 serialized string representation. 173 """ 174 if not self.isset(dbobj): 175 msg = "This attribute has not been retrieved from the database." 176 raise AttributeError(msg) 177 else: 178 value = getattr(dbobj, self.data_attribute_name()) 179 180 if value is None: 181 return sql.NULL 182 else: 183 pickled = dumps(value, self.pickle_protocol) 184 return sql.string_literal(pickled)
185 186 187 # Local variables: 188 # mode: python 189 # ispell-local-dictionary: "english" 190 # End: 191