Whitespace normalization.

This commit is contained in:
Tim Peters 2001-01-15 01:36:40 +00:00
parent 0c9886d589
commit 495ad3c8cc
14 changed files with 695 additions and 695 deletions

View File

@ -41,10 +41,10 @@ class scheduler:
def enterabs(self, time, priority, action, argument): def enterabs(self, time, priority, action, argument):
"""Enter a new event in the queue at an absolute time. """Enter a new event in the queue at an absolute time.
Returns an ID for the event which can be used to remove it, Returns an ID for the event which can be used to remove it,
if necessary. if necessary.
""" """
event = time, priority, action, argument event = time, priority, action, argument
bisect.insort(self.queue, event) bisect.insort(self.queue, event)
return event # The ID return event # The ID
@ -52,19 +52,19 @@ class scheduler:
def enter(self, delay, priority, action, argument): def enter(self, delay, priority, action, argument):
"""A variant that specifies the time as a relative time. """A variant that specifies the time as a relative time.
This is actually the more commonly used interface. This is actually the more commonly used interface.
""" """
time = self.timefunc() + delay time = self.timefunc() + delay
return self.enterabs(time, priority, action, argument) return self.enterabs(time, priority, action, argument)
def cancel(self, event): def cancel(self, event):
"""Remove an event from the queue. """Remove an event from the queue.
This must be presented the ID as returned by enter(). This must be presented the ID as returned by enter().
If the event is not in the queue, this raises RuntimeError. If the event is not in the queue, this raises RuntimeError.
""" """
self.queue.remove(event) self.queue.remove(event)
def empty(self): def empty(self):
@ -73,25 +73,25 @@ class scheduler:
def run(self): def run(self):
"""Execute events until the queue is empty. """Execute events until the queue is empty.
When there is a positive delay until the first event, the
delay function is called and the event is left in the queue;
otherwise, the event is removed from the queue and executed
(its action function is called, passing it the argument). If
the delay function returns prematurely, it is simply
restarted.
It is legal for both the delay function and the action When there is a positive delay until the first event, the
function to to modify the queue or to raise an exception; delay function is called and the event is left in the queue;
exceptions are not caught but the scheduler's state remains otherwise, the event is removed from the queue and executed
well-defined so run() may be called again. (its action function is called, passing it the argument). If
the delay function returns prematurely, it is simply
restarted.
A questionably hack is added to allow other threads to run: It is legal for both the delay function and the action
just after an event is executed, a delay of 0 is executed, to function to to modify the queue or to raise an exception;
avoid monopolizing the CPU when other threads are also exceptions are not caught but the scheduler's state remains
runnable. well-defined so run() may be called again.
""" A questionably hack is added to allow other threads to run:
just after an event is executed, a delay of 0 is executed, to
avoid monopolizing the CPU when other threads are also
runnable.
"""
q = self.queue q = self.queue
while q: while q:
time, priority, action, argument = q[0] time, priority, action, argument = q[0]

View File

@ -137,7 +137,7 @@ class SGMLParser:
k = self.parse_pi(i) k = self.parse_pi(i)
if k < 0: break if k < 0: break
i = i+k i = i+k
continue continue
match = special.match(rawdata, i) match = special.match(rawdata, i)
if match: if match:
if self.literal: if self.literal:
@ -211,7 +211,7 @@ class SGMLParser:
__starttag_text = None __starttag_text = None
def get_starttag_text(self): def get_starttag_text(self):
return self.__starttag_text return self.__starttag_text
# Internal -- handle starttag, return length or -1 if not terminated # Internal -- handle starttag, return length or -1 if not terminated
def parse_starttag(self, i): def parse_starttag(self, i):
self.__starttag_text = None self.__starttag_text = None

View File

@ -31,127 +31,127 @@ or may not be necessary to flush changes to disk.
# Try using cPickle and cStringIO if available. # Try using cPickle and cStringIO if available.
try: try:
from cPickle import Pickler, Unpickler from cPickle import Pickler, Unpickler
except ImportError: except ImportError:
from pickle import Pickler, Unpickler from pickle import Pickler, Unpickler
try: try:
from cStringIO import StringIO from cStringIO import StringIO
except ImportError: except ImportError:
from StringIO import StringIO from StringIO import StringIO
class Shelf: class Shelf:
"""Base class for shelf implementations. """Base class for shelf implementations.
This is initialized with a dictionary-like object. This is initialized with a dictionary-like object.
See the module's __doc__ string for an overview of the interface. See the module's __doc__ string for an overview of the interface.
""" """
def __init__(self, dict): def __init__(self, dict):
self.dict = dict self.dict = dict
def keys(self):
return self.dict.keys()
def __len__(self):
return len(self.dict)
def has_key(self, key):
return self.dict.has_key(key)
def get(self, key, default=None): def keys(self):
if self.dict.has_key(key): return self.dict.keys()
return self[key]
return default
def __getitem__(self, key):
f = StringIO(self.dict[key])
return Unpickler(f).load()
def __setitem__(self, key, value):
f = StringIO()
p = Pickler(f)
p.dump(value)
self.dict[key] = f.getvalue()
def __delitem__(self, key):
del self.dict[key]
def close(self):
try:
self.dict.close()
except:
pass
self.dict = 0
def __del__(self): def __len__(self):
self.close() return len(self.dict)
def has_key(self, key):
return self.dict.has_key(key)
def get(self, key, default=None):
if self.dict.has_key(key):
return self[key]
return default
def __getitem__(self, key):
f = StringIO(self.dict[key])
return Unpickler(f).load()
def __setitem__(self, key, value):
f = StringIO()
p = Pickler(f)
p.dump(value)
self.dict[key] = f.getvalue()
def __delitem__(self, key):
del self.dict[key]
def close(self):
try:
self.dict.close()
except:
pass
self.dict = 0
def __del__(self):
self.close()
def sync(self):
if hasattr(self.dict, 'sync'):
self.dict.sync()
def sync(self):
if hasattr(self.dict, 'sync'):
self.dict.sync()
class BsdDbShelf(Shelf): class BsdDbShelf(Shelf):
"""Shelf implementation using the "BSD" db interface. """Shelf implementation using the "BSD" db interface.
This adds methods first(), next(), previous(), last() and This adds methods first(), next(), previous(), last() and
set_location() that have no counterpart in [g]dbm databases. set_location() that have no counterpart in [g]dbm databases.
The actual database must be opened using one of the "bsddb" The actual database must be opened using one of the "bsddb"
modules "open" routines (i.e. bsddb.hashopen, bsddb.btopen or modules "open" routines (i.e. bsddb.hashopen, bsddb.btopen or
bsddb.rnopen) and passed to the constructor. bsddb.rnopen) and passed to the constructor.
See the module's __doc__ string for an overview of the interface. See the module's __doc__ string for an overview of the interface.
""" """
def __init__(self, dict): def __init__(self, dict):
Shelf.__init__(self, dict) Shelf.__init__(self, dict)
def set_location(self, key): def set_location(self, key):
(key, value) = self.dict.set_location(key) (key, value) = self.dict.set_location(key)
f = StringIO(value) f = StringIO(value)
return (key, Unpickler(f).load()) return (key, Unpickler(f).load())
def next(self): def next(self):
(key, value) = self.dict.next() (key, value) = self.dict.next()
f = StringIO(value) f = StringIO(value)
return (key, Unpickler(f).load()) return (key, Unpickler(f).load())
def previous(self): def previous(self):
(key, value) = self.dict.previous() (key, value) = self.dict.previous()
f = StringIO(value) f = StringIO(value)
return (key, Unpickler(f).load()) return (key, Unpickler(f).load())
def first(self): def first(self):
(key, value) = self.dict.first() (key, value) = self.dict.first()
f = StringIO(value) f = StringIO(value)
return (key, Unpickler(f).load()) return (key, Unpickler(f).load())
def last(self): def last(self):
(key, value) = self.dict.last() (key, value) = self.dict.last()
f = StringIO(value) f = StringIO(value)
return (key, Unpickler(f).load()) return (key, Unpickler(f).load())
class DbfilenameShelf(Shelf): class DbfilenameShelf(Shelf):
"""Shelf implementation using the "anydbm" generic dbm interface. """Shelf implementation using the "anydbm" generic dbm interface.
This is initialized with the filename for the dbm database. This is initialized with the filename for the dbm database.
See the module's __doc__ string for an overview of the interface. See the module's __doc__ string for an overview of the interface.
""" """
def __init__(self, filename, flag='c'): def __init__(self, filename, flag='c'):
import anydbm import anydbm
Shelf.__init__(self, anydbm.open(filename, flag)) Shelf.__init__(self, anydbm.open(filename, flag))
def open(filename, flag='c'): def open(filename, flag='c'):
"""Open a persistent dictionary for reading and writing. """Open a persistent dictionary for reading and writing.
Argument is the filename for the dbm database. Argument is the filename for the dbm database.
See the module's __doc__ string for an overview of the interface. See the module's __doc__ string for an overview of the interface.
""" """
return DbfilenameShelf(filename, flag) return DbfilenameShelf(filename, flag)

