<s> from h . api import uri <EOL> from h . api . _compat import string_types <EOL> def prepare ( annotation , fetcher ) : <EOL> """<STR_LIT>""" <EOL> set_group_if_reply ( annotation , fetcher = fetcher ) <EOL> insert_group_if_none ( annotation ) <EOL> set_group_permissions ( annotation ) <EOL> fix_old_style_comments ( annotation ) <EOL> normalize_annotation_target_uris ( annotation ) <EOL> def set_group_if_reply ( annotation , fetcher ) : <EOL> """<STR_LIT>""" <EOL> def is_reply ( annotation ) : <EOL> """<STR_LIT>""" <EOL> if annotation . get ( '<STR_LIT>' ) : <EOL> return True <EOL> else : <EOL> return False <EOL> if not is_reply ( annotation ) : <EOL> return <EOL> top_level_annotation_id = annotation [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> top_level_annotation = fetcher ( top_level_annotation_id ) <EOL> if top_level_annotation is None : <EOL> return <EOL> if '<STR_LIT>' in top_level_annotation : <EOL> annotation [ '<STR_LIT>' ] = top_level_annotation [ '<STR_LIT>' ] <EOL> else : <EOL> if '<STR_LIT>' in annotation : <EOL> del annotation [ '<STR_LIT>' ] <EOL> def insert_group_if_none ( annotation ) : <EOL> if not annotation . get ( '<STR_LIT>' ) : <EOL> annotation [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> def set_group_permissions ( annotation ) : <EOL> """<STR_LIT>""" <EOL> permissions = annotation . get ( '<STR_LIT>' ) <EOL> if permissions is None : <EOL> return <EOL> is_private = ( permissions . get ( '<STR_LIT>' ) == [ annotation [ '<STR_LIT:user>' ] ] ) <EOL> if is_private : <EOL> return <EOL> group = annotation . get ( '<STR_LIT>' ) <EOL> if group == '<STR_LIT>' : <EOL> return <EOL> group_principal = '<STR_LIT>' + group <EOL> annotation [ '<STR_LIT>' ] [ '<STR_LIT>' ] = [ group_principal ] <EOL> def normalize_annotation_target_uris ( annotation ) : <EOL> if '<STR_LIT:target>' not in annotation : <EOL> return <EOL> if not isinstance ( annotation [ '<STR_LIT:target>' ] , list ) : <EOL> return <EOL> for target in annotation [ '<STR_LIT:target>' ] : <EOL> if not isinstance ( target , dict ) : <EOL> continue <EOL> if '<STR_LIT:source>' not in target : <EOL> continue <EOL> if not isinstance ( target [ '<STR_LIT:source>' ] , string_types ) : <EOL> continue <EOL> target [ '<STR_LIT>' ] = [ uri . normalize ( target [ '<STR_LIT:source>' ] ) ] <EOL> def fix_old_style_comments ( annotation ) : <EOL> """<STR_LIT>""" <EOL> def isempty ( container , field ) : <EOL> val = container . get ( field ) <EOL> return val is None or val == [ ] <EOL> has_uri = annotation . get ( '<STR_LIT>' ) is not None <EOL> has_no_targets = isempty ( annotation , '<STR_LIT:target>' ) <EOL> has_no_references = isempty ( annotation , '<STR_LIT>' ) <EOL> if has_uri and has_no_targets and has_no_references : <EOL> annotation [ '<STR_LIT:target>' ] = [ { '<STR_LIT:source>' : annotation . get ( '<STR_LIT>' ) } ] </s>
