Skip to content

Commit

Permalink
support faster loading in ToolMate AI
Browse files Browse the repository at this point in the history
  • Loading branch information
eliranwong committed Oct 26, 2024
1 parent 1ff5533 commit 32869f6
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 188 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
# https://packaging.python.org/en/latest/guides/distributing-packages-using-setuptools/
setup(
name=package,
version="0.1.18",
version="0.1.21",
python_requires=">=3.8, <3.13",
description=f"UniqueBible App is a cross-platform & offline bible application, integrated with high-quality resources and unique features. Developers: Eliran Wong and Oliver Tseng",
long_description=long_description,
Expand Down
64 changes: 0 additions & 64 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,64 +0,0 @@
import os, requests, pkg_resources
from shutil import copytree
from pathlib import Path
from packaging import version


# go to resource directory
thisFile = os.path.realpath(__file__)
wd = os.path.dirname(thisFile)
if os.getcwd() != wd:
os.chdir(wd)

ubahome = os.path.expanduser(os.path.join("~", "UniqueBible"))

if not os.path.isdir(ubahome):
Path(ubahome).mkdir(parents=True, exist_ok=True)
for i in ("audio", "htmlResources", "import", "macros", "marvelData", "music", "notes", "temp", "terminal_history", "terminal_mode", "thirdParty", "video", "webstorage", "workspace"):
targetFolder = os.path.join(ubahome, i)
if not os.path.isdir(targetFolder):
copytree(i, targetFolder, dirs_exist_ok=True)
#copytree(i, os.path.join(ubahome, i), dirs_exist_ok=True)

# Add folders below for all new folders created after version 0.1.17
# ...

# change directory
if os.getcwd() != ubahome:
os.chdir(ubahome)

# user plugins; create folders for users to place their own plugins
# TODO: user plugins not working for now; will implement later
for i in ("chatGPT", "config", "context", "event", "language", "layout", "menu", "shutdown", "startup", "terminal", "text_editor"):
Path(os.path.join(ubahome, "plugins", i)).mkdir(parents=True, exist_ok=True)


def getPackageInstalledVersion(package):
try:
installed_version = pkg_resources.get_distribution(package).version
return version.parse(installed_version)
except pkg_resources.DistributionNotFound:
return None

def getPackageLatestVersion(package):
try:
response = requests.get(f"https://pypi.org/pypi/{package}/json", timeout=10)
latest_version = response.json()['info']['version']
return version.parse(latest_version)
except:
return None

thisPackage = "uniquebible"

installed_version = getPackageInstalledVersion(thisPackage)
if installed_version is None:
print("Installed version information is not accessible!")
else:
print(f"Installed version: {installed_version}")
latest_version = getPackageLatestVersion(thisPackage)
if latest_version is None:
print("Latest version information is not accessible at the moment!")
elif installed_version is not None:
print(f"Latest version: {latest_version}")
if latest_version > installed_version:
print("Run `pip install --upgrade uniquebible` to upgrade!")
2 changes: 2 additions & 0 deletions uniquebible/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def getPackageLatestVersion(package):

thisPackage = "uniquebible"

print(f"Checking '{thisPackage}' version ...")

installed_version = getPackageInstalledVersion(thisPackage)
if installed_version is None:
print("Installed version information is not accessible!")
Expand Down
6 changes: 4 additions & 2 deletions uniquebible/util/BibleVerseParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@
"""
import logging
import os
import importlib.resources

# File "config.py" is essential for running module "config"
# Create file "config.py" if it is missing.
# The following two lines are written for use of this parser outside UniqueBible.app
import pprint

if not os.path.isfile("config.py"):
open("config.py", "w", encoding="utf-8").close()
configFile = os.path.join(str(importlib.resources.files("uniquebible")), "config.py")
if not os.path.isfile(configFile):
open(configFile, "w", encoding="utf-8").close()

# import modules, which are ESSENTIAL for running BibleVerseParser
import re, glob, sys
Expand Down
10 changes: 8 additions & 2 deletions uniquebible/util/ConfigUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@ def setup(noQt=None, cli=None, enableCli=None, enableApiServer=None, enableHttpS
config.isChromeOS = True if config.thisOS == "Linux" and os.path.exists("/mnt/chromeos/") else False

# check current directory
config.ubaUserDir = os.getcwd()
ubaUserDir = os.path.join(os.path.expanduser("~"), "UniqueBible")
config.ubaUserDir = ubaUserDir if os.path.isdir(ubaUserDir) else os.getcwd()
config.packageDir = str(importlib.resources.files("uniquebible"))

userMarvelData = os.path.join(config.ubaUserDir, "marvelData")
if config.marvelData == "marvelData" and os.path.isdir(userMarvelData):
config.marvelData = userMarvelData

# check running mode
config.runMode = sys.argv[1] if len(sys.argv) > 1 else ""
Expand Down Expand Up @@ -1683,7 +1688,8 @@ def save():
config.bookSearchString = ""
config.noteSearchString = ""
#config.instantHighlightString = ""
with open("config.py", "w", encoding="utf-8") as fileObj:
configFile = os.path.join(config.packageDir, "config.py")
with open(configFile, "w", encoding="utf-8") as fileObj:
for name in config.help.keys():
try:
value = eval(f"config.{name}")
Expand Down
4 changes: 3 additions & 1 deletion uniquebible/util/FileUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from itertools import (takewhile, repeat)
import platform
from urllib.parse import urlsplit, urlunsplit
import importlib.resources


class FileUtil:
Expand Down Expand Up @@ -37,7 +38,8 @@ def createCustomFiles():
# "custom.js" is essential for custom javascript feature.
customCssFile = os.path.join("htmlResources", "css", "custom.css")
customJsFile = os.path.join("htmlResources", "js", "custom.js")
userFiles = ("config.py", customCssFile, customJsFile)
configFile = os.path.join(str(importlib.resources.files("uniquebible")), "config.py")
userFiles = (configFile, customCssFile, customJsFile)
for userFile in userFiles:
if not os.path.isfile(userFile):
open(userFile, "a", encoding="utf-8").close()
Expand Down
10 changes: 6 additions & 4 deletions uniquebible/util/LocalCliHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,7 @@ def getContent(self, command, checkDotCommand=True):
config.mainB, config.mainC, config.mainV, *_ = references[-1]
return plainText
except:
#print(traceback.format_exc())
return self.printInvalidOptionEntered()

def fineTuneTextForWebBrowserDisplay(self, text=""):
Expand Down Expand Up @@ -3729,12 +3730,13 @@ def changeconfig(self, terminalCommandOnly=False):
return self.printInvalidOptionEntered()

def editConfig(self, editor):
if not os.path.isfile("config.py"):
configFile = os.path.join(config.packageDir, "config.py")
if not os.path.isfile(configFile):
return ""
self.print(self.divider)
self.print("Caution! Editing 'config.py' incorrectly may stop UBA from working.")
if not editor:
changesMade = self.multilineEditor(filepath="config.py")
changesMade = self.multilineEditor(filepath=configFile)
if changesMade:
config.saveConfigOnExit = False
self.print(self.divider)
Expand All @@ -3746,8 +3748,8 @@ def editConfig(self, editor):
userInput = userInput.lower()
if userInput in ("y", "yes"):
self.print("reading config content ...")
if os.path.isfile("config.py"):
with open("config.py", "r", encoding="utf-8") as input_file:
if os.path.isfile(configFile):
with open(configFile, "r", encoding="utf-8") as input_file:
content = input_file.read()
self.print("config is ready for editing ...")
self.print("To apply changes, save as 'config.py' and replace the existing 'config.py' when you finish editing.")
Expand Down
5 changes: 4 additions & 1 deletion uniquebible/util/RemoteCliMainWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ def __init__(self):
self.bibleInfo = DatafileLocation.marvelBibles
if not config.enableHttpServer:
self.setupResourceLists()
config.thisTranslation = LanguageUtil.loadTranslation(config.displayLanguage)
try:
config.thisTranslation = LanguageUtil.loadTranslation(config.displayLanguage)
except:
pass

def importModulesInFolder(self, directory="import"):
if os.path.isdir(directory):
Expand Down
Loading

0 comments on commit 32869f6

Please sign in to comment.