View File

@ -1,6 +1,6 @@
"""A lexical analyzer class for simple shell-like syntaxes.""" """A lexical analyzer class for simple shell-like syntaxes."""
# Module and documentation by Eric S. Raymond, 21 Dec 1998 # Module and documentation by Eric S. Raymond, 21 Dec 1998
# Input stacking and error message cleanup added by ESR, March 2000 # Input stacking and error message cleanup added by ESR, March 2000
import os.path import os.path
@ -8,7 +8,7 @@ import sys
class shlex: class shlex:
"A lexical analyzer class for simple shell-like syntaxes." "A lexical analyzer class for simple shell-like syntaxes."
def __init__(self, instream=None, infile=None): def __init__(self, instream=None, infile=None):
if instream: if instream:
self.instream = instream self.instream = instream
@ -88,7 +88,7 @@ class shlex:
self.lineno = self.lineno + 1 self.lineno = self.lineno + 1
if self.debug >= 3: if self.debug >= 3:
print "shlex: in state", repr(self.state), \ print "shlex: in state", repr(self.state), \
"I see character:", repr(nextchar) "I see character:", repr(nextchar)
if self.state is None: if self.state is None:
self.token = '' # past end of file self.token = '' # past end of file
break break
@ -181,7 +181,7 @@ class shlex:
return "\"%s\", line %d: " % (infile, lineno) return "\"%s\", line %d: " % (infile, lineno)
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) == 1: if len(sys.argv) == 1:
lexer = shlex() lexer = shlex()
else: else:

View File

@ -17,7 +17,7 @@ def copyfileobj(fsrc, fdst, length=16*1024):
break break
fdst.write(buf) fdst.write(buf)
def copyfile(src, dst): def copyfile(src, dst):
"""Copy data from src to dst""" """Copy data from src to dst"""
fsrc = None fsrc = None
@ -48,7 +48,7 @@ def copystat(src, dst):
def copy(src, dst): def copy(src, dst):
"""Copy data and mode bits ("cp src dst"). """Copy data and mode bits ("cp src dst").
The destination may be a directory. The destination may be a directory.
""" """

View File

