Commit 5cfa5dfe authored by Guido van Rossum's avatar Guido van Rossum

* calendar.py: all libC functionality now moved to built-in time module

* imghdr.py: added jpeg recognition
* torgb.py: added jpeg conversion
* tzparse.py: use functions from time instead of calendar
* whatsound.py: add /ufs/guido/biin/sgi to $PATH when calling 'whatsound'
parent 9b3bc715
############################## ###############################
# Calendar support functions # # Calendar printing functions #
############################## ###############################
# Revision 2: uses funtions from built-in time module where possible. # Revision 2: uses funtions from built-in time module
# Import functions and variables from time module # Import functions and variables from time module
from time import gmtime, localtime, mktime from time import gmtime, localtime, mktime, asctime, ctime
from time import timezone, altzone, daylight, tzname from time import timezone, altzone, daylight, tzname
# Exception raised for bad input (with string parameter for details) # Exception raised for bad input (with string parameter for details)
error = 'calendar.error' error = 'calendar.error'
# Abbreviated names of months (1-based arrays!!!)
month_abbr = [' ', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', \
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
# Turn calendar time as returned by localtime() into a string
def asctime(arg):
year, month, day, hours, mins, secs, wday, yday, isdst = arg
return '%s %s %02d %02d:%02d:%02d %04d' % (
day_abbr[wday], month_abbr[month], day,
hours, mins, secs, year)
# UNIX-style ctime (except it doesn't append '\n'!)
def ctime(secs):
return asctime(localtime(secs))
######################
# Non-UNIX additions #
######################
# Calendar printing etc.
# Note when comparing these calendars to the ones printed by cal(1): # Note when comparing these calendars to the ones printed by cal(1):
# My calendars have Monday as the first day of the week, and Sunday as # My calendars have Monday as the first day of the week, and Sunday as
# the last! (I believe this is the European convention.) # the last! (I believe this is the European convention.)
...@@ -48,10 +27,12 @@ day_name = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', \ ...@@ -48,10 +27,12 @@ day_name = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', \
'Friday', 'Saturday', 'Sunday'] 'Friday', 'Saturday', 'Sunday']
day_abbr = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] day_abbr = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
# Full names of months (1-based arrays!!!) # Full and abbreviated names of months (1-based arrays!!!)
month_name = ['', 'January', 'February', 'March', 'April', \ month_name = ['', 'January', 'February', 'March', 'April', \
'May', 'June', 'July', 'August', \ 'May', 'June', 'July', 'August', \
'September', 'October', 'November', 'December'] 'September', 'October', 'November', 'December']
month_abbr = [' ', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', \
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
# Return 1 for leap years, 0 for non-leap years # Return 1 for leap years, 0 for non-leap years
def isleap(year): def isleap(year):
......
...@@ -81,6 +81,12 @@ def test_xbm(h, f): ...@@ -81,6 +81,12 @@ def test_xbm(h, f):
tests.append(test_xbm) tests.append(test_xbm)
def test_jpeg(h, f):
# JPEG data in JFIF format
if h[6:10] == 'JFIF':
return 'jpeg'
tests.append(test_jpeg)
#--------------------# #--------------------#
# Small test program # # Small test program #
......
...@@ -40,6 +40,12 @@ t.append('pnmtoppm', '--') ...@@ -40,6 +40,12 @@ t.append('pnmtoppm', '--')
t.append('fromppm $IN $OUT', 'ff') t.append('fromppm $IN $OUT', 'ff')
table['rast'] = t table['rast'] = t
t = pipes.Template().init()
t.append('djpeg', '--')
t.append('pnmtoppm', '--')
t.append('fromppm $IN $OUT', 'ff')
table['jpeg'] = t
uncompress = pipes.Template().init() uncompress = pipes.Template().init()
uncompress.append('uncompress', '--') uncompress.append('uncompress', '--')
......
...@@ -56,6 +56,7 @@ def whatraw(filename): ...@@ -56,6 +56,7 @@ def whatraw(filename):
from stat import ST_SIZE from stat import ST_SIZE
# XXX "whatsound" should be part of the distribution somehow... # XXX "whatsound" should be part of the distribution somehow...
cmd = 'whatsound ' + filename + ' 2>/dev/null' cmd = 'whatsound ' + filename + ' 2>/dev/null'
cmd = 'PATH=$PATH:/ufs/guido/bin/sgi\n' + cmd
pipe = os.popen(cmd, 'r') pipe = os.popen(cmd, 'r')
data = pipe.read() data = pipe.read()
sts = pipe.close() sts = pipe.close()
......
...@@ -40,6 +40,12 @@ t.append('pnmtoppm', '--') ...@@ -40,6 +40,12 @@ t.append('pnmtoppm', '--')
t.append('fromppm $IN $OUT', 'ff') t.append('fromppm $IN $OUT', 'ff')
table['rast'] = t table['rast'] = t
t = pipes.Template().init()
t.append('djpeg', '--')
t.append('pnmtoppm', '--')
t.append('fromppm $IN $OUT', 'ff')
table['jpeg'] = t
uncompress = pipes.Template().init() uncompress = pipes.Template().init()
uncompress.append('uncompress', '--') uncompress.append('uncompress', '--')
......
...@@ -24,11 +24,11 @@ def tzparse(tzstr): ...@@ -24,11 +24,11 @@ def tzparse(tzstr):
[tzname, delta, dstname, daystart, hourstart, dayend, hourend] = subs [tzname, delta, dstname, daystart, hourstart, dayend, hourend] = subs
return (tzname, delta, dstname, daystart, hourstart, dayend, hourend) return (tzname, delta, dstname, daystart, hourstart, dayend, hourend)
def tzlocaltime(time, params): def tzlocaltime(secs, params):
import calendar import time
(tzname, delta, dstname, daystart, hourstart, dayend, hourend) = params (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = params
year, month, days, hours, mins, secs, yday, wday = \ year, month, days, hours, mins, secs, yday, wday, isdst = \
calendar.gmtime(time - delta*3600) time.gmtime(secs - delta*3600)
if (daystart, hourstart) <= (yday+1, hours) < (dayend, hourend): if (daystart, hourstart) <= (yday+1, hours) < (dayend, hourend):
tzname = dstname tzname = dstname
hours = hours + 1 hours = hours + 1
...@@ -44,33 +44,39 @@ def tzset(): ...@@ -44,33 +44,39 @@ def tzset():
daylight = 1 daylight = 1
tzname = tzparams[0], tzparams[2] tzname = tzparams[0], tzparams[2]
def isdst(time): def isdst(secs):
import calendar import time
(tzname, delta, dstname, daystart, hourstart, dayend, hourend) = \ (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = \
tzparams tzparams
year, month, days, hours, mins, secs, yday, wday, isdst = \ year, month, days, hours, mins, secs, yday, wday, isdst = \
calendar.gmtime(time - delta*3600) time.gmtime(secs - delta*3600)
return (daystart, hourstart) <= (yday+1, hours) < (dayend, hourend) return (daystart, hourstart) <= (yday+1, hours) < (dayend, hourend)
tzset() tzset()
def localtime(time): def localtime(secs):
return tzlocaltime(time, tzparams) return tzlocaltime(secs, tzparams)
def test(): def test():
from calendar import asctime, gmtime from time import asctime, gmtime
import time, sys import time, sys
now = time.time() now = time.time()
x = localtime(now) x = localtime(now)
print 'now =', now, '=', asctime(x[:-1]), x[-1] tm = x[:-1] + (0,)
print 'now =', now, '=', asctime(tm), x[-1]
now = now - now % (24*3600) now = now - now % (24*3600)
if sys.argv[1:]: now = now + eval(sys.argv[1]) if sys.argv[1:]: now = now + eval(sys.argv[1])
x = gmtime(now) x = gmtime(now)
print 'gmtime =', now, '=', asctime(x), 'yday =', x[-2] tm = x[:-1] + (0,)
print 'gmtime =', now, '=', asctime(tm), 'yday =', x[-2]
jan1 = now - x[-2]*24*3600 jan1 = now - x[-2]*24*3600
x = localtime(jan1) x = localtime(jan1)
print 'jan1 =', jan1, '=', asctime(x[:-1]), x[-1] tm = x[:-1] + (0,)
print 'jan1 =', jan1, '=', asctime(tm), x[-1]
for d in range(85, 95) + range(265, 275): for d in range(85, 95) + range(265, 275):
t = jan1 + d*24*3600 t = jan1 + d*24*3600
x = localtime(t) x = localtime(t)
print 'd =', d, 't =', t, '=', asctime(x[:-1]), x[-1] tm = x[:-1] + (0,)
print 'd =', d, 't =', t, '=', asctime(tm), x[-1]
test()
...@@ -56,6 +56,7 @@ def whatraw(filename): ...@@ -56,6 +56,7 @@ def whatraw(filename):
from stat import ST_SIZE from stat import ST_SIZE
# XXX "whatsound" should be part of the distribution somehow... # XXX "whatsound" should be part of the distribution somehow...
cmd = 'whatsound ' + filename + ' 2>/dev/null' cmd = 'whatsound ' + filename + ' 2>/dev/null'
cmd = 'PATH=$PATH:/ufs/guido/bin/sgi\n' + cmd
pipe = os.popen(cmd, 'r') pipe = os.popen(cmd, 'r')
data = pipe.read() data = pipe.read()
sts = pipe.close() sts = pipe.close()
......
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