-
Notifications
You must be signed in to change notification settings - Fork 0
/
woofer.py
207 lines (170 loc) · 8.09 KB
/
woofer.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
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Woofer - free open-source cross-platform music player
# Copyright (C) 2015 Milan Herbig <milanherbig[at]gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
"""
Main application launcher. Method application_start(string) is used to start the application.
PRODUCTION mode is set as default if started from console.
"""
import sys
import argparse
import os
import logging
import components.log
import components.translator
import tools
if sys.version_info < (3, 0):
raise Exception("Application requires Python 3+!")
try:
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
except ImportError as e:
raise Exception("%s! PyQt5 library is required!" % e.msg)
try:
import send2trash
except ImportError as e:
raise Exception("%s! Send2Trash package is required!" % e.msg)
try:
import ujson
except ImportError as e:
raise Exception("%s! json package is required!" % e.msg)
if sys.platform.startswith('linux'):
try:
import Xlib
except ImportError as e:
raise Exception("%s! Python-Xlib libraries are required on Linux platform!" % e.msg)
logger = logging.getLogger(__name__)
def foundLibVLC():
if components.libvlc.dll is not None:
logger.info("Using libvlc.dll found at: %s", components.libvlc.plugin_path)
return True
else:
return False
def findCmdHelpFile():
if os.path.isfile(os.path.join(tools.APP_ROOT_DIR, 'cmdargs.exe')): # built exe dist
return os.path.join(tools.APP_ROOT_DIR, 'cmdargs.exe')
elif os.path.isfile(os.path.join(tools.APP_ROOT_DIR, 'cmdargs.py')):
return os.path.join(tools.APP_ROOT_DIR, 'cmdargs.py')
else:
raise Exception("Script cmdargs.py/exe was not found!")
def displayLibVLCError(platform):
logger.error("LibVLC dll not found, dll instance is None! Root path: %s" % tools.APP_ROOT_DIR)
msgBox = QMessageBox()
msgBox.setTextFormat(Qt.RichText)
msgBox.setIcon(QMessageBox.Critical)
msgBox.setWindowTitle("Critical error - dependency not found!")
msgBox.setText("Woofer player was unable to find core LibVLC libraries!")
if platform == 'nt':
msgBox.setInformativeText("If you are using distributed binary package, "
"please report this issue on http://m1lhaus.github.io/woofer immediately. "
"You can try to install "
"<a href='http://www.videolan.org/vlc/#download'>VLC media player %d-bit</a> "
"as temporary workaround. Woofer player can use their libraries."
% tools.check_binary_type(sys.executable))
else:
msgBox.setInformativeText("Please install appropriate VLC media player 2.x from your repository "
"or you can download last version directly from "
"<a href='http://www.videolan.org/vlc/#download'>VideoLAN</a>.")
msgBox.exec_()
def displayLoggerError(e_msg):
msgBox = QMessageBox()
msgBox.setIcon(QMessageBox.Critical)
msgBox.setWindowTitle("Critical error - initialization failed!")
msgBox.setText("Initialization of logger component failed. "
"The application probably doesn't have write permissions.")
msgBox.setInformativeText("Details:\n" + e_msg)
msgBox.exec_()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Woofer player is free and open-source cross-platform music player.",
add_help=False)
parser.add_argument('input', nargs='?', type=str, help='Media file path.')
parser.add_argument('-d', "--debug", action='store_true', help="debug/verbose mode")
parser.add_argument('-h', "--help", action='store_true', help="show this help message and exit")
parser.add_argument('-u', type=str, help='Direct path to updater.exe to invoke update mechanism.')
args = parser.parse_args()
env = 'DEBUG' if args.debug else 'PRODUCTION'
# init Qt application
app = QApplication(sys.argv)
app.setOrganizationName("WooferPlayer")
app.setOrganizationDomain("com.woofer.player")
app.setApplicationName("Woofer")
# QSettings().clear()
# init logging module
try:
components.log.setup_logging(env)
except Exception as exception:
displayLoggerError(str(exception))
sys.exit(-1)
# log info about Woofer player
logger.info("Binary located at: " + sys.executable)
build_info = tools.loadBuildInfo()
logger.info("Build info - version: %s | revision: %s | build date: %s" % (build_info.get("version", None),
build_info.get("revision", None),
build_info.get("date", None)))
logger.info("Mode: '%s' Python '%s.%s.%s' PyQt: '%s' Qt: '%s'", env, sys.version_info[0], sys.version_info[1],
sys.version_info[2], PYQT_VERSION_STR, QT_VERSION_STR)
logger.info("WooferPlayer data will be stored in: %s", tools.OS_DATA_DIR)
# if -h/--help console argument is given, display help content
if args.help:
logger.debug("Help cmd arg given, opening cmd help file...")
cmd_args_file = findCmdHelpFile()
# if not running built win32 dist
if not tools.IS_WIN32_EXE:
parser.print_help()
# win32gui apps has no stdout/stderr, so cmd help has to be opened in new window
else:
os.startfile(cmd_args_file) # opens console application window with help
logging.shutdown() # quit application
sys.exit()
# start server and detect another instance
import components.network
applicationServer = components.network.LocalServer("com.woofer.player")
try:
if not applicationServer.another_instance_running:
# init LibVLC binaries
import components.libvlc # import for libvlc check
if foundLibVLC():
# init translator module
settings = QSettings()
lang_code = settings.value("components/translator/Translator/language", "en_US.ini")
tr = components.translator.init(lang_code)
# start gui application
import dialogs.main_dialog
logger.debug("Initializing gui application and all components...")
mainApp = dialogs.main_dialog.MainApp(env, args.input)
applicationServer.messageReceivedSignal.connect(mainApp.messageFromAnotherInstance)
# prepare for launching updater.exe given by args.u
if args.u:
mainApp.prepareForAppUpdate(args.u)
mainApp.show()
app.exec_()
logger.debug("MainThread loop stopped.")
else:
logger.error("LibVLC libraries not found")
displayLibVLCError(os.name)
else:
if args.input:
applicationServer.sendMessage(r"play %s" % args.input) # play input file immediately
else:
applicationServer.sendMessage(r"open") # raise application on top
logger.debug("Another instance has been notified, closing app now...")
# if anything goes wrong, don't forget to close the socket
finally:
if not applicationServer.exit():
logger.error("Local server components are not closed properly!")
logger.debug("Application has been closed")
logging.shutdown()