Handle conversion from format directives to regexes.
| 219 | |
| 220 | import UserDict |
| 221 | class TimeRE(UserDict.UserDict): |
| 222 | """Handle conversion from format directives to regexes.""" |
| 223 | |
| 224 | def __init__(self, locale_time=None): |
| 225 | """Create keys/values. |
| 226 | |
| 227 | Order of execution is important for dependency reasons. |
| 228 | |
| 229 | """ |
| 230 | if locale_time: |
| 231 | self.locale_time = locale_time |
| 232 | else: |
| 233 | self.locale_time = LocaleTime() |
| 234 | base = UserDict.UserDict |
| 235 | base.__init__(self, { |
| 236 | # The " \d" part of the regex is to make %c from ANSI C work |
| 237 | 'd': r"(?P<d>3[0-1]|[1-2]\d|0[1-9]|[1-9]| [1-9])", |
| 238 | 'H': r"(?P<H>2[0-3]|[0-1]\d|\d)", |
| 239 | 'I': r"(?P<I>1[0-2]|0[1-9]|[1-9])", |
| 240 | 'j': r"(?P<j>36[0-6]|3[0-5]\d|[1-2]\d\d|0[1-9]\d|00[1-9]|[1-9]\d|0[1-9]|[1-9])", |
| 241 | 'm': r"(?P<m>1[0-2]|0[1-9]|[1-9])", |
| 242 | 'M': r"(?P<M>[0-5]\d|\d)", |
| 243 | 'S': r"(?P<S>6[0-1]|[0-5]\d|\d)", |
| 244 | 'U': r"(?P<U>5[0-3]|[0-4]\d|\d)", |
| 245 | 'w': r"(?P<w>[0-6])", |
| 246 | # W is set below by using 'U' |
| 247 | 'y': r"(?P<y>\d\d)", |
| 248 | #XXX: Does 'Y' need to worry about having less or more than |
| 249 | # 4 digits? |
| 250 | 'Y': r"(?P<Y>\d\d\d\d)", |
| 251 | 'A': self.__seqToRE(self.locale_time.f_weekday, 'A'), |
| 252 | 'a': self.__seqToRE(self.locale_time.a_weekday, 'a'), |
| 253 | 'B': self.__seqToRE(self.locale_time.f_month[1:], 'B'), |
| 254 | 'b': self.__seqToRE(self.locale_time.a_month[1:], 'b'), |
| 255 | 'p': self.__seqToRE(self.locale_time.am_pm, 'p'), |
| 256 | '%': '%'}) |
| 257 | temp_list = [] |
| 258 | for tz_names in self.locale_time.timezone: |
| 259 | for tz in tz_names.keys(): |
| 260 | temp_list.append(tz) |
| 261 | base.__setitem__(self, 'Z', self.__seqToRE(temp_list, 'Z')) |
| 262 | base.__setitem__(self, 'W', base.__getitem__(self, 'U')) |
| 263 | base.__setitem__(self, 'c', self.pattern(self.locale_time.LC_date_time)) |
| 264 | base.__setitem__(self, 'x', self.pattern(self.locale_time.LC_date)) |
| 265 | base.__setitem__(self, 'X', self.pattern(self.locale_time.LC_time)) |
| 266 | |
| 267 | def __seqToRE(self, to_convert, directive): |
| 268 | """Convert a list to a regex string for matching a directive. |
| 269 | |
| 270 | Want possible matching values to be from longest to shortest. This |
| 271 | prevents the possibility of a match occuring for a value that also |
| 272 | a substring of a larger value that should have matched (e.g., 'abc' |
| 273 | matching when 'abcdef' should have been the match). |
| 274 | |
| 275 | """ |
| 276 | for value in to_convert: |
| 277 | if value != '': |
| 278 | break |
no outgoing calls
no test coverage detected