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

Feature/xml generator #1862

Merged
merged 10 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions faker/providers/misc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import csv

Check failure on line 1 in faker/providers/misc/__init__.py

View workflow job for this annotation

GitHub Actions / isort

Imports are incorrectly sorted and/or formatted.
import hashlib
import io
import json
Expand All @@ -13,6 +13,7 @@

from faker.exceptions import UnsupportedFeature

from ..python import TypesSpec
from .. import BaseProvider

localized = True
Expand Down Expand Up @@ -613,6 +614,30 @@
data = [create_json_structure(data_columns) for _ in range(num_rows)]
return json.dumps(data, indent=indent, cls=cls)

def xml(self,
nb_elements: int = 10,

Check failure on line 618 in faker/providers/misc/__init__.py

View workflow job for this annotation

GitHub Actions / flake8

continuation line under-indented for visual indent
variable_nb_elements: bool = True,

Check failure on line 619 in faker/providers/misc/__init__.py

View workflow job for this annotation

GitHub Actions / flake8

continuation line under-indented for visual indent
value_types: Optional[TypesSpec] = None,

Check failure on line 620 in faker/providers/misc/__init__.py

View workflow job for this annotation

GitHub Actions / flake8

continuation line under-indented for visual indent
allowed_types: Optional[TypesSpec] = None,

Check failure on line 621 in faker/providers/misc/__init__.py

View workflow job for this annotation

GitHub Actions / flake8

continuation line under-indented for visual indent
) -> str:

Check failure on line 622 in faker/providers/misc/__init__.py

View workflow job for this annotation

GitHub Actions / flake8

closing bracket does not match visual indentation

Check failure on line 622 in faker/providers/misc/__init__.py

View workflow job for this annotation

GitHub Actions / flake8

continuation line with same indent as next logical line
"""
Returns a xml.
Elihayb marked this conversation as resolved.
Show resolved Hide resolved
Elihayb marked this conversation as resolved.
Show resolved Hide resolved

:nb_elements: number of elements for dictionary
:variable_nb_elements: is use variable number of elements for dictionary
:value_types: type of dictionary values

Note: this provider required xmltodict library installed
"""
try:
import xmltodict
except ImportError:
raise UnsupportedFeature("`xml` requires the `xmltodict` python library.", "xml")
Elihayb marked this conversation as resolved.
Show resolved Hide resolved
Elihayb marked this conversation as resolved.
Show resolved Hide resolved
_dict = self.generator.pydict(nb_elements=nb_elements, variable_nb_elements=variable_nb_elements,
value_types=value_types, allowed_types=allowed_types)

Check failure on line 637 in faker/providers/misc/__init__.py

View workflow job for this annotation

GitHub Actions / flake8

continuation line under-indented for visual indent
_dict = {self.generator.word(): _dict}
return xmltodict.unparse(_dict)

Check warning on line 640 in faker/providers/misc/__init__.py

View workflow job for this annotation

GitHub Actions / flake8

blank line contains whitespace
def fixed_width(self, data_columns: Optional[list] = None, num_rows: int = 10, align: str = "left") -> str:
"""
Generate random fixed width values.
Expand Down
7 changes: 7 additions & 0 deletions tests/providers/test_misc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import csv

Check failure on line 1 in tests/providers/test_misc.py

View workflow job for this annotation

GitHub Actions / isort

Imports are incorrectly sorted and/or formatted.
import datetime
import io
import itertools
Expand All @@ -8,6 +8,7 @@
import unittest
import uuid
import zipfile
import xml
Elihayb marked this conversation as resolved.
Show resolved Hide resolved

try:
import PIL.Image
Expand Down Expand Up @@ -455,6 +456,12 @@
for args, kwargs in mock_writer.call_args_list:
assert kwargs == test_kwargs

def test_xml(self, faker):
try:
xml.etree.ElementTree.fromstring(faker.xml())
except xml.etree.ElementTree.ParseError:
raise AssertionError("The xml format is invalid")
Elihayb marked this conversation as resolved.
Show resolved Hide resolved

Check warning on line 464 in tests/providers/test_misc.py

View workflow job for this annotation

GitHub Actions / flake8

blank line contains whitespace
def test_csv_helper_method(self, faker):
kwargs = {
"header": ["Column 1", "Column 2"],
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ deps =
validators>=0.13.0
sphinx>=2.4,<3.0
Pillow
xmltodict
commands =
coverage run --source=faker -m pytest {posargs}
coverage run --source=faker -a -m pytest --exclusive-faker-session tests/pytest/session_overrides {posargs}
Expand Down
Loading