Skip to content

Commit

Permalink
feat(toolkit): Simple package class from single text
Browse files Browse the repository at this point in the history
  • Loading branch information
attakei committed Feb 24, 2024
1 parent 3d36d45 commit ae274d4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/rst_package_refs/toolkit.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
"""Toolkit to create original custom role."""
import abc
import re
from dataclasses import dataclass
from typing import Tuple


@dataclass
class Package(abc.ABC):
"""Package data struct (abstract)."""

name: str

@property
@abc.abstractmethod
def url(self) -> str:
"""Return URL of package."""
pass

@classmethod
def parse(cls, target) -> "Package":
"""Parse target and generate package object.
If your definition package requires extra method to parse, override it.
"""
return cls(name=target)


def split_text(source: str) -> Tuple[str, str]:
"""Split from content to displaying text and target text.
Expand Down
16 changes: 16 additions & 0 deletions tests/test_toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,19 @@ def test_split_text(text, display_text, target_text):
display, target = toolkit.split_text(text)
assert display == display_text
assert target == target_text


def test_Package_require_inherit():
with pytest.raises(TypeError):
package = toolkit.Package.parse("test")
package.url


def test_InheritPackage():
class MyPackage(toolkit.Package):
@property
def url(self):
return "ok"

package = MyPackage.parse("name")
assert package.url == "ok"

0 comments on commit ae274d4

Please sign in to comment.