Better handling of standard paths (#187)

This commit is contained in:
Aleksana 2024-08-04 04:07:14 +08:00 committed by GitHub
parent 131e8fb6be
commit 5a0d1ed408
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 43 additions and 18 deletions

22
src/internal.py Normal file
View File

@ -0,0 +1,22 @@
import os
app_id = "com.jeffser.Alpaca"
in_flatpak = True if os.getenv("FLATPAK_ID") else False
def get_xdg_home(env, default):
if in_flatpak:
return os.getenv(env)
else:
base = os.getenv(env) or os.path.expanduser(default)
path = os.path.join(base, app_id)
if not os.path.exists(path):
os.makedirs(path)
return path
data_dir = get_xdg_home("XDG_DATA_HOME", "~/.local/share")
config_dir = get_xdg_home("XDG_CONFIG_HOME", "~/.config")
cache_dir = get_xdg_home("XDG_CACHE_HOME", "~/.cache")
source_dir = os.path.abspath(os.path.dirname(__file__))

View File

@ -2,24 +2,24 @@
import subprocess, os, threading import subprocess, os, threading
from time import sleep from time import sleep
from logging import getLogger from logging import getLogger
from .internal import data_dir, cache_dir
logger = getLogger(__name__) logger = getLogger(__name__)
instance = None instance = None
port = 11435 port = 11435
data_dir = os.getenv("XDG_DATA_HOME")
overrides = {} overrides = {}
def start(): def start():
if not os.path.isdir(os.path.join(os.getenv("XDG_CACHE_HOME"), 'tmp/ollama')): if not os.path.isdir(os.path.join(cache_dir, 'tmp/ollama')):
os.mkdir(os.path.join(os.getenv("XDG_CACHE_HOME"), 'tmp/ollama')) os.mkdir(os.path.join(cache_dir, 'tmp/ollama'))
global instance, overrides global instance, overrides
params = overrides.copy() params = overrides.copy()
params["OLLAMA_HOST"] = f"127.0.0.1:{port}" # You can't change this directly sorry :3 params["OLLAMA_HOST"] = f"127.0.0.1:{port}" # You can't change this directly sorry :3
params["HOME"] = data_dir params["HOME"] = data_dir
params["TMPDIR"] = os.path.join(os.getenv("XDG_CACHE_HOME"), 'tmp/ollama') params["TMPDIR"] = os.path.join(cache_dir, 'tmp/ollama')
instance = subprocess.Popen(["/app/bin/ollama", "serve"], env={**os.environ, **params}, stderr=subprocess.PIPE, text=True) instance = subprocess.Popen(["ollama", "serve"], env={**os.environ, **params}, stderr=subprocess.PIPE, text=True)
logger.info("Starting Alpaca's Ollama instance...") logger.info("Starting Alpaca's Ollama instance...")
logger.debug(params) logger.debug(params)
sleep(1) sleep(1)

View File

@ -27,6 +27,7 @@ gi.require_version('Adw', '1')
from gi.repository import Gtk, Gio, Adw, GLib from gi.repository import Gtk, Gio, Adw, GLib
from .window import AlpacaWindow from .window import AlpacaWindow
from .internal import cache_dir, data_dir
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -63,7 +64,7 @@ class AlpacaApplication(Adw.Application):
issue_url='https://github.com/Jeffser/Alpaca/issues', issue_url='https://github.com/Jeffser/Alpaca/issues',
license_type=3, license_type=3,
website="https://jeffser.com/alpaca", website="https://jeffser.com/alpaca",
debug_info=open(os.path.join(os.getenv("XDG_DATA_HOME"), 'tmp.log'), 'r').read()) debug_info=open(os.path.join(data_dir, 'tmp.log'), 'r').read())
about.present(parent=self.props.active_window) about.present(parent=self.props.active_window)
def create_action(self, name, callback, shortcuts=None): def create_action(self, name, callback, shortcuts=None):
@ -75,16 +76,16 @@ class AlpacaApplication(Adw.Application):
def main(version): def main(version):
if os.path.isfile(os.path.join(os.getenv("XDG_DATA_HOME"), 'tmp.log')): if os.path.isfile(os.path.join(data_dir, 'tmp.log')):
os.remove(os.path.join(os.getenv("XDG_DATA_HOME"), 'tmp.log')) os.remove(os.path.join(data_dir, 'tmp.log'))
if os.path.isdir(os.path.join(os.getenv("XDG_CACHE_HOME"), 'tmp')): if os.path.isdir(os.path.join(cache_dir, 'tmp')):
os.system('rm -rf ' + os.path.join(os.getenv("XDG_CACHE_HOME"), "tmp/*")) os.system('rm -rf ' + os.path.join(cache_dir, "tmp/*"))
else: else:
os.mkdir(os.path.join(os.getenv("XDG_CACHE_HOME"), 'tmp')) os.mkdir(os.path.join(cache_dir, 'tmp'))
logging.basicConfig( logging.basicConfig(
format="%(levelname)s\t[%(filename)s | %(funcName)s] %(message)s", format="%(levelname)s\t[%(filename)s | %(funcName)s] %(message)s",
level=logging.INFO, level=logging.INFO,
handlers=[logging.FileHandler(filename=os.path.join(os.getenv("XDG_DATA_HOME"), 'tmp.log')), logging.StreamHandler(stream=sys.stdout)] handlers=[logging.FileHandler(filename=os.path.join(data_dir, 'tmp.log')), logging.StreamHandler(stream=sys.stdout)]
) )
app = AlpacaApplication(version) app = AlpacaApplication(version)
logger.info(f"Alpaca version: {app.version}") logger.info(f"Alpaca version: {app.version}")

