Parsed NumberFormat objects from XML element
| 231 | |
| 232 | |
| 233 | class XNumberFormat(UnicodeMixin): |
| 234 | """Parsed NumberFormat objects from XML element""" |
| 235 | def __init__(self, owning_xterr, xtag, national_prefix, |
| 236 | national_prefix_formatting_rule, |
| 237 | national_prefix_optional_when_formatting, |
| 238 | carrier_code_formatting_rule): |
| 239 | if xtag is None: |
| 240 | self.o = None |
| 241 | self.io = None |
| 242 | else: |
| 243 | self.o = NumberFormat() |
| 244 | self.o._mutable = True |
| 245 | # Find the REQUIRED attribute |
| 246 | self.o.pattern = xtag.attrib['pattern'] |
| 247 | # Find the IMPLIED attribute(s) |
| 248 | self.o.domestic_carrier_code_formatting_rule = xtag.get('carrierCodeFormattingRule', None) |
| 249 | self.o.national_prefix_formatting_rule = xtag.get('nationalPrefixFormattingRule', None) |
| 250 | self.o.national_prefix_optional_when_formatting = get_optional_true_attrib(xtag, 'nationalPrefixOptionalWhenFormatting') |
| 251 | |
| 252 | # Post-process formatting rules for expansions and defaults |
| 253 | if self.o.national_prefix_formatting_rule is not None: |
| 254 | # expand abbreviations |
| 255 | self.o.national_prefix_formatting_rule = _expand_formatting_rule(self.o.national_prefix_formatting_rule, |
| 256 | national_prefix) |
| 257 | else: |
| 258 | # set to territory-wide formatting rule |
| 259 | self.o.national_prefix_formatting_rule = national_prefix_formatting_rule |
| 260 | if self.o.national_prefix_formatting_rule is not None: |
| 261 | # Replace '$1' etc with '\1' to match Python regexp group reference format |
| 262 | self.o.national_prefix_formatting_rule = re.sub(r'\$', r'\\', self.o.national_prefix_formatting_rule) |
| 263 | |
| 264 | if not self.o.national_prefix_optional_when_formatting and national_prefix_optional_when_formatting: |
| 265 | # If attrib is None, it was missing and inherits territory-wide value |
| 266 | self.o.national_prefix_optional_when_formatting = national_prefix_optional_when_formatting |
| 267 | |
| 268 | if self.o.domestic_carrier_code_formatting_rule is not None: |
| 269 | # expand abbreviations |
| 270 | self.o.domestic_carrier_code_formatting_rule = _expand_formatting_rule(self.o.domestic_carrier_code_formatting_rule, |
| 271 | national_prefix) |
| 272 | else: |
| 273 | # set to territory-wide formatting rule |
| 274 | self.o.domestic_carrier_code_formatting_rule = carrier_code_formatting_rule |
| 275 | if self.o.domestic_carrier_code_formatting_rule is not None: |
| 276 | # Replace '$1' etc with '\1' to match Python regexp group reference format |
| 277 | self.o.domestic_carrier_code_formatting_rule = re.sub(r'\$(\d)', r'\\\1', self.o.domestic_carrier_code_formatting_rule) |
| 278 | |
| 279 | self.o.format = _get_unique_child_value(xtag, 'format') |
| 280 | if self.o.format is None: |
| 281 | raise Exception("No format pattern found") |
| 282 | else: |
| 283 | # Replace '$1' etc with '\1' to match Python regexp group reference format |
| 284 | self.o.format = re.sub(r'\$', u(r'\\'), self.o.format) |
| 285 | xleading_digits = xtag.findall("leadingDigits") |
| 286 | for xleading_digit in xleading_digits: |
| 287 | self.o.leading_digits_pattern.append(_dews_re(xleading_digit.text)) |
| 288 | |
| 289 | # Add this NumberFormat object into the owning metadata |
| 290 | owning_xterr.o.number_format.append(self.o) |