@ -36,7 +36,7 @@ Example:
# Eric S. Raymond <esr@thyrsus.com> # Eric S. Raymond <esr@thyrsus.com>
# Better RFC 821 compliance (MAIL and RCPT, and CRLF in data) # Better RFC 821 compliance (MAIL and RCPT, and CRLF in data)
# by Carey Evans <c.evans@clear.net.nz>, for picky mail servers. # by Carey Evans <c.evans@clear.net.nz>, for picky mail servers.
# #
# This was modified from the Python 1.5 library HTTP lib. # This was modified from the Python 1.5 library HTTP lib.
import socket import socket
@ -48,7 +48,7 @@ import types
SMTP_PORT = 25 SMTP_PORT = 25
CRLF="\r\n" CRLF="\r\n"
# Exception classes used by this module. # Exception classes used by this module.
class SMTPException(Exception): class SMTPException(Exception):
"""Base class for all exceptions raised by this module.""" """Base class for all exceptions raised by this module."""
@ -89,8 +89,8 @@ class SMTPSenderRefused(SMTPResponseException):
class SMTPRecipientsRefused(SMTPException): class SMTPRecipientsRefused(SMTPException):
"""All recipient addresses refused. """All recipient addresses refused.
The errors for each recipient are accessible through the attribute The errors for each recipient are accessible through the attribute
'recipients', which is a dictionary of exactly the same sort as 'recipients', which is a dictionary of exactly the same sort as
SMTP.sendmail() returns. SMTP.sendmail() returns.
""" """
def __init__(self, recipients): def __init__(self, recipients):
@ -137,27 +137,27 @@ def quotedata(data):
class SMTP: class SMTP:
"""This class manages a connection to an SMTP or ESMTP server. """This class manages a connection to an SMTP or ESMTP server.
SMTP Objects: SMTP Objects:
SMTP objects have the following attributes: SMTP objects have the following attributes:
helo_resp helo_resp
This is the message given by the server in response to the This is the message given by the server in response to the
most recent HELO command. most recent HELO command.
ehlo_resp ehlo_resp
This is the message given by the server in response to the This is the message given by the server in response to the
most recent EHLO command. This is usually multiline. most recent EHLO command. This is usually multiline.
does_esmtp does_esmtp
This is a True value _after you do an EHLO command_, if the This is a True value _after you do an EHLO command_, if the
server supports ESMTP. server supports ESMTP.
esmtp_features esmtp_features
This is a dictionary, which, if the server supports ESMTP, This is a dictionary, which, if the server supports ESMTP,
will _after you do an EHLO command_, contain the names of the will _after you do an EHLO command_, contain the names of the
SMTP service extensions this server supports, and their SMTP service extensions this server supports, and their
parameters (if any). parameters (if any).
Note, all extension names are mapped to lower case in the Note, all extension names are mapped to lower case in the
dictionary. dictionary.
See each method's docstrings for details. In general, there is a See each method's docstrings for details. In general, there is a
method of the same name to perform each SMTP command. There is also a method of the same name to perform each SMTP command. There is also a
@ -183,7 +183,7 @@ class SMTP:
(code, msg) = self.connect(host, port) (code, msg) = self.connect(host, port)
if code != 220: if code != 220:
raise SMTPConnectError(code, msg) raise SMTPConnectError(code, msg)
def set_debuglevel(self, debuglevel): def set_debuglevel(self, debuglevel):
"""Set the debug output level. """Set the debug output level.
@ -222,7 +222,7 @@ class SMTP:
(code,msg)=self.getreply() (code,msg)=self.getreply()
if self.debuglevel >0 : print "connect:", msg if self.debuglevel >0 : print "connect:", msg
return (code,msg) return (code,msg)
def send(self, str): def send(self, str):
"""Send `str' to the server.""" """Send `str' to the server."""
if self.debuglevel > 0: print 'send:', `str` if self.debuglevel > 0: print 'send:', `str`
@ -235,7 +235,7 @@ class SMTP:
raise SMTPServerDisconnected('Server not connected') raise SMTPServerDisconnected('Server not connected')
else: else:
raise SMTPServerDisconnected('please run connect() first') raise SMTPServerDisconnected('please run connect() first')
def putcmd(self, cmd, args=""): def putcmd(self, cmd, args=""):
"""Send a command to the server.""" """Send a command to the server."""
if args == "": if args == "":
@ -243,10 +243,10 @@ class SMTP:
else: else:
str = '%s %s%s' % (cmd, args, CRLF) str = '%s %s%s' % (cmd, args, CRLF)
self.send(str) self.send(str)
def getreply(self): def getreply(self):
"""Get a reply from the server. """Get a reply from the server.
Returns a tuple consisting of: Returns a tuple consisting of:
- server response code (e.g. '250', or such, if all goes well) - server response code (e.g. '250', or such, if all goes well)
@ -280,10 +280,10 @@ class SMTP:
break break
errmsg = string.join(resp,"\n") errmsg = string.join(resp,"\n")
if self.debuglevel > 0: if self.debuglevel > 0:
print 'reply: retcode (%s); Msg: %s' % (errcode,errmsg) print 'reply: retcode (%s); Msg: %s' % (errcode,errmsg)
return errcode, errmsg return errcode, errmsg
def docmd(self, cmd, args=""): def docmd(self, cmd, args=""):
"""Send a command, and return its response code.""" """Send a command, and return its response code."""
self.putcmd(cmd,args) self.putcmd(cmd,args)
@ -313,8 +313,8 @@ class SMTP:
else: else:
self.putcmd("ehlo", socket.getfqdn()) self.putcmd("ehlo", socket.getfqdn())
(code,msg)=self.getreply() (code,msg)=self.getreply()
# According to RFC1869 some (badly written) # According to RFC1869 some (badly written)
# MTA's will disconnect on an ehlo. Toss an exception if # MTA's will disconnect on an ehlo. Toss an exception if
# that happens -ddm # that happens -ddm
if code == -1 and len(msg) == 0: if code == -1 and len(msg) == 0:
raise SMTPServerDisconnected("Server not connected") raise SMTPServerDisconnected("Server not connected")
@ -368,7 +368,7 @@ class SMTP:
return self.getreply() return self.getreply()
def data(self,msg): def data(self,msg):
"""SMTP 'DATA' command -- sends message data to server. """SMTP 'DATA' command -- sends message data to server.
Automatically quotes lines beginning with a period per rfc821. Automatically quotes lines beginning with a period per rfc821.
Raises SMTPDataError if there is an unexpected reply to the Raises SMTPDataError if there is an unexpected reply to the
@ -404,14 +404,14 @@ class SMTP:
# some useful methods # some useful methods
def sendmail(self, from_addr, to_addrs, msg, mail_options=[], def sendmail(self, from_addr, to_addrs, msg, mail_options=[],
rcpt_options=[]): rcpt_options=[]):
"""This command performs an entire mail transaction. """This command performs an entire mail transaction.
The arguments are: The arguments are:
- from_addr : The address sending this mail. - from_addr : The address sending this mail.
- to_addrs : A list of addresses to send this mail to. A bare - to_addrs : A list of addresses to send this mail to. A bare
string will be treated as a list with 1 address. string will be treated as a list with 1 address.
- msg : The message to send. - msg : The message to send.
- mail_options : List of ESMTP options (such as 8bitmime) for the - mail_options : List of ESMTP options (such as 8bitmime) for the
mail command. mail command.
- rcpt_options : List of ESMTP options (such as DSN commands) for - rcpt_options : List of ESMTP options (such as DSN commands) for
@ -430,7 +430,7 @@ class SMTP:
This method may raise the following exceptions: This method may raise the following exceptions:
SMTPHeloError The server didn't reply properly to SMTPHeloError The server didn't reply properly to
the helo greeting. the helo greeting.
SMTPRecipientsRefused The server rejected ALL recipients SMTPRecipientsRefused The server rejected ALL recipients
(no mail was sent). (no mail was sent).
SMTPSenderRefused The server didn't accept the from_addr. SMTPSenderRefused The server didn't accept the from_addr.
@ -441,7 +441,7 @@ class SMTP:
Note: the connection will be open even after an exception is raised. Note: the connection will be open even after an exception is raised.
Example: Example:
>>> import smtplib >>> import smtplib
>>> s=smtplib.SMTP("localhost") >>> s=smtplib.SMTP("localhost")
>>> tolist=["one@one.org","two@two.org","three@three.org","four@four.org"] >>> tolist=["one@one.org","two@two.org","three@three.org","four@four.org"]
@ -453,7 +453,7 @@ class SMTP:
>>> s.sendmail("me@my.org",tolist,msg) >>> s.sendmail("me@my.org",tolist,msg)
{ "three@three.org" : ( 550 ,"User unknown" ) } { "three@three.org" : ( 550 ,"User unknown" ) }
>>> s.quit() >>> s.quit()
In the above example, the message was accepted for delivery to three In the above example, the message was accepted for delivery to three
of the four addresses, and one was rejected, with the error code of the four addresses, and one was rejected, with the error code
550. If all addresses are accepted, then the method will return an 550. If all addresses are accepted, then the method will return an
@ -494,7 +494,7 @@ class SMTP:
self.rset() self.rset()
raise SMTPDataError(code, resp) raise SMTPDataError(code, resp)
#if we got here then somebody got our mail #if we got here then somebody got our mail
return senderrs return senderrs
def close(self): def close(self):

View File

