Move most ABCs into common/abstract.py
This commit is contained in:
parent
e42a8272e4
commit
c58183d3cc
7 changed files with 87 additions and 91 deletions
80
doc/src/common/abstract.py
Normal file
80
doc/src/common/abstract.py
Normal file
|
@ -0,0 +1,80 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from minecraft.resource import ResourceLocation
|
||||
|
||||
# circular imports are gross
|
||||
if TYPE_CHECKING:
|
||||
from patchouli.book import Book
|
||||
from patchouli.category import Category
|
||||
from patchouli.entry import Entry
|
||||
else:
|
||||
Book, Category, Entry = Any, Any, Any
|
||||
|
||||
|
||||
class WithBook(ABC):
|
||||
"""ABC for composition with Book."""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def book(self) -> Book:
|
||||
...
|
||||
|
||||
@property
|
||||
def resource_dir(self):
|
||||
"""book.resource_dir"""
|
||||
return self.book.resource_dir
|
||||
|
||||
@property
|
||||
def modid(self):
|
||||
"""book.modid"""
|
||||
return self.book.modid
|
||||
|
||||
@property
|
||||
def i18n(self):
|
||||
"""book.i18n"""
|
||||
return self.book.i18n
|
||||
|
||||
|
||||
class Sortable(ABC):
|
||||
"""ABC for classes which can be sorted."""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def cmp_key(self) -> Any:
|
||||
...
|
||||
|
||||
def __lt__(self, other: Any) -> bool:
|
||||
if isinstance(other, Sortable):
|
||||
return self.cmp_key < other.cmp_key
|
||||
return NotImplemented
|
||||
|
||||
|
||||
@dataclass
|
||||
class WithPathId(ABC):
|
||||
"""ABC for classes with a ResourceLocation id."""
|
||||
|
||||
path: Path
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def base_dir(self) -> Path:
|
||||
"""Base directory. Combine with self.id.path to find this file."""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def modid(self) -> str:
|
||||
...
|
||||
|
||||
@property
|
||||
def id(self) -> ResourceLocation:
|
||||
resource_path = self.path.relative_to(self.base_dir).with_suffix("").as_posix()
|
||||
return ResourceLocation(self.modid, resource_path)
|
||||
|
||||
@property
|
||||
def href(self) -> str:
|
||||
return f"#{self.id.path}"
|
|
@ -1,38 +0,0 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
# circular imports are gross
|
||||
if TYPE_CHECKING:
|
||||
from patchouli.book import Book
|
||||
from patchouli.category import Category
|
||||
from patchouli.entry import Entry
|
||||
else:
|
||||
Book, Category, Entry = Any, Any, Any
|
||||
|
||||
# TODO: consolidate ABCs here
|
||||
|
||||
|
||||
class WithBook(ABC):
|
||||
"""ABC for composition with Book."""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def book(self) -> Book:
|
||||
...
|
||||
|
||||
@property
|
||||
def resource_dir(self):
|
||||
"""book.resource_dir"""
|
||||
return self.book.resource_dir
|
||||
|
||||
@property
|
||||
def modid(self):
|
||||
"""book.modid"""
|
||||
return self.book.modid
|
||||
|
||||
@property
|
||||
def i18n(self):
|
||||
"""book.i18n"""
|
||||
return self.book.i18n
|
|
@ -1,20 +1,6 @@
|
|||
from abc import ABC, abstractmethod
|
||||
from typing import Any, Mapping, TypeVar
|
||||
|
||||
|
||||
class Sortable(ABC):
|
||||
"""ABC for classes which can be sorted."""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def cmp_key(self) -> Any:
|
||||
...
|
||||
|
||||
def __lt__(self, other: Any) -> bool:
|
||||
if isinstance(other, Sortable):
|
||||
return self.cmp_key < other.cmp_key
|
||||
return NotImplemented
|
||||
from typing import Mapping, TypeVar
|
||||
|
||||
from common.abstract import Sortable
|
||||
|
||||
_T = TypeVar("_T")
|
||||
_T_Sortable = TypeVar("_T_Sortable", bound=Sortable)
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Self
|
||||
|
||||
from common.deserialize import FromStr
|
||||
|
@ -12,8 +10,6 @@ _RESOURCE_LOCATION_RE = re.compile(r"(?:([0-9a-z_\-.]+):)?([0-9a-z_\-./]+)")
|
|||
_ITEM_STACK_SUFFIX_RE = re.compile(r"(?:#([0-9]+))?({.*})?")
|
||||
|
||||
|
||||
# TODO: instead of the dataclass field thing, make this subclass str
|
||||
# _namespace and _method, access via properties
|
||||
@dataclass(repr=False, frozen=True)
|
||||
class ResourceLocation(FromStr):
|
||||
"""Represents a Minecraft resource location / namespaced ID."""
|
||||
|
@ -69,29 +65,3 @@ class ItemStack(ResourceLocation):
|
|||
if self.nbt is not None:
|
||||
s += self.nbt
|
||||
return s
|
||||
|
||||
|
||||
@dataclass
|
||||
class WithPathId(ABC):
|
||||
"""ABC for classes with a ResourceLocation id."""
|
||||
|
||||
path: Path
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def base_dir(self) -> Path:
|
||||
"""Base directory. Combine with self.id.path to find this file."""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def modid(self) -> str:
|
||||
...
|
||||
|
||||
@property
|
||||
def id(self) -> ResourceLocation:
|
||||
resource_path = self.path.relative_to(self.base_dir).with_suffix("").as_posix()
|
||||
return ResourceLocation(self.modid, resource_path)
|
||||
|
||||
@property
|
||||
def href(self) -> str:
|
||||
return f"#{self.id.path}"
|
||||
|
|
|
@ -3,12 +3,11 @@ from __future__ import annotations
|
|||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
from common.composition import Book, WithBook
|
||||
from common.abstract import Book, Sortable, WithBook, WithPathId
|
||||
from common.deserialize import FromJson
|
||||
from common.formatting import FormatTree
|
||||
from common.utils import Sortable
|
||||
from minecraft.i18n import LocalizedStr
|
||||
from minecraft.resource import ItemStack, ResourceLocation, WithPathId
|
||||
from minecraft.resource import ItemStack, ResourceLocation
|
||||
from patchouli.entry import Entry
|
||||
from serde import deserialize
|
||||
|
||||
|
|
|
@ -4,11 +4,10 @@ from dataclasses import dataclass
|
|||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from common.composition import Book, Category, WithBook
|
||||
from common.abstract import Book, Category, Sortable, WithBook, WithPathId
|
||||
from common.deserialize import FromJson
|
||||
from common.utils import Sortable
|
||||
from minecraft.i18n import LocalizedStr
|
||||
from minecraft.resource import ItemStack, ResourceLocation, WithPathId
|
||||
from minecraft.resource import ItemStack, ResourceLocation
|
||||
from patchouli.page import Page, Page_patchouli_text, page_transformers
|
||||
from serde import deserialize
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ from typing import (
|
|||
TypeVar,
|
||||
)
|
||||
|
||||
from common.composition import Book, Entry
|
||||
from common.abstract import Book, Entry
|
||||
from common.formatting import FormatTree
|
||||
from common.pattern_info import PatternInfo, RawPatternInfo
|
||||
|
||||
|
|
Loading…
Reference in a new issue