Skip to content

Commit

Permalink
use pypi importlib_resources for <3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianPugh committed Nov 1, 2023
1 parent e058e3e commit f9320c0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
9 changes: 7 additions & 2 deletions belay/cli/new.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import importlib.resources as pkg_resources
import re
import shutil
import sys
from pathlib import Path

from packaging.utils import canonicalize_name
from typer import Argument, Option

if sys.version_info < (3, 9, 0):
import importlib_resources
else:
import importlib.resources as importlib_resources


def new(project_name: str = Argument(..., help="Project Name.")):
"""Create a new micropython project structure."""
package_name = canonicalize_name(project_name)
dst_dir = Path() / project_name
template_dir = pkg_resources.files("belay") / "cli" / "new_template"
template_dir = importlib_resources.files("belay") / "cli" / "new_template"

shutil.copytree(str(template_dir), str(dst_dir))

Expand Down
10 changes: 7 additions & 3 deletions belay/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import atexit
import concurrent.futures
import contextlib
import importlib.resources
import linecache
import re
import shutil
Expand Down Expand Up @@ -46,6 +45,11 @@
from .typing import BelayReturn, PathType
from .webrepl import WebreplToSerial

if sys.version_info < (3, 9, 0):
import importlib_resources
else:
import importlib.resources as importlib_resources

P = ParamSpec("P")
R = TypeVar("R")

Expand Down Expand Up @@ -495,11 +499,11 @@ def sync_dependencies(
**kwargs
Passed along to ``Device.sync``.
"""
pkg_files = importlib.resources.files(package)
pkg_files = importlib_resources.files(package)
with TemporaryDirectory() as tmp_dir:
tmp_dir = Path(tmp_dir)
for subfolder in subfolders:
with importlib.resources.as_file(pkg_files / str(subfolder)) as f:
with importlib_resources.as_file(pkg_files / str(subfolder)) as f:
shutil.copytree(f, tmp_dir, dirs_exist_ok=True)
self.sync(tmp_dir, dst=dst, **kwargs)

Expand Down
9 changes: 7 additions & 2 deletions belay/helpers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import importlib.resources
import secrets
import string
import sys
from functools import lru_cache, partial, wraps

from . import snippets

if sys.version_info < (3, 9, 0):
import importlib_resources
else:
import importlib.resources as importlib_resources

_python_identifier_chars = string.ascii_uppercase + string.ascii_lowercase + string.digits


Expand All @@ -20,4 +25,4 @@ def random_python_identifier(n=16):
@lru_cache
def read_snippet(name):
resource = f"{name}.py"
return importlib.resources.files(snippets).joinpath(resource).read_text()
return importlib_resources.files(snippets).joinpath(resource).read_text()

0 comments on commit f9320c0

Please sign in to comment.