Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 5bc2e6d

Browse files
committed
added Album class, cleaned up for pylint
1 parent 8fd6677 commit 5bc2e6d

File tree

1 file changed

+72
-65
lines changed

1 file changed

+72
-65
lines changed

wavCue2Flac.py

+72-65
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,101 @@
11
import os
22
import pipes
33
import sys
4-
import shlex
54
import glob
65
from mutagen.flac import FLAC
76
from subprocess import call
87

98
def find_command(name):
109
#print('checking for %s' % name)
11-
for dir in os.environ['PATH'].split(':'):
12-
command = os.path.join(dir, name)
13-
if os.path.exists(command): return command
10+
for path in os.environ['PATH'].split(':'):
11+
command = os.path.join(path, name)
12+
if os.path.exists(command):
13+
return command
1414

1515
def print_usage():
1616
print('usage: wavCue2Flac albumPath')
1717

18-
def find_cue_wav(path):
19-
print('checking for cue/wav in %s' % path)
20-
for root, dirs, files in os.walk(path):
21-
for file in files:
22-
cue = os.path.abspath(os.path.join(root, file))
23-
if cue.endswith('.cue'):
24-
for ext in ['.wav', '.wv']:
25-
wav = cue.replace('.cue', ext)
26-
if os.path.exists(wav):
27-
return cue, wav
28-
29-
def split_album(path, cue, wav):
30-
print('splitting album with cuebreakpoints | shnsplit')
31-
os.chdir(path)
32-
ret = call(
33-
'cuebreakpoints %s | shnsplit -q -o "flac flac -s --best -o %%f -" %s' % (pipes.quote(cue), pipes.quote(wav)),
34-
shell=True)
35-
if ret != 0:
36-
print >> sys.stderr, 'error calling cuebreakpoints/shnsplit'
37-
sys.exit(1)
38-
39-
def tag_tracks(cue, path):
40-
print('tagging tracks with cuetag')
41-
os.chdir(path)
42-
ret = call('cuetag %s split-track*.flac' % pipes.quote(cue), shell=True)
43-
if ret != 0:
44-
print >> sys.stderr, 'error calling cuetag'
45-
sys.exit(1)
46-
47-
def rename_tracks(path):
48-
print('renaming tracks')
49-
for name in glob.glob(os.path.join(path, 'split-track*.flac')):
50-
track = FLAC(name)
51-
title = track["TITLE"][0]
52-
artist = track["ARTIST"][0]
53-
num = track["TRACKNUMBER"][0]
54-
newName = '{0} - {1} - {2}.flac'.format(num.zfill(2), artist, title)
55-
newPath = os.path.join(path, newName)
56-
os.rename(name, newPath)
18+
class Album:
19+
def __init__(self, album_path):
20+
self.album_path = album_path
21+
22+
def find_cue_wav(self):
23+
print('checking for cue/wav in %s' % self.album_path)
24+
for root, dirs, files in os.walk(self.album_path):
25+
for file_entry in files:
26+
cue = os.path.abspath(os.path.join(root, file_entry))
27+
if cue.endswith('.cue'):
28+
for ext in ['.wav', '.wv']:
29+
wav = cue.replace('.cue', ext)
30+
if os.path.exists(wav):
31+
return cue, wav
32+
return None
33+
34+
35+
def split_album(self, cue, wav):
36+
print('splitting album with cuebreakpoints | shnsplit')
37+
os.chdir(self.album_path)
38+
ret = call(
39+
'cuebreakpoints %s | shnsplit -q -o "flac flac -s --best -o %%f -" %s'
40+
% (pipes.quote(cue), pipes.quote(wav)),
41+
shell=True)
42+
if ret != 0:
43+
print >> sys.stderr, 'error calling cuebreakpoints/shnsplit'
44+
sys.exit(1)
45+
46+
47+
def tag_tracks(self, cue):
48+
print('tagging tracks with cuetag')
49+
os.chdir(self.album_path)
50+
ret = call('cuetag %s split-track*.flac' % pipes.quote(cue), shell=True)
51+
if ret != 0:
52+
print >> sys.stderr, 'error calling cuetag'
53+
sys.exit(1)
54+
55+
56+
def rename_tracks(self):
57+
print('renaming tracks')
58+
for name in glob.glob(os.path.join(self.album_path, 'split-track*.flac')):
59+
track = FLAC(name)
60+
title = track["TITLE"][0]
61+
artist = track["ARTIST"][0]
62+
num = track["TRACKNUMBER"][0]
63+
new_name = '{0} - {1} - {2}.flac'.format(num.zfill(2), artist, title)
64+
new_path = os.path.join(self.album_path, new_name)
65+
os.rename(name, new_path)
66+
67+
def process(self):
68+
paths = self.find_cue_wav()
69+
if paths is None:
70+
print('folder does not contain cue/wav; nothing to do')
71+
sys.exit(0)
72+
73+
cue_path = paths[0]
74+
wav_path = paths[1]
75+
76+
self.split_album(cue_path, wav_path)
77+
self.tag_tracks(cue_path)
78+
self.rename_tracks()
5779

5880
if len(sys.argv) < 2:
5981
print_usage()
6082
sys.exit(1)
6183

62-
albumPath = sys.argv[1]
63-
64-
flac = find_command('flac')
65-
if flac is None:
84+
if find_command('flac') is None:
6685
print >> sys.stderr, 'unable to find flac'
6786
sys.exit(1)
6887

69-
cuebreakpoints = find_command('cuebreakpoints')
70-
if cuebreakpoints is None:
88+
if find_command('cuebreakpoints') is None:
7189
print >> sys.stderr, 'unable to find cuebreakpoints (cuetools)'
7290
sys.exit(1)
7391

74-
shnsplit = find_command('shnsplit')
75-
if shnsplit is None:
92+
if find_command('shnsplit') is None:
7693
print >> sys.stderr, 'unable to find shnsplit (shntool)'
7794
sys.exit(1)
7895

79-
cuetag = find_command('cuetag')
80-
if cuetag is None:
81-
print >> sys.stderr, 'unable to find cuetag (download from https://github.com/gumayunov/split-cue/blob/master/cuetag)'
96+
if find_command('cuetag') is None:
97+
CUETAG_URL = 'https://github.com/gumayunov/split-cue/blob/master/cuetag'
98+
print >> sys.stderr, 'unable to find cuetag (download from %s)' % CUETAG_URL
8299
sys.exit(1)
83100

84-
paths = find_cue_wav(albumPath)
85-
if paths is None:
86-
print('folder does not contain cue/wav; nothing to do')
87-
sys.exit(0)
88-
89-
cue = paths[0]
90-
wav = paths[1]
91-
92-
split_album(albumPath, cue, wav)
93-
tag_tracks(cue, albumPath)
94-
rename_tracks(albumPath)
101+
Album(sys.argv[1]).process()

0 commit comments

Comments
 (0)