Skip to content

Commit

Permalink
👹 Feed the hobgoblins (delint).
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Dec 23, 2024
1 parent 515f403 commit 991168d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 50 deletions.
26 changes: 13 additions & 13 deletions imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@


def rel_prefix(node):
return '.' * getattr(node, 'level', 0)
return "." * getattr(node, "level", 0)


class Import(str):
@classmethod
def read(cls, node, alias):
return cls(
rel_prefix(node)
+ '.'.join(
filter(bool, [getattr(node, 'module', None), alias.name]),
+ ".".join(
filter(bool, [getattr(node, "module", None), alias.name]),
)
)

Expand All @@ -51,13 +51,13 @@ def relative_to(self, parent):
>>> Import('..foo.bar').relative_to('coherent._private.mod')
'coherent._private.foo.bar'
"""
if not self.startswith('.'):
if not self.startswith("."):
return self
p_names = parent.split('.')
l_names = self[1:].split('.')
blanks = l_names.count('')
p_names = parent.split(".")
l_names = self[1:].split(".")
blanks = l_names.count("")
parents = p_names[:-blanks] if blanks else p_names
return '.'.join(parents + l_names[blanks:])
return ".".join(parents + l_names[blanks:])

def standard(self):
"""
Expand Down Expand Up @@ -90,7 +90,7 @@ def top(self) -> str | None:
'foo'
>>> Import('.foo.bar').top
"""
return self.split('.')[0] or None
return self.split(".")[0] or None

def builtin(self):
# for compatibility
Expand All @@ -111,7 +111,7 @@ def implicit(self):
>>> Import('os').implicit()
False
"""
implicit = {'_typeshed'}
implicit = {"_typeshed"}
return self.top in implicit

def excluded(self):
Expand All @@ -129,8 +129,8 @@ def _check_standard(top_level_name: str) -> bool:
Return True if it's found in the standard library, and False otherwise.
"""
# Windows can choke without these vars (python/cpython#120836)
safe_isolation = Projection(['SYSTEMDRIVE', 'SYSTEMROOT'], os.environ)
cmd = [sys.executable, '-S', '-c', f'import {top_level_name}']
safe_isolation = Projection(["SYSTEMDRIVE", "SYSTEMROOT"], os.environ)
cmd = [sys.executable, "-S", "-c", f"import {top_level_name}"]
subprocess.check_call(cmd, env=safe_isolation, stderr=subprocess.DEVNULL)


Expand Down Expand Up @@ -171,4 +171,4 @@ def print_module_imports(path: pathlib.Path):
print(list(name for name in get_module_imports(path) if not name.standard()))


__name__ == '__main__' and print_module_imports(pathlib.Path(sys.argv[1]))
__name__ == "__main__" and print_module_imports(pathlib.Path(sys.argv[1]))
74 changes: 37 additions & 37 deletions pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@
from retry_requests import retry
from zipp.compat.overlay import zipfile

session = retry(sessions.BaseUrlSession('https://pypi.python.org/pypi/'))
session.mount('file://', FileAdapter())
session = retry(sessions.BaseUrlSession("https://pypi.python.org/pypi/"))
session.mount("file://", FileAdapter())
log = logging.getLogger(__name__)

top_8k = 'https://hugovk.github.io/top-pypi-packages/top-pypi-packages-30-days.min.json'
top_8k = "https://hugovk.github.io/top-pypi-packages/top-pypi-packages-30-days.min.json"


@cache
Expand All @@ -84,8 +84,8 @@ def store():
An authenticated, read-write connection to the collection.
"""
db = client(getpass.getuser())
db.distributions.create_index([('roots', 1)])
db.distributions.create_index([('id', 1)])
db.distributions.create_index([("roots", 1)])
db.distributions.create_index([("id", 1)])
return db.distributions


Expand All @@ -96,14 +96,14 @@ def client(username=None):
Defaults to an anonymous, read-only connection if no username is supplied.
"""
username = username or os.environ.get('DB_USER') or 'anonymous'
cluster = os.environ.get('DB_CLUSTER') or 'cluster0.acvlhai.mongodb.net'
username = username or os.environ.get("DB_USER") or "anonymous"
cluster = os.environ.get("DB_CLUSTER") or "cluster0.acvlhai.mongodb.net"
password = (
'coherent.build'
if username == 'anonymous'
"coherent.build"
if username == "anonymous"
else keyring.get_password(cluster, username)
)
uri = f'mongodb+srv://{username}:{password}@{cluster}/pypi'
uri = f"mongodb+srv://{username}:{password}@{cluster}/pypi"
return jaraco.mongodb.helper.connect_db(uri)


Expand All @@ -121,14 +121,14 @@ def all_names(module):
['foo.bar.baz', 'foo.bar', 'foo']
"""
yield module
parent, _, _ = module.rpartition('.')
parent, _, _ = module.rpartition(".")
if not parent:
return
yield from all_names(parent)


def is_root(module):
return client().distributions.find_one({'roots': module})
return client().distributions.find_one({"roots": module})


class NoDistributionForImport(LookupError): ...
Expand All @@ -154,7 +154,7 @@ def distribution_for(import_name: str) -> str:
coherent.deps...NoDistributionForImport: import_who_shall_not_be_named.foo.bar
"""
try:
return next(filter(bool, map(is_root, all_names(import_name))))['name']
return next(filter(bool, map(is_root, all_names(import_name))))["name"]
except StopIteration:
raise NoDistributionForImport(import_name) from None

Expand All @@ -165,36 +165,36 @@ def wheel(self) -> zipfile.Path:
"""
Return the wheel.
"""
resp = session.get(f'{super().__str__()}/json')
resp = session.get(f"{super().__str__()}/json")
resp.raise_for_status()
info = resp.json()
if 'urls' not in info:
if "urls" not in info:
raise ValueError("No dists")
try:
match = first(
url for url in info['urls'] if url['filename'].endswith('.whl')
url for url in info["urls"] if url["filename"].endswith(".whl")
)
except ValueError:
raise ValueError("No wheels")
resp = session.get(match['url'])
resp = session.get(match["url"])
zf = zipfile.ZipFile(io.BytesIO(resp.content))
zf.filename = match['filename']
zf.filename = match["filename"]
return zipfile.Path(zf)

@classmethod
def query(cls, url=top_8k):
return map(cls.from_row, session.get(url).json()['rows'])
return map(cls.from_row, session.get(url).json()["rows"])

@classmethod
def from_row(cls, row):
self = cls(row['project'])
self.downloads = row['download_count']
self = cls(row["project"])
self.downloads = row["download_count"]
return self

@classmethod
def unprocessed(cls):
query = {"updated": {"$exists": False}}
return map(cls, map(operator.itemgetter('id'), store().find(query)))
return map(cls, map(operator.itemgetter("id"), store().find(query)))

def refresh(self):
vars(self).update(self.from_wheel())
Expand All @@ -217,11 +217,11 @@ def from_wheel(self):
)

def __json__(self):
keys = ['name', 'roots', 'error', 'downloads', 'updated']
keys = ["name", "roots", "error", "downloads", "updated"]
return dict(id=self, **jaraco.collections.Projection(keys, vars(self)))

def _get_name(self):
info = one(self.wheel.glob('*.dist-info'))
info = one(self.wheel.glob("*.dist-info"))
return importlib.metadata.PathDistribution(info).name

def __str__(self):
Expand All @@ -237,7 +237,7 @@ def top(package_name: str) -> str:
>>> top('foo')
''
"""
top, sep, name = package_name.partition('.')
top, sep, name = package_name.partition(".")
return sep and top


Expand All @@ -250,7 +250,7 @@ def parent(package_name: str) -> str:
>>> parent('foo')
''
"""
parent, sep, name = package_name.rpartition('.')
parent, sep, name = package_name.rpartition(".")
return sep and parent


Expand All @@ -273,15 +273,15 @@ def find_packages(wheel: zipfile.Path):
Find all Python packages in the wheel.
"""
return (
init.parent.at.rstrip('/').replace('/', '.')
for init in wheel.glob('**/__init__.py')
init.parent.at.rstrip("/").replace("/", ".")
for init in wheel.glob("**/__init__.py")
if not is_namespace(init)
)


ns_pattern = re.compile(
r'import.*(pkg_resources|pkgutil).*'
r'(\.declare_namespace\(__name__\)|\.extend_path\(__path__, __name__\))',
r"import.*(pkg_resources|pkgutil).*"
r"(\.declare_namespace\(__name__\)|\.extend_path\(__path__, __name__\))",
flags=re.DOTALL,
)

Expand All @@ -290,12 +290,12 @@ def open(path: zipfile.Path):
"""
Modeled after tokenize.open, open the path using detected encoding.
"""
buffer = path.open('rb')
buffer = path.open("rb")
try:
encoding, lines = tokenize.detect_encoding(buffer.readline)
buffer.seek(0)
text = io.TextIOWrapper(buffer, encoding, line_buffering=True)
text.mode = 'r'
text.mode = "r"
return text
except Exception:
buffer.close()
Expand Down Expand Up @@ -348,17 +348,17 @@ def find_modules(wheel: zipfile.Path):
Find all modules in the wheel.
"""
return (
str(pathlib.PurePosixPath(modfile.at).with_suffix('')).replace('/', '.')
str(pathlib.PurePosixPath(modfile.at).with_suffix("")).replace("/", ".")
for modfile in itertools.chain(
wheel.glob('**/*.py'),
wheel.glob('*.py'),
wheel.glob("**/*.py"),
wheel.glob("*.py"),
)
if modfile.name != '__init__.py'
if modfile.name != "__init__.py"
)


def importable(name: str) -> bool:
return all(map(str.isidentifier, name.split('.')))
return all(map(str.isidentifier, name.split(".")))


def find_names(wheel):
Expand Down

0 comments on commit 991168d

Please sign in to comment.