Package manager app started using tkinter
This commit is contained in:
2
source/packages/__init__.py
Normal file
2
source/packages/__init__.py
Normal file
@ -0,0 +1,2 @@
|
||||
|
||||
from .manager import PackageManager
|
14
source/packages/errors.py
Normal file
14
source/packages/errors.py
Normal file
@ -0,0 +1,14 @@
|
||||
class FilepathNotFound(Exception):
|
||||
pass
|
||||
|
||||
class GamePackageMissing(Exception):
|
||||
pass
|
||||
|
||||
class GamePackageMalformed(Exception):
|
||||
pass
|
||||
|
||||
class PackageAlreadyLoaded(Exception):
|
||||
pass
|
||||
|
||||
class InvalidPackageType(Exception):
|
||||
pass
|
41
source/packages/manager.py
Normal file
41
source/packages/manager.py
Normal file
@ -0,0 +1,41 @@
|
||||
|
||||
import os, json
|
||||
from .package import Package
|
||||
from .errors import *
|
||||
|
||||
class PackageManager:
|
||||
def __init__(self, vfs):
|
||||
self.vfs = vfs
|
||||
self.package_map = {}
|
||||
self.package_list = []
|
||||
self.main_package = None
|
||||
|
||||
def get_package_meta(self, filepath):
|
||||
if not os.path.exists(filepath):
|
||||
raise FilepathNotFound(filepath)
|
||||
gamepkg_path = os.path.join(filepath, 'gamepackage.json')
|
||||
if not os.path.exists(gamepkg_path):
|
||||
raise GamePackageMissing(gamepkg_path)
|
||||
meta = json.load(open(gamepkg_path))
|
||||
if 'name' not in meta or 'type' not in meta:
|
||||
raise GamePackageMalformed(gamepkg_path)
|
||||
return meta
|
||||
|
||||
def set_main_package(self, filepath):
|
||||
meta = self.get_package_meta(filepath)
|
||||
if meta['type'] != 'main':
|
||||
raise InvalidPackageType(filepath, meta['type'])
|
||||
vfs_path = f'/main/'
|
||||
self.vfs.mount(filepath, vfs_path, 0)
|
||||
new_package = Package(type('', (object,), meta), vfs_path, self.vfs)
|
||||
self.main_package = new_package
|
||||
|
||||
def include_package(self, filepath):
|
||||
meta = self.get_package_meta(filepath)
|
||||
if meta['name'] in self.package_map:
|
||||
raise PackageAlreadyLoaded(meta['name'])
|
||||
vfs_path = f'/include/{meta["name"]}'
|
||||
self.vfs.mount(filepath, vfs_path, 0)
|
||||
new_package = Package(type('', (object,), meta), vfs_path, self.vfs)
|
||||
self.package_map[meta['name']] = new_package
|
||||
self.package_list.insert(0, new_package)
|
32
source/packages/package.py
Normal file
32
source/packages/package.py
Normal file
@ -0,0 +1,32 @@
|
||||
|
||||
import posixpath
|
||||
from direct.stdpy.file import walk
|
||||
|
||||
class Package:
|
||||
def __init__(self, meta, mount_dir, vfs):
|
||||
self.meta = meta
|
||||
self.mount_dir = mount_dir
|
||||
self.vfs = vfs
|
||||
|
||||
def all_models(self):
|
||||
models_path = posixpath.join(self.mount_dir, 'models')
|
||||
output = []
|
||||
for root, paths, files in walk(models_path):
|
||||
rel_root = root.replace(f'{models_path}', '').strip()
|
||||
if len(rel_root) > 0 and rel_root[0] == '/':
|
||||
rel_root = rel_root[1:]
|
||||
for filepath in files:
|
||||
output.append(posixpath.join(rel_root, filepath))
|
||||
return output
|
||||
|
||||
def all_textures(self):
|
||||
textures_path = posixpath.join(self.mount_dir, 'textures')
|
||||
output = []
|
||||
for root, paths, files in walk(textures_path):
|
||||
rel_root = root.replace(f'{textures_path}', '').strip()
|
||||
if len(rel_root) > 0 and rel_root[0] == '/':
|
||||
rel_root = rel_root[1:]
|
||||
for filepath in files:
|
||||
output.append(posixpath.join(rel_root, filepath))
|
||||
return output
|
||||
|
Reference in New Issue
Block a user