# HG changeset patch # User Paul Boddie # Date 1533488873 -7200 # Node ID 7cb2b343029abfc5d73b04433cc527ce319707b8 # Parent 390bf818f5f1c7c8e426fbd77e0f6bf7bb320b74 Employ a separate directory instance within directory contexts. diff -r 390bf818f5f1 -r 7cb2b343029a moinformat/input/directory.py --- a/moinformat/input/directory.py Sun Aug 05 19:07:27 2018 +0200 +++ b/moinformat/input/directory.py Sun Aug 05 19:07:53 2018 +0200 @@ -23,7 +23,7 @@ from moinformat.utils.directory import Directory from os.path import sep -class DirectoryInput(Input, Directory): +class DirectoryInput(Input): "A directory output context." @@ -37,7 +37,7 @@ raise ValueError, parameters Input.__init__(self, parameters) - Directory.__init__(self, parameters["filename"]) + self.dir = Directory(parameters["filename"]) # Support an encoding of the level separator for the filesystem. # Where it is the same as the directory separator, documents are stored @@ -49,7 +49,7 @@ "Return all pages in the context." - return map(self.to_pagename, self.select_files("*")) + return map(self.to_pagename, self.dir.select_files("*")) # Page characteristics. @@ -58,7 +58,7 @@ "Return the subpage filenames of 'pagename'." pattern = self.to_filename("%s/*" % pagename) - return self.select_files(pattern) + return self.dir.select_files(pattern) def subpages(self, pagename): @@ -75,7 +75,7 @@ 'encoding'. """ - return self.readpath(self.get_filename(filename), encoding) + return self.readpath(self.dir.get_filename(filename), encoding) # NOTE: Translation methods should encode filenames appropriately. diff -r 390bf818f5f1 -r 7cb2b343029a moinformat/output/directory.py --- a/moinformat/output/directory.py Sun Aug 05 19:07:27 2018 +0200 +++ b/moinformat/output/directory.py Sun Aug 05 19:07:53 2018 +0200 @@ -21,10 +21,9 @@ from moinformat.output.common import Output from moinformat.utils.directory import Directory -from os import makedirs, rename -from os.path import exists, isdir, isfile, join, split +from os.path import join, split -class DirectoryOutput(Output, Directory): +class DirectoryOutput(Output): "A directory output context." @@ -38,12 +37,20 @@ raise ValueError, parameters Output.__init__(self, parameters) - Directory.__init__(self, parameters["filename"]) + self.dir = Directory(parameters["filename"]) + self.dir.ensure() + self.index_name = parameters and parameters.get("index_name") or "index" + + # Convenience methods. - if not exists(self.filename): - makedirs(self.filename) + def get_filename(self, filename): - self.index_name = parameters and parameters.get("index_name") or "index" + """ + Return the full path of a file with the given 'filename' found within + the directory. The full path is an absolute path. + """ + + return self.dir.get_filename(filename) # Name translation methods. @@ -76,7 +83,7 @@ optional 'encoding' is specified, override the general encoding. """ - return self.writepath(text, self.get_filename(filename), encoding) + return self.writepath(text, self.dir.get_filename(filename), encoding) def writepage(self, text, pagename, encoding=None): @@ -86,38 +93,35 @@ the page is a parent of other pages. """ + dir = self.dir + filename = self.to_filename(pagename) parent = split(filename)[0] # The page may have a parent. if parent and parent != filename: - dirpath = self.get_filename(parent) # Relocate any file for the parent to an index file within a page # directory. - if isfile(dirpath): - parent_tmp = "%s.tmp" % dirpath - rename(dirpath, parent_tmp) - makedirs(dirpath) - rename(parent_tmp, join(dirpath, self.index_name)) + if dir.isfile(parent): + parent_tmp = "%s.tmp" % parent + dir.rename(parent, parent_tmp) + dir.makedirs(parent) + dir.rename(parent_tmp, join(parent, self.index_name)) # Or make a directory for the parent. - elif not exists(dirpath): - makedirs(dirpath) - - # Obtain a complete filename for the content. - - pathname = self.get_filename(filename) + elif not dir.exists(parent): + dir.makedirs(parent) # Write to an index filename within any existing directory. - if isdir(pathname): - pathname = join(pathname, self.index_name) + if dir.isdir(filename): + filename = join(filename, self.index_name) - self.writepath(text, pathname, encoding) + self.writefile(text, filename, encoding) output = DirectoryOutput