forked from RaphaelWimmer/open-access-media-importer
-
Notifications
You must be signed in to change notification settings - Fork 8
/
config.py
85 lines (70 loc) · 2.94 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from os import makedirs, path
from sys import stderr, exit
from xdg import BaseDirectory
from ConfigParser import RawConfigParser, NoSectionError, NoOptionError
APPLICATION_NAME="open-access-media-importer"
cache_path = path.join(BaseDirectory.xdg_cache_home, APPLICATION_NAME)
config_path = path.join(BaseDirectory.xdg_config_home, APPLICATION_NAME)
data_path = path.join(BaseDirectory.xdg_data_home, APPLICATION_NAME)
def database_path(source):
return path.join(data_path, '%s.sqlite' % source)
def ensure_directory_exists(directory):
if not path.exists(directory):
makedirs(directory)
for p in (cache_path, config_path, data_path):
ensure_directory_exists(p)
_metadata_path = path.join(cache_path, 'metadata')
_metadata_raw_path = path.join(_metadata_path, 'raw')
def get_metadata_raw_source_path(source_name):
p = path.join(_metadata_raw_path, source_name)
ensure_directory_exists(p)
return p
_metadata_refined_path = path.join(_metadata_path, 'refined')
def get_metadata_refined_source_path(source_name):
p = path.join(_metadata_refined_path, source_name)
ensure_directory_exists(p)
return p
_media_path = path.join(cache_path, 'media')
_media_raw_path = path.join(_media_path, 'raw')
def get_media_raw_source_path(source_name):
p = path.join(_media_raw_path, source_name)
ensure_directory_exists(p)
return p
_media_refined_path = path.join(_media_path, 'refined')
def get_media_refined_source_path(source_name):
p = path.join(_media_refined_path, source_name)
ensure_directory_exists(p)
return p
free_license_urls = [
'http://creativecommons.org/licenses/by/2.0/',
'http://creativecommons.org/licenses/by-sa/2.0/',
'http://creativecommons.org/licenses/by/2.5/',
'http://creativecommons.org/licenses/by-sa/2.5/',
'http://creativecommons.org/licenses/by/3.0/',
'http://creativecommons.org/licenses/by-sa/3.0/',
'http://creativecommons.org/licenses/by/4.0/',
'http://creativecommons.org/licenses/by-sa/4.0/',
'http://creativecommons.org/publicdomain/zero/1.0/'
]
USERCONFIG_FILENAME = "userconfig"
userconfig_file = path.join(config_path, USERCONFIG_FILENAME)
userconfig = RawConfigParser()
userconfig.optionsxform = str # case sensitivity
userconfig.read(userconfig_file)
def get_userconfig(section, option):
try:
return userconfig.get(section, option)
except NoSectionError:
stderr.write("“%s” does not contain a “%s” section.\n" % \
(userconfig_file, section))
exit(127)
except NoOptionError:
stderr.write("“%s” does not contain a “%s” option in the “%s” section.\n" % \
(userconfig_file, option, section))
exit(127)
api_url = get_userconfig('wiki', 'api_url')
username = get_userconfig('wiki', 'username')
password = get_userconfig('wiki', 'password')
whitelist_doi = get_userconfig('whitelist', 'doi').split()