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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# -*- coding: utf-8 -*-
"""
This file is part of GetTor, a service providing alternative methods to download
the Tor Browser.
:authors: Hiro <hiro@torproject.org>
please also see AUTHORS file
:copyright: (c) 2008-2014, The Tor Project, Inc.
(c) 2014, all entities within the AUTHORS file
:license: see included LICENSE for information
"""
import json
import os
import inspect
import re
strings = {}
translations = {}
_rundir = None
def setRundir(path):
"""Set the absolute path to the runtime directory.
See :meth:`BaseOptions.postOptions`.
:param string path: The path to set.
"""
global _rundir
_rundir = path
def getRundir():
"""Get the absolute path to the runtime directory.
:rtype: string
:returns: The path to the config file.
"""
return _rundir
def find_run_dir(rundir=None):
"""Get the absolute path to the runtime directory.
:rtype: string
:returns: The path to the config file.
"""
gRundir = getRundir()
if gRundir is None:
if rundir is not None:
gRundir = os.path.abspath(os.path.expanduser(rundir))
else:
gRundir = os.getcwd()
setRundir(gRundir)
if not os.path.isdir(gRundir): # pragma: no cover
raise usage.UsageError(
"Could not change to runtime directory: `%s'" % gRundir)
return gRundir
def get_resource_path(filename, path):
"""
Returns the absolute path of a resource
"""
rundir = find_run_dir()
prefix = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))), path)
prefix = os.path.join(rundir, prefix)
if not os.path.exists(prefix):
prefix = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(prefix)))), path)
return os.path.join(prefix, filename)
def get_version():
"""
The current version
"""
version = ""
with open(get_resource_path('version.txt', '../share')) as f:
version = f.read().strip()
return version
def get_locales():
"""
Get available_locales
"""
filename = get_resource_path("available_locales.json", '../share/locale')
locales = {}
with open(filename, encoding='utf-8') as f:
locales = json.load(f)
return locales
def load_strings(current_locale):
"""
Loads translated strings and fallback to English
if the translation does not exist.
"""
global strings, translations
# Load all translations
translations = {}
available_locales = get_locales()
if current_locale not in available_locales:
current_locale = "en"
filename = get_resource_path("{}.json".format(current_locale), '../share/locale')
with open(filename, encoding='utf-8') as f:
translations[current_locale] = json.load(f)
strings = {}
for s in translations[current_locale]:
strings[s] = translations[current_locale][s]
def redact_emails(text):
redacted_text = re.sub(r'[\w\.+-]+@[\w\.+-]+', '[REDACTED_EMAIL]', text)
return redacted_text
def translated(k):
"""
Returns a translated string.
"""
return strings[k]
_ = translated
|