Compare commits

...

1 Commits
main ... casts

Author SHA1 Message Date
Frank DeMarco c54ebd914d Configuration options stored as text, cast on retrieval 2012-07-16 17:23:51 +09:00
8 changed files with 91 additions and 82 deletions

View File

@ -33,7 +33,8 @@ class Animation:
last_ticks = self.last_ticks last_ticks = self.last_ticks
actual_frame_duration = self.get_ticks() - last_ticks actual_frame_duration = self.get_ticks() - last_ticks
last_ticks = self.get_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: while actual_frame_duration < self.target_frame_duration:
pygame.time.wait(wait_duration) pygame.time.wait(wait_duration)
actual_frame_duration += self.get_ticks() - last_ticks actual_frame_duration += self.get_ticks() - last_ticks

View File

@ -6,16 +6,17 @@ class Display(GameChild):
def __init__(self, game): def __init__(self, game):
GameChild.__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_screen()
self.set_caption() self.set_caption()
self.set_icon() self.set_icon()
def set_screen(self): 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): def set_caption(self):
display.set_caption(self.config["caption"]) display.set_caption(self.config.get("display", "caption"))
def set_icon(self): def set_icon(self):
if self.get_configuration().has_option("display", "icon-path"): if self.get_configuration().has_option("display", "icon-path"):

View File

@ -39,10 +39,11 @@ class Game(GameChild, Animation):
def init_animation(self): def init_animation(self):
Animation.__init__(self, Animation.__init__(self,
self.configuration.get("display", "frame-duration")) self.configuration.get("display", "frame-duration",
"int"))
def align_window(self): def align_window(self):
if self.configuration.get("display", "centered"): if self.configuration.get("display", "centered", "bool"):
environ["SDL_VIDEO_CENTERED"] = "1" environ["SDL_VIDEO_CENTERED"] = "1"
def set_children(self): def set_children(self):

View File

@ -34,8 +34,8 @@ class GameChild:
def get_resource(self, section, option): def get_resource(self, section, option):
config = self.get_configuration() config = self.get_configuration()
rel_path = config.get(section, option) rel_path = config.get(section, option, "path")
for root in config.get("setup", "resources-search-path"): for root in config.get("setup", "resources-search-path", "list"):
if self.is_shared_mode() and not self.is_absolute_path(root): if self.is_shared_mode() and not self.is_absolute_path(root):
continue continue
path = join(root, rel_path) path = join(root, rel_path)

View File

@ -25,17 +25,14 @@ class Input(GameChild):
def build_key_map(self): def build_key_map(self):
key_map = {} 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] = [] key_map[command] = []
if type(keys) == str:
keys = [keys]
for key in keys: for key in keys:
key_map[command].append(globals()[key]) key_map[command].append(globals()[key])
self.key_map = key_map self.key_map = key_map
def translate_key_press(self, evt): def translate_key_press(self, evt):
self.print_debug("You pressed {0}, suppressed => {1}".\
format(evt.key, self.suppressed))
if not self.suppressed: if not self.suppressed:
key = evt.key key = evt.key
for cmd, keys in self.key_map.iteritems(): for cmd, keys in self.key_map.iteritems():
@ -44,7 +41,6 @@ class Input(GameChild):
def post_command(self, cmd): def post_command(self, cmd):
eid = self.get_user_event_id() 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") name = self.get_configuration().get("event", "command-event-name")
event.post(event.Event(eid, name=name, command=cmd)) event.post(event.Event(eid, name=name, command=cmd))

View File

@ -16,7 +16,8 @@ class ScreenGrabber(GameChild):
def save_display(self, event): def save_display(self, event):
if self.is_command(event, "capture-screen"): 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: try:
if not exists(directory): if not exists(directory):
makedirs(directory) makedirs(directory)

View File