View File

@ -44,7 +44,8 @@ alpaca_sources = [
'local_instance.py', 'local_instance.py',
'available_models.json', 'available_models.json',
'available_models_descriptions.py', 'available_models_descriptions.py',
'table_widget.py' 'table_widget.py',
'internal.py'
] ]
install_data(alpaca_sources, install_dir: moduledir) install_data(alpaca_sources, install_dir: moduledir)

View File

@ -29,20 +29,21 @@ from pypdf import PdfReader
from datetime import datetime from datetime import datetime
from . import dialogs, local_instance, connection_handler, available_models_descriptions from . import dialogs, local_instance, connection_handler, available_models_descriptions
from .table_widget import TableWidget from .table_widget import TableWidget
from .internal import config_dir, data_dir, cache_dir, source_dir
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@Gtk.Template(resource_path='/com/jeffser/Alpaca/window.ui') @Gtk.Template(resource_path='/com/jeffser/Alpaca/window.ui')
class AlpacaWindow(Adw.ApplicationWindow): class AlpacaWindow(Adw.ApplicationWindow):
config_dir = os.getenv("XDG_CONFIG_HOME")
data_dir = os.getenv("XDG_DATA_HOME")
app_dir = os.getenv("FLATPAK_DEST") app_dir = os.getenv("FLATPAK_DEST")
cache_dir = os.getenv("XDG_CACHE_HOME") config_dir = config_dir
data_dir = data_dir
cache_dir = cache_dir
__gtype_name__ = 'AlpacaWindow' __gtype_name__ = 'AlpacaWindow'
localedir = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'locale') localedir = os.path.join(source_dir, 'locale')
gettext.bindtextdomain('com.jeffser.Alpaca', localedir) gettext.bindtextdomain('com.jeffser.Alpaca', localedir)
gettext.textdomain('com.jeffser.Alpaca') gettext.textdomain('com.jeffser.Alpaca')
@ -1602,7 +1603,7 @@ Generate a title following these rules:
def __init__(self, **kwargs): def __init__(self, **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
GtkSource.init() GtkSource.init()
with open('/app/share/Alpaca/alpaca/available_models.json', 'r') as f: with open(os.path.join(source_dir, 'available_models.json'), 'r') as f:
self.available_models = json.load(f) self.available_models = json.load(f)
if not os.path.exists(os.path.join(self.data_dir, "chats")): if not os.path.exists(os.path.join(self.data_dir, "chats")):
os.makedirs(os.path.join(self.data_dir, "chats")) os.makedirs(os.path.join(self.data_dir, "chats"))