Modify khashmir's config system to not use the const module.
[quix0rs-apt-p2p.git] / apt_dht / apt_dht_conf.py
1
2 import os, sys
3 from ConfigParser import SafeConfigParser
4
5 from twisted.python import log, versions
6
7 class ConfigError(Exception):
8     def __init__(self, message):
9         self.message = message
10     def __str__(self):
11         return repr(self.message)
12
13 version = versions.Version('apt-dht', 0, 0, 0)
14 home = os.path.expandvars('${HOME}')
15 if home == '${HOME}' or not os.path.isdir(home):
16     home = os.path.expanduser('~')
17     if not os.path.isdir(home):
18         home = os.path.abspath(os.path.dirname(sys.argv[0]))
19
20 DEFAULTS = {
21
22     # Port to listen on for all requests (TCP and UDP)
23     'PORT': '9977',
24     
25     # Directory to store the downloaded files in
26     'CACHE_DIR': home + '/.apt-dht/cache',
27     
28     # User name to try and run as
29     'USERNAME': '',
30
31     # Which DHT implementation to use.
32     # It must be possile to do "from <DHT>.DHT import DHT" to get a class that
33     # implements the IDHT interface.
34     'DHT': 'apt_dht_Khashmir',
35
36     # Whether to only run the DHT (for providing a login node)
37     'DHT-ONLY': 'no',
38 }
39
40 DHT_DEFAULTS = {
41     # bootstrap nodes to contact to join the DHT
42     'BOOTSTRAP': """www.camrdale.org:9977
43         steveholt.hopto.org:9977""",
44     
45     # Kademlia "K" constant, this should be an even number
46     'K': '8',
47     
48     # SHA1 is 160 bits long
49     'HASH_LENGTH': '160',
50     
51     # checkpoint every this many seconds
52     'CHECKPOINT_INTERVAL': '15m', # fifteen minutes
53     
54     ### SEARCHING/STORING
55     # concurrent xmlrpc calls per find node/value request!
56     'CONCURRENT_REQS': '4',
57     
58     # how many hosts to post to
59     'STORE_REDUNDANCY': '3',
60     
61     ###  ROUTING TABLE STUFF
62     # how many times in a row a node can fail to respond before it's booted from the routing table
63     'MAX_FAILURES': '3',
64     
65     # never ping a node more often than this
66     'MIN_PING_INTERVAL': '15m', # fifteen minutes
67     
68     # refresh buckets that haven't been touched in this long
69     'BUCKET_STALENESS': '1h', # one hour
70     
71     ###  KEY EXPIRER
72     # time before expirer starts running
73     'KEINITIAL_DELAY': '15s', # 15 seconds - to clean out old stuff in persistent db
74     
75     # time between expirer runs
76     'KE_DELAY': '20m', # 20 minutes
77     
78     # expire entries older than this
79     'KE_AGE': '1h', # 60 minutes
80 }
81
82 class AptDHTConfigParser(SafeConfigParser):
83     """
84     Adds 'gettime' to ConfigParser to interpret the suffixes.
85     """
86     time_multipliers={
87         's': 1,    #seconds
88         'm': 60,   #minutes
89         'h': 3600, #hours
90         'd': 86400,#days
91         }
92
93     def gettime(self, section, option):
94         mult = 1
95         value = self.get(section, option)
96         if len(value) == 0:
97             raise ConfigError("Configuration parse error: [%s] %s" % (section, option))
98         suffix = value[-1].lower()
99         if suffix in self.time_multipliers.keys():
100             mult = self.time_multipliers[suffix]
101             value = value[:-1]
102         return int(value)*mult
103     def getstring(self, section, option):
104         return self.get(section,option)
105     def getstringlist(self, section, option):
106         return self.get(section,option).split()
107     def optionxform(self, option):
108         return option.upper()
109
110 config = AptDHTConfigParser(DEFAULTS)
111 config.add_section(config.get('DEFAULT', 'DHT'))
112 for k in DHT_DEFAULTS:
113     config.set(config.get('DEFAULT', 'DHT'), k, DHT_DEFAULTS[k])