forked from four-o-four/omniverse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.py
More file actions
68 lines (51 loc) · 1.85 KB
/
Copy pathsettings.py
File metadata and controls
68 lines (51 loc) · 1.85 KB
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
# Settings Manager
import ConfigParser
import os
import logging
# todo a little thread safety would help
_shared_settings = ConfigParser.RawConfigParser()
class NoSectionError(ConfigParser.NoSectionError):
def __init__(self, *args, **kwargs):
ConfigParser.NoSectionError.__init__(self, *args, **kwargs)
class NoOptionError(ConfigParser.NoOptionError):
def __init__(self, *args, **kwargs):
ConfigParser.NoOptionError.__init__(self, *args, **kwargs)
class SettingsError(ConfigParser.Error):
def __init__(self, *args, **kwargs):
ConfigParser.Error.__init__(self, *args, **kwargs)
# creates a path to a file resource relative to the current working directory
# todo refactor into new module
def build_path(*args):
return os.path.join(os.path.abspath("."), *args)
def load(filename):
_shared_settings.read(build_path(filename));
def save(filename):
logging.getLogger().info("Saving settings.")
try:
fobj = open(build_path(filename), "wb")
_shared_settings.write(fobj)
except IOErrror, e:
logging.getLogger().exception("Unable to save settings.")
def get(option):
(section, option) = option.split(":")
if _shared_settings.has_section(section):
try:
return _shared_settings.get(section=section, option=option)
except ConfigParser.NoOptionError, error:
raise NoOptionError(section, option)
else:
raise NoSectionError("No section named %s." % section)
def remove(option):
(section, option) = option.split(":")
_shared_settings.remove_option(section, option)
# parses the group name/last message id pairing
# ie, "(rec.anime:3100)" -> ("rec.anime" : "3100")
def group_parse(group_option):
return tuple(group_option[1:-1].split(':'))
def set(option, value):
(section, option) = option.split(":")
try:
_shared_settings.add_section(section)
except ConfigParser.DuplicateSectionError:
pass
return _shared_settings.set(section, option, value)