Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ctrlcctrlv
Copy link

@ctrlcctrlv ctrlcctrlv commented Jul 30, 2022

  • 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 ".

Closes #95472.

@cpython-cla-bot
Copy link

@cpython-cla-bot cpython-cla-bot bot commented Jul 30, 2022

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented Jul 30, 2022

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 `"`.
@ctrlcctrlv ctrlcctrlv force-pushed the Issue№95472_MergeMain branch from fc3ba6b to 5c159e1 Compare Jul 30, 2022
@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented Jul 30, 2022

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>
@ctrlcctrlv ctrlcctrlv force-pushed the Issue№95472_MergeMain branch from c857de4 to fb47e0d Compare Jul 30, 2022
@tirkarthi tirkarthi requested a review from scoder Jul 31, 2022
write("<?xml version={0}1.0{0} encoding={0}{1}{0}?>\n"
.format(xml_declaration_quotes, declared_encoding))
Copy link
Contributor

@scoder scoder Aug 3, 2022

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.

Copy link
Author

@ctrlcctrlv ctrlcctrlv Aug 4, 2022

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]):
Copy link
Contributor

@scoder scoder Aug 3, 2022

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.

Suggested change
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:
Copy link
Contributor

@scoder scoder Aug 3, 2022

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.

Suggested change
elif bool(short_empty_elements) is True:
elif short_empty_elements:

return self.value

class ShortEmptyElements(enum.Enum):
Copy link
Contributor

@scoder scoder Aug 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return self.value
class ShortEmptyElements(enum.Enum):
return self.value
class ShortEmptyElements(enum.Enum):


class XMLDeclarationQuotes(enum.Enum):
Copy link
Contributor

@scoder scoder Aug 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class XMLDeclarationQuotes(enum.Enum):
class XMLDeclarationQuotes(enum.Enum):

return short_empty_elements

class ElementTree:
Copy link
Contributor

@scoder scoder Aug 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)
Copy link
Contributor

@scoder scoder Aug 3, 2022

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.

@scoder
Copy link
Contributor

@scoder scoder commented Aug 3, 2022

The feature seems good in general.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants