Home | Trees | Indices | Help |
|
---|
|
1 # ============================================================================ 2 # 3 # Copyright (C) 2007-2008 Conceptive Engineering bvba. All rights reserved. 4 # www.conceptive.be / project-camelot@conceptive.be 5 # 6 # This file is part of the Camelot Library. 7 # 8 # This file may be used under the terms of the GNU General Public 9 # License version 2.0 as published by the Free Software Foundation 10 # and appearing in the file LICENSE.GPL included in the packaging of 11 # this file. Please review the following information to ensure GNU 12 # General Public Licensing requirements will be met: 13 # http://www.trolltech.com/products/qt/opensource.html 14 # 15 # If you are unsure which license is appropriate for your use, please 16 # review the following information: 17 # http://www.trolltech.com/products/qt/licensing.html or contact 18 # project-camelot@conceptive.be. 19 # 20 # This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 21 # WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 22 # 23 # For use of this library in commercial applications, please contact 24 # project-camelot@conceptive.be 25 # 26 # ============================================================================ 27 28 """Helper functions for the view subpackage""" 29 30 from PyQt4 import QtCore 31 32 from datetime import datetime, time, date 33 import re 34 import logging 35 import operator 36 37 from camelot.core import constants 38 from camelot.core.sql import like_op 39 from sqlalchemy.sql.operators import between_op 40 from camelot.core.utils import ugettext 41 42 logger = logging.getLogger('camelot.view.utils') 43 44 _local_date_format = None 4547 """Get the local data format and cache it for reuse""" 48 global _local_date_format 49 if not _local_date_format: 50 locale = QtCore.QLocale() 51 format_sequence = re.split('y*', unicode(locale.dateFormat(locale.ShortFormat))) 52 # make sure a year always has 4 numbers 53 format_sequence.insert(-1, 'yyyy') 54 _local_date_format = unicode(u''.join(format_sequence)) 55 return _local_date_format56 58 6365 if s is None: raise ParsingError() 66 if s.lower() not in ['false', 'true']: raise ParsingError() 67 return eval(s.lower().capitalize())6870 if not s: 71 return None 72 from PyQt4.QtCore import QDate 73 import string 74 s = s.strip() 75 f = local_date_format() 76 if not s: 77 return None 78 dt = QDate.fromString(s, f) 79 if not dt.isValid(): 80 # try parsing without separators 81 # attention : using non ascii letters will fail on windows 82 # string.letters then contains non ascii letters of which we don't know the 83 # encoding, so we cannot convert them to unicode to compare them 84 only_letters_format = u''.join([c for c in f if c in string.ascii_letters]) 85 only_letters_string = u''.join([c for c in s if c in (string.ascii_letters+string.digits)]) 86 dt = QDate.fromString(only_letters_string, only_letters_format) 87 if not dt.isValid(): 88 # try parsing without the year, and take the current year by default 89 only_letters_format = u''.join([c for c in only_letters_format if c not in ['y']]) 90 dt = QDate.fromString(only_letters_string, only_letters_format) 91 if not dt.isValid(): 92 raise ParsingError() 93 # # try parsing without year and month, and take the current year and month by default 94 # only_letters_format = u''.join([c for c in only_letters_format if c not in ['M']]) 95 # dt = QDate.fromString(only_letters_string, only_letters_format) 96 # if not dt.isValid(): 97 # raise ParsingError() 98 # else: 99 # today = date.today() 100 # return date(today.year, today.month, dt.day()) 101 else: 102 return date(date.today().year, dt.month(), dt.day()) 103 return date(dt.year(), dt.month(), dt.day())104106 if s is None: raise ParsingError() 107 s = s.strip() 108 try: 109 dt = datetime.strptime(s, format) 110 except ValueError: 111 raise ParsingError() 112 return dt.time()113 114116 if s is None: raise ParsingError() 117 s = s.strip() 118 119 try: 120 dt = datetime.strptime(s, format) 121 except ValueError: 122 raise ParsingError() 123 return dt124 125127 if s is None: raise ParsingError() 128 if s.isspace(): return int() 129 130 s = s.strip() 131 if len(s) == 0: return int() 132 133 try: 134 i = int(s) 135 except ValueError: 136 raise ParsingError() 137 return i138 139141 if not s: 142 return None 143 locale = QtCore.QLocale() 144 f, ok = locale.toFloat(s) 145 if not ok: 146 raise ParsingError() 147 return f148 149151 if pytype is str: 152 return str(s) 153 elif pytype is unicode: 154 return unicode(s) 155 elif pytype is bool: 156 return bool_from_string(s) 157 elif pytype is date: 158 return date_from_string(s) 159 elif pytype is time: 160 return date_from_string(s) 161 elif pytype is datetime: 162 return datetime_from_string(s) 163 elif pytype is float: 164 return float_from_string(s) 165 elif pytype is int: 166 return int_from_string(s)167 170 171 operator_names = { 172 operator.eq : u'=', 173 operator.ne : u'!=', 174 operator.lt : u'<', 175 operator.le : u'<=', 176 operator.gt : u'>', 177 operator.ge : u'>=', 178 like_op : u'like', 179 between_op: u'between', 180 } 181
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Sat Jun 12 15:42:12 2010 | http://epydoc.sourceforge.net |