Skip to content

Commit

Permalink
py/pack: root data config reading/writing implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJJ committed Mar 31, 2015
1 parent 5231c8e commit 65e229b
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 0 deletions.
1 change: 1 addition & 0 deletions py/openage/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ add_py_package(openage)
add_test_py(openage.assets.test "tests python asset locator")

add_subdirectory("convert")
add_subdirectory("pack")
add_subdirectory("testing")
4 changes: 4 additions & 0 deletions py/openage/pack/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_py_package(openage.pack)

add_test_py(openage.pack.tests.packcfg_read "tests reading mod pack root config files")
add_test_py(openage.pack.tests.packcfg_write "tests reading mod pack root config files")
Empty file added py/openage/pack/__init__.py
Empty file.
113 changes: 113 additions & 0 deletions py/openage/pack/pack_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Copyright 2015-2015 the openage authors. See copying.md for legal info.

import configparser
import re


class PackConfig:
"""
main config file for a mod pack.
stores the package metainformation for the mod list.
file contents:
[openage-pack]
name = <package name>
version = <package version>
author = <pack author>
pack_type = (game|mod|...)
config_type = (cfg|nyan|...)
config = <filename.cfg>
"""

# everything has to be in this section:
root_cfg = "openage-pack"

# required attributes
pack_attrs = ("name", "version", "author",
"pack_type", "config_type", "config")

def __init__(self):
"""
init all required attributes to None.
"""

self.cfg = dict()

for attr in self.pack_attrs:
self.cfg[attr] = None

def check_sanity(self):
"""
checks whether the current config is valid.
"""

if None in self.cfg.values():
nones = [v for k, v in self.cfg.items() if v is None]
raise Exception("unset attribute(s): %s" % nones)

if self.cfg["pack_type"] not in ("game", "mod"):
raise Exception("unsupported data pack type")

if self.cfg["config_type"] not in ("cfg",):
raise Exception("config_type not supported: %s" %
self.cfg["config_type"])

if not re.match(r"[a-zA-Z][a-zA-Z0-9_]*", self.cfg["name"]):
raise Exception("disallowed chars in pack name found")

if not re.match(r"v[0-9]+(\.[0-9]+)*(-r[0-9]+)", self.cfg["version"]):
raise Exception("invalid pack version")

def read(self, handle):
"""
fill this pack config by the contents provided by the file handle.
"""

cfp = configparser.ConfigParser()

# read the config from the (pseudo-?)handle
cfp.read_file(handle, source=handle.name)

if self.root_cfg not in cfp:
raise Exception("pack root config doesn't contain '%s' section."
% self.root_cfg)
else:
self.cfg = cfp[self.root_cfg]

self.check_sanity()

def write(self, handle):
"""
store the settings of this configfile to the given file handle.
"""

self.check_sanity()

cfp = configparser.ConfigParser()
cfp[self.root_cfg] = self.cfg

cfp.write(handle)

def set_attrs(self, **dct):
"""
set all attributes from the dict to this pack config.
"""

# just take the known attributes
to_set = {k: v for k, v in dct.items() if k in self.pack_attrs}

# check if all keys were used
unknown_keys = dct.keys() - to_set.keys()
if unknown_keys:
raise Exception("unknown attr name(s) to set: %s" % unknown_keys)

self.cfg.update(to_set)

def get_attrs(self):
"""
return this config's settings.
"""

return self.cfg
74 changes: 74 additions & 0 deletions py/openage/pack/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Copyright 2015-2015 the openage authors. See copying.md for legal info.

from .pack_config import PackConfig

from openage.util import VirtualFile


def packcfg_read():
"""
test reading a virtual root config file
"""

pcfg = PackConfig()

f = VirtualFile(name="virtual_input_test.cfg", binary=False)
content = (
"[" + PackConfig.root_cfg + "]\n"
"name = {name}\n"
"version = {version}\n"
"author = {author}\n"
"pack_type = {pack_type}\n"
"config_type = {config_type}\n"
"config = {config}\n"
)

test = {
"name": "Valid_Name_1337",
"version": "v42-r8",
"pack_type": "mod",
"author": "test bot 9001",
"config_type": "cfg",
"config": "nonexistant.cfg",
}

f.set_data(content.format(**test))
pcfg.read(f)

if not pcfg.get_attrs() == test:
raise Exception("parsed config does not equal the input")


def packcfg_write():
"""
test writing the root config to a virtual file
"""

pcfg = PackConfig()
test = {
"name": "roflPack2000",
"version": "v1337-r42",
"pack_type": "game",
"author": "i did nothing wrong",
"config_type": "cfg",
"config": "roflfile.cfg",
}
pcfg.set_attrs(**test)

o = VirtualFile(name="virtual_output_test.cfg", binary=False)
pcfg.write(o)

lines = o.data().split("\n")

result = dict()
# ignore first and last line
for line in lines[1:-2]:
k, v = tuple(line.split(" = "))
result[k] = v

if test.keys() != result.keys():
raise Exception("unexpected key set found.")

for k, v in result.items():
if test[k] != v:
raise Exception("%s didn't have expected value" % k)

0 comments on commit 65e229b

Please sign in to comment.