Configuration options stored as text, cast on retrieval
This commit is contained in:
parent
78557d7c71
commit
c54ebd914d
|
@ -33,7 +33,8 @@ class Animation:
|
|||
last_ticks = self.last_ticks
|
||||
actual_frame_duration = self.get_ticks() - last_ticks
|
||||
last_ticks = self.get_ticks()
|
||||
wait_duration = self.get_configuration().get("display", "wait-duration")
|
||||
wait_duration = self.get_configuration().get("display", "wait-duration",
|
||||
"int")
|
||||
while actual_frame_duration < self.target_frame_duration:
|
||||
pygame.time.wait(wait_duration)
|
||||
actual_frame_duration += self.get_ticks() - last_ticks
|
||||
|
|
|
@ -6,16 +6,17 @@ class Display(GameChild):
|
|||
|
||||
def __init__(self, game):
|
||||
GameChild.__init__(self, game)
|
||||
self.config = self.get_configuration().get_section("display")
|
||||
self.config = self.get_configuration()
|
||||
self.set_screen()
|
||||
self.set_caption()
|
||||
self.set_icon()
|
||||
|
||||
def set_screen(self):
|
||||
self.screen = display.set_mode(self.config["dimensions"])
|
||||
self.screen = display.set_mode(self.config.get("display", "dimensions",
|
||||
"int-list"))
|
||||
|
||||
def set_caption(self):
|
||||
display.set_caption(self.config["caption"])
|
||||
display.set_caption(self.config.get("display", "caption"))
|
||||
|
||||
def set_icon(self):
|
||||
if self.get_configuration().has_option("display", "icon-path"):
|
||||
|
|
|
@ -39,10 +39,11 @@ class Game(GameChild, Animation):
|
|||
|
||||
def init_animation(self):
|
||||
Animation.__init__(self,
|
||||
self.configuration.get("display", "frame-duration"))
|
||||
self.configuration.get("display", "frame-duration",
|
||||
"int"))
|
||||
|
||||
def align_window(self):
|
||||
if self.configuration.get("display", "centered"):
|
||||
if self.configuration.get("display", "centered", "bool"):
|
||||
environ["SDL_VIDEO_CENTERED"] = "1"
|
||||
|
||||
def set_children(self):
|
||||
|
|
|
@ -34,8 +34,8 @@ class GameChild:
|
|||
|
||||
def get_resource(self, section, option):
|
||||
config = self.get_configuration()
|
||||
rel_path = config.get(section, option)
|
||||
for root in config.get("setup", "resources-search-path"):
|
||||
rel_path = config.get(section, option, "path")
|
||||
for root in config.get("setup", "resources-search-path", "list"):
|
||||
if self.is_shared_mode() and not self.is_absolute_path(root):
|
||||
continue
|
||||
path = join(root, rel_path)
|
||||
|
|
|
@ -25,17 +25,14 @@ class Input(GameChild):
|
|||
|
||||
def build_key_map(self):
|
||||
key_map = {}
|
||||
for command, keys in self.get_configuration().items("keys"):
|
||||
config = self.get_configuration()
|
||||
for command, keys in config.get_section("keys", "list").iteritems():
|
||||
key_map[command] = []
|
||||
if type(keys) == str:
|
||||
keys = [keys]
|
||||
for key in keys:
|
||||
key_map[command].append(globals()[key])
|
||||
self.key_map = key_map
|
||||
|
||||
def translate_key_press(self, evt):
|
||||
self.print_debug("You pressed {0}, suppressed => {1}".\
|
||||
format(evt.key, self.suppressed))
|
||||
if not self.suppressed:
|
||||
key = evt.key
|
||||
for cmd, keys in self.key_map.iteritems():
|
||||
|
@ -44,7 +41,6 @@ class Input(GameChild):
|
|||
|
||||
def post_command(self, cmd):
|
||||
eid = self.get_user_event_id()
|
||||
self.print_debug("Posting {0} command with id {1}".format(cmd, eid))
|
||||
name = self.get_configuration().get("event", "command-event-name")
|
||||
event.post(event.Event(eid, name=name, command=cmd))
|
||||
|
||||
|
|
|
@ -16,7 +16,8 @@ class ScreenGrabber(GameChild):
|
|||
|
||||
def save_display(self, event):
|
||||
if self.is_command(event, "capture-screen"):
|
||||
directory = self.get_configuration().get("screen-captures", "path")
|
||||
directory = self.get_configuration().get("screen-captures", "path",
|
||||
"path")
|
||||
try:
|
||||
if not exists(directory):
|
||||
makedirs(directory)
|
||||
|
|
|
@ -20,7 +20,7 @@ class Setup:
|
|||
|
||||
def build_package_list(self):
|
||||
packages = []
|
||||
package_root = self.config.get("setup", "package-root")
|
||||
package_root = self.config.get("setup", "package-root", "path")
|
||||
if exists(package_root):
|
||||
for root, dirs, files in walk(package_root, followlinks=True):
|
||||
packages.append(root.replace(sep, "."))
|
||||
|
@ -28,15 +28,17 @@ class Setup:
|
|||
|
||||
def build_data_map(self):
|
||||
include = []
|
||||
config = self.config.get_section("setup")
|
||||
exclude = map(normpath, config["data-exclude"])
|
||||
config = self.config
|
||||
exclude = map(normpath, config.get("setup", "data-exclude", "list"))
|
||||
for root, dirs, files in walk("."):
|
||||
dirs = self.remove_excluded(dirs, root, exclude)
|
||||
files = [join(root, f) for f in self.remove_excluded(files, root,
|
||||
exclude)]
|
||||
if files:
|
||||
include.append((normpath(join(config["installation-path"],
|
||||
root)), files))
|
||||
include.append((normpath(join(config.get("setup",
|
||||
"installation-path",
|
||||
"path"), root)),
|
||||
files))
|
||||
return include
|
||||
|
||||
def remove_excluded(self, paths, root, exclude):
|
||||
|
@ -53,7 +55,7 @@ class Setup:
|
|||
|
||||
def build_description(self):
|
||||
description = ""
|
||||
path = self.config.get("setup", "description-file")
|
||||
path = self.config.get("setup", "description-file", "path")
|
||||
if exists(path):
|
||||
description = "\n{0}\n{1}\n{2}".format(file(path).read(),
|
||||
"Changelog\n=========",
|
||||
|
@ -62,7 +64,7 @@ class Setup:
|
|||
|
||||
def translate_changelog(self):
|
||||
translation = ""
|
||||
path = self.config.get("setup", "changelog")
|
||||
path = self.config.get("setup", "changelog", "path")
|
||||
if exists(path):
|
||||
lines = file(path).readlines()
|
||||
package_name = lines[0].split()[0]
|
||||
|
@ -79,22 +81,23 @@ class Setup:
|
|||
return translation
|
||||
|
||||
def setup(self):
|
||||
config = self.config.get_section("setup")
|
||||
config = self.config
|
||||
section = config.get_section("setup")
|
||||
setup(cmdclass={"install": insert_resources_path},
|
||||
name=self.translate_title(),
|
||||
packages=self.build_package_list(),
|
||||
scripts=[config["init-script"]],
|
||||
scripts=[config.get("setup", "init-script", "path")],
|
||||
data_files=self.build_data_map(),
|
||||
requires=config["requirements"],
|
||||
version=config["version"],
|
||||
description=config["summary"],
|
||||
classifiers=config["classifiers"],
|
||||
requires=config.get("setup", "requirements", "list"),
|
||||
version=section["version"],
|
||||
description=section["summary"],
|
||||
classifiers=config.get("setup", "classifiers", "list"),
|
||||
long_description=self.build_description(),
|
||||
license=config["license"],
|
||||
platforms=config["platforms"],
|
||||
author=config["contact-name"],
|
||||
author_email=config["contact-email"],
|
||||
url=config["url"])
|
||||
license=section["license"],
|
||||
platforms=config.get("setup", "platforms", "list"),
|
||||
author=section["contact-name"],
|
||||
author_email=section["contact-email"],
|
||||
url=section["url"])
|
||||
|
||||
|
||||
class insert_resources_path(install):
|
||||
|
@ -104,13 +107,17 @@ class insert_resources_path(install):
|
|||
self.edit_game_object_file()
|
||||
|
||||
def edit_game_object_file(self):
|
||||
config = Configuration().get_section("setup")
|
||||
config = Configuration()
|
||||
section = "setup"
|
||||
for path in self.get_outputs():
|
||||
if path.endswith(config["main-object"]):
|
||||
if path.endswith(config.get(section, "main-object", "path")):
|
||||
for line in FileInput(path, inplace=True):
|
||||
pattern = "^ *{0} *=.*".\
|
||||
format(config["resources-path-identifier"])
|
||||
format(config.get(section,
|
||||
"resources-path-identifier"))
|
||||
if match(pattern, line):
|
||||
line = sub("=.*$", "= \"{0}\"".\
|
||||
format(config["installation-path"]), line)
|
||||
format(config.get(section,
|
||||
"installation-path",
|
||||
"path"), line)
|
||||
print line.strip("\n")
|
||||
|
|
|
@ -9,6 +9,7 @@ class Configuration(RawConfigParser):
|
|||
|
||||
default_project_file_rel_path = "config"
|
||||
default_resources_paths = [".", "resources"]
|
||||
list_member_sep = ','
|
||||
|
||||
def __init__(self, project_file_rel_path=None, resources_path=None,
|
||||
type_declarations=None):
|
||||
|
@ -26,36 +27,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 translate_path(self, path):
|
||||
new = ""
|
||||
if path and path[0] == sep:
|
||||
new += sep
|
||||
return expanduser("{0}{1}".format(new, join(*path.split(sep))))
|
||||
# def set(self, section, option, value):
|
||||
# value = self.cast_value(section, option, value)
|
||||
# RawConfigParser.set(self, section, option, value)
|
||||
|
||||
def set_defaults(self):
|
||||
add_section = self.add_section
|
||||
|
@ -98,6 +72,7 @@ class Configuration(RawConfigParser):
|
|||
set_option(section, "right", "K_RIGHT, K_d")
|
||||
set_option(section, "down", "K_DOWN, K_s")
|
||||
set_option(section, "left", "K_LEFT, K_a")
|
||||
set_option(section, "confirm", "K_RETURN")
|
||||
set_option(section, "capture-screen", "K_F9")
|
||||
section = "event"
|
||||
add_section(section)
|
||||
|
@ -148,24 +123,51 @@ class Configuration(RawConfigParser):
|
|||
|
||||
def set_installation_path(self):
|
||||
self.set("setup", "installation-path",
|
||||
join(self.get("setup", "installation-dir"),
|
||||
self.get("setup", "package-root")))
|
||||
join(self.get("setup", "installation-dir", "path"),
|
||||
self.get("setup", "package-root", "path")))
|
||||
|
||||
def set_resources_search_path(self):
|
||||
section, option = "setup", "resources-search-path"
|
||||
search_path = self.get(section, option)
|
||||
search_path = self.get(section, option, "list")
|
||||
if self.resources_path:
|
||||
search_path.append(self.resources_path)
|
||||
else:
|
||||
search_path.append(self.get("setup", "installation-path"))
|
||||
search_path.append(self.get("setup", "installation-path", "path"))
|
||||
self.set(section, option, search_path)
|
||||
|
||||
def get(self, section, option):
|
||||
def get(self, section, option, cast=None):
|
||||
value = RawConfigParser.get(self, section, option)
|
||||
if value is None:
|
||||
value = self.get_substitute(section, option)
|
||||
if cast:
|
||||
value = self.cast_value(value, cast)
|
||||
return value
|
||||
|
||||
def cast_value(self, value, cast):
|
||||
list_sep = self.list_member_sep
|
||||
if cast == "bool":
|
||||
return True if value == "yes" else False
|
||||
elif cast == "int":
|
||||
return int(value)
|
||||
elif cast == "float":
|
||||
return float(value)
|
||||
elif cast == "path":
|
||||
return self.translate_path(value)
|
||||
elif cast == "list":
|
||||
if value == "":
|
||||
return []
|
||||
else:
|
||||
return map(str.strip, value.split(list_sep))
|
||||
elif cast == "int-list":
|
||||
return map(int, value.split(list_sep))
|
||||
return value
|
||||
|
||||
def translate_path(self, path):
|
||||
new = ""
|
||||
if path and path[0] == sep:
|
||||
new += sep
|
||||
return expanduser("{0}{1}".format(new, join(*path.split(sep))))
|
||||
|
||||
def get_substitute(self, section, option):
|
||||
if section == "display":
|
||||
if option == "caption":
|
||||
|
@ -175,34 +177,36 @@ class Configuration(RawConfigParser):
|
|||
section, option = "screen-captures", "path"
|
||||
if not self.has_option(section, option):
|
||||
self.set(section, option, join(self.build_home_path(),
|
||||
self.get(section, "rel-path")))
|
||||
self.get(section, "rel-path",
|
||||
"path")))
|
||||
|
||||
def build_home_path(self):
|
||||
return join("~", "." + self.get("setup", "package-root"))
|
||||
return join("~", "." + self.get("setup", "package-root", "path"))
|
||||
|
||||
def set_data_exclusion_list(self):
|
||||
section, option = "setup", "data-exclude"
|
||||
exclude = []
|
||||
if self.has_option(section, option):
|
||||
exclude = self.get(section, option)
|
||||
exclude = self.get(section, option, "list")
|
||||
exclude += [".git", ".gitignore", "README", "build/", "dist/",
|
||||
"setup.py", "MANIFEST", self.get("setup", "package-root"),
|
||||
self.get("setup", "changelog")]
|
||||
"setup.py", "MANIFEST",
|
||||
self.get("setup", "package-root", "path"),
|
||||
self.get("setup", "changelog", "path")]
|
||||
self.set(section, option, exclude)
|
||||
|
||||
def set_requirements(self):
|
||||
section, option = "setup", "requirements"
|
||||
requirements = []
|
||||
if self.has_option(section, option):
|
||||
requirements = self.get(section, option)
|
||||
requirements = self.get(section, option, "list")
|
||||
if "pygame" not in requirements:
|
||||
requirements.append("pygame")
|
||||
self.set(section, option, requirements)
|
||||
|
||||
def get_section(self, section):
|
||||
def get_section(self, section, cast=None):
|
||||
assignments = {}
|
||||
for option in self.options(section):
|
||||
assignments[option] = self.get(section, option)
|
||||
assignments[option] = self.get(section, option, cast)
|
||||
return assignments
|
||||
|
||||
def __repr__(self):
|
||||
|
@ -214,8 +218,6 @@ class Configuration(RawConfigParser):
|
|||
|
||||
class TypeDeclarations(dict):
|
||||
|
||||
list_member_sep = ','
|
||||
|
||||
def __init__(self):
|
||||
dict.__init__(self, {"bool": [], "int": [], "float": [], "path": [],
|
||||
"list": [], "int-list": []})
|
||||
|
|
Loading…
Reference in New Issue