Skip to content
This repository was archived by the owner on Aug 25, 2024. It is now read-only.

Commit 75103bb

Browse files
sudharsana-kjlpdxjohnny
authored andcommitted
source: json: Add label to JSONSource
Signed-off-by: John Andersen <john.s.andersen@intel.com>
1 parent fffca85 commit 75103bb

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2828
sources.
2929
- A temporary directory is used to replicate `mktemp -u` functionality so as to
3030
provide tests using a FileSource with a valid tempfile name.
31+
- Labels for JSON sources
3132

3233
## [0.2.1] - 2019-06-07
3334
### Added

dffml/source/file.py

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
class FileSourceConfig(BaseConfig, NamedTuple):
2121
filename: str
22+
label: str = "unlabeled"
2223
readonly: bool = False
2324

2425

@@ -114,11 +115,15 @@ def args(cls, args, *above) -> Dict[str, Arg]:
114115
"readonly",
115116
Arg(type=bool, action="store_true", default=False),
116117
)
118+
cls.config_set(
119+
args, above, "label", Arg(type=str, default="unlabeled")
120+
)
117121
return args
118122

119123
@classmethod
120124
def config(cls, config, *above):
121125
return FileSourceConfig(
122126
filename=cls.config_get(config, above, "filename"),
123127
readonly=cls.config_get(config, above, "readonly"),
128+
label=cls.config_get(config, above, "label"),
124129
)

dffml/source/json.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,21 @@ class JSONSource(FileSource, MemorySource):
1717
stored in memory.
1818
"""
1919

20+
def __init__(self, config):
21+
super().__init__(config)
22+
self.repos = {}
23+
2024
async def load_fd(self, fd):
21-
repos = json.load(fd)
25+
self.repos = json.load(fd)
2226
self.mem = {
2327
src_url: Repo(src_url, data=data)
24-
for src_url, data in repos.items()
28+
for src_url, data in self.repos.get(self.config.label, {}).items()
2529
}
2630
LOGGER.debug("%r loaded %d records", self, len(self.mem))
2731

2832
async def dump_fd(self, fd):
29-
json.dump(
30-
{repo.src_url: repo.dict() for repo in self.mem.values()}, fd
31-
)
33+
self.repos[self.config.label] = {
34+
repo.src_url: repo.dict() for repo in self.mem.values()
35+
}
36+
json.dump(self.repos, fd)
3237
LOGGER.debug("%r saved %d records", self, len(self.mem))

tests/source/test_file.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ def test_args(self):
7070
),
7171
"config": {},
7272
},
73+
"label": {
74+
"arg": Arg(type=str, default="unlabeled"),
75+
"config": {},
76+
},
7377
},
7478
}
7579
},
@@ -82,19 +86,27 @@ def test_config_readonly_default(self):
8286
parse_unknown("--source-file-filename", "feedface")
8387
)
8488
self.assertEqual(config.filename, "feedface")
89+
self.assertEqual(config.label, "unlabeled")
8590
self.assertFalse(config.readonly)
8691

8792
def test_config_readonly_set(self):
8893
config = FileSource.config(
8994
parse_unknown(
90-
"--source-file-filename", "feedface", "--source-file-readonly"
95+
"--source-file-filename",
96+
"feedface",
97+
"--source-file-label",
98+
"default-label",
99+
"--source-file-readonly",
91100
)
92101
)
93102
self.assertEqual(config.filename, "feedface")
103+
self.assertEqual(config.label, "default-label")
94104
self.assertTrue(config.readonly)
95105

96-
def config(self, filename, readonly=False):
97-
return FileSourceConfig(filename=filename, readonly=readonly)
106+
def config(self, filename, label="unlabeled", readonly=False):
107+
return FileSourceConfig(
108+
filename=filename, readonly=readonly, label=label
109+
)
98110

99111
async def test_open(self):
100112
m_open = mock_open()

0 commit comments

Comments
 (0)