<s> """<STR_LIT>""" <EOL> import os <EOL> import couchdb <EOL> import time <EOL> import random <EOL> _db = None <EOL> _cache = [ ] <EOL> _timestamp = <NUM_LIT:0> <EOL> _timestamp_count = <NUM_LIT:0> <EOL> design = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : """<STR_LIT>""" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : """<STR_LIT>""" , <EOL> "<STR_LIT>" : """<STR_LIT>""" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : """<STR_LIT>""" , <EOL> "<STR_LIT>" : """<STR_LIT>""" <EOL> } <EOL> } <EOL> } <EOL> class TooManySaves ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def generate_blanks ( amount = <NUM_LIT:5> ) : <EOL> """<STR_LIT>""" <EOL> print ( "<STR_LIT>" % amount ) <EOL> res = _db . view ( "<STR_LIT>" , reduce = True ) <EOL> if len ( res ) == <NUM_LIT:0> : <EOL> base_id = <NUM_LIT:0> <EOL> else : <EOL> base_id = res . rows [ <NUM_LIT:0> ] . value + <NUM_LIT:1> <EOL> print ( "<STR_LIT>" % base_id ) <EOL> for i in range ( base_id , base_id + amount ) : <EOL> doc = { "<STR_LIT>" : str ( i ) , "<STR_LIT>" : True } <EOL> try : <EOL> _db . save ( doc ) <EOL> except couchdb . ResourceConflict : <EOL> pass <EOL> def get_available_doc ( ) : <EOL> """<STR_LIT>""" <EOL> res = _db . view ( "<STR_LIT>" , limit = <NUM_LIT:5> , include_docs = True ) <EOL> if len ( res ) == <NUM_LIT:0> : <EOL> generate_blanks ( ) <EOL> return get_available_doc ( ) <EOL> return random . choice ( [ row . doc for row in res . rows ] ) <EOL> def save_paste ( source_id , data , expiration ) : <EOL> """<STR_LIT>""" <EOL> global _timestamp , _timestamp_count <EOL> now = int ( time . time ( ) ) <EOL> if _timestamp != now : <EOL> _timestamp = now <EOL> _timestamp_count = <NUM_LIT:1> <EOL> else : <EOL> _timestamp_count += <NUM_LIT:1> <EOL> if _timestamp_count > <NUM_LIT:10> : <EOL> raise TooManySaves ( ) <EOL> doc = get_available_doc ( ) <EOL> new_doc = doc . copy ( ) <EOL> new_doc [ "<STR_LIT>" ] = data <EOL> new_doc [ "<STR_LIT:source>" ] = source_id <EOL> new_doc [ "<STR_LIT>" ] = False <EOL> new_doc [ "<STR_LIT>" ] = int ( time . time ( ) + expiration ) <EOL> try : <EOL> _db . save ( new_doc ) <EOL> except ResourceConflict : <EOL> return save_paste ( id , data , expiration ) <EOL> id = int ( doc . id ) <EOL> _cache . append ( new_doc ) <EOL> while len ( _cache ) > <NUM_LIT:50> : <EOL> _cache . pop ( <NUM_LIT:0> ) <EOL> return id <EOL> def get_paste ( id ) : <EOL> for doc in _cache : <EOL> if int ( doc [ "<STR_LIT>" ] ) == id : <EOL> print ( "<STR_LIT>" % id ) <EOL> return doc <EOL> return _db . get ( str ( id ) ) <EOL> def allocated_slots ( ) : <EOL> """<STR_LIT>""" <EOL> res = _db . view ( "<STR_LIT>" , reduce = True ) <EOL> return res . rows [ <NUM_LIT:0> ] . value <EOL> def cache_len ( ) : <EOL> """<STR_LIT>""" <EOL> return len ( _cache ) <EOL> def last_paste_id ( ) : <EOL> """<STR_LIT>""" <EOL> if _cache : <EOL> return int ( _cache [ - <NUM_LIT:1> ] [ "<STR_LIT>" ] ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def init ( ) : <EOL> """<STR_LIT>""" <EOL> global _db <EOL> if _db : return <EOL> couch_url = os . environ . get ( "<STR_LIT>" ) <EOL> if couch_url : <EOL> kwargs = { "<STR_LIT:url>" : couch_url } <EOL> else : <EOL> kwargs = { } <EOL> server = couchdb . Server ( ** kwargs ) <EOL> if "<STR_LIT>" not in server : <EOL> print ( "<STR_LIT>" ) <EOL> _db = server . create ( "<STR_LIT>" ) <EOL> else : <EOL> _db = server [ "<STR_LIT>" ] <EOL> current_design = _db . get ( "<STR_LIT>" ) <EOL> if not current_design : <EOL> print ( "<STR_LIT>" ) <EOL> _db . save ( design ) <EOL> elif current_design [ "<STR_LIT>" ] != design [ "<STR_LIT>" ] : <EOL> print ( "<STR_LIT>" ) <EOL> new_design = current_design . copy ( ) <EOL> new_design [ "<STR_LIT>" ] = design [ "<STR_LIT>" ] <EOL> _db . save ( new_design ) </s>
