Skip to content

Commit

Permalink
Downgrade to python 3.7 (from 3.9)
Browse files Browse the repository at this point in the history
  • Loading branch information
crispyricepc committed Mar 17, 2021
1 parent 9e85f44 commit 9d8a761
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 61 deletions.
14 changes: 8 additions & 6 deletions mcpkg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,18 @@ def install_packs(pack_set: PackSet, directory: Path, noconfirm=False):
raise SystemExit(-1)


def install(expressions: list[str], directory: Path):
def install(expressions: "list[str]", directory: Path):
log("Getting pack metadata...", LogLevel.INFO)
packs = syncdb.get_local_pack_list()
packs.filter_by(expressions)
install_packs(packs, directory)


def remove_packs(expressions: list[str], directory: Path):
def remove_packs(expressions: "list[str]", directory: Path):
installed_packs = worldmanager.get_installed_packs(directory)
for search_term in expressions:
if pack := installed_packs.get(search_term):
pack = installed_packs.get(search_term)
if pack:
worldmanager.remove_pack(pack, directory)
continue

Expand All @@ -122,7 +123,7 @@ def update():
syncdb.fetch_pack_list()


def upgrade(packs: list[str], force: bool, directory: Path):
def upgrade(packs: "list[str]", force: bool, directory: Path):
installed_packs = worldmanager.get_installed_packs(directory)
if packs:
installed_packs.filter_by(packs)
Expand Down Expand Up @@ -167,7 +168,7 @@ def list_packages(compact: bool, installed: bool, directory: Path):
log("Run 'mcpkg upgrade' to update packs in this world", LogLevel.INFO)


def search(expressions: list[str], compact: bool):
def search(expressions: "list[str]", compact: bool):
log("Searching:", LogLevel.INFO)
pack_set = syncdb.search_local_pack_list(expressions)
for pack in pack_set:
Expand All @@ -181,7 +182,8 @@ def main() -> None:
installed = arguments["--installed"]
force = arguments["--force"]

if not (path := arguments["--path"]):
path = arguments["--path"]
if not path:
path = str(Path.cwd().absolute())

if arguments["install"]:
Expand Down
10 changes: 6 additions & 4 deletions mcpkg/fileio.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def dl_with_progress(url: str, display: str) -> BytesIO:
return buffer


def separate_datapacks(src_file: BytesIO) -> dict[Pack, Path]:
def separate_datapacks(src_file: BytesIO) -> "dict[Pack, Path]":
"""
Separates a single zip file into their stored packs
"""
Expand All @@ -46,15 +46,17 @@ def separate_datapacks(src_file: BytesIO) -> dict[Pack, Path]:

# If datapacks, the child objects of the zip should be more zip files
output_zips = tmploc.glob("*.zip")
output_packs: dict[Pack, Path] = {}
output_packs: "dict[Pack, Path]" = {}

