Class representing metadata for international telephone numbers for a region. This class is hand created based on phonemetadata.proto. Please refer to that file for detailed descriptions of the meaning of each field. WARNING: This API isn't stable. It is considered libphonenumber-inter
| 238 | |
| 239 | |
| 240 | class PhoneMetadata(UnicodeMixin, ImmutableMixin): |
| 241 | """Class representing metadata for international telephone numbers for a region. |
| 242 | |
| 243 | This class is hand created based on phonemetadata.proto. Please refer to that file |
| 244 | for detailed descriptions of the meaning of each field. |
| 245 | |
| 246 | WARNING: This API isn't stable. It is considered libphonenumber-internal |
| 247 | and can change at any time. We only declare it as public for easy |
| 248 | inclusion in our build tools not in this package. Clients should not |
| 249 | refer to this file, we do not commit to support backwards-compatibility or |
| 250 | to warn about breaking changes. |
| 251 | |
| 252 | """ |
| 253 | # Lock that protects the *_available fields while they are being modified. |
| 254 | # The modification involves loading data from a file, so we cannot just |
| 255 | # rely on the GIL. |
| 256 | _metadata_lock = threading.Lock() |
| 257 | # If a region code is a key in this dict, metadata for that region is available. |
| 258 | # The corresponding value of the map is either: |
| 259 | # - a function which loads the region's metadata |
| 260 | # - None, to indicate that the metadata is already loaded |
| 261 | _region_available = {} # ISO 3166-1 alpha 2 => function or None |
| 262 | # Likewise for short number metadata. |
| 263 | _short_region_available = {} # ISO 3166-1 alpha 2 => function or None |
| 264 | # Likewise for non-geo country calling codes. |
| 265 | _country_code_available = {} # country calling code (as int) => function or None |
| 266 | |
| 267 | _region_metadata = {} # ISO 3166-1 alpha 2 => PhoneMetadata |
| 268 | _short_region_metadata = {} # ISO 3166-1 alpha 2 => PhoneMetadata |
| 269 | # A mapping from a country calling code for a non-geographical entity to |
| 270 | # the PhoneMetadata for that country calling code. Examples of the country |
| 271 | # calling codes include 800 (International Toll Free Service) and 808 |
| 272 | # (International Shared Cost Service). |
| 273 | _country_code_metadata = {} # country calling code (as int) => PhoneMetadata |
| 274 | |
| 275 | @classmethod |
| 276 | def metadata_for_region(kls, region_code, default=None): |
| 277 | loader = kls._region_available.get(region_code, None) |
| 278 | if loader is not None: |
| 279 | # Region metadata is available but has not yet been loaded. Do so now. |
| 280 | kls._metadata_lock.acquire() |
| 281 | loader(region_code) |
| 282 | kls._region_available[region_code] = None |
| 283 | kls._metadata_lock.release() |
| 284 | return kls._region_metadata.get(region_code, default) |
| 285 | |
| 286 | @classmethod |
| 287 | def short_metadata_for_region(kls, region_code, default=None): |
| 288 | loader = kls._short_region_available.get(region_code, None) |
| 289 | if loader is not None: |
| 290 | # Region short number metadata is available but has not yet been loaded. Do so now. |
| 291 | kls._metadata_lock.acquire() |
| 292 | loader(region_code) |
| 293 | kls._short_region_available[region_code] = None |
| 294 | kls._metadata_lock.release() |
| 295 | return kls._short_region_metadata.get(region_code, default) |
| 296 | |
| 297 | @classmethod |
no outgoing calls