@ -32,20 +32,20 @@ explicitly given directories.
def what(filename): def what(filename):
"""Guess the type of a sound file""" """Guess the type of a sound file"""
res = whathdr(filename) res = whathdr(filename)
return res return res
def whathdr(filename): def whathdr(filename):
"""Recognize sound headers""" """Recognize sound headers"""
f = open(filename, 'r') f = open(filename, 'r')
h = f.read(512) h = f.read(512)
for tf in tests: for tf in tests:
res = tf(h, f) res = tf(h, f)
if res: if res:
return res return res
return None return None
#-----------------------------------# #-----------------------------------#
@ -55,113 +55,113 @@ def whathdr(filename):
tests = [] tests = []
def test_aifc(h, f): def test_aifc(h, f):
import aifc import aifc
if h[:4] != 'FORM': if h[:4] != 'FORM':
return None return None
if h[8:12] == 'AIFC': if h[8:12] == 'AIFC':
fmt = 'aifc' fmt = 'aifc'
elif h[8:12] == 'AIFF': elif h[8:12] == 'AIFF':
fmt = 'aiff' fmt = 'aiff'
else: else:
return None return None
f.seek(0) f.seek(0)
try: try:
a = aifc.openfp(f, 'r') a = aifc.openfp(f, 'r')
except (EOFError, aifc.Error): except (EOFError, aifc.Error):
return None return None
return (fmt, a.getframerate(), a.getnchannels(), \ return (fmt, a.getframerate(), a.getnchannels(), \
a.getnframes(), 8*a.getsampwidth()) a.getnframes(), 8*a.getsampwidth())
tests.append(test_aifc) tests.append(test_aifc)
def test_au(h, f): def test_au(h, f):
if h[:4] == '.snd': if h[:4] == '.snd':
f = get_long_be f = get_long_be
elif h[:4] in ('\0ds.', 'dns.'): elif h[:4] in ('\0ds.', 'dns.'):
f = get_long_le f = get_long_le
else: else:
return None return None
type = 'au' type = 'au'
hdr_size = f(h[4:8]) hdr_size = f(h[4:8])
data_size = f(h[8:12]) data_size = f(h[8:12])
encoding = f(h[12:16]) encoding = f(h[12:16])
rate = f(h[16:20]) rate = f(h[16:20])
nchannels = f(h[20:24]) nchannels = f(h[20:24])
sample_size = 1 # default sample_size = 1 # default
if encoding == 1: if encoding == 1:
sample_bits = 'U' sample_bits = 'U'
elif encoding == 2: elif encoding == 2:
sample_bits = 8 sample_bits = 8
elif encoding == 3: elif encoding == 3:
sample_bits = 16 sample_bits = 16
sample_size = 2 sample_size = 2
else: else:
sample_bits = '?' sample_bits = '?'
frame_size = sample_size * nchannels frame_size = sample_size * nchannels
return type, rate, nchannels, data_size/frame_size, sample_bits return type, rate, nchannels, data_size/frame_size, sample_bits
tests.append(test_au) tests.append(test_au)
def test_hcom(h, f): def test_hcom(h, f):
if h[65:69] != 'FSSD' or h[128:132] != 'HCOM': if h[65:69] != 'FSSD' or h[128:132] != 'HCOM':
return None return None
divisor = get_long_be(h[128+16:128+20]) divisor = get_long_be(h[128+16:128+20])
return 'hcom', 22050/divisor, 1, -1, 8 return 'hcom', 22050/divisor, 1, -1, 8
tests.append(test_hcom) tests.append(test_hcom)
def test_voc(h, f): def test_voc(h, f):
if h[:20] != 'Creative Voice File\032': if h[:20] != 'Creative Voice File\032':
return None return None
sbseek = get_short_le(h[20:22]) sbseek = get_short_le(h[20:22])
rate = 0 rate = 0
if 0 <= sbseek < 500 and h[sbseek] == '\1': if 0 <= sbseek < 500 and h[sbseek] == '\1':
ratecode = ord(h[sbseek+4]) ratecode = ord(h[sbseek+4])
rate = int(1000000.0 / (256 - ratecode)) rate = int(1000000.0 / (256 - ratecode))
return 'voc', rate, 1, -1, 8 return 'voc', rate, 1, -1, 8
tests.append(test_voc) tests.append(test_voc)
def test_wav(h, f): def test_wav(h, f):
# 'RIFF' <len> 'WAVE' 'fmt ' <len> # 'RIFF' <len> 'WAVE' 'fmt ' <len>
if h[:4] != 'RIFF' or h[8:12] != 'WAVE' or h[12:16] != 'fmt ': if h[:4] != 'RIFF' or h[8:12] != 'WAVE' or h[12:16] != 'fmt ':
return None return None
style = get_short_le(h[20:22]) style = get_short_le(h[20:22])
nchannels = get_short_le(h[22:24]) nchannels = get_short_le(h[22:24])
rate = get_long_le(h[24:28]) rate = get_long_le(h[24:28])
sample_bits = get_short_le(h[34:36]) sample_bits = get_short_le(h[34:36])
return 'wav', rate, nchannels, -1, sample_bits return 'wav', rate, nchannels, -1, sample_bits
tests.append(test_wav) tests.append(test_wav)
def test_8svx(h, f): def test_8svx(h, f):
if h[:4] != 'FORM' or h[8:12] != '8SVX': if h[:4] != 'FORM' or h[8:12] != '8SVX':
return None return None
# Should decode it to get #channels -- assume always 1 # Should decode it to get #channels -- assume always 1
return '8svx', 0, 1, 0, 8 return '8svx', 0, 1, 0, 8
tests.append(test_8svx) tests.append(test_8svx)
def test_sndt(h, f): def test_sndt(h, f):
if h[:5] == 'SOUND': if h[:5] == 'SOUND':
nsamples = get_long_le(h[8:12]) nsamples = get_long_le(h[8:12])
rate = get_short_le(h[20:22]) rate = get_short_le(h[20:22])
return 'sndt', rate, 1, nsamples, 8 return 'sndt', rate, 1, nsamples, 8
tests.append(test_sndt) tests.append(test_sndt)
def test_sndr(h, f): def test_sndr(h, f):
if h[:2] == '\0\0': if h[:2] == '\0\0':
rate = get_short_le(h[2:4]) rate = get_short_le(h[2:4])
if 4000 <= rate <= 25000: if 4000 <= rate <= 25000:
return 'sndr', rate, 1, -1, 8 return 'sndr', rate, 1, -1, 8
tests.append(test_sndr) tests.append(test_sndr)
@ -171,16 +171,16 @@ tests.append(test_sndr)
#---------------------------------------------# #---------------------------------------------#
def get_long_be(s): def get_long_be(s):
return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3]) return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3])
def get_long_le(s): def get_long_le(s):
return (ord(s[3])<<24) | (ord(s[2])<<16) | (ord(s[1])<<8) | ord(s[0]) return (ord(s[3])<<24) | (ord(s[2])<<16) | (ord(s[1])<<8) | ord(s[0])
def get_short_be(s): def get_short_be(s):
return (ord(s[0])<<8) | ord(s[1]) return (ord(s[0])<<8) | ord(s[1])
def get_short_le(s): def get_short_le(s):
return (ord(s[1])<<8) | ord(s[0]) return (ord(s[1])<<8) | ord(s[0])
#--------------------# #--------------------#
@ -188,40 +188,40 @@ def get_short_le(s):
#--------------------# #--------------------#
def test(): def test():
import sys import sys
recursive = 0 recursive = 0
if sys.argv[1:] and sys.argv[1] == '-r': if sys.argv[1:] and sys.argv[1] == '-r':
del sys.argv[1:2] del sys.argv[1:2]
recursive = 1 recursive = 1
try: try:
if sys.argv[1:]: if sys.argv[1:]:
testall(sys.argv[1:], recursive, 1) testall(sys.argv[1:], recursive, 1)
else: else:
testall(['.'], recursive, 1) testall(['.'], recursive, 1)
except KeyboardInterrupt: except KeyboardInterrupt:
sys.stderr.write('\n[Interrupted]\n') sys.stderr.write('\n[Interrupted]\n')
sys.exit(1) sys.exit(1)
def testall(list, recursive, toplevel): def testall(list, recursive, toplevel):
import sys import sys
import os import os
for filename in list: for filename in list:
if os.path.isdir(filename): if os.path.isdir(filename):
print filename + '/:', print filename + '/:',
if recursive or toplevel: if recursive or toplevel:
print 'recursing down:' print 'recursing down:'
import glob import glob
names = glob.glob(os.path.join(filename, '*')) names = glob.glob(os.path.join(filename, '*'))
testall(names, recursive, 0) testall(names, recursive, 0)
else: else:
print '*** directory (use -r) ***' print '*** directory (use -r) ***'
else: else:
print filename + ':', print filename + ':',
sys.stdout.flush() sys.stdout.flush()
try: try:
print what(filename) print what(filename)
except IOError: except IOError:
print '*** not found ***' print '*** not found ***'
if __name__ == '__main__': if __name__ == '__main__':
test() test()

View File

@ -4,7 +4,7 @@
"""\ """\
This module provides socket operations and some related functions. This module provides socket operations and some related functions.
On Unix, it supports IP (Internet Protocol) and Unix domain sockets. On Unix, it supports IP (Internet Protocol) and Unix domain sockets.
On other systems, it only supports IP. Functions specific for a On other systems, it only supports IP. Functions specific for a
socket are available as methods of the socket object. socket are available as methods of the socket object.
Functions: Functions:

View File

