def hash(self): ''' Hash code of the element ''' return base.hash(self.identifier())
def guid(self): ''' GUID of the vertex ''' guid_original = "%s@%s" % (self.name, self.__creation_time__) return base.hash(guid_original)
def render(self, source, **kwargs): ''' This function is to render Mako templates. This function *can* have more than *one* parameter. There are *three* optional parameters only used by this function which are described below. The *rest* are context variables for rendering. The required parameter *source* is a string indicating the location of the template. If the optional parameter *direct_rendering* is false, it will attempt to render the *source* directly as if it is a template data. The optional parameter *direct_rendering* is a boolean acting as a switch to enable or disable the behaviour described above. This option may be set globally in the configuration file. It is set to 'False' by default. The optional parameter *text_minification* is a boolean acting as a switch to enable or disable auto HTML minification. This option may be set globally in the configuration file. Currently, it doesn't fully support JavaScript minification. It is set to 'False' by default. Any paarameters beside the three ones above will be treated as template context. ''' global base_uri global settings global template # Local flags (optional parameters) flag_memory_cache_template = "cache_template" flag_text_minification = "text_minification" flag_direct_rendering = "direct_rendering" # Cache key cache_key = base.hash(source) # Switches enable_text_minification = kwargs[flag_text_minification] if flag_text_minification in kwargs else (flag_text_minification in settings and settings[flag_text_minification]) enable_direct_rendering = kwargs[flag_direct_rendering] if flag_direct_rendering in kwargs else (flag_direct_rendering in settings and settings[flag_direct_rendering]) use_from_cache = self.__cache_enabled and cache_key in self.__cache # Default output output = '' # Actual source actual_source = source # Render with Mako compiled_template = None if use_from_cache: compiled_template = self.__cache[cache_key] else: for directory in self.__directories: if os.path.exists(os.path.join(directory, source)): actual_source = os.path.join(directory, source) compiled_template = self.__get(source) break if compiled_template: pass elif not compiled_template and os.path.exists(source): compiled_template = Template(filename=source, **self.__default_options) elif not compiled_template and enable_direct_rendering: compiled_template = Template(source, **self.__default_options) else: if enable_direct_rendering: raise cherrypy.HTTPError(500, "Cannot render the template directly.") else: raise cherrypy.HTTPError(500, "Cannot render the template from %s." % actual_source) try: output = compiled_template.render(**kwargs) except: raise Exception(html_error_template().render()) if self.__cache_enabled: self.__cache[base.hash(actual_source)] = compiled_template # HTML Minification if enable_text_minification: output = minify_content(output, True) return output