1 #!/usr/bin/env python 2 3 "A dictionary example application." 4 5 import WebStack.Generic 6 from XSLForms.Resources.WebResources import \ 7 XSLFormsResource, output, resources, prepare_resources as xslforms_prepare_resources 8 import os 9 10 # Site map imports. 11 12 from WebStack.Resources.ResourceMap import MapResource 13 from WebStack.Resources.Selectors import EncodingSelector 14 from WebStack.Resources.Static import DirectoryResource 15 16 # Configuration setting. 17 18 encoding = "utf-8" 19 20 # Resource classes. 21 22 class DictionaryResource(XSLFormsResource): 23 24 "A simple resource providing dictionary lookup." 25 26 resource_dir = resources(__file__) 27 template_resources = { 28 "words" : output("words_template.xhtml") 29 } 30 in_page_resources = { 31 "matches" : ("words", "words_output_entry.xsl", "matches-node"), 32 #"word" : ("words", "words_output_word.xsl", "word-node") 33 } 34 35 def __init__(self, dict): 36 37 "Initialise the resource with the given 'dict'." 38 39 self.dict = dict 40 41 def select_activity(self, trans, form): 42 form.set_activity("words") 43 44 def respond_to_input(self, trans, form): 45 46 # Add and remove elements according to the selectors found. 47 48 selectors = form.get_selectors() 49 self.remove_elements(selectors.get("remove")) 50 self.add_elements(selectors.get("add"), "entry") 51 52 def init_document(self, trans, form): 53 54 # Ensure all entries have a matches element. 55 # Ensure all matches elements have at least one choice. 56 # Copy selected matches to their corresponding text field. 57 58 words = form.get_document() 59 60 all_entries = words.xpath("words/entry") 61 62 for entry in all_entries: 63 matches_list = entry.xpath("matches") 64 if len(matches_list) == 0: 65 matches = words.createElement("matches") 66 entry.appendChild(matches) 67 else: 68 matches = matches_list[0] 69 70 if len(entry.xpath("matches/match-enum")) == 0: 71 match_enum = words.createElement("match-enum") 72 match_enum.setAttribute("word", "") 73 matches.appendChild(match_enum) 74 75 # Find requested search locations. 76 77 selectors = form.get_selectors() 78 in_page_resource = self.get_in_page_resource(trans) 79 80 if selectors.has_key("search"): 81 entries = selectors["search"] 82 elif in_page_resource == "matches": 83 entries = all_entries 84 else: 85 entries = [] 86 87 # Transform, adding dictionary information. 88 89 for entry in entries: 90 word = entry.getAttribute("word") 91 if word != "": 92 matches = entry.xpath("matches")[0] 93 for found_word in self.dict.find(word): 94 match_enum = words.createElement("match-enum") 95 match_enum.setAttribute("word", found_word) 96 matches.appendChild(match_enum) 97 98 # Copy selected values into text fields. 99 # NOTE: Since libxml2dom does not guarantee node equality for two nodes 100 # NOTE: referring to the same thing, we cannot just loop over all the 101 # NOTE: entries and query whether they reside in the search locations. 102 103 for entry in all_entries: 104 matches = entry.xpath("matches")[0] 105 if matches.hasAttribute("word"): 106 word = matches.getAttribute("word") 107 if word != "" and word.startswith(entry.getAttribute("word")): 108 entry.setAttribute("word", word) 109 110 # Site map initialisation. 111 112 def get_site(dict): 113 114 """ 115 Return a simple Web site resource using the given 'dict' - a dictionary 116 employed by the application. 117 """ 118 119 # Get the main resource and the directory used by the application. 120 121 dictionary_resource = DictionaryResource(dict) 122 directory = dictionary_resource.resource_dir 123 124 # Make a simple Web site. 125 126 resource = MapResource({ 127 # Static resources: 128 "scripts" : DirectoryResource(os.path.join(directory, "scripts"), {"js" : "text/javascript"}), 129 # Main page and in-page resources: 130 "" : dictionary_resource, 131 "matches" : dictionary_resource 132 }) 133 134 return EncodingSelector(resource, encoding) 135 136 # Resource preparation ahead of time - useful for making installations. 137 138 def prepare_resources(): 139 for cls in [DictionaryResource]: 140 xslforms_prepare_resources(cls) 141 142 # vim: tabstop=4 expandtab shiftwidth=4