@ -24,10 +24,10 @@ ST_CTIME = 9
# Extract bits from the mode # Extract bits from the mode
def S_IMODE(mode): def S_IMODE(mode):
return mode & 07777 return mode & 07777
def S_IFMT(mode): def S_IFMT(mode):
return mode & 0170000 return mode & 0170000
# Constants used as S_IFMT() for various file types # Constants used as S_IFMT() for various file types
# (not all are implemented on all systems) # (not all are implemented on all systems)
@ -43,25 +43,25 @@ S_IFSOCK = 0140000
# Functions to test for each file type # Functions to test for each file type
def S_ISDIR(mode): def S_ISDIR(mode):
return S_IFMT(mode) == S_IFDIR return S_IFMT(mode) == S_IFDIR
def S_ISCHR(mode): def S_ISCHR(mode):
return S_IFMT(mode) == S_IFCHR return S_IFMT(mode) == S_IFCHR
def S_ISBLK(mode): def S_ISBLK(mode):
return S_IFMT(mode) == S_IFBLK return S_IFMT(mode) == S_IFBLK
def S_ISREG(mode): def S_ISREG(mode):
return S_IFMT(mode) == S_IFREG return S_IFMT(mode) == S_IFREG
def S_ISFIFO(mode): def S_ISFIFO(mode):
return S_IFMT(mode) == S_IFIFO return S_IFMT(mode) == S_IFIFO
def S_ISLNK(mode): def S_ISLNK(mode):
return S_IFMT(mode) == S_IFLNK return S_IFMT(mode) == S_IFLNK
def S_ISSOCK(mode): def S_ISSOCK(mode):
return S_IFMT(mode) == S_IFSOCK return S_IFMT(mode) == S_IFSOCK
# Names for permission bits # Names for permission bits

View File

@ -13,63 +13,63 @@ cache = {}
def stat(path): def stat(path):
"""Stat a file, possibly out of the cache.""" """Stat a file, possibly out of the cache."""
if cache.has_key(path): if cache.has_key(path):
return cache[path] return cache[path]
cache[path] = ret = os.stat(path) cache[path] = ret = os.stat(path)
return ret return ret
def reset(): def reset():
"""Reset the cache completely.""" """Reset the cache completely."""
global cache global cache
cache = {} cache = {}
def forget(path): def forget(path):
"""Remove a given item from the cache, if it exists.""" """Remove a given item from the cache, if it exists."""
if cache.has_key(path): if cache.has_key(path):
del cache[path] del cache[path]
def forget_prefix(prefix): def forget_prefix(prefix):
"""Remove all pathnames with a given prefix.""" """Remove all pathnames with a given prefix."""
n = len(prefix) n = len(prefix)
for path in cache.keys(): for path in cache.keys():
if path[:n] == prefix: if path[:n] == prefix:
del cache[path] del cache[path]
def forget_dir(prefix): def forget_dir(prefix):
"""Forget about a directory and all entries in it, but not about """Forget about a directory and all entries in it, but not about
entries in subdirectories.""" entries in subdirectories."""
if prefix[-1:] == '/' and prefix != '/': if prefix[-1:] == '/' and prefix != '/':
prefix = prefix[:-1] prefix = prefix[:-1]
forget(prefix) forget(prefix)
if prefix[-1:] != '/': if prefix[-1:] != '/':
prefix = prefix + '/' prefix = prefix + '/'
n = len(prefix) n = len(prefix)
for path in cache.keys(): for path in cache.keys():
if path[:n] == prefix: if path[:n] == prefix:
rest = path[n:] rest = path[n:]
if rest[-1:] == '/': rest = rest[:-1] if rest[-1:] == '/': rest = rest[:-1]
if '/' not in rest: if '/' not in rest:
del cache[path] del cache[path]
def forget_except_prefix(prefix): def forget_except_prefix(prefix):
"""Remove all pathnames except with a given prefix. """Remove all pathnames except with a given prefix.
Normally used with prefix = '/' after a chdir().""" Normally used with prefix = '/' after a chdir()."""
n = len(prefix) n = len(prefix)
for path in cache.keys(): for path in cache.keys():
if path[:n] != prefix: if path[:n] != prefix:
del cache[path] del cache[path]
def isdir(path): def isdir(path):
"""Check for directory.""" """Check for directory."""
try: try:
st = stat(path) st = stat(path)
except os.error: except os.error:
return 0 return 0
return S_ISDIR(st[ST_MODE]) return S_ISDIR(st[ST_MODE])

View File

@ -3,13 +3,13 @@
# Indices for statvfs struct members in the tuple returned by # Indices for statvfs struct members in the tuple returned by
# os.statvfs() and os.fstatvfs(). # os.statvfs() and os.fstatvfs().
F_BSIZE = 0 # Preferred file system block size F_BSIZE = 0 # Preferred file system block size
F_FRSIZE = 1 # Fundamental file system block size F_FRSIZE = 1 # Fundamental file system block size
F_BLOCKS = 2 # Total number of file system blocks (FRSIZE) F_BLOCKS = 2 # Total number of file system blocks (FRSIZE)
F_BFREE = 3 # Total number of free blocks F_BFREE = 3 # Total number of free blocks
F_BAVAIL = 4 # Free blocks available to non-superuser F_BAVAIL = 4 # Free blocks available to non-superuser
F_FILES = 5 # Total number of file nodes F_FILES = 5 # Total number of file nodes
F_FFREE = 6 # Total number of free file nodes F_FFREE = 6 # Total number of free file nodes
F_FAVAIL = 7 # Free nodes available to non-superuser F_FAVAIL = 7 # Free nodes available to non-superuser
F_FLAG = 8 # Flags (see your local statvfs man page) F_FLAG = 8 # Flags (see your local statvfs man page)
F_NAMEMAX = 9 # Maximum file name length F_NAMEMAX = 9 # Maximum file name length

View File

