-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fallback to native etree implementation
- Loading branch information
Showing
6 changed files
with
80 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# -*- coding: utf-8 -*- | ||
from tortik.logger import tortik_log | ||
|
||
native_etree = False | ||
|
||
try: | ||
from lxml import etree | ||
except ImportError: | ||
tortik_log.info('lxml not installed. Using native etree implementation') | ||
native_etree = True | ||
import xml.etree.ElementTree as etree | ||
|
||
|
||
def parse(source, parser=None): | ||
if parser is None and native_etree is False: | ||
parser = etree.XMLParser(remove_blank_text=True) | ||
|
||
return etree.parse(source, parser) | ||
|
||
|
||
def tostring(element, **kwargs): | ||
native_args = ['encoding', 'method'] | ||
if native_etree is True: | ||
pass_args = dict() | ||
for a in native_args: | ||
if a in kwargs: | ||
pass_args[a] = kwargs[a] | ||
else: | ||
pass_args = kwargs | ||
|
||
return etree.tostring(element, **pass_args) | ||
|
||
|
||
ParseError = etree.ParseError |
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