<s> """<STR_LIT>""" <EOL> import collections <EOL> import copy <EOL> LINK_GENERATORS_KEY = '<STR_LIT>' <EOL> class AnnotationBasePresenter ( object ) : <EOL> def __init__ ( self , request , annotation ) : <EOL> self . annotation = annotation <EOL> self . request = request <EOL> @ property <EOL> def created ( self ) : <EOL> if self . annotation . created : <EOL> return utc_iso8601 ( self . annotation . created ) <EOL> @ property <EOL> def updated ( self ) : <EOL> if self . annotation . updated : <EOL> return utc_iso8601 ( self . annotation . updated ) <EOL> @ property <EOL> def links ( self ) : <EOL> """<STR_LIT>""" <EOL> link_generators = self . request . registry . get ( LINK_GENERATORS_KEY , { } ) <EOL> out = { } <EOL> for name , generator in link_generators . items ( ) : <EOL> link = generator ( self . request , self . annotation ) <EOL> if link is not None : <EOL> out [ name ] = link <EOL> return out <EOL> @ property <EOL> def text ( self ) : <EOL> if self . annotation . text : <EOL> return self . annotation . text <EOL> else : <EOL> return '<STR_LIT>' <EOL> @ property <EOL> def tags ( self ) : <EOL> if self . annotation . tags : <EOL> return self . annotation . tags <EOL> else : <EOL> return [ ] <EOL> @ property <EOL> def target ( self ) : <EOL> target = { '<STR_LIT:source>' : self . annotation . target_uri } <EOL> if self . annotation . target_selectors : <EOL> target [ '<STR_LIT>' ] = self . annotation . target_selectors <EOL> return [ target ] <EOL> class AnnotationJSONPresenter ( AnnotationBasePresenter ) : <EOL> def asdict ( self ) : <EOL> docpresenter = DocumentJSONPresenter ( self . annotation . document ) <EOL> base = { <EOL> '<STR_LIT:id>' : self . annotation . id , <EOL> '<STR_LIT>' : self . created , <EOL> '<STR_LIT>' : self . updated , <EOL> '<STR_LIT:user>' : self . annotation . userid , <EOL> '<STR_LIT>' : self . annotation . target_uri , <EOL> '<STR_LIT:text>' : self . text , <EOL> '<STR_LIT>' : self . tags , <EOL> '<STR_LIT>' : self . annotation . groupid , <EOL> '<STR_LIT>' : self . permissions , <EOL> '<STR_LIT:target>' : self . target , <EOL> '<STR_LIT>' : docpresenter . asdict ( ) , <EOL> '<STR_LIT>' : self . links , <EOL> } <EOL> if self . annotation . references : <EOL> base [ '<STR_LIT>' ] = self . annotation . references <EOL> annotation = copy . copy ( self . annotation . extra ) or { } <EOL> annotation . update ( base ) <EOL> return annotation <EOL> @ property <EOL> def permissions ( self ) : <EOL> read = self . annotation . userid <EOL> if self . annotation . shared : <EOL> read = '<STR_LIT>' . format ( self . annotation . groupid ) <EOL> return { '<STR_LIT>' : [ read ] , <EOL> '<STR_LIT>' : [ self . annotation . userid ] , <EOL> '<STR_LIT>' : [ self . annotation . userid ] , <EOL> '<STR_LIT>' : [ self . annotation . userid ] } <EOL> class AnnotationJSONLDPresenter ( AnnotationBasePresenter ) : <EOL> """<STR_LIT>""" <EOL> CONTEXT_URL = '<STR_LIT>' <EOL> def asdict ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . CONTEXT_URL , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : self . id , <EOL> '<STR_LIT>' : self . created , <EOL> '<STR_LIT>' : self . updated , <EOL> '<STR_LIT>' : self . annotation . userid , <EOL> '<STR_LIT:body>' : self . bodies , <EOL> '<STR_LIT:target>' : self . target , <EOL> } <EOL> @ property <EOL> def id ( self ) : <EOL> return self . request . route_url ( '<STR_LIT>' , id = self . annotation . id ) <EOL> @ property <EOL> def bodies ( self ) : <EOL> bodies = [ { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:text>' : self . text , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ] <EOL> for t in self . tags : <EOL> bodies . append ( { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:text>' : t , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) <EOL> return bodies <EOL> class DocumentJSONPresenter ( object ) : <EOL> def __init__ ( self , document ) : <EOL> self . document = document <EOL> def asdict ( self ) : <EOL> if not self . document : <EOL> return { } <EOL> d = { } <EOL> for docmeta in self . document . meta : <EOL> meta_presenter = DocumentMetaJSONPresenter ( docmeta ) <EOL> deep_merge_dict ( d , meta_presenter . asdict ( ) ) <EOL> d [ '<STR_LIT>' ] = [ ] <EOL> for docuri in self . document . document_uris : <EOL> uri_presenter = DocumentURIJSONPresenter ( docuri ) <EOL> d [ '<STR_LIT>' ] . append ( uri_presenter . asdict ( ) ) <EOL> return d <EOL> class DocumentMetaJSONPresenter ( object ) : <EOL> def __init__ ( self , document_meta ) : <EOL> self . document_meta = document_meta <EOL> def asdict ( self ) : <EOL> reversed_path = self . document_meta . type . split ( '<STR_LIT:.>' ) [ : : - <NUM_LIT:1> ] <EOL> d = self . document_meta . value <EOL> for nested in reversed_path : <EOL> d = { nested : d } <EOL> return d <EOL> class DocumentURIJSONPresenter ( object ) : <EOL> def __init__ ( self , document_uri ) : <EOL> self . document_uri = document_uri <EOL> def asdict ( self ) : <EOL> data = { '<STR_LIT>' : self . document_uri . uri } <EOL> rel = self . rel <EOL> if rel : <EOL> data [ '<STR_LIT>' ] = rel <EOL> type = self . document_uri . content_type <EOL> if type : <EOL> data [ '<STR_LIT:type>' ] = type <EOL> return data <EOL> @ property <EOL> def rel ( self ) : <EOL> type = self . document_uri . type <EOL> if type and type . startswith ( '<STR_LIT>' ) : <EOL> return self . document_uri . type [ <NUM_LIT:4> : ] <EOL> def add_annotation_link_generator ( registry , name , generator ) : <EOL> """<STR_LIT>""" <EOL> if LINK_GENERATORS_KEY not in registry : <EOL> registry [ LINK_GENERATORS_KEY ] = { } <EOL> registry [ LINK_GENERATORS_KEY ] [ name ] = generator <EOL> def utc_iso8601 ( datetime ) : <EOL> return datetime . strftime ( '<STR_LIT>' ) <EOL> def deep_merge_dict ( a , b ) : <EOL> """<STR_LIT>""" <EOL> for k , v in b . items ( ) : <EOL> if isinstance ( v , collections . Mapping ) : <EOL> if k not in a or not isinstance ( a [ k ] , dict ) : <EOL> a [ k ] = dict ( ) <EOL> deep_merge_dict ( a [ k ] , v ) <EOL> else : <EOL> a [ k ] = v <EOL> def _json_link ( request , annotation ) : <EOL> return request . route_url ( '<STR_LIT>' , id = annotation . id ) <EOL> def includeme ( config ) : <EOL> config . add_directive ( <EOL> '<STR_LIT>' , <EOL> lambda c , n , g : add_annotation_link_generator ( c . registry , n , g ) ) <EOL> config . add_annotation_link_generator ( '<STR_LIT>' , _json_link ) </s>
