Configuration casts values on get instead of set

This commit is contained in:
Frank DeMarco 2012-08-25 17:33:43 -04:00
parent 9b5e29860b
commit d18d28e210
4 changed files with 54 additions and 42 deletions

View File

@ -26,30 +26,9 @@ class Configuration(RawConfigParser):
type_declarations = TypeDeclarations()
self.type_declarations = type_declarations
def set(self, section, option, value):
value = self.cast_value(section, option, value)
RawConfigParser.set(self, section, option, value)
def cast_value(self, section, option, value):
pair = section, option
types = self.type_declarations
if type(value) == str:
if pair in types["bool"]:
return True if value == "yes" else False
elif pair in types["int"]:
return int(value)
elif pair in types["float"]:
return float(value)
elif pair in types["path"]:
return self.translate_path(value)
elif pair in types["list"]:
if value == "":
return []
else:
return map(str.strip, value.split(types.list_member_sep))
elif pair in types["int-list"]:
return map(int, value.split(types.list_member_sep))
return value
# def set(self, section, option, value):
# value = self.cast_value(section, option, value)
# RawConfigParser.set(self, section, option, value)
def translate_path(self, path):
new = ""
@ -62,7 +41,8 @@ class Configuration(RawConfigParser):
set_option = self.set
section = "setup"
add_section(section)
set_option(section, "packages", basename(getcwd()) + ", pgfw")
set_option(section, "package-root", basename(getcwd()))
set_option(section, "additional-packages", "")
set_option(section, "title", "")
set_option(section, "classifiers", "")
set_option(section, "resources-search-path", "./, resources/")
@ -108,12 +88,12 @@ class Configuration(RawConfigParser):
set_option(section, "custom-event-id", "USEREVENT")
set_option(section, "command-event-name", "command")
def read(self, filenames):
files_read = RawConfigParser.read(self, filenames)
for section in self.sections():
for option, value in self.items(section):
self.set(section, option, value)
return files_read
# def read(self, filenames):
# files_read = RawConfigParser.read(self, filenames)
# for section in self.sections():
# for option, value in self.items(section):
# self.set(section, option, value)
# return files_read
def read_project_config_file(self):
path = self.locate_project_config_file()
@ -153,7 +133,7 @@ class Configuration(RawConfigParser):
def set_installation_path(self):
self.set("setup", "installation-path",
join(self.get("setup", "installation-dir"),
self.get("setup", "packages")[0]))
self.get("setup", "package-root")))
def set_resources_search_path(self):
section, option = "setup", "resources-search-path"
@ -168,13 +148,34 @@ class Configuration(RawConfigParser):
value = RawConfigParser.get(self, section, option)
if value is None:
value = self.get_substitute(section, option)
return value
return self.cast_value(section, option, value)
def get_substitute(self, section, option):
if section == "display":
if option == "caption":
return self.get("setup", "title")
def cast_value(self, section, option, value):
pair = section, option
types = self.type_declarations
if type(value) == str:
if pair in types["bool"]:
return True if value == "yes" else False
elif pair in types["int"]:
return int(value)
elif pair in types["float"]:
return float(value)
elif pair in types["path"]:
return self.translate_path(value)
elif pair in types["list"]:
if value == "":
return []
else:
return map(str.strip, value.split(types.list_member_sep))
elif pair in types["int-list"]:
return map(int, value.split(types.list_member_sep))
return value
def set_screen_captures_path(self):
section, option = "screen-captures", "path"
if not self.has_option(section, option):
@ -182,7 +183,7 @@ class Configuration(RawConfigParser):
self.get(section, "rel-path")))
def build_home_path(self):
return join("~", "." + self.get("setup", "packages")[0])
return join("~", "." + self.get("setup", "package-root"))
def set_data_exclusion_list(self):
section, option = "setup", "data-exclude"
@ -191,8 +192,9 @@ class Configuration(RawConfigParser):
exclude = self.get(section, option)
exclude += [".git", ".gitignore", "README", "build/", "dist/",
"setup.py", "MANIFEST", "PKG-INFO",
self.get("setup", "changelog")]
for location in self.get("setup", "packages"):
self.get("setup", "changelog"),
self.get("setup", "package-root")]
for location in self.get("setup", "additional-packages"):
exclude.append(location)
self.set(section, option, exclude)
@ -217,6 +219,12 @@ class Configuration(RawConfigParser):
config[section] = self.get_section(section)
return pformat(config, 2, 1)
def items(self, section):
items = []
for option in self.options(section):
items.append((option, self.get(section, option)))
return items
class TypeDeclarations(dict):
@ -226,10 +234,12 @@ class TypeDeclarations(dict):
"int-list": "dimensions"},
"screen-captures": {"path": "path"},
"setup": {"list": ["classifiers", "resources-search-path",
"requirements", "data-exclude", "packages"],
"requirements", "data-exclude",
"additional-packages"],
"path": ["installation-dir", "changelog",
"description-file", "main-object",
"icon-path", "windows-dist-path"]},
"icon-path", "windows-dist-path",
"package-root"]},
"keys": {"list": ["up", "right", "down", "left"]}}
additional_defaults = {}

View File

@ -69,5 +69,5 @@ class GameChild:
def is_command(self, evt, cmd):
name = self.get_configuration().get("event", "command-event-name")
return evt.type == self.get_user_event_id() and evt.name == name and \
return evt.type == self.get_custom_event_id() and evt.name == name and \
evt.command == cmd

View File

@ -43,9 +43,10 @@ class Input(GameChild):
self.post_command(cmd)
def post_command(self, cmd):
eid = self.get_user_event_id()
config = self.get_configuration().get_section("event")
eid = self.get_custom_event_id()
self.print_debug("Posting {0} command with id {1}".format(cmd, eid))
name = self.get_configuration().get("event", "command-event-name")
name = config["command-event-name"]
event.post(event.Event(eid, name=name, command=cmd))
def translate_joy_button(self, evt):

View File

@ -24,7 +24,8 @@ class Setup:
def build_package_list(self):
packages = []
locations = self.config.get("setup", "packages")
config = self.config.get_section("setup")
locations = [config["package-root"]] + config["additional-packages"]
for location in locations:
if exists(location):
for root, dirs, files in walk(location, followlinks=True):