git-svn-id: https://s3tools.svn.sourceforge.net/svnroot/s3tools/s3py/trunk@53 830e0280-6d2a-0410-9c65-932aecc39d9d
Michal Ludvig authored on 2007/01/18 11:21:17... | ... |
@@ -2,6 +2,76 @@ import logging |
2 | 2 |
from logging import debug, info, warning, error |
3 | 3 |
import re |
4 | 4 |
|
5 |
+class Config(object): |
|
6 |
+ _instance = None |
|
7 |
+ _parsed_files = [] |
|
8 |
+ access_key = "" |
|
9 |
+ secret_key = "" |
|
10 |
+ host = "s3.amazonaws.com" |
|
11 |
+ verbosity = logging.WARNING |
|
12 |
+ send_chunk = 4096 |
|
13 |
+ recv_chunk = 4096 |
|
14 |
+ human_readable_sizes = False |
|
15 |
+ force = False |
|
16 |
+ show_uri = False |
|
17 |
+ acl_public = False |
|
18 |
+ |
|
19 |
+ ## Creating a singleton |
|
20 |
+ def __new__(self, configfile = None): |
|
21 |
+ if self._instance is None: |
|
22 |
+ self._instance = object.__new__(self) |
|
23 |
+ return self._instance |
|
24 |
+ |
|
25 |
+ def __init__(self, configfile = None): |
|
26 |
+ if configfile: |
|
27 |
+ self.read_config_file(configfile) |
|
28 |
+ |
|
29 |
+ def option_list(self): |
|
30 |
+ retval = [] |
|
31 |
+ for option in dir(self): |
|
32 |
+ ## Skip attributes that start with underscore or are not string, int or bool |
|
33 |
+ option_type = type(getattr(Config, option)) |
|
34 |
+ if option.startswith("_") or \ |
|
35 |
+ not (option_type in ( |
|
36 |
+ type("string"), # str |
|
37 |
+ type(42), # int |
|
38 |
+ type(True))): # bool |
|
39 |
+ continue |
|
40 |
+ retval.append(option) |
|
41 |
+ return retval |
|
42 |
+ |
|
43 |
+ def read_config_file(self, configfile): |
|
44 |
+ cp = ConfigParser(configfile) |
|
45 |
+ for option in self.option_list(): |
|
46 |
+ self.update_option(option, cp.get(option)) |
|
47 |
+ self._parsed_files.append(configfile) |
|
48 |
+ |
|
49 |
+ def update_option(self, option, value): |
|
50 |
+ if value is None: |
|
51 |
+ return |
|
52 |
+ #### Special treatment of some options |
|
53 |
+ ## verbosity must be known to "logging" module |
|
54 |
+ if option == "verbosity": |
|
55 |
+ try: |
|
56 |
+ setattr(Config, "verbosity", logging._levelNames[value]) |
|
57 |
+ except KeyError: |
|
58 |
+ error("Config: verbosity level '%s' is not valid" % value) |
|
59 |
+ ## allow yes/no, true/false, on/off and 1/0 for boolean options |
|
60 |
+ elif type(getattr(Config, option)) is type(True): # bool |
|
61 |
+ if str(value).lower() in ("true", "yes", "on", "1"): |
|
62 |
+ setattr(Config, option, True) |
|
63 |
+ elif str(value).lower() in ("false", "no", "off", "0"): |
|
64 |
+ setattr(Config, option, False) |
|
65 |
+ else: |
|
66 |
+ error("Config: value of option '%s' must be Yes or No, not '%s'" % (option, value)) |
|
67 |
+ elif type(getattr(Config, option)) is type(42): # int |
|
68 |
+ try: |
|
69 |
+ setattr(Config, option, int(value)) |
|
70 |
+ except ValueError, e: |
|
71 |
+ error("Config: value of option '%s' must be an integer, not '%s'" % (option, value)) |
|
72 |
+ else: # string |
|
73 |
+ setattr(Config, option, value) |
|
74 |
+ |
|
5 | 75 |
class ConfigParser: |
6 | 76 |
def __init__(self, file, sections = []): |
7 | 77 |
self.cfg = {} |
... | ... |
@@ -10,77 +10,7 @@ from stat import ST_SIZE |
10 | 10 |
from Utils import * |
11 | 11 |
from SortedDict import SortedDict |
12 | 12 |
from BidirMap import BidirMap |
13 |
-from Config import ConfigParser |
|
14 |
- |
|
15 |
-class Config(object): |
|
16 |
- _instance = None |
|
17 |
- _parsed_files = [] |
|
18 |
- access_key = "" |
|
19 |
- secret_key = "" |
|
20 |
- host = "s3.amazonaws.com" |
|
21 |
- verbosity = logging.WARNING |
|
22 |
- send_chunk = 4096 |
|
23 |
- recv_chunk = 4096 |
|
24 |
- human_readable_sizes = False |
|
25 |
- force = False |
|
26 |
- show_uri = False |
|
27 |
- acl_public = False |
|
28 |
- |
|
29 |
- ## Creating a singleton |
|
30 |
- def __new__(self, configfile = None): |
|
31 |
- if self._instance is None: |
|
32 |
- self._instance = object.__new__(self) |
|
33 |
- return self._instance |
|
34 |
- |
|
35 |
- def __init__(self, configfile = None): |
|
36 |
- if configfile: |
|
37 |
- self.read_config_file(configfile) |
|
38 |
- |
|
39 |
- def option_list(self): |
|
40 |
- retval = [] |
|
41 |
- for option in dir(self): |
|
42 |
- ## Skip attributes that start with underscore or are not string, int or bool |
|
43 |
- option_type = type(getattr(Config, option)) |
|
44 |
- if option.startswith("_") or \ |
|
45 |
- not (option_type in ( |
|
46 |
- type("string"), # str |
|
47 |
- type(42), # int |
|
48 |
- type(True))): # bool |
|
49 |
- continue |
|
50 |
- retval.append(option) |
|
51 |
- return retval |
|
52 |
- |
|
53 |
- def read_config_file(self, configfile): |
|
54 |
- cp = ConfigParser(configfile) |
|
55 |
- for option in self.option_list(): |
|
56 |
- self.update_option(option, cp.get(option)) |
|
57 |
- self._parsed_files.append(configfile) |
|
58 |
- |
|
59 |
- def update_option(self, option, value): |
|
60 |
- if value is None: |
|
61 |
- return |
|
62 |
- #### Special treatment of some options |
|
63 |
- ## verbosity must be known to "logging" module |
|
64 |
- if option == "verbosity": |
|
65 |
- try: |
|
66 |
- setattr(Config, "verbosity", logging._levelNames[value]) |
|
67 |
- except KeyError: |
|
68 |
- error("Config: verbosity level '%s' is not valid" % value) |
|
69 |
- ## allow yes/no, true/false, on/off and 1/0 for boolean options |
|
70 |
- elif type(getattr(Config, option)) is type(True): # bool |
|
71 |
- if str(value).lower() in ("true", "yes", "on", "1"): |
|
72 |
- setattr(Config, option, True) |
|
73 |
- elif str(value).lower() in ("false", "no", "off", "0"): |
|
74 |
- setattr(Config, option, False) |
|
75 |
- else: |
|
76 |
- error("Config: value of option '%s' must be Yes or No, not '%s'" % (option, value)) |
|
77 |
- elif type(getattr(Config, option)) is type(42): # int |
|
78 |
- try: |
|
79 |
- setattr(Config, option, int(value)) |
|
80 |
- except ValueError, e: |
|
81 |
- error("Config: value of option '%s' must be an integer, not '%s'" % (option, value)) |
|
82 |
- else: # string |
|
83 |
- setattr(Config, option, value) |
|
13 |
+from Config import Config |
|
84 | 14 |
|
85 | 15 |
class S3Error (Exception): |
86 | 16 |
def __init__(self, response): |