Skip to content

Commit

Permalink
feat: auto switch lang on first init (fix #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoPlagiarism committed Nov 20, 2024
1 parent 3cba6c5 commit 1f7c3b4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/osettings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import logging
import json

from pyflowlauncher import Result, ResultResponse
Expand All @@ -9,6 +10,7 @@

import typing as t

logger = logging.getLogger(__name__)

@dataclass
class ExtSearch:
Expand All @@ -31,6 +33,8 @@ def from_dict(cls, data):
return cls(**data)

class OSettings:
first_initial: bool = False

def __init__(self, data: t.Optional[dict] = None):
self._data = data if data else dict()
self.initialize()
Expand All @@ -40,7 +44,8 @@ def load(self):
with open(OSETTINGS_FILE, mode="r", encoding="utf-8") as f:
self._data = json.load(f)
except FileNotFoundError:
pass
logger.info("First init")
self.first_initial = True

def save(self):
with open(OSETTINGS_FILE, mode="w+", encoding="utf-8") as f:
Expand All @@ -53,6 +58,9 @@ def initialize(self):
self._data["external_links"] = list()
if "external_search" not in self._data:
self._data["external_search"] = list()

if self.first_initial:
self.save()

@classmethod
def create_empty(cls):
Expand Down
25 changes: 22 additions & 3 deletions src/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import json
import logging

from pyflowlauncher import Plugin, Result, send_results, api, ResultResponse
from pyflowlauncher import Plugin, Result, send_results as _send_results, api, ResultResponse

from .osettings_menu import OSettingsMenu
from .osettings_menu import OSettingsMenu, osettings as _osettings
from .result import ResultConstructor
from .graphql_queries import GraphQLQueryConstructor
from .search import SearchQLClient
from .shared import FS_ICO_PATH, SETTINGS_TYPE, SETTINGS_FILE
from .shared import FS_ICO_PATH, SETTINGS_TYPE, SETTINGS_FILE, FL_SETTINGS_FILE

import typing as t

Expand All @@ -31,6 +31,25 @@ def get_settings():
osettings = OSettingsMenu(lang=lang)


if _osettings.first_initial:
try:
with open(FL_SETTINGS_FILE, mode="r", encoding="utf-8") as f:
fl_settings = json.load(f)
lang = fl_settings.get("Language")
logger.info(f"Found FL lang {lang}")

if lang in ("ru", "uk-UA"):
def send_results(results: t.Iterable[Result]) -> ResultResponse:
return _send_results(results=results, settings={"language": "Russian"})
else:
send_results = _send_results
except Exception as e:
logger.warn(f"Got exc {e} while trying to fetch language")
send_results = _send_results
else:
send_results = _send_results


class SearchTags:
TAG_ANIME = "a"
TAG_MANGA = "m"
Expand Down
1 change: 1 addition & 0 deletions src/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
})

FLOW_PROGRAM_DIRECTORY = os.environ.get("FLOW_PROGRAM_DIRECTORY")
FL_SETTINGS_FILE = os.path.join(FLOW_PROGRAM_DIRECTORY, "UserData", "Settings", "Settings.json") if FLOW_PROGRAM_DIRECTORY else None
PLUGIN_SETTINGS_DIRECTORY = os.path.join(FLOW_PROGRAM_DIRECTORY, "UserData", "Settings", "Plugins", "ShikiFlow") if FLOW_PROGRAM_DIRECTORY else None
SETTINGS_FILE = os.path.join(PLUGIN_SETTINGS_DIRECTORY, "Settings.json") if PLUGIN_SETTINGS_DIRECTORY else None
OSETTINGS_FILE = os.path.join(PLUGIN_SETTINGS_DIRECTORY, "osettings.json") if PLUGIN_SETTINGS_DIRECTORY else None
Expand Down

0 comments on commit 1f7c3b4

Please sign in to comment.