Coverage for portality / datasets.py: 91%
69 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-05 00:09 +0100
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-05 00:09 +0100
1"""
2~~DataSets:Data~~
3"""
5import pycountry
6from collections import OrderedDict
7from portality.lib import isolang
10def _generate_country_options():
11 """
12 ~~->Countries:Data~~
13 ~~!Countries:Data->PyCountry:Technology~~
14 Gather the countries with 2-character codes
15 """
16 country_options_ = [('', '')]
18 # add Kosovo
19 pycountry.countries.add_entry(
20 alpha_2="XK", alpha_3="XXK", name="Kosovo", numeric="926"
21 )
23 for co in sorted(pycountry.countries, key=lambda x: x.name):
24 try:
25 country_options_.append((co.alpha_2.upper(), co.name))
26 except AttributeError:
27 continue
28 return country_options_
31def _generate_currency_options():
32 """
33 ~~->Currencies:Data~~
34 ~~!Currencies:Data->PyCountry:Technology~~
35 :return:
36 """
37 currency_options_ = [(CURRENCY_DEFAULT, CURRENCY_DEFAULT)]
39 for cu in sorted(pycountry.currencies, key=lambda x: x.name):
40 try:
41 currency_options_.append((cu.alpha_3, '{name} ({code})'.format(code=cu.alpha_3, name=cu.name)))
42 except AttributeError:
43 continue
45 return currency_options_
48def _generate_language_options():
49 """
50 ~~->Languages:Data~~
51 ~~!Languages:Data->PyCountry:Technology~~
52 Gather the languages with 2-character codes (ISO 639-1)
53 """
54 language_options_ = [('', '')]
55 for l in sorted(pycountry.languages, key=lambda x: x.name):
56 try:
57 language_options_.append((l.alpha_2.upper(), l.name))
58 except AttributeError:
59 continue
61 return language_options_
64def _generate_license_options():
65 """
66 ~~->Licences:Data~~
67 Licenses and their rights
68 """
69 licenses_ = {
70 # The titles and types are made to match the current values of journals in the DOAJ.
71 # DOAJ currently assumes type and title are the same.
72 "CC BY": {'BY': True, 'NC': False, 'ND': False, 'SA': False, 'form_label': 'CC BY', "url" : "https://creativecommons.org/licenses/by/4.0/"},
73 "CC BY-SA": {'BY': True, 'NC': False, 'ND': False, 'SA': True, 'form_label': 'CC BY-SA', "url" : "https://creativecommons.org/licenses/by-sa/4.0/"},
74 "CC BY-ND": {'BY': True, 'NC': False, 'ND': True, 'SA': False, 'form_label': 'CC BY-ND', "url" : "https://creativecommons.org/licenses/by-nd/4.0/"},
75 "CC BY-NC": {'BY': True, 'NC': True, 'ND': False, 'SA': False, 'form_label': 'CC BY-NC', "url" : "https://creativecommons.org/licenses/by-nc/4.0/"},
76 "CC BY-NC-SA": {'BY': True, 'NC': True, 'ND': False, 'SA': True, 'form_label': 'CC BY-NC-SA', "url" : "https://creativecommons.org/licenses/by-nc-sa/4.0/"},
77 "CC BY-NC-ND": {'BY': True, 'NC': True, 'ND': True, 'SA': False, 'form_label': 'CC BY-NC-ND', "url" : "https://creativecommons.org/licenses/by-nc-nd/4.0/"},
78 "CC0" : {'BY': False, 'NC': False, 'ND': False, 'SA': False, 'form_label': 'CC0', "url" : "https://creativecommons.org/publicdomain/zero/1.0/"},
79 "Public domain" : {'BY': False, 'NC': False, 'ND': False, 'SA': False, 'form_label': 'CC BY', "url" : "https://creativecommons.org/publicdomain/mark/1.0/"},
80 }
82 # The top-level keys in the licenses dict should always be == to the "type" of each license object
83 for lic_type, lic_info in licenses_.items():
84 lic_info['type'] = lic_type
85 lic_info['title'] = lic_type
87 license_dict_ = OrderedDict(sorted(list(licenses_.items()), key=lambda x: x[1]['type']))
89 main_license_options_ = []
90 for lic_type, lic_info in license_dict_.items():
91 main_license_options_.append((lic_type, lic_info['form_label']))
93 return licenses_, license_dict_, main_license_options_
96####################################################
97# Datasets for export
98####################################################
100# COUNTRIES
101# country_options is a list of tuples, [(2-char_code, name)] for 2-char codes (ISO 3166).
102country_options = _generate_country_options()
104# CURRENCIES
105CURRENCY_DEFAULT = ''
106# currency_options is a list of tuples, [(3-char_code, '3-char_code - name')] for all currencies.
107currency_options = _generate_currency_options()
109# LANGUAGES
110language_options = _generate_language_options()
112# LICENSES
113# licenses contains a dict with each license's permissions. license_dict is ordered by the name of the licenses.
114# main_license_options is a list of tuples [(license_type, label)] for use in dropdown menus.
115licenses, license_dict, main_license_options = _generate_license_options()
118def language_for(rep):
119 """ Get the entire language entry for a given representation """
120 return isolang.find_raw(rep)
123def name_for_lang(rep):
124 """ Get the language name from a representation of the language"""
125 lang = isolang.find_raw(rep)
126 return lang.name if lang is not None else rep
129def get_country_code(current_country, fail_if_not_found=False):
130 """ Get the two-character country code for a given country name """
131 try:
132 return pycountry.countries.lookup(current_country).alpha_2
133 except LookupError:
134 return None if fail_if_not_found else current_country
137def get_country_name(code):
138 """ Get the name of a country from its two-character code """
139 try:
140 return pycountry.countries.lookup(code).name
141 except LookupError:
142 return code # return what was passed in if not found
145def get_currency_name(code):
146 """ get the name of a currency from its code """
147 try:
148 cur = pycountry.currencies.lookup(code)
149 return '{code} - {name}'.format(code=cur.alpha_3, name=cur.name)
150 except LookupError:
151 return code # return what was passed in if not found
154def get_currency_code(name, fail_if_not_found=True):
155 """ Retrieve a currency code by the currency name """
156 try:
157 return pycountry.currencies.lookup(name).alpha_3
158 except LookupError:
159 return None if fail_if_not_found else name