Skip to content

Commit

Permalink
Add a class for parsed media items
Browse files Browse the repository at this point in the history
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
dseomn committed Sep 24, 2023
1 parent a17308b commit 78419c3
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
45 changes: 45 additions & 0 deletions rock_paper_sand/media_item.py
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)),
)
62 changes: 62 additions & 0 deletions rock_paper_sand/media_item_test.py
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()

0 comments on commit 78419c3

Please sign in to comment.