X-Git-Url: https://mudpy.org/gitweb?p=mudpy.git;a=blobdiff_plain;f=lib%2Fmudpy%2Fdata.py;h=9ee09e15929cc5c3612f81e54ab4f6c0bfe6d214;hp=de7641685189541ba0764f16c9ae4335b597b822;hb=7549f8f2b17705dcd20e9ae827f2f4d067d8d9a6;hpb=67faabf76655719a9e4d6a15560afb590dd21f8b diff --git a/lib/mudpy/data.py b/lib/mudpy/data.py index de76416..9ee09e1 100644 --- a/lib/mudpy/data.py +++ b/lib/mudpy/data.py @@ -1,10 +1,20 @@ # -*- coding: utf-8 -*- """Data interface functions for the mudpy engine.""" -# Copyright (c) 2004-2013 Jeremy Stanley . Permission +# Copyright (c) 2004-2014 Jeremy Stanley . Permission # to use, copy, modify, and distribute this software is granted under # terms provided in the LICENSE file distributed with this software. +import codecs +import configparser +import os +import re +import stat +import sys + +import mudpy +import yaml + class DataFile: @@ -13,20 +23,106 @@ class DataFile: def __init__(self, filename, universe): self.filename = filename self.universe = universe + self.data = {} self.load() def load(self): """Read a file and create elements accordingly.""" - import mudpy.misc - import os - import os.path - # TODO: remove this check after the switch to py3k + # TODO(fungi): remove this indirection after the YAML transition + if self.filename.endswith('.yaml'): + self.load_yaml() + else: + self.load_mpy() + + def load_yaml(self): + """Read a file and create elements accordingly.""" + # TODO(fungi): remove this parameter after the YAML transition + self._format = 'yaml' + self.modified = False try: - import configparser - except ImportError: - import ConfigParser as configparser - self.data = configparser.RawConfigParser() + self.data = yaml.load(open(self.filename)) + except FileNotFoundError: + # it's normal if the file is one which doesn't exist yet + try: + mudpy.misc.log("Couldn't read %s file." % self.filename, 6) + except NameError: + # happens when we're not far enough along in the init process + pass + if not hasattr(self.universe, "files"): + self.universe.files = {} + self.universe.files[self.filename] = self + includes = [] + if "__control__" in self.data: + if "include_files" in self.data["__control__"]: + for included in makelist( + self.data["__control__"]["include_files"]): + included = find_file( + included, + relative=self.filename, + universe=self.universe) + if included not in includes: + includes.append(included) + if "include_dirs" in self.data["__control__"]: + for included in [ + os.path.join(x, "__init__.mpy") for x in makelist( + self.data["__control__"]["include_dirs"] + ) + ]: + included = find_file( + included, + relative=self.filename, + universe=self.universe + ) + if included not in includes: + includes.append(included) + if "default_files" in self.data["__control__"]: + origins = makedict( + self.data["__control__"]["default_files"] + ) + for key in origins.keys(): + origins[key] = find_file( + origins[key], + relative=self.filename, + universe=self.universe + ) + if origins[key] not in includes: + includes.append(origins[key]) + self.universe.default_origins[key] = origins[key] + if key not in self.universe.categories: + self.universe.categories[key] = {} + if "private_files" in self.data["__control__"]: + for item in makelist( + self.data["__control__"]["private_files"] + ): + item = find_file( + item, + relative=self.filename, + universe=self.universe + ) + if item not in includes: + includes.append(item) + if item not in self.universe.private_files: + self.universe.private_files.append(item) + for element in self.data: + if element != "__control__": + mudpy.misc.Element(element, self.universe, self.filename) + for include_file in includes: + if not os.path.isabs(include_file): + include_file = find_file( + include_file, + relative=self.filename, + universe=self.universe + ) + if (include_file not in self.universe.files or not + self.universe.files[include_file].is_writeable()): + DataFile(include_file, self.universe) + + # TODO(fungi): remove this method after the YAML transition + def load_mpy(self): + """Read a file and create elements accordingly.""" + self._format = 'mpy' self.modified = False + self.data = configparser.RawConfigParser() if os.access(self.filename, os.R_OK): self.data.read(self.filename) if not hasattr(self.universe, "files"): @@ -99,13 +195,9 @@ class DataFile: self.universe.files[include_file].is_writeable()): DataFile(include_file, self.universe) + # TODO(fungi): this should support writing YAML def save(self): """Write the data, if necessary.""" - import codecs - import os - import os.path - import re - import stat # when modified, writeable and has content or the file exists if self.modified and self.is_writeable() and ( @@ -181,11 +273,18 @@ class DataFile: def is_writeable(self): """Returns True if the __control__ read_only is False.""" - return not self.data.has_option( - "__control__", "read_only" - ) or not self.data.getboolean( - "__control__", "read_only" - ) + # TODO(fungi): remove this indirection after the YAML transition + if self._format == "yaml": + try: + return not self.data["__control__"].get("read_only", False) + except KeyError: + return True + else: + return not self.data.has_option( + "__control__", "read_only" + ) or not self.data.getboolean( + "__control__", "read_only" + ) def find_file( @@ -197,9 +296,6 @@ def find_file( universe=None ): """Return an absolute file path based on configuration.""" - import os - import os.path - import sys # make sure to get rid of any surrounding quotes first thing if file_name: