コード例 #1
0
ファイル: templates.py プロジェクト: gdw2/zim
def get_template(format, template):
	'''Returns a Template object for a template name, file path, or File object'''
	# NOTE: here the "category" needs to be a format at the same time !
	if isinstance(template, File):
		file = template
	else:
		if not is_path_re.match(template):
			file = None
			path = list(data_dirs(('templates', format)))
			path.reverse()
			for dir in path:
				for basename in dir.list():
					name = basename.rsplit('.')[0] # robust if no '.' in basename
					if name == template:
						file = dir.file(basename)
						if file.exists(): # is a file
							break

			if not file:
				file = File(template)
		else:
			file = File(template)

	logger.info('Loading template from: %s', file)
	if not file.exists():
		raise AssertionError, 'No such file: %s' % file

	basename, ext = file.basename.rsplit('.', 1)
	resources = file.dir.subdir(basename)

	return Template(file.readlines(), format, name=file.path, resources_dir=resources)
コード例 #2
0
def get_template(category, template):
	'''Returns a Template object for a template name or file path
	@param category: the template category (e.g. "html"). Use to resolve
	the template if a template name is given
	@param template: the template name or file path
	'''
	assert isinstance(template, basestring)

	if is_path_re.match(template):
		file = File(template)
	else:
		file = None
		for dir in data_dirs(('templates', category)):
			for basename in dir.list():
				name = basename.rsplit('.')[0] # robust if no '.' in basename
				if basename == template or name == template:
					file = dir.file(basename)
					if file.exists(): # is a file
						break
			if file and file.exists():
				break
		else:
			file = File(template)
			if not file.exists():
				raise PathLookupError, _('Could not find template "%s"') % template
					# T: Error message in template lookup

	if not file.exists():
		raise PathLookupError, _('No such file: %s') % file
			# T: Error message in template lookup

	logger.info('Loading template from: %s', file)
	#~ basename, ext = file.basename.rsplit('.', 1)
	#~ resources = file.dir.subdir(basename)
	return Template(file)
コード例 #3
0
def get_template(format, template):
    '''Returns a Template object for a template name, file path, or File object'''
    # NOTE: here the "category" needs to be a format at the same time !
    if isinstance(template, File):
        file = template
    else:
        if not is_path_re.match(template):
            file = None
            path = list(data_dirs(('templates', format)))
            path.reverse()
            for dir in path:
                for basename in dir.list():
                    name = basename.rsplit('.')[
                        0]  # robust if no '.' in basename
                    if name == template:
                        file = dir.file(basename)
                        if file.exists():  # is a file
                            break

            if not file:
                file = File(template)
        else:
            file = File(template)

    logger.info('Loading template from: %s', file)
    if not file.exists():
        raise AssertionError, 'No such file: %s' % file

    basename, ext = file.basename.rsplit('.', 1)
    resources = file.dir.subdir(basename)

    return Template(file.readlines(),
                    format,
                    name=file.path,
                    resources_dir=resources)
コード例 #4
0
ファイル: templates.py プロジェクト: damiansimanuk/texslide
def get_template(format, name):
	'''Returns a Template object for a tempalte name or a file path'''
	if is_path_re.match(name):
		file = File(name)
	else:
		templates = list_templates(format)
		#~ if not name in templates: FIXME exception type
			#~ raise
		file = File(templates[name])
	logger.info('Loading template from: %s', file)
	return Template(file.readlines(), format, name=file)