Skip to content

Commit

Permalink
refactor(stubs): remove old StubRepo class.
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 11, 2022
1 parent 5410a13 commit b9de35a
Showing 1 changed file with 1 addition and 120 deletions.
121 changes: 1 addition & 120 deletions micropy/stubs/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,137 +9,18 @@
from __future__ import annotations

import abc
import json
import shutil
import tempfile
from contextlib import contextmanager
from functools import partial
from pathlib import Path, PurePosixPath
from pathlib import Path
from typing import Any, Callable, Optional, final
from urllib import parse

import micropy.exceptions as exc
import requests
from micropy import utils
from micropy.logger import Log
from micropy.utils.types import PathStr


class StubRepo:
"""Represents a remote repository for stubs.
Args:
name (str): Repo Name
location (str): Valid url
ref (str): path to repo definition file
"""

repos = set()

def __init__(self, name, location, path, **kwargs):
self.name = name
self.path = path
self.log = Log.add_logger(self.name)
self.location = location
self.packages = kwargs.get("packages", [])
self.repos.add(self)

def has_package(self, name):
"""Checks if package is available in repo.
Args:
name (str): name of package
Returns:
bool: True if package is available
"""
url = self.get_url(name)
return utils.is_downloadable(url)

def get_url(self, path):
"""Returns formatted url to provided path.
Args:
path (str): path to format
Returns:
str: formatted url
"""
base_path = PurePosixPath(parse.urlparse(self.location).path)
pkg_path = base_path / PurePosixPath(self.path) / PurePosixPath(path)
url = parse.urljoin(self.location, str(pkg_path))
self.log.debug(f"Stub Url: {url}")
return url

def search(self, query):
"""Searches repository packages.
Args:
query (str): query to search by
Returns:
[str]: List of matching results
"""
query = query.strip().lower()
pkg_names = [p["name"] for p in self.packages]
results = {p for p in pkg_names if query in p.lower()}
return results

@classmethod
def resolve_package(cls, name):
"""Attempts to resolve package from all repos.
Args:
name (str): package to resolve
Raises:
StubNotFound: Package could not be resolved
Returns:
str: url to package
"""
results = (r for r in cls.repos if r.has_package(name))
try:
repo = next(results)
except StopIteration:
raise exc.StubNotFound(name)
else:
pkg_url = repo.get_url(name)
return pkg_url

@classmethod
def from_json(cls, content):
"""Create StubRepo Instances from JSON file.
Args:
file_obj (str or bytes): json content
Returns:
iterable of created repos
"""
data = json.loads(content)
for source in data:
source_url = source["source"]
source_data = requests.get(source_url).json()
try:
cls(**source_data)
except Exception as e:
pass
return cls.repos

def __eq__(self, other):
return self.location == getattr(other, "location", None)

def __hash__(self):
return hash(self.location)


class StubSource(abc.ABC):
"""Abstract Base Class for Stub Sources."""

Expand Down

0 comments on commit b9de35a

Please sign in to comment.