From b63645104d25b44e52f6e019471b795301deae2e Mon Sep 17 00:00:00 2001 From: Miro <200482516+Mirochill@users.noreply.github.com> Date: Sat, 23 May 2026 15:28:23 +0200 Subject: [PATCH] Replace pkg_resources resource lookups --- pipgui/Package_Management/installedList.py | 7 ++--- pipgui/Package_Management/outdatedList.py | 7 ++--- pipgui/Scraping/genreList.py | 10 ++++---- pipgui/Scraping/packageList.py | 14 +++++----- pipgui/Scraping/pypiList.py | 4 +-- pipgui/__main__.py | 30 +++++++++++----------- pipgui/paths.py | 10 ++++++++ 7 files changed, 47 insertions(+), 35 deletions(-) create mode 100644 pipgui/paths.py diff --git a/pipgui/Package_Management/installedList.py b/pipgui/Package_Management/installedList.py index 3711af0..6c5a967 100644 --- a/pipgui/Package_Management/installedList.py +++ b/pipgui/Package_Management/installedList.py @@ -1,7 +1,8 @@ import json -import pkg_resources from subprocess import getoutput +from pipgui.paths import resource_filename + INSTALLED_DIR = './Resource_Files/Installed Packages/' packages = getoutput('pip freeze') @@ -17,8 +18,8 @@ print('Loaded...') # Dump files -iP = open(pkg_resources.resource_filename('pipgui', INSTALLED_DIR + 'installedPackageList.json'), 'w') -iP3 = open(pkg_resources.resource_filename('pipgui', INSTALLED_DIR + 'installedPackageList3.json'), 'w') +iP = open(resource_filename(INSTALLED_DIR + 'installedPackageList.json'), 'w') +iP3 = open(resource_filename(INSTALLED_DIR + 'installedPackageList3.json'), 'w') json.dump(installedPackages, iP) json.dump(installedPackages3, iP3) diff --git a/pipgui/Package_Management/outdatedList.py b/pipgui/Package_Management/outdatedList.py index f0bb9ac..d1ec7e7 100644 --- a/pipgui/Package_Management/outdatedList.py +++ b/pipgui/Package_Management/outdatedList.py @@ -1,7 +1,8 @@ import json -import pkg_resources from subprocess import getoutput +from pipgui.paths import resource_filename + OUTDATED_DIR = './Resource_Files/Outdated Packages/' print('Loading outdated Packages...') @@ -18,8 +19,8 @@ print('Loaded...') -oP = open(pkg_resources.resource_filename('pipgui', OUTDATED_DIR + 'outdatedPackageList.json'), 'w') -oP3 = open(pkg_resources.resource_filename('pipgui', OUTDATED_DIR + 'outdatedPackageList3.json'), 'w') +oP = open(resource_filename(OUTDATED_DIR + 'outdatedPackageList.json'), 'w') +oP3 = open(resource_filename(OUTDATED_DIR + 'outdatedPackageList3.json'), 'w') json.dump(outdatedPackages, oP) json.dump(outdatedPackages3, oP3) diff --git a/pipgui/Scraping/genreList.py b/pipgui/Scraping/genreList.py index dc57152..7a489e7 100644 --- a/pipgui/Scraping/genreList.py +++ b/pipgui/Scraping/genreList.py @@ -1,15 +1,15 @@ import re import json -import pkg_resources from bs4 import BeautifulSoup +from pipgui.paths import resource_filename PKG_SOURCE_DIR = '../Resource_Files/Package Sources/' genrePattern = re.compile(r'#[\w-]+') #Source Code File -file = open(pkg_resources.resource_filename('pipgui', PKG_SOURCE_DIR + 'db.txt')) +file = open(resource_filename(PKG_SOURCE_DIR + 'db.txt')) src = BeautifulSoup(file.read(), 'lxml') #List of all genre 'a' tags @@ -24,9 +24,9 @@ genres = {i.string:i['href'] for i in aTagList if i.string != None} #JSON dump files -gL = open(pkg_resources.resource_filename('pipgui', PKG_SOURCE_DIR + 'genreListFile.json'), 'w') -gT = open(pkg_resources.resource_filename('pipgui', PKG_SOURCE_DIR + 'genreTagFile.json'), 'w') -g = open(pkg_resources.resource_filename('pipgui', PKG_SOURCE_DIR + 'genreFile.json'), 'w') +gL = open(resource_filename(PKG_SOURCE_DIR + 'genreListFile.json'), 'w') +gT = open(resource_filename(PKG_SOURCE_DIR + 'genreTagFile.json'), 'w') +g = open(resource_filename(PKG_SOURCE_DIR + 'genreFile.json'), 'w') json.dump(genreList, gL) json.dump(genreTags, gT) diff --git a/pipgui/Scraping/packageList.py b/pipgui/Scraping/packageList.py index 52594df..67a0244 100644 --- a/pipgui/Scraping/packageList.py +++ b/pipgui/Scraping/packageList.py @@ -1,6 +1,6 @@ import json -import pkg_resources from bs4 import BeautifulSoup +from pipgui.paths import resource_filename PKG_SOURCE_DIR = '../Resource_Files/Package Sources/' ASSETS_DIR = '../Resource_Files/Assets/' @@ -11,16 +11,16 @@ def indices(string, substring): return [string.find(substring), string.find(substring) + string[string.find(substring) + 1:].find(substring)] #Source Code File -file = open(pkg_resources.resource_filename('pipgui', PKG_SOURCE_DIR + 'db.txt')) +file = open(resource_filename(PKG_SOURCE_DIR + 'db.txt')) src = str(file.read()) file.close() #List of all Genres and their 'a' tag HREF values -genreList = json.load(open(pkg_resources.resource_filename('pipgui', PKG_SOURCE_DIR + 'genreListFile.json'))) -genreTags = json.load(open(pkg_resources.resource_filename('pipgui', PKG_SOURCE_DIR + 'genreTagFile.json'))) +genreList = json.load(open(resource_filename(PKG_SOURCE_DIR + 'genreListFile.json'))) +genreTags = json.load(open(resource_filename(PKG_SOURCE_DIR + 'genreTagFile.json'))) #Genre and HREF value dictionary -genres = json.load(open(pkg_resources.resource_filename('pipgui', PKG_SOURCE_DIR + 'genreFile.json'))) +genres = json.load(open(resource_filename(PKG_SOURCE_DIR + 'genreFile.json'))) #Tag values and their indices indexDict = {i:max(indices(src, i)) for i in genreTags} @@ -33,6 +33,6 @@ def substr(string, start, tag='ul'): #Dictionary of genre tags with all relative packages packageDict = {i:substr(src, indexDict[i]) for i in genreTags} -json.dump(packageDict, open(pkg_resources.resource_filename('pipgui', PACKAGE_DIR + 'packageDictFile.json'), 'w')) +json.dump(packageDict, open(resource_filename(PACKAGE_DIR + 'packageDictFile.json'), 'w')) -k = json.load(open(pkg_resources.resource_filename('pipgui', PACKAGE_DIR + 'packageDictFile.json'))) +k = json.load(open(resource_filename(PACKAGE_DIR + 'packageDictFile.json'))) diff --git a/pipgui/Scraping/pypiList.py b/pipgui/Scraping/pypiList.py index 23cf1fd..3e800c0 100644 --- a/pipgui/Scraping/pypiList.py +++ b/pipgui/Scraping/pypiList.py @@ -1,8 +1,8 @@ import json -import pkg_resources from urllib.request import urlopen from bs4 import BeautifulSoup +from pipgui.paths import resource_filename PACKAGE_DIR = './Resource_Files/Current Packages/' @@ -14,7 +14,7 @@ for i in soup.find_all('a'): pypi_list.append(i['href']) -file = open(pkg_resources.resource_filename('pipgui', PACKAGE_DIR + 'packageList.json'), 'w') +file = open(resource_filename(PACKAGE_DIR + 'packageList.json'), 'w') json.dump(pypi_list, file) print("All Packages are updated!!") diff --git a/pipgui/__main__.py b/pipgui/__main__.py index 1c0fa5f..455b938 100644 --- a/pipgui/__main__.py +++ b/pipgui/__main__.py @@ -3,9 +3,9 @@ import json import sys -import pkg_resources from PyQt5 import QtCore, QtGui, QtWidgets +from pipgui.paths import resource_filename # Importing GUIs try: @@ -68,7 +68,7 @@ def __init__(self): super(ProgressWindow, self).__init__() self.setupUi(self) self.setWindowIcon( - QtGui.QIcon(pkg_resources.resource_filename('pipgui', ASSETS_DIR + 'googledev.png'))) + QtGui.QIcon(resource_filename(ASSETS_DIR + 'googledev.png'))) # QProcess object for external app self.process = QtCore.QProcess(self) @@ -144,7 +144,7 @@ def __init__(self): super(MainWindow, self).__init__() self.setupUi(self) self.setWindowIcon( - QtGui.QIcon(pkg_resources.resource_filename('pipgui', ASSETS_DIR + 'googledev.png'))) + QtGui.QIcon(resource_filename(ASSETS_DIR + 'googledev.png'))) # Check for python version if sys.version_info.major == 3: self.radioPy3.setChecked(True) @@ -217,9 +217,9 @@ def __init__(self): super(UpdateWindow, self).__init__() self.setupUi(self) self.setWindowIcon( - QtGui.QIcon(pkg_resources.resource_filename('pipgui', ASSETS_DIR + 'googledev.png'))) + QtGui.QIcon(resource_filename(ASSETS_DIR + 'googledev.png'))) self.outdatedPackages = json.load(open( - pkg_resources.resource_filename('pipgui', OUTDATED_DIR + 'outdatedPackage' + FILEVERSION + '.json'))) + resource_filename(OUTDATED_DIR + 'outdatedPackage' + FILEVERSION + '.json'))) self.selectedList = list() self.btnBack.clicked.connect(self.backFn) self.btnUpdateAll.clicked.connect(self.updateAllFn) @@ -251,7 +251,7 @@ def updateFn(self): ['install'] + self.selectedList, 2) # print 'Selected Packages Updated' - with open(pkg_resources.resource_filename('pipgui', OUTDATED_DIR + 'outdatedPackage' + FILEVERSION + '.json'), + with open(resource_filename(OUTDATED_DIR + 'outdatedPackage' + FILEVERSION + '.json'), 'w') as file: json.dump(self.outdatedPackages, file) @@ -263,7 +263,7 @@ def updateAllFn(self): self.progWindow.callProgram(VERSION, ['install'] + self.outdatedPackages, 2) # print 'All Packages Updated.' - with open(pkg_resources.resource_filename('pipgui', OUTDATED_DIR + 'outdatedPackage' + FILEVERSION + '.json'), + with open(resource_filename(OUTDATED_DIR + 'outdatedPackage' + FILEVERSION + '.json'), 'w') as file: json.dump([], file) @@ -287,9 +287,9 @@ def __init__(self): super(UninstallWindow, self).__init__() self.setupUi(self) self.setWindowIcon( - QtGui.QIcon(pkg_resources.resource_filename('pipgui', ASSETS_DIR + 'googledev.png'))) + QtGui.QIcon(resource_filename(ASSETS_DIR + 'googledev.png'))) self.allPackages = json.load(open( - pkg_resources.resource_filename('pipgui', INSTALLED_DIR + 'installedPackage' + FILEVERSION + '.json'))) + resource_filename(INSTALLED_DIR + 'installedPackage' + FILEVERSION + '.json'))) self.btnBack.clicked.connect(self.backFn) self.btnUninstallAll.clicked.connect(self.uninstallAllFn) self.btnUninstall.clicked.connect(self.uninstallFn) @@ -318,7 +318,7 @@ def uninstallFn(self): # print 'Selected Packages Uninstalled' self.selectedList = list() - with open(pkg_resources.resource_filename('pipgui', + with open(resource_filename( INSTALLED_DIR + 'installedPackage' + FILEVERSION + '.json'), 'w') as file: json.dump(self.allPackages, file) @@ -335,7 +335,7 @@ def uninstallAllFn(self): self.progWindow.callProgram(VERSION, ['uninstall'] + self.allPackages, 5) # print 'All Packages Uninstalled.' - with open(pkg_resources.resource_filename('pipgui', + with open(resource_filename( INSTALLED_DIR + 'installedPackage' + FILEVERSION + '.json'), 'w') as file: json.dump([], file) @@ -362,11 +362,11 @@ def __init__(self): super(InstallWindow, self).__init__() self.setupUi(self) self.setWindowIcon( - QtGui.QIcon(pkg_resources.resource_filename('pipgui', ASSETS_DIR + 'googledev.png'))) + QtGui.QIcon(resource_filename(ASSETS_DIR + 'googledev.png'))) self.offlinePackages = json.load(open( - pkg_resources.resource_filename('pipgui', INSTALLED_DIR + 'installedPackage' + FILEVERSION + '.json'))) + resource_filename(INSTALLED_DIR + 'installedPackage' + FILEVERSION + '.json'))) self.packages = json.load( - open(pkg_resources.resource_filename('pipgui', PACKAGE_DIR + 'package' + FILEVERSION + '.json'))) + open(resource_filename(PACKAGE_DIR + 'package' + FILEVERSION + '.json'))) self.matchedList = list() self.selectedList = list() self.searchStr = str() @@ -401,7 +401,7 @@ def installFn(self): self.progWindow.callProgram(VERSION, ['install'] + self.selectedList, 1) # print 'Selected Packages Installed' - with open(pkg_resources.resource_filename('pipgui', INSTALLED_DIR + 'installedPackage' + FILEVERSION + '.json'), + with open(resource_filename(INSTALLED_DIR + 'installedPackage' + FILEVERSION + '.json'), 'w') as file: json.dump(sorted(self.offlinePackages), file) self.close() diff --git a/pipgui/paths.py b/pipgui/paths.py new file mode 100644 index 0000000..dc6e30c --- /dev/null +++ b/pipgui/paths.py @@ -0,0 +1,10 @@ +import os + + +PACKAGE_ROOT = os.path.dirname(os.path.abspath(__file__)) + + +def resource_filename(resource): + while resource.startswith(("./", "../")): + resource = resource[2:] if resource.startswith("./") else resource[3:] + return os.path.join(PACKAGE_ROOT, *resource.split("/"))