def load_template(self): if not self.__mako_template or settings.MAKO_ALWAYS_RELOAD: if not self.__mako_lookup: self.__build() # To załaduje sam plik mako try: self.__mako_template = self.__mako_lookup.get_template( self.__mako_filename) # natomiast trzeba jeszcze załadować nadrzędne mako tmp_context = mako_runtime.Context( mako_util.FastEncodingBuffer()) tmp_context._with_template = self.__mako_template mako_runtime._populate_self_namespace(tmp_context, self.__mako_template) except Exception as ex: log.error("Cannot load template: %s (%s) in %s" % ( self.__mako_filename, self.__mako_template.module if self.__mako_template else 'file not found', self.__mako_lookup.directories, )) # print "Cannot load template: %s (%s)" % (self.__mako_filename, self.__mako_template.module if self.__mako_template else 'file not found') raise
def GenerateApiMap(base_dir, root_dir, api_config): """Create an apis_map.py file in the given root_dir with for given api_config. Args: base_dir: str, Path of directory for the project. root_dir: str, Path of the map file location within the project. api_config: regeneration config for all apis. """ api_def_filename, _ = os.path.splitext(api_def.__file__) api_def_source = files.ReadFileContents(api_def_filename + '.py') tpl = template.Template( filename=os.path.join(os.path.dirname(__file__), 'template.tpl')) api_map_file = os.path.join(base_dir, root_dir, 'apis_map.py') logging.debug('Generating api map at %s', api_map_file) api_map = _MakeApiMap(root_dir.replace('/', '.'), api_config) logging.debug('Creating following api map %s', api_map) with files.FileWriter(api_map_file) as apis_map_file: ctx = runtime.Context(apis_map_file, api_def_source=api_def_source, apis_map=api_map) tpl.render_context(ctx)
def test_context_small(self): ctx = runtime.Context([].append, x=5, y=4) eq_(sorted(ctx.keys()), ['caller', 'capture', 'x', 'y'])
def GenerateResourceModule(base_dir, root_dir, api_name, api_version, discovery_doc_path, custom_resources): """Create resource.py file for given api and its discovery doc. Args: base_dir: str, Path of directory for the project. root_dir: str, Path of the resource file location within the project. api_name: str, name of the api. api_version: str, the version for the api. discovery_doc_path: str, file path to discovery doc. custom_resources: dict, dictionary of custom resource collections. Raises: WrongDiscoveryDoc: if discovery doc api name/version does not match. """ discovery_doc = resource_generator.DiscoveryDoc.FromJson( os.path.join(base_dir, root_dir, discovery_doc_path)) if discovery_doc.api_version != api_version: logging.warning( 'Discovery api version %s does not match %s, ' 'this client will be accessible via new alias.', discovery_doc.api_version, api_version) if discovery_doc.api_name != api_name: raise WrongDiscoveryDoc('api name {0}, expected {1}'.format( discovery_doc.api_name, api_name)) resource_collections = discovery_doc.GetResourceCollections( custom_resources, api_version) if custom_resources: # Check if this is redefining one of the existing collections. matched_resources = set([]) for collection in resource_collections: if collection.name in custom_resources: matched_resources.add(collection.name) custom_path = custom_resources[collection.name]['path'] if isinstance(custom_path, dict): collection.flat_paths.update(custom_path) elif isinstance(custom_path, six.string_types): collection.flat_paths[ resource_generator.DEFAULT_PATH_NAME] = custom_path # Remaining must be new custom resources. for collection_name in set( custom_resources.keys()) - matched_resources: collection_def = custom_resources[collection_name] collection_path = collection_def['path'] enable_uri_parsing = collection_def.get('enable_uri_parsing', True) collection_info = discovery_doc.MakeResourceCollection( collection_name, collection_path, enable_uri_parsing, api_version) resource_collections.append(collection_info) api_dir = os.path.join(base_dir, root_dir, api_name, api_version) if not os.path.exists(api_dir): os.makedirs(api_dir) resource_file_name = os.path.join(api_dir, 'resources.py') if resource_collections: logging.debug('Generating resource module at %s', resource_file_name) tpl = template.Template( filename=os.path.join(os.path.dirname(__file__), 'resources.tpl')) with files.FileWriter(resource_file_name) as output_file: ctx = runtime.Context(output_file, collections=sorted(resource_collections), base_url=resource_collections[0].base_url, docs_url=discovery_doc.docs_url) tpl.render_context(ctx) elif os.path.isfile(resource_file_name): logging.debug('Removing existing resource module at %s', resource_file_name) os.remove(resource_file_name)
def test_context_small(self): ctx = runtime.Context([].append, x=5, y=4) eq_(sorted(ctx.keys()), ["caller", "capture", "x", "y"])
def init_opencl(self): """ Initializes the base items needed to run OpenCL code. """ # Startup script shamelessly taken from CS205 homework if self.context is None: platforms = cl.get_platforms() print 'The platforms detected are:' print '---------------------------' for platform in platforms: print platform.name, platform.vendor, 'version:', platform.version # List devices in each platform for platform in platforms: print 'The devices detected on platform', platform.name, 'are:' print '---------------------------' for device in platform.get_devices(): print device.name, '[Type:', cl.device_type.to_string( device.type), ']' print 'Maximum clock Frequency:', device.max_clock_frequency, 'MHz' print 'Maximum allocable memory size:', int( device.max_mem_alloc_size / 1e6), 'MB' print 'Maximum work group size', device.max_work_group_size print 'Maximum work item dimensions', device.max_work_item_dimensions print 'Maximum work item size', device.max_work_item_sizes print '---------------------------' # Create a context with all the devices devices = platforms[0].get_devices() if not self.use_interop: self.context = cl.Context(devices) else: self.context = cl.Context(properties=[ (cl.context_properties.PLATFORM, platforms[0]) ] + cl.tools.get_gl_sharing_context_properties(), devices=devices) print 'This context is associated with ', len( self.context.devices), 'devices' # Create a simple queue self.queue = cl.CommandQueue( self.context, self.context.devices[0], properties=cl.command_queue_properties.PROFILING_ENABLE) # Compile our OpenCL code...render MAKO first. lookup = mlo.TemplateLookup(directories=[file_dir]) template = lookup.get_template('colony_growth.mako') template.strict_undefined = True buf = sio.StringIO() mako_context = mrt.Context(buf, **self.ctx_info) try: template.render_context(mako_context) except: with open('mako_exception.html', 'w') as fi: fi.write(mako.exceptions.html_error_template().render()) assert False, 'Mako rendering failed...quitting...' with open('temp_kernels_DLA_colony.cl', 'w') as fi: fi.write(buf.getvalue()) self.kernels = cl.Program(self.context, buf.getvalue()).build(options='')