settings.py 2.57 KB
Newer Older
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
# -*- 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 platform
import locale


from . import strings


class Settings(object):
    """
    This class stores all of the settings for GetTor
    """
    def __init__(self, config=False):

Hiro's avatar
Hiro committed
28

29
30
31
32
33
        # If a readable config file was provided, use that instead
        if config:
            if os.path.isfile(config):
                self.filename = config

Hiro's avatar
Hiro committed
34
35
36
        # Default config
        self.filename = self.build_filename()

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
        # Dictionary of available languages,
        # mapped to the language name, in that language
        self._available_locales = strings.get_locales()
        self._version = strings.get_version()
        self._settings = {}

    def build_filename(self):
        """
        Returns the path of the settings file.
        """
        return strings.get_resource_path('gettor.conf.json', strings.find_run_dir())

    def load(self):
        """
        Load the settings from file.
        """

        # If the settings file exists, load it
        if os.path.exists(self.filename):
            try:
                with open(self.filename, 'r') as f:
Hiro's avatar
Hiro committed
58
                  self._settings = json.load(f)
59
60
            except:
                pass
61
62
63
        else:
            self._settings = {
              "platforms": ["linux", "osx", "windows"],
Hiro's avatar
Hiro committed
64
65
              "dbname": "/srv/gettor.torproject.org/home/gettor/gettor.db",
              "email_parser_logfile": "/srv/gettor.torproject.org/home/gettor/log/email_parser.log",
Hiro's avatar
Hiro committed
66
              "email_requests_limit": 30,
Hiro's avatar
Hiro committed
67
              "twitter_requests_limit": 1,
68
              "sendmail_interval": 10,
Hiro's avatar
Hiro committed
69
              "twitter_interval": 10,
Hiro's avatar
Hiro committed
70
              "sendmail_addr": "gettor@torproject.org",
71
              "sendmail_host": "localhost",
Hiro's avatar
Hiro committed
72
73
74
75
76
              "sendmail_port": 587,
              "consumer_key": "",
              "consumer_secret": "",
              "access_key": "",
              "access_secret": "",
Hiro's avatar
Hiro committed
77
78
79
80
              "test_hid": "",
              "twitter_handle": "get_tor",
              "twitter_messages_endpoint": "https://api.twitter.com/1.1/direct_messages/events/list.json",
              "twitter_new_message_endpoint": "https://api.twitter.com/1.1/direct_messages/events/new.json"
81
            }
82
83
84

    def get(self, key):
        return self._settings[key]