Skip to content

Commit

Permalink
use bundle dir when bundled
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewthe2 committed May 16, 2021
1 parent ecdfc59 commit 2e5b1bb
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 41 deletions.
9 changes: 4 additions & 5 deletions ankiconnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import eel
import yaml
from pathlib import Path
from mimetypes import guess_extension
from logger import AUDIO_LOG_PATH, IMAGE_LOG_PATH, get_base64_image_with_log
from logger import AUDIO_LOG_PATH
from config import r_config, ANKI_CONFIG
from dictionary import get_jpod_audio_base64
from tools import bundle_dir

SCRIPT_DIR = Path(__file__).parent
ANKI_MODELS_FILENAME = 'ankimodels.yaml'

NOTE_SCREENSHOT = 'screenshot'
Expand All @@ -35,7 +34,7 @@ def invoke(action, params):


def get_anki_models():
filename = str(Path(SCRIPT_DIR, 'anki', ANKI_MODELS_FILENAME))
filename = str(Path(bundle_dir, 'anki', ANKI_MODELS_FILENAME))
ankiModels = []
with open(filename, 'r') as stream:
try:
Expand All @@ -48,7 +47,7 @@ def get_anki_models():

def update_anki_models(ankiModels):
# save ankimodels
with open(str(Path(SCRIPT_DIR, 'anki', ANKI_MODELS_FILENAME)), 'w') as outfile:
with open(str(Path(bundle_dir, 'anki', ANKI_MODELS_FILENAME)), 'w') as outfile:
yaml.dump(ankiModels, outfile, sort_keys=False, default_flow_style=False)
return outfile.name

Expand Down
2 changes: 0 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from configparser import ConfigParser
import eel
import os
import platform
import json

OCR_CONFIG = 'OCRCONFIG'
TRANSLATION_CONFIG = 'TRANSLATIONCONFIG'
Expand Down
6 changes: 3 additions & 3 deletions dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import base64
from sudachipy import tokenizer
from sudachipy import dictionary
from logger import SCRIPT_DIR
from tools import bundle_dir
from pathlib import Path
from config import r_config, ANKI_CONFIG
import glob
Expand All @@ -16,7 +16,7 @@
tokenizer_obj = dictionary.Dictionary(dict_type='small').create()
mode = tokenizer.Tokenizer.SplitMode.A

DICTIONARY_PATH = Path(SCRIPT_DIR, 'resources', 'dictionaries')
DICTIONARY_PATH = Path(bundle_dir, 'resources', 'dictionaries')

def get_local_dictionaries():
files = glob.glob(str(DICTIONARY_PATH) + '/*.zip')
Expand Down Expand Up @@ -44,7 +44,7 @@ def load_dictionary_by_path(dictionary_path):
def load_all_dictionaries():
default_dictionary = r_config(ANKI_CONFIG, 'anki_dictionary')
load_dictionary(default_dictionary)
# pitch_dictionary_map = load_sdictionary(str(Path(SCRIPT_DIR, 'dictionaries', 'kanjium_pitch_accents.zip')))
# pitch_dictionary_map = load_sdictionary(str(Path(bundle_dir, 'dictionaries', 'kanjium_pitch_accents.zip')))

def load_dictionary(dictionary_name):
global dictionary_map
Expand Down
6 changes: 2 additions & 4 deletions gamescript.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import eel
import glob
from pathlib import Path
from tkinter import *
from tkinter.filedialog import askopenfile
from shutil import copyfile
from fuzzywuzzy import fuzz
from fuzzywuzzy import process
from config import w_config, LOG_CONFIG
from tools import bundle_dir

SCRIPT_DIR = Path(__file__).parent
GAME_SCRIPT_PATH = Path(SCRIPT_DIR, 'gamescripts')
GAME_SCRIPT_PATH = Path(bundle_dir, 'gamescripts')
MATCH_LIMIT= 4

def open_game_script():
Expand Down
4 changes: 2 additions & 2 deletions imageprofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from pathlib import Path
from tkinter import *
from tkinter.filedialog import asksaveasfile, askopenfile
from tools import bundle_dir

SCRIPT_DIR = Path(__file__).parent
IMAGE_PROFILE_PATH = Path(SCRIPT_DIR, 'profiles')
IMAGE_PROFILE_PATH = Path(bundle_dir, 'profiles')

def open_image_profile():
root = Tk()
Expand Down
9 changes: 4 additions & 5 deletions logger.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import time
import os
import re
import sys
import eel
import glob
import base64
Expand All @@ -14,11 +13,11 @@
from util import create_directory_if_not_exists, base64_to_image_path
from audio import play_audio_from_file
from gamescript import add_matching_script_to_logs
from tools import bundle_dir

SCRIPT_DIR = Path(__file__).parent
TEXT_LOG_PATH = Path(SCRIPT_DIR, 'logs', 'text')
IMAGE_LOG_PATH = Path(SCRIPT_DIR, 'logs', 'images')
AUDIO_LOG_PATH = Path(SCRIPT_DIR, 'logs', 'audio')
TEXT_LOG_PATH = Path(bundle_dir, 'logs', 'text')
IMAGE_LOG_PATH = Path(bundle_dir, 'logs', 'images')
AUDIO_LOG_PATH = Path(bundle_dir, 'logs', 'audio')

