Configuration defines resources search path, screen caps path

This commit is contained in:
Frank DeMarco 2012-07-07 15:26:29 +09:00
parent dcdc16f21e
commit 8170a28817
4 changed files with 73 additions and 34 deletions

View File

@ -15,9 +15,10 @@ from ScreenGrabber import *
class Game(GameChild, Animation):
def __init__(self, installed_resources_path=".", config_rel_path=None):
resources_path = None
def __init__(self, config_rel_path=None):
self.init_gamechild()
self.installed_resources_path = installed_resources_path
self.config_rel_path = config_rel_path
self.set_configuration()
self.init_animation()
@ -33,8 +34,8 @@ class Game(GameChild, Animation):
GameChild.__init__(self)
def set_configuration(self):
self.configuration = Configuration(self.installed_resources_path,
self.config_rel_path)
self.configuration = Configuration(self.config_rel_path,
self.resources_path)
def init_animation(self):
Animation.__init__(self,

View File

@ -25,7 +25,8 @@ class Setup:
@classmethod
def build_data_map(self):
install_root = self.config["resources-install-path"]
include = []
install_root = self.config.get("setup", "resources-install-path")
include = [(install_root, ["config", "Basic.ttf", "hi-scores"])]
exclude = map(realpath,
[".git", "esp_hadouken", "vid", "aud/uncompressed", "aud/mod",

View File

@ -7,18 +7,18 @@ from ConfigParser import RawConfigParser
class Configuration(RawConfigParser):
default_rel_path = "config"
default_project_file_rel_path = "config"
default_resources_paths = [".", "resources"]
defaults_file_path = "defaults"
def __init__(self, installed_resources_path=None, rel_path=None,
type_declarations=None, local=False):
def __init__(self, project_file_rel_path=None, resources_path=None,
type_declarations=None):
RawConfigParser.__init__(self)
self.local = local
self.project_file_rel_path = project_file_rel_path
self.resources_path = resources_path
self.set_type_declarations(type_declarations)
self.installed_resources_path = installed_resources_path
self.rel_path = rel_path
self.read_defaults()
self.read_project_file()
self.read_project_config_file()
self.print_debug_statement(self)
def set_type_declarations(self, type_declarations):
@ -41,13 +41,22 @@ class Configuration(RawConfigParser):
elif pair in types["float"]:
return float(value)
elif pair in types["path"]:
return join(*value.split(sep))
return self.translate_path(value)
elif pair in types["list"]:
return map(str.strip, value.split(types.list_member_sep))
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[0] == sep:
new += sep
return new + join(*path.split(sep))
def read_defaults(self):
self.read(join(dirname(__file__), self.defaults_file_path))
self.set("setup", "package-root", basename(getcwd()))
@ -59,11 +68,28 @@ class Configuration(RawConfigParser):
self.set(section, option, value)
return files_read
def read_project_file(self):
path = self.locate_project_file()
def read_project_config_file(self):
path = self.locate_project_config_file()
if path:
self.read(path)
self.print_debug_statement("No configuration file found")
else:
self.print_debug_statement("No configuration file found")
self.set_resources_search_path()
self.set_screen_captures_path()
def locate_project_config_file(self):
rel_path = self.project_file_rel_path
if not rel_path:
rel_path = self.default_project_file_rel_path
if exists(rel_path) and not self.is_shared_mode():
return rel_path
if self.resources_path:
installed_path = join(self.resources_path, rel_path)
if exists(installed_path):
return installed_path
def is_shared_mode(self):
return "-s" in argv
def print_debug_statement(self, statement):
if self.is_debug_mode():
@ -72,17 +98,15 @@ class Configuration(RawConfigParser):
def is_debug_mode(self):
return "-d" in argv
def locate_project_file(self):
rel_path = self.rel_path if self.rel_path else self.default_rel_path
if not self.is_local_mode():
installed_path = join(self.installed_resources_path, rel_path)
if exists(installed_path):
return installed_path
if exists(rel_path):
return rel_path
def is_local_mode(self):
return "-l" in argv or self.local
def set_resources_search_path(self):
section, option = "setup", "resources-search-path"
search_path = self.get(section, option)
if self.resources_path:
search_path.append(self.resources_path)
else:
search_path.append(join(self.get("setup", "installation-dir"),
self.get("setup", "package-root")))
self.set(section, option, search_path)
def get(self, section, option):
value = RawConfigParser.get(self, section, option)
@ -95,6 +119,15 @@ class Configuration(RawConfigParser):
if option == "caption":
return self.get("setup", "title")
def set_screen_captures_path(self):
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")))
def build_home_path(self):
return join("~", "." + self.get("setup", "package-root"))
def get_section(self, section):
assignments = {}
for option in self.options(section):
@ -119,9 +152,11 @@ class TypeDeclarations(dict):
self.add("int", "display", "wait-duration")
self.add("bool", "display", "centered")
self.add("int-list", "display", "dimensions")
self.add("path", "resources", "installation-path")
self.add("path", "screen-captures", "path")
self.add("list", "setup", "classifiers")
self.add("list", "setup", "resources-search-path")
self.add("path", "setup", "installation-dir")
self.add("path", "setup", "package-root")
self.add("list", "keys", "up")
self.add("list", "keys", "right")
self.add("list", "keys", "down")

View File

@ -1,7 +1,11 @@
# Commented options are defined by the Configuration class
[setup]
title =
# package-root = (This default is added in the code)
classifiers =
resources-search-path = ./, resources/
installation-dir = /usr/local/share/games/
# package-root =
[display]
dimensions = 480, 320
@ -10,11 +14,9 @@ wait-duration = 2
caption =
centered = yes
[resources]
installation-path = .
[screen-captures]
path = caps
# path =
rel-path = caps
[keys]
up = K_UP, K_w