-
Notifications
You must be signed in to change notification settings - Fork 1
/
lnp.py
627 lines (555 loc) · 22.7 KB
/
lnp.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""PyLNP main library."""
from __future__ import print_function, unicode_literals
import sys
from tkgui import TkGui
import fnmatch, glob, os, re, shutil, subprocess, tempfile, webbrowser
import distutils.dir_util as dir_util
from datetime import datetime
from settings import DFConfiguration
BASEDIR = '.'
VERSION = '0.1'
class PyLNP(object):
"""
PyLNP library class.
Acts as an abstraction layer between the UI and the Dwarf Fortress
instance.
"""
def __init__(self):
"""Constructor for the PyLNP library."""
self.bundle = ''
if hasattr(sys, 'frozen'):
os.chdir(os.path.dirname(sys.executable))
if sys.platform == 'win32':
self.bundle = 'win'
elif sys.platform.startswith('linux'):
self.bundle = 'linux'
elif sys.platform == 'darwin':
self.bundle = 'osx'
#OS X bundles start in different directory
os.chdir('../../..')
else:
os.chdir(os.path.dirname(os.path.abspath(__file__)))
self.lnp_dir = os.path.join(BASEDIR, 'LNP')
if not os.path.isdir(self.lnp_dir):
print('WARNING: LNP folder is missing!', file=sys.stderr)
self.keybinds_dir = self.case_sensitive_folder('Keybinds')
self.graphics_dir = self.case_sensitive_folder('Graphics')
self.utils_dir = self.case_sensitive_folder('Utilities')
self.folders = []
self.df_dir = ''
self.settings = None
self.init_dir = ''
self.save_dir = ''
self.autorun = []
self.load_autorun()
self.find_df_folder()
TkGui(self)
def load_params(self):
"""Loads settings from the selected Dwarf Fortress instance."""
try:
self.settings.read_settings()
except IOError:
sys.excepthook(*sys.exc_info())
msg = ("Failed to read settings, "
"{0} not really a DF dir?"
).format(self.df_dir)
raise IOError(msg)
def save_params(self):
"""Saves settings to the selected Dwarf Fortress instance."""
self.settings.write_settings()
def restore_defaults(self):
"""Copy default settings into the selected Dwarf Fortress instance."""
shutil.copy(
os.path.join(self.lnp_dir, 'Defaults', 'init.txt'),
os.path.join(self.init_dir, 'init.txt')
)
shutil.copy(
os.path.join(self.lnp_dir, 'Defaults', 'd_init.txt'),
os.path.join(self.init_dir, 'd_init.txt')
)
self.load_params()
def run_df(self):
"""Launches Dwarf Fortress."""
if sys.platform == 'win32':
self.run_program(os.path.join(self.df_dir, 'Dwarf Fortress.exe'))
else:
if os.path.isfile(os.path.join(self.df_dir, 'dfhack')):
self.run_program(os.path.join(self.df_dir, 'dfhack'))
else:
self.run_program(os.path.join(self.df_dir, 'df'))
for prog in self.autorun:
if os.access(os.path.join(self.utils_dir, prog), os.F_OK):
self.run_program(os.path.join(self.utils_dir, prog))
@staticmethod
def run_program(path):
"""
Launches an external program.
:param path: The path of the program to launch.
"""
try:
path = os.path.abspath(path)
if path.endswith('.jar'): #Explicitly launch JAR files with Java
subprocess.Popen(
['java', '-jar', os.path.basename(path)],
cwd=os.path.dirname(path))
elif path.endswith('.app'): #OS X application bundle
subprocess.Popen(['open', path], cwd=path)
else:
subprocess.Popen(path, cwd=os.path.dirname(path))
return True
except OSError:
sys.excepthook(*sys.exc_info())
return False
def open_savegames(self):
"""Opens the save game folder."""
open_folder(self.save_dir)
def open_utils(self):
"""Opens the utilities folder."""
open_folder(self.utils_dir)
def open_graphics(self):
"""Opens the graphics pack folder."""
open_folder(self.graphics_dir)
@staticmethod
def open_main_folder():
"""Opens the folder containing the program."""
open_folder('.')
def open_lnp_folder(self):
"""Opens the folder containing data for the LNP."""
open_folder(self.lnp_dir)
def open_df_folder(self):
"""Opens the Dwarf Fortress folder."""
open_folder(self.df_dir)
def open_init_folder(self):
"""Opens the init folder in the selected Dwarf Fortress instance."""
open_folder(self.init_dir)
@staticmethod
def open_df_web():
"""Launches a web browser to the Dwarf Fortress webpage."""
webbrowser.open('http://www.bay12games.com/dwarves/')
@staticmethod
def open_wiki():
"""Launches a web browser to the Dwarf Fortress Wiki."""
webbrowser.open('http://dwarffortresswiki.org/')
@staticmethod
def open_forums():
"""Launches a web browser to the Dwarf Fortress forums."""
webbrowser.open('http://www.bay12forums.com/smf/')
def find_df_folder(self):
"""Locates all suitable Dwarf Fortress installations (folders starting
with "Dwarf Fortress" or "df")"""
self.folders = folders = tuple([
o for o in
glob.glob(os.path.join(BASEDIR, 'Dwarf Fortress*'))+
glob.glob(os.path.join(BASEDIR, 'df*')) if os.path.isdir(o)
])
self.df_dir = ''
if len(folders) == 1:
self.set_df_folder(folders[0])
def set_df_folder(self, path):
"""
Selects the Dwarf Fortress instance to operate on.
:param path: The path of the Dwarf Fortress instance to use.
"""
self.df_dir = os.path.abspath(path)
self.init_dir = os.path.join(self.df_dir, 'data', 'init')
self.save_dir = os.path.join(self.df_dir, 'data', 'save')
self.settings = DFConfiguration(self.df_dir)
self.install_extras()
self.load_params()
def read_keybinds(self):
"""Returns a list of keybinding files."""
return tuple([
os.path.basename(o) for o in
glob.glob(os.path.join(self.keybinds_dir, '*.txt'))
])
def read_graphics(self):
"""Returns a list of graphics directories."""
return tuple([
os.path.basename(o) for o in
glob.glob(os.path.join(self.graphics_dir, '*')) if
os.path.isdir(o)
])
@staticmethod
def read_utility_lists(path):
"""
Reads a list of filenames from a utility list (e.g. include.txt).
:param path: The file to read.
"""
result = []
try:
util_file = open(path)
for line in util_file:
for match in re.findall(r'\[(.+)\]', line):
result.append(match)
except IOError:
pass
return result
def read_utilities(self):
"""Returns a list of utility programs."""
exclusions = self.read_utility_lists(os.path.join(
self.utils_dir, 'exclude.txt'))
#Allow for an include list of filenames that will be treated as valid
#utilities. Useful for e.g. Linux, where executables rarely have
#extensions.
inclusions = self.read_utility_lists(os.path.join(
self.utils_dir, 'include.txt'))
progs = []
patterns = ['*.jar'] # Java applications
if sys.platform in ['windows', 'win32']:
patterns.append('*.exe') # Windows executables
else:
patterns.append('*.sh') # Shell scripts for Linux and OS X
for root, dirnames, filenames in os.walk(self.utils_dir):
if sys.platform == 'darwin':
for dirname in dirnames:
if fnmatch.fnmatch(dirname, '*.app'):
#OS X application bundles are really directories
progs.append(os.path.relpath(
os.path.join(root, dirname),
os.path.join(self.utils_dir)))
for filename in filenames:
if ((
any(fnmatch.fnmatch(filename, p) for p in patterns) or
filename in inclusions) and
filename not in exclusions):
progs.append(os.path.relpath(
os.path.join(root, filename),
os.path.join(self.utils_dir)))
return progs
def toggle_autorun(self, item):
"""
Toggles autorun for the specified item.
Params:
item
The item to toggle autorun for.
"""
if item in self.autorun:
self.autorun.remove(item)
else:
self.autorun.append(item)
self.save_autorun()
def load_autorun(self):
"""Loads autorun settings."""
self.autorun = []
try:
for line in open(os.path.join(self.utils_dir, 'autorun.txt')):
self.autorun.append(line)
except IOError:
pass
def save_autorun(self):
"""Saves autorun settings."""
autofile = open(os.path.join(self.utils_dir, 'autorun.txt'), 'w')
autofile.write("\n".join(self.autorun))
autofile.close()
def cycle_option(self, field):
"""
Cycles an option field between its possible values.
:param field: The field to cycle.
"""
self.settings.cycle_item(field)
self.save_params()
def set_option(self, field, value):
"""
Sets a field to a specific value.
Params:
field
The field to set.
value
The new value for the field.
"""
self.settings.set_value(field, value)
self.save_params()
def load_keybinds(self, filename):
"""
Overwrites Dwarf Fortress keybindings from a file.
Params:
filename
The keybindings file to use.
"""
if not filename.endswith('.txt'):
filename = filename + '.txt'
target = os.path.join(self.init_dir, 'interface.txt')
os.rename(target, target+'.bak')
shutil.copyfile(os.path.join(self.keybinds_dir, filename), target)
def keybind_exists(self, filename):
"""
Returns whether or not a keybindings file already exists.
Params:
filename
The filename to check.
"""
if not filename.endswith('.txt'):
filename = filename + '.txt'
return os.access(os.path.join(self.keybinds_dir, filename), os.F_OK)
def save_keybinds(self, filename):
"""
Save current keybindings to a file.
Params:
filename
The name of the new keybindings file.
"""
if not filename.endswith('.txt'):
filename = filename + '.txt'
shutil.copyfile(os.path.join(self.init_dir, 'interface.txt'), filename)
self.read_keybinds()
def delete_keybinds(self, filename):
"""
Deletes a keybindings file.
Params:
filename
The filename to delete.
"""
if not filename.endswith('.txt'):
filename = filename + '.txt'
os.remove(os.path.join(self.keybinds_dir, filename))
def install_graphics(self, pack):
"""
Installs the graphics pack located in LNP/Graphics/<pack>.
install_inits should point to the appropriate method used to handle the
inits (copy_inits or patch_inits).
Params:
pack
The name of the pack to install.
Returns:
True if successful,
False if an exception occured
None if required files are missing (raw/graphics, data/init)
"""
gfx_dir = os.path.join(self.graphics_dir, pack)
if (os.path.isdir(gfx_dir) and
os.path.isdir(os.path.join(gfx_dir, 'raw', 'graphics')) and
os.path.isdir(os.path.join(gfx_dir, 'data', 'init'))):
try:
#Delete old graphics
if os.path.isdir(os.path.join(self.df_dir, 'raw', 'graphics')):
dir_util.remove_tree(
os.path.join(self.df_dir, 'raw', 'graphics'))
#Copy new raws
dir_util.copy_tree(
os.path.join(gfx_dir, 'raw'),
os.path.join(self.df_dir, 'raw'))
if os.path.isdir(os.path.join(self.df_dir, 'data', 'art')):
dir_util.remove_tree(
os.path.join(self.df_dir, 'data', 'art'))
dir_util.copy_tree(
os.path.join(gfx_dir, 'data', 'art'),
os.path.join(self.df_dir, 'data', 'art'))
self.install_inits(gfx_dir)
shutil.copyfile(
os.path.join(gfx_dir, 'data', 'init', 'colors.txt'),
os.path.join(self.df_dir, 'data', 'init', 'colors.txt'))
except Exception:
sys.excepthook(*sys.exc_info())
return False
else:
return True
else:
return None
self.load_params()
def copy_inits(self, gfx_dir):
"""
Installs init files from a graphics pack by overwriting.
"""
shutil.copyfile(
os.path.join(gfx_dir, 'data', 'init', 'init.txt'),
os.path.join(self.df_dir, 'data', 'init', 'init.txt'))
shutil.copyfile(
os.path.join(gfx_dir, 'data', 'init', 'd_init.txt'),
os.path.join(self.df_dir, 'data', 'init', 'd_init.txt'))
def patch_inits(self, gfx_dir):
"""
Installs init files from a graphics pack by selectively changing
specific fields. All settings outside of the mentioned fields are
preserved.
TODO: Consider if there's a better option than listing all fields
explicitly...
"""
d_init_fields = [
'WOUND_COLOR_NONE', 'WOUND_COLOR_MINOR',
'WOUND_COLOR_INHIBITED', 'WOUND_COLOR_FUNCTION_LOSS',
'WOUND_COLOR_BROKEN', 'WOUND_COLOR_MISSING', 'SKY', 'CHASM',
'PILLAR_TILE',
#Tracks
'TRACK_N', 'TRACK_S', 'TRACK_E', 'TRACK_W', 'TRACK_NS',
'TRACK_NE', 'TRACK_NW', 'TRACK_SE', 'TRACK_SW', 'TRACK_EW',
'TRACK_NSE', 'TRACK_NSW', 'TRACK_NEW', 'TRACK_SEW',
'TRACK_NSEW', 'TRACK_RAMP_N', 'TRACK_RAMP_S', 'TRACK_RAMP_E',
'TRACK_RAMP_W', 'TRACK_RAMP_NS', 'TRACK_RAMP_NE',
'TRACK_RAMP_NW', 'TRACK_RAMP_SE', 'TRACK_RAMP_SW',
'TRACK_RAMP_EW', 'TRACK_RAMP_NSE', 'TRACK_RAMP_NSW',
'TRACK_RAMP_NEW', 'TRACK_RAMP_SEW', 'TRACK_RAMP_NSEW',
#Trees
'TREE_ROOT_SLOPING', 'TREE_TRUNK_SLOPING',
'TREE_ROOT_SLOPING_DEAD', 'TREE_TRUNK_SLOPING_DEAD',
'TREE_ROOTS', 'TREE_ROOTS_DEAD', 'TREE_BRANCHES',
'TREE_BRANCHES_DEAD', 'TREE_SMOOTH_BRANCHES',
'TREE_SMOOTH_BRANCHES_DEAD', 'TREE_TRUNK_PILLAR',
'TREE_TRUNK_PILLAR_DEAD', 'TREE_CAP_PILLAR',
'TREE_CAP_PILLAR_DEAD', 'TREE_TRUNK_N', 'TREE_TRUNK_S',
'TREE_TRUNK_N_DEAD', 'TREE_TRUNK_S_DEAD', 'TREE_TRUNK_EW',
'TREE_TRUNK_EW_DEAD', 'TREE_CAP_WALL_N', 'TREE_CAP_WALL_S',
'TREE_CAP_WALL_N_DEAD', 'TREE_CAP_WALL_S_DEAD', 'TREE_TRUNK_E',
'TREE_TRUNK_W', 'TREE_TRUNK_E_DEAD', 'TREE_TRUNK_W_DEAD',
'TREE_TRUNK_NS', 'TREE_TRUNK_NS_DEAD', 'TREE_CAP_WALL_E',
'TREE_CAP_WALL_W', 'TREE_CAP_WALL_E_DEAD',
'TREE_CAP_WALL_W_DEAD', 'TREE_TRUNK_NW', 'TREE_CAP_WALL_NW',
'TREE_TRUNK_NW_DEAD', 'TREE_CAP_WALL_NW_DEAD', 'TREE_TRUNK_NE',
'TREE_CAP_WALL_NE', 'TREE_TRUNK_NE_DEAD',
'TREE_CAP_WALL_NE_DEAD', 'TREE_TRUNK_SW', 'TREE_CAP_WALL_SW',
'TREE_TRUNK_SW_DEAD', 'TREE_CAP_WALL_SW_DEAD', 'TREE_TRUNK_SE',
'TREE_CAP_WALL_SE', 'TREE_TRUNK_SE_DEAD',
'TREE_CAP_WALL_SE_DEAD', 'TREE_TRUNK_NSE',
'TREE_TRUNK_NSE_DEAD', 'TREE_TRUNK_NSW', 'TREE_TRUNK_NSW_DEAD',
'TREE_TRUNK_NEW', 'TREE_TRUNK_NEW_DEAD', 'TREE_TRUNK_SEW',
'TREE_TRUNK_SEW_DEAD', 'TREE_TRUNK_NSEW',
'TREE_TRUNK_NSEW_DEAD', 'TREE_TRUNK_BRANCH_N',
'TREE_TRUNK_BRANCH_N_DEAD', 'TREE_TRUNK_BRANCH_S',
'TREE_TRUNK_BRANCH_S_DEAD', 'TREE_TRUNK_BRANCH_E',
'TREE_TRUNK_BRANCH_E_DEAD', 'TREE_TRUNK_BRANCH_W',
'TREE_TRUNK_BRANCH_W_DEAD', 'TREE_BRANCH_NS',
'TREE_BRANCH_NS_DEAD', 'TREE_BRANCH_EW', 'TREE_BRANCH_EW_DEAD',
'TREE_BRANCH_NW', 'TREE_BRANCH_NW_DEAD', 'TREE_BRANCH_NE',
'TREE_BRANCH_NE_DEAD', 'TREE_BRANCH_SW', 'TREE_BRANCH_SW_DEAD',
'TREE_BRANCH_SE', 'TREE_BRANCH_SE_DEAD', 'TREE_BRANCH_NSE',
'TREE_BRANCH_NSE_DEAD', 'TREE_BRANCH_NSW',
'TREE_BRANCH_NSW_DEAD', 'TREE_BRANCH_NEW',
'TREE_BRANCH_NEW_DEAD', 'TREE_BRANCH_SEW',
'TREE_BRANCH_SEW_DEAD', 'TREE_BRANCH_NSEW',
'TREE_BRANCH_NSEW_DEAD', 'TREE_TWIGS', 'TREE_TWIGS_DEAD',
'TREE_CAP_RAMP', 'TREE_CAP_RAMP_DEAD', 'TREE_CAP_FLOOR1',
'TREE_CAP_FLOOR2', 'TREE_CAP_FLOOR1_DEAD',
'TREE_CAP_FLOOR2_DEAD', 'TREE_CAP_FLOOR3', 'TREE_CAP_FLOOR4',
'TREE_CAP_FLOOR3_DEAD', 'TREE_CAP_FLOOR4_DEAD',
'TREE_TRUNK_INTERIOR', 'TREE_TRUNK_INTERIOR_DEAD']
init_fields = [
'FONT', 'FULLFONT', 'GRAPHICS', 'GRAPHICS_FONT',
'GRAPHICS_FULLFONT', 'TRUETYPE']
self.settings.read_file(
os.path.join(gfx_dir, 'data', 'init', 'init.txt'), init_fields,
False)
self.settings.read_file(
os.path.join(gfx_dir, 'data', 'init', 'd_init.txt'), d_init_fields,
False)
self.save_params()
install_inits = copy_inits
def update_savegames(self):
"""Update save games with current raws."""
saves = [
o for o in glob.glob(os.path.join(self.save_dir, '*'))
if os.path.isdir(o) and not o.endswith('current')]
count = 0
if saves:
for save in saves:
count = count + 1
#Delete old graphics
if os.path.isdir(os.path.join(save, 'raw', 'graphics')):
dir_util.remove_tree(os.path.join(save, 'raw', 'graphics'))
#Copy new raws
dir_util.copy_tree(
os.path.join(self.df_dir, 'raw'),
os.path.join(save, 'raw'))
return count
def simplify_graphics(self):
"""Removes unnecessary files from all graphics packs."""
for pack in self.read_graphics():
self.simplify_pack(pack)
def simplify_pack(self, pack):
"""
Removes unnecessary files from LNP/Graphics/<pack>.
Params:
pack
The pack to simplify.
Returns:
The number of files removed if successful
False if an exception occurred
None if folder is empty
"""
pack = os.path.join(self.graphics_dir, pack)
files_before = sum(len(f) for (_, _, f) in os.walk(pack))
if files_before == 0:
return None
tmp = tempfile.mkdtemp()
try:
dir_util.copy_tree(pack, tmp)
if os.path.isdir(pack):
dir_util.remove_tree(pack)
os.makedirs(pack)
os.makedirs(os.path.join(pack, 'data', 'art'))
os.makedirs(os.path.join(pack, 'raw', 'graphics'))
os.makedirs(os.path.join(pack, 'raw', 'objects'))
os.makedirs(os.path.join(pack, 'data', 'init'))
dir_util.copy_tree(
os.path.join(tmp, 'data', 'art'),
os.path.join(pack, 'data', 'art'))
dir_util.copy_tree(
os.path.join(tmp, 'raw', 'graphics'),
os.path.join(pack, 'raw', 'graphics'))
dir_util.copy_tree(
os.path.join(tmp, 'raw', 'objects'),
os.path.join(pack, 'raw', 'objects'))
shutil.copyfile(
os.path.join(tmp, 'data', 'init', 'colors.txt'),
os.path.join(pack, 'data', 'init', 'colors.txt'))
shutil.copyfile(
os.path.join(tmp, 'data', 'init', 'init.txt'),
os.path.join(pack, 'data', 'init', 'init.txt'))
shutil.copyfile(
os.path.join(tmp, 'data', 'init', 'd_init.txt'),
os.path.join(pack, 'data', 'init', 'd_init.txt'))
except IOError:
sys.excepthook(*sys.exc_info())
retval = False
else:
files_after = sum(len(f) for (_, _, f) in os.walk(pack))
retval = files_after - files_before
if os.path.isdir(tmp):
dir_util.remove_tree(tmp)
return retval
def install_extras(self):
"""
Installs extra utilities to the Dwarf Fortress folder, if this has not
yet been done.
"""
extras_dir = os.path.join(self.lnp_dir, 'Extras')
if not os.path.isdir(extras_dir):
return
install_file = os.path.join(self.df_dir, 'PyLNP{0}.txt'.format(VERSION))
if not os.access(install_file, os.F_OK):
dir_util.copy_tree(extras_dir, self.df_dir)
textfile = open(install_file, 'w')
textfile.write(
'PyLNP V{0} extras installed!\nTime: {1}'.format(
VERSION, datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
textfile.close()
def case_sensitive_folder(self, keyword):
"""
Case sensitivity for folders
"""
if os.path.isdir(os.path.join(self.lnp_dir, keyword)):
return os.path.join(self.lnp_dir, keyword)
elif os.path.isdir(os.path.join(self.lnp_dir, keyword.lower())):
return os.path.join(self.lnp_dir, keyword.lower())
else:
print('WARNING, ' + keyword + 'Folder is missing!', file=sys.stderr)
def open_folder(path):
"""
Opens a folder in the system file manager.
Params:
path
The folder path to open.
"""
#http://stackoverflow.com/q/6631299
try:
if sys.platform == 'darwin':
subprocess.check_call(['open', '--', path])
elif sys.platform.startswith('linux'):
subprocess.check_call(['xdg-open', path])
elif sys.platform in ['windows', 'win32']:
subprocess.check_call(['explorer', path])
except Exception:
pass
if __name__ == "__main__":
PyLNP()
# vim:expandtab