Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add WO_ANKI env var #40

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,12 @@ cd local-audio-yomichan
# You must fill `plugin/user_files` with the audio files, like with step 3 of the main instructions.
# You can run one of the following OS-specific commands:
mklink /d %LOCALAPPDATA%/local-audio-yomichan %APPDATA%/Anki2/addons21/1045800357/user_files # Windows (requires elavated priviledges)
ln -s ~/.local/share/Anki2/addons21/1045800357/user_files ~/local/share/local-audio-yomichan # Linux
ln -s ~/.local/share/Anki2/addons21/1045800357/user_files ~/.local/share/local-audio-yomichan # Linux
ln -s ~/Library/Application\ Support/Anki2/addons21/1045800357/user_files ~/Library/Application\ Support/local-audio-yomichan # MacOS

# After filling in `plugin/user_files` with the audio files, you can now run the server.
# Ensure you have python 3.9 or above.
python3 run_server.py
WO_ANKI=1 python3 run_server.py
```

## Install from Source
Expand Down
50 changes: 42 additions & 8 deletions plugin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,36 @@
from pathlib import Path
from typing import NamedTuple, Optional
from dataclasses import dataclass
from enum import Enum, auto

class Environment(Enum):
ANKI = auto()
WINDOWS = auto()
LINUX = auto()
DARWIN = auto()

@classmethod
def check(cls):

VAR_NAME = "WO_ANKI"

# check if the env var WO_ANKI is set
if os.environ.get(VAR_NAME, "").strip().lower() in ("1", "true"):
sys_platform = platform.system()
if sys_platform == "Windows":
return cls.WINDOWS
elif sys_platform == "Linux":
return cls.LINUX
elif sys_platform == "Darwin":
return cls.DARWIN
else:
raise Exception(f"Unknown platform: {sys_platform}")
elif importlib.util.find_spec("aqt"):
return cls.ANKI
else:
raise Exception(f"Could not determine environment, set the environment variable {VAR_NAME} if you are running without Anki.")



from .consts import APP_NAME, DB_FILE_NAME, ANDROID_DB_FILE_NAME, LATEST_VERSION_FILE_NAME

Expand Down Expand Up @@ -75,13 +105,15 @@ def get_data_dir():
"""
returns the native, platform-specific directory for the application data directory
"""
if importlib.util.find_spec("aqt"):
env = Environment.check()

if env == Environment.ANKI:
return get_anki_data_dir()
elif platform.system() == "Windows":
elif env == Environment.WINDOWS:
return get_win_data_dir()
elif platform.system() == "Linux":
elif env == Environment.LINUX:
return get_linux_data_dir()
elif platform.system() == "Darwin":
elif env == Environment.DARWIN:
return get_mac_data_dir()
else:
return get_anki_data_dir()
Expand All @@ -103,13 +135,15 @@ def get_config_dir():
"""
returns the native, platform-specific directory for the application config directory
"""
if importlib.util.find_spec("aqt"):
env = Environment.check()

if env == Environment.ANKI:
return get_anki_config_dir()
elif platform.system() == "Windows":
elif env == Environment.WINDOWS:
return get_win_config_dir()
elif platform.system() == "Linux":
elif env == Environment.LINUX:
return get_linux_config_dir()
elif platform.system() == "Darwin":
elif env == Environment.DARWIN:
return get_mac_config_dir()
else:
return get_anki_config_dir()
Expand Down