Created
May 20, 2015 07:45
-
-
Save pavelpatrin/6b201b72b529f7f0fb8b to your computer and use it in GitHub Desktop.
Нерабочий вариант функции для схлопывания wsdl-схем
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _get_flatten_wsdl(self, url): | |
""" | |
Выполняет загрузку wsdl-схемы и зависимых файлов и объединяет их | |
в единый файл. | |
Ускоряет загрузку wsdl-схемы для клиента. | |
TODO: реализовать. | |
:type url: unicode | |
:rtype: lxml.etree._Element._Element | |
""" | |
root = etree.fromstring(get(url).content) | |
cache = {} | |
def flatten_wsdl_imports(root): | |
""" | |
В настоящий момент не ясно как именно производить слияние для | |
wsdl:import элементов. Так же не ясно, используются ли они в СМЭВ. | |
:type root: lxml.etree._Element._Element | |
""" | |
if root.xpath('//wsdl:import', namespaces=self.namespaces): | |
raise ProxifierException('WSDL imports are not supported.') | |
def flatten_xsd_imports(root): | |
""" | |
:type root: lxml.etree._Element._Element | |
""" | |
for xsd_import in root.xpath( | |
'//xsd:import[@schemaLocation]', namespaces=self.namespaces | |
): | |
xsd_import_url = xsd_import.get('schemaLocation') | |
if xsd_import_url not in cache: | |
cache[xsd_import_url] = get(xsd_import_url).content | |
# Подгруженная замена элемента xsd:import. | |
xsd_import_root = etree.fromstring(cache[xsd_import_url]) | |
# Cхема может содержать ссылки на другие схемы. | |
flatten_xsd_imports(xsd_import_root) | |
flatten_xsd_includes(xsd_import_root) | |
xsd_import.getparent().replace(xsd_import, xsd_import_root) | |
def flatten_xsd_includes(root): | |
""" | |
:type root: lxml.etree._Element._Element | |
""" | |
for xsd_include in root.xpath( | |
'//xsd:include[@schemaLocation]', namespaces=self.namespaces | |
): | |
xsd_import_url = xsd_include.get('schemaLocation') | |
if xsd_import_url not in cache: | |
cache[xsd_import_url] = get(xsd_import_url).content | |
# Подгруженная замена элемента xsd:include. | |
xsd_import_root = etree.fromstring(cache[xsd_import_url]) | |
# Схема может содержать ссылки на другие схемы. | |
flatten_xsd_imports(xsd_import_root) | |
flatten_xsd_includes(xsd_import_root) | |
xsd_include.getparent().replace(xsd_include, xsd_import_root) | |
flatten_wsdl_imports(root) | |
flatten_xsd_imports(root) | |
flatten_xsd_includes(root) | |
return root |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment