<s> """<STR_LIT>""" <EOL> import logging <EOL> import os <EOL> import time <EOL> import boto . dynamodb2 <EOL> from boto . dynamodb2 import fields <EOL> from boto . dynamodb2 import table <EOL> from boto . dynamodb2 import types <EOL> from boto . dynamodb2 import layer1 <EOL> import flask <EOL> from appengine import building <EOL> TABLE_NAME = '<STR_LIT>' <EOL> SCHEMA = [ <EOL> fields . HashKey ( '<STR_LIT>' ) , <EOL> fields . RangeKey ( '<STR_LIT>' , data_type = types . NUMBER ) , <EOL> ] <EOL> THROUGHPUT = { '<STR_LIT>' : <NUM_LIT:5> , '<STR_LIT>' : <NUM_LIT:15> } <EOL> INDEXES = [ <EOL> fields . AllIndex ( '<STR_LIT>' , parts = [ <EOL> fields . HashKey ( '<STR_LIT>' ) , <EOL> fields . RangeKey ( '<STR_LIT>' , data_type = types . NUMBER ) <EOL> ] ) , <EOL> ] <EOL> FIELDS_TO_IGNORE = { '<STR_LIT:class>' , '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' } <EOL> def get_history_table ( ) : <EOL> """<STR_LIT>""" <EOL> if False : <EOL> logging . info ( '<STR_LIT>' ) <EOL> connection = layer1 . DynamoDBConnection ( <EOL> region = '<STR_LIT>' , <EOL> host = '<STR_LIT:localhost>' , <EOL> port = <NUM_LIT> , <EOL> aws_access_key_id = '<STR_LIT>' , <EOL> aws_secret_access_key = '<STR_LIT>' , <EOL> is_secure = False <EOL> ) <EOL> else : <EOL> from common import creds <EOL> connection = boto . dynamodb2 . connect_to_region ( <EOL> '<STR_LIT>' , <EOL> aws_access_key_id = creds . AWS_ACCESS_KEY_ID , <EOL> aws_secret_access_key = creds . AWS_SECRET_ACCESS_KEY , <EOL> ) <EOL> if TABLE_NAME in connection . list_tables ( ) [ '<STR_LIT>' ] : <EOL> history_table = table . Table ( <EOL> TABLE_NAME , <EOL> schema = SCHEMA , <EOL> throughput = THROUGHPUT , <EOL> indexes = INDEXES , <EOL> connection = connection ) <EOL> else : <EOL> history_table = table . Table . create ( <EOL> TABLE_NAME , <EOL> schema = SCHEMA , <EOL> throughput = THROUGHPUT , <EOL> indexes = INDEXES , <EOL> connection = connection ) <EOL> return history_table <EOL> def store_version ( version ) : <EOL> """<STR_LIT>""" <EOL> batch = flask . g . get ( '<STR_LIT>' , None ) <EOL> if batch is None : <EOL> batch = [ ] <EOL> setattr ( flask . g , '<STR_LIT>' , batch ) <EOL> building_id = building . get_id ( ) <EOL> batch . append ( ( building_id , version ) ) <EOL> def store_batch ( ) : <EOL> """<STR_LIT>""" <EOL> history = flask . g . get ( '<STR_LIT>' , None ) <EOL> setattr ( flask . g , '<STR_LIT>' , None ) <EOL> if history is None : <EOL> return <EOL> logging . info ( '<STR_LIT>' , len ( history ) ) <EOL> timestamp = long ( time . time ( ) * <NUM_LIT:1000> ) <EOL> items = { } <EOL> for building_id , version in history : <EOL> version [ '<STR_LIT>' ] = '<STR_LIT>' % ( <EOL> building_id , version [ '<STR_LIT:class>' ] , version [ '<STR_LIT:id>' ] ) <EOL> version [ '<STR_LIT>' ] = timestamp <EOL> for key in FIELDS_TO_IGNORE : <EOL> version . pop ( key , None ) <EOL> for key in version . keys ( ) : <EOL> value = version [ key ] <EOL> if isinstance ( value , ( list , dict ) ) : <EOL> version [ key ] = flask . json . dumps ( value ) <EOL> elif isinstance ( value , float ) : <EOL> del version [ key ] <EOL> items [ version [ '<STR_LIT>' ] ] = version <EOL> history_table = get_history_table ( ) <EOL> with history_table . batch_write ( ) as batch : <EOL> for item in items . itervalues ( ) : <EOL> batch . put_item ( data = item ) <EOL> def get_range ( cls , object_id , start , end ) : <EOL> """<STR_LIT>""" <EOL> building_id = building . get_id ( ) <EOL> history_table = get_history_table ( ) <EOL> values = history_table . query_2 ( <EOL> hash_key__eq = '<STR_LIT>' % ( building_id , cls , object_id ) , <EOL> range_key__gt = start , <EOL> range_key__lte = end ) <EOL> for value in values : <EOL> del value [ '<STR_LIT>' ] <EOL> yield value </s>