def get_time_string():
return time.strftime('%Y%m%d-%H%M%S')
Expand Down
12 changes: 4 additions & 8 deletions ocr.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import pytesseract
from pathlib import Path
import platform
from logger import log_text, log_media, get_time_string
from logger import log_text, log_media
from config import r_config, OCR_CONFIG
from util import create_directory_if_not_exists, base64_to_image, base64_to_image_path
from tools import path_to_tesseract, get_tessdata_dir
import requests
import eel
from util import base64_to_image, base64_to_image_path
from tools import path_to_tesseract, get_tessdata_dir, bundle_dir
from ocr_space import ocr_space_file, OCRSPACE_API_URL_USA, OCRSPACE_API_URL_EU

HORIZONTAL_TEXT_DETECTION = 6
VERTICAL_TEXT_DETECTON = 5

def get_temp_image_path():
return str(Path(SCRIPT_DIR,"logs", "images", "temp.png"))
return str(Path(bundle_dir,"logs", "images", "temp.png"))

def detect_and_log(engine, cropped_image, text_orientation, session_start_time, request_time, audio_recorder):
result = image_to_text(engine, cropped_image, text_orientation)
Expand Down Expand Up @@ -50,6 +47,5 @@ def tesseract_ocr(image, text_orientation):
result = pytesseract.image_to_string(image, config=custom_config, lang=language)
return result

SCRIPT_DIR = Path(__file__).parent
tesseract_cmd, platform_name = path_to_tesseract()
pytesseract.pytesseract.tesseract_cmd = tesseract_cmd
27 changes: 18 additions & 9 deletions tools.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import os
import sys, os
import platform
from pathlib import Path
from config import r_config, w_config, OCR_CONFIG, PATHS_CONFIG
from tkinter import *
from tkinter.filedialog import asksaveasfile, askopenfile
from tkinter.filedialog import askopenfile

bundle_dir = Path(__file__).parent

if getattr(sys, 'frozen', False):
# we are running in a bundle
frozen = 'ever so'
bundle_dir = sys._MEIPASS
else:
# we are running in a normal Python environment
bundle_dir = os.path.dirname(os.path.abspath(__file__))

SCRIPT_DIR = Path(__file__).parent
OSX_TESSERACT_VERSION = "4.1.1"
WIN_TESSERACT_DIR = Path(SCRIPT_DIR, "resources", "bin", "win", "tesseract")
OSX_TESSERACT_DIR = Path(SCRIPT_DIR, "resources", "bin", "mac", "tesseract", OSX_TESSERACT_VERSION)
WIN_TESSERACT_DIR = Path(bundle_dir, "resources", "bin", "win", "tesseract")
OSX_TESSERACT_DIR = Path(bundle_dir, "resources", "bin", "mac", "tesseract", OSX_TESSERACT_VERSION)

def path_to_ffmpeg():
platform_name = platform.system()
if platform_name == 'Windows':
return str(Path(SCRIPT_DIR, "resources", "bin", "win", "ffmpeg", "ffmpeg.exe"))
return str(Path(bundle_dir, "resources", "bin", "win", "ffmpeg", "ffmpeg.exe"))
elif platform_name == 'Darwin':
return str(Path(SCRIPT_DIR, "resources", "bin", "mac", "ffmpeg", "ffmpeg"))
return str(Path(bundle_dir, "resources", "bin", "mac", "ffmpeg", "ffmpeg"))
return ''

def path_to_ffmpeg_folder():
Expand Down Expand Up @@ -49,10 +58,10 @@ def get_tessdata_dir():

def path_to_textractor():
path = r_config('PATHS', 'textractor')
return path if path != 'default' else str(Path(SCRIPT_DIR, 'resources', 'bin', 'win', 'textractor', 'TextractorCLI.exe'))
return path if path != 'default' else str(Path(bundle_dir, 'resources', 'bin', 'win', 'textractor', 'TextractorCLI.exe'))

def path_to_wexpect():
return str(Path(SCRIPT_DIR, 'resources', 'bin', 'win', 'wexpect', 'wexpect.exe'))
return str(Path(bundle_dir, 'resources', 'bin', 'win', 'wexpect', 'wexpect.exe'))

def open_folder_textractor_path():
root = Tk()
Expand Down
5 changes: 2 additions & 3 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
import base64
import cv2
import eel
from tools import bundle_dir
try:
from winreg import HKEY_CURRENT_USER, OpenKey, QueryValueEx
except ImportError:
if platform.system() == 'Windows':
print('failed to import winreg')

SCRIPT_DIR = Path(__file__).parent

class RepeatedTimer(object):
def __init__(self, interval, function, *args, **kwargs):
self._timer = None
Expand Down Expand Up @@ -56,7 +55,7 @@ def create_directory_if_not_exists(filename):
def open_folder_by_relative_path(relative_path):
platform_name = platform.system()
if platform_name == 'Windows':
path = os.path.realpath(str(Path(SCRIPT_DIR, relative_path)))
path = os.path.realpath(str(Path(bundle_dir, relative_path)))
os.startfile(path)

def base64_to_image(base64string, path):
Expand Down

0 comments on commit 2e5b1bb

Please sign in to comment.