本文整理汇总了Python中sfa.rspecs.version_manager.VersionManager.get_version_by_schema方法的典型用法代码示例。如果您正苦于以下问题:Python VersionManager.get_version_by_schema方法的具体用法?Python VersionManager.get_version_by_schema怎么用?Python VersionManager.get_version_by_schema使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sfa.rspecs.version_manager.VersionManager
的用法示例。
在下文中一共展示了VersionManager.get_version_by_schema方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from sfa.rspecs.version_manager import VersionManager [as 别名]
# 或者: from sfa.rspecs.version_manager.VersionManager import get_version_by_schema [as 别名]
class RSpec:
def __init__(self, rspec="", version=None, user_options={}):
self.header = '<?xml version="1.0"?>\n'
self.template = """<RSpec></RSpec>"""
self.version = None
self.xml = XML()
self.version_manager = VersionManager()
self.user_options = user_options
self.elements = {}
if rspec:
if version:
self.version = self.version_manager.get_version(version)
self.parse_xml(rspec, version)
else:
self.parse_xml(rspec)
elif version:
self.create(version)
else:
raise InvalidRSpec("No RSpec or version specified. Must specify a valid rspec string or a valid version")
def create(self, version=None):
"""
Create root element
"""
self.version = self.version_manager.get_version(version)
self.namespaces = self.version.namespaces
self.parse_xml(self.version.template, self.version)
# eg. 2011-03-23T19:53:28Z
date_format = "%Y-%m-%dT%H:%M:%SZ"
now = datetime.utcnow()
generated_ts = now.strftime(date_format)
expires_ts = (now + timedelta(hours=1)).strftime(date_format)
self.xml.set("expires", expires_ts)
self.xml.set("generated", generated_ts)
def parse_xml(self, xml, version=None):
self.xml.parse_xml(xml)
if not version:
if self.xml.schema:
self.version = self.version_manager.get_version_by_schema(self.xml.schema)
else:
# raise InvalidRSpec('unknown rspec schema: %s' % schema)
# TODO: Should start raising an exception once SFA defines a schema.
# for now we just default to sfa
self.version = self.version_manager.get_version({"type": "sfa", "version": "1"})
self.version.xml = self.xml
self.namespaces = self.xml.namespaces
def load_rspec_elements(self, rspec_elements):
self.elements = {}
for rspec_element in rspec_elements:
if isinstance(rspec_element, RSpecElement):
self.elements[rspec_element.type] = rspec_element
def register_rspec_element(self, element_type, element_name, element_path):
if element_type not in RSpecElements:
raise InvalidRSpecElement(
element_type, extra="no such element type: %s. Must specify a valid RSpecElement" % element_type
)
self.elements[element_type] = RSpecElement(element_type, element_name, element_path)
def get_rspec_element(self, element_type):
if element_type not in self.elements:
msg = "ElementType %s not registerd for this rspec" % element_type
raise InvalidRSpecElement(element_type, extra=msg)
return self.elements[element_type]
def get(self, element_type, filter={}, depth=0):
elements = self.get_elements(element_type, filter)
elements = [self.xml.get_element_attributes(elem, depth=depth) for elem in elements]
return elements
def get_elements(self, element_type, filter={}):
"""
search for a registered element
"""
if element_type not in self.elements:
msg = "Unable to search for element %s in rspec, expath expression not found." % element_type
raise InvalidRSpecElement(element_type, extra=msg)
rspec_element = self.get_rspec_element(element_type)
xpath = rspec_element.path + XpathFilter.xpath(filter)
return self.xml.xpath(xpath)
def merge(self, in_rspec):
self.version.merge(in_rspec)
def filter(self, filter):
if "component_manager_id" in filter:
nodes = self.version.get_node_elements()
for node in nodes:
if (
"component_manager_id" not in node.attrib
or node.attrib["component_manager_id"] != filter["component_manager_id"]
):
parent = node.getparent()
parent.remove(node)
def toxml(self, header=True):
if header:
return self.header + self.xml.toxml()
#.........这里部分代码省略.........
示例2: __init__
# 需要导入模块: from sfa.rspecs.version_manager import VersionManager [as 别名]
# 或者: from sfa.rspecs.version_manager.VersionManager import get_version_by_schema [as 别名]
class RSpec:
def __init__(self, rspec="", version=None, user_options=None, ttl=None, expires=None):
if user_options is None: user_options={}
self.header = '<?xml version="1.0"?>\n'
self.template = """<RSpec></RSpec>"""
self.version = None
self.xml = XML()
self.version_manager = VersionManager()
self.user_options = user_options
self.ttl = ttl
self.expires = expires
self.elements = {}
if rspec:
if version:
self.version = self.version_manager.get_version(version)
self.parse_xml(rspec, version)
else:
self.parse_xml(rspec)
elif version:
self.create(version, ttl, expires)
else:
raise InvalidRSpec("No RSpec or version specified. Must specify a valid rspec string or a valid version")
def create(self, version=None, ttl=None, expires=None):
"""
Create root element
ttl: time to live in minutes, this will determine the expires tag of the RSpec
"""
self.version = self.version_manager.get_version(version)
self.namespaces = self.version.namespaces
self.parse_xml(self.version.template, self.version)
now = datetime.utcnow()
generated_ts = now.strftime(SFATIME_FORMAT)
if ttl is None:
ttl = 60
if expires is None:
expires_ts = (now + timedelta(minutes=ttl)).strftime(SFATIME_FORMAT)
else:
if isinstance(expires,int):
expires_date = datetime.fromtimestamp(expires)
else:
expires_date = expires
expires_ts = expires_date.strftime(SFATIME_FORMAT)
self.xml.set('expires', expires_ts)
self.xml.set('generated', generated_ts)
def parse_xml(self, xml, version=None):
self.xml.parse_xml(xml)
if not version:
if self.xml.schema:
self.version = self.version_manager.get_version_by_schema(self.xml.schema)
else:
#raise InvalidRSpec('unknown rspec schema: %s' % schema)
# TODO: Should start raising an exception once SFA defines a schema.
# for now we just default to sfa
self.version = self.version_manager.get_version({'type':'sfa','version': '1'})
self.version.xml = self.xml
self.namespaces = self.xml.namespaces
def load_rspec_elements(self, rspec_elements):
self.elements = {}
for rspec_element in rspec_elements:
if isinstance(rspec_element, RSpecElement):
self.elements[rspec_element.type] = rspec_element
def register_rspec_element(self, element_type, element_name, element_path):
if element_type not in RSpecElements:
raise InvalidRSpecElement(element_type, extra="no such element type: %s. Must specify a valid RSpecElement" % element_type)
self.elements[element_type] = RSpecElement(element_type, element_name, element_path)
def get_rspec_element(self, element_type):
if element_type not in self.elements:
msg = "ElementType %s not registerd for this rspec" % element_type
raise InvalidRSpecElement(element_type, extra=msg)
return self.elements[element_type]
def get(self, element_type, filter=None, depth=0):
if filter is None: filter={}
elements = self.get_elements(element_type, filter)
elements = [self.xml.get_element_attributes(elem, depth=depth) for elem in elements]
return elements
def get_elements(self, element_type, filter=None):
"""
search for a registered element
"""
if filter is None: filter={}
if element_type not in self.elements:
msg = "Unable to search for element %s in rspec, expath expression not found." % \
element_type
raise InvalidRSpecElement(element_type, extra=msg)
rspec_element = self.get_rspec_element(element_type)
xpath = rspec_element.path + XpathFilter.xpath(filter)
return self.xml.xpath(xpath)
def merge(self, in_rspec):
self.version.merge(in_rspec)
def filter(self, filter):
#.........这里部分代码省略.........