Skip to content

Commit

Permalink
feat(stubs): RepoStubLocator locate strategy.
Browse files Browse the repository at this point in the history
Signed-off-by: Braden Mars <bradenmars@bradenmars.me>
  • Loading branch information
BradenM committed Dec 12, 2022
1 parent b873a62 commit 08f8f86
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion micropy/stubs/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
from typing import TYPE_CHECKING, Any, Callable, ContextManager, Optional, Union, cast

import attrs
import micropy.exceptions as exc
from micropy import utils
from micropy.logger import Log
from micropy.utils.types import PathStr
from typing_extensions import Protocol

if TYPE_CHECKING:
pass
from micropy.stubs.repo import StubRepository


class LocateStrategy(Protocol):
Expand Down Expand Up @@ -125,6 +126,23 @@ def prepare(self, location: PathStr) -> tuple[PathStr, Optional[Callable[..., An
return source_path, teardown


@attrs.define
class RepoStubLocator(LocateStrategy):

repo: StubRepository = attrs.field(repr=False)

def prepare(self, location: PathStr) -> Union[PathStr, tuple[PathStr, Callable[..., Any]]]:
if not self.repo:
return location
try:
source = self.repo.resolve_package(location)
except exc.StubNotFound as e:
logger.debug(f"{self}: {location} not found in repo, skipping... (exc: {e})")
return location
else:
return source.url


def get_source(location, **kwargs):
"""Factory for StubSource Instance.
Expand Down

0 comments on commit 08f8f86

Please sign in to comment.