-class DataFile:
- u"""A file containing universe elements."""
- def __init__(self, filename, universe):
- self.filename = filename
- self.universe = universe
- self.load()
- def load(self):
- u"""Read a file and create elements accordingly."""
- import ConfigParser, os, os.path
- self.data = ConfigParser.RawConfigParser()
- self.modified = False
- if os.access(self.filename, os.R_OK): self.data.read(self.filename)
- if not hasattr(self.universe, u"files"): self.universe.files = {}
- self.universe.files[self.filename] = self
- includes = []
- if self.data.has_option(u"__control__", u"include_files"):
- for included in makelist(
- self.data.get(u"__control__", u"include_files")
- ):
- included = find_file(
- included,
- relative=self.filename,
- universe=self.universe
- )
- if included not in includes: includes.append(included)
- if self.data.has_option(u"__control__", u"include_dirs"):
- for included in [ os.path.join(x, u"__init__.mpy") for x in makelist(
- self.data.get(u"__control__", u"include_dirs")
- ) ]:
- included = find_file(
- included,
- relative=self.filename,
- universe=self.universe
- )
- if included not in includes: includes.append(included)
- if self.data.has_option(u"__control__", u"default_files"):
- origins = makedict(self.data.get(u"__control__", u"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 self.data.has_option(u"__control__", u"private_files"):
- for item in makelist(self.data.get(u"__control__", u"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 section in self.data.sections():
- if section != u"__control__":
- Element(section, 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)
- def save(self):
- u"""Write the data, if necessary."""
- import codecs, os, os.path, re, stat
-
- # when modified, writeable and has content or the file exists
- if self.modified and self.is_writeable() and (
- self.data.sections() or os.path.exists(self.filename)
- ):
-
- # make parent directories if necessary
- if not os.path.exists(os.path.dirname(self.filename)):
- os.makedirs(os.path.dirname(self.filename))
-
- # backup the file
- if self.data.has_option(u"__control__", u"backup_count"):
- max_count = self.data.has_option(u"__control__", u"backup_count")
- else:
- max_count = universe.categories[u"internal"][u"limits"].getint(
- u"default_backup_count"
- )
- if os.path.exists(self.filename) and max_count:
- backups = []
- for candidate in os.listdir(os.path.dirname(self.filename)):
- if re.match(
- os.path.basename(self.filename) + u"""\.\d+$""", candidate
- ):
- backups.append(int(candidate.split(u".")[-1]))
- backups.sort()
- backups.reverse()
- for old_backup in backups:
- if old_backup >= max_count-1:
- os.remove(self.filename+u"."+unicode(old_backup))
- elif not os.path.exists(
- self.filename+u"."+unicode(old_backup+1)
- ):
- os.rename(
- self.filename + u"."+unicode(old_backup),
- self.filename + u"."+unicode( old_backup + 1 )
- )
- if not os.path.exists(self.filename+u".0"):
- os.rename( self.filename, self.filename + u".0" )
-
- # our data file
- file_descriptor = codecs.open(self.filename, u"w", u"utf-8")
-
- # if it's marked private, chmod it appropriately
- if self.filename in self.universe.private_files and oct(
- stat.S_IMODE( os.stat(self.filename)[stat.ST_MODE] )
- ) != 0600:
- os.chmod(self.filename, 0600)
-
- # write it back sorted, instead of using ConfigParser
- sections = self.data.sections()
- sections.sort()
- for section in sections:
- file_descriptor.write(u"[" + section + u"]\n")
- options = self.data.options(section)
- options.sort()
- for option in options:
- file_descriptor.write(
- option + u" = " + self.data.get(section, option) + u"\n"
- )
- file_descriptor.write(u"\n")
-
- # flush and close the file
- file_descriptor.flush()
- file_descriptor.close()
-
- # unset the modified flag
- self.modified = False
- def is_writeable(self):
- u"""Returns True if the __control__ read_only is False."""
- return not self.data.has_option(
- u"__control__", u"read_only"
- ) or not self.data.getboolean(
- u"__control__", u"read_only"
- )
-