Coverage for portality/datasets.py: 71%
72 statements
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-20 16:12 +0100
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-20 16:12 +0100
1"""
2~~DataSets:Data~~
3"""
5import pycountry
6from collections import OrderedDict
9def _generate_country_options():
10 """
11 ~~->Countries:Data~~
12 ~~!Countries:Data->PyCountry:Technology~~
13 Gather the countries with 2-character codes
14 """
15 country_options_ = [('', '')]
17 for co in sorted(pycountry.countries, key=lambda x: x.name):
18 try:
19 country_options_.append((co.alpha_2.upper(), co.name))
20 except AttributeError:
21 continue
22 return country_options_
25def _generate_currency_options():
26 """
27 ~~->Currencies:Data~~
28 ~~!Currencies:Data->PyCountry:Technology~~
29 :return:
30 """
31 currency_options_ = [(CURRENCY_DEFAULT, CURRENCY_DEFAULT)]
33 for cu in sorted(pycountry.currencies, key=lambda x: x.name):
34 try:
35 currency_options_.append((cu.alpha_3, '{name} ({code})'.format(code=cu.alpha_3, name=cu.name)))
36 except AttributeError:
37 continue
39 return currency_options_
42def _generate_language_options():
43 """
44 ~~->Languages:Data~~
45 ~~!Languages:Data->PyCountry:Technology~~
46 Gather the languages with 2-character codes (ISO 639-2b)
47 """
48 language_options_ = [('', '')]
49 for l in sorted(pycountry.languages, key=lambda x: x.name):
50 try:
51 language_options_.append((l.alpha_2.upper(), l.name))
52 except AttributeError:
53 continue
55 return language_options_
58def _generate_license_options():
59 """
60 ~~->Licences:Data~~
61 Licenses and their rights
62 """
63 licenses_ = {
64 # The titles and types are made to match the current values of journals in the DOAJ.
65 # DOAJ currently assumes type and title are the same.
66 "CC BY": {'BY': True, 'NC': False, 'ND': False, 'SA': False, 'form_label': 'CC BY', "url" : "https://creativecommons.org/licenses/by/4.0/"},
67 "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/"},
68 "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/"},
69 "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/"},
70 "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/"},
71 "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/"},
72 "CC0" : {'BY': False, 'NC': False, 'ND': False, 'SA': False, 'form_label': 'CC0', "url" : "https://creativecommons.org/publicdomain/zero/1.0/"},
73 "Public domain" : {'BY': False, 'NC': False, 'ND': False, 'SA': False, 'form_label': 'CC BY', "url" : "https://creativecommons.org/publicdomain/mark/1.0/"},
74 }
76 # The top-level keys in the licenses dict should always be == to the "type" of each license object
77 for lic_type, lic_info in licenses_.items():
78 lic_info['type'] = lic_type
79 lic_info['title'] = lic_type
81 license_dict_ = OrderedDict(sorted(list(licenses_.items()), key=lambda x: x[1]['type']))
83 main_license_options_ = []
84 for lic_type, lic_info in license_dict_.items():
85 main_license_options_.append((lic_type, lic_info['form_label']))
87 return licenses_, license_dict_, main_license_options_
90####################################################
91# Datasets for export
92####################################################
94# COUNTRIES
95# country_options is a list of tuples, [(2-char_code, name)] for 2-char codes (ISO 3166).
96country_options = _generate_country_options()
98# CURRENCIES
99CURRENCY_DEFAULT = ''
100# currency_options is a list of tuples, [(3-char_code, '3-char_code - name')] for all currencies.
101currency_options = _generate_currency_options()
103# LANGUAGES
104language_options = _generate_language_options()
106# LICENSES
107# licenses contains a dict with each license's permissions. license_dict is ordered by the name of the licenses.
108# main_license_options is a list of tuples [(license_type, label)] for use in dropdown menus.
109licenses, license_dict, main_license_options = _generate_license_options()
112def language_for(rep):
113 """ Get the entire language entry for a given representation """
114 try:
115 return pycountry.languages.lookup(rep)
116 except LookupError:
117 return None
120def name_for_lang(rep):
121 """ Get the language name from a representation of the language"""
122 try:
123 return pycountry.languages.lookup(rep).name
124 except LookupError:
125 return rep
128def get_country_code(current_country, fail_if_not_found=False):
129 """ Get the two-character country code for a given country name """
130 try:
131 return pycountry.countries.lookup(current_country).alpha_2
132 except LookupError:
133 return None if fail_if_not_found else current_country
136def get_country_name(code):
137 """ Get the name of a country from its two-character code """
138 try:
139 return pycountry.countries.lookup(code).name
140 except LookupError:
141 return code # return what was passed in if not found
144def get_currency_name(code):
145 """ get the name of a currency from its code """
146 try:
147 cur = pycountry.currencies.lookup(code)
148 return '{code} - {name}'.format(code=cur.alpha_3, name=cur.name)
149 except LookupError:
150 return code # return what was passed in if not found
153def get_currency_code(name):
154 """ Retrieve a currency code by the currency name """
155 try:
156 return pycountry.currencies.lookup(name).alpha_3
157 except LookupError:
158 return None