@ -27,7 +27,7 @@ letters = lowercase + uppercase
digits = '0123456789' digits = '0123456789'
hexdigits = digits + 'abcdef' + 'ABCDEF' hexdigits = digits + 'abcdef' + 'ABCDEF'
octdigits = '01234567' octdigits = '01234567'
punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""" punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
printable = digits + letters + punctuation + whitespace printable = digits + letters + punctuation + whitespace
# Case conversion helpers # Case conversion helpers

View File

@ -43,25 +43,25 @@ When the setpos() and rewind() methods are not used, the seek()
method is not necessary. method is not necessary.
This returns an instance of a class with the following public methods: This returns an instance of a class with the following public methods:
getnchannels() -- returns number of audio channels (1 for getnchannels() -- returns number of audio channels (1 for
mono, 2 for stereo) mono, 2 for stereo)
getsampwidth() -- returns sample width in bytes getsampwidth() -- returns sample width in bytes
getframerate() -- returns sampling frequency getframerate() -- returns sampling frequency
getnframes() -- returns number of audio frames getnframes() -- returns number of audio frames
getcomptype() -- returns compression type ('NONE' or 'ULAW') getcomptype() -- returns compression type ('NONE' or 'ULAW')
getcompname() -- returns human-readable version of getcompname() -- returns human-readable version of
compression type ('not compressed' matches 'NONE') compression type ('not compressed' matches 'NONE')
getparams() -- returns a tuple consisting of all of the getparams() -- returns a tuple consisting of all of the
above in the above order above in the above order
getmarkers() -- returns None (for compatibility with the getmarkers() -- returns None (for compatibility with the
aifc module) aifc module)
getmark(id) -- raises an error since the mark does not getmark(id) -- raises an error since the mark does not
exist (for compatibility with the aifc module) exist (for compatibility with the aifc module)
readframes(n) -- returns at most n frames of audio readframes(n) -- returns at most n frames of audio
rewind() -- rewind to the beginning of the audio stream rewind() -- rewind to the beginning of the audio stream
setpos(pos) -- seek to the specified position setpos(pos) -- seek to the specified position
tell() -- return the current position tell() -- return the current position
close() -- close the instance (make it unusable) close() -- close the instance (make it unusable)
The position returned by tell() and the position given to setpos() The position returned by tell() and the position given to setpos()
are compatible and have nothing to do with the actual position in the are compatible and have nothing to do with the actual position in the
file. file.
@ -75,22 +75,22 @@ The open file pointer must have methods write(), tell(), seek(), and
close(). close().
This returns an instance of a class with the following public methods: This returns an instance of a class with the following public methods:
setnchannels(n) -- set the number of channels setnchannels(n) -- set the number of channels
setsampwidth(n) -- set the sample width setsampwidth(n) -- set the sample width
setframerate(n) -- set the frame rate setframerate(n) -- set the frame rate
setnframes(n) -- set the number of frames setnframes(n) -- set the number of frames
setcomptype(type, name) setcomptype(type, name)
-- set the compression type and the -- set the compression type and the
human-readable compression type human-readable compression type
setparams(tuple)-- set all parameters at once setparams(tuple)-- set all parameters at once
tell() -- return current position in output file tell() -- return current position in output file
writeframesraw(data) writeframesraw(data)
-- write audio frames without pathing up the -- write audio frames without pathing up the
file header file header
writeframes(data) writeframes(data)
-- write audio frames and patch up the file header -- write audio frames and patch up the file header
close() -- patch up the file header and close the close() -- patch up the file header and close the
output file output file
You should set the parameters before the first writeframesraw or You should set the parameters before the first writeframesraw or
writeframes. The total number of frames does not need to be set, writeframes. The total number of frames does not need to be set,
but when it is set to the correct value, the header does not have to but when it is set to the correct value, the header does not have to
@ -119,356 +119,356 @@ AUDIO_FILE_ENCODING_ADPCM_G723_5 = 26
AUDIO_FILE_ENCODING_ALAW_8 = 27 AUDIO_FILE_ENCODING_ALAW_8 = 27
# from <multimedia/audio_hdr.h> # from <multimedia/audio_hdr.h>
AUDIO_UNKNOWN_SIZE = 0xFFFFFFFFL # ((unsigned)(~0)) AUDIO_UNKNOWN_SIZE = 0xFFFFFFFFL # ((unsigned)(~0))
_simple_encodings = [AUDIO_FILE_ENCODING_MULAW_8, _simple_encodings = [AUDIO_FILE_ENCODING_MULAW_8,
AUDIO_FILE_ENCODING_LINEAR_8, AUDIO_FILE_ENCODING_LINEAR_8,
AUDIO_FILE_ENCODING_LINEAR_16, AUDIO_FILE_ENCODING_LINEAR_16,
AUDIO_FILE_ENCODING_LINEAR_24, AUDIO_FILE_ENCODING_LINEAR_24,
AUDIO_FILE_ENCODING_LINEAR_32, AUDIO_FILE_ENCODING_LINEAR_32,
AUDIO_FILE_ENCODING_ALAW_8] AUDIO_FILE_ENCODING_ALAW_8]
class Error(Exception): class Error(Exception):
pass pass
def _read_u32(file): def _read_u32(file):
x = 0L x = 0L
for i in range(4): for i in range(4):
byte = file.read(1) byte = file.read(1)
if byte == '': if byte == '':
raise EOFError raise EOFError
x = x*256 + ord(byte) x = x*256 + ord(byte)
return x return x
def _write_u32(file, x): def _write_u32(file, x):
data = [] data = []
for i in range(4): for i in range(4):
d, m = divmod(x, 256) d, m = divmod(x, 256)
data.insert(0, m) data.insert(0, m)
x = d x = d
for i in range(4): for i in range(4):
file.write(chr(int(data[i]))) file.write(chr(int(data[i])))
class Au_read: class Au_read:
def __init__(self, f): def __init__(self, f):
if type(f) == type(''): if type(f) == type(''):
import __builtin__ import __builtin__
f = __builtin__.open(f, 'rb') f = __builtin__.open(f, 'rb')
self.initfp(f) self.initfp(f)
def __del__(self): def __del__(self):
if self._file: if self._file:
self.close() self.close()
def initfp(self, file): def initfp(self, file):
self._file = file self._file = file
self._soundpos = 0 self._soundpos = 0
magic = int(_read_u32(file)) magic = int(_read_u32(file))
if magic != AUDIO_FILE_MAGIC: if magic != AUDIO_FILE_MAGIC:
raise Error, 'bad magic number' raise Error, 'bad magic number'
self._hdr_size = int(_read_u32(file)) self._hdr_size = int(_read_u32(file))
if self._hdr_size < 24: if self._hdr_size < 24:
raise Error, 'header size too small' raise Error, 'header size too small'
if self._hdr_size > 100: if self._hdr_size > 100:
raise Error, 'header size ridiculously large' raise Error, 'header size ridiculously large'
self._data_size = _read_u32(file) self._data_size = _read_u32(file)
if self._data_size != AUDIO_UNKNOWN_SIZE: if self._data_size != AUDIO_UNKNOWN_SIZE:
self._data_size = int(self._data_size) self._data_size = int(self._data_size)
self._encoding = int(_read_u32(file)) self._encoding = int(_read_u32(file))
if self._encoding not in _simple_encodings: if self._encoding not in _simple_encodings:
raise Error, 'encoding not (yet) supported' raise Error, 'encoding not (yet) supported'
if self._encoding in (AUDIO_FILE_ENCODING_MULAW_8, if self._encoding in (AUDIO_FILE_ENCODING_MULAW_8,
AUDIO_FILE_ENCODING_ALAW_8): AUDIO_FILE_ENCODING_ALAW_8):
self._sampwidth = 2 self._sampwidth = 2
self._framesize = 1 self._framesize = 1
elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_8: elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_8:
self._framesize = self._sampwidth = 1 self._framesize = self._sampwidth = 1
elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_16: elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_16:
self._framesize = self._sampwidth = 2 self._framesize = self._sampwidth = 2
elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_24: elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_24:
self._framesize = self._sampwidth = 3 self._framesize = self._sampwidth = 3
elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_32: elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_32:
self._framesize = self._sampwidth = 4 self._framesize = self._sampwidth = 4
else: else:
raise Error, 'unknown encoding' raise Error, 'unknown encoding'
self._framerate = int(_read_u32(file)) self._framerate = int(_read_u32(file))
self._nchannels = int(_read_u32(file)) self._nchannels = int(_read_u32(file))
self._framesize = self._framesize * self._nchannels self._framesize = self._framesize * self._nchannels
if self._hdr_size > 24: if self._hdr_size > 24:
self._info = file.read(self._hdr_size - 24) self._info = file.read(self._hdr_size - 24)
for i in range(len(self._info)): for i in range(len(self._info)):
if self._info[i] == '\0': if self._info[i] == '\0':
self._info = self._info[:i] self._info = self._info[:i]
break break
else: else:
self._info = '' self._info = ''
def getfp(self): def getfp(self):
return self._file return self._file
def getnchannels(self): def getnchannels(self):
return self._nchannels return self._nchannels
def getsampwidth(self): def getsampwidth(self):
return self._sampwidth return self._sampwidth
def getframerate(self): def getframerate(self):
return self._framerate return self._framerate
def getnframes(self): def getnframes(self):
if self._data_size == AUDIO_UNKNOWN_SIZE: if self._data_size == AUDIO_UNKNOWN_SIZE:
return AUDIO_UNKNOWN_SIZE return AUDIO_UNKNOWN_SIZE
if self._encoding in _simple_encodings: if self._encoding in _simple_encodings:
return self._data_size / self._framesize return self._data_size / self._framesize
return 0 # XXX--must do some arithmetic here return 0 # XXX--must do some arithmetic here
def getcomptype(self): def getcomptype(self):
if self._encoding == AUDIO_FILE_ENCODING_MULAW_8: if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
return 'ULAW' return 'ULAW'
elif self._encoding == AUDIO_FILE_ENCODING_ALAW_8: elif self._encoding == AUDIO_FILE_ENCODING_ALAW_8:
return 'ALAW' return 'ALAW'
else: else:
return 'NONE' return 'NONE'
def getcompname(self): def getcompname(self):
if self._encoding == AUDIO_FILE_ENCODING_MULAW_8: if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
return 'CCITT G.711 u-law' return 'CCITT G.711 u-law'
elif self._encoding == AUDIO_FILE_ENCODING_ALAW_8: elif self._encoding == AUDIO_FILE_ENCODING_ALAW_8:
return 'CCITT G.711 A-law' return 'CCITT G.711 A-law'
else: else:
return 'not compressed' return 'not compressed'
def getparams(self): def getparams(self):
return self.getnchannels(), self.getsampwidth(), \ return self.getnchannels(), self.getsampwidth(), \
self.getframerate(), self.getnframes(), \ self.getframerate(), self.getnframes(), \
self.getcomptype(), self.getcompname() self.getcomptype(), self.getcompname()
def getmarkers(self): def getmarkers(self):
return None return None
def getmark(self, id): def getmark(self, id):
raise Error, 'no marks' raise Error, 'no marks'
def readframes(self, nframes): def readframes(self, nframes):
if self._encoding in _simple_encodings: if self._encoding in _simple_encodings:
if nframes == AUDIO_UNKNOWN_SIZE: if nframes == AUDIO_UNKNOWN_SIZE:
data = self._file.read() data = self._file.read()
else: else:
data = self._file.read(nframes * self._framesize * self._nchannels) data = self._file.read(nframes * self._framesize * self._nchannels)
if self._encoding == AUDIO_FILE_ENCODING_MULAW_8: if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
import audioop import audioop
data = audioop.ulaw2lin(data, self._sampwidth) data = audioop.ulaw2lin(data, self._sampwidth)
return data return data
return None # XXX--not implemented yet return None # XXX--not implemented yet
def rewind(self): def rewind(self):
self._soundpos = 0 self._soundpos = 0
self._file.seek(self._hdr_size) self._file.seek(self._hdr_size)
def tell(self): def tell(self):
return self._soundpos return self._soundpos
def setpos(self, pos): def setpos(self, pos):
if pos < 0 or pos > self.getnframes(): if pos < 0 or pos > self.getnframes():
raise Error, 'position not in range' raise Error, 'position not in range'
self._file.seek(pos * self._framesize + self._hdr_size) self._file.seek(pos * self._framesize + self._hdr_size)
self._soundpos = pos self._soundpos = pos
def close(self): def close(self):
self._file = None self._file = None
class Au_write: class Au_write:
def __init__(self, f): def __init__(self, f):
if type(f) == type(''): if type(f) == type(''):
import __builtin__ import __builtin__
f = __builtin__.open(f, 'wb') f = __builtin__.open(f, 'wb')
self.initfp(f) self.initfp(f)
def __del__(self): def __del__(self):
if self._file: if self._file:
self.close() self.close()
def initfp(self, file): def initfp(self, file):
self._file = file self._file = file
self._framerate = 0 self._framerate = 0
self._nchannels = 0 self._nchannels = 0
self._sampwidth = 0 self._sampwidth = 0
self._framesize = 0 self._framesize = 0
self._nframes = AUDIO_UNKNOWN_SIZE self._nframes = AUDIO_UNKNOWN_SIZE
self._nframeswritten = 0 self._nframeswritten = 0
self._datawritten = 0 self._datawritten = 0
self._datalength = 0 self._datalength = 0
self._info = '' self._info = ''
self._comptype = 'ULAW' # default is U-law self._comptype = 'ULAW' # default is U-law
def setnchannels(self, nchannels): def setnchannels(self, nchannels):
if self._nframeswritten: if self._nframeswritten:
raise Error, 'cannot change parameters after starting to write' raise Error, 'cannot change parameters after starting to write'
if nchannels not in (1, 2, 4): if nchannels not in (1, 2, 4):
raise Error, 'only 1, 2, or 4 channels supported' raise Error, 'only 1, 2, or 4 channels supported'
self._nchannels = nchannels self._nchannels = nchannels
def getnchannels(self): def getnchannels(self):
if not self._nchannels: if not self._nchannels:
raise Error, 'number of channels not set' raise Error, 'number of channels not set'
return self._nchannels return self._nchannels
def setsampwidth(self, sampwidth): def setsampwidth(self, sampwidth):
if self._nframeswritten: if self._nframeswritten:
raise Error, 'cannot change parameters after starting to write' raise Error, 'cannot change parameters after starting to write'
if sampwidth not in (1, 2, 4): if sampwidth not in (1, 2, 4):
raise Error, 'bad sample width' raise Error, 'bad sample width'
self._sampwidth = sampwidth self._sampwidth = sampwidth
def getsampwidth(self): def getsampwidth(self):
if not self._framerate: if not self._framerate:
raise Error, 'sample width not specified' raise Error, 'sample width not specified'
return self._sampwidth return self._sampwidth
def setframerate(self, framerate): def setframerate(self, framerate):
if self._nframeswritten: if self._nframeswritten:
raise Error, 'cannot change parameters after starting to write' raise Error, 'cannot change parameters after starting to write'
self._framerate = framerate self._framerate = framerate
def getframerate(self): def getframerate(self):
if not self._framerate: if not self._framerate:
raise Error, 'frame rate not set' raise Error, 'frame rate not set'
return self._framerate return self._framerate
def setnframes(self, nframes): def setnframes(self, nframes):
if self._nframeswritten: if self._nframeswritten:
raise Error, 'cannot change parameters after starting to write' raise Error, 'cannot change parameters after starting to write'
if nframes < 0: if nframes < 0:
raise Error, '# of frames cannot be negative' raise Error, '# of frames cannot be negative'
self._nframes = nframes self._nframes = nframes
def getnframes(self): def getnframes(self):
return self._nframeswritten return self._nframeswritten
def setcomptype(self, type, name): def setcomptype(self, type, name):
if type in ('NONE', 'ULAW'): if type in ('NONE', 'ULAW'):
self._comptype = type self._comptype = type
else: else:
raise Error, 'unknown compression type' raise Error, 'unknown compression type'
def getcomptype(self): def getcomptype(self):
return self._comptype return self._comptype
def getcompname(self): def getcompname(self):
if self._comptype == 'ULAW': if self._comptype == 'ULAW':
return 'CCITT G.711 u-law' return 'CCITT G.711 u-law'
elif self._comptype == 'ALAW': elif self._comptype == 'ALAW':
return 'CCITT G.711 A-law' return 'CCITT G.711 A-law'
else: else:
return 'not compressed' return 'not compressed'
def setparams(self, (nchannels, sampwidth, framerate, nframes, comptype, compname)): def setparams(self, (nchannels, sampwidth, framerate, nframes, comptype, compname)):
self.setnchannels(nchannels) self.setnchannels(nchannels)
self.setsampwidth(sampwidth) self.setsampwidth(sampwidth)
self.setframerate(framerate) self.setframerate(framerate)
self.setnframes(nframes) self.setnframes(nframes)
self.setcomptype(comptype, compname) self.setcomptype(comptype, compname)
def getparams(self): def getparams(self):
return self.getnchannels(), self.getsampwidth(), \ return self.getnchannels(), self.getsampwidth(), \
self.getframerate(), self.getnframes(), \ self.getframerate(), self.getnframes(), \
self.getcomptype(), self.getcompname() self.getcomptype(), self.getcompname()
def tell(self): def tell(self):
return self._nframeswritten return self._nframeswritten
def writeframesraw(self, data): def writeframesraw(self, data):
self._ensure_header_written() self._ensure_header_written()
nframes = len(data) / self._framesize nframes = len(data) / self._framesize
if self._comptype == 'ULAW': if self._comptype == 'ULAW':
import audioop import audioop
data = audioop.lin2ulaw(data, self._sampwidth) data = audioop.lin2ulaw(data, self._sampwidth)
self._file.write(data) self._file.write(data)
self._nframeswritten = self._nframeswritten + nframes self._nframeswritten = self._nframeswritten + nframes
self._datawritten = self._datawritten + len(data) self._datawritten = self._datawritten + len(data)
def writeframes(self, data): def writeframes(self, data):
self.writeframesraw(data) self.writeframesraw(data)
if self._nframeswritten != self._nframes or \ if self._nframeswritten != self._nframes or \
self._datalength != self._datawritten: self._datalength != self._datawritten:
self._patchheader() self._patchheader()
def close(self): def close(self):
self._ensure_header_written() self._ensure_header_written()
if self._nframeswritten != self._nframes or \ if self._nframeswritten != self._nframes or \
self._datalength != self._datawritten: self._datalength != self._datawritten:
self._patchheader() self._patchheader()
self._file.flush() self._file.flush()
self._file = None self._file = None
# #
# private methods # private methods
# #
def _ensure_header_written(self): def _ensure_header_written(self):
if not self._nframeswritten: if not self._nframeswritten:
if not self._nchannels: if not self._nchannels:
raise Error, '# of channels not specified' raise Error, '# of channels not specified'
if not self._sampwidth: if not self._sampwidth:
raise Error, 'sample width not specified' raise Error, 'sample width not specified'
if not self._framerate: if not self._framerate:
raise Error, 'frame rate not specified' raise Error, 'frame rate not specified'
self._write_header() self._write_header()
def _write_header(self): def _write_header(self):
if self._comptype == 'NONE': if self._comptype == 'NONE':
if self._sampwidth == 1: if self._sampwidth == 1:
encoding = AUDIO_FILE_ENCODING_LINEAR_8 encoding = AUDIO_FILE_ENCODING_LINEAR_8
self._framesize = 1 self._framesize = 1
elif self._sampwidth == 2: elif self._sampwidth == 2:
encoding = AUDIO_FILE_ENCODING_LINEAR_16 encoding = AUDIO_FILE_ENCODING_LINEAR_16
self._framesize = 2 self._framesize = 2
elif self._sampwidth == 4: elif self._sampwidth == 4:
encoding = AUDIO_FILE_ENCODING_LINEAR_32 encoding = AUDIO_FILE_ENCODING_LINEAR_32
self._framesize = 4 self._framesize = 4
else: else:
raise Error, 'internal error' raise Error, 'internal error'
elif self._comptype == 'ULAW': elif self._comptype == 'ULAW':
encoding = AUDIO_FILE_ENCODING_MULAW_8 encoding = AUDIO_FILE_ENCODING_MULAW_8
self._framesize = 1 self._framesize = 1
else: else:
raise Error, 'internal error' raise Error, 'internal error'
self._framesize = self._framesize * self._nchannels self._framesize = self._framesize * self._nchannels
_write_u32(self._file, AUDIO_FILE_MAGIC) _write_u32(self._file, AUDIO_FILE_MAGIC)
header_size = 25 + len(self._info) header_size = 25 + len(self._info)
header_size = (header_size + 7) & ~7 header_size = (header_size + 7) & ~7
_write_u32(self._file, header_size) _write_u32(self._file, header_size)
if self._nframes == AUDIO_UNKNOWN_SIZE: if self._nframes == AUDIO_UNKNOWN_SIZE:
length = AUDIO_UNKNOWN_SIZE length = AUDIO_UNKNOWN_SIZE
else: else:
length = self._nframes * self._framesize length = self._nframes * self._framesize
_write_u32(self._file, length) _write_u32(self._file, length)
self._datalength = length self._datalength = length
_write_u32(self._file, encoding) _write_u32(self._file, encoding)
_write_u32(self._file, self._framerate) _write_u32(self._file, self._framerate)
_write_u32(self._file, self._nchannels) _write_u32(self._file, self._nchannels)
self._file.write(self._info) self._file.write(self._info)
self._file.write('\0'*(header_size - len(self._info) - 24)) self._file.write('\0'*(header_size - len(self._info) - 24))
def _patchheader(self): def _patchheader(self):
self._file.seek(8) self._file.seek(8)
_write_u32(self._file, self._datawritten) _write_u32(self._file, self._datawritten)
self._datalength = self._datawritten self._datalength = self._datawritten
self._file.seek(0, 2) self._file.seek(0, 2)
def open(f, mode=None): def open(f, mode=None):
if mode is None: if mode is None:
if hasattr(f, 'mode'): if hasattr(f, 'mode'):
mode = f.mode mode = f.mode
else: else:
mode = 'rb' mode = 'rb'
if mode in ('r', 'rb'): if mode in ('r', 'rb'):
return Au_read(f) return Au_read(f)
elif mode in ('w', 'wb'): elif mode in ('w', 'wb'):
return Au_write(f) return Au_write(f)
else: else:
raise Error, "mode must be 'r', 'rb', 'w', or 'wb'" raise Error, "mode must be 'r', 'rb', 'w', or 'wb'"
openfp = open openfp = open

View File

@ -3,42 +3,42 @@
MAGIC = '.snd' MAGIC = '.snd'
class error(Exception): class error(Exception):
pass pass
def get_long_be(s): def get_long_be(s):
"""Convert a 4-char value to integer.""" """Convert a 4-char value to integer."""
return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3]) return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3])
def gethdr(fp): def gethdr(fp):
"""Read a sound header from an open file.""" """Read a sound header from an open file."""
if fp.read(4) != MAGIC: if fp.read(4) != MAGIC:
raise error, 'gethdr: bad magic word' raise error, 'gethdr: bad magic word'
hdr_size = get_long_be(fp.read(4)) hdr_size = get_long_be(fp.read(4))
data_size = get_long_be(fp.read(4)) data_size = get_long_be(fp.read(4))
encoding = get_long_be(fp.read(4)) encoding = get_long_be(fp.read(4))
sample_rate = get_long_be(fp.read(4)) sample_rate = get_long_be(fp.read(4))
channels = get_long_be(fp.read(4)) channels = get_long_be(fp.read(4))
excess = hdr_size - 24 excess = hdr_size - 24
if excess < 0: if excess < 0:
raise error, 'gethdr: bad hdr_size' raise error, 'gethdr: bad hdr_size'
if excess > 0: if excess > 0:
info = fp.read(excess) info = fp.read(excess)
else: else:
info = '' info = ''
return (data_size, encoding, sample_rate, channels, info) return (data_size, encoding, sample_rate, channels, info)
def printhdr(file): def printhdr(file):
"""Read and print the sound header of a named file.""" """Read and print the sound header of a named file."""
hdr = gethdr(open(file, 'r')) hdr = gethdr(open(file, 'r'))
data_size, encoding, sample_rate, channels, info = hdr data_size, encoding, sample_rate, channels, info = hdr
while info[-1:] == '\0': while info[-1:] == '\0':
info = info[:-1] info = info[:-1]
print 'File name: ', file print 'File name: ', file
print 'Data size: ', data_size print 'Data size: ', data_size
print 'Encoding: ', encoding print 'Encoding: ', encoding
print 'Sample rate:', sample_rate print 'Sample rate:', sample_rate
print 'Channels: ', channels print 'Channels: ', channels
print 'Info: ', `info` print 'Info: ', `info`