The WebContentManager class
The WebContentManager class contains useful tools that other content managers can call upon.
WebContentManager(pathname)
Base Class for Web Content Managers. WebContentManager objects automatically load a CMS object in the project folder and creates a report header, but takes no other action
InputFiles
Returns a list of files to be processed. It looks in the directory specified by CMS.inpath and includes all files in the inpath and all subdirectories.
FilterInputFiles([ext='.xml’]))
Returns a list of files in the Inpath with a specific extention. If you do not specify a period it will be added. The default extension is '.xml’ and this method only accepts one extension. I plan on adding the ability to filter with a list of extentions, but now the user will have to call this method several times and join the lists to do this.
OutputFiles
Returns a list of files in the CMS object’s Outpath including all files in all subdirectories.
FilterOutputFiles([ext='.html’])
Returns a list of files in the Outpath with a specific extention. If you do not specify a period it will be added. The default extension is '.html’
ListNewFiles([inext='.xml’,outext='.html’])
Finds all files that are in the cms.inpath directory that do not have corresponding files in the cms.outpath directory. You can customize the input extension and output extensions. By default these are '.xml’ and '.html’
ListModifiedFiles([inext='.xml’,outext='.html’])
Finds all files that are in the cms.inpath directory that have been updated after the corresponding file in the cms.outpath directory. You can customize the input extension and output extensions. The default values are inext=.xml and outext=.html
GetOutputPath(infile,[ext=".html”])
Returns an appropriate path in the cms.outpath directory for a file in the cms.inpath directory. The default ext value is '.html’. This will raise an Assertion Error with the infile path if does not refer to a file that is in (or potentially in) the cms.input directory.
AddToReport(item)
Adds the string item to the Web Content Managers report as a new line of text.
GenerateReport
Returns the text of the report that the Web Content Manager object creates
ClearReport
This resets the ContentManagers Report
WriteReport
Writes the contents of the Web Content Manager objects report to the file ‹log.txt’ in the project directory. It overwrites any previous log file.
The SiteUpdater subclass
The most common use of the WebContentManager is to update an entire site. I use the SiteUpdater subclass to do this.
SiteUpdater(pathname)
Updates the site by using the appropriate parser when necessary. It loads all of the parsers in the cms.source directory and determines which pages to update, then updates each page with the appropriate parser.
UpdateSite([report=1,overwrite=0])
Updates the site. If report is 0 then no report is made, if report is 1 (the default) then it prints the report to sys.out, if report is 2, then it creates a text file called ‹log.txt’ in the project folder and writes the report to that file.
if overwrite is 0 (the default) then the UpdateSite method will only process files that are new or files that have been modified since the last update.
UpdatePage(file,opath)
The file and opath parameters are the full pathnames of the input file and the output file. It determines which parser to use and processes the file accordingly.
Using SiteUpdater
Using SiteUpdater is very simple. Two lines of code:
myparser = SiteUpdater('zaphod:web')
myparser.UpdateSite()
The PageUpdater module
Here is the code to a file I use that lets you process one file at a time. I created an applet using MacPython so I have a drag and drop icon to work with.
from WCMS import SiteUpdater import os import sys pathnames = sys.argv[1:] if pathnames: pathname = pathnames[0] foldername,filename = os.path.split(pathname) print foldername,filename # fix the fact that the file is in a subfolder foldername= os.path.abspath(foldername+os.pardir) while not os.path.exists(os.path.join(foldername,'cms.ini')): foldername= os.path.abspath(foldername+os.pardir) updater = SiteUpdater(foldername) updater._LoadMenu() updater._LoadParsers() filestoupdate = pathnames for filename in filestoupdate: print "Updating",filename opath = updater.GetOutputPath(filename) updater.UpdatePage(filename,opath) print updater.GenerateReport() print "fin" else: print "This is a drag and drop file, drag a WCMS based XML file onto me."