forked from puddletag/puddletag
/
puddletag
executable file
·359 lines (282 loc) · 11.3 KB
/
puddletag
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import os
import shutil
import sys
from glob import glob
from optparse import OptionParser
def convert_ini_files():
from puddlestuff.constants import CONFIGDIR, SAVEDIR
config_files = [
'puddletag.conf', 'combos',
'gensettings', 'menus', 'shortcuts',
'tagpanel', 'tagsources.conf',
'function_settings', 'user_shortcuts', 'view_all_fields',
]
data_files = glob(os.path.join(SAVEDIR, 'actions/*')) + \
glob(os.path.join(SAVEDIR, 'masstagging/*'))
files = [os.path.join(CONFIGDIR, fn) for fn in config_files] + data_files
for fn in files:
if os.path.exists(fn):
settings_to_json(fn)
def settings_to_json(filename):
data = open(filename, 'r').read()
try:
json.loads(data)
return # Valid json so already converted.
except:
pass
try:
config_data = json.dumps(dict(ConfigObj(filename)), indent=2)
fo = open(filename, 'w')
fo.write(config_data)
fo.close()
except:
pass
def _migrate_action_shortcut_filenames():
from puddlestuff.constants import ACTIONDIR
filename = os.path.join(ACTIONDIR, 'action_shortcuts')
if not os.path.exists(filename):
return
with open(filename, 'r') as fo:
data = json.loads(fo.read());
old_dir = os.path.join(os.getenv('HOME'), '.puddletag', 'actions')
for key, shortcut in data.items():
shortcut['filenames'] = [z.replace(old_dir, ACTIONDIR) for z in shortcut['filenames']]
with open(filename, 'w') as fo:
fo.write(json.dumps(data, indent=2))
def migrate_settings():
from puddlestuff.constants import CONFIGDIR, SAVEDIR
for dirpath in [CONFIGDIR, SAVEDIR]:
if not os.path.exists(dirpath):
os.makedirs(dirpath)
old_dir = os.path.join(os.getenv('HOME'), '.puddletag')
if (os.path.exists(old_dir)):
dirnames = [
'plugins', 'actions', 'masstagging', 'mp3tag_sources', 'translations']
for dirname in dirnames:
src = os.path.join(old_dir, dirname)
dest = os.path.join(SAVEDIR, dirname)
if os.path.exists(src) and not os.path.exists(dest):
os.rename(src, os.path.join(SAVEDIR, dirname))
for filename in os.listdir(old_dir):
dest = os.path.join(CONFIGDIR, filename)
if (not os.path.exists(dest)):
src = os.path.join(old_dir, filename)
shutil.move(src, dest)
shutil.rmtree(old_dir)
_migrate_action_shortcuts()
convert_ini_files()
_migrate_action_shortcut_filenames()
def _migrate_action_shortcuts():
from puddlestuff.constants import SAVEDIR, ACTIONDIR
from puddlestuff.puddleobjects import PuddleConfig
filename = os.path.join(SAVEDIR, 'actions', 'action_shortcuts')
if not os.path.exists(filename):
return
try:
cparser = PuddleConfig(filename)
except:
# Already converted to json. Nothing to do.
return None
for section in sorted(cparser.sections()):
if section.startswith('Shortcut'):
filenames = []
for filename in cparser.get(section, 'filenames', []):
filenames.append(os.path.join(ACTIONDIR, os.path.basename(filename)))
cparser.set(section, 'filenames', filenames)
def check_libs():
"""Check for required libs.
If some not found, show/print a message.
Exit if requirements aren't met."""
_libs = [('mutagen', '(usually python3-mutagen or just mutagen)',
'http://code.google.com/p/mutagen/'),
('configobj', '(usually python3-configobj)',
'http://code.google.com/p/configobj/'),
('pyparsing', '(python3-pyparsing or python3-parsing in most distros)',
'https://github.com/pyparsing/pyparsing/'),
('PyQt5', '(python3-pyqt5 usually)',
'http://www.riverbankcomputing.co.uk/software/pyqt/intro')]
found = True
errors = []
msg = "Error: The %s module wasn't found. Please check that it's installed " \
"properly. Download it from your distro's repository" \
" %s or directly from %s \n"
for lib in _libs:
try:
__import__(lib[0])
except ImportError:
print((msg % lib))
errors.append(msg % lib)
found = False
msg = "Error: %s module wasn't found. %s. Please check that it's " \
"installed properly. You can download it from your distro's " \
"repository (%s), or download directly from %s.\n"
try:
if not found:
from PyQt5.QtWidgets import QApplication, QMessageBox
app = QApplication([])
QMessageBox.critical(None, 'puddletag', '\n'.join(errors))
except:
pass
if not found:
sys.exit(1)
def init(options, qapp):
"""Initializes things that need to be initialized.
Parses command line options. Loads Translations. Sets fontsize."""
from puddlestuff.logger import init_logger
if options.version:
# It's already printed.
exit()
if options.debug:
init_logger(logging.DEBUG)
else:
init_logger(logging.WARN)
load_language(qapp, options.langfile)
if options.fontsize:
try:
size = int(options.fontsize)
font = qapp.font()
font.setPointSize(size)
app.setFont(font)
except IndexError:
logging.error('No fontsize specified.')
except ValueError:
logging.error('Invalid fontsize specified.')
def load_language(qapp, langfile=None):
"""Loads the language file to be used by puddletag.
app => QApplication object.
langfile => If None, then the last option the user chose will be used.
Should be a path to a Qt translation file (.qm)."""
TRANSDIR = puddlestuff.constants.TRANSDIR
global translators # Needs to live throughout app.
translators = []
if langfile:
if not langfile.endswith('.qm'):
print(translate('Defaults', 'Invalid translation file.'))
langargs = None
else:
langargs = os.path.basename(langfile), os.path.dirname(langfile)
else:
langargs = None
if not langargs:
cparser = PuddleConfig()
lang = cparser.get('main', 'lang', 'auto')
langs = get_languages([TRANSDIR])
if lang != 'auto':
if lang in langs:
f = langs[lang]
langargs = os.path.basename(f), os.path.dirname(f)
if not langargs and lang != 'default':
locale = str(QLocale.system().name())
if locale in langs:
f = langs[locale]
langargs = os.path.basename(f), os.path.dirname(f)
if langargs and langargs != 'default':
translator = QTranslator()
translator.load("qt_" + QLocale.system().name(),
QLibraryInfo.location(QLibraryInfo.TranslationsPath))
translators.append(translator)
if 'puddletag_' in langargs[0]:
translator = QTranslator()
locale = langargs[0][len('puddletag_'):-len('.qm')]
translator.load("qt_" + locale,
QLibraryInfo.location(QLibraryInfo.TranslationsPath))
print("Locale: " + locale)
translators.append(translator)
translator = QTranslator()
if translator.load(*langargs):
translators.append(translator)
elif langargs != 'default':
translator = QTranslator()
translator.load("qt_" + QLocale.system().name(),
QLibraryInfo.location(QLibraryInfo.TranslationsPath))
print("Locale: " + str(QLocale.system().name()))
translators.append(translator)
else:
print("Loading default (ie. no) translation.")
for t in reversed(translators):
qapp.installTranslator(t)
puddlestuff.constants.trans_strings()
puddlestuff.puddleobjects.trans_imagetypes()
def parse_cmd_options():
usage = "Usage: %prog [options] [directory path]"
parser = OptionParser(usage=usage)
parser.add_option("--langfile", dest="langfile",
default='',
help="Path to a translation (.qm) file.", metavar="LANGFILE")
parser.add_option("--fontsize", type='float',
dest="fontsize", help="Default fontsize to use (in points).")
parser.add_option("-d", "--debug", action="store_true",
dest="debug", default=False, help="Show (useless) debug messages.")
parser.add_option("-v", "--version", action="store_true",
dest="version", default=False, help="Show version info and exit.")
return parser.parse_args()
def print_info():
"""Print program info."""
from puddlestuff import version_string, changeset
if changeset:
print(translate('Defaults', 'puddletag Version: %1, Changeset: %2').arg(
version_string).arg(changeset))
else:
print(translate('Defaults', 'puddletag Version: %s' % version_string))
class StdOut(object):
"""Class to redirect stdout/in/err."""
def __init__(self, old_write, new_write):
"""old_write should be the original sys.stdout.out
new_write is a function is what sys.stdout.out will be replaced with.
Text will be passed to both functions."""
self._old_write = old_write
self._new_write = new_write
def write(self, text):
self._old_write(text)
try:
self._new_write(text)
except:
pass
if __name__ == '__main__':
check_libs()
from PyQt5.QtGui import QPixmap, QIcon
from PyQt5.QtWidgets import QApplication, QSplashScreen
from PyQt5.QtCore import (pyqtRemoveInputHook, QTranslator,
QLibraryInfo, QLocale)
import logging
from configobj import ConfigObj
pyqtRemoveInputHook()
# Load puddletag modules.
import puddlestuff.resource # Needs to be first as other modules use it.
import puddlestuff.constants
migrate_settings()
from puddlestuff.puddleobjects import get_languages, PuddleConfig
from puddlestuff.translations import translate
# Init.
print_info()
app = QApplication(sys.argv)
options, filenames = parse_cmd_options()
init(options, app)
# Depends on init being called first.
from puddlestuff.puddletag import MainWin
from puddlestuff.puddlesettings import load_gen_settings
app.setWindowIcon(QIcon(":/appicon.png"))
pixmap = QPixmap(':/puddlelogo.png')
app.setOrganizationName("Puddle Inc.")
app.setApplicationName("puddletag")
splash = QSplashScreen(pixmap)
splash.show()
app.processEvents()
win = MainWin()
splash.close()
win.setVisible(True)
app.processEvents()
# Check if dirnames passed on command line.
if filenames:
for filename in filenames:
if not os.path.exists(filename):
print('Directory or file %s does not exist.' % filename)
sys.exit(1)
win.openFiles(filenames, False)
elif load_gen_settings([('&Load last folder at startup', False)])[0][1]:
if win._lastdir and os.path.exists(win._lastdir[0]):
win.openDir(win._lastdir[0], False)
app.exec_()