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 export plugin for JSON #2058

Merged
merged 3 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions tests/plan/export/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ rlJournalStart
if rlIsRHELLike "=8"; then
# RHEL-8 and Centos stream 8 usually offer an older Click package that has slightly
# different wording & quotes.
rlAssertgrep "Error: Invalid value for \"-h\" / \"--how\": invalid choice: weird. (choose from dict, yaml)" $rlRun_LOG
rlAssertgrep "Error: Invalid value for \"-h\" / \"--how\": invalid choice: weird. (choose from dict, json, yaml)" $rlRun_LOG
else
rlAssertGrep "Error: Invalid value for '-h' / '--how': 'weird' is not one of 'dict', 'yaml'." $rlRun_LOG
rlAssertGrep "Error: Invalid value for '-h' / '--how': 'weird' is not one of 'dict', 'json', 'yaml'." $rlRun_LOG
fi
rlPhaseEnd

Expand Down
4 changes: 2 additions & 2 deletions tests/test/export/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ rlJournalStart
if rlIsRHELLike "=8"; then
# RHEL-8 and Centos stream 8 usually offer an older Click package that has slightly
# different wording & quotes.
rlAssertgrep "Error: Invalid value for \"-h\" / \"--how\": invalid choice: weird. (choose from dict, nitrate, polarion, yaml)" $rlRun_LOG
rlAssertgrep "Error: Invalid value for \"-h\" / \"--how\": invalid choice: weird. (choose from dict, json, nitrate, polarion, yaml)" $rlRun_LOG
else
rlAssertGrep "Error: Invalid value for '-h' / '--how': 'weird' is not one of 'dict', 'nitrate', 'polarion', 'yaml'." $rlRun_LOG
rlAssertGrep "Error: Invalid value for '-h' / '--how': 'weird' is not one of 'dict', 'json', 'nitrate', 'polarion', 'yaml'." $rlRun_LOG
fi
rlPhaseEnd

Expand Down
60 changes: 60 additions & 0 deletions tmt/export/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,66 @@ def export_story_collection(cls,
""" Export collection of stories """
raise NotImplementedError()

# It's tempting to make this the default implementation of `ExporterPlugin` class,
# but that would mean the `ExportPlugin` would suddenly not raise `NotImplementedError`
# in methods where the export is not supported by a child class. That does not
# feel right, therefore the "simple export plugin" class of plugins has its own
# dedicated base.


class TrivialExporter(ExportPlugin):
"""
A helper base class for exporters with trivial export procedure.

Child classes need to implement a single method that performs a conversion
of a single collection item, and that's all. It is good enough for formats
like ``dict`` or ``YAML`` as they do not require any other input than the
data to convert.
"""

@classmethod
def _export(cls, data: _RawExported) -> str:
"""
Perform the actual conversion of internal data package to desired format.

The method is left for child classes to implement, all other public
methods call this method to perform the conversion for each collection
item.

:param data: data package to export.
:returns: string representation of the given data.
"""

raise NotImplementedError

@classmethod
def export_fmfid_collection(cls,
fmf_ids: List['tmt.base.FmfId'],
keys: Optional[List[str]] = None,
**kwargs: Any) -> str:
return cls._export([fmf_id._export(keys=keys) for fmf_id in fmf_ids])

@classmethod
def export_test_collection(cls,
tests: List['tmt.base.Test'],
keys: Optional[List[str]] = None,
**kwargs: Any) -> str:
return cls._export([test._export(keys=keys) for test in tests])

@classmethod
def export_plan_collection(cls,
plans: List['tmt.base.Plan'],
keys: Optional[List[str]] = None,
**kwargs: Any) -> str:
return cls._export([plan._export(keys=keys) for plan in plans])

@classmethod
def export_story_collection(cls,
stories: List['tmt.base.Story'],
keys: Optional[List[str]] = None,
**kwargs: Any) -> str:
return cls._export([story._export(keys=keys) for story in stories])


def get_bz_instance() -> BugzillaInstance:
""" Import the bugzilla module and return BZ instance """
Expand Down
32 changes: 1 addition & 31 deletions tmt/export/_dict.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Any, List, Optional

import tmt.base
import tmt.export
import tmt.utils
Expand All @@ -9,35 +7,7 @@
@tmt.base.Test.provides_export('dict')
@tmt.base.Plan.provides_export('dict')
@tmt.base.Story.provides_export('dict')
class DictExporter(tmt.export.ExportPlugin):
class DictExporter(tmt.export.TrivialExporter):
@classmethod
def _export(cls, data: tmt.export._RawExported) -> str:
return str(data)

@classmethod
def export_fmfid_collection(cls,
fmf_ids: List[tmt.base.FmfId],
keys: Optional[List[str]] = None,
**kwargs: Any) -> str:
return cls._export([fmf_id._export(keys=keys) for fmf_id in fmf_ids])

@classmethod
def export_test_collection(cls,
tests: List[tmt.base.Test],
keys: Optional[List[str]] = None,
**kwargs: Any) -> str:
return cls._export([test._export(keys=keys) for test in tests])

@classmethod
def export_plan_collection(cls,
plans: List[tmt.base.Plan],
keys: Optional[List[str]] = None,
**kwargs: Any) -> str:
return cls._export([plan._export(keys=keys) for plan in plans])

@classmethod
def export_story_collection(cls,
stories: List[tmt.base.Story],
keys: Optional[List[str]] = None,
**kwargs: Any) -> str:
return cls._export([story._export(keys=keys) for story in stories])
15 changes: 15 additions & 0 deletions tmt/export/_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import json

import tmt.base
import tmt.export
import tmt.utils


@tmt.base.FmfId.provides_export('json')
@tmt.base.Test.provides_export('json')
@tmt.base.Plan.provides_export('json')
@tmt.base.Story.provides_export('json')
class JSONExporter(tmt.export.TrivialExporter):
@classmethod
def _export(cls, data: tmt.export._RawExported) -> str:
return json.dumps(data)
32 changes: 1 addition & 31 deletions tmt/export/yaml.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Any, List, Optional

import tmt.base
import tmt.export
import tmt.utils
Expand All @@ -9,35 +7,7 @@
@tmt.base.Test.provides_export('yaml')
@tmt.base.Plan.provides_export('yaml')
@tmt.base.Story.provides_export('yaml')
class YAMLExporter(tmt.export.ExportPlugin):
class YAMLExporter(tmt.export.TrivialExporter):
@classmethod
def _export(cls, data: tmt.export._RawExported) -> str:
return tmt.utils.dict_to_yaml(data)

@classmethod
def export_fmfid_collection(cls,
fmf_ids: List[tmt.base.FmfId],
keys: Optional[List[str]] = None,
**kwargs: Any) -> str:
return cls._export([fmf_id._export(keys=keys) for fmf_id in fmf_ids])

@classmethod
def export_test_collection(cls,
tests: List[tmt.base.Test],
keys: Optional[List[str]] = None,
**kwargs: Any) -> str:
return cls._export([test._export(keys=keys) for test in tests])

@classmethod
def export_plan_collection(cls,
plans: List[tmt.base.Plan],
keys: Optional[List[str]] = None,
**kwargs: Any) -> str:
return cls._export([plan._export(keys=keys) for plan in plans])

@classmethod
def export_story_collection(cls,
stories: List[tmt.base.Story],
keys: Optional[List[str]] = None,
**kwargs: Any) -> str:
return cls._export([story._export(keys=keys) for story in stories])