comparison amakode.py @ 3:de86a9e19151

Misc bug fixes, whitespace changes. - Make it easier to schedule multiple jobs (which is useless until amarok issues them..) - Remove superfluous initialisation and closing of descriptors.
author darius@inchoate.localdomain
date Mon, 12 Nov 2007 14:52:41 +1030
parents 07e3d8655a29
children 65a9f99302cd
comparison
equal deleted inserted replaced
2:de1f6ba76d6d 3:de86a9e19151
48 import re 48 import re
49 49
50 class QueueMgr: 50 class QueueMgr:
51 queuedjobs = [] 51 queuedjobs = []
52 activejobs = [] 52 activejobs = []
53 maxjobs = 1 53
54 54 def __init__(self, callback = None, maxjobs = 2):
55 def __init__(self, _callback): 55 self.callback = callback
56 self.callback = _callback 56 self.maxjobs = maxjobs
57 pass 57 pass
58 58
59 def add(self, job): 59 def add(self, job):
60 log.debug("Job added") 60 log.debug("Job added")
61 self.queuedjobs.append(job) 61 self.queuedjobs.append(job)
103 encode["mp4"] = ["faac", "-o", "/dev/stdout", "-"] 103 encode["mp4"] = ["faac", "-o", "/dev/stdout", "-"]
104 encode["m4a"] = encode["mp4"] 104 encode["m4a"] = encode["mp4"]
105 encode["flac"] = ["flac", "-c", "-"] 105 encode["flac"] = ["flac", "-c", "-"]
106 106
107 def __init__(self, _inurl, _tofmt): 107 def __init__(self, _inurl, _tofmt):
108 self.outfname = None
109 self.errfname = None
110 self.errormsg = None 108 self.errormsg = None
111 log.debug("Creating job") 109 log.debug("Creating job")
112 self.inurl = _inurl 110 self.inurl = _inurl
113 self.tofmt = string.lower(_tofmt) 111 self.tofmt = string.lower(_tofmt)
114 self.inext = string.lower(string.rsplit(self.inurl, ".", 1)[1]) 112 self.inext = string.lower(string.rsplit(self.inurl, ".", 1)[1])
115 if (self.inext in self.decode): 113 if (self.inext in self.decode):
116 log.debug("can decode with " + str(self.decode[self.inext])) 114 log.debug("can decode with " + str(self.decode[self.inext]))
117 else: 115 else:
118 log.debug("unable to decode " + self.inext) 116 log.debug("unable to decode " + self.inext)
119 raise KeyError("no available decoder") 117 raise KeyError("no available decoder")
120 118
121 if (self.tofmt in self.encode): 119 if (self.tofmt in self.encode):
122 log.debug("can encode with " + str(self.encode[self.tofmt])) 120 log.debug("can encode with " + str(self.encode[self.tofmt]))
123 else: 121 else:
124 log.debug("unable to encode " + self.tofmt) 122 log.debug("unable to encode " + self.tofmt)
125 raise KeyError("no available encoder") 123 raise KeyError("no available encoder")
129 try: 127 try:
130 self.inputfile = urllib.urlopen(self.inurl) 128 self.inputfile = urllib.urlopen(self.inurl)
131 self.outfd, self.outfname = tempfile.mkstemp(prefix="transcode-", suffix="." + self.tofmt) 129 self.outfd, self.outfname = tempfile.mkstemp(prefix="transcode-", suffix="." + self.tofmt)
132 #self.outfname = string.join(string.rsplit(self.inurl, ".")[:-1] + [self.tofmt], ".") 130 #self.outfname = string.join(string.rsplit(self.inurl, ".")[:-1] + [self.tofmt], ".")
133 131
134 self.errfd, self.errfname = tempfile.mkstemp(prefix="transcode-", suffix=".log") 132 self.errfh, self.errfname = tempfile.mkstemp(prefix="transcode-", suffix=".log")
135 self.outurl = urlparse.urlunsplit(["file", None, self.outfname, None, None]) 133 self.outurl = urlparse.urlunsplit(["file", None, self.outfname, None, None])
136 log.debug("Outputting to " + self.outfname + " " + self.outurl + ")") 134 log.debug("Outputting to " + self.outfname + " " + self.outurl + ")")
137 log.debug("Errors to " + self.errfname) 135 log.debug("Errors to " + self.errfname)
138 self.decoder = subprocess.Popen(self.decode[self.inext], stdin=self.inputfile, stdout=subprocess.PIPE, stderr=self.errfd) 136 self.decoder = subprocess.Popen(self.decode[self.inext], stdin=self.inputfile, stdout=subprocess.PIPE, stderr=self.errfd)
139 self.encoder = subprocess.Popen(self.encode[self.tofmt], stdin=self.decoder.stdout, stdout=self.outfd, stderr=self.errfd) 137 self.encoder = subprocess.Popen(self.encode[self.tofmt], stdin=self.decoder.stdout, stdout=self.outfd, stderr=self.errfd)
151 149
152 rtn = self.encoder.poll() 150 rtn = self.encoder.poll()
153 if (rtn == None): 151 if (rtn == None):
154 return(False) 152 return(False)
155 153
156 os.close(self.errfd)
157 os.close(self.outfd)
158
159 if (rtn == 0): 154 if (rtn == 0):
160 os.unlink(self.errfname) 155 os.unlink(self.errfname)
161 self.errormsg = None 156 self.errormsg = None
162 else: 157 else:
163 log.debug("error in transcode, please review " + self.errfname) 158 log.debug("error in transcode, please review " + self.errfname)
179 """ Main loop waits for something to do then does it """ 174 """ Main loop waits for something to do then does it """
180 log.debug("Started.") 175 log.debug("Started.")
181 176
182 self.readSettings() 177 self.readSettings()
183 178
184 self.queue = QueueMgr(self.notify) 179 self.queue = QueueMgr(callback = self.notify, maxjobs = 1)
185 180
186 while True: 181 while True:
187 # Check for finished jobs, etc 182 # Check for finished jobs, etc
188 self.queue.poll() 183 self.queue.poll()
189 # Check if there's anything waiting on stdin 184 # Check if there's anything waiting on stdin
295 signal.signal(signal.SIGHUP, onStop) 290 signal.signal(signal.SIGHUP, onStop)
296 signal.signal(signal.SIGTERM, onStop) 291 signal.signal(signal.SIGTERM, onStop)
297 if 1: 292 if 1:
298 app = amaKode(sys.argv) 293 app = amaKode(sys.argv)
299 else: 294 else:
295 # Quick test case
300 q = QueueMgr(reportJob) 296 q = QueueMgr(reportJob)
301 j = TranscodeJob("file:///tmp/test.mp3", "ogg") 297 j = TranscodeJob("file:///tmp/test.mp3", "ogg")
302 q.add(j) 298 q.add(j)
303 j2 = TranscodeJob("file:///tmp/test2.mp3", "m4a") 299 j2 = TranscodeJob("file:///tmp/test2.mp3", "m4a")
304 q.add(j2) 300 q.add(j2)