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

feat: add simple config for iSearch #17

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion iSearch/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# coding=utf-8

__version__ = '1.0.2'
__version__ = '1.0.3'
60 changes: 60 additions & 0 deletions iSearch/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import os

SKIP_SAVE_DB_CONFIRM_MESSAGE = "skipSaveDbConfirmMessage"
DEFAULT_SAVE_DB_LEVEL = "defaultSaveDbLevel"

DEFAULT_CONFIG = {
SKIP_SAVE_DB_CONFIRM_MESSAGE : False,
DEFAULT_SAVE_DB_LEVEL: "3"
}

CONFIG_FILE_DIRECTORIES = [
os.curdir,
os.path.join(os.path.expanduser('~'), '.iSearch'), # default path
os.path.join(os.path.expanduser('~'), '.config', 'iSearch'),
"/etc/iSearch",
os.getenv("ISEARCH_CONF", "NotFound") # add fault tolerance
]

'''
simple parser to parse key-value format file to avoid introduce new library
eg.
skipSaveDbConfirmMessage=True
defaultSaveDbLevel=3
'''
def parseConfigFile(configFile):
config = {}
with open(configFile, 'r') as reader:
rawConfig = reader.read()
lines = rawConfig.split("\n")
for line in lines:
if line.startswith("#") or len(line) == 0:
continue
key, value = line.split("=")
key, value = key.strip(), value.strip()
if value.lower() == "true":
value = True
elif value.lower() == "false":
value = False
config[key] = value

if SKIP_SAVE_DB_CONFIRM_MESSAGE not in config:
config[SKIP_SAVE_DB_CONFIRM_MESSAGE] = DEFAULT_CONFIG[SKIP_SAVE_DB_CONFIRM_MESSAGE]
if DEFAULT_SAVE_DB_LEVEL not in config:
config[DEFAULT_SAVE_DB_LEVEL] = DEFAULT_CONFIG[DEFAULT_SAVE_DB_LEVEL]
return config

def getConfig():
# https://stackoverflow.com/questions/7567642/where-to-put-a-configuration-file-in-python
for loc in CONFIG_FILE_DIRECTORIES:
try:
return parseConfigFile(os.path.join(loc, "iSearch.txt")) # return the first found config file
except IOError:
pass
except FileNotFoundError:
pass

return DEFAULT_CONFIG

# test code
# print(getConfig())
30 changes: 19 additions & 11 deletions iSearch/isearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import argparse
import os
import sqlite3
from display import Displayer, Display_mode
from termcolor import colored
from parser import Parser
from review import Reviewer
from word_sql import Word_sql
from iSearch.display import Displayer, Display_mode
from iSearch.parser import Parser
from iSearch.review import Reviewer
from iSearch.word_sql import Word_sql
from iSearch.config import getConfig, SKIP_SAVE_DB_CONFIRM_MESSAGE, DEFAULT_SAVE_DB_LEVEL

# Python2 compatibility
if sys.version_info[0] == 2:
Expand Down Expand Up @@ -54,18 +55,24 @@ def search_database(word, word_sql, displayer):
return True


def search_word(word, word_parser, word_sql, displayer):
def search_word(word, word_parser, word_sql, displayer, config):
flag = search_database(word, word_sql, displayer)
if False == flag:
print(colored(word + '提示: 不在本地,从有道词典查询', 'green', 'on_grey'))
word_dict = search_online(word, word_parser)
displayer.show(word_dict)

input_msg = '请输入,放弃保存0,优先级(1~5)(默认为3),6自定义\n>>> '
if sys.version_info[0] == 2:
add_in_db_pr = raw_input(input_msg)
else:
add_in_db_pr = input(input_msg)
if config[DEFAULT_SAVE_DB_LEVEL] == 0:
return

if config[SKIP_SAVE_DB_CONFIRM_MESSAGE] == True:
add_in_db_pr = config[DEFAULT_SAVE_DB_LEVEL]
else:
input_msg = '请输入,放弃保存0,优先级(1~5)(默认为3),6自定义\n>>> '
if sys.version_info[0] == 2:
add_in_db_pr = raw_input(input_msg)
else:
add_in_db_pr = input(input_msg)

if add_in_db_pr and add_in_db_pr.isdigit():
if int(add_in_db_pr) >= 1 and int(add_in_db_pr) <= 5:
Expand Down Expand Up @@ -298,7 +305,8 @@ def main():

elif args.word:
word = ' '.join(args.word)
search_word(word, word_parser, word_sql, displayer)
config = getConfig()
search_word(word, word_parser, word_sql, displayer, config)


if __name__ == '__main__':
Expand Down