New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-95472: [xml.etree.ElementTree] Add fine-grained formatting classes #95476
base: main
Are you sure you want to change the base?
Conversation
Most changes to Python require a NEWS entry. Please add it using the blurb_it web app or the blurb command-line tool. |
* ShortEmptyElements — make it possible to remove space between end of tag and slash, also make it possible to turn this on and off based on tag being processed via `defaultdict`. * XMLDeclarationQuotes — change quote char used in XML declaration from `'` to `"`.
fc3ba6b
to
5c159e1
Compare
Most changes to Python require a NEWS entry. Please add it using the blurb_it web app or the blurb command-line tool. |
Co-Authored-By: Fredrick Brennan <copypaste@kittens.ph>
c857de4
to
fb47e0d
Compare
write("<?xml version={0}1.0{0} encoding={0}{1}{0}?>\n" | ||
.format(xml_declaration_quotes, declared_encoding)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like we don't need an enum at all but can just pass a string containing the quote characters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, that is true!
@@ -885,7 +951,7 @@ def _serialize_xml(write, elem, qnames, namespaces, | |||
else: | |||
v = _escape_attrib(v) | |||
write(" %s=\"%s\"" % (qnames[k], v)) | |||
if text or len(elem) or not short_empty_elements: | |||
if text or len(elem) or not bool(short_empty_elements[tag]): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bool()
isn't needed inside of conditions.
if text or len(elem) or not bool(short_empty_elements[tag]): | |
if text or len(elem) or not short_empty_elements[tag]: |
if not isinstance(short_empty_elements, collections.defaultdict): | ||
if isinstance(short_empty_elements, ShortEmptyElements): | ||
return collections.defaultdict(lambda: short_empty_elements) | ||
elif bool(short_empty_elements) is True: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what the intention is here, but the spelling is quite redundant.
elif bool(short_empty_elements) is True: | |
elif short_empty_elements: |
return self.value | ||
|
||
class ShortEmptyElements(enum.Enum): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return self.value | |
class ShortEmptyElements(enum.Enum): | |
return self.value | |
class ShortEmptyElements(enum.Enum): |
|
||
class XMLDeclarationQuotes(enum.Enum): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class XMLDeclarationQuotes(enum.Enum): | |
class XMLDeclarationQuotes(enum.Enum): |
return short_empty_elements | ||
|
||
class ElementTree: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return short_empty_elements | |
class ElementTree: | |
return short_empty_elements | |
class ElementTree: |
if not isinstance(short_empty_elements, collections.defaultdict): | ||
if isinstance(short_empty_elements, ShortEmptyElements): | ||
return collections.defaultdict(lambda: short_empty_elements) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would also allow mappings that implement __missing__
. Therefore, I suggest checking for everything else and assuming that what remains is probably some kind of acceptable dict. Or check for collections.abc.Mapping
instead.
The feature seems good in general. |
tag and slash, also make it possible to turn this on and off based on
tag being processed via
defaultdict
.'
to"
.Closes #95472.