Skip to content

Commit

Permalink
Remove eval and fix more PEP8 problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
scattm committed Jul 9, 2016
1 parent a353452 commit 9c3bad2
Showing 1 changed file with 43 additions and 16 deletions.
59 changes: 43 additions & 16 deletions flask_redislite.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
and RQ.
"""
import atexit
import json
import signal
from flask import current_app
from multiprocessing import Process
from os import remove, path
from os import remove
from redislite import Redis, StrictRedis
from redis_collections import Dict as RC_Dict, List as RC_List
from rq import Queue, Worker
Expand All @@ -28,6 +29,7 @@
def worker_wrapper(worker_instance, pid_path):
"""
A wrapper to start RQ worker as a new process.
:param worker_instance: RQ's worker instance
:param pid_path: A file to check if the worker
is running or not
Expand All @@ -51,33 +53,58 @@ def exit_handler(*args):


class Collection(object):
"""
Wrapper class for redis-collections
"""
"""Wrapper class for redis-collections."""

def __init__(self, redis):
self._redis = redis

def dict(self, key=None):
"""
Create a redis-collections dict class.
:type key: str
:param key: The key used for redis.
:return: None
"""
return RC_Dict(key=key, redis=self._redis)

def get_dict(self, key=None):
"""
Get a redis-collections dict class.
:type key: str
:param key: The key used for redis.
:return: None
"""
return RC_Dict(key=key, redis=self._redis)

def list(self, key=None):
"""
Create a redis-collections list class.
:type key: str
:param key: The key used for redis.
:return: None
"""
return RC_List(key=key, redis=self._redis)

def get_list(self, key=None):
"""
Get a redis-collections list class.
:type key: str
:param key: The key used for redis.
:return: None
"""
return RC_List(key=key, redis=self._redis)


class FlaskRedis(object):
"""
Main class for Flask-Redislite
"""
"""Main class for Flask-Redislite."""

def __init__(self, app=None, **kwargs):
"""
:param app: Flask's app
:param kwargs: strict, config_prefix, collections, rq, rq_queues
"""
Expand All @@ -97,28 +124,31 @@ def __init__(self, app=None, **kwargs):

self._rdb = None

def connect(self):
def _connect(self):
try:
settings_file = open("%s.settings" % current_app.config[
"{}_PATH".format(self.config_prefix)
], 'r')
self._rdb = self.redis_class(
current_app.config["{}_PATH".format(self.config_prefix)],
serverconfig=eval(settings_file.read())
serverconfig=json.load(settings_file)
)
settings_file.close()
except IOError:
self._rdb = self.redis_class(
current_app.config["{}_PATH".format(self.config_prefix)]
)

@property
def connection(self):
"""Return Redislite instance. Class depend on initialization"""
if self._rdb is None:
self.connect()
self._connect()
return self._rdb

@property
def collection(self):
"""Return the redis-collection instance."""
if not self.include_collections:
return None
ctx = stack.top
Expand All @@ -129,6 +159,7 @@ def collection(self):

@property
def queue(self):
"""The queue property. Return rq.Queue instance."""
if not self.include_rq:
return None
ctx = stack.top
Expand All @@ -138,9 +169,7 @@ def queue(self):
return ctx.redislite_queue

def start_worker(self):
"""
Trigger new process as a RQ worker.
"""
"""Trigger new process as a RQ worker."""
if not self.include_rq:
return None

Expand Down Expand Up @@ -174,7 +203,5 @@ def start_worker(self):
return self.worker_process.pid

def commit(self):
"""
Saving data from memory to disk.
"""
"""Saving data from memory to disk."""
self.connection.bgsave()

0 comments on commit 9c3bad2

Please sign in to comment.