Kaydet (Commit) 495ad3c8 authored tarafından Tim Peters's avatar Tim Peters

Whitespace normalization.

üst 0c9886d5
...@@ -41,10 +41,10 @@ class scheduler: ...@@ -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: ...@@ -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: ...@@ -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 When there is a positive delay until the first event, the
delay function is called and the event is left in the queue; delay function is called and the event is left in the queue;
otherwise, the event is removed from the queue and executed otherwise, the event is removed from the queue and executed
(its action function is called, passing it the argument). If (its action function is called, passing it the argument). If
the delay function returns prematurely, it is simply the delay function returns prematurely, it is simply
restarted. restarted.
It is legal for both the delay function and the action It is legal for both the delay function and the action
function to to modify the queue or to raise an exception; function to to modify the queue or to raise an exception;
exceptions are not caught but the scheduler's state remains exceptions are not caught but the scheduler's state remains
well-defined so run() may be called again. well-defined so run() may be called again.
A questionably hack is added to allow other threads to run: A questionably hack is added to allow other threads to run:
just after an event is executed, a delay of 0 is executed, to just after an event is executed, a delay of 0 is executed, to
avoid monopolizing the CPU when other threads are also avoid monopolizing the CPU when other threads are also
runnable. runnable.
""" """
q = self.queue q = self.queue
while q: while q:
time, priority, action, argument = q[0] time, priority, action, argument = q[0]
......
...@@ -137,7 +137,7 @@ class SGMLParser: ...@@ -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: ...@@ -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
......
...@@ -31,127 +31,127 @@ or may not be necessary to flush changes to disk. ...@@ -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): def keys(self):
return self.dict.keys() return self.dict.keys()
def __len__(self): def __len__(self):
return len(self.dict) return len(self.dict)
def has_key(self, key): def has_key(self, key):
return self.dict.has_key(key) return self.dict.has_key(key)
def get(self, key, default=None): def get(self, key, default=None):
if self.dict.has_key(key): if self.dict.has_key(key):
return self[key] return self[key]
return default return default
def __getitem__(self, key): def __getitem__(self, key):
f = StringIO(self.dict[key]) f = StringIO(self.dict[key])
return Unpickler(f).load() return Unpickler(f).load()
def __setitem__(self, key, value): def __setitem__(self, key, value):
f = StringIO() f = StringIO()
p = Pickler(f) p = Pickler(f)
p.dump(value) p.dump(value)
self.dict[key] = f.getvalue() self.dict[key] = f.getvalue()
def __delitem__(self, key): def __delitem__(self, key):
del self.dict[key] del self.dict[key]
def close(self): def close(self):
try: try:
self.dict.close() self.dict.close()
except: except:
pass pass
self.dict = 0 self.dict = 0
def __del__(self): def __del__(self):
self.close() self.close()
def sync(self): def sync(self):
if hasattr(self.dict, 'sync'): if hasattr(self.dict, 'sync'):
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.
See the module's __doc__ string for an overview of the interface.
"""
This is initialized with the filename for the dbm database. def __init__(self, filename, flag='c'):
See the module's __doc__ string for an overview of the interface. import anydbm
""" Shelf.__init__(self, anydbm.open(filename, flag))
def __init__(self, filename, flag='c'):
import anydbm
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.
See the module's __doc__ string for an overview of the interface.
"""
Argument is the filename for the dbm database. return DbfilenameShelf(filename, flag)
See the module's __doc__ string for an overview of the interface.
"""
return DbfilenameShelf(filename, flag)
"""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 ...@@ -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: ...@@ -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: ...@@ -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:
......
...@@ -17,7 +17,7 @@ def copyfileobj(fsrc, fdst, length=16*1024): ...@@ -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): ...@@ -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.
""" """
......
...@@ -36,7 +36,7 @@ Example: ...@@ -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 ...@@ -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): ...@@ -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): ...@@ -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: ...@@ -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: ...@@ -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: ...@@ -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: ...@@ -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: ...@@ -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: ...@@ -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: ...@@ -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: ...@@ -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: ...@@ -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: ...@@ -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: ...@@ -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: ...@@ -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):
......
...@@ -32,20 +32,20 @@ explicitly given directories. ...@@ -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): ...@@ -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) ...@@ -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): ...@@ -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()
...@@ -4,7 +4,7 @@ ...@@ -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:
......
...@@ -24,10 +24,10 @@ ST_CTIME = 9 ...@@ -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 ...@@ -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
......
...@@ -13,63 +13,63 @@ cache = {} ...@@ -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])
...@@ -3,13 +3,13 @@ ...@@ -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
...@@ -27,7 +27,7 @@ letters = lowercase + uppercase ...@@ -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
......
This diff is collapsed.
...@@ -3,42 +3,42 @@ ...@@ -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`
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment