本文整理汇总了Python中nikola.utils.get_asset_path函数的典型用法代码示例。如果您正苦于以下问题:Python get_asset_path函数的具体用法?Python get_asset_path怎么用?Python get_asset_path使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_asset_path函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_demo
def build_demo(theme, themes_dir, demo_source, demo_destination):
demo_destination = os.path.abspath(demo_destination)
if os.path.isdir(demo_source):
shutil.rmtree(demo_source)
if os.path.isdir(demo_destination):
shutil.rmtree(demo_destination)
sys.stderr.write('=> Building {}...\n'.format(theme))
subprocess.check_call(["nikola", "init", "-qd", demo_source], stdout=subprocess.PIPE)
os.symlink(os.path.abspath(themes_dir), os.path.abspath("/".join([demo_source, "themes"])))
conf_path = "/".join([demo_source, "conf.py"])
book_path = "/".join([demo_source, "templates", "book.tmpl"])
lorem_path = "/".join([demo_source, "posts", "lorem-ipsum.rst"])
# Get custom required settings from the theme
themes = utils.get_theme_chain(theme, themes_dirs=[themes_dir, 'themes'])
engine_path = utils.get_asset_path('engine', themes)
extra_conf_path = utils.get_asset_path('conf.py.sample', themes)
extra_conf = ''
if extra_conf_path:
extra_conf = io.open(extra_conf_path, 'r', encoding="utf-8").read()
if engine_path:
engine = io.open(engine_path, 'r', encoding="utf-8").read().strip()
if engine == 'jinja':
shutil.copy('book-jinja.tmpl', book_path)
with io.open(conf_path, "a", encoding="utf-8") as conf:
conf.write(u"\n\nTHEME = '{0}'\nUSE_BUNDLES = False\nOUTPUT_FOLDER = '{1}'\nSOCIAL_BUTTONS_CODE = ''\nUSE_BASE_TAG = False\n\n{2}\n".format(theme, demo_destination, extra_conf))
shutil.copy(LOREM_BASE, lorem_path)
with cd(demo_source):
subprocess.check_call(["nikola", "build"], stdout=subprocess.PIPE)
sys.stderr.write('=> Done building {}\n'.format(theme))
示例2: get_data
def get_data(theme):
data = {}
data['chain'] = utils.get_theme_chain(theme)
data['name'] = theme
readme = utils.get_asset_path('README.md', data['chain'])
conf_sample = utils.get_asset_path('conf.py.sample', data['chain'])
if readme:
data['readme'] = codecs.open(readme, 'r', 'utf8').read()
else:
data['readme'] = 'No README.md file available.'
if conf_sample:
data['confpy'] = pygments.highlight(
codecs.open(conf_sample, 'r', 'utf8').read(),
PythonLexer(), HtmlFormatter(cssclass='code'))
else:
data['confpy'] = None
data['bootswatch'] = ('bootstrap' in data['chain'] or
'bootstrap-jinja' in data['chain'] or
'bootstrap3-jinja' in data['chain'] or
'bootstrap3' in data['chain']) and \
'bootstrap3-gradients' not in data['chain']
data['engine'] = utils.get_template_engine(data['chain'])
data['chain'] = data['chain'][::-1]
return data
示例3: get_data
def get_data(theme):
data = {}
data['chain'] = utils.get_theme_chain(theme, DIR)
data['name'] = theme
readme = utils.get_asset_path('README.md', data['chain'], _themes_dir=DIR)
conf_sample = utils.get_asset_path('conf.py.sample', data['chain'], _themes_dir=DIR)
if readme:
data['readme'] = io.open(readme, 'r', encoding='utf-8').read()
else:
data['readme'] = 'No README.md file available.'
if conf_sample:
data['confpy'] = pygments.highlight(
io.open(conf_sample, 'r', encoding='utf-8').read(),
PythonLexer(), HtmlFormatter(cssclass='code'))
else:
data['confpy'] = None
data['bootswatch'] = ('bootstrap' in data['chain'] or
'bootstrap-jinja' in data['chain'] or
'bootstrap3-jinja' in data['chain'] or
'bootstrap3' in data['chain']) and \
'bootstrap3-gradients' not in data['chain']
data['engine'] = utils.get_template_engine(data['chain'], DIR)
data['chain'] = data['chain'][::-1]
data['allver'] = []
for v in ALL_VERSIONS_SUPPORTED:
if glob.glob('v{0}/*'.format(v)):
data['allver'].append(v)
return data
示例4: is_asset_duplicated
def is_asset_duplicated(path, themes):
# First get the path for the asset with whole theme chain
p1 = utils.get_asset_path(path, themes)
# Get the path for asset with truncated theme chain
p2 = utils.get_asset_path(path, themes[1:])
# Compare
if p1 and p2:
return filecmp.cmp(p1, p2, False), p1, p2
else:
return False, p1, p2
示例5: is_asset_duplicated
def is_asset_duplicated(path, themes):
# First get the path for the asset with whole theme chain
p1 = utils.get_asset_path(path, themes, _themes_dir="v7")
# Get the path for asset with truncated theme chain
p2 = utils.get_asset_path(path, themes[1:], _themes_dir="v7")
# README.md is ok to duplicate
if "README.md" in path:
return False, p1, p2
# Compare
if p1 and p2:
return filecmp.cmp(p1, p2, False), p1, p2
else:
return False, p1, p2
示例6: is_asset_duplicated
def is_asset_duplicated(path, themes):
# First get the path for the asset with whole theme chain
p1 = utils.get_asset_path(path, themes)
# Get the path for asset with truncated theme chain
p2 = utils.get_asset_path(path, themes[1:])
# Ignore some files that may be duplicated
if any(i in path for i in ('README.md', 'engine', 'AUTHORS.txt', 'conf.py.sample')):
return False, p1, p2
# Compare
if p1 and p2:
return filecmp.cmp(p1, p2, False), p1, p2
else:
return False, p1, p2
示例7: sanity_check
def sanity_check(theme=None):
if theme is None: # Check them all
for theme in theme_list():
sanity_check(theme)
return
themes = utils.get_theme_chain(theme, themes_dirs=['v7'])
themes_bn = [os.path.basename(i) for i in themes]
engine = utils.get_template_engine(themes)
# Inheritance checks
# All themes must inherit from base
if themes_bn[-1] != 'base':
error("theme {0} doesn't inherit from base".format(theme))
# All jinja themes must inherit from base-jinja
if engine == "jinja" and "base-jinja" not in themes_bn:
error("theme {0} is jinja-based and doesn't inherit from base-jinja".format(theme))
# All mako themes must NOT inherit from base-jinja
if engine == "mako" and "base-jinja" in themes_bn:
error("theme {0} is mako-based and inherits from base-jinja".format(theme))
# Detect exact asset duplication in theme chain
for root, dirs, files in os.walk("v7/"+theme):
for f in files:
path = "/".join([root, f])
asset = path.split("/", 2)[-1]
r, p1, p2 = is_asset_duplicated(asset, themes)
if r:
error("duplicated asset: {0} {1}".format(p1, p2))
# Detect deprecated names and anonymous namespaces
for root, dirs, files in os.walk("v7/"+theme+"/templates"):
for f in files:
path = "/".join([root, f])
with io.open(path, "r", encoding="utf-8") as inf:
data = inf.read()
for k, exceptions in blacklist:
if k in data and f not in exceptions:
error("theme '{0}' contains deprecated name '{1}' in {2}".format(theme, k, path))
# Ensure the theme has a README.md
if utils.get_asset_path('README.md', themes) is None:
error("theme '{0}' has no README.md".format(theme))
# Ensure the theme has a meta file
if utils.get_asset_path(theme + '.theme', themes) is None:
warning("theme '{0}' has no {0}.theme meta file".format(theme))
示例8: gen_tasks
def gen_tasks(self):
"""Bundle assets using WebAssets."""
kw = {
'filters': self.site.config['FILTERS'],
'output_folder': self.site.config['OUTPUT_FOLDER'],
'cache_folder': self.site.config['CACHE_FOLDER'],
'theme_bundles': get_theme_bundles(self.site.THEMES),
'themes': self.site.THEMES,
'files_folders': self.site.config['FILES_FOLDERS'],
'code_color_scheme': self.site.config['CODE_COLOR_SCHEME'],
}
def build_bundle(output, inputs):
out_dir = os.path.join(kw['output_folder'],
os.path.dirname(output))
inputs = [i for i in inputs if os.path.isfile(
os.path.join(out_dir, i))]
cache_dir = os.path.join(kw['cache_folder'], 'webassets')
utils.makedirs(cache_dir)
env = webassets.Environment(out_dir, os.path.dirname(output),
cache=cache_dir)
if inputs:
bundle = webassets.Bundle(*inputs, output=os.path.basename(output))
env.register(output, bundle)
# This generates the file
env[output].urls()
else:
with open(os.path.join(out_dir, os.path.basename(output)), 'wb+'):
pass # Create empty file
yield self.group_task()
if (webassets is not None and self.site.config['USE_BUNDLES'] is not
False):
for name, _files in kw['theme_bundles'].items():
output_path = os.path.join(kw['output_folder'], name)
dname = os.path.dirname(name)
files = []
for fname in _files:
# paths are relative to dirname
files.append(os.path.join(dname, fname))
file_dep = [os.path.join(kw['output_folder'], fname)
for fname in files if
utils.get_asset_path(fname, self.site.THEMES, self.site.config['FILES_FOLDERS'])]
task = {
'file_dep': list(file_dep),
'task_dep': ['copy_assets'],
'basename': str(self.name),
'name': str(output_path),
'actions': [(build_bundle, (name, files))],
'targets': [output_path],
'uptodate': [
utils.config_changed({
1: kw,
2: file_dep
})],
'clean': True,
}
yield utils.apply_filters(task, kw['filters'])
示例9: gen_tasks
def gen_tasks(self):
"""Generate CSS out of LESS sources."""
kw = {
'cache_folder': self.site.config['CACHE_FOLDER'],
'themes': self.site.THEMES,
}
# Find where in the theme chain we define the LESS targets
# There can be many *.less in the folder, but we only will build
# the ones listed in less/targets
targets_path = utils.get_asset_path(os.path.join(self.sources_folder, "targets"), self.site.THEMES)
try:
with codecs.open(targets_path, "rb", "utf-8") as inf:
targets = [x.strip() for x in inf.readlines()]
except Exception:
targets = []
for theme_name in kw['themes']:
src = os.path.join(utils.get_theme_path(theme_name), self.sources_folder)
for task in utils.copy_tree(src, os.path.join(kw['cache_folder'], self.sources_folder)):
task['basename'] = 'prepare_less_sources'
yield task
# Build targets and write CSS files
base_path = utils.get_theme_path(self.site.THEMES[0])
dst_dir = os.path.join(self.site.config['OUTPUT_FOLDER'], 'assets', 'css')
# Make everything depend on all sources, rough but enough
deps = glob.glob(os.path.join(
base_path,
self.sources_folder,
"*{0}".format(self.sources_ext)))
def compile_target(target, dst):
utils.makedirs(dst_dir)
src = os.path.join(kw['cache_folder'], self.sources_folder, target)
try:
compiled = subprocess.check_output([self.compiler_name, src])
except OSError:
utils.req_missing([self.compiler_name],
'build LESS files (and use this theme)',
False, False)
with open(dst, "wb+") as outf:
outf.write(compiled)
yield self.group_task()
for target in targets:
dst = os.path.join(dst_dir, target.replace(self.sources_ext, ".css"))
yield {
'basename': self.name,
'name': dst,
'targets': [dst],
'file_dep': deps,
'task_dep': ['prepare_less_sources'],
'actions': ((compile_target, [target, dst]), ),
'uptodate': [utils.config_changed(kw)],
'clean': True
}
示例10: gen_tasks
def gen_tasks(self):
"""Bundle assets using WebAssets."""
kw = {
'filters': self.site.config['FILTERS'],
'output_folder': self.site.config['OUTPUT_FOLDER'],
'cache_folder': self.site.config['CACHE_FOLDER'],
'theme_bundles': get_theme_bundles(self.site.THEMES),
'themes': self.site.THEMES,
'files_folders': self.site.config['FILES_FOLDERS'],
'code_color_scheme': self.site.config['CODE_COLOR_SCHEME'],
}
def build_bundle(output, inputs):
out_dir = os.path.join(kw['output_folder'],
os.path.dirname(output))
inputs = [i for i in inputs if os.path.isfile(
os.path.join(out_dir, i))]
cache_dir = os.path.join(kw['cache_folder'], 'webassets')
if not os.path.isdir(cache_dir):
os.makedirs(cache_dir)
env = webassets.Environment(out_dir, os.path.dirname(output),
cache=cache_dir)
bundle = webassets.Bundle(*inputs, output=os.path.basename(output))
env.register(output, bundle)
# This generates the file
env[output].urls()
flag = False
if (webassets is not None and self.site.config['USE_BUNDLES'] is not
False):
for name, files in kw['theme_bundles'].items():
output_path = os.path.join(kw['output_folder'], name)
dname = os.path.dirname(name)
file_dep = [utils.get_asset_path(
os.path.join(dname, fname), kw['themes'],
kw['files_folders'])
for fname in files
]
file_dep = filter(None, file_dep) # removes missing files
task = {
'file_dep': file_dep,
'basename': str(self.name),
'name': str(output_path),
'actions': [(build_bundle, (name, files))],
'targets': [output_path],
'uptodate': [utils.config_changed(kw)],
'clean': True,
}
flag = True
yield utils.apply_filters(task, kw['filters'])
if flag is False: # No page rendered, yield a dummy task
yield {
'basename': self.name,
'uptodate': [True],
'name': 'None',
'actions': [],
}
示例11: test_get_asset_path
def test_get_asset_path():
assert get_asset_path('assets/css/nikola_rst.css',
get_theme_chain('bootstrap4', ['themes'])).replace(
'\\', '/').endswith('nikola/data/themes/base/assets/css/nikola_rst.css')
assert get_asset_path('assets/css/theme.css',
get_theme_chain('bootstrap4', ['themes'])).replace(
'\\', '/').endswith(
'nikola/data/themes/bootstrap4/assets/css/theme.css')
assert get_asset_path(
'nikola.py', get_theme_chain('bootstrap4', ['themes']),
{'nikola': ''}).replace(
'\\', '/').endswith('nikola/nikola.py')
assert get_asset_path('nikola.py', get_theme_chain(
'bootstrap4', ['themes']), {'nikola': 'nikola'}) is None
assert get_asset_path(
'nikola/nikola.py', get_theme_chain('bootstrap4', ['themes']),
{'nikola': 'nikola'}).replace(
'\\', '/').endswith('nikola/nikola.py')
示例12: gen_tasks
def gen_tasks(self):
"""Generate CSS out of LESS sources."""
kw = {"cache_folder": self.site.config["CACHE_FOLDER"], "themes": self.site.THEMES}
# Find where in the theme chain we define the LESS targets
# There can be many *.less in the folder, but we only will build
# the ones listed in less/targets
targets_path = utils.get_asset_path(os.path.join(self.sources_folder, "targets"), self.site.THEMES)
try:
with codecs.open(targets_path, "rb", "utf-8") as inf:
targets = [x.strip() for x in inf.readlines()]
except Exception:
targets = []
for theme_name in kw["themes"]:
src = os.path.join(utils.get_theme_path(theme_name), self.sources_folder)
for task in utils.copy_tree(src, os.path.join(kw["cache_folder"], self.sources_folder)):
# task['basename'] = self.name
yield task
# Build targets and write CSS files
base_path = utils.get_theme_path(self.site.THEMES[0])
dst_dir = os.path.join(self.site.config["OUTPUT_FOLDER"], "assets", "css")
# Make everything depend on all sources, rough but enough
deps = glob.glob(os.path.join(base_path, self.sources_folder, "*{0}".format(self.sources_ext)))
def compile_target(target, dst):
if not os.path.isdir(dst_dir):
os.makedirs(dst_dir)
src = os.path.join(kw["cache_folder"], self.sources_folder, target)
compiled = subprocess.check_output([self.compiler_name, src])
with open(dst, "wb+") as outf:
outf.write(compiled)
for target in targets:
dst = os.path.join(dst_dir, target.replace(self.sources_ext, ".css"))
yield {
"basename": self.name,
"name": dst,
"targets": [dst],
"file_dep": deps,
"actions": ((compile_target, [target, dst]),),
"uptodate": [utils.config_changed(kw)],
"clean": True,
}
if not targets:
yield {"basename": self.name, "actions": []}
示例13: gen_tasks
def gen_tasks(self):
"""Bundle assets using WebAssets."""
kw = {
"filters": self.site.config["FILTERS"],
"output_folder": self.site.config["OUTPUT_FOLDER"],
"cache_folder": self.site.config["CACHE_FOLDER"],
"theme_bundles": get_theme_bundles(self.site.THEMES),
"themes": self.site.THEMES,
"files_folders": self.site.config["FILES_FOLDERS"],
"code_color_scheme": self.site.config["CODE_COLOR_SCHEME"],
}
def build_bundle(output, inputs):
out_dir = os.path.join(kw["output_folder"], os.path.dirname(output))
inputs = [i for i in inputs if os.path.isfile(os.path.join(out_dir, i))]
cache_dir = os.path.join(kw["cache_folder"], "webassets")
if not os.path.isdir(cache_dir):
os.makedirs(cache_dir)
env = webassets.Environment(out_dir, os.path.dirname(output), cache=cache_dir)
bundle = webassets.Bundle(*inputs, output=os.path.basename(output))
env.register(output, bundle)
# This generates the file
env[output].urls()
flag = False
if webassets is not None and self.site.config["USE_BUNDLES"] is not False:
for name, files in kw["theme_bundles"].items():
output_path = os.path.join(kw["output_folder"], name)
dname = os.path.dirname(name)
file_dep = [
utils.get_asset_path(os.path.join(dname, fname), kw["themes"], kw["files_folders"])
for fname in files
]
file_dep = filter(None, file_dep) # removes missing files
task = {
"file_dep": file_dep,
"basename": str(self.name),
"name": str(output_path),
"actions": [(build_bundle, (name, files))],
"targets": [output_path],
"uptodate": [utils.config_changed(kw)],
}
flag = True
yield utils.apply_filters(task, kw["filters"])
if flag is False: # No page rendered, yield a dummy task
yield {"basename": self.name, "uptodate": [True], "name": "None", "actions": []}
示例14: gen_tasks
def gen_tasks(self):
"""Generate a robots.txt file."""
kw = {
"base_url": self.site.config["BASE_URL"],
"site_url": self.site.config["SITE_URL"],
"output_folder": self.site.config["OUTPUT_FOLDER"],
"files_folders": self.site.config["FILES_FOLDERS"],
"robots_exclusions": self.site.config["ROBOTS_EXCLUSIONS"],
"filters": self.site.config["FILTERS"],
}
sitemapindex_url = urljoin(kw["base_url"], "sitemapindex.xml")
robots_path = os.path.join(kw["output_folder"], "robots.txt")
def write_robots():
if kw["site_url"] != urljoin(kw["site_url"], "/"):
utils.LOGGER.warn("robots.txt not ending up in server root, will be useless")
with io.open(robots_path, "w+", encoding="utf8") as outf:
outf.write("Sitemap: {0}\n\n".format(sitemapindex_url))
if kw["robots_exclusions"]:
outf.write("User-Agent: *\n")
for loc in kw["robots_exclusions"]:
outf.write("Disallow: {0}\n".format(loc))
yield self.group_task()
if not utils.get_asset_path("robots.txt", [], files_folders=kw["files_folders"], output_dir=False):
yield utils.apply_filters(
{
"basename": self.name,
"name": robots_path,
"targets": [robots_path],
"actions": [(write_robots)],
"uptodate": [utils.config_changed(kw, "nikola.plugins.task.robots")],
"clean": True,
"task_dep": ["sitemap"],
},
kw["filters"],
)
elif kw["robots_exclusions"]:
utils.LOGGER.warn(
"Did not generate robots.txt as one already exists in FILES_FOLDERS. ROBOTS_EXCLUSIONS will not have any affect on the copied file."
)
else:
utils.LOGGER.debug("Did not generate robots.txt as one already exists in FILES_FOLDERS.")
示例15: get_data
def get_data(theme):
data = {}
data['name'] = theme
readme = utils.get_asset_path('README', [theme])
if readme:
data['readme'] = open(readme).read()
else:
data['readme'] = 'No readme file available'
data['chain'] = utils.get_theme_chain(theme)
data['bootswatch'] = ('bootstrap' in data['chain'] or
'bootstrap-jinja' in data['chain'] or
'bootstrap3-jinja' in data['chain'] or
'bootstrap3' in data['chain']) and \
'bootstrap3-gradients' not in data['chain']
data['engine'] = utils.get_template_engine(data['chain'])
data['chain'] = data['chain'][::-1]
return data