Permalink
Browse files

Use u() utility in generated metadata

  • Loading branch information...
1 parent 51c72a7 commit e019a990aacde54228a7f322e1959ae09f1a20c3 @daviddrysdale committed Nov 27, 2011
@@ -62,8 +62,9 @@
# "main_country_for_code" in the metadata should be first.'''
# Boilerplate header for individual region data files
-_REGION_METADATA_PROLOG = '''"""Auto-generated file, do not edit by hand. %s metadata"""
-from %s.phonemetadata import NumberFormat, PhoneNumberDesc, PhoneMetadata
+_REGION_METADATA_PROLOG = '''"""Auto-generated file, do not edit by hand. %(region)s metadata"""
+from %(module)s.util import u
+from %(module)s.phonemetadata import NumberFormat, PhoneNumberDesc, PhoneMetadata
'''
# Copyright notice covering the XML metadata; include current year.
@@ -376,7 +377,7 @@ def emit_metadata_for_region_py(self, region, region_filename, module_prefix):
"""Emit Python code generating the metadata for the given region"""
terrobj = self.territory[region]
with open(region_filename, "w") as outfile:
- prnt(_REGION_METADATA_PROLOG % (terrobj.o.id, module_prefix), file=outfile)
+ prnt(_REGION_METADATA_PROLOG % {'region': terrobj.o.id, 'module': module_prefix}, file=outfile)
prnt("PHONE_METADATA_%s = %s" % (terrobj.o.id, terrobj), file=outfile)
def emit_metadata_py(self, datadir, module_prefix):
@@ -16,7 +16,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-from .util import UnicodeMixin, u
+from .util import UnicodeMixin, u, rpr
class NumberFormat(UnicodeMixin):
@@ -123,17 +123,17 @@ def __repr__(self):
def __unicode__(self):
# Generate a string that is valid Python input for the constructor.
- # Note that we use %r, which generates its own quotes.
- result = u("NumberFormat(pattern=%r, format=%r") % (self.pattern, self.format)
+ # Note that we use rpr (variant of repr), which generates its own quotes.
+ result = u("NumberFormat(pattern=%s, format=%s") % (rpr(self.pattern), rpr(self.format))
if len(self.leading_digits_pattern) > 0:
result += (u(", leading_digits_pattern=[%s]") %
- ", ".join(["%r" % ld for ld in self.leading_digits_pattern]))
+ ", ".join([rpr(ld) for ld in self.leading_digits_pattern]))
if self.national_prefix_formatting_rule is not None:
- result += u(", national_prefix_formatting_rule=%r") % self.national_prefix_formatting_rule
+ result += u(", national_prefix_formatting_rule=%s") % rpr(self.national_prefix_formatting_rule)
if self.national_prefix_optional_when_formatting:
- result += u(", national_prefix_optional_when_formatting=%r") % self.national_prefix_optional_when_formatting
+ result += u(", national_prefix_optional_when_formatting=%s") % rpr(self.national_prefix_optional_when_formatting)
if self.domestic_carrier_code_formatting_rule is not None:
- result += u(", domestic_carrier_code_formatting_rule=%r") % self.domestic_carrier_code_formatting_rule
+ result += u(", domestic_carrier_code_formatting_rule=%s") % rpr(self.domestic_carrier_code_formatting_rule)
result += u(")")
return result
@@ -188,13 +188,13 @@ def __unicode__(self):
result = u("PhoneNumberDesc(")
sep = u("")
if self.national_number_pattern is not None:
- result += u("%snational_number_pattern=%r") % (sep, self.national_number_pattern)
+ result += u("%snational_number_pattern=%s") % (sep, rpr(self.national_number_pattern))
sep = u(", ")
if self.possible_number_pattern is not None:
- result += u("%spossible_number_pattern=%r") % (sep, self.possible_number_pattern)
+ result += u("%spossible_number_pattern=%s") % (sep, rpr(self.possible_number_pattern))
sep = u(", ")
if self.example_number is not None:
- result += u("%sexample_number=%r") % (sep, self.example_number)
+ result += u("%sexample_number=%s") % (sep, rpr(self.example_number))
sep = u(", ")
result += u(")")
return result
@@ -414,8 +414,8 @@ def __unicode__(self):
country_code = self.country_code
if country_code is None:
country_code = -1
- result = (u("PhoneMetadata(id='%s', country_code=%d, international_prefix=%r") %
- (self.id, country_code, self.international_prefix))
+ result = (u("PhoneMetadata(id='%s', country_code=%d, international_prefix=%s") %
+ (self.id, country_code, rpr(self.international_prefix)))
result += ",\n general_desc=%s" % self.general_desc
result += ",\n fixed_line=%s" % self.fixed_line
result += ",\n mobile=%s" % self.mobile
@@ -430,16 +430,16 @@ def __unicode__(self):
result += ",\n no_international_dialling=%s" % self.no_international_dialling
if self.preferred_international_prefix is not None:
- result += ",\n preferred_international_prefix=%r" % self.preferred_international_prefix
+ result += ",\n preferred_international_prefix=%s" % rpr(self.preferred_international_prefix)
if self.national_prefix is not None:
- result += ",\n national_prefix=%r" % self.national_prefix
+ result += ",\n national_prefix=%s" % rpr(self.national_prefix)
if self.preferred_extn_prefix is not None:
- result += ",\n preferred_extn_prefix=%r" % self.preferred_extn_prefix
+ result += ",\n preferred_extn_prefix=%s" % rpr(self.preferred_extn_prefix)
if self.national_prefix_for_parsing is not None:
- result += ",\n national_prefix_for_parsing=%r" % self.national_prefix_for_parsing
+ result += ",\n national_prefix_for_parsing=%s" % rpr(self.national_prefix_for_parsing)
if self.national_prefix_transform_rule is not None:
- # Note that we use %r on self.national_prefix_transform_rule, which generates its own quotes
- result += ",\n national_prefix_transform_rule=%r" % self.national_prefix_transform_rule
+ # Note that we use rpr() on self.national_prefix_transform_rule, which generates its own quotes
+ result += ",\n national_prefix_transform_rule=%s" % rpr(self.national_prefix_transform_rule)
if len(self.number_format) > 0:
result += ",\n number_format=[%s]" % ',\n '.join(map(u, self.number_format))
if len(self.intl_number_format) > 0:
@@ -16,7 +16,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-from .util import UnicodeMixin
+from .util import UnicodeMixin, rpr
class CountryCodeSource(object):
@@ -180,14 +180,14 @@ def __ne__(self, other):
return not self.__eq__(other)
def __repr__(self):
- return (("PhoneNumber(country_code=%r, national_number=%r, extension=%r, " +
- "italian_leading_zero=%r, country_code_source=%r, preferred_domestic_carrier_code=%r)") %
- (self.country_code,
- self.national_number,
- self.extension,
- self.italian_leading_zero,
- self.country_code_source,
- self.preferred_domestic_carrier_code))
+ return (("PhoneNumber(country_code=%s, national_number=%s, extension=%s, " +
+ "italian_leading_zero=%s, country_code_source=%s, preferred_domestic_carrier_code=%s)") %
+ (rpr(self.country_code),
+ rpr(self.national_number),
+ rpr(self.extension),
+ rpr(self.italian_leading_zero),
+ rpr(self.country_code_source),
+ rpr(self.preferred_domestic_carrier_code)))
def __unicode__(self):
result = ("Country Code: %s National Number: %s" %
@@ -27,6 +27,9 @@
u = str
uchr = chr
to_long = int
+ # TODO create a Py3k repr-equivalent that produces something
+ # parsable in Python 2 (with the assistance of this module)
+ rpr = repr
def prnt(*args, **kwargs):
sep = kwargs.get('sep', ' ')
@@ -56,6 +59,11 @@ def u(s):
uchr = unichr
to_long = long
+ _U_RE = re.compile("^u('[^']*')")
+ def rpr(obj):
+ s = repr(obj)
+ return re.sub(_U_RE, r'u(\1)', s)
+
def prnt(*args, **kwargs):
sep = kwargs.get('sep', ' ')
end = kwargs.get('end', '\n')

0 comments on commit e019a99

Please sign in to comment.