-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NAS-133661 / 25.04 / simplify country_choices endpoints (#15433)
* simplify country_choices endpoints * more generic function name and add docstring
- Loading branch information
Showing
3 changed files
with
24 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 7 additions & 48 deletions
55
src/middlewared/middlewared/plugins/system_general/country.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,17 @@ | ||
import csv | ||
|
||
from middlewared.schema import accepts, Dict, returns | ||
from middlewared.service import private, Service | ||
|
||
from middlewared.service import Service | ||
from middlewared.utils.country_codes import get_country_codes | ||
|
||
class SystemGeneralService(Service): | ||
|
||
COUNTRY_CHOICES = None | ||
|
||
class Config: | ||
namespace = 'system.general' | ||
cli_namespace = 'system.general' | ||
|
||
@accepts() | ||
@returns(Dict('country_choices', additional_attrs=True, register=True)) | ||
async def country_choices(self): | ||
""" | ||
Returns country choices. | ||
""" | ||
if not self.COUNTRY_CHOICES: | ||
self.COUNTRY_CHOICES = await self.middleware.call('system.general.get_country_choices') | ||
|
||
return self.COUNTRY_CHOICES | ||
|
||
@private | ||
def get_country_choices(self): | ||
def _get_index(country_columns, column): | ||
index = -1 | ||
i = 0 | ||
for c in country_columns: | ||
if c.lower() == column.lower(): | ||
index = i | ||
break | ||
|
||
i += 1 | ||
return index | ||
|
||
country_file = '/etc/iso_3166_2_countries.csv' | ||
cni, two_li = None, None | ||
country_choices = {} | ||
with open(country_file, 'r', encoding='utf-8') as csvfile: | ||
reader = csv.reader(csvfile) | ||
|
||
for index, row in enumerate(reader): | ||
if index != 0: | ||
if row[cni] and row[two_li]: | ||
if row[two_li] in country_choices: | ||
# If two countries in the iso file have the same key, we concatenate their names | ||
country_choices[row[two_li]] += f' + {row[cni]}' | ||
else: | ||
country_choices[row[two_li]] = row[cni] | ||
else: | ||
# ONLY CNI AND TWO_LI ARE BEING CONSIDERED FROM THE CSV | ||
cni = _get_index(row, 'Common Name') | ||
two_li = _get_index(row, 'ISO 3166-1 2 Letter Code') | ||
|
||
return country_choices | ||
def country_choices(self): | ||
"""Return a dictionary whose keys represent the | ||
ISO 3166-1 alpha 2 country code and values represent | ||
the English short name (used in ISO 3166/MA)""" | ||
return get_country_codes() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from functools import cache | ||
from json import load | ||
|
||
__all__ = ("get_country_codes",) | ||
|
||
|
||
@cache | ||
def get_country_codes() -> dict[str, str]: | ||
"""Return the ISO 3166-1 alpha 2 code as the key and the | ||
English short name (used in ISO 3166/MA) of the country | ||
as the value (i.e {"US": "United States of America", ...})""" | ||
with open("/usr/share/iso-codes/json/iso_3166-1.json") as f: | ||
return {i["alpha_2"]: i["name"] for i in load(f)["3166-1"]} |