# HG changeset patch # User paulb # Date 1195330160 0 # Node ID 7f718bab79920fed23ffa3dd72e7a236d733ec1d # Parent ab1c6978cdf19bebd9390094d3eece8e99223fc7 [project @ 2007-11-17 20:09:16 by paulb] Added convenience functions for use with the different XSLFormsResource class attributes. diff -r ab1c6978cdf1 -r 7f718bab7992 XSLForms/Resources/WebResources.py --- a/XSLForms/Resources/WebResources.py Fri Oct 05 23:44:25 2007 +0000 +++ b/XSLForms/Resources/WebResources.py Sat Nov 17 20:09:20 2007 +0000 @@ -359,4 +359,53 @@ for input_identifier in cls.init_resources.keys(): prepare_initialiser(cls, input_identifier, 1) +# Convenience methods for specifying resources. + +def split(filename): + + """ + Return a tuple containing the directory and filename without extension for + 'filename'. + """ + + d, leafname = os.path.split(filename) + name, ext = os.path.splitext(leafname) + return d, name + +def output(template_filename): + + """ + Return a tuple containing the 'template_filename' and a suitable output + stylesheet filename. + """ + + d, name = split(template_filename) + output_name = name.replace("_template", "_output") + os.path.extsep + "xsl" + return (template_filename, os.path.join(d, output_name)) + +def input(template_filename): + + """ + Return a tuple containing the 'template_filename' and a suitable output + stylesheet filename. + """ + + d, name = split(template_filename) + input_name = name.replace("_template", "_input") + os.path.extsep + "xsl" + return (template_filename, os.path.join(d, input_name)) + +def resources(filename, d="Resources"): + + """ + Return the resource directory for the given 'filename', using the optional + directory name 'd' to indicate the directory relative to the directory of + 'filename' (or the default directory name, indicating that the directory + called "Resources" - a sibling of 'filename' - is the resource directory). + + It is envisaged that callers provide the value of the __file__ special + variable to get the resource directory relative to a particular module. + """ + + return os.path.join(os.path.split(filename)[0], d) + # vim: tabstop=4 expandtab shiftwidth=4 diff -r ab1c6978cdf1 -r 7f718bab7992 examples/Common/Candidate/__init__.py --- a/examples/Common/Candidate/__init__.py Fri Oct 05 23:44:25 2007 +0000 +++ b/examples/Common/Candidate/__init__.py Sat Nov 17 20:09:20 2007 +0000 @@ -4,7 +4,7 @@ import WebStack.Generic from WebStack.Repositories.Directory import DirectoryRepository -import XSLForms.Resources.WebResources +from XSLForms.Resources.WebResources import XSLFormsResource, input, output, resources import XSLForms.Utils import os import libxml2dom @@ -21,16 +21,16 @@ # Resource classes. -class AdminResource(XSLForms.Resources.WebResources.XSLFormsResource): +class AdminResource(XSLFormsResource): "A resource providing administration facilities for job candidate profiles." - resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") + resource_dir = resources(__file__) template_resources = { - "admin" : ("admin_template.xhtml", "admin_output.xsl") + "admin" : output("admin_template.xhtml") } init_resources = { - "admin" : ("admin_template.xhtml", "admin_input.xsl") + "admin" : input("admin_template.xhtml") } def __init__(self, repository): @@ -116,12 +116,12 @@ "A resource providing editing facilities for a job candidate profile." - resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") + resource_dir = resources(__file__) template_resources = { - "candidate_display" : ("candidate_display_template.xhtml", "candidate_display_output.xsl") + "candidate_display" : output("candidate_display_template.xhtml") } init_resources = { - "candidate" : ("candidate_template.xhtml", "candidate_input.xsl") + "candidate" : input("candidate_template.xhtml") } document_resources = { "status" : "candidate_status.xml" @@ -176,12 +176,12 @@ "A resource providing editing facilities for a job candidate profile." - resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") + resource_dir = resources(__file__) template_resources = { - "candidate" : ("candidate_template.xhtml", "candidate_output.xsl") + "candidate" : output("candidate_template.xhtml") } init_resources = { - "candidate" : ("candidate_template.xhtml", "candidate_input.xsl") + "candidate" : input("candidate_template.xhtml") } document_resources = { "status" : "candidate_status.xml" @@ -274,7 +274,7 @@ if use_cwd: resource_dir = os.getcwd() else: - resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") + resource_dir = resources(__file__) repository = DirectoryRepository(os.path.join(resource_dir, "candidates"), fsencoding) # Get the main resource and the directory used by the application.