@ -20,7 +20,7 @@ class Setup:
def build_package_list(self): def build_package_list(self):
packages = [] packages = []
package_root = self.config.get("setup", "package-root") package_root = self.config.get("setup", "package-root", "path")
if exists(package_root): if exists(package_root):
for root, dirs, files in walk(package_root, followlinks=True): for root, dirs, files in walk(package_root, followlinks=True):
packages.append(root.replace(sep, ".")) packages.append(root.replace(sep, "."))
@ -28,15 +28,17 @@ class Setup:
def build_data_map(self): def build_data_map(self):
include = [] include = []
config = self.config.get_section("setup") config = self.config
exclude = map(normpath, config["data-exclude"]) exclude = map(normpath, config.get("setup", "data-exclude", "list"))
for root, dirs, files in walk("."): for root, dirs, files in walk("."):
dirs = self.remove_excluded(dirs, root, exclude) dirs = self.remove_excluded(dirs, root, exclude)
files = [join(root, f) for f in self.remove_excluded(files, root, files = [join(root, f) for f in self.remove_excluded(files, root,
exclude)] exclude)]
if files: if files:
include.append((normpath(join(config["installation-path"], include.append((normpath(join(config.get("setup",
root)), files)) "installation-path",
"path"), root)),
files))
return include return include
def remove_excluded(self, paths, root, exclude): def remove_excluded(self, paths, root, exclude):
@ -53,7 +55,7 @@ class Setup:
def build_description(self): def build_description(self):
description = "" description = ""
path = self.config.get("setup", "description-file") path = self.config.get("setup", "description-file", "path")
if exists(path): if exists(path):
description = "\n{0}\n{1}\n{2}".format(file(path).read(), description = "\n{0}\n{1}\n{2}".format(file(path).read(),
"Changelog\n=========", "Changelog\n=========",
@ -62,7 +64,7 @@ class Setup:
def translate_changelog(self): def translate_changelog(self):
translation = "" translation = ""
path = self.config.get("setup", "changelog") path = self.config.get("setup", "changelog", "path")
if exists(path): if exists(path):
lines = file(path).readlines() lines = file(path).readlines()
package_name = lines[0].split()[0] package_name = lines[0].split()[0]
@ -79,22 +81,23 @@ class Setup:
return translation return translation
def setup(self): def setup(self):
config = self.config.get_section("setup") config = self.config
section = config.get_section("setup")
setup(cmdclass={"install": insert_resources_path}, setup(cmdclass={"install": insert_resources_path},
name=self.translate_title(), name=self.translate_title(),
packages=self.build_package_list(), packages=self.build_package_list(),
scripts=[config["init-script"]], scripts=[config.get("setup", "init-script", "path")],
data_files=self.build_data_map(), data_files=self.build_data_map(),
requires=config["requirements"], requires=config.get("setup", "requirements", "list"),
version=config["version"], version=section["version"],
description=config["summary"], description=section["summary"],
classifiers=config["classifiers"], classifiers=config.get("setup", "classifiers", "list"),
long_description=self.build_description(), long_description=self.build_description(),
license=config["license"], license=section["license"],
platforms=config["platforms"], platforms=config.get("setup", "platforms", "list"),
author=config["contact-name"], author=section["contact-name"],
author_email=config["contact-email"], author_email=section["contact-email"],
url=config["url"]) url=section["url"])
class insert_resources_path(install): class insert_resources_path(install):
@ -104,13 +107,17 @@ class insert_resources_path(install):
self.edit_game_object_file() self.edit_game_object_file()
def edit_game_object_file(self): def edit_game_object_file(self):
config = Configuration().get_section("setup") config = Configuration()
section = "setup"
for path in self.get_outputs(): 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): for line in FileInput(path, inplace=True):
pattern = "^ *{0} *=.*".\ pattern = "^ *{0} *=.*".\
format(config["resources-path-identifier"]) format(config.get(section,
"resources-path-identifier"))
if match(pattern, line): if match(pattern, line):
line = sub("=.*$", "= \"{0}\"".\ line = sub("=.*$", "= \"{0}\"".\
format(config["installation-path"]), line) format(config.get(section,
"installation-path",
"path"), line)
print line.strip("\n") print line.strip("\n")

View File

@ -9,6 +9,7 @@ class Configuration(RawConfigParser):
default_project_file_rel_path = "config" default_project_file_rel_path = "config"
default_resources_paths = [".", "resources"] default_resources_paths = [".", "resources"]
list_member_sep = ','
def __init__(self, project_file_rel_path=None, resources_path=None, def __init__(self, project_file_rel_path=None, resources_path=None,
type_declarations=None): type_declarations=None):
@ -26,36 +27,9 @@ class Configuration(RawConfigParser):
type_declarations = TypeDeclarations() type_declarations = TypeDeclarations()
self.type_declarations = type_declarations self.type_declarations = type_declarations
def set(self, section, option, value): # def set(self, section, option, value):
value = self.cast_value(section, option, value) # value = self.cast_value(section, option, value)
RawConfigParser.set(self, 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_defaults(self): def set_defaults(self):
add_section = self.add_section add_section = self.add_section
@ -98,6 +72,7 @@ class Configuration(RawConfigParser):
set_option(section, "right", "K_RIGHT, K_d") set_option(section, "right", "K_RIGHT, K_d")
set_option(section, "down", "K_DOWN, K_s") set_option(section, "down", "K_DOWN, K_s")
set_option(section, "left", "K_LEFT, K_a") set_option(section, "left", "K_LEFT, K_a")
set_option(section, "confirm", "K_RETURN")
set_option(section, "capture-screen", "K_F9") set_option(section, "capture-screen", "K_F9")
section = "event" section = "event"
add_section(section) add_section(section)
@ -148,24 +123,51 @@ class Configuration(RawConfigParser):
def set_installation_path(self): def set_installation_path(self):
self.set("setup", "installation-path", self.set("setup", "installation-path",
join(self.get("setup", "installation-dir"), join(self.get("setup", "installation-dir", "path"),
self.get("setup", "package-root"))) self.get("setup", "package-root", "path")))
def set_resources_search_path(self): def set_resources_search_path(self):
section, option = "setup", "resources-search-path" section, option = "setup", "resources-search-path"
search_path = self.get(section, option) search_path = self.get(section, option, "list")
if self.resources_path: if self.resources_path:
search_path.append(self.resources_path) search_path.append(self.resources_path)
else: else:
search_path.append(self.get("setup", "installation-path")) search_path.append(self.get("setup", "installation-path", "path"))
self.set(section, option, search_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) value = RawConfigParser.get(self, section, option)
if value is None: if value is None:
value = self.get_substitute(section, option) value = self.get_substitute(section, option)
if cast:
value = self.cast_value(value, cast)
return value 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): def get_substitute(self, section, option):
if section == "display": if section == "display":
if option == "caption": if option == "caption":
@ -175,34 +177,36 @@ class Configuration(RawConfigParser):
section, option = "screen-captures", "path" section, option = "screen-captures", "path"
if not self.has_option(section, option): if not self.has_option(section, option):
self.set(section, option, join(self.build_home_path(), 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): 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): def set_data_exclusion_list(self):
section, option = "setup", "data-exclude" section, option = "setup", "data-exclude"
exclude = [] exclude = []
if self.has_option(section, option): if self.has_option(section, option):
exclude = self.get(section, option) exclude = self.get(section, option, "list")
exclude += [".git", ".gitignore", "README", "build/", "dist/", exclude += [".git", ".gitignore", "README", "build/", "dist/",
"setup.py", "MANIFEST", self.get("setup", "package-root"), "setup.py", "MANIFEST",
self.get("setup", "changelog")] self.get("setup", "package-root", "path"),
self.get("setup", "changelog", "path")]
self.set(section, option, exclude) self.set(section, option, exclude)
def set_requirements(self): def set_requirements(self):
section, option = "setup", "requirements" section, option = "setup", "requirements"
requirements = [] requirements = []
if self.has_option(section, option): if self.has_option(section, option):
requirements = self.get(section, option) requirements = self.get(section, option, "list")
if "pygame" not in requirements: if "pygame" not in requirements:
requirements.append("pygame") requirements.append("pygame")
self.set(section, option, requirements) self.set(section, option, requirements)
def get_section(self, section): def get_section(self, section, cast=None):
assignments = {} assignments = {}
for option in self.options(section): for option in self.options(section):
assignments[option] = self.get(section, option) assignments[option] = self.get(section, option, cast)
return assignments return assignments
def __repr__(self): def __repr__(self):
@ -214,8 +218,6 @@ class Configuration(RawConfigParser):
class TypeDeclarations(dict): class TypeDeclarations(dict):
list_member_sep = ','
def __init__(self): def __init__(self):
dict.__init__(self, {"bool": [], "int": [], "float": [], "path": [], dict.__init__(self, {"bool": [], "int": [], "float": [], "path": [],
"list": [], "int-list": []}) "list": [], "int-list": []})