def __init__(self): self.adDictionaryFileNames = redis_wrap.get_hash('adDictionaryFileNames') self.adDictionaryFileTotalFrames = redis_wrap.get_hash('adDictionaryFileTotalFrames') self.currFilePlaybackFrameNum = 0 self.currFilePlaybackCurrTimeInSeconds = 0 self.currFilePlaybackTotalTimeInSeconds = 0 self.saveTrimAdsMpegReader = None return
def validate_token(token): """ Validates the token stored in a cookie """ user_id = get_redis().get("auth.%s" % token) user = get_hash("users.%s" % user_id) if not len(user): return False return user['auth'] == token
def vimtips(msg=None, debug=False): '''显示一条 vim 使用技巧''' try: existing_tips = get_hash('vimtips') _len = len(existing_tips) if _len > 0: _index = randint(0, _len - 1) _k = existing_tips.keys()[_index] _v = existing_tips[_k] tip = { 'Content': _k, 'Comment': _v } else: tip = requests.get('http://vim-tips.com/random_tips/json').json() existing_tips.update({ tip['Content']: tip['Comment'] }) collect_tip.delay() except Exception as e: print e return '哦,不小心玩坏了……' result = '%s\n%s' % (tip['Content'], tip['Comment'], ) if debug: result = '%s\n%s' % (result, ('debug: 当前有 %d 条 vimtips' % _len), ) return result
def test_hash(self): villains = get_hash(u'villains') try: self.assertNotIn(u'riddler', villains) except AttributeError: self.assertTrue(u'riddler' not in villains) villains[u'riddler'] = 'Edward Nigma' villains[u'Magneto'] = 'Max Eisenhardt' try: self.assertIn(u'riddler', villains) except AttributeError: self.assertTrue(u'riddler' in villains) self.assertEqual(villains.get(u'riddler'), u'Edward Nigma') self.assertEqual(len(villains.keys()), 2) for expected,actual_key in zip([u'Magneto',u'riddler'], sorted(villains.keys())): self.assertEqual(expected,actual_key) for expected,actual_value in zip([u'Edward Nigma',u'Max Eisenhardt'], sorted(villains.values())): self.assertEqual(expected,actual_value) villains.update({'Green Goblin':'Norman Osborn','riddler':'E. Nigma'}) self.assertEqual(villains['Green Goblin'],'Norman Osborn') self.assertEqual(villains['riddler'],'E. Nigma') del villains[u'riddler'] self.assertEqual(len(villains.keys()), 2) try: self.assertNotIn(u'riddler', villains) except AttributeError: self.assertTrue(u'riddler' not in villains)
def test_hash(): villains = get_hash('villains') assert 'riddler' not in villains villains['riddler'] = 'Edward Nigma' assert 'riddler' in villains assert villains.get('riddler') == 'Edward Nigma' assert len(villains.keys()) == 1 assert villains.values() == ['Edward Nigma'] assert list(villains) == ['riddler'] assert villains.items() == [('riddler', 'Edward Nigma')] del villains['riddler'] assert len(villains.keys()) == 0 assert 'riddler' not in villains villains['drZero'] = '' assert villains['drZero'] == '' assert villains.pop('drZero') == '' assert 'drZero' not in villains assert raises(lambda: villains.pop('Mysterio'), KeyError) assert villains.pop('Mysterio', 'Quentin Beck') == 'Quentin Beck' villains.update({'drEvil': 'Douglas Powers'}) villains.update([('joker', 'Jack'), ('penguin', 'Oswald Chesterfield Cobblepot')]) assert set(villains.items()) == set([('drEvil', 'Douglas Powers'), ('joker', 'Jack'), ('penguin', 'Oswald Chesterfield Cobblepot')]) other_villains = get_hash('minions') other_villains.update(lizard='Curt Connors', rhino='Aleksei Sytsevich') villains.update(other_villains) assert set(villains.items()) == set([('drEvil', 'Douglas Powers'), ('joker', 'Jack'), ('penguin', 'Oswald Chesterfield Cobblepot'), ('lizard', 'Curt Connors'), ('rhino', 'Aleksei Sytsevich')]) print(sys._getframe(0).f_code.co_name, 'ok.')
def get_updates_from_list(feed): """ Retrieve updates from their ids """ newfeed = [] for item in feed: update = get_hash('updates.%s' % item) if len(update): newfeed.append(update) return newfeed
def test_hash(): villains = get_hash('villains') assert 'riddler' not in villains villains['riddler'] = 'Edward Nigma' assert 'riddler' in villains assert villains.get('riddler') == 'Edward Nigma' assert len(villains.keys()) == 1 assert villains.values() == ['Edward Nigma'] assert list(villains) == ['riddler'] assert villains.items() == [('riddler', 'Edward Nigma')] del villains['riddler'] assert len(villains.keys()) == 0 assert 'riddler' not in villains villains['drZero'] = '' assert villains['drZero'] == '' assert villains.pop('drZero') == '' assert 'drZero' not in villains assert raises(lambda: villains.pop('Mysterio'), KeyError) assert villains.pop('Mysterio', 'Quentin Beck') == 'Quentin Beck' villains.update({'drEvil':'Douglas Powers'}) villains.update([('joker','Jack'),('penguin','Oswald Chesterfield Cobblepot')]) assert set(villains.items()) == set([ ('drEvil','Douglas Powers'), ('joker','Jack'), ('penguin','Oswald Chesterfield Cobblepot')]) other_villains = get_hash('minions') other_villains.update(lizard='Curt Connors', rhino='Aleksei Sytsevich') villains.update(other_villains) assert set(villains.items()) == set([ ('drEvil','Douglas Powers'), ('joker','Jack'), ('penguin','Oswald Chesterfield Cobblepot'), ('lizard','Curt Connors'), ('rhino', 'Aleksei Sytsevich')]) print sys._getframe(0).f_code.co_name, 'ok.'
def check_password(username, password, password_field = 'password'): """ password_field is the name of the field in 'create_user' that represents the password. It is advisable to store and check against a hash of the password rather than the password itself. """ user = get_hash('users.%s' % get_numeric_user_id(username)) if not user: return False return user[password_field] == password
def test_hash(): villains = get_hash('villains') assert 'riddler' not in villains villains['riddler'] = 'Edward Nigma' assert 'riddler' in villains assert villains.get('riddler') == 'Edward Nigma' assert len(villains.keys()) == 1 assert villains.values() == ['Edward Nigma'] del villains['riddler'] assert len(villains.keys()) == 0 assert 'riddler' not in villains
def __init__(self): self.redisdb = redis_wrap.get_hash("dscan_categories") self.redis_sizes = redis_wrap.get_hash("dscan_sizes") self.redis_systems = redis_wrap.get_hash("dscan_systems") self.db = sqlite3.connect("sqlite-latest.sqlite") c = self.db.cursor() for row in c.execute( "select distinct invTypes.typeName, invGroups.groupName, invCategories.categoryName from invTypes, invGroups, invCategories WHERE invGroups.groupID=invTypes.groupID AND invCategories.categoryID=invGroups.categoryID" ): try: self.redisdb[row[0]] = json.dumps((row[1] or None, row[2] or None)) except: print "could not load row for %s" % row[0] for row in c.execute( "select invTypes.typeName, dgmTypeAttributes.valueFloat from invTypes, dgmTypeAttributes where dgmTypeAttributes.typeID=invTypes.typeID and dgmTypeAttributes.attributeID=1547" ): self.redis_sizes[row[0]] = int(row[1]) for row in c.execute( "select mapRegions.regionName, mapSolarSystems.solarSystemName from mapRegions, mapSolarSystems where mapRegions.regionID=mapSolarSystems.regionID" ): self.redis_systems[row[1]] = row[0] c.close() print "Database loaded"
def get(key, default=None): ''' Get raw config from redis with a prefix ''' list_keys = ('admins', ) hash_keys = ('conversations', ) string_keys = ('owner', 'last_update_id', ) real_key = '%s:%s' % (str(__name__), key, ) if key in list_keys: return get_list(real_key) elif key in hash_keys: return get_hash(real_key) elif key in string_keys: r = redis.get(real_key) if r is None: r = default return r
def create_user(id_attr, **kwargs): """ Creates a new user with attributes specified as keyword arguments 'id_attr' is the name of the attribute that will uniquely identify the user. """ get_redis().incr("users.counter") ctr = get_redis().get("users.counter") user = get_hash("users." + ctr) for key in kwargs: user[key] = kwargs[key] if id_attr not in kwargs: raise KeyError, "You did not specify any key '%s' in the call to this function." % id_attr else: get_redis().set("users.id.%s" % kwargs[id_attr], ctr) user['id'] = kwargs[id_attr]
def new_update(username, **update): """Creates a new update object and pushes the update to the feeds of the user's friends and followers.""" user = get_numeric_user_id(username) get_redis().incr("updates.counter") ctr = get_redis().get("updates.counter") upd = get_hash("updates." + ctr) upd['user'] = user for key in update: upd[key] = update[key] friends = get_redis().sunion("users.%s.friends" % user, "users.%s.followers" % user) for friend in friends: get_redis().lpush("users.%s.feed" % friend, ctr) get_redis().ltrim("users.%s.feed" % friend, 0, resn_settings['Feed Size']) get_redis().lpush("users.%s.updates" % user, ctr) #This is useful for maintaining a list of updates by a particular user
def addvimtip(msg=None, debug=False): '''(管理员)添加一条 vim 使用技巧''' usage = '命令格式:\n/addvimtip [/forceadd]\n内容\n解释' if msg.text is None: return usage command, options, words = extract_texts(msg.text) if not words: return usage force_add = '/forceadd' in options # We do not want words here, we want lines parts = [i.strip() for i in msg.text.strip().split('\n')] if len(parts) < 3 or not all([bool(i) for i in parts]): return usage content, comment = parts[1], parts[2] tips = get_hash('vimtips') if content in tips: if not force_add: return '这条 tip 已经存在了,希望覆盖的话可以使用 /forceadd 选项' tips.update({ content: comment }) return u'添加了一条 vimtip:\n%s\n%s' % (content, comment, )
def collect_tip(): tip = requests.get('http://vim-tips.com/random_tips/json').json() get_hash('vimtips').update({ tip['Content']: tip['Comment'] })
password = request.form["password"] next_page = request.form["next_page"] if ldaptools.check_credentials(username, password): user = ldaptools.getuser(username) login_user(user) flash("Logged in as %s" % username, "success") if next_page and next_page!="None": return redirect(next_page) else: return redirect("/") else: flash("Invalid Credentials. ", "danger") return redirect("/login") login_manager.login_view = "/login" recoverymap = redis_wrap.get_hash("recovery") @app.route("/forgot_password", methods=["POST", "GET"]) def forgot_password(): if request.method=="GET": return render_template("forgot_password.html") username = request.form["username"] email = request.form["email"] try: user = ldaptools.getuser(username) assert(user) assert(email == user.email[0]) token = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for x in range(24)) url = request.host_url+"recovery/"+token recoverymap[token] = username emailtools.render_email(email, "Password Recovery", "forgot_password.txt", url=url, config=app.config)
def get_hash(name, system=REDIS_NAME): return redis_wrap.get_hash('{}:{}'.format(PREFIX, name), system=system)
def get_task_hash(queue_name): """ 得到 一个 task_md5 -> task 的字典对象 """ return get_hash('ztq:hash:task:' + queue_name)
def _getter(self): redis_key = gen_property_key(self.__class__, self.idx, key) return get_hash(redis_key)
import redis_wrap import string, random import hashlib from string import zfill from dscan import DScan app = Flask(__name__) # Load configuration with open("config.json") as fh: config = json.loads(fh.read()) assert config app.config.update(config) dscan = DScan() store = redis_wrap.get_hash("dscan") @app.route("/", methods=["GET", "POST"]) def dscanmake(): if request.method == "GET": return render_template("dscan.html") if request.method == "POST": data = dscan.parseDscan(request.form["dscandata"]) token = "".join( random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for x in range(24) ) while token in store: token = "".join( random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for x in range(24) )
def get_task_hash(queue_name, system='default'): """ 得到 一个 task_md5 -> task 的字典对象 """ return get_hash('ztq:hash:task:' + queue_name, system=system)
def get_user(user): """ Returns an editable dictionary like wrapper based on the attribute set in 'id_attr' """ return get_hash('users.' + get_numeric_user_id(user))
import requests from requests.auth import HTTPBasicAuth from flask import request, jsonify, redirect, flash, session from flask.ext.login import LoginManager, login_user, logout_user, login_required, current_user import eveapi import redis_wrap from main import load_user from models import * import json mod = Blueprint('login', __name__, template_folder='templates') api = eveapi.EVEAPIConnection() users = redis_wrap.get_hash("timerboard_net_users") @mod.route("/login") def redirect_route(): config = app.myconfig print app.config req = requests.Request("GET", config["CCP_SSO_DOMAIN"] + config["CCP_SSO_AUTHORIZE"], params={ "response_type": "code", "redirect_uri": config["CCP_SSO_CALLBACK"], "client_id": config["CCP_SSO_CLIENTID"], "scope": "", "state": "stategoeshere"
import redis_wrap # redis_url = 'redis://*****:*****@jack.redistogo.com:9604/' redis_url = 'redis://127.0.0.1:6379/' redis_wrap.from_url(redis_url) redis_conn = redis_wrap.get_redis() left = redis_wrap.get_list('left') right = redis_wrap.get_list('right') color_mapping = redis_wrap.get_hash('color-mapping') vote_count = redis_wrap.get_hash('vote-count') state = redis_wrap.get_hash('state') # assorted vars such as current-position status_lock = redis_conn.lock('status-lock')
def get_user_by_id(user_id): """Returns an editable dictionary like wrapper of a user by numeric id""" return get_hash('users.' + user_id)
from flask import Flask, render_template, send_from_directory, url_for, redirect from flask.ext.assets import Environment, Bundle from flask.ext.login import LoginManager from assets import assets from flask.ext.sqlalchemy import SQLAlchemy from models import * import redis_wrap import os, json login_manager = LoginManager() redis_wrap.setup_system("main", host="timerboard-redis", port=6379) users = redis_wrap.get_hash("timerboard_net_users", "main") @login_manager.user_loader def load_user(user): if user in users: return User(json.loads(users[user])) else: flash("User not found.", "warning") def create_app(): # We could pass a config object here app = Flask(__name__) app.debug = os.environ.get('TIMERBOARD_PRODUCTION', True) app.config.from_envvar("TIMERBOARD_SETTINGS") with open(os.environ["TIMERBOARD_SETTINGS"]) as fh: app.myconfig = json.loads(fh.read()) app.config["SQLALCHEMY_DATABASE_URI"] = app.myconfig["SQLALCHEMY_DATABASE_URI"]
def get_error_hash(queue_name, system='default'): """ json格式和work_log相同 """ error_queue = 'ztq:hash:error:' + queue_name return get_hash(error_queue, system=system)
def __init__(self): self.adDictionaryFileNames = redis_wrap.get_hash('adDictionaryFileNames') self.adDictionaryFileTotalFrames = redis_wrap.get_hash('adDictionaryFileTotalFrames') return
#!/usr/bin/env python import redis from redis_wrap import setup_system, get_hash, get_set, get_list import Config clients = {'default': redis.Redis(Config.REDIS_HOST, Config.REDIS_PORT)} setup_system('default', Config.REDIS_HOST, Config.REDIS_PORT) def Redis(k = 'default'): return clients[k] if __name__ == '__main__': h = get_hash('test') print set(get_set('haha'))