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

Add option to pretty print the JSON Output #34

Merged
merged 2 commits into from
Jun 30, 2021
Merged

Conversation

SchoolGuy
Copy link
Member

This should be rebased after PR #33 and then reviewed.

@SchoolGuy SchoolGuy added the enhancement New feature or request label Jun 22, 2021
@SchoolGuy SchoolGuy added this to the v1.0.0 milestone Jun 22, 2021
@SchoolGuy SchoolGuy requested a review from nodeg June 22, 2021 07:28
@SchoolGuy
Copy link
Member Author

Splitout of #31

@codecov
Copy link

codecov bot commented Jun 22, 2021

Codecov Report

Merging #34 (125d412) into main (00d5126) will decrease coverage by 1.08%.
The diff coverage is 37.83%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #34      +/-   ##
==========================================
- Coverage   51.60%   50.51%   -1.09%     
==========================================
  Files           4        4              
  Lines         655      675      +20     
==========================================
+ Hits          338      341       +3     
- Misses        317      334      +17     
Flag Coverage Δ
unittests 50.51% <37.83%> (-1.09%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
libcobblersignatures/cli.py 0.00% <0.00%> (ø)
libcobblersignatures/signatures.py 96.93% <82.35%> (-3.07%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 00d5126...125d412. Read the comment docs.

libcobblersignatures/cli.py Outdated Show resolved Hide resolved
apt = 4
"""
A repository which is managed by apt.
"""


class Osversion:
Copy link

@agraul agraul Jun 25, 2021

Choose a reason for hiding this comment

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

If you ask me, this class really wants to be a dataclass. I wonder how much mutability you need, are the deleters and setters really needed?

I could imagine something like the below is working as well, but I don't have the full context of theses models:

@dataclasses.dataclass
class OSVersion:  # pylint: disable = too-many-instance-attributes
    """Version of an OS breed."""

    signatures: list
    version_file: str
    version_file_regex: str
    kernel_arch: str
    kernel_arch_regex: str
    supported_arches: list
    supported_repo_breeds: list
    kernel_file: str
    initrd_file: str
    isolinux_ok: bool
    default_autoinstall: str
    kernel_options: str
    kernel_options_post: str
    template_files: str
    boot_files: list
    boot_loaders: dict

    def encode(self) -> dict:
        """
        Encodes a single :class:`Osversion`. This means that the properties of an object is transferred into a JSON.

        :return: The dictionary with the data.
        """
       return dataclasses.asdict(self)

# instead of
version = Osversion()
version.decode(my_dict)
# you'd use
version = OSVersion(**my_dict)

The upside of using dataclasses is that you only need to write what attributes you have and what type they should have and the rest is autogenerated. But you'd lose strict type checking at runtime, at least without additional code/libraries.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well what is missing currently is the fact that for example I want to do more checks in the setters. For example I do want to do regex validation and give back the actual objects on the getters later. Also I don't want consumers to be able to acutally delete attributes, thus the explicit overwrite of the deleters. Also this is a nice solution in my eyes to have a possibility to reset values to their defaults if required.

Tbh. I didn't knew the concept of a dataclass before but I think especially for the dict and list the validation should be there at least which then means that a dataclass is not enough. What I like is that the objects can be made immutable but since I am using lists and dicts that sadly is not possible to my understanding.

If I only would limit myself to type checking I found this library - https://pypi.org/project/dataclass-type-validator/ - but I am not sure if it is wise to do only minimal validation if you have user input... My design skills are sadly not my biggest strength.

Copy link
Member Author

Choose a reason for hiding this comment

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

Summary of a talk @agraul @meaksh and I had after our standup: Since the only consumers of this library are the CLI and Cobbler itself, currently and in the foreseeable future, it makes more sense to handle the data validation directly in these points. This point is strengthened by the fact that it doesn't matter how much validation I will build in because in Python there is always around my code. We also discussed other solutions but came to the conclusion that @agraul's solution with the dataclasses is the most elegant for now.

@SchoolGuy SchoolGuy force-pushed the feature/pretty-print branch from c8cf3a5 to fbe942f Compare June 28, 2021 11:01
@SchoolGuy
Copy link
Member Author

Rebased since PR #33 was merged

@SchoolGuy SchoolGuy marked this pull request as ready for review June 28, 2021 11:01
@SchoolGuy
Copy link
Member Author

So the context of this PR is not choosing the dataclass type validation library or converting this to a dataclass itself. I will open an issue for discussion for this now. Please rereview this PR what that in mind again if possible @agraul & @meaksh

@SchoolGuy SchoolGuy requested a review from vzhestkov June 30, 2021 15:45
@SchoolGuy SchoolGuy merged commit deee448 into main Jun 30, 2021
@SchoolGuy SchoolGuy deleted the feature/pretty-print branch June 30, 2021 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

Successfully merging this pull request may close these issues.

5 participants