-
Notifications
You must be signed in to change notification settings - Fork 17
/
config.py
196 lines (175 loc) · 6.64 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# !/usr/local/bin/python3.6
# MIT licensed
# Copyright (c) 2018 White Russsian
# Github: <https://github.com/Eve-PySpy/PySpy>**********************
''' Define a few paths and constants used throughout other modules.'''
# **********************************************************************
import logging.config
import logging
import os
import platform
import sys
import uuid
import requests
import wx # required for colour codes in DARK_MODE
import optstore
# cSpell Checker - Correct Words****************************************
# // cSpell:words MEIPASS, datefmt, russsian, pyinstaller, posix, pyspy
# // cSpell:words zkill, amarr, caldari, gallente, minmatar, isfile
# **********************************************************************
Logger = logging.getLogger(__name__)
# Example call: Logger.info("Something badhappened", exc_info=True) ****
# Location of packaged resource files when running pyinstaller --onefile
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.dirname(__file__)
return os.path.join(base_path, relative_path)
# If application is frozen executable
if getattr(sys, 'frozen', False):
ABOUT_ICON = resource_path("pyspy_mid.png")
application_path = os.path.dirname(sys.executable)
if os.name == "posix":
PREF_PATH = os.path.expanduser("~/Library/Preferences")
LOG_PATH = os.path.expanduser("~/Library/Logs")
ICON_FILE = resource_path("pyspy.png")
elif os.name == "nt":
local_path = os.path.join(os.path.expandvars("%LocalAppData%"), "PySpy")
if not os.path.exists(local_path):
os.makedirs(local_path)
PREF_PATH = local_path
LOG_PATH = local_path
ICON_FILE = resource_path("pyspy.ico")
# If application is run as script
elif __file__:
ABOUT_ICON = resource_path("assets/pyspy_mid.png")
application_path = os.path.dirname(__file__)
if platform.system() == "Linux":
PREF_PATH = os.path.expanduser("~/.config/pyspy")
else:
PREF_PATH = os.path.join(application_path, "tmp")
if not os.path.exists(PREF_PATH):
os.makedirs(PREF_PATH)
LOG_PATH = PREF_PATH
if os.name == "posix":
ICON_FILE = resource_path("assets/pyspy.png")
elif os.name == "nt":
ICON_FILE = resource_path("assets/pyspy.ico")
LOG_FILE = os.path.join(LOG_PATH, "pyspy.log")
GUI_CFG_FILE = os.path.join(PREF_PATH, "pyspy.cfg")
OPTIONS_FILE = os.path.join(PREF_PATH, "pyspy.pickle")
DB_FILE = os.path.join(PREF_PATH, "pyspy.sqlite3")
# Persisten options object
OPTIONS_OBJECT = optstore.PersistentOptions(OPTIONS_FILE)
# Read current version from VERSION file
with open(resource_path('VERSION'), 'r') as ver_file:
CURRENT_VER = ver_file.read().replace('\n', '')
# Clean up old GUI_CFG_FILES and OPTIONS_OBJECT keys
if os.path.isfile(GUI_CFG_FILE) and not os.path.isfile(OPTIONS_FILE):
try:
os.remove(GUI_CFG_FILE)
except:
pass
if OPTIONS_OBJECT.Get("version", 0) != CURRENT_VER:
print("Config file erased.")
try:
os.remove(GUI_CFG_FILE)
except:
pass
for key in OPTIONS_OBJECT.ListKeys():
if key != "uuid":
OPTIONS_OBJECT.Del(key)
# Unique identifier for usage statistics reporting
if OPTIONS_OBJECT.Get("uuid", "not set") == "not set":
OPTIONS_OBJECT.Set("uuid", str(uuid.uuid4()))
# Store version information
OPTIONS_OBJECT.Set("version", CURRENT_VER)
# Various constants
MAX_NAMES = 500 # The max number of char names to be processed
ZKILL_DELAY = 1 # API rate limit is 10/second, pushing it a little...
ZKILL_CALLS = 100
GUI_TITLE = "PySpy " + CURRENT_VER
FONT_SCALE_MIN = 7 # 7 equates to 70%
FONT_SCALE_MAX = 13
MAX_SHIP_DATA_AGE = 7 # The maximum age of ship data (used in db.prepare_ship_data)
CYNO_HL_PERCENTAGE = 0.05 # The minimum cover / normal cyno probability for highlighting
CACHE_TIME = 43200 # Seconds for which zkill requests are cached
# Colour Scheme
DARK_MODE = {
"BG": wx.Colour(0, 0, 0),
"TXT": wx.Colour(247, 160, 55), # Yellow
"LNE": wx.Colour(15, 15, 15),
"LBL": wx.Colour(160, 160, 160),
"HL1": wx.Colour(237, 72, 59), # Red
"HL2": wx.Colour(62, 157, 250), # Blue
"HL3": wx.Colour(237, 47, 218) # Pink
}
NORMAL_MODE = {
"BG": wx.Colour(-1, -1, -1),
"TXT": wx.Colour(45, 45, 45),
"LNE": wx.Colour(240, 240, 240),
"LBL": wx.Colour(32, 32, 32),
"HL1": wx.Colour(187, 55, 46),
"HL2": wx.Colour(38, 104, 166),
"HL3": wx.Colour(237, 47, 218)
}
# Note, Amarr and Caldari are allied and have IDs ending on uneven integers.
# Likewise, Gallente and Minmatar, also allied, have even IDs.
# We will use this to block certain faction alliances.
FACTION_IDS = (
(("500001", "Caldari"), ) +
(("500002", "Minmatar"), ) +
(("500003", "Amarr"), ) +
(("500004", "Gallente"), )
)
IGNORED_FACTIONS = OPTIONS_OBJECT.Get("IgnoredFactions", 0)
# Logging setup
''' For each module that requires logging, make sure to import modules
logging and this config. Then get a new logger at the beginning
of the module like this: "Logger = logging.getLogger(__name__)" and
produce log messages like this: "Logger.error("text", exc_info=True)"
'''
LOG_DETAIL = 'DEBUG'
LOG_DICT = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'standard': {
'format': '%(asctime)s [%(levelname)s] (%(name)s): %(message)s',
'datefmt': '%d-%b-%Y %I:%M:%S %p'
},
},
'handlers': {
'stream_handler': {
'level': 'DEBUG',
'formatter': 'standard',
'class': 'logging.StreamHandler',
},
'file_handler': {
'level': 'DEBUG',
'filename': LOG_FILE,
'class': 'logging.FileHandler',
'formatter': 'standard'
},
'timed_rotating_file_handler': {
'level': 'DEBUG',
'filename': LOG_FILE,
'class': 'logging.handlers.TimedRotatingFileHandler',
'when': 'D',
'interval': 7, # Log file rolling over every week
'backupCount': 1,
'formatter': 'standard'
},
},
'loggers': {
'': {
'handlers': ['timed_rotating_file_handler', 'stream_handler'],
'level': 'DEBUG',
'propagate': True
},
}
}
logging.config.dictConfig(LOG_DICT)