Skip to content

Commit 35839ee

Browse files
committed
refactor: use localizations for menubar and structure optimization
- use localizations for menubar - bump AutoGGUF version to v2.0.0 - rename imports_and_globals.py to globals.py - reformat code - use file select for Merge/Split GGUF functions - move general functions verify_gguf and process_args to globals.py - create Plugins class for extensibility
1 parent b1b3a35 commit 35839ee

7 files changed

+188
-151
lines changed

Diff for: src/AutoGGUF.py

+49-146
Large diffs are not rendered by default.

Diff for: src/Localizations.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import re
33

4-
AUTOGGUF_VERSION = "v1.9.0"
4+
AUTOGGUF_VERSION = "v2.0.0"
55

66

77
class _Localization:
@@ -429,6 +429,31 @@ def __init__(self):
429429
self.SELECT_FOLDER = "Select Folder"
430430
self.SELECT_FILE = "Select File"
431431

432+
# Menubar
433+
self.CLOSE = "Close"
434+
self.FILE = "File"
435+
self.FOLDER = "Folder"
436+
self.HELP = "Help"
437+
self.ABOUT = "About"
438+
439+
self.AUTOFP8 = "AutoFP8"
440+
self.TOOLS = "Tools"
441+
self.HF_TRANSFER = "HF Transfer"
442+
self.MERGE_GGUF = "Merge GGUF"
443+
444+
self.HF_UPLOAD = "HF Upload"
445+
self.HF_REPOSITORY = "Repository:"
446+
self.HF_REMOTE_PATH = "Remote Path:"
447+
self.HF_LOCAL_PATH = "Local Path:"
448+
self.MODEL = "Model"
449+
self.DATASET = "Dataset"
450+
self.SPACE = "Space"
451+
self.HF_REPOSITORY_TYPE = "Repository Type"
452+
self.UPLOAD_TYPE = "Upload Type"
453+
self.UPLOAD = "Upload"
454+
455+
self.EXTRA_COMMAND_ARGUMENTS = "Additional command-line arguments"
456+
432457

433458
class _French(_Localization):
434459
def __init__(self):

Diff for: src/Plugins.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import importlib
2+
import os
3+
from typing import Any, Dict
4+
from Localizations import *
5+
6+
7+
class Plugins:
8+
9+
def load_plugins(self) -> Dict[str, Dict[str, Any]]:
10+
plugins = {}
11+
plugin_dir = "plugins"
12+
13+
if not os.path.exists(plugin_dir):
14+
self.logger.info(PLUGINS_DIR_NOT_EXIST.format(plugin_dir))
15+
return plugins
16+
17+
if not os.path.isdir(plugin_dir):
18+
self.logger.warning(PLUGINS_DIR_NOT_DIRECTORY.format(plugin_dir))
19+
return plugins
20+
21+
for file in os.listdir(plugin_dir):
22+
if file.endswith(".py") and not file.endswith(".disabled.py"):
23+
name = file[:-3]
24+
path = os.path.join(plugin_dir, file)
25+
26+
try:
27+
spec = importlib.util.spec_from_file_location(name, path)
28+
module = importlib.util.module_from_spec(spec)
29+
spec.loader.exec_module(module)
30+
31+
for item_name in dir(module):
32+
item = getattr(module, item_name)
33+
if isinstance(item, type) and hasattr(item, "__data__"):
34+
plugin_instance = item()
35+
plugin_data = plugin_instance.__data__()
36+
37+
compatible_versions = plugin_data.get(
38+
"compatible_versions", []
39+
)
40+
if (
41+
"*" in compatible_versions
42+
or AUTOGGUF_VERSION in compatible_versions
43+
):
44+
plugins[name] = {
45+
"instance": plugin_instance,
46+
"data": plugin_data,
47+
}
48+
self.logger.info(
49+
PLUGIN_LOADED.format(
50+
plugin_data["name"], plugin_data["version"]
51+
)
52+
)
53+
else:
54+
self.logger.warning(
55+
PLUGIN_INCOMPATIBLE.format(
56+
plugin_data["name"],
57+
plugin_data["version"],
58+
AUTOGGUF_VERSION,
59+
", ".join(compatible_versions),
60+
)
61+
)
62+
break
63+
except Exception as e:
64+
self.logger.error(PLUGIN_LOAD_FAILED.format(name, str(e)))
65+
66+
return plugins
67+
68+
def apply_plugins(self) -> None:
69+
if not self.plugins:
70+
self.logger.info(NO_PLUGINS_LOADED)
71+
return
72+
73+
for plugin_name, plugin_info in self.plugins.items():
74+
plugin_instance = plugin_info["instance"]
75+
for attr_name in dir(plugin_instance):
76+
if not attr_name.startswith("__") and attr_name != "init":
77+
attr_value = getattr(plugin_instance, attr_name)
78+
setattr(self, attr_name, attr_value)
79+
80+
if hasattr(plugin_instance, "init") and callable(plugin_instance.init):
81+
plugin_instance.init(self)

Diff for: src/QuantizationThread.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from PySide6.QtCore import Signal, QThread
77

8-
from imports_and_globals import open_file_safe
8+
from globals import open_file_safe
99
from Localizations import IN_PROGRESS, COMPLETED
1010

1111

Diff for: src/imports_and_globals.py renamed to src/globals.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import re
33
import sys
4-
from typing import Any, TextIO, Union
4+
from typing import Any, List, TextIO, Union
55

66
from PySide6.QtWidgets import (
77
QMessageBox,
@@ -15,6 +15,34 @@
1515
)
1616

1717

18+
def verify_gguf(file_path) -> bool:
19+
try:
20+
with open(file_path, "rb") as f:
21+
magic = f.read(4)
22+
return magic == b"GGUF"
23+
except (FileNotFoundError, IOError, OSError):
24+
return False
25+
26+
27+
def process_args(args: List[str]) -> bool:
28+
try:
29+
i = 1
30+
while i < len(args):
31+
key = (
32+
args[i][2:].replace("-", "_").upper()
33+
) # Strip the first two '--' and replace '-' with '_'
34+
if i + 1 < len(args) and not args[i + 1].startswith("--"):
35+
value = args[i + 1]
36+
i += 2
37+
else:
38+
value = "enabled"
39+
i += 1
40+
os.environ[key] = value
41+
return True
42+
except Exception:
43+
return False
44+
45+
1846
def load_dotenv(self=Any) -> None:
1947
if not os.path.isfile(".env"):
2048
self.logger.warning(DOTENV_FILE_NOT_FOUND)

Diff for: src/lora_conversion.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from QuantizationThread import QuantizationThread
1313
from TaskListItem import TaskListItem
1414
from error_handling import handle_error, show_error
15-
from imports_and_globals import ensure_directory
15+
from globals import ensure_directory
1616
from Localizations import *
1717

1818

Diff for: src/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from DownloadThread import DownloadThread
88
from Localizations import *
99
from error_handling import show_error
10-
from imports_and_globals import ensure_directory
10+
from globals import ensure_directory
1111
from KVOverrideEntry import KVOverrideEntry
1212

1313

0 commit comments

Comments
 (0)