本文整理汇总了Python中shove.Shove.sync方法的典型用法代码示例。如果您正苦于以下问题:Python Shove.sync方法的具体用法?Python Shove.sync怎么用?Python Shove.sync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类shove.Shove
的用法示例。
在下文中一共展示了Shove.sync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test__cmp__
# 需要导入模块: from shove import Shove [as 别名]
# 或者: from shove.Shove import sync [as 别名]
def test__cmp__(self):
tstore = Shove()
self.store['max'] = 3
tstore['max'] = 3
self.store.sync()
tstore.sync()
self.assertEqual(self.store, tstore)
示例2: test__cmp__
# 需要导入模块: from shove import Shove [as 别名]
# 或者: from shove.Shove import sync [as 别名]
def test__cmp__(self):
from shove import Shove
tstore = Shove()
self.store['max'] = 3
tstore['max'] = 3
self.store.sync()
tstore.sync()
self.assertEqual(self.store, tstore)
示例3: load_facts
# 需要导入模块: from shove import Shove [as 别名]
# 或者: from shove.Shove import sync [as 别名]
def load_facts(config):
import requests
from bs4 import BeautifulSoup
db = Shove(config['dburi'])
db['facts'] = []
url1 = 'http://www.cats.alpha.pl/facts.htm'
raw = requests.get(url1).text
soup = BeautifulSoup(raw).findAll('ul')[1]
for string in soup.stripped_strings:
if string:
db['facts'].append(string)
db.sync()
示例4: load_facts
# 需要导入模块: from shove import Shove [as 别名]
# 或者: from shove.Shove import sync [as 别名]
def load_facts(config):
import requests
import re
db = Shove(config['dburi'])
db['facts'] = []
url1 = 'http://www.cats.alpha.pl/facts.htm'
raw = requests.get(url1).text
filtered = filter(
lambda l: l.startswith('<li>'),
map(lambda l: l.strip(), raw.split('\n')))
stripped = map(lambda l: re.sub('<[^<]+?>', '', l), filtered)
db['facts'].extend(stripped)
db.sync()
示例5: lookup_page_name
# 需要导入模块: from shove import Shove [as 别名]
# 或者: from shove.Shove import sync [as 别名]
def lookup_page_name(page_name, cache_file='file://test', sparql_url='', polite_factor=1):
"""lookup info from cache"""
page_name = page_name_normalize(page_name)
logging.info(cache_file)
logging.info(page_name)
cache = Shove(cache_file)
if page_name in cache:
logging.debug("cache hit")
return cache[page_name]
else:
logging.debug("cache miss")
res = perform_sparql_query(page_name, sparql_url, polite_factor)
cache[page_name] = res
cache.sync()
return res
示例6: setup_models
# 需要导入模块: from shove import Shove [as 别名]
# 或者: from shove.Shove import sync [as 别名]
def setup_models(networks, add_morphogene=True):
''' Convert dict of networks in networkx format to models in ModelContainer format (batch mode) '''
''' This runs out of memory on the laptop. '''
models_dict_name = "models_dictionary.db"
#models_dict = shelve.open(models_dict_name)
models_dict = Shove("file://"+models_dict_name, compress=True)
for localparset, net in enumerate(networks.values()):
print localparset, ":",
#if localparset>=50: break # enable for quick run
mc = dict_to_model(net, add_morphogene)
print len(mc._psc), "parameter sets, shoving."
if not localparset%10:
tend = datetime.now()
print "total execution time:", tend-tstart
models_dict[str(localparset)] = mc
models_dict.sync()
#models_dict.close()
print "shoved", localparset+1, "model containers to", models_dict_name, "."
示例7: TestSvnStore
# 需要导入模块: from shove import Shove [as 别名]
# 或者: from shove.Shove import sync [as 别名]
class TestSvnStore(unittest.TestCase):
svnstring = 'SVN test string here'
def setUp(self):
from shove import Shove
self.store = Shove(self.svnstring, compress=True)
def tearDown(self):
self.store.clear()
self.store.close()
def test__getitem__(self):
self.store['max'] = 3
self.store.sync()
self.assertEqual(self.store['max'], 3)
def test__setitem__(self):
self.store['max'] = 3
self.store.sync()
self.assertEqual(self.store['max'], 3)
def test__delitem__(self):
self.store['max'] = 3
del self.store['max']
self.assertEqual('max' in self.store, False)
def test_get(self):
self.store['max'] = 3
self.store.sync()
self.assertEqual(self.store.get('min'), None)
def test__cmp__(self):
from shove import Shove
tstore = Shove()
self.store['max'] = 3
tstore['max'] = 3
self.store.sync()
tstore.sync()
self.assertEqual(self.store, tstore)
def test__len__(self):
self.store['max'] = 3
self.store['min'] = 6
self.store.sync()
self.assertEqual(len(self.store), 2)
def test_clear(self):
self.store['max'] = 3
self.store['min'] = 6
self.store['pow'] = 7
self.store.sync()
self.store.clear()
self.assertEqual(len(self.store), 0)
def test_items(self):
self.store['max'] = 3
self.store['min'] = 6
self.store['pow'] = 7
self.store.sync()
slist = list(self.store.items())
self.assertEqual(('min', 6) in slist, True)
def test_iteritems(self):
self.store['max'] = 3
self.store['min'] = 6
self.store['pow'] = 7
self.store.sync()
slist = list(self.store.iteritems())
self.assertEqual(('min', 6) in slist, True)
def test_iterkeys(self):
self.store['max'] = 3
self.store['min'] = 6
self.store['pow'] = 7
self.store.sync()
slist = list(self.store.iterkeys())
self.assertEqual('min' in slist, True)
def test_itervalues(self):
self.store['max'] = 3
self.store['min'] = 6
self.store['pow'] = 7
self.store.sync()
slist = list(self.store.itervalues())
self.assertEqual(6 in slist, True)
def test_pop(self):
self.store['max'] = 3
self.store['min'] = 6
self.store.sync()
item = self.store.pop('min')
self.assertEqual(item, 6)
def test_popitem(self):
self.store['max'] = 3
self.store['min'] = 6
self.store['pow'] = 7
self.store.sync()
item = self.store.popitem()
#.........这里部分代码省略.........
示例8: CatFactsREST
# 需要导入模块: from shove import Shove [as 别名]
# 或者: from shove.Shove import sync [as 别名]
class CatFactsREST(object):
def __init__(self, config):
self.config = config
self.apikeys = [s.strip() for s in self.config['apikeys'].split(',')]
dburi = self.config['dburi']
self.db = Shove(dburi)
self.app = Flask(__name__)
self.twilio = TwilioRestClient(
self.config['SID'],
self.config['token'])
if 'numbers' not in self.db:
self.db['numbers'] = []
if 'facts' not in self.db:
print "No catfacts found, run catfacts load"
exit()
self.db.sync()
self.routes = {
"/api/numbers": (self.add_number, {"methods": ['POST']}),
"/api/numbers/<num>": (self.remove_number, {"methods":
['DELETE']}),
"/api/callback": (self.twilio_callback, {"methods": ['GET']}),
"/api/facts": (self.add_facts, {"methods": ['POST']})}
map(
lambda route: self.app.route(
route,
**self.routes[route][1])(self.routes[route][0]),
self.routes)
def add_number(self):
"""
POST: /api/numbers
"""
try:
data = json.loads(request.data)
except:
return json.dumps(dict(
success=False,
message="Invalid data recieved"))
try:
if data['apikey'] not in self.apikeys:
raise Exception
except:
return json.dumps(dict(
succes=False,
message="Unauthorized"))
try:
number = data['number']
if number not in self.db['numbers']:
self.db['numbers'].append(number)
self.db.sync()
self.twilio.sms.messages.create(
to=number,
body="Congrats, you have been signed up for catfacts, \
the Premire cat information service, you will \
receive hourly cat information")
return json.dumps(dict(
success=True,
message="Added {0} to catfacts".format(number)))
else:
return json.dumps(dict(
success=False,
message="{0} is already signed up for catfacts".format(
number)))
except KeyError:
return json.dumps(dict(
success=False,
message="Not Enough paramters"))
def remove_number(self, num):
"""
DELETE: /api/numbers/<number>
"""
if num in self.db:
self.db['numbers'].remove(num)
self.db.sync()
return json.dumps(dict(
success=True,
message="Removed {0} from catfacts".format(num)))
else:
return json.dumps(dict(
success=False,
message="{0} is not signed up for catfacts".format(num)))
def twilio_callback(self):
"""
POST: /api/callback
"""
response = twilio.twiml.Response()
response.sms(choice(self.db['facts']))
return str(response)
def add_facts(self):
"""
POST: /api/facts
"""
try:
data = json.loads(request.body)
#.........这里部分代码省略.........
示例9: TestFileStore
# 需要导入模块: from shove import Shove [as 别名]
# 或者: from shove.Shove import sync [as 别名]
class TestFileStore(unittest.TestCase):
def setUp(self):
self.store = Shove('file://test', compress=True)
def tearDown(self):
self.store.close()
for x in os.listdir('test'): os.remove(os.path.join('test', x))
os.rmdir('test')
def test__getitem__(self):
self.store['max'] = 3
self.store.sync()
self.assertEqual(self.store['max'], 3)
def test__setitem__(self):
self.store['max'] = 3
self.store.sync()
self.assertEqual(self.store['max'], 3)
def test__delitem__(self):
self.store['max'] = 3
del self.store['max']
self.assertEqual('max' in self.store, False)
def test_get(self):
self.store['max'] = 3
self.store.sync()
self.assertEqual(self.store.get('min'), None)
def test__cmp__(self):
tstore = Shove()
self.store['max'] = 3
tstore['max'] = 3
self.store.sync()
tstore.sync()
self.assertEqual(self.store, tstore)
def test__len__(self):
self.store['max'] = 3
self.store['min'] = 6
self.assertEqual(len(self.store), 2)
def test_close(self):
self.store.close()
self.assertEqual(self.store, None)
def test_clear(self):
self.store['max'] = 3
self.store['min'] = 6
self.store['pow'] = 7
self.store.clear()
self.assertEqual(len(self.store), 0)
def test_items(self):
self.store['max'] = 3
self.store['min'] = 6
self.store['pow'] = 7
slist = list(self.store.items())
self.assertEqual(('min', 6) in slist, True)
def test_iteritems(self):
self.store['max'] = 3
self.store['min'] = 6
self.store['pow'] = 7
slist = list(self.store.iteritems())
self.assertEqual(('min', 6) in slist, True)
def test_iterkeys(self):
self.store['max'] = 3
self.store['min'] = 6
self.store['pow'] = 7
slist = list(self.store.iterkeys())
self.assertEqual('min' in slist, True)
def test_itervalues(self):
self.store['max'] = 3
self.store['min'] = 6
self.store['pow'] = 7
slist = list(self.store.itervalues())
self.assertEqual(6 in slist, True)
def test_pop(self):
self.store['max'] = 3
self.store['min'] = 6
item = self.store.pop('min')
self.assertEqual(item, 6)
def test_popitem(self):
self.store['max'] = 3
self.store['min'] = 6
self.store['pow'] = 7
item = self.store.popitem()
self.assertEqual(len(item) + len(self.store), 4)
def test_setdefault(self):
self.store['max'] = 3
self.store['min'] = 6
self.store['powl'] = 7
self.store.setdefault('pow', 8)
#.........这里部分代码省略.........
示例10: TestFtpStore
# 需要导入模块: from shove import Shove [as 别名]
# 或者: from shove.Shove import sync [as 别名]
class TestFtpStore(unittest.TestCase):
ftpstring = "put ftp string here"
def setUp(self):
self.store = Shove(self.ftpstring, compress=True)
def tearDown(self):
self.store.clear()
self.store.close()
def test__getitem__(self):
self.store["max"] = 3
self.store.sync()
self.assertEqual(self.store["max"], 3)
def test__setitem__(self):
self.store["max"] = 3
self.store.sync()
self.assertEqual(self.store["max"], 3)
def test__delitem__(self):
self.store["max"] = 3
del self.store["max"]
self.assertEqual("max" in self.store, False)
def test_get(self):
self.store["max"] = 3
self.store.sync()
self.assertEqual(self.store.get("min"), None)
def test__cmp__(self):
tstore = Shove()
self.store["max"] = 3
tstore["max"] = 3
self.store.sync()
tstore.sync()
self.assertEqual(self.store, tstore)
def test__len__(self):
self.store["max"] = 3
self.store["min"] = 6
self.store.sync()
self.assertEqual(len(self.store), 2)
def test_clear(self):
self.store["max"] = 3
self.store["min"] = 6
self.store["pow"] = 7
self.store.sync()
self.store.clear()
self.assertEqual(len(self.store), 0)
def test_items(self):
self.store["max"] = 3
self.store["min"] = 6
self.store["pow"] = 7
self.store.sync()
slist = list(self.store.items())
self.assertEqual(("min", 6) in slist, True)
def test_iteritems(self):
self.store["max"] = 3
self.store["min"] = 6
self.store["pow"] = 7
self.store.sync()
slist = list(self.store.iteritems())
self.assertEqual(("min", 6) in slist, True)
def test_iterkeys(self):
self.store["max"] = 3
self.store["min"] = 6
self.store["pow"] = 7
self.store.sync()
slist = list(self.store.iterkeys())
self.assertEqual("min" in slist, True)
def test_itervalues(self):
self.store["max"] = 3
self.store["min"] = 6
self.store["pow"] = 7
self.store.sync()
slist = list(self.store.itervalues())
self.assertEqual(6 in slist, True)
def test_pop(self):
self.store["max"] = 3
self.store["min"] = 6
self.store.sync()
item = self.store.pop("min")
self.assertEqual(item, 6)
def test_popitem(self):
self.store["max"] = 3
self.store["min"] = 6
self.store["pow"] = 7
self.store.sync()
item = self.store.popitem()
self.store.sync()
self.assertEqual(len(item) + len(self.store), 4)
#.........这里部分代码省略.........
示例11: DataManager
# 需要导入模块: from shove import Shove [as 别名]
# 或者: from shove.Shove import sync [as 别名]
class DataManager():
def __init__(self, filename, connection_details=VERTICA_CONNECTION):
self.filename = filename
self.store = Shove('file://'+self.filename, 'memory://', optimize=False)
self.connection_details = connection_details
@staticmethod
def get_attribute_name(data_set, attribute):
return '_' + data_set + '_' + attribute
def get_data_set_attribute(self, data_set, key):
attrib_name = self.get_attribute_name(data_set, key)
if self.has_attribute(data_set, key):
return self.store[attrib_name]
else:
return None
self.store.close()
gc.collect()
self.store = Shove('file://'+self.filename, 'memory://', optimize=False)
def set_data_set_attribute(self, data_set, key, value):
attrib_name = self.get_attribute_name(data_set, key)
self.store.update({attrib_name: value})
self.store.sync()
self.store.close()
gc.collect()
self.store = Shove('file://'+self.filename, 'memory://', optimize=False)
def has_attribute(self, data_set, key):
attrib_name = self.get_attribute_name(data_set, key)
if attrib_name in self.store.keys():
return True
else:
return False
def remove_attribute(self, data_set, key):
attrib_name = self.get_attribute_name(data_set, key)
if attrib_name in self.store:
del self.store[attrib_name]
self.store.sync()
def get_data_set_list(self):
data_sets = [key.split('_')[1] for key in self.store.keys() if '_query' in key]
return data_sets
def fetch_from_vertica_to_df(self, data_set, query):
data_set_query = self.get_data_set_attribute(data_set, 'query')
if data_set_query != query:
connection = connect(self.connection_details)
cursor = connection.cursor()
print 'Executing ', data_set, 'Query...'
print query
columns = get_column_names_from_sql_query(query)
cursor.execute(query)
data = []
while True:
rows = cursor.fetchmany(10000)
data.extend([[str(ele) for ele in row] for row in rows])
if len(rows) <= 1:
break
df = MetadataDataFrame(data=data, columns=columns, meta_info={'query': query, 'built_features': [], 'aggregate_values': {},
'columns': columns})
cursor.close()
if len(df) == 0:
raise(ValueError('SQL result in empty fetch!!'))
else:
self.set_data_set_attribute(data_set, 'data', df)
self.set_data_set_attribute(data_set, 'query', query)
self.set_data_set_attribute(data_set, 'columns', columns)
self.set_data_set_attribute(data_set, 'built_features', [])
示例12: CatFactsREST
# 需要导入模块: from shove import Shove [as 别名]
# 或者: from shove.Shove import sync [as 别名]
class CatFactsREST(object):
def __init__(self, config):
self.config = config
self.apikeys = [s.strip() for s in self.config['apikeys'].split(',')]
dburi = self.config['dburi']
self.db = Shove(dburi)
self.app = Flask(__name__)
self.api = TwilioRestClient(
self.config['SID'],
self.config['token'])
if 'numbers' not in self.db:
self.db['numbers'] = []
if 'facts' not in self.db:
print "No catfacts found, run catfacts load"
exit()
self.db.sync()
self.routes = {
"/api/numbers": (self.add_number, {"methods": ['POST']}),
"/api/numbers/<num>": (self.remove_number, {"methods":
['DELETE']}),
"/api/callback": (self.twilio_callback, {"methods": ['GET']}),
"/api/facts": (self.add_facts, {"methods": ['POST']}),
"/": (self.view_home, {"methods": ['GET']}),
"/subscribe": (self.subscribe, {"methods": ['POST']}),
"/submit": (self.submit, {"methods": ['POST']}),
}
map(
lambda route: self.app.route(route,
**self.routes[route][1])(self.routes[route][0]),
self.routes)
def view_home(self):
"""
View the CatFacts homepage, where you can submit CatFacts!
"""
return render_template('index.html')
def subscribe(self):
"""
Add a phone number to the CatFacts database.
"""
number = request.values['number']
data = json.dumps(dict(
number=number,
apikey='submitkey',
))
payload = dict(json=data)
urllib2.urlopen('http://localhost:{0}/api/numbers'.format(
self.config['port'], data=urllib.urlencode(payload)))
self.app.redirect('/') # TODO: Add success message
def submit(self):
"""
Submit a cat fact to the CatFacts database.
"""
fact = request.values['fact']
data = json.dumps(dict(
fact=fact,
apikey='submitkey',
))
payload = dict(json=data)
urllib2.urlopen('http://localhost:{0}/api/facts'.format(
self.config['port'], data=urllib.urlencode(payload)))
self.app.redirect('/') # TODO: Add success message
def add_number(self):
"""
POST: /api/numbers
"""
print "Adding numbers"
try:
j = request.values['json']
data = json.loads(j)
except Exception as e:
return json.dumps(dict(
success=False,
message="Invalid data recieved"))
try:
if data['apikey'] not in self.apikeys:
raise Exception
except:
return json.dumps(dict(
succes=False,
message="Unauthorized"))
try:
number = data['number']
if number not in self.db['numbers']:
self.db['numbers'].append(number)
self.db.sync()
self.api.sms.messages.create(
to=number,
from_="2037947419",
body="Congrats, you have been signed up for catfacts, \
the Premire cat information service, you will \
receive hourly cat information")
return json.dumps(dict(
success=True,
message="Added {0} to catfacts".format(number)))
#.........这里部分代码省略.........
示例13: Store
# 需要导入模块: from shove import Shove [as 别名]
# 或者: from shove.Shove import sync [as 别名]
class Store(object):
def setUp(self):
from shove import Shove
self.store = Shove(
self.initstring, sync=0
)
def tearDown(self):
self.store.close()
def test__getitem__(self):
self.store['max'] = 3
self.store.sync()
self.assertEqual(self.store['max'], 3)
def test__setitem__(self):
self.store['max'] = 3
self.store['d'] = {'A': 1}, {'A': 1}
self.store['d'] = {'AA': 1}, {'A': 1}
self.store['d'] = {'AA': 1}, {'AA': 1}
self.store.sync()
self.assertEqual(self.store['max'], 3)
def test__delitem__(self):
self.store['max'] = 3
self.store.sync()
del self.store['max']
self.store.sync()
self.assertEqual('max' in self.store, False)
def test_get(self):
self.store['max'] = 3
self.store.sync()
self.assertEqual(self.store.get('min'), None)
def test__cmp__(self):
from shove import Shove
tstore = Shove()
self.store['max'] = 3
self.store.sync()
tstore['max'] = 3
self.assertEqual(self.store, tstore)
def test__len__(self):
self.store['max'] = 3
self.store['min'] = 6
self.store.sync()
self.assertEqual(len(self.store), 2)
def test_items(self):
self.store['max'] = 3
self.store['min'] = 6
self.store['pow'] = 7
self.store.sync()
slist = list(items(self.store))
self.assertEqual(('min', 6) in slist, True)
def test_keys(self):
self.store['max'] = 3
self.store['min'] = 6
self.store['pow'] = 7
self.store.sync()
slist = list(keys(self.store))
self.assertEqual('min' in slist, True)
def test_values(self):
self.store['max'] = 3
self.store['min'] = 6
self.store['pow'] = 7
self.store.sync()
slist = list(values(self.store))
self.assertEqual(6 in slist, True)
def test_pop(self):
self.store['max'] = 3
self.store['min'] = 6
self.store.sync()
item = self.store.pop('min')
self.store.sync()
self.assertEqual(item, 6)
def test_setdefault(self):
self.store['max'] = 3
self.store['min'] = 6
self.store.sync()
self.assertEqual(self.store.setdefault('pow', 8), 8)
self.store.sync()
self.assertEqual(self.store['pow'], 8)
def test_update(self):
from shove import Shove
tstore = Shove()
tstore['max'] = 3
tstore['min'] = 6
tstore['pow'] = 7
self.store['max'] = 2
self.store['min'] = 3
self.store['pow'] = 7
self.store.update(tstore)
#.........这里部分代码省略.........
示例14: __init__
# 需要导入模块: from shove import Shove [as 别名]
# 或者: from shove.Shove import sync [as 别名]
class Personis_server:
def __init__(self, modeldir=None, adminsfile=None, oauthconfig=None):
self.modeldir = modeldir
self.admins = yaml.load(file(adminsfile,'r'))
self.oauth_clients = Shove('sqlite:///oauth_clients.dat')
self.users = Shove('sqlite:///oauth_users.dat')
self.bearers = Shove('sqlite:///oauth_bearers.dat')
self.oauthconf = yaml.load(file(oauthconfig,'r'))
def stopper():
print 'saving persistant data'
self.oauth_clients.close()
self.users.close()
self.bearers.close()
cherrypy.engine.subscribe('stop', stopper)
@cherrypy.expose
def list_clients(self):
if cherrypy.session.get('user') == None:
cherrypy.session['target_url'] = '/list_clients'
raise cherrypy.HTTPRedirect('/login')
if not cherrypy.session.get('user')['id'] in self.admins.keys():
raise cherrypy.HTTPError()
base_path = os.path.dirname(os.path.abspath(__file__))
loader = TemplateLoader([base_path])
tmpl = loader.load('list_clients.html')
for k, v in self.oauth_clients.items():
print k, v.friendly_name
stream = tmpl.generate(clients=self.oauth_clients.values())
return stream.render('xhtml')
@cherrypy.expose
def list_clients_save(self, id, value, _method='get'):
if cherrypy.session.get('user') == None:
raise cherrypy.HTTPError()
if not cherrypy.session.get('user')['id'] in self.admins.keys():
raise cherrypy.HTTPError()
if id == "removeOneForMe":
del(self.oauth_clients[value])
self.oauth_clients.sync()
print "removed a client"
raise cherrypy.HTTPRedirect('/list_clients')
if id == "addOneForMe":
clid = ''
secret = ''
for i in range(10):
clid = clid + str(int(random.random()*100))
secret = secret + str(int(random.random()*100))
self.oauth_clients[clid] = oauth_client(
client_id = clid,
friendly_name= 'my client',
secret= secret,
redirect_uri='http://www.example.com/',
icon='/static/images/icon.svg')
self.oauth_clients.sync()
print "added a client"
raise cherrypy.HTTPRedirect('/list_clients')
clid, field = id.split('|')
print 'saving: ',clid, field, value
oldc = self.oauth_clients[clid]
oldc.__dict__[field] = value
self.oauth_clients[clid] = oldc
for k, v in self.oauth_clients.items():
print k, v.friendly_name
self.oauth_clients.sync()
return value
@cherrypy.expose
def authorize(self, client_id, redirect_uri, scope, access_type, response_type='code', approval_prompt='auto', state=None):
body = cherrypy.request.body.fp.read()
rurl = cherrypy.request.base+cherrypy.request.path_info
cherrypy.session['client_id'] = client_id
cli = self.oauth_clients[client_id]
if state <> None:
cherrypy.session['state'] = state
if cli.redirect_uri <> redirect_uri:
raise cherrypy.HTTPError()
raise cherrypy.HTTPRedirect('/login')
@cherrypy.expose
def login(self):
flow = OAuth2WebServerFlow(client_id=self.oauthconf['personis_client_id'],
client_secret=self.oauthconf['personis_client_secret'],
scope='https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email',
user_agent='personis-server/1.0')
callback = callback = cherrypy.request.base + '/logged_in'
authorize_url = flow.step1_get_authorize_url(callback)
cherrypy.session['flow'] = flow
raise cherrypy.HTTPRedirect(authorize_url)
@cherrypy.expose
def logged_in(self, code):
flow = cherrypy.session.get('flow')
if not flow:
raise IOError()
credentials = flow.step2_exchange(cherrypy.request.params)
#.........这里部分代码省略.........
示例15: CSHLYServer
# 需要导入模块: from shove import Shove [as 别名]
# 或者: from shove.Shove import sync [as 别名]
class CSHLYServer(object):
"""
WSGI Server exposing the link shortening api
:type port: int
:param port: port to listen for connections on
:type link_db_uri: str
:param link_db_uri: a shove compatible database uri for storing shortened links
:type user_db_uri: str
:param user_db_uri: a shove compatible database uri for storing user information
:type use_auth: boolean
:param use_auth: Enable authentication or disable authentication, default is enabled
"""
def __new__(self, *args, **kwargs):
"""
Used to call decorators on all the functions in the class
shorten -> /api/shorten
unshorten -> /api/unshorten/<hashed>
unshorten_redirect -> /<hashed>
get_link_data is cached
"""
obj = super(CSHLYServer, self).__new__(self, *args, **kwargs)
obj.cache = Shove()
obj.unshorten = cache(obj.unshorten, obj.cache, 300)
route("/api/shorten", method='PUT')(obj.shorten)
route("/api/unshorten/<hashed>", method='GET')(obj.unshorten)
route("/<hashed>", method='GET')(obj.unshorten_redirect)
obj.get_link_data = cache(obj.get_link_data, obj.cache, 1200)
return obj
def __init__(self, port, link_db_uri, user_db_uri, use_auth=True):
self.port = port
self.link_db = Shove(link_db_uri)
self.user_db = Shove(user_db_uri)
self.use_auth = use_auth
if not self.use_auth and 'null' not in self.user_db:
self.user_db['null'] = {'token': '', 'username':'null', 'links': []}
def get_link_data(self, hashed_link):
"""
Used to get information on a hashed link. This function is cached
:type hashed_link: str
:param hashed_link: the hashed link to retrieve information on
"""
try:
data = self.link_db[hashed_link]
return data
except:
return None
def is_user_authenticated(self, user_id, auth_token):
"""
Check to see if a user is authenticated or not
:type user_id: str
:param user_id: the users ID
:type auth_token: str
:param auth_token: a token used to see if a user is valid
"""
user = self.user_db[user_id]
if user['token'] == auth_token:
return True
else:
return False
def shorten(self):
"""
Used to handle the shorten api endpoint
The body of the request contains the json formatted request to shorten a url
this function returns a json formatted response with the shortened url
"""
data = request.body.readline()
print "Received shorten request: {0}".format(data)
if not data:
abort(400, 'No data received')
data = json.loads(data)
if "full_link" in data:
if ("user_id" in data and "auth_token" in data and self.is_user_authenticated(data['user_id'], data['auth_token'])) or not self.use_auth:
hashed = hashLink(data['full_link'])
self.link_db[hashed] = {'lookups':0, 'owner': data.get('user_id','null'), 'full_link': data['full_link']}
self.link_db.sync()
try:
self.user_db[data.get('user_id','null')]['links'].append(hashed)
except:
self.user_db[data.get('user_id','null')]['links'] = [hashed,]
self.user_db.sync()
return json.dumps({"shortened": hashed})
else:
abort(403, 'User id or auth token incorrect')
#.........这里部分代码省略.........