The usual.
This commit is contained in:
parent
01b7ced834
commit
2e7840fe8f
|
@ -24,34 +24,37 @@ ConfigParser -- responsible for for parsing a list of
|
|||
|
||||
methods:
|
||||
|
||||
__init__(defaults=None) -- create the parser and specify a
|
||||
dictionary of intrinsic defaults. The
|
||||
keys must be strings, the values must
|
||||
be appropriate for %()s string
|
||||
interpolation. Note that `__name__' is
|
||||
always an intrinsic default; it's value
|
||||
is the section's name.
|
||||
__init__(defaults=None)
|
||||
create the parser and specify a dictionary of intrinsic defaults. The
|
||||
keys must be strings, the values must be appropriate for %()s string
|
||||
interpolation. Note that `__name__' is always an intrinsic default;
|
||||
it's value is the section's name.
|
||||
|
||||
sections() -- return all the configuration section names, sans DEFAULT
|
||||
sections()
|
||||
return all the configuration section names, sans DEFAULT
|
||||
|
||||
options(section) -- return list of configuration options for the named
|
||||
section
|
||||
options(section)
|
||||
return list of configuration options for the named section
|
||||
|
||||
read(*filenames) -- read and parse the list of named configuration files
|
||||
read(filenames)
|
||||
read and parse the list of named configuration files
|
||||
|
||||
get(section, option, raw=0) -- return a string value for the named
|
||||
option. All % interpolations are
|
||||
expanded in the return values, based on
|
||||
the defaults passed into the constructor
|
||||
and the DEFAULT section.
|
||||
get(section, option, raw=0, vars=None)
|
||||
return a string value for the named option. All % interpolations are
|
||||
expanded in the return values, based on the defaults passed into the
|
||||
constructor and the DEFAULT section. Additional substitutions may be
|
||||
provided using the `vars' argument, which must be a dictionary whose
|
||||
contents override any pre-existing defaults.
|
||||
|
||||
getint(section, options) -- like get(), but convert value to an integer
|
||||
getint(section, options)
|
||||
like get(), but convert value to an integer
|
||||
|
||||
getfloat(section, options) -- like get(), but convert value to a float
|
||||
getfloat(section, options)
|
||||
like get(), but convert value to a float
|
||||
|
||||
getboolean(section, options) -- like get(), but convert value to
|
||||
a boolean (currently defined as 0
|
||||
or 1, only)
|
||||
getboolean(section, options)
|
||||
like get(), but convert value to a boolean (currently defined as 0 or
|
||||
1, only)
|
||||
"""
|
||||
|
||||
import sys
|
||||
|
@ -173,12 +176,14 @@ class ConfigParser:
|
|||
except IOError:
|
||||
pass
|
||||
|
||||
def get(self, section, option, raw=0):
|
||||
def get(self, section, option, raw=0, vars=None):
|
||||
"""Get an option value for a given section.
|
||||
|
||||
All % interpolations are expanded in the return values, based
|
||||
on the defaults passed into the constructor, unless the optional
|
||||
argument `raw' is true.
|
||||
All % interpolations are expanded in the return values, based on the
|
||||
defaults passed into the constructor, unless the optional argument
|
||||
`raw' is true. Additional substitutions may be provided using the
|
||||
`vars' argument, which must be a dictionary whose contents overrides
|
||||
any pre-existing defaults.
|
||||
|
||||
The section DEFAULT is special.
|
||||
"""
|
||||
|
@ -191,6 +196,9 @@ class ConfigParser:
|
|||
raise NoSectionError(section)
|
||||
d = self.__defaults.copy()
|
||||
d.update(sectdict)
|
||||
# Update with the entry specific variables
|
||||
if vars:
|
||||
d.update(vars)
|
||||
option = string.lower(option)
|
||||
try:
|
||||
rawval = d[option]
|
||||
|
@ -199,10 +207,16 @@ class ConfigParser:
|
|||
# do the string interpolation
|
||||
if raw:
|
||||
return rawval
|
||||
|
||||
value = rawval # Make it a pretty variable name
|
||||
while 1: # Loop through this until it's done
|
||||
if not string.find(value, "%("):
|
||||
try:
|
||||
return rawval % d
|
||||
value = value % d
|
||||
except KeyError, key:
|
||||
raise InterpolationError(key, option, section, rawval)
|
||||
else:
|
||||
return value
|
||||
|
||||
def __get(self, section, conv, option):
|
||||
return conv(self.get(section, option))
|
||||
|
|
|
@ -367,3 +367,10 @@ def normpath(path):
|
|||
if not comps and not slashes:
|
||||
comps.append('.')
|
||||
return slashes + string.joinfields(comps, '/')
|
||||
|
||||
|
||||
# Return an absolute path.
|
||||
def abspath(path):
|
||||
if not isabs(path):
|
||||
path = join(os.getcwd(), path)
|
||||
return normpath(path)
|
||||
|
|
|
@ -5,9 +5,14 @@
|
|||
try:
|
||||
class Empty(Exception):
|
||||
pass
|
||||
class Full(Exception):
|
||||
pass
|
||||
except TypeError:
|
||||
# string based exceptions
|
||||
Empty = 'Queue.Empty' # Exception raised by get_nowait()
|
||||
# exception raised by get(block=0)/get_nowait()
|
||||
Empty = 'Queue.Empty'
|
||||
# exception raised by put(block=0)/put_nowait()
|
||||
Full = 'Queue.Full'
|
||||
|
||||
class Queue:
|
||||
def __init__(self, maxsize):
|
||||
|
@ -23,32 +28,38 @@ class Queue:
|
|||
self.fsema = thread.allocate_lock()
|
||||
|
||||
def qsize(self):
|
||||
"""Returns the approximate size of the queue (not reliable!)."""
|
||||
"""Return the approximate size of the queue (not reliable!)."""
|
||||
self.mutex.acquire()
|
||||
n = self._qsize()
|
||||
self.mutex.release()
|
||||
return n
|
||||
|
||||
def empty(self):
|
||||
"""Returns 1 if the queue is empty, 0 otherwise (not reliable!)."""
|
||||
"""Return 1 if the queue is empty, 0 otherwise (not reliable!)."""
|
||||
self.mutex.acquire()
|
||||
n = self._empty()
|
||||
self.mutex.release()
|
||||
return n
|
||||
|
||||
def full(self):
|
||||
"""Returns 1 if the queue is full, 0 otherwise (not reliable!)."""
|
||||
"""Return 1 if the queue is full, 0 otherwise (not reliable!)."""
|
||||
self.mutex.acquire()
|
||||
n = self._full()
|
||||
self.mutex.release()
|
||||
return n
|
||||
|
||||
def put(self, item):
|
||||
def put(self, item, block=1):
|
||||
"""Put an item into the queue.
|
||||
|
||||
If the queue is full, block until a free slot is avaiable.
|
||||
If optional arg 'block' is 1 (the default), block if
|
||||
necessary until a free slot is available. Otherwise (block
|
||||
is 0), put an item on the queue if a free slot is immediately
|
||||
available, else raise the Full exception.
|
||||
"""
|
||||
if block:
|
||||
self.fsema.acquire()
|
||||
elif not self.fsema.acquire(0):
|
||||
raise Full
|
||||
self.mutex.acquire()
|
||||
was_empty = self._empty()
|
||||
self._put(item)
|
||||
|
@ -58,12 +69,26 @@ class Queue:
|
|||
self.fsema.release()
|
||||
self.mutex.release()
|
||||
|
||||
def get(self):
|
||||
"""Gets and returns an item from the queue.
|
||||
def put_nowait(self, item):
|
||||
"""Put an item into the queue without blocking.
|
||||
|
||||
This method blocks if necessary until an item is available.
|
||||
Only enqueue the item if a free slot is immediately available.
|
||||
Otherwise raise the Full exception.
|
||||
"""
|
||||
return self.put(item, 0)
|
||||
|
||||
def get(self, block=1):
|
||||
"""Remove and return an item from the queue.
|
||||
|
||||
If optional arg 'block' is 1 (the default), block if
|
||||
necessary until an item is available. Otherwise (block is 0),
|
||||
return an item if one is immediately available, else raise the
|
||||
Empty exception.
|
||||
"""
|
||||
if block:
|
||||
self.esema.acquire()
|
||||
elif not self.esema.acquire(0):
|
||||
raise Empty
|
||||
self.mutex.acquire()
|
||||
was_full = self._full()
|
||||
item = self._get()
|
||||
|
@ -74,40 +99,13 @@ class Queue:
|
|||
self.mutex.release()
|
||||
return item
|
||||
|
||||
# Get an item from the queue if one is immediately available,
|
||||
# raise Empty if the queue is empty or temporarily unavailable
|
||||
def get_nowait(self):
|
||||
"""Gets and returns an item from the queue.
|
||||
"""Remove and return an item from the queue without blocking.
|
||||
|
||||
Only gets an item if one is immediately available, Otherwise
|
||||
this raises the Empty exception if the queue is empty or
|
||||
temporarily unavailable.
|
||||
Only get an item if one is immediately available. Otherwise
|
||||
raise the Empty exception.
|
||||
"""
|
||||
locked = self.esema.acquire(0)
|
||||
self.mutex.acquire()
|
||||
if self._empty():
|
||||
# The queue is empty -- we can't have esema
|
||||
self.mutex.release()
|
||||
raise Empty
|
||||
if not locked:
|
||||
locked = self.esema.acquire(0)
|
||||
if not locked:
|
||||
# Somebody else has esema
|
||||
# but we have mutex --
|
||||
# go out of their way
|
||||
self.mutex.release()
|
||||
raise Empty
|
||||
was_full = self._full()
|
||||
item = self._get()
|
||||
if was_full:
|
||||
self.fsema.release()
|
||||
if not self._empty():
|
||||
self.esema.release()
|
||||
self.mutex.release()
|
||||
return item
|
||||
|
||||
# XXX Need to define put_nowait() as well.
|
||||
|
||||
return self.get(0)
|
||||
|
||||
# Override these methods to implement other queue organizations
|
||||
# (e.g. stack or priority queue).
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
import ntpath
|
||||
import string
|
||||
|
||||
errors = 0
|
||||
|
||||
def tester(fn, wantResult):
|
||||
fn = string.replace(fn, "\\", "\\\\")
|
||||
gotResult = eval(fn)
|
||||
if wantResult != gotResult:
|
||||
print "error!"
|
||||
print "evaluated: " + str(fn)
|
||||
print "should be: " + str(wantResult)
|
||||
print " returned: " + str(gotResult)
|
||||
print ""
|
||||
global errors
|
||||
errors = errors + 1
|
||||
|
||||
tester('ntpath.splitdrive("c:\\foo\\bar")', ('c:', '\\foo\\bar'))
|
||||
tester('ntpath.splitdrive("\\\\conky\\mountpoint\\foo\\bar")', ('\\\\conky\\mountpoint', '\\foo\\bar'))
|
||||
tester('ntpath.splitdrive("c:/foo/bar")', ('c:', '/foo/bar'))
|
||||
tester('ntpath.splitdrive("//conky/mountpoint/foo/bar")', ('//conky/mountpoint', '/foo/bar'))
|
||||
|
||||
tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
|
||||
tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")', ('\\\\conky\\mountpoint\\foo', 'bar'))
|
||||
|
||||
tester('ntpath.split("c:\\")', ('c:\\', ''))
|
||||
tester('ntpath.split("\\\\conky\\mountpoint\\")', ('\\\\conky\\mountpoint\\', ''))
|
||||
|
||||
tester('ntpath.split("c:/")', ('c:/', ''))
|
||||
tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint/', ''))
|
||||
|
||||
tester('ntpath.isabs("c:\\")', 1)
|
||||
tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1)
|
||||
tester('ntpath.isabs("\\foo")', 1)
|
||||
tester('ntpath.isabs("\\foo\\bar")', 1)
|
||||
|
||||
if errors:
|
||||
print str(errors) + " errors."
|
||||
else:
|
||||
print "No errors. Thank your lucky stars."
|
||||
|
Loading…
Reference in New Issue