Permalink
Browse files

Fix pep8 violations, logging level cleaning, and lots of ugliness

Changed the repr for some objects which broke a couple tests. I can't
imagine the repr really needs the fully qualified namespace for the
classes... it scares me to think why that'd be necessary.
  • Loading branch information...
1 parent 65843ed commit 1a42d58d8c93bf73b8bc3f1ba8a74357fc22450f @schmichael committed Mar 21, 2012
Showing with 75 additions and 55 deletions.
  1. +73 −53 supervisor/datatypes.py
  2. +2 −2 supervisor/tests/test_datatypes.py
@@ -1,8 +1,12 @@
+import grp
import os
-import re
+import pwd
import sys
-import socket
+import signal
import shlex
+import socket
+import urlparse
+
from supervisor.loggers import getLevelNumByDescription
# I dont know why we bother, this doesn't run on Windows, but just
@@ -14,10 +18,12 @@
here = None
+
def set_here(v):
global here
here = v
+
def integer(value):
try:
return int(value)
@@ -26,9 +32,11 @@ def integer(value):
except OverflowError:
return long(value)
+
TRUTHY_STRINGS = ('yes', 'true', 'on', '1')
FALSY_STRINGS = ('no', 'false', 'off', '0')
+
def boolean(s):
"""Convert a string value to a boolean value."""
ss = str(s).lower()
@@ -39,6 +47,7 @@ def boolean(s):
else:
raise ValueError("not a valid boolean value: " + repr(s))
+
def list_of_strings(arg):
if not arg:
return []
@@ -47,6 +56,7 @@ def list_of_strings(arg):
except:
raise ValueError("not a valid list of strings: " + repr(arg))
+
def list_of_ints(arg):
if not arg:
return []
@@ -56,6 +66,7 @@ def list_of_ints(arg):
except:
raise ValueError("not a valid list of ints: " + repr(arg))
+
def list_of_exitcodes(arg):
try:
vals = list_of_ints(arg)
@@ -66,6 +77,7 @@ def list_of_exitcodes(arg):
except:
raise ValueError("not a valid list of exit codes: " + repr(arg))
+
def dict_of_key_value_pairs(arg):
""" parse KEY=val,KEY2=val2 into {'KEY':'val', 'KEY2':'val2'}
Quotes can be used to allow commas in the value
@@ -81,17 +93,19 @@ def dict_of_key_value_pairs(arg):
while i in range(0, tokens_len):
k_eq_v = tokens[i:i+3]
if len(k_eq_v) != 3:
- raise ValueError, "Unexpected end of key/value pairs"
+ raise ValueError("Unexpected end of key/value pairs")
D[k_eq_v[0]] = k_eq_v[2].strip('\'"')
i += 4
return D
-class Automatic:
- pass
+
+Automatic = object()
+
LOGFILE_NONES = ('none', 'off', None)
LOGFILE_AUTOS = (Automatic, 'auto')
+
def logfile_name(val):
if hasattr(val, 'lower'):
coerced = val.lower()
@@ -105,7 +119,8 @@ def logfile_name(val):
else:
return existing_dirpath(val)
-class RangeCheckedConversion:
+
+class RangeCheckedConversion(object):
"""Conversion helper that range checks another conversion."""
def __init__(self, conversion, min=None, max=None):
@@ -116,14 +131,13 @@ def __init__(self, conversion, min=None, max=None):
def __call__(self, value):
v = self._conversion(value)
if self._min is not None and v < self._min:
- raise ValueError("%s is below lower bound (%s)"
- % (`v`, `self._min`))
+ raise ValueError("%r is below lower bound (%r)" % (v, self._min))
if self._max is not None and v > self._max:
- raise ValueError("%s is above upper bound (%s)"
- % (`v`, `self._max`))
+ raise ValueError("%r is above upper bound (%r)" % (v, self._max))
return v
-port_number = RangeCheckedConversion(integer, min=1, max=0xffff).__call__
+port_number = RangeCheckedConversion(integer, min=1, max=0xffff)
+
def inet_address(s):
# returns (host, port) tuple
@@ -144,7 +158,8 @@ def inet_address(s):
host = DEFAULT_HOST
return host, port
-class SocketAddress:
+
+class SocketAddress(object):
def __init__(self, s):
# returns (family, address) tuple
if "/" in s or s.find(os.sep) >= 0 or ":" not in s:
@@ -154,16 +169,15 @@ def __init__(self, s):
self.family = socket.AF_INET
self.address = inet_address(s)
-class SocketConfig:
+
+class SocketConfig(object):
""" Abstract base class which provides a uniform abstraction
for TCP vs Unix sockets """
url = '' # socket url
addr = None #socket addr
def __repr__(self):
- return '<%s at %s for %s>' % (self.__class__,
- id(self),
- self.url)
+ return '<%s at %s for %s>' % (type(self).__name__, id(self), self.url)
def __str__(self):
return str(self.url)
@@ -186,6 +200,7 @@ def addr(self): # pragma: no cover
def create_and_bind(self): # pragma: no cover
raise NotImplementedError
+
class InetStreamSocketConfig(SocketConfig):
""" TCP socket config helper """
@@ -206,6 +221,7 @@ def create_and_bind(self):
sock.bind(self.addr())
return sock
+
class UnixStreamSocketConfig(SocketConfig):
""" Unix domain socket config helper """
@@ -281,85 +297,89 @@ def colon_separated_user_group(arg):
return (uid, gid)
return result
except:
- raise ValueError, 'Invalid user.group definition %s' % arg
+ raise ValueError('Invalid user.group definition %s' % arg)
+
def octal_type(arg):
try:
return int(arg, 8)
except TypeError:
raise ValueError('%s is not convertable to an octal type' % arg)
+
def name_to_uid(name):
if name is None:
return None
- import pwd
try:
- uid = int(name)
+ uid = int(name)
except ValueError:
- try:
- pwrec = pwd.getpwnam(name)
- except KeyError:
+ try:
+ pwrec = pwd.getpwnam(name)
+ except KeyError:
return None
- uid = pwrec[2]
+ uid = pwrec[2]
else:
- try:
- pwrec = pwd.getpwuid(uid)
- except KeyError:
+ try:
+ pwrec = pwd.getpwuid(uid)
+ except KeyError:
return None
return uid
+
def name_to_gid(name):
- import grp
try:
- gid = int(name)
+ gid = int(name)
except ValueError:
- try:
- pwrec = grp.getgrnam(name)
- except KeyError:
+ try:
+ pwrec = grp.getgrnam(name)
+ except KeyError:
return None
- gid = pwrec[2]
+ gid = pwrec[2]
else:
- try:
- pwrec = grp.getgrgid(gid)
- except KeyError:
+ try:
+ pwrec = grp.getgrgid(gid)
+ except KeyError:
return None
return gid
+
def gid_for_uid(uid):
- import pwd
pwrec = pwd.getpwuid(uid)
return pwrec[3]
+
def existing_directory(v):
- import os
- nv = v % {'here':here}
+ nv = v % {'here': here}
nv = os.path.expanduser(nv)
if os.path.isdir(nv):
return nv
raise ValueError('%s is not an existing directory' % v)
+
def existing_dirpath(v):
import os
- nv = v % {'here':here}
+ nv = v % {'here': here}
nv = os.path.expanduser(nv)
dir = os.path.dirname(nv)
if not dir:
# relative pathname with no directory component
return nv
if os.path.isdir(dir):
return nv
- raise ValueError, ('The directory named as part of the path %s '
+ raise ValueError('The directory named as part of the path %s '
'does not exist.' % v)
+
def logging_level(value):
s = str(value).lower()
- level = getLevelNumByDescription(value)
+ level = getLevelNumByDescription(s)
if level is None:
raise ValueError('bad logging level name %r' % value)
return level
-class SuffixMultiplier:
+
+class SuffixMultiplier(object):
# d is a dictionary of suffixes to integer multipliers. If no suffixes
# match, default is the multiplier. Matches are case insensitive. Return
# values are in the fundamental unit.
@@ -383,10 +403,10 @@ def __call__(self, v):
byte_size = SuffixMultiplier({'kb': 1024,
'mb': 1024*1024,
- 'gb': 1024*1024*1024L,})
+ 'gb': 1024*1024*1024, })
+
def url(value):
- import urlparse
# earlier Python 2.6 urlparse (2.6.4 and under) can't parse unix:// URLs,
# later ones can but we need to straddle
uri = value.replace('unix://', 'http://', 1).strip()
@@ -395,8 +415,8 @@ def url(value):
return value
raise ValueError("value %s is not a URL" % value)
+
def signal_number(value):
- import signal
result = None
try:
result = int(value)
@@ -408,18 +428,17 @@ def signal_number(value):
except (ValueError, TypeError):
raise ValueError('value %s is not a signal name/number' % value)
-class RestartWhenExitUnexpected:
- pass
-class RestartUnconditionally:
- pass
+RestartWhenExitUnexpected = object()
+RestartUnconditionally = object()
+
def auto_restart(value):
value = str(value.lower())
- computed_value = value
- if value in ('true', '1', 'on', 'yes'):
+ computed_value = value
+ if value in TRUTHY_STRINGS:
computed_value = RestartUnconditionally
- elif value in ('false', '0', 'off', 'no'):
+ elif value in FALSY_STRINGS:
computed_value = False
elif value == 'unexpected':
computed_value = RestartWhenExitUnexpected
@@ -428,8 +447,9 @@ def auto_restart(value):
raise ValueError("invalid 'autorestart' value %r" % value)
return computed_value
+
def profile_options(value):
- options = [x.lower() for x in list_of_strings(value) ]
+ options = [x.lower() for x in list_of_strings(value)]
sort_options = []
callers = False
for thing in options:
@@ -217,7 +217,7 @@ def test_repr(self):
conf = self._makeOne('127.0.0.1', 8675)
s = repr(conf)
self.assertTrue(s.startswith(
- '<supervisor.datatypes.InetStreamSocketConfig at'), s)
+ '<InetStreamSocketConfig at'), s)
self.assertTrue(s.endswith('for tcp://127.0.0.1:8675>'), s)
def test_addr(self):
@@ -275,7 +275,7 @@ def test_repr(self):
conf = self._makeOne('/tmp/foo.sock')
s = repr(conf)
self.assertTrue(s.startswith(
- '<supervisor.datatypes.UnixStreamSocketConfig at'), s)
+ '<UnixStreamSocketConfig at'), s)
self.assertTrue(s.endswith('for unix:///tmp/foo.sock>'), s)
def test_get_addr(self):

0 comments on commit 1a42d58

Please sign in to comment.