本文整理汇总了Python中hyde.fs.Folder.child方法的典型用法代码示例。如果您正苦于以下问题:Python Folder.child方法的具体用法?Python Folder.child怎么用?Python Folder.child使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hyde.fs.Folder
的用法示例。
在下文中一共展示了Folder.child方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create
# 需要导入模块: from hyde.fs import Folder [as 别名]
# 或者: from hyde.fs.Folder import child [as 别名]
def create(self, args):
"""
The create command. Creates a new site from the template at the given
sitepath.
"""
self.main(args)
sitepath = Folder(Folder(args.sitepath).fully_expanded_path)
markers = ['content', 'layout', 'site.yaml']
exists = any((FS(sitepath.child(item)).exists for item in markers))
if exists and not args.overwrite:
raise HydeException(
"The given site path [%s] already contains a hyde site."
" Use -f to overwrite." % sitepath)
layout = Layout.find_layout(args.layout)
logger.info(
"Creating site at [%s] with layout [%s]" % (sitepath, layout))
if not layout or not layout.exists:
raise HydeException(
"The given layout is invalid. Please check if you have the"
" `layout` in the right place and the environment variable(%s)"
" has been setup properly if you are using custom path for"
" layouts" % HYDE_DATA)
layout.copy_contents_to(args.sitepath)
logger.info("Site creation complete")
示例2: has_resource_changed
# 需要导入模块: from hyde.fs import Folder [as 别名]
# 或者: from hyde.fs.Folder import child [as 别名]
def has_resource_changed(self, resource):
"""
Checks if the given resource has changed since the
last generation.
"""
logger.debug("Checking for changes in %s" % resource)
self.load_site_if_needed()
self.load_template_if_needed()
target = File(self.site.config.deploy_root_path.child(
resource.relative_deploy_path))
if not target.exists or target.older_than(resource.source_file):
logger.debug("Found changes in %s" % resource)
return True
if resource.source_file.is_binary:
logger.debug("No Changes found in %s" % resource)
return False
deps = self.get_dependencies(resource)
if not deps or None in deps:
logger.debug("No changes found in %s" % resource)
return False
content = self.site.content.source_folder
layout = Folder(self.site.sitepath).child_folder('layout')
logger.debug("Checking for changes in dependents:%s" % deps)
for dep in deps:
if not dep:
return True
source = File(content.child(dep))
if not source.exists:
source = File(layout.child(dep))
if not source.exists:
return True
if target.older_than(source):
return True
logger.debug("No changes found in %s" % resource)
return False
示例3: begin_site
# 需要导入模块: from hyde.fs import Folder [as 别名]
# 或者: from hyde.fs.Folder import child [as 别名]
def begin_site(self):
"""
Finds all the folders that need flattening and changes the
relative deploy path of all resources in those folders.
"""
items = []
try:
items = self.site.config.flattener.items
except AttributeError:
pass
for item in items:
node = None
target = ''
try:
node = self.site.content.node_from_relative_path(item.source)
target = Folder(item.target)
except AttributeError:
continue
if node:
for resource in node.walk_resources():
target_path = target.child(resource.name)
self.logger.debug(
'Flattening resource path[%s] to [%s]' %
(resource, target_path))
resource.relative_deploy_path = target_path
示例4: Dependents
# 需要导入模块: from hyde.fs import Folder [as 别名]
# 或者: from hyde.fs.Folder import child [as 别名]
class Dependents(IterableUserDict):
"""
Represents the dependency graph for hyde.
"""
def __init__(self, sitepath, depends_file_name='.hyde_deps'):
self.sitepath = Folder(sitepath)
self.deps_file = File(self.sitepath.child(depends_file_name))
self.data = {}
if self.deps_file.exists:
self.data = yaml.load(self.deps_file.read_all())
import atexit
atexit.register(self.save)
def save(self):
"""
Saves the dependency graph (just a dict for now).
"""
if self.deps_file.parent.exists:
self.deps_file.write(yaml.dump(self.data))
示例5: serve
# 需要导入模块: from hyde.fs import Folder [as 别名]
# 或者: from hyde.fs.Folder import child [as 别名]
def serve(self, args):
"""
The serve command. Serves the site at the given
deployment directory, address and port. Regenerates
the entire site or specific files based on ths request.
"""
self.main(args)
sitepath = Folder(Folder(args.sitepath).fully_expanded_path)
config_file = sitepath.child(args.config)
site = self.make_site(args.sitepath, args.config, args.deploy)
from hyde.server import HydeWebServer
server = HydeWebServer(site, args.address, args.port)
logger.info("Starting webserver at [%s]:[%d]", args.address, args.port)
try:
server.serve_forever()
except KeyboardInterrupt, SystemExit:
logger.info("Received shutdown request. Shutting down...")
server.shutdown()
logger.info("Server successfully stopped")
exit()
示例6: Config
# 需要导入模块: from hyde.fs import Folder [as 别名]
# 或者: from hyde.fs.Folder import child [as 别名]
class Config(Expando):
"""
Represents the hyde configuration file
"""
def __init__(self, sitepath, config_file=None, config_dict=None):
self.default_config = dict(
mode='production',
simple_copy = [],
content_root='content',
deploy_root='deploy',
media_root='media',
layout_root='layout',
media_url='/media',
base_url="/",
not_found='404.html',
plugins = [],
ignore = [ "*~", "*.bak", ".hg", ".git", ".svn"],
meta = {
"nodemeta": 'meta.yaml'
}
)
self.config_file = config_file
self.config_dict = config_dict
self.load_time = datetime.min
self.config_files = []
self.sitepath = Folder(sitepath)
super(Config, self).__init__(self.load())
@property
def last_modified(self):
return max((conf.last_modified for conf in self.config_files))
def needs_refresh(self):
if not self.config_files:
return True
return any((conf.has_changed_since(self.load_time)
for conf in self.config_files))
def load(self):
conf = dict(**self.default_config)
conf.update(self.read_config(self.config_file))
if self.config_dict:
conf.update(self.config_dict)
return conf
def reload(self):
if not self.config_file:
return
self.update(self.load())
def read_config(self, config_file):
"""
Reads the configuration file and updates this
object while allowing for inherited configurations.
"""
conf_file = self.sitepath.child(
config_file if
config_file else 'site.yaml')
conf = {}
if File(conf_file).exists:
self.config_files.append(File(conf_file))
logger.info("Reading site configuration from [%s]", conf_file)
with codecs.open(conf_file, 'r', 'utf-8') as stream:
conf = yaml.load(stream)
if 'extends' in conf:
parent = self.read_config(conf['extends'])
parent.update(conf)
conf = parent
self.load_time = datetime.now()
return conf
@property
def deploy_root_path(self):
"""
Derives the deploy root path from the site path
"""
return self.sitepath.child_folder(self.deploy_root)
@property
def content_root_path(self):
"""
Derives the content root path from the site path
"""
return self.sitepath.child_folder(self.content_root)
@property
def media_root_path(self):
"""
Derives the media root path from the content path
"""
return self.content_root_path.child_folder(self.media_root)
@property
def layout_root_path(self):
"""
Derives the layout root path from the site path
"""
#.........这里部分代码省略.........
示例7: Config
# 需要导入模块: from hyde.fs import Folder [as 别名]
# 或者: from hyde.fs.Folder import child [as 别名]
class Config(Expando):
"""
Represents the hyde configuration file
"""
def __init__(self, sitepath, config_file=None, config_dict=None):
default_config = dict(
content_root='content',
deploy_root='deploy',
media_root='media',
layout_root='layout',
media_url='/media',
site_url='/',
not_found='404.html',
plugins = []
)
conf = dict(**default_config)
self.sitepath = Folder(sitepath)
conf.update(self.read_config(config_file))
if config_dict:
conf.update(config_dict)
super(Config, self).__init__(conf)
def read_config(self, config_file):
"""
Reads the configuration file and updates this
object while allowing for inherited configurations.
"""
conf_file = self.sitepath.child(
config_file if
config_file else 'site.yaml')
conf = {}
if File(conf_file).exists:
logger.info("Reading site configuration from [%s]", conf_file)
with codecs.open(conf_file, 'r', 'utf-8') as stream:
conf = yaml.load(stream)
if 'extends' in conf:
parent = self.read_config(conf['extends'])
parent.update(conf)
conf = parent
return conf
@property
def deploy_root_path(self):
"""
Derives the deploy root path from the site path
"""
return self.sitepath.child_folder(self.deploy_root)
@property
def content_root_path(self):
"""
Derives the content root path from the site path
"""
return self.sitepath.child_folder(self.content_root)
@property
def media_root_path(self):
"""
Derives the media root path from the site path
"""
return self.sitepath.child_folder(self.media_root)
@property
def layout_root_path(self):
"""
Derives the layout root path from the site path
"""
return self.sitepath.child_folder(self.layout_root)
示例8: Node
# 需要导入模块: from hyde.fs import Folder [as 别名]
# 或者: from hyde.fs.Folder import child [as 别名]
class Node(Processable):
"""
Represents any folder that is processed by hyde
"""
def __init__(self, source_folder, parent=None):
super(Node, self).__init__(source_folder)
if not source_folder:
raise HydeException("Source folder is required"
" to instantiate a node.")
self.root = self
self.module = None
self.site = None
self.source_folder = Folder(unicode(source_folder))
self.parent = parent
if parent:
self.root = self.parent.root
self.module = self.parent.module if self.parent.module else self
self.site = parent.site
self.child_nodes = []
self.resources = []
def contains_resource(self, resource_name):
"""
Returns True if the given resource name exists as a file
in this node's source folder.
"""
return File(self.source_folder.child(resource_name)).exists
def get_resource(self, resource_name):
"""
Gets the resource if the given resource name exists as a file
in this node's source folder.
"""
if self.contains_resource(resource_name):
return self.root.resource_from_path(
self.source_folder.child(resource_name))
return None
def add_child_node(self, folder):
"""
Creates a new child node and adds it to the list of child nodes.
"""
if folder.parent != self.source_folder:
raise HydeException("The given folder [%s] is not a"
" direct descendant of [%s]" %
(folder, self.source_folder))
node = Node(folder, self)
self.child_nodes.append(node)
return node
def add_child_resource(self, afile):
"""
Creates a new resource and adds it to the list of child resources.
"""
if afile.parent != self.source_folder:
raise HydeException("The given file [%s] is not"
" a direct descendant of [%s]" %
(afile, self.source_folder))
resource = Resource(afile, self)
self.resources.append(resource)
return resource
def walk(self):
"""
Walks the node, first yielding itself then
yielding the child nodes depth-first.
"""
yield self
for child in self.child_nodes:
for node in child.walk():
yield node
def walk_resources(self):
"""
Walks the resources in this hierarchy.
"""
for node in self.walk():
for resource in node.resources:
yield resource
@property
def relative_path(self):
"""
Gets the path relative to the root folder (Content, Media, Layout)
"""
return self.source_folder.get_relative_path(self.root.source_folder)
@property
def url(self):
return '/' + self.relative_path
@property
def full_url(self):
return self.site.full_url(self.relative_path)
示例9: SphinxGenerator
# 需要导入模块: from hyde.fs import Folder [as 别名]
# 或者: from hyde.fs.Folder import child [as 别名]
class SphinxGenerator(object):
def __init__(self, node, settings):
self.__node = node
self.__config = None
self.__build_dir = None
self.__settings = settings
self.__layout = self.__settings.get("layout", "sphinx.j2")
def node(self):
return self.__node
def update_meta(self):
self.__node.meta.update({"sphinx_project_name": self.config().get("project")})
def config(self):
if self.__config is None:
# Sphinx always execs the config file in its parent dir.
conf_file = os.path.join(self.__node.path, "conf.py")
self.__config = {"__file__": conf_file}
curdir = os.getcwd()
os.chdir(self.__node.path)
try:
execfile(conf_file, self.__config)
finally:
os.chdir(curdir)
return self.__config
def fix_generated_filenames(self):
suffix = self.config().get("source_suffix", ".rst")
for resource in self.__node.walk_resources():
if resource.source_file.path.endswith(suffix):
new_name = resource.source_file.name_without_extension + ".html"
target_folder = File(resource.relative_deploy_path).parent
resource.relative_deploy_path = target_folder.child(new_name)
if self.__settings.get("block_map", None):
resource.meta.default_block = None
def generate_resource(self, resource, text):
suffix = self.config().get("source_suffix", ".rst")
if not resource.source_file.path.endswith(suffix):
return "".join(("{% raw %}", text, "{% endraw %}"))
if self.__build_dir is None:
self.__run_sphinx()
output = []
sphinx_output = self.__get_sphinx_output(resource)
for name, content in sphinx_output.iteritems():
if name in ("body", "toc"):
output.append("{{% block {0} %}}{{% raw %}}".format(name))
output.append(content)
output.append("{% endraw %}{% endblock %}")
self.__update_metadata(resource, {
"sphinx": sphinx_output,
"extends": self.__layout,
"doc_root_url": self.node().get_relative_deploy_path(),
"sphinx_project_name": self.config().get("project")
})
return "".join(output)
def clean(self):
if self.__build_dir is not None:
self.__build_dir.delete()
def __run_sphinx(self):
"""Run sphinx to generate the necessary output files.
This method creates a temporary directory for sphinx's output, then
run sphinx against the Hyde input directory.
"""
logger.info("running sphinx")
self.__build_dir = Folder(tempfile.mkdtemp())
sphinx_app = Sphinx(
self.node().path,
self.node().path,
self.__build_dir.path,
self.node().path,
"json"
)
sphinx_app.add_builder(HydeJSONHTMLBuilder)
sphinx_app._init_builder("hyde_json")
sphinx_app.build()
def __get_sphinx_output(self, resource):
"""Get the sphinx output for a given resource.
This returns a dict mapping block names to HTML text fragments.
The most important fragment is "body" which contains the main text
of the document. The other fragments are for things like navigation,
related pages and so-on.
"""
relpath = File(resource.relative_path)
relpath = relpath.parent.child(relpath.name_without_extension + ".fjson")[len(self.node().relative_path) + 1:]
with open(self.__build_dir.child(relpath), "rb") as f:
return json.load(f)
def __update_metadata(self, resource, metadata):
if not hasattr(resource, 'meta') or not resource.meta:
if not hasattr(resource.node, 'meta'):
resource.node.meta = Metadata({})
resource.meta = Metadata(metadata, resource.node.meta)
#.........这里部分代码省略.........
示例10: SphinxPlugin
# 需要导入模块: from hyde.fs import Folder [as 别名]
# 或者: from hyde.fs.Folder import child [as 别名]
class SphinxPlugin(Plugin):
"""The plugin class for rendering sphinx-generated documentation."""
def __init__(self, site):
self.sphinx_build_dir = None
self._sphinx_config = None
super(SphinxPlugin, self).__init__(site)
@property
def plugin_name(self):
"""The name of the plugin, obivously."""
return "sphinx"
@property
def settings(self):
"""Settings for this plugin.
This property combines default settings with those specified in the
site config to produce the final settings for this plugin.
"""
settings = Expando({})
settings.sanity_check = True
settings.conf_path = "."
settings.block_map = {}
try:
user_settings = getattr(self.site.config, self.plugin_name)
except AttributeError:
pass
else:
for name in dir(user_settings):
if not name.startswith("_"):
setattr(settings,name,getattr(user_settings,name))
return settings
@property
def sphinx_config(self):
"""Configuration options for sphinx.
This is a lazily-generated property giving the options from the
sphinx configuration file. It's generated by actualy executing
the config file, so don't do anything silly in there.
"""
if self._sphinx_config is None:
conf_path = self.settings.conf_path
conf_path = self.site.sitepath.child_folder(conf_path)
# Sphinx always execs the config file in its parent dir.
conf_file = conf_path.child("conf.py")
self._sphinx_config = {"__file__":conf_file}
curdir = os.getcwd()
os.chdir(conf_path.path)
try:
execfile(conf_file,self._sphinx_config)
finally:
os.chdir(curdir)
return self._sphinx_config
def begin_site(self):
"""Event hook for when site processing begins.
This hook checks that the site is correctly configured for building
with sphinx, and adjusts any sphinx-controlled resources so that
hyde will process them correctly.
"""
settings = self.settings
if settings.sanity_check:
self._sanity_check()
# Find and adjust all the resource that will be handled by sphinx.
# We need to:
# * change the deploy name from .rst to .html
# * if a block_map is given, switch off default_block
suffix = self.sphinx_config.get("source_suffix",".rst")
for resource in self.site.content.walk_resources():
if resource.source_file.path.endswith(suffix):
new_name = resource.source_file.name_without_extension + ".html"
target_folder = File(resource.relative_deploy_path).parent
resource.relative_deploy_path = target_folder.child(new_name)
if settings.block_map:
resource.meta.default_block = None
def begin_text_resource(self,resource,text):
"""Event hook for processing an individual resource.
If the input resource is a sphinx input file, this method will replace
replace the text of the file with the sphinx-generated documentation.
Sphinx itself is run lazily the first time this method is called.
This means that if no sphinx-related resources need updating, then
we entirely avoid running sphinx.
"""
suffix = self.sphinx_config.get("source_suffix",".rst")
if not resource.source_file.path.endswith(suffix):
return text
if self.sphinx_build_dir is None:
self._run_sphinx()
output = []
settings = self.settings
sphinx_output = self._get_sphinx_output(resource)
# If they're set up a block_map, use the specific blocks.
# Otherwise, output just the body for use by default_block.
if not settings.block_map:
#.........这里部分代码省略.........