Skip to content

Commit

Permalink
pylint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwight Hubbard committed May 1, 2015
1 parent 0d088b8 commit 0767340
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
40 changes: 23 additions & 17 deletions redislite/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def _cleanup(self):
)
# noinspection PyUnresolvedReferences
logger.debug(
'Shutting down redis server with pid of %d' % self.pid)
'Shutting down redis server with pid of %d', self.pid
)
self.shutdown()
self.socket_file = None

Expand Down Expand Up @@ -112,6 +113,10 @@ def _cleanup(self):
self.pidfile = None

def _connection_count(self):
"""
Return the number of active connections to the redis server.
:return:
"""
if not self._is_redis_running(): # pragma: no cover
return 0
active_connections = 0
Expand Down Expand Up @@ -197,21 +202,21 @@ def _is_redis_running(self):
return False

if os.path.exists(self.settingregistryfile):
with open(self.settingregistryfile) as fh:
settings = json.load(fh)
with open(self.settingregistryfile) as file_handle:
settings = json.load(file_handle)

if not os.path.exists(settings['pidfile']):
return False

with open(settings['pidfile']) as fh:
pid = fh.read().strip() # NOQA
with open(settings['pidfile']) as file_handle:
pid = file_handle.read().strip() # NOQA
pid = int(pid)
if pid: # pragma: no cover
try:
p = psutil.Process(pid)
process = psutil.Process(pid)
except psutil.NoSuchProcess:
return False
if not p.is_running():
if not process.is_running():
return False
else: # pragma: no cover
return False
Expand Down Expand Up @@ -248,8 +253,8 @@ def _load_setting_registry(self):
with open(pidfile) as fh:
pid_number = int(fh.read())
if pid_number:
p = psutil.Process(pid_number)
if not p.is_running(): # pragma: no cover
process = psutil.Process(pid_number)
if not process.is_running(): # pragma: no cover
logger.warn('Loaded registry for non-existant redis-server')
return
else: # pragma: no cover
Expand All @@ -271,8 +276,7 @@ def __init__(self, *args, **kwargs):
# If the user is specifying settings we can't configure just pass the
# request to the redis.Redis module
if 'host' in kwargs.keys() or 'port' in kwargs.keys():
# noinspection PyArgumentList,PyPep8
return super(RedisMixin, self).__init__(*args, **kwargs) # pragma: no cover
super(RedisMixin, self).__init__(*args, **kwargs) # pragma: no cover

self.socket_file = kwargs.get('unix_socket_path', None)
if self.socket_file and self.socket_file == os.path.basename(self.socket_file):
Expand Down Expand Up @@ -356,14 +360,14 @@ def pid(self):
running.
"""
if self.pidfile and os.path.exists(self.pidfile):
with open(self.pidfile) as fh:
pid = int(fh.read().strip())
with open(self.pidfile) as file_handle:
pid = int(file_handle.read().strip())
if pid: # pragma: no cover
try:
p = psutil.Process(pid)
process = psutil.Process(pid)
except psutil.NoSuchProcess:
return 0
if not p.is_running():
if not process.is_running():
return 0
else: # pragma: no cover
return 0
Expand Down Expand Up @@ -410,7 +414,8 @@ class Redis(RedisMixin, redis.Redis):
argument is not None, the embedded redis server will not be used.
Defaults to None.
serverconfig(dict): A dictionary of additional redis-server configuration settings. All keys and values must be str.
serverconfig(dict): A dictionary of additional redis-server
configuration settings. All keys and values must be str.
Supported keys are:
activerehashing,
aof_rewrite_incremental_fsync,
Expand Down Expand Up @@ -514,7 +519,8 @@ class that uses an embedded redis-server by default.
not None, the embedded redis server will not be used. Defaults to
None.
serverconfig(dict): A dictionary of additional redis-server configuration settings. All keys and values must be str.
serverconfig(dict): A dictionary of additional redis-server
configuration settings. All keys and values must be str.
Supported keys are:
activerehashing,
aof_rewrite_incremental_fsync,
Expand Down
25 changes: 11 additions & 14 deletions redislite/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@


import logging


logger = logging.getLogger(__name__)

template = Template("""
# Redis configuration file example

REDIS_SERVER_TEMPLATE = Template("""# Redis configuration file example
# Note on units: when memory size is needed, it is possible to specify
# it in the usual form of 1k 5GB 4M and so forth:
Expand Down Expand Up @@ -810,7 +812,9 @@
# big latency spikes.
aof-rewrite-incremental-fsync {{ aof_rewrite_incremental_fsync }}
""")
default_settings = {


DEFAULT_REDIS_SETTINGS = {
'daemonize': 'yes',
'pidfile': '/var/run/redislite/redis.pid',
'tcp_backlog': '511',
Expand Down Expand Up @@ -857,27 +861,20 @@
}


def settings(*args, **kwargs):
def settings(**kwargs):
"""
Return a config settings based on the defaults and the arguments passed
:return:
"""
global default_settings

new_settings = default_settings
new_settings = dict(DEFAULT_REDIS_SETTINGS)
new_settings.update(kwargs)

return new_settings


def config(*args, **kwargs):
def config(**kwargs):
"""
Generate a redis configuration file based on the passed arguments
:return:
"""
global default_settings

settings = dict(default_settings)
settings.update(kwargs)

return template.render(settings)
return REDIS_SERVER_TEMPLATE.render(**settings(**kwargs))
4 changes: 3 additions & 1 deletion tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ def test_configuration_modify_defaults(self):
self.assertIn('\ndaemonize no', result)

# ensure the global defaults are not modified
self.assertEquals(redislite.configuration.default_settings["daemonize"], "yes")
self.assertEquals(
redislite.configuration.DEFAULT_REDIS_SETTINGS["daemonize"], "yes"
)


def test_configuration_settings(self):
Expand Down

0 comments on commit 0767340

Please sign in to comment.