<s> import copy <EOL> from itertools import chain <EOL> from jedi . parser import tree <EOL> def deep_ast_copy ( obj , parent = None , new_elements = None ) : <EOL> """<STR_LIT>""" <EOL> if new_elements is None : <EOL> new_elements = { } <EOL> def copy_node ( obj ) : <EOL> try : <EOL> return new_elements [ obj ] <EOL> except KeyError : <EOL> new_obj = copy . copy ( obj ) <EOL> new_elements [ obj ] = new_obj <EOL> new_children = [ ] <EOL> for child in obj . children : <EOL> typ = child . type <EOL> if typ in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:string>' ) : <EOL> new_child = child <EOL> elif typ == '<STR_LIT:name>' : <EOL> new_elements [ child ] = new_child = copy . copy ( child ) <EOL> new_child . parent = new_obj <EOL> else : <EOL> new_child = copy_node ( child ) <EOL> new_child . parent = new_obj <EOL> new_children . append ( new_child ) <EOL> new_obj . children = new_children <EOL> try : <EOL> names_dict = obj . names_dict <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> try : <EOL> new_obj . names_dict = new_names_dict = { } <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> for string , names in names_dict . items ( ) : <EOL> new_names_dict [ string ] = [ new_elements [ n ] for n in names ] <EOL> return new_obj <EOL> if obj . type == '<STR_LIT:name>' : <EOL> new_elements [ obj ] = new_obj = copy . copy ( obj ) <EOL> if parent is not None : <EOL> new_obj . parent = parent <EOL> elif isinstance ( obj , tree . BaseNode ) : <EOL> new_obj = copy_node ( obj ) <EOL> if parent is not None : <EOL> for child in new_obj . children : <EOL> if isinstance ( child , ( tree . Name , tree . BaseNode ) ) : <EOL> child . parent = parent <EOL> else : <EOL> new_obj = obj <EOL> return new_obj <EOL> def call_of_name ( name , cut_own_trailer = False ) : <EOL> """<STR_LIT>""" <EOL> par = name <EOL> if tree . is_node ( par . parent , '<STR_LIT>' ) : <EOL> par = par . parent <EOL> power = par . parent <EOL> if tree . is_node ( power , '<STR_LIT>' ) and power . children [ <NUM_LIT:0> ] != name and not ( power . children [ - <NUM_LIT:2> ] == '<STR_LIT>' and <EOL> name . start_pos > power . children [ - <NUM_LIT:1> ] . start_pos ) : <EOL> par = power <EOL> index = par . children . index ( name . parent ) <EOL> if index != len ( par . children ) - <NUM_LIT:1> or cut_own_trailer : <EOL> par = deep_ast_copy ( par ) <EOL> if not cut_own_trailer : <EOL> index = index + <NUM_LIT:1> <EOL> par . children [ index : ] = [ ] <EOL> return par <EOL> def get_module_names ( module , all_scopes ) : <EOL> """<STR_LIT>""" <EOL> if all_scopes : <EOL> dct = module . used_names <EOL> else : <EOL> dct = module . names_dict <EOL> return chain . from_iterable ( dct . values ( ) ) <EOL> class FakeImport ( tree . ImportName ) : <EOL> def __init__ ( self , name , parent , level = <NUM_LIT:0> ) : <EOL> super ( FakeImport , self ) . __init__ ( [ ] ) <EOL> self . parent = parent <EOL> self . _level = level <EOL> self . name = name <EOL> def get_defined_names ( self ) : <EOL> return [ self . name ] <EOL> def aliases ( self ) : <EOL> return { } <EOL> @ property <EOL> def level ( self ) : <EOL> return self . _level <EOL> @ property <EOL> def start_pos ( self ) : <EOL> return <NUM_LIT:0> , <NUM_LIT:0> <EOL> def paths ( self ) : <EOL> return [ [ self . name ] ] <EOL> def is_definition ( self ) : <EOL> return True <EOL> class FakeName ( tree . Name ) : <EOL> def __init__ ( self , name_str , parent = None , start_pos = ( <NUM_LIT:0> , <NUM_LIT:0> ) , is_definition = None ) : <EOL> """<STR_LIT>""" <EOL> super ( FakeName , self ) . __init__ ( tree . zero_position_modifier , name_str , start_pos ) <EOL> self . parent = parent <EOL> self . _is_definition = is_definition <EOL> def get_definition ( self ) : <EOL> return self . parent <EOL> def is_definition ( self ) : <EOL> if self . _is_definition is None : <EOL> return super ( FakeName , self ) . is_definition ( ) <EOL> else : <EOL> return self . _is_definition <EOL> class LazyName ( FakeName ) : <EOL> def __init__ ( self , name , parent_callback , is_definition = None ) : <EOL> super ( LazyName , self ) . __init__ ( name , is_definition = is_definition ) <EOL> self . _parent_callback = parent_callback <EOL> @ property <EOL> def parent ( self ) : <EOL> return self . _parent_callback ( ) <EOL> @ parent . setter <EOL> def parent ( self , value ) : <EOL> pass </s>