for pack_zip in output_zips:
if not (match := Pattern.DATAPACK.match(pack_zip.stem)):
match = Pattern.DATAPACK.match(pack_zip.stem)
if not match:
log(f"Regex match for '{pack_zip.stem}' failed",
LogLevel.ERROR)
raise SystemExit(-1)
# Get pack metadata info from syncdb
if not (pack := syncdb.get_pack_metadata(match.group("name"))):
pack = syncdb.get_pack_metadata(match.group("name"))
if not pack:
log(f"Couldn't find '{match.group('name')}' in syncdb",
LogLevel.ERROR)
raise SystemExit(-1)
Expand Down
16 changes: 9 additions & 7 deletions mcpkg/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(
category: str,
version: str = "0.0.0",
description: Optional[str] = None,
tags: Optional[list[str]] = None,
tags: "Optional[list[str]]" = None,
installed: Optional[Path] = None
):
self.remote_name = remote_name
Expand Down Expand Up @@ -50,7 +50,7 @@ class PackSet:
"""

def __init__(self):
self._content: dict[str, Pack] = {}
self._content: "dict[str, Pack]" = {}

def __len__(self):
return len(self._content)
Expand All @@ -74,16 +74,17 @@ def union(self, s):
"""
Merges another packset into this one
"""
self._content = self._content | s._content
self._content.update(s._content)

def filter_by(self, pack_filter: Optional[list[str]]):
def filter_by(self, pack_filter: "Optional[list[str]]"):
if not pack_filter:
return

results = PackSet()
for search_term in pack_filter:
# First try to non-iteratively find
if pack := self.get(search_term):
pack = self.get(search_term)
if pack:
results[search_term] = pack
continue

Expand All @@ -108,10 +109,11 @@ def encode_packset(pack_set: PackSet, fp):


def decode_packset(fp):
lst: list[dict[str, Any]] = json.load(fp)
lst: "list[dict[str, Any]]" = json.load(fp)
pack_set = PackSet()
for pack_dct in lst:
if installed_loc := pack_dct.get("installed"):
installed_loc = pack_dct.get("installed")
if installed_loc:
installed_path = Path(installed_loc)
else:
installed_path = None
Expand Down
10 changes: 6 additions & 4 deletions mcpkg/syncdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def name_is_formalised(name: str) -> bool:
return "VanillaTweaks." in name


def make_post(url: str, request: dict[str, str]) -> str:
def make_post(url: str, request: "dict[str, str]") -> str:
# Prep the request (allows for more verbose debug output)
prep_request = requests.Request('POST',
url,
Expand Down Expand Up @@ -125,7 +125,7 @@ def vt_to_packdb(src: BytesIO, dst: Path, pack_type: PackType) -> None:
- dst: The path to the new pack list (usually ~/.config/mcpkg/packs.json)
"""
global pack_data
src_dict: dict[str, Any] = json.load(src)
src_dict: "dict[str, Any]" = json.load(src)
pack_set = PackSet()
for src_category in src_dict["categories"]:
category_name = src_category["category"]
Expand Down Expand Up @@ -176,7 +176,9 @@ def get_pack_metadata(pack_id: str) -> Optional[Pack]:
`pack_id` can be either the formal id or the remote name
"""
local_list = get_local_pack_list()
if pack := local_list.get(pack_id):

pack = local_list.get(pack_id)
if pack:
return pack

for pack in local_list:
Expand Down Expand Up @@ -207,7 +209,7 @@ def get_local_pack_list() -> PackSet:
return packs


def search_local_pack_list(expressions: list[str]) -> PackSet:
def search_local_pack_list(expressions: "list[str]") -> PackSet:
results = PackSet()
for search_term in expressions:
for pack in get_local_pack_list():
Expand Down
9 changes: 5 additions & 4 deletions mcpkg/worldmanager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pathlib import Path
from typing import Final
from colorama import Fore
import shutil

Expand All @@ -8,7 +7,7 @@
from .logger import log


WORLD_FILES: Final[tuple[str, ...]] = (
WORLD_FILES = (
"advancements", "data", "datapacks", "level.dat", "playerdata", "region", "stats"
)

Expand Down Expand Up @@ -74,7 +73,8 @@ def install_pack(source_zip: Path, dest_dir: Path, pack: Pack, noconfirm=False):
if installed_packs.get(pack.id) and not noconfirm:
log(
f"The pack you are trying to install ({Fore.GREEN}{pack.id}{Fore.RESET}) already exists", LogLevel.WARN)
if not ((replace_pack := input("Replace? [y/N]: ").lower()) == "y" or replace_pack == "yes"):
replace_pack = input("Replace? [y/N]: ").lower()
if not (replace_pack == "y" or replace_pack == "yes"):
return

log(f"Installing '{source_zip}' to '{installed_pack_path}'",
Expand Down Expand Up @@ -102,7 +102,8 @@ def remove_pack(pack: Pack, directory: Path):
installed_packs = get_installed_packs(directory)

# Handle possible errors
if not (installed_pack := installed_packs.get(pack.id)):
installed_pack = installed_packs.get(pack.id)
if not installed_pack:
log(f"Pack '{pack.id}' is not installed to '{directory}'",
LogLevel.ERROR)
raise SystemExit(-1)
Expand Down
Loading

0 comments on commit 9d8a761

Please sign in to comment.