-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This will make it easier to parse the `done` fields at the beginning instead of waiting for a filter to try to use them. And I'm planning to add other fields here later.
- Loading branch information
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Media items.""" | ||
|
||
from collections.abc import Sequence | ||
import dataclasses | ||
from typing import Self | ||
|
||
from rock_paper_sand import multi_level_set | ||
from rock_paper_sand.proto import config_pb2 | ||
|
||
|
||
@dataclasses.dataclass(frozen=True, kw_only=True) | ||
class MediaItem: | ||
"""Media item. | ||
Attributes: | ||
proto: Proto from the config file. | ||
done: Parsed proto.done field. | ||
parts: Parsed proto.parts field. | ||
""" | ||
|
||
proto: config_pb2.MediaItem | ||
done: multi_level_set.MultiLevelSet | ||
parts: Sequence["MediaItem"] | ||
|
||
@classmethod | ||
def from_config(cls, proto: config_pb2.MediaItem) -> Self: | ||
"""Parses from a config proto.""" | ||
return cls( | ||
proto=proto, | ||
done=multi_level_set.MultiLevelSet.from_string(proto.done), | ||
parts=tuple(map(cls.from_config, proto.parts)), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# pylint: disable=missing-module-docstring | ||
|
||
from unittest import mock | ||
|
||
from absl.testing import absltest | ||
from absl.testing import parameterized | ||
from google.protobuf import json_format | ||
|
||
from rock_paper_sand import media_item | ||
from rock_paper_sand import multi_level_set | ||
from rock_paper_sand.proto import config_pb2 | ||
|
||
|
||
class MediaItemTest(parameterized.TestCase): | ||
def test_from_config(self) -> None: | ||
proto = json_format.ParseDict( | ||
{ | ||
"name": "some-name", | ||
"done": "all", | ||
"parts": [ | ||
{"name": "some-part"}, | ||
], | ||
}, | ||
config_pb2.MediaItem(), | ||
) | ||
|
||
item = media_item.MediaItem.from_config(proto) | ||
|
||
self.assertEqual( | ||
media_item.MediaItem( | ||
proto=proto, | ||
done=mock.ANY, | ||
parts=( | ||
media_item.MediaItem( | ||
proto=config_pb2.MediaItem(name="some-part"), | ||
done=mock.ANY, | ||
parts=(), | ||
), | ||
), | ||
), | ||
item, | ||
) | ||
self.assertIn(multi_level_set.parse_number("1"), item.done) | ||
self.assertNotIn(multi_level_set.parse_number("1"), item.parts[0].done) | ||
|
||
|
||
if __name__ == "__main__": | ||
absltest.main() |