<s> from biicode . server . model . address_table import AddressTable <EOL> from biicode . server . model . time_base_map import TimeBaseMap <EOL> from biicode . common . model . symbolic . block_version import BlockVersion <EOL> from biicode . common . model . brl . brl_block import BRLBlock <EOL> from biicode . common . model . block_delta import BlockDelta <EOL> from biicode . common . model . symbolic . block_version_table import BlockVersionTable <EOL> from biicode . common . exception import PublishException <EOL> import time <EOL> from biicode . common . utils . bii_logging import logger <EOL> from biicode . common . utils . serializer import Serializer , ListDeserializer <EOL> from biicode . common . model . id import ID <EOL> from biicode . server . model . time_base_map import TimeBaseMapDeserializer <EOL> from biicode . common . model . renames import Renames <EOL> from biicode . common . model . version_tag import DEV <EOL> class Block ( object ) : <EOL> def __init__ ( self , numeric_id , brl_id ) : <EOL> self . _numeric_id = numeric_id <EOL> self . _id = brl_id <EOL> self . _cells_table = AddressTable ( numeric_id ) <EOL> self . _contents_table = AddressTable ( numeric_id ) <EOL> self . _deps_table = TimeBaseMap ( ) <EOL> self . _renames = TimeBaseMap ( ) <EOL> self . _deltas = [ ] <EOL> self . _cell_count = <NUM_LIT:0> <EOL> self . _content_count = <NUM_LIT:0> <EOL> def __repr__ ( self ) : <EOL> result = [ ] <EOL> result . append ( '<STR_LIT>' + repr ( self . _numeric_id ) + repr ( self . _id ) ) <EOL> result . append ( repr ( self . _cells_table ) ) <EOL> result . append ( repr ( self . _contents_table ) ) <EOL> result . append ( repr ( self . _deps_table ) ) <EOL> return '<STR_LIT:\n>' . join ( result ) <EOL> def all_ids ( self ) : <EOL> return self . _cells_table . all_ids ( ) , self . _contents_table . all_ids ( ) <EOL> @ property <EOL> def ID ( self ) : <EOL> return self . _id <EOL> @ property <EOL> def numeric_id ( self ) : <EOL> return self . _numeric_id <EOL> @ property <EOL> def cells ( self ) : <EOL> return self . _cells_table <EOL> @ property <EOL> def contents ( self ) : <EOL> return self . _contents_table <EOL> @ property <EOL> def dep_tables ( self ) : <EOL> return self . _deps_table <EOL> @ property <EOL> def deltas ( self ) : <EOL> return self . _deltas <EOL> @ property <EOL> def cell_count ( self ) : <EOL> return self . _cell_count <EOL> @ property <EOL> def content_count ( self ) : <EOL> return self . _content_count <EOL> @ property <EOL> def last_delta ( self ) : <EOL> if not self . deltas : <EOL> return None <EOL> return self . deltas [ len ( self . deltas ) - <NUM_LIT:1> ] <EOL> def last_version ( self ) : <EOL> last_delta = len ( self . _deltas ) - <NUM_LIT:1> <EOL> versiontag = self . _deltas [ last_delta ] . versiontag if last_delta >= <NUM_LIT:0> else None <EOL> return BlockVersion ( self . _id , last_delta , versiontag ) <EOL> def last_version_cells ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . cells . get_all_ids ( len ( self . _deltas ) - <NUM_LIT:1> ) <EOL> def get_renames ( self , begin , end ) : <EOL> '''<STR_LIT>''' <EOL> renames = Renames ( ) <EOL> for r in self . _renames . xrange ( begin + <NUM_LIT:1> , end + <NUM_LIT:1> ) : <EOL> renames . cat ( r ) <EOL> return renames <EOL> def add_publication ( self , publish_request , commiter = None ) : <EOL> logger . debug ( "<STR_LIT>" % repr ( publish_request ) ) <EOL> current_time = len ( self . _deltas ) <EOL> delta = BlockDelta ( publish_request . msg , publish_request . tag , <EOL> versiontag = publish_request . versiontag , date = time . time ( ) , <EOL> origin = publish_request . origin , <EOL> commiter = commiter ) <EOL> target_time = publish_request . parent . time <EOL> if target_time != current_time - <NUM_LIT:1> : <EOL> raise PublishException ( "<STR_LIT>" <EOL> % ( publish_request . block_name , target_time , current_time - <NUM_LIT:1> ) ) <EOL> if not publish_request : <EOL> if current_time > <NUM_LIT:0> : <EOL> old_delta = self . _deltas [ - <NUM_LIT:1> ] <EOL> if old_delta . tag < publish_request . tag : <EOL> self . _deltas [ - <NUM_LIT:1> ] = delta <EOL> return [ ] , [ ] , [ ] , [ ] <EOL> raise PublishException ( '<STR_LIT>' ) <EOL> if ( current_time > <NUM_LIT:0> and self . _deltas [ current_time - <NUM_LIT:1> ] . tag == DEV ) : <EOL> if publish_request . parent_time != self . _deltas [ - <NUM_LIT:1> ] . date : <EOL> raise PublishException ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> current_time = current_time - <NUM_LIT:1> <EOL> self . _deltas [ - <NUM_LIT:1> ] = delta <EOL> else : <EOL> self . _deltas . append ( delta ) <EOL> self . _deps_table . pop_dev ( current_time ) <EOL> _ , old_table = self . _deps_table . last ( ) <EOL> if old_table != publish_request . deptable : <EOL> self . _deps_table . append ( current_time , publish_request . deptable ) <EOL> contents = [ ] <EOL> cells = [ ] <EOL> dev_cell_ids = [ ] <EOL> dev_content_ids = [ ] <EOL> for name in publish_request . deleted : <EOL> self . _cells_table . delete ( name , current_time ) <EOL> try : <EOL> self . _contents_table . delete ( name , current_time ) <EOL> except KeyError : <EOL> pass <EOL> for cell in publish_request . cells : <EOL> old_id = self . _cells_table . pop_dev ( cell . name . cell_name , current_time ) <EOL> if old_id is not None : <EOL> old_id = self . _numeric_id + old_id [ <NUM_LIT:0> ] <EOL> dev_cell_ids . append ( old_id ) <EOL> id_ = self . _numeric_id + self . _cell_count <EOL> self . _cell_count += <NUM_LIT:1> <EOL> cell . ID = id_ <EOL> cells . append ( cell ) <EOL> self . _cells_table . create ( cell . name . cell_name , cell . ID , current_time ) <EOL> for name , content in publish_request . contents . iteritems ( ) : <EOL> old_id = self . _contents_table . pop_dev ( name , current_time ) <EOL> if old_id is not None : <EOL> old_id = self . _numeric_id + old_id [ <NUM_LIT:0> ] <EOL> dev_content_ids . append ( old_id ) <EOL> if content is not None : <EOL> id_ = self . _numeric_id + self . _content_count <EOL> self . _content_count += <NUM_LIT:1> <EOL> content . ID = id_ <EOL> contents . append ( content ) <EOL> self . _contents_table . create ( name , content . ID , current_time ) <EOL> else : <EOL> self . _contents_table . delete ( name , current_time ) <EOL> for name , content_id in publish_request . contents_ids . iteritems ( ) : <EOL> self . _contents_table . create ( name , content_id , current_time ) <EOL> self . _renames . pop_dev ( current_time ) <EOL> if publish_request . renames : <EOL> self . _renames . append ( current_time , publish_request . renames ) <EOL> return ( cells , contents , dev_cell_ids , dev_content_ids ) <EOL> def publish_datetime ( self , time ) : <EOL> '''<STR_LIT>''' <EOL> return self . _deltas [ time ] . date <EOL> def __eq__ ( self , other ) : <EOL> if self is other : <EOL> return True <EOL> return isinstance ( other , self . __class__ ) and self . _numeric_id == other . _numeric_id and self . _id == other . _id and self . _cells_table == other . _cells_table and self . _contents_table == other . _contents_table and self . _deps_table == other . _deps_table and self . _renames == other . _renames and self . _deltas == other . _deltas and self . _cell_count == other . _cell_count and self . _content_count == other . _content_count <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> SERIAL_ACCESS_KEY = '<STR_LIT:a>' <EOL> SERIAL_NUMERIC_ID_KEY = '<STR_LIT:n>' <EOL> SERIAL_ID_KEY = '<STR_LIT>' <EOL> SERIAL_CELL_TABLE = '<STR_LIT>' <EOL> SERIAL_CONTENT_TABLE = '<STR_LIT>' <EOL> SERIAL_DEPS_TABLE = '<STR_LIT>' <EOL> SERIAL_RENAMES = '<STR_LIT:m>' <EOL> SERIAL_DELTAS = '<STR_LIT>' <EOL> SERIAL_CELLS_COUNTER = '<STR_LIT:i>' <EOL> SERIAL_CONTENT_COUNTER = '<STR_LIT>' <EOL> def serialize ( self ) : <EOL> assert isinstance ( self . _numeric_id , ID ) , self . _numeric_id . __class__ <EOL> return Serializer ( ) . build ( <EOL> ( self . SERIAL_ID_KEY , self . _id ) , <EOL> ( self . SERIAL_NUMERIC_ID_KEY , self . _numeric_id ) , <EOL> ( self . SERIAL_CELL_TABLE , self . _cells_table ) , <EOL> ( self . SERIAL_CONTENT_TABLE , self . _contents_table ) , <EOL> ( self . SERIAL_DEPS_TABLE , self . _deps_table ) , <EOL> ( self . SERIAL_RENAMES , self . _renames ) , <EOL> ( self . SERIAL_DELTAS , self . _deltas ) , <EOL> ( self . SERIAL_CELLS_COUNTER , self . _cell_count ) , <EOL> ( self . SERIAL_CONTENT_COUNTER , self . _content_count ) <EOL> ) <EOL> @ staticmethod <EOL> def deserialize ( doc ) : <EOL> numeric_id = ID . deserialize ( doc [ Block . SERIAL_NUMERIC_ID_KEY ] ) <EOL> m = Block ( brl_id = BRLBlock ( doc [ Block . SERIAL_ID_KEY ] ) , numeric_id = numeric_id ) <EOL> m . _cells_table = AddressTable . deserialize ( doc [ Block . SERIAL_CELL_TABLE ] , numeric_id ) <EOL> m . _contents_table = AddressTable . deserialize ( doc [ Block . SERIAL_CONTENT_TABLE ] , numeric_id ) <EOL> m . _deps_table = TimeBaseMapDeserializer ( BlockVersionTable ) . deserialize ( doc [ Block . SERIAL_DEPS_TABLE ] ) <EOL> m . _renames = TimeBaseMapDeserializer ( Renames ) . deserialize ( doc [ Block . SERIAL_RENAMES ] ) <EOL> m . _deltas = ListDeserializer ( BlockDelta ) . deserialize ( doc [ Block . SERIAL_DELTAS ] ) <EOL> m . _cell_count = int ( doc [ Block . SERIAL_CELLS_COUNTER ] ) <EOL> m . _content_count = int ( doc [ Block . SERIAL_CONTENT_COUNTER ] ) <EOL> return m </s>
