# HG changeset patch # User Paul Boddie # Date 1533485754 -7200 # Node ID e8f4edb1d59935113cc623bd33a9778c7b095dee # Parent 6bc36ce901802510a0dd406ab53ae7bca13e0aee Expanded input contexts to support query operations on pagenames, with operations being reorganised to distinguish between accesses to pages and files within contexts and to files in general. diff -r 6bc36ce90180 -r e8f4edb1d599 moinformat/input/common.py --- a/moinformat/input/common.py Sun Aug 05 18:13:36 2018 +0200 +++ b/moinformat/input/common.py Sun Aug 05 18:15:54 2018 +0200 @@ -19,6 +19,7 @@ this program. If not, see . """ +from os.path import split import codecs class Input: @@ -34,9 +35,51 @@ self.parameters = parameters self.encoding = parameters and parameters.get("encoding") or self.default_encoding + def all(self): + + "Return all pages in the context." + + return [] + + # Page characteristics. + + def parent(self, pagename): + + "Return the parent of 'pagename'." + + return pagename.rsplit("/", 1)[0] + + def subpages(self, pagename): + + "Return the subpages of 'pagename'." + + return [] + + # Page access methods. + def readfile(self, filename, encoding=None): """ + Return the contents of the file having the given 'filename' and optional + 'encoding'. This implementation treats 'filename' as a path. + """ + + return self.readpath(filename, encoding) + + def readpage(self, pagename, encoding=None): + + """ + Return the contents of the file having the given 'pagename' and optional + 'encoding'. + """ + + return self.readfile(self.to_filename(pagename), encoding) + + # Input methods. + + def readpath(self, filename, encoding=None): + + """ Return the contents of the file having the given 'filename'. If the optional 'encoding' is specified, override the general encoding. """ @@ -47,4 +90,20 @@ finally: f.close() + # Name translation methods. + + def to_filename(self, pagename): + + "Return the filename corresponding to 'pagename'." + + return pagename + + def to_pagename(self, filename): + + "Return the pagename corresponding to 'filename'." + + # Take the leafname as the pagename from an arbitrary filename. + + return split(filename)[-1] + # vim: tabstop=4 expandtab shiftwidth=4 diff -r 6bc36ce90180 -r e8f4edb1d599 moinformat/input/directory.py --- a/moinformat/input/directory.py Sun Aug 05 18:13:36 2018 +0200 +++ b/moinformat/input/directory.py Sun Aug 05 18:15:54 2018 +0200 @@ -21,6 +21,7 @@ from moinformat.input.common import Input from moinformat.utils.directory import Directory +from os.path import sep class DirectoryInput(Input, Directory): @@ -38,6 +39,35 @@ Input.__init__(self, parameters) Directory.__init__(self, parameters["filename"]) + # Support an encoding of the level separator for the filesystem. + # Where it is the same as the directory separator, documents are stored + # using nested directories, not as a flat list. + + self.level_sep = parameters and parameters.get("separator") or sep + + def all(self): + + "Return all pages in the context." + + return map(self.to_pagename, self.select_files("*")) + + # Page characteristics. + + def subpage_filenames(self, pagename): + + "Return the subpage filenames of 'pagename'." + + pattern = self.to_filename("%s/*" % pagename) + return self.select_files(pattern) + + def subpages(self, pagename): + + "Return the subpages of 'pagename'." + + return map(self.to_pagename, self.subpage_filenames(pagename)) + + # Page access methods. + def readfile(self, filename, encoding=None): """ @@ -45,7 +75,27 @@ 'encoding'. """ - return Input.readfile(self, self.get_filename(filename), encoding) + return self.readpath(self.get_filename(filename), encoding) + + # NOTE: Translation methods should encode filenames appropriately. + + def to_filename(self, pagename): + + "Return the filename corresponding to 'pagename'." + + if sep == self.level_sep: + return pagename + else: + return self.level_sep.join(pagename.split("/")) + + def to_pagename(self, filename): + + "Return the pagename corresponding to 'filename'." + + if sep == self.level_sep: + return filename + else: + return "/".join(filename.split(self.level_sep)) input = DirectoryInput diff -r 6bc36ce90180 -r e8f4edb1d599 moinformat/input/standalone.py --- a/moinformat/input/standalone.py Sun Aug 05 18:13:36 2018 +0200 +++ b/moinformat/input/standalone.py Sun Aug 05 18:15:54 2018 +0200 @@ -40,15 +40,6 @@ f = codecs.getreader(self.encoding)(stream) return f.read() - def readfile(self, filename, encoding=None): - - """ - Return the contents of the file having the given 'filename'. If the - optional 'encoding' is specified, override the general encoding. - """ - - return Input.readfile(self, filename, encoding) - input = StandaloneInput # vim: tabstop=4 expandtab shiftwidth=4