1 #!/usr/bin/env python 2 3 "A dictionary example application." 4 5 import WebStack.Generic 6 import XSLForms.Resources 7 import XSLForms.Utils 8 import os 9 10 # Site map imports. 11 12 from WebStack.Resources.ResourceMap import MapResource 13 from WebStack.Resources.Static import DirectoryResource 14 15 # Resource classes. 16 17 class DictionaryResource(XSLForms.Resources.XSLFormsResource): 18 19 "A simple resource providing dictionary lookup." 20 21 resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") 22 encoding = "utf-8" 23 template_resources = { 24 "words" : ("words_template.xhtml", "words_output.xsl") 25 } 26 in_page_resources = { 27 "entry" : ("words_output_entry.xsl", "entry-node") 28 } 29 30 def __init__(self, dict): 31 32 "Initialise the resource with the given 'dict'." 33 34 self.dict = dict 35 36 def respond_to_form(self, trans, form): 37 38 """ 39 Respond to a request having the given transaction 'trans' and the given 40 'form' information. 41 """ 42 43 in_page_resource = self.get_in_page_resource(trans) 44 parameters = form.get_parameters() 45 documents = form.get_documents() 46 47 # Ensure the presence of a document. 48 49 if documents.has_key("words"): 50 words = documents["words"] 51 else: 52 words = form.new_instance("words") 53 54 # Add and remove elements according to the selectors found. 55 56 selectors = form.get_selectors() 57 XSLForms.Utils.remove_elements(selectors.get("remove")) 58 XSLForms.Utils.add_elements(selectors.get("add"), "entry") 59 60 # Ensure all entries have a matches element. 61 # Ensure all matches elements have at least one choice. 62 # Copy selected matches to their corresponding text field. 63 64 for entry in words.xpath("words/entry"): 65 matches_list = entry.xpath("matches") 66 if len(matches_list) == 0: 67 matches = words.createElement("matches") 68 entry.appendChild(matches) 69 else: 70 matches = matches_list[0] 71 72 if len(entry.xpath("matches/match-enum")) == 0: 73 match_enum = words.createElement("match-enum") 74 match_enum.setAttribute("word", "") 75 matches.appendChild(match_enum) 76 77 if matches.hasAttribute("word"): 78 word = matches.getAttribute("word") 79 if word != "": 80 entry.setAttribute("word", word) 81 82 # Find requested search locations. 83 84 if selectors.has_key("search"): 85 entries = selectors["search"] 86 elif in_page_resource == "entry": 87 entries = words.xpath("words/entry") 88 else: 89 entries = [] 90 91 # Transform, adding dictionary information. 92 93 for entry in entries: 94 word = entry.getAttribute("word") 95 if word != "": 96 matches = entry.xpath("matches")[0] 97 for found_word in self.dict.find(word): 98 match_enum = words.createElement("match-enum") 99 match_enum.setAttribute("word", found_word) 100 matches.appendChild(match_enum) 101 102 # Start the response. 103 104 trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding)) 105 106 # Define the stylesheet parameters. 107 108 stylesheet_parameters = {} 109 110 # Ensure that an output stylesheet exists. 111 112 if in_page_resource in self.in_page_resources.keys(): 113 trans_xsl = self.prepare_fragment("words", in_page_resource) 114 element_path = parameters.get("element-path", [""])[0] 115 stylesheet_parameters["element-path"] = element_path 116 else: 117 trans_xsl = self.prepare_output("words") 118 119 # Complete the response. 120 121 self.send_output(trans, [trans_xsl], words, stylesheet_parameters) 122 123 # Site map initialisation. 124 125 def get_site(dict): 126 127 """ 128 Return a simple Web site resource using the given 'dict' - a dictionary 129 employed by the application. 130 """ 131 132 # Get the main resource and the directory used by the application. 133 134 dictionary_resource = DictionaryResource(dict) 135 directory = dictionary_resource.resource_dir 136 137 # Make a simple Web site. 138 139 resource = MapResource({ 140 # Static resources: 141 "scripts" : DirectoryResource(os.path.join(directory, "scripts"), {"js" : "text/javascript"}), 142 # Main page and in-page resources: 143 None : dictionary_resource 144 }) 145 146 return resource 147 148 # vim: tabstop=4 expandtab shiftwidth=4