Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def strip_lang_subdomains_from_netloc(netloc):
if netloc.count('.') > 1:
subdomain, remaining_netloc = netloc.split('.', 1)
if len(subdomain) == 5 and '-' in subdomain:
lang, country = subdomain.split('-', 1)
if len(lang) == 2 and len(country) == 2:
if pycountry.countries.get(alpha_2=lang.upper()) and pycountry.countries.get(alpha_2=country.upper()):
netloc = remaining_netloc
elif len(subdomain) == 2:
if pycountry.countries.get(alpha_2=subdomain.upper()):
netloc = remaining_netloc
return netloc
self.stdout.write("Countries already populated; nothing to be done.")
sys.exit(0)
else:
raise CommandError(
"You already have countries in your database. This command "
"currently does not support updating existing countries.")
countries = [
Country(
iso_3166_1_a2=country.alpha2,
iso_3166_1_a3=country.alpha3,
iso_3166_1_numeric=country.numeric,
printable_name=country.name,
name=getattr(country, 'official_name', ''),
is_shipping_country=options['is_shipping'])
for country in pycountry.countries]
Country.objects.bulk_create(countries)
self.stdout.write("Successfully added %s countries." % len(countries))
def iso3166alpha3_to_iso3166alpha2(iso3):
for country in pycountry.countries:
if country.alpha_3 == iso3:
return country.alpha_2
return iso3
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import pycountry
import colander
# COUNTRY_CODES stores the alpha-2 code reprsentation of countries
# in accordance to ISO 3166-1
# see https://en.wikipedia.org/wiki/ISO_3166-1
COUNTRY_CODES = [c.alpha_2 for c in pycountry.countries]
def is_valid_country_code(_, value):
if value not in COUNTRY_CODES:
raise colander.Invalid(
'invalid ISO 3166-1 country code: [{}].' +
' Please see https://en.wikipedia.org/wiki/ISO_3166-1'.format(value)
)
def is_valid_country_code(code):
if code == NO_COUNTRY:
return True
try:
countries.get(alpha_2=code)
return True
except KeyError:
return False
def supported_country_codes():
""" Return a set of recognized country codes.
Are supported:
* ISO 3166-1 alpha-2 country codes and exceptional reservations
* European Commision country code exceptions
"""
return set(chain(
imap(attrgetter('alpha_2'), countries),
# Include ISO and EC exceptions.
COUNTRY_ALIASES.keys(),
RESERVED_COUNTRY_CODES.keys(),
COUNTRY_ALIAS_TO_SUBDIVISION.keys()))
# Switch locale via the following URL ignores currency settings!
# Switch to regions far away from your location is rejected!
# url = 'https://www.mouser.com/localsites.aspx'
# The following approach works for selecting currency:
# - Access "www.mouser.com" (done in constructor) and store local redirect URL.
# - Manually set currency preference for your regional URL.
# - Completely restart fake_browser session to apply currency settings.
# Switching locale seems to be not possible yet.
try:
if currency_iso and not locale_iso:
money = pycountry.currencies.get(alpha_3=currency_iso.upper())
locale_iso = pycountry.countries.get(numeric=money.numeric).alpha_2
if locale_iso:
currency_iso = currency_iso.upper()
country = pycountry.countries.get(alpha_2=locale_iso.upper())
# TODO: Mouser uses either "USDe" or "USDu" to select USD as
# currency, depending on your location.
if currency_iso == "USD" and locale_iso == "US":
currency_iso = "USDu"
else:
currency_iso = "USDe"
# Some mouser regions are subdomains from mouser.com, other
# regions user their own top level domains, e.g. mouser.eu.
# Extract the region specific part and suffix it to
# the preferences cookie.
local_domains = re.search("https://(.+)\.mouser\.(.+)/", self.browser.ret_url)
if local_domains.group(1).startswith("www"):
domain = local_domains.group(2)
else:
)
#:
PROTOFEED_ATTRS = [
'frequencies',
'meta',
'service_windows',
'shapes',
'shapes_extra',
'stops',
]
# Country name by country alpha-2 code
COUNTRY_BY_ALPHA2 = {
country.alpha_2: country.name
for country in pycountry.countries
}
# Country alpha-2 code by timezone
ALPHA2_BY_TIMEZONE = {
timezone: alpha2
for alpha2, timezones in pytz.country_timezones.items()
for timezone in timezones
}
# Country name by timezone
COUNTRY_BY_TIMEZONE = {
timezone: COUNTRY_BY_ALPHA2[alpha2]
for timezone, alpha2 in ALPHA2_BY_TIMEZONE.items()
}
# Country names with left hand traffic
dbm=PostgressDBM(user='opwu', password='0pwu', host='portalwatch.ai.wu.ac.at', port=5432, db='portalwatch')
P = Portal(
id=_calc_id('http://daten.buergernetz.bz.it/de/')
,uri='http://daten.buergernetz.bz.it/de/'
,apiuri = 'http://daten.buergernetz.bz.it/de/api/3'
,software = 'CKAN'
,iso = 'IT'
,active= True
)
db.add(P)
c=0
for p in dbm.getPortals():
if len(p['iso3'])>0:
iso = pycountry.countries.get(alpha3=p['iso3']).alpha2
else:
iso='EU'
P = Portal(
id=_calc_id(p['url'])
,uri=p['url']
,apiuri = p['apiurl']
,software = p['software']
,iso = iso
,active= True
)
print P
db.add(P)
c+=1
print c
def iso3166alpha3_to_iso3166alpha2(iso3):
for country in pycountry.countries:
if country.alpha_3 == iso3:
return country.alpha_2
return iso3