-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachine.py
114 lines (102 loc) · 3.96 KB
/
machine.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
# TODO
# one line to give the program's name and an idea of what it does.
# Copyright (C) 2013 Petey Aldous
#
# 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 2
# of the License, or (at your option) any later version.
#
# *ADDITIONAL TERMS*
# This software or binaries made from this software may not be
# distributed with any form digital rights management or any
# advertisements. Similarly, no software that uses or is
# protected by digital rights management or that displays,
# stores, or transmits advertisements in any form may make use of
# this software.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import subprocess
import os
class Command:
def __init__(self, before, after):
self.before = before
self.after = after
def execute(self, parameter):
args = list(self.before)
args.append(parameter)
args.extend(self.after)
proc = subprocess.Popen(args, bufsize=1, stdout=subprocess.PIPE)
(output, _) = proc.communicate()
return output
class Machine:
instances = {}
def __init__(self, tag_list, tag_indices, regex, command, music_directory, directory_delimiter='/'):
self.tag_list = tag_list
self.tag_indices = tag_indices
self.regex = regex
self.command = command
self.music_directory = music_directory
self.directory_delimiter = '/'
class Tags:
def __init__(self, name, tag_list):
self.name = name
self.tag_list = tag_list
def __iter__(self):
return self.tag_list.__iter__()
def __str__(self):
return self.name
def __repr__(self):
return self.__str__()
class TagList:
def __init__(self, title, artist, num, album, composer, genre, year):
self.title = Machine.Tags('title', title)
self.artist = Machine.Tags('artist', artist)
self.num = Machine.Tags('num', num) # may be in the form "4" or "4/16"
self.album = Machine.Tags('album', album)
self.composer = Machine.Tags('composer', composer)
self.genre = Machine.Tags('genre', genre)
self.year = Machine.Tags('year', year)
mac_tag_names = Machine.TagList(['TIT2'],
['TPE1', 'TPE2', 'TPE3'],
['TRCK'],
['TALB'],
['TCOM'],
['TCON'],
['TYER'])
linux_tag_names = Machine.TagList(['Title'],
['Artist'],
['Track'],
['Album'],
[],
['Genre'],
['Year'])
mac_regex = "=== ([A-Z0-9a-z]*) .*: ([^:]*)$"
linux_regex = "([A-Z0-9a-z]*): (.*)$"
mac_tag_indices = {'tag' : 1, 'value' : 2}
linux_tag_indices = {'tag' : 1, 'value' : 2}
mac_ls = Command(["id3info"], [])
linux_ls = Command(["id3", "-lR"], [])
# TODO get username instead of hardcoded
# TODO make sure this is configurable
# /Users/petey/Music/iTunes/iTunes\ Media/Music
mac_music_directory = os.path.join("/", "Users", "petey", "Music", "iTunes", "iTunes Media", "Music")
# /home/petey/Music
linux_music_directory = os.path.join("/", "home", "petey", "Music")
Machine.instances['mac'] = Machine(mac_tag_names,
mac_tag_indices,
mac_regex,
mac_ls,
mac_music_directory)
Machine.instances['linux'] = Machine(linux_tag_names,
linux_tag_indices,
linux_regex,
linux_ls,
linux_music_directory)