本文整理汇总了Python中lxml.etree.XML.tail方法的典型用法代码示例。如果您正苦于以下问题:Python XML.tail方法的具体用法?Python XML.tail怎么用?Python XML.tail使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lxml.etree.XML
的用法示例。
在下文中一共展示了XML.tail方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: install
# 需要导入模块: from lxml.etree import XML [as 别名]
# 或者: from lxml.etree.XML import tail [as 别名]
def install(project_path, server_xml_location, server_xml, server_name, lib_path, lib_name, app_name, mount=None):
from lxml.etree import fromstring, ElementTree, parse
from lxml.etree import XML, Comment
changes = 0
with fsopendir(project_path) as project_fs:
with project_fs.opendir(server_xml_location) as server_fs:
with server_fs.open(server_xml, 'rb') as server_xml_file:
root = parse(server_xml_file)
import_tag = XML('<import location="{lib_path}"/>'.format(lib_path=lib_path))
import_tag.tail = "\n"
install_tag = XML('<install name="{app_name}" lib="{lib_name}" />'.format(app_name=app_name, lib_name=lib_name, mount=mount))
install_tag.tail = "\n"
def has_child(node, tag, **attribs):
for el in node.findall(tag):
if all(el.get(k, None) == v for k, v in attribs.items()):
return True
return False
server_el = "{{http://moyaproject.com}}server[@docname='{}']".format(server_name)
for server in root.findall(server_el):
def get_comment():
comment = Comment('added by moya-pm')
return comment
if not has_child(server, "{http://moyaproject.com}import", location=lib_path):
server.insert(0, import_tag)
server.insert(0, get_comment())
changes += 1
if not has_child(server, "{http://moyaproject.com}install", lib=lib_name):
server.append(Comment('added by moya-pm'))
server.append(install_tag)
changes += 1
if mount is not None and app_name is not None:
if not has_child(server, "{http://moyaproject.com}mount", app_name=app_name):
mount_tag = XML('<mount app="{app_name}" url="{mount}" />'.format(app_name=app_name, mount=mount))
mount_tag.tail = '\n'
server.append(get_comment())
server.append(mount_tag)
changes += 1
with open_atomic_write(server_fs, server_xml, 'wb') as server_xml_file:
root.write(server_xml_file)
return bool(changes)
示例2: start_library
# 需要导入模块: from lxml.etree import XML [as 别名]
# 或者: from lxml.etree.XML import tail [as 别名]
def start_library(self):
console = self.console
from ...tools import get_moya_dir
from os.path import join, abspath
project_path = None
if self.args.location is not None:
library_path = self.args.location
else:
try:
project_path = get_moya_dir(self.args.project_location)
except:
console.error("Please run 'moya start library' inside your project directory, or specifiy the -o switch")
return False
library_path = abspath(join(project_path, './local/'))
cfg = None
if not self.args.location and project_path:
from ... import build
cfg = build.read_config(project_path, self.get_settings())
if not self.args.acceptdefaults:
console.table([[Cell("Moya Library Wizard", bold=True, fg="green", center=True)],
["""This will ask you a few questions, then create a new library in your Moya project based on your answers.
Default values are shown in grey (simply hit return to accept defaults). Some defaults may be taken from your ".bashrc" file, if it exists.
"""]])
author = self.get_author_details()
library = {}
library["title"] = LibraryTitle.ask(console, default=self.args.title)
longname = self.args.longname or make_name(author["organization"], library["title"])
longname = library["longname"] = LibraryLongName.ask(console, default=longname)
library["url"] = LibraryURL.ask(console, default="")
library["namespace"] = LibraryNamespace.ask(console, default="")
mount = None
appname = None
do_mount = DoMount.ask(console, default="yes")
if do_mount:
mount = Mount.ask(console, default=self.args.mount or "/{}/".format(make_name(library["title"])))
appname = AppName.ask(console, default=self.args.name or make_name(library["title"]))
data = dict(author=author,
library=library,
timezone=self.get_timezone())
actions = []
from ...command.sub import library_template
from fs.memoryfs import MemoryFS
from fs.opener import fsopendir
memfs = MemoryFS()
templatebuilder.compile_fs_template(memfs,
library_template.template,
data=data)
dest_fs = fsopendir(join(library_path, library["longname"]), create_dir=True, writeable=True)
continue_overwrite = 'overwrite'
if not dest_fs.isdirempty('.'):
if self.args.force:
continue_overwrite = 'overwrite'
elif self.args.new:
continue_overwrite = 'new'
else:
continue_overwrite = DirNotEmpty.ask(console, default="cancel")
if continue_overwrite != 'cancel':
if continue_overwrite == 'overwrite':
from fs.utils import copydir
copydir(memfs, dest_fs)
actions.append("Written library files to {}".format(dest_fs.getsyspath('.')))
elif continue_overwrite == 'new':
files_copied = copy_new(memfs, dest_fs)
table = [[
Cell("{} new file(s) written".format(len(files_copied)), fg="green", bold=True, center=True),
]]
for path in files_copied:
table.append([Cell(dest_fs.desc(path), bold=True, fg="black")])
console.table(table)
return 0
if cfg:
project_cfg = cfg['project']
location = project_cfg['location']
server_name = "main"
if location:
with fsopendir(project_path) as project_fs:
with project_fs.opendir(location) as server_fs:
from lxml.etree import fromstring, ElementTree, parse
from lxml.etree import XML, Comment
server_xml_path = server_fs.getsyspath(project_cfg['startup'])
root = parse(server_xml_path)
import_tag = XML('<import location="./local/{longname}" />\n\n'.format(**library))
import_tag.tail = "\n"
install_tag = None
if mount:
tag = '<install name="{appname}" lib="{longname}" mount="{mount}" />'
else:
#.........这里部分代码省略.........