Commit a4cd6cdf authored by Guido van Rossum's avatar Guido van Rossum

* calendar.py: minor cleanups

* ftplib.py: support __init__ with optional host, port args
* aifc.py: ensure header is written on close even when no data is written
parent f94d263d
......@@ -619,6 +619,7 @@ class Aifc_write:
self._nframes = 0
self._nframeswritten = 0
self._datawritten = 0
self._datalength = 0
self._markers = []
self._marklength = 0
self._aifc = 1 # AIFF-C is default
......@@ -743,19 +744,7 @@ class Aifc_write:
return self._markers
def writeframesraw(self, data):
if not self._nframeswritten:
if self._comptype in ('ULAW', 'ALAW'):
if not self._sampwidth:
self._sampwidth = AL.SAMPLE_16
if self._sampwidth != AL.SAMPLE_16:
raise Error, 'sample width must be 2 when compressing with ULAW or ALAW'
if not self._nchannels:
raise Error, '# channels not specified'
if not self._sampwidth:
raise Error, 'sample width not specified'
if not self._framerate:
raise Error, 'sampling rate not specified'
self._write_header(len(data))
self._ensure_header_written(len(data))
nframes = len(data) / (self._sampwidth * self._nchannels)
if self._comp:
dummy = self._comp.SetParam(CL.FRAME_BUFFER_SIZE, \
......@@ -774,6 +763,7 @@ class Aifc_write:
self._patchheader()
def close(self):
self._ensure_header_written(0)
if self._datawritten & 1:
# quick pad to even size
self._file.write(chr(0))
......@@ -792,6 +782,21 @@ class Aifc_write:
#
# Internal methods.
#
def _ensure_header_written(self, datasize):
if not self._nframeswritten:
if self._comptype in ('ULAW', 'ALAW'):
if not self._sampwidth:
self._sampwidth = AL.SAMPLE_16
if self._sampwidth != AL.SAMPLE_16:
raise Error, 'sample width must be 2 when compressing with ULAW or ALAW'
if not self._nchannels:
raise Error, '# channels not specified'
if not self._sampwidth:
raise Error, 'sample width not specified'
if not self._framerate:
raise Error, 'sampling rate not specified'
self._write_header(datasize)
def _write_header(self, initlength):
if self._aifc and self._comptype != 'NONE':
try:
......
......@@ -9,11 +9,18 @@
# - the order of the elements of a 'struct tm' differs, to ease sorting
# - months numbers are 1-12, not 0-11; month arrays have a dummy element 0
# - Monday is the first day of the week (numbered 0)
# - years are expressed in full, e.g. 1970, not 70.
# - timezone is currently hardcoded
# - doesn't know about daylight saving time
# These are really parameters of the 'time' module:
epoch = 1970 # Time began on January 1 of this year (00:00:00 UTC)
day_0 = 3 # The epoch begins on a Thursday (Monday = 0)
# Localization: Minutes West from Greenwich
timezone = -2*60 # Middle-European time with DST on
# timezone = 5*60 # EST (sigh -- THINK time() doesn't return UTC)
# Return 1 for leap years, 0 for non-leap years
def isleap(year):
return year % 4 == 0 and (year % 100 <> 0 or year % 400 == 0)
......@@ -97,10 +104,6 @@ def asctime(arg):
s = s + ' ' + dd(`hours`) + ':' + dd(`mins`) + ':' + dd(`secs`)
return s + ' ' + `year`
# Localization: Minutes West from Greenwich
timezone = -2*60 # Middle-European time with DST on
# timezone = 5*60 # EST (sigh -- THINK time() doesn't return UTC)
# Local time ignores DST issues for now -- adjust 'timezone' to fake it
def localtime(secs):
return gmtime(secs - timezone*60)
......
......@@ -68,16 +68,31 @@ PORT_CYCLE = 1000
# The class itself
class FTP:
# Initialize an instance. Arguments:
# - host: hostname to connect to
# - port: port to connect to (default the standard FTP port)
def init(self, host, *args):
if len(args) > 1: raise TypeError, 'too many args'
if args: port = args[0]
else: port = FTP_PORT
self.host = host
self.port = port
# New initialization method (called by class instantiation)
# Initialize host to localhost, port to standard ftp port
def __init__(self, *args):
# Initialize the instance to something mostly harmless
self.debugging = 0
self.host = ''
self.port = FTP_PORT
self.sock = None
self.file = None
self.welcome = None
if args:
apply(self.connect, args)
# Old init method (explicitly called by caller)
def init(self, *args):
if args:
apply(self.connect, args)
# Connect to host. Arguments:
# - host: hostname to connect to (default previous host)
# - port: port to connect to (default previous port)
def init(self, *args):
if args: self.host = args[0]
if args[1:]: self.port = args[1]
if args[2:]: raise TypeError, 'too many args'
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect(self.host, self.port)
self.file = self.sock.makefile('r')
......
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