sequence
stringlengths
546
16.2k
code
stringlengths
108
19.3k
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:now; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 10; 5, 14; 5, 15; 5, 16; 5, 17; 5, 38; 5, 39; 5, 40; 5, 41; 5, 42; 5, 43; 5, 44; 5, 67; 5, 68; 5, 69; 5, 70; 5, 71; 5, 81; 5, 82; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:now; 13, None; 14, comment; 15, comment; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:d; 20, call; 20, 21; 20, 26; 21, attribute; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:datetime; 24, identifier:datetime; 25, identifier:now; 26, argument_list; 26, 27; 27, keyword_argument; 27, 28; 27, 29; 28, identifier:tz; 29, attribute; 29, 30; 29, 37; 30, attribute; 30, 31; 30, 36; 31, subscript; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:days; 35, integer:0; 36, identifier:date; 37, identifier:tzinfo; 38, comment; 39, comment; 40, comment; 41, comment; 42, comment; 43, comment; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:for_total_seconds; 47, binary_operator:-; 47, 48; 47, 49; 47, 50; 48, identifier:d; 49, line_continuation:\; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:d; 53, identifier:replace; 54, argument_list; 54, 55; 54, 58; 54, 61; 54, 64; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:hour; 57, integer:0; 58, keyword_argument; 58, 59; 58, 60; 59, identifier:minute; 60, integer:0; 61, keyword_argument; 61, 62; 61, 63; 62, identifier:second; 63, integer:0; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:microsecond; 66, integer:0; 67, comment; 68, comment; 69, comment; 70, comment; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:msm; 74, binary_operator:/; 74, 75; 74, 80; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:for_total_seconds; 78, identifier:total_seconds; 79, argument_list; 80, integer:60; 81, comment; 82, if_statement; 82, 83; 82, 102; 82, 103; 82, 104; 82, 105; 82, 106; 82, 107; 82, 108; 82, 109; 82, 137; 82, 138; 82, 139; 82, 140; 82, 141; 82, 142; 82, 143; 82, 184; 83, comparison_operator:==; 83, 84; 83, 96; 84, call; 84, 85; 84, 94; 85, attribute; 85, 86; 85, 93; 86, attribute; 86, 87; 86, 92; 87, subscript; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:days; 91, integer:0; 92, identifier:date; 93, identifier:strftime; 94, argument_list; 94, 95; 95, string:"%Y-%m-%dZ"; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:d; 99, identifier:strftime; 100, argument_list; 100, 101; 101, string:"%Y-%m-%dZ"; 102, comment; 103, comment; 104, comment; 105, comment; 106, comment; 107, comment; 108, comment; 109, block; 109, 110; 109, 135; 110, for_statement; 110, 111; 110, 112; 110, 119; 111, identifier:timestep; 112, attribute; 112, 113; 112, 118; 113, subscript; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:self; 116, identifier:days; 117, integer:0; 118, identifier:timesteps; 119, block; 119, 120; 119, 129; 119, 130; 119, 131; 120, if_statement; 120, 121; 120, 126; 120, 127; 121, comparison_operator:>; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:timestep; 124, identifier:name; 125, identifier:msm; 126, comment; 127, block; 127, 128; 128, break_statement; 129, comment; 130, comment; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:now; 134, identifier:timestep; 135, return_statement; 135, 136; 136, identifier:now; 137, comment; 138, comment; 139, comment; 140, comment; 141, comment; 142, comment; 143, elif_clause; 143, 144; 143, 167; 143, 168; 144, comparison_operator:<; 144, 145; 144, 166; 145, call; 145, 146; 145, 165; 146, attribute; 146, 147; 146, 164; 147, call; 147, 148; 147, 149; 148, identifier:abs; 149, argument_list; 149, 150; 150, binary_operator:-; 150, 151; 150, 163; 151, attribute; 151, 152; 151, 162; 152, subscript; 152, 153; 152, 160; 153, attribute; 153, 154; 153, 159; 154, subscript; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:self; 157, identifier:days; 158, integer:0; 159, identifier:timesteps; 160, unary_operator:-; 160, 161; 161, integer:1; 162, identifier:date; 163, identifier:d; 164, identifier:total_seconds; 165, argument_list; 166, integer:14400; 167, comment; 168, block; 168, 169; 168, 182; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:timestep_to_return; 172, subscript; 172, 173; 172, 180; 173, attribute; 173, 174; 173, 179; 174, subscript; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:self; 177, identifier:days; 178, integer:0; 179, identifier:timesteps; 180, unary_operator:-; 180, 181; 181, integer:1; 182, return_statement; 182, 183; 183, identifier:timestep_to_return; 184, else_clause; 184, 185; 185, block; 185, 186; 186, return_statement; 186, 187; 187, False
def now(self): """ Function to return just the current timestep from this forecast """ # From the comments in issue 19: forecast.days[0] is dated for the # previous day shortly after midnight now = None # Set the time now to be in the same time zone as the first timestep in # the forecast. This shouldn't cause problems with daylight savings as # the change is far enough after midnight. d = datetime.datetime.now(tz=self.days[0].date.tzinfo) # d is something like datetime.datetime(2019, 1, 19, 17, 5, 28, 337439) # d.replace(...) is datetime.datetime(2019, 1, 19, 0, 0) # for_total_seconds is then: datetime.timedelta(seconds=61528, # microseconds=337439) # In this example, this is (17*60*60) + (5*60) + 28 = 61528 # this is the number of seconds through the day for_total_seconds = d - \ d.replace(hour=0, minute=0, second=0, microsecond=0) # In the example time, # for_total_seconds.total_seconds() = 61528 + 0.337439 # This is the number of seconds after midnight # msm is then the number of minutes after midnight msm = for_total_seconds.total_seconds() / 60 # If the date now and the date in the forecast are the same, proceed if self.days[0].date.strftime("%Y-%m-%dZ") == d.strftime("%Y-%m-%dZ"): # We have determined that the date in the forecast and the date now # are the same. # # Now, test if timestep.name is larger than the number of minutes # since midnight for each timestep. # The timestep we keep is the one with the largest timestep.name # which is less than the number of minutes since midnight for timestep in self.days[0].timesteps: if timestep.name > msm: # break here stops the for loop break # now is assigned to the last timestep that did not break the # loop now = timestep return now # Bodge to get around problems near midnight: # Previous method does not account for the end of the month. The test # trying to be evaluated is that the absolute difference between the # last timestep of the first day and the current time is less than 4 # hours. 4 hours is because the final timestep of the previous day is # for 21:00 elif abs(self.days[0].timesteps[-1].date - d).total_seconds() < 14400: # This is verbose to check that the returned data makes sense timestep_to_return = self.days[0].timesteps[-1] return timestep_to_return else: return False
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:from_dict; 3, parameters; 3, 4; 3, 5; 4, identifier:cls; 5, identifier:d; 6, block; 6, 7; 6, 9; 6, 15; 6, 19; 6, 25; 6, 26; 6, 110; 6, 111; 6, 193; 6, 194; 6, 207; 6, 215; 6, 226; 6, 227; 6, 233; 6, 248; 6, 253; 6, 259; 6, 274; 6, 279; 6, 280; 6, 287; 6, 294; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:main_memory; 12, call; 12, 13; 12, 14; 13, identifier:MainMemory; 14, argument_list; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:caches; 18, dictionary; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:referred_caches; 22, call; 22, 23; 22, 24; 23, identifier:set; 24, argument_list; 25, comment; 26, for_statement; 26, 27; 26, 30; 26, 35; 27, pattern_list; 27, 28; 27, 29; 28, identifier:name; 29, identifier:conf; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:d; 33, identifier:items; 34, argument_list; 35, block; 35, 36; 35, 68; 35, 82; 35, 96; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 41; 38, subscript; 38, 39; 38, 40; 39, identifier:caches; 40, identifier:name; 41, call; 41, 42; 41, 43; 42, identifier:Cache; 43, argument_list; 43, 44; 43, 47; 44, keyword_argument; 44, 45; 44, 46; 45, identifier:name; 46, identifier:name; 47, dictionary_splat; 47, 48; 48, dictionary_comprehension; 48, 49; 48, 52; 48, 61; 49, pair; 49, 50; 49, 51; 50, identifier:k; 51, identifier:v; 52, for_in_clause; 52, 53; 52, 56; 53, pattern_list; 53, 54; 53, 55; 54, identifier:k; 55, identifier:v; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:conf; 59, identifier:items; 60, argument_list; 61, if_clause; 61, 62; 62, comparison_operator:not; 62, 63; 62, 64; 63, identifier:k; 64, list:['store_to', 'load_from', 'victims_to']; 64, 65; 64, 66; 64, 67; 65, string:'store_to'; 66, string:'load_from'; 67, string:'victims_to'; 68, if_statement; 68, 69; 68, 72; 69, comparison_operator:in; 69, 70; 69, 71; 70, string:'store_to'; 71, identifier:conf; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:referred_caches; 77, identifier:add; 78, argument_list; 78, 79; 79, subscript; 79, 80; 79, 81; 80, identifier:conf; 81, string:'store_to'; 82, if_statement; 82, 83; 82, 86; 83, comparison_operator:in; 83, 84; 83, 85; 84, string:'load_from'; 85, identifier:conf; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:referred_caches; 91, identifier:add; 92, argument_list; 92, 93; 93, subscript; 93, 94; 93, 95; 94, identifier:conf; 95, string:'load_from'; 96, if_statement; 96, 97; 96, 100; 97, comparison_operator:in; 97, 98; 97, 99; 98, string:'victims_to'; 99, identifier:conf; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:referred_caches; 105, identifier:add; 106, argument_list; 106, 107; 107, subscript; 107, 108; 107, 109; 108, identifier:conf; 109, string:'victims_to'; 110, comment; 111, for_statement; 111, 112; 111, 115; 111, 120; 112, pattern_list; 112, 113; 112, 114; 113, identifier:name; 114, identifier:conf; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:d; 118, identifier:items; 119, argument_list; 120, block; 120, 121; 120, 145; 120, 169; 121, if_statement; 121, 122; 121, 131; 122, boolean_operator:and; 122, 123; 122, 126; 123, comparison_operator:in; 123, 124; 123, 125; 124, string:'store_to'; 125, identifier:conf; 126, comparison_operator:is; 126, 127; 126, 130; 127, subscript; 127, 128; 127, 129; 128, identifier:conf; 129, string:'store_to'; 130, None; 131, block; 131, 132; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 139; 134, attribute; 134, 135; 134, 138; 135, subscript; 135, 136; 135, 137; 136, identifier:caches; 137, identifier:name; 138, identifier:set_store_to; 139, argument_list; 139, 140; 140, subscript; 140, 141; 140, 142; 141, identifier:caches; 142, subscript; 142, 143; 142, 144; 143, identifier:conf; 144, string:'store_to'; 145, if_statement; 145, 146; 145, 155; 146, boolean_operator:and; 146, 147; 146, 150; 147, comparison_operator:in; 147, 148; 147, 149; 148, string:'load_from'; 149, identifier:conf; 150, comparison_operator:is; 150, 151; 150, 154; 151, subscript; 151, 152; 151, 153; 152, identifier:conf; 153, string:'load_from'; 154, None; 155, block; 155, 156; 156, expression_statement; 156, 157; 157, call; 157, 158; 157, 163; 158, attribute; 158, 159; 158, 162; 159, subscript; 159, 160; 159, 161; 160, identifier:caches; 161, identifier:name; 162, identifier:set_load_from; 163, argument_list; 163, 164; 164, subscript; 164, 165; 164, 166; 165, identifier:caches; 166, subscript; 166, 167; 166, 168; 167, identifier:conf; 168, string:'load_from'; 169, if_statement; 169, 170; 169, 179; 170, boolean_operator:and; 170, 171; 170, 174; 171, comparison_operator:in; 171, 172; 171, 173; 172, string:'victims_to'; 173, identifier:conf; 174, comparison_operator:is; 174, 175; 174, 178; 175, subscript; 175, 176; 175, 177; 176, identifier:conf; 177, string:'victims_to'; 178, None; 179, block; 179, 180; 180, expression_statement; 180, 181; 181, call; 181, 182; 181, 187; 182, attribute; 182, 183; 182, 186; 183, subscript; 183, 184; 183, 185; 184, identifier:caches; 185, identifier:name; 186, identifier:set_victims_to; 187, argument_list; 187, 188; 188, subscript; 188, 189; 188, 190; 189, identifier:caches; 190, subscript; 190, 191; 190, 192; 191, identifier:conf; 192, string:'victims_to'; 193, comment; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:first_level; 197, binary_operator:-; 197, 198; 197, 206; 198, call; 198, 199; 198, 200; 199, identifier:set; 200, argument_list; 200, 201; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:d; 204, identifier:keys; 205, argument_list; 206, identifier:referred_caches; 207, assert_statement; 207, 208; 207, 214; 208, comparison_operator:==; 208, 209; 208, 213; 209, call; 209, 210; 209, 211; 210, identifier:len; 211, argument_list; 211, 212; 212, identifier:first_level; 213, integer:1; 214, string:"Unable to find first cache level."; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:first_level; 218, subscript; 218, 219; 218, 220; 219, identifier:caches; 220, subscript; 220, 221; 220, 225; 221, call; 221, 222; 221, 223; 222, identifier:list; 223, argument_list; 223, 224; 224, identifier:first_level; 225, integer:0; 226, comment; 227, expression_statement; 227, 228; 228, assignment; 228, 229; 228, 230; 229, identifier:last_level_load; 230, assignment; 230, 231; 230, 232; 231, identifier:c; 232, identifier:first_level; 233, while_statement; 233, 234; 233, 237; 234, comparison_operator:is; 234, 235; 234, 236; 235, identifier:c; 236, None; 237, block; 237, 238; 237, 242; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 241; 240, identifier:last_level_load; 241, identifier:c; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 245; 244, identifier:c; 245, attribute; 245, 246; 245, 247; 246, identifier:c; 247, identifier:load_from; 248, assert_statement; 248, 249; 248, 252; 249, comparison_operator:is; 249, 250; 249, 251; 250, identifier:last_level_load; 251, None; 252, string:"Unable to find last cache level."; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 256; 255, identifier:last_level_store; 256, assignment; 256, 257; 256, 258; 257, identifier:c; 258, identifier:first_level; 259, while_statement; 259, 260; 259, 263; 260, comparison_operator:is; 260, 261; 260, 262; 261, identifier:c; 262, None; 263, block; 263, 264; 263, 268; 264, expression_statement; 264, 265; 265, assignment; 265, 266; 265, 267; 266, identifier:last_level_store; 267, identifier:c; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 271; 270, identifier:c; 271, attribute; 271, 272; 271, 273; 272, identifier:c; 273, identifier:store_to; 274, assert_statement; 274, 275; 274, 278; 275, comparison_operator:is; 275, 276; 275, 277; 276, identifier:last_level_store; 277, None; 278, string:"Unable to find last cache level."; 279, comment; 280, expression_statement; 280, 281; 281, call; 281, 282; 281, 285; 282, attribute; 282, 283; 282, 284; 283, identifier:main_memory; 284, identifier:load_to; 285, argument_list; 285, 286; 286, identifier:last_level_load; 287, expression_statement; 287, 288; 288, call; 288, 289; 288, 292; 289, attribute; 289, 290; 289, 291; 290, identifier:main_memory; 291, identifier:store_from; 292, argument_list; 292, 293; 293, identifier:last_level_store; 294, return_statement; 294, 295; 295, expression_list; 295, 296; 295, 301; 295, 302; 296, call; 296, 297; 296, 298; 297, identifier:cls; 298, argument_list; 298, 299; 298, 300; 299, identifier:first_level; 300, identifier:main_memory; 301, identifier:caches; 302, identifier:main_memory
def from_dict(cls, d): """Create cache hierarchy from dictionary.""" main_memory = MainMemory() caches = {} referred_caches = set() # First pass, create all named caches and collect references for name, conf in d.items(): caches[name] = Cache(name=name, **{k: v for k, v in conf.items() if k not in ['store_to', 'load_from', 'victims_to']}) if 'store_to' in conf: referred_caches.add(conf['store_to']) if 'load_from' in conf: referred_caches.add(conf['load_from']) if 'victims_to' in conf: referred_caches.add(conf['victims_to']) # Second pass, connect caches for name, conf in d.items(): if 'store_to' in conf and conf['store_to'] is not None: caches[name].set_store_to(caches[conf['store_to']]) if 'load_from' in conf and conf['load_from'] is not None: caches[name].set_load_from(caches[conf['load_from']]) if 'victims_to' in conf and conf['victims_to'] is not None: caches[name].set_victims_to(caches[conf['victims_to']]) # Find first level (not target of any load_from or store_to) first_level = set(d.keys()) - referred_caches assert len(first_level) == 1, "Unable to find first cache level." first_level = caches[list(first_level)[0]] # Find last level caches (has no load_from or store_to target) last_level_load = c = first_level while c is not None: last_level_load = c c = c.load_from assert last_level_load is not None, "Unable to find last cache level." last_level_store = c = first_level while c is not None: last_level_store = c c = c.store_to assert last_level_store is not None, "Unable to find last cache level." # Set main memory connections main_memory.load_to(last_level_load) main_memory.store_from(last_level_store) return cls(first_level, main_memory), caches, main_memory
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_object_getattr; 3, parameters; 3, 4; 3, 5; 4, identifier:obj; 5, identifier:field; 6, block; 6, 7; 6, 9; 6, 10; 6, 14; 6, 15; 6, 42; 6, 43; 7, expression_statement; 7, 8; 8, string:'''Attribute getter for the objects to operate on. This function can be overridden in classes or instances of Query, Filter, and Order. Thus, a custom function to extract values to attributes can be specified, and the system can remain agnostic to the client's data model, without loosing query power. For example, the default implementation works with attributes and items:: def _object_getattr(obj, field): # check whether this key is an attribute if hasattr(obj, field): value = getattr(obj, field) # if not, perhaps it is an item (raw dicts, etc) elif field in obj: value = obj[field] # return whatever we've got. return value Or consider a more complex, application-specific structure:: def _object_getattr(version, field): if field in ['key', 'committed', 'created', 'hash']: return getattr(version, field) else: return version.attributes[field]['value'] '''; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:value; 13, None; 14, comment; 15, if_statement; 15, 16; 15, 21; 15, 30; 15, 31; 16, call; 16, 17; 16, 18; 17, identifier:hasattr; 18, argument_list; 18, 19; 18, 20; 19, identifier:obj; 20, identifier:field; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:value; 25, call; 25, 26; 25, 27; 26, identifier:getattr; 27, argument_list; 27, 28; 27, 29; 28, identifier:obj; 29, identifier:field; 30, comment; 31, elif_clause; 31, 32; 31, 35; 32, comparison_operator:in; 32, 33; 32, 34; 33, identifier:field; 34, identifier:obj; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:value; 39, subscript; 39, 40; 39, 41; 40, identifier:obj; 41, identifier:field; 42, comment; 43, return_statement; 43, 44; 44, identifier:value
def _object_getattr(obj, field): '''Attribute getter for the objects to operate on. This function can be overridden in classes or instances of Query, Filter, and Order. Thus, a custom function to extract values to attributes can be specified, and the system can remain agnostic to the client's data model, without loosing query power. For example, the default implementation works with attributes and items:: def _object_getattr(obj, field): # check whether this key is an attribute if hasattr(obj, field): value = getattr(obj, field) # if not, perhaps it is an item (raw dicts, etc) elif field in obj: value = obj[field] # return whatever we've got. return value Or consider a more complex, application-specific structure:: def _object_getattr(version, field): if field in ['key', 'committed', 'created', 'hash']: return getattr(version, field) else: return version.attributes[field]['value'] ''' # TODO: consider changing this to raise an exception if no value is found. value = None # check whether this key is an attribute if hasattr(obj, field): value = getattr(obj, field) # if not, perhaps it is an item (raw dicts, etc) elif field in obj: value = obj[field] # return whatever we've got. return value
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:sorted; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:cls; 5, identifier:items; 6, identifier:orders; 7, block; 7, 8; 7, 10; 8, expression_statement; 8, 9; 9, string:'''Returns the elements in `items` sorted according to `orders`'''; 10, return_statement; 10, 11; 11, call; 11, 12; 11, 13; 12, identifier:sorted; 13, argument_list; 13, 14; 13, 15; 14, identifier:items; 15, keyword_argument; 15, 16; 15, 17; 16, identifier:cmp; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:cls; 20, identifier:multipleOrderComparison; 21, argument_list; 21, 22; 22, identifier:orders
def sorted(cls, items, orders): '''Returns the elements in `items` sorted according to `orders`''' return sorted(items, cmp=cls.multipleOrderComparison(orders))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_unique_fields; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 43; 6, expression_statement; 6, 7; 7, comment; 8, for_statement; 8, 9; 8, 10; 8, 15; 9, identifier:unique_together; 10, attribute; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:_meta; 14, identifier:unique_together; 15, block; 15, 16; 16, if_statement; 16, 17; 16, 20; 17, comparison_operator:in; 17, 18; 17, 19; 18, string:'sort_order'; 19, identifier:unique_together; 20, block; 20, 21; 20, 28; 20, 35; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:unique_fields; 24, call; 24, 25; 24, 26; 25, identifier:list; 26, argument_list; 26, 27; 27, identifier:unique_together; 28, expression_statement; 28, 29; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:unique_fields; 32, identifier:remove; 33, argument_list; 33, 34; 34, string:'sort_order'; 35, return_statement; 35, 36; 36, list_comprehension; 36, 37; 36, 40; 37, binary_operator:%; 37, 38; 37, 39; 38, string:'%s_id'; 39, identifier:f; 40, for_in_clause; 40, 41; 40, 42; 41, identifier:f; 42, identifier:unique_fields; 43, return_statement; 43, 44; 44, list:[]
def get_unique_fields(self): """List field names that are unique_together with `sort_order`.""" for unique_together in self._meta.unique_together: if 'sort_order' in unique_together: unique_fields = list(unique_together) unique_fields.remove('sort_order') return ['%s_id' % f for f in unique_fields] return []
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_is_sort_order_unique_together_with_something; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 16; 5, 34; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:unique_together; 11, attribute; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:self; 14, identifier:_meta; 15, identifier:unique_together; 16, for_statement; 16, 17; 16, 18; 16, 19; 17, identifier:fields; 18, identifier:unique_together; 19, block; 19, 20; 20, if_statement; 20, 21; 20, 31; 21, boolean_operator:and; 21, 22; 21, 25; 22, comparison_operator:in; 22, 23; 22, 24; 23, string:'sort_order'; 24, identifier:fields; 25, comparison_operator:>; 25, 26; 25, 30; 26, call; 26, 27; 26, 28; 27, identifier:len; 28, argument_list; 28, 29; 29, identifier:fields; 30, integer:1; 31, block; 31, 32; 32, return_statement; 32, 33; 33, True; 34, return_statement; 34, 35; 35, False
def _is_sort_order_unique_together_with_something(self): """ Is the sort_order field unique_together with something """ unique_together = self._meta.unique_together for fields in unique_together: if 'sort_order' in fields and len(fields) > 1: return True return False
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_update; 3, parameters; 3, 4; 4, identifier:qs; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, try_statement; 8, 9; 8, 35; 9, block; 9, 10; 10, with_statement; 10, 11; 10, 18; 11, with_clause; 11, 12; 12, with_item; 12, 13; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:transaction; 16, identifier:atomic; 17, argument_list; 18, block; 18, 19; 19, expression_statement; 19, 20; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:qs; 23, identifier:update; 24, argument_list; 24, 25; 25, keyword_argument; 25, 26; 25, 27; 26, identifier:sort_order; 27, binary_operator:+; 27, 28; 27, 34; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:models; 31, identifier:F; 32, argument_list; 32, 33; 33, string:'sort_order'; 34, integer:1; 35, except_clause; 35, 36; 35, 37; 36, identifier:IntegrityError; 37, block; 37, 38; 38, for_statement; 38, 39; 38, 40; 38, 46; 39, identifier:obj; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:qs; 43, identifier:order_by; 44, argument_list; 44, 45; 45, string:'-sort_order'; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, call; 48, 49; 48, 61; 49, attribute; 49, 50; 49, 60; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:qs; 53, identifier:filter; 54, argument_list; 54, 55; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:pk; 57, attribute; 57, 58; 57, 59; 58, identifier:obj; 59, identifier:pk; 60, identifier:update; 61, argument_list; 61, 62; 62, keyword_argument; 62, 63; 62, 64; 63, identifier:sort_order; 64, binary_operator:+; 64, 65; 64, 71; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:models; 68, identifier:F; 69, argument_list; 69, 70; 70, string:'sort_order'; 71, integer:1
def _update(qs): """ Increment the sort_order in a queryset. Handle IntegrityErrors caused by unique constraints. """ try: with transaction.atomic(): qs.update(sort_order=models.F('sort_order') + 1) except IntegrityError: for obj in qs.order_by('-sort_order'): qs.filter(pk=obj.pk).update(sort_order=models.F('sort_order') + 1)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:set_orders; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:object_pks; 6, block; 6, 7; 6, 9; 6, 20; 6, 44; 6, 45; 6, 46; 6, 61; 6, 62; 6, 63; 6, 64; 6, 65; 6, 113; 6, 166; 6, 167; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:objects_to_sort; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:filter; 16, argument_list; 16, 17; 17, keyword_argument; 17, 18; 17, 19; 18, identifier:pk__in; 19, identifier:object_pks; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:max_value; 23, subscript; 23, 24; 23, 43; 24, call; 24, 25; 24, 36; 25, attribute; 25, 26; 25, 35; 26, call; 26, 27; 26, 34; 27, attribute; 27, 28; 27, 33; 28, attribute; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:model; 32, identifier:objects; 33, identifier:all; 34, argument_list; 35, identifier:aggregate; 36, argument_list; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:models; 40, identifier:Max; 41, argument_list; 41, 42; 42, string:'sort_order'; 43, string:'sort_order__max'; 44, comment; 45, comment; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:orders; 49, call; 49, 50; 49, 51; 50, identifier:list; 51, argument_list; 51, 52; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:objects_to_sort; 55, identifier:values_list; 56, argument_list; 56, 57; 56, 58; 57, string:'sort_order'; 58, keyword_argument; 58, 59; 58, 60; 59, identifier:flat; 60, True; 61, comment; 62, comment; 63, comment; 64, comment; 65, if_statement; 65, 66; 65, 75; 66, comparison_operator:!=; 66, 67; 66, 71; 67, call; 67, 68; 67, 69; 68, identifier:len; 69, argument_list; 69, 70; 70, identifier:orders; 71, call; 71, 72; 71, 73; 72, identifier:len; 73, argument_list; 73, 74; 74, identifier:object_pks; 75, block; 75, 76; 75, 91; 75, 108; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:pks; 79, call; 79, 80; 79, 81; 80, identifier:set; 81, argument_list; 81, 82; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:objects_to_sort; 85, identifier:values_list; 86, argument_list; 86, 87; 86, 88; 87, string:'pk'; 88, keyword_argument; 88, 89; 88, 90; 89, identifier:flat; 90, True; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:message; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, string:'The following object_pks are not in this queryset: {}'; 97, identifier:format; 98, argument_list; 98, 99; 99, list_comprehension; 99, 100; 99, 101; 99, 104; 100, identifier:pk; 101, for_in_clause; 101, 102; 101, 103; 102, identifier:pk; 103, identifier:object_pks; 104, if_clause; 104, 105; 105, comparison_operator:not; 105, 106; 105, 107; 106, identifier:pk; 107, identifier:pks; 108, raise_statement; 108, 109; 109, call; 109, 110; 109, 111; 110, identifier:TypeError; 111, argument_list; 111, 112; 112, identifier:message; 113, with_statement; 113, 114; 113, 121; 114, with_clause; 114, 115; 115, with_item; 115, 116; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:transaction; 119, identifier:atomic; 120, argument_list; 121, block; 121, 122; 121, 138; 122, expression_statement; 122, 123; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:objects_to_sort; 126, identifier:update; 127, argument_list; 127, 128; 128, keyword_argument; 128, 129; 128, 130; 129, identifier:sort_order; 130, binary_operator:+; 130, 131; 130, 137; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:models; 134, identifier:F; 135, argument_list; 135, 136; 136, string:'sort_order'; 137, identifier:max_value; 138, for_statement; 138, 139; 138, 142; 138, 147; 138, 148; 138, 149; 139, pattern_list; 139, 140; 139, 141; 140, identifier:pk; 141, identifier:order; 142, call; 142, 143; 142, 144; 143, identifier:zip; 144, argument_list; 144, 145; 144, 146; 145, identifier:object_pks; 146, identifier:orders; 147, comment; 148, comment; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, call; 151, 152; 151, 162; 152, attribute; 152, 153; 152, 161; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:filter; 157, argument_list; 157, 158; 158, keyword_argument; 158, 159; 158, 160; 159, identifier:pk; 160, identifier:pk; 161, identifier:update; 162, argument_list; 162, 163; 163, keyword_argument; 163, 164; 163, 165; 164, identifier:sort_order; 165, identifier:order; 166, comment; 167, return_statement; 167, 168; 168, identifier:objects_to_sort
def set_orders(self, object_pks): """ Perform a mass update of sort_orders across the full queryset. Accepts a list, object_pks, of the intended order for the objects. Works as follows: - Compile a list of all sort orders in the queryset. Leave out anything that isn't in the object_pks list - this deals with pagination and any inconsistencies. - Get the maximum among all model object sort orders. Update the queryset to add it to all the existing sort order values. This lifts them 'out of the way' of unique_together clashes when setting the intended sort orders. - Set the sort order on each object. Use only sort_order values that the objects had before calling this method, so they get rearranged in place. Performs O(n) queries. """ objects_to_sort = self.filter(pk__in=object_pks) max_value = self.model.objects.all().aggregate( models.Max('sort_order') )['sort_order__max'] # Call list() on the values right away, so they don't get affected by the # update() later (since values_list() is lazy). orders = list(objects_to_sort.values_list('sort_order', flat=True)) # Check there are no unrecognised entries in the object_pks list. If so, # throw an error. We only have to check that they're the same length because # orders is built using only entries in object_pks, and all the pks are unique, # so if their lengths are the same, the elements must match up exactly. if len(orders) != len(object_pks): pks = set(objects_to_sort.values_list('pk', flat=True)) message = 'The following object_pks are not in this queryset: {}'.format( [pk for pk in object_pks if pk not in pks] ) raise TypeError(message) with transaction.atomic(): objects_to_sort.update(sort_order=models.F('sort_order') + max_value) for pk, order in zip(object_pks, orders): # Use update() to save a query per item and dodge the insertion sort # code in save(). self.filter(pk=pk).update(sort_order=order) # Return the operated-on queryset for convenience. return objects_to_sort
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:merge_records; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:env; 5, identifier:model_name; 6, identifier:record_ids; 7, identifier:target_record_id; 8, default_parameter; 8, 9; 8, 10; 9, identifier:field_spec; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:method; 13, string:'orm'; 14, default_parameter; 14, 15; 14, 16; 15, identifier:delete; 16, True; 17, default_parameter; 17, 18; 17, 19; 18, identifier:exclude_columns; 19, None; 20, block; 20, 21; 20, 23; 20, 32; 20, 41; 20, 55; 20, 63; 20, 70; 20, 77; 20, 89; 20, 90; 20, 107; 20, 112; 20, 121; 21, expression_statement; 21, 22; 22, comment; 23, if_statement; 23, 24; 23, 27; 24, comparison_operator:is; 24, 25; 24, 26; 25, identifier:exclude_columns; 26, None; 27, block; 27, 28; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:exclude_columns; 31, list:[]; 32, if_statement; 32, 33; 32, 36; 33, comparison_operator:is; 33, 34; 33, 35; 34, identifier:field_spec; 35, None; 36, block; 36, 37; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:field_spec; 40, dictionary; 41, if_statement; 41, 42; 41, 47; 42, call; 42, 43; 42, 44; 43, identifier:isinstance; 44, argument_list; 44, 45; 44, 46; 45, identifier:record_ids; 46, identifier:list; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:record_ids; 51, call; 51, 52; 51, 53; 52, identifier:tuple; 53, argument_list; 53, 54; 54, identifier:record_ids; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:args0; 58, tuple; 58, 59; 58, 60; 58, 61; 58, 62; 59, identifier:env; 60, identifier:model_name; 61, identifier:record_ids; 62, identifier:target_record_id; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:args; 66, binary_operator:+; 66, 67; 66, 68; 67, identifier:args0; 68, tuple; 68, 69; 69, identifier:exclude_columns; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:args2; 73, binary_operator:+; 73, 74; 73, 75; 74, identifier:args0; 75, tuple; 75, 76; 76, identifier:field_spec; 77, if_statement; 77, 78; 77, 81; 78, comparison_operator:in; 78, 79; 78, 80; 79, identifier:target_record_id; 80, identifier:record_ids; 81, block; 81, 82; 82, raise_statement; 82, 83; 83, call; 83, 84; 83, 85; 84, identifier:Exception; 85, argument_list; 85, 86; 86, concatenated_string; 86, 87; 86, 88; 87, string:"You can't put the target record in the list or "; 88, string:"records to be merged."; 89, comment; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:record_ids; 93, attribute; 93, 94; 93, 106; 94, call; 94, 95; 94, 105; 95, attribute; 95, 96; 95, 104; 96, call; 96, 97; 96, 102; 97, attribute; 97, 98; 97, 101; 98, subscript; 98, 99; 98, 100; 99, identifier:env; 100, identifier:model_name; 101, identifier:browse; 102, argument_list; 102, 103; 103, identifier:record_ids; 104, identifier:exists; 105, argument_list; 106, identifier:ids; 107, if_statement; 107, 108; 107, 110; 108, not_operator; 108, 109; 109, identifier:record_ids; 110, block; 110, 111; 111, return_statement; 112, expression_statement; 112, 113; 113, call; 113, 114; 113, 115; 114, identifier:_change_generic; 115, argument_list; 115, 116; 115, 118; 116, list_splat; 116, 117; 117, identifier:args; 118, keyword_argument; 118, 119; 118, 120; 119, identifier:method; 120, identifier:method; 121, if_statement; 121, 122; 121, 125; 121, 185; 122, comparison_operator:==; 122, 123; 122, 124; 123, identifier:method; 124, string:'orm'; 125, block; 125, 126; 125, 132; 125, 138; 125, 144; 125, 150; 125, 151; 125, 166; 125, 174; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 129; 128, identifier:_change_many2one_refs_orm; 129, argument_list; 129, 130; 130, list_splat; 130, 131; 131, identifier:args; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 135; 134, identifier:_change_many2many_refs_orm; 135, argument_list; 135, 136; 136, list_splat; 136, 137; 137, identifier:args; 138, expression_statement; 138, 139; 139, call; 139, 140; 139, 141; 140, identifier:_change_reference_refs_orm; 141, argument_list; 141, 142; 142, list_splat; 142, 143; 143, identifier:args; 144, expression_statement; 144, 145; 145, call; 145, 146; 145, 147; 146, identifier:_change_translations_orm; 147, argument_list; 147, 148; 148, list_splat; 148, 149; 149, identifier:args; 150, comment; 151, with_statement; 151, 152; 151, 159; 152, with_clause; 152, 153; 153, with_item; 153, 154; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:env; 157, identifier:norecompute; 158, argument_list; 159, block; 159, 160; 160, expression_statement; 160, 161; 161, call; 161, 162; 161, 163; 162, identifier:_adjust_merged_values_orm; 163, argument_list; 163, 164; 164, list_splat; 164, 165; 165, identifier:args2; 166, expression_statement; 166, 167; 167, call; 167, 168; 167, 173; 168, attribute; 168, 169; 168, 172; 169, subscript; 169, 170; 169, 171; 170, identifier:env; 171, identifier:model_name; 172, identifier:recompute; 173, argument_list; 174, if_statement; 174, 175; 174, 176; 175, identifier:delete; 176, block; 176, 177; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 180; 179, identifier:_delete_records_orm; 180, argument_list; 180, 181; 180, 182; 180, 183; 180, 184; 181, identifier:env; 182, identifier:model_name; 183, identifier:record_ids; 184, identifier:target_record_id; 185, else_clause; 185, 186; 186, block; 186, 187; 186, 193; 186, 199; 186, 205; 186, 206; 187, expression_statement; 187, 188; 188, call; 188, 189; 188, 190; 189, identifier:_change_foreign_key_refs; 190, argument_list; 190, 191; 191, list_splat; 191, 192; 192, identifier:args; 193, expression_statement; 193, 194; 194, call; 194, 195; 194, 196; 195, identifier:_change_reference_refs_sql; 196, argument_list; 196, 197; 197, list_splat; 197, 198; 198, identifier:args; 199, expression_statement; 199, 200; 200, call; 200, 201; 200, 202; 201, identifier:_change_translations_sql; 202, argument_list; 202, 203; 203, list_splat; 203, 204; 204, identifier:args; 205, comment; 206, if_statement; 206, 207; 206, 208; 207, identifier:delete; 208, block; 208, 209; 209, expression_statement; 209, 210; 210, call; 210, 211; 210, 212; 211, identifier:_delete_records_sql; 212, argument_list; 212, 213; 212, 214; 212, 215; 212, 216; 213, identifier:env; 214, identifier:model_name; 215, identifier:record_ids; 216, identifier:target_record_id
def merge_records(env, model_name, record_ids, target_record_id, field_spec=None, method='orm', delete=True, exclude_columns=None): """Merge several records into the target one. NOTE: This should be executed in end migration scripts for assuring that all the possible relations are loaded and changed. Tested on v10/v11. :param env: Environment variable :param model_name: Name of the model of the records to merge :param record_ids: List of IDS of records that are going to be merged. :param target_record_id: ID of the record where the rest records are going to be merge in. :param field_spec: Dictionary with field names as keys and forced operation to perform as values. If a field is not present here, default operation will be performed. See _adjust_merged_values_orm method doc for all the available operators. :param method: Specify how to perform operations. By default or specifying 'orm', operations will be performed with ORM, maybe slower, but safer, as related and computed fields will be recomputed on changes, and all constraints will be checked. :param delete: If set, the source ids will be unlinked. :exclude_columns: list of tuples (table, column) that will be ignored. """ if exclude_columns is None: exclude_columns = [] if field_spec is None: field_spec = {} if isinstance(record_ids, list): record_ids = tuple(record_ids) args0 = (env, model_name, record_ids, target_record_id) args = args0 + (exclude_columns, ) args2 = args0 + (field_spec, ) if target_record_id in record_ids: raise Exception("You can't put the target record in the list or " "records to be merged.") # Check which records to be merged exist record_ids = env[model_name].browse(record_ids).exists().ids if not record_ids: return _change_generic(*args, method=method) if method == 'orm': _change_many2one_refs_orm(*args) _change_many2many_refs_orm(*args) _change_reference_refs_orm(*args) _change_translations_orm(*args) # TODO: serialized fields with env.norecompute(): _adjust_merged_values_orm(*args2) env[model_name].recompute() if delete: _delete_records_orm(env, model_name, record_ids, target_record_id) else: _change_foreign_key_refs(*args) _change_reference_refs_sql(*args) _change_translations_sql(*args) # TODO: Adjust values of the merged records through SQL if delete: _delete_records_sql(env, model_name, record_ids, target_record_id)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_get_existing_records; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:cr; 5, identifier:fp; 6, identifier:module_name; 7, block; 7, 8; 7, 10; 7, 148; 8, expression_statement; 8, 9; 9, comment; 10, function_definition; 10, 11; 10, 12; 10, 17; 11, function_name:yield_element; 12, parameters; 12, 13; 12, 14; 13, identifier:node; 14, default_parameter; 14, 15; 14, 16; 15, identifier:path; 16, None; 17, block; 17, 18; 18, if_statement; 18, 19; 18, 27; 18, 107; 19, comparison_operator:not; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:node; 22, identifier:tag; 23, list:['openerp', 'odoo', 'data']; 23, 24; 23, 25; 23, 26; 24, string:'openerp'; 25, string:'odoo'; 26, string:'data'; 27, block; 27, 28; 27, 83; 27, 98; 27, 104; 28, if_statement; 28, 29; 28, 34; 29, comparison_operator:==; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:node; 32, identifier:tag; 33, string:'record'; 34, block; 34, 35; 34, 43; 34, 66; 34, 76; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:xmlid; 38, subscript; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:node; 41, identifier:attrib; 42, string:'id'; 43, if_statement; 43, 44; 43, 47; 43, 52; 44, comparison_operator:not; 44, 45; 44, 46; 45, string:'.'; 46, identifier:xmlid; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:module; 51, identifier:module_name; 52, else_clause; 52, 53; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 59; 56, pattern_list; 56, 57; 56, 58; 57, identifier:module; 58, identifier:xmlid; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:xmlid; 62, identifier:split; 63, argument_list; 63, 64; 63, 65; 64, string:'.'; 65, integer:1; 66, expression_statement; 66, 67; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:cr; 70, identifier:execute; 71, argument_list; 71, 72; 71, 73; 72, string:'select id from ir_model_data where module=%s and name=%s'; 73, tuple; 73, 74; 73, 75; 74, identifier:module; 75, identifier:xmlid; 76, if_statement; 76, 77; 76, 81; 77, not_operator; 77, 78; 78, attribute; 78, 79; 78, 80; 79, identifier:cr; 80, identifier:rowcount; 81, block; 81, 82; 82, return_statement; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:result; 86, call; 86, 87; 86, 88; 87, identifier:StringIO; 88, argument_list; 88, 89; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:etree; 92, identifier:tostring; 93, argument_list; 93, 94; 93, 95; 94, identifier:path; 95, keyword_argument; 95, 96; 95, 97; 96, identifier:encoding; 97, string:'unicode'; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:result; 102, identifier:name; 103, None; 104, expression_statement; 104, 105; 105, yield; 105, 106; 106, identifier:result; 107, else_clause; 107, 108; 108, block; 108, 109; 109, for_statement; 109, 110; 109, 111; 109, 112; 110, identifier:child; 111, identifier:node; 112, block; 112, 113; 113, for_statement; 113, 114; 113, 115; 113, 144; 114, identifier:value; 115, call; 115, 116; 115, 117; 116, identifier:yield_element; 117, argument_list; 117, 118; 117, 119; 118, identifier:child; 119, conditional_expression:if; 119, 120; 119, 132; 119, 133; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:etree; 123, identifier:SubElement; 124, argument_list; 124, 125; 124, 126; 124, 129; 125, identifier:path; 126, attribute; 126, 127; 126, 128; 127, identifier:node; 128, identifier:tag; 129, attribute; 129, 130; 129, 131; 130, identifier:node; 131, identifier:attrib; 132, identifier:path; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:etree; 136, identifier:Element; 137, argument_list; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:node; 140, identifier:tag; 141, attribute; 141, 142; 141, 143; 142, identifier:node; 143, identifier:attrib; 144, block; 144, 145; 145, expression_statement; 145, 146; 146, yield; 146, 147; 147, identifier:value; 148, return_statement; 148, 149; 149, call; 149, 150; 149, 151; 150, identifier:yield_element; 151, argument_list; 151, 152; 152, call; 152, 153; 152, 161; 153, attribute; 153, 154; 153, 160; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:etree; 157, identifier:parse; 158, argument_list; 158, 159; 159, identifier:fp; 160, identifier:getroot; 161, argument_list
def _get_existing_records(cr, fp, module_name): """yield file like objects per 'leaf' node in the xml file that exists. This is for not trying to create a record with partial data in case the record was removed in the database.""" def yield_element(node, path=None): if node.tag not in ['openerp', 'odoo', 'data']: if node.tag == 'record': xmlid = node.attrib['id'] if '.' not in xmlid: module = module_name else: module, xmlid = xmlid.split('.', 1) cr.execute( 'select id from ir_model_data where module=%s and name=%s', (module, xmlid) ) if not cr.rowcount: return result = StringIO(etree.tostring(path, encoding='unicode')) result.name = None yield result else: for child in node: for value in yield_element( child, etree.SubElement(path, node.tag, node.attrib) if path else etree.Element(node.tag, node.attrib) ): yield value return yield_element(etree.parse(fp).getroot())
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:check_ab; 3, parameters; 3, 4; 3, 5; 4, identifier:ab; 5, identifier:verb; 6, block; 6, 7; 6, 9; 6, 10; 6, 28; 6, 29; 6, 30; 6, 70; 6, 99; 6, 100; 6, 111; 6, 112; 6, 120; 6, 121; 6, 129; 6, 130; 6, 131; 6, 167; 6, 168; 6, 200; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, try_statement; 10, 11; 10, 19; 11, block; 11, 12; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:ab; 15, call; 15, 16; 15, 17; 16, identifier:int; 17, argument_list; 17, 18; 18, identifier:ab; 19, except_clause; 19, 20; 19, 21; 20, identifier:VariableCatch; 21, block; 21, 22; 21, 27; 22, expression_statement; 22, 23; 23, call; 23, 24; 23, 25; 24, identifier:print; 25, argument_list; 25, 26; 26, string:'* ERROR :: <ab> must be an integer'; 27, raise_statement; 28, comment; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:pab; 33, list:[11, 12, 13, 14, 15, 16, 21, 22, 23, 24, 25, 26, 31, 32, 33, 34, 35, 36, 41, 42, 43, 44, 45, 46, 51, 52, 53, 54, 55, 56, 61, 62, 63, 64, 65, 66]; 33, 34; 33, 35; 33, 36; 33, 37; 33, 38; 33, 39; 33, 40; 33, 41; 33, 42; 33, 43; 33, 44; 33, 45; 33, 46; 33, 47; 33, 48; 33, 49; 33, 50; 33, 51; 33, 52; 33, 53; 33, 54; 33, 55; 33, 56; 33, 57; 33, 58; 33, 59; 33, 60; 33, 61; 33, 62; 33, 63; 33, 64; 33, 65; 33, 66; 33, 67; 33, 68; 33, 69; 34, integer:11; 35, integer:12; 36, integer:13; 37, integer:14; 38, integer:15; 39, integer:16; 40, integer:21; 41, integer:22; 42, integer:23; 43, integer:24; 44, integer:25; 45, integer:26; 46, integer:31; 47, integer:32; 48, integer:33; 49, integer:34; 50, integer:35; 51, integer:36; 52, integer:41; 53, integer:42; 54, integer:43; 55, integer:44; 56, integer:45; 57, integer:46; 58, integer:51; 59, integer:52; 60, integer:53; 61, integer:54; 62, integer:55; 63, integer:56; 64, integer:61; 65, integer:62; 66, integer:63; 67, integer:64; 68, integer:65; 69, integer:66; 70, if_statement; 70, 71; 70, 74; 71, comparison_operator:not; 71, 72; 71, 73; 72, identifier:ab; 73, identifier:pab; 74, block; 74, 75; 74, 94; 75, expression_statement; 75, 76; 76, call; 76, 77; 76, 78; 77, identifier:print; 78, argument_list; 78, 79; 79, binary_operator:+; 79, 80; 79, 90; 80, binary_operator:+; 80, 81; 80, 89; 81, binary_operator:+; 81, 82; 81, 88; 82, binary_operator:+; 82, 83; 82, 84; 83, string:'* ERROR :: <ab> must be one of: '; 84, call; 84, 85; 84, 86; 85, identifier:str; 86, argument_list; 86, 87; 87, identifier:pab; 88, string:';'; 89, string:' <ab> provided: '; 90, call; 90, 91; 90, 92; 91, identifier:str; 92, argument_list; 92, 93; 93, identifier:ab; 94, raise_statement; 94, 95; 95, call; 95, 96; 95, 97; 96, identifier:ValueError; 97, argument_list; 97, 98; 98, string:'ab'; 99, comment; 100, if_statement; 100, 101; 100, 104; 101, comparison_operator:>; 101, 102; 101, 103; 102, identifier:verb; 103, integer:2; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, call; 106, 107; 106, 108; 107, identifier:print; 108, argument_list; 108, 109; 108, 110; 109, string:" Input ab : "; 110, identifier:ab; 111, comment; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:msrc; 115, comparison_operator:>; 115, 116; 115, 119; 116, binary_operator:%; 116, 117; 116, 118; 117, identifier:ab; 118, integer:10; 119, integer:3; 120, comment; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:mrec; 124, comparison_operator:>; 124, 125; 124, 128; 125, binary_operator://; 125, 126; 125, 127; 126, identifier:ab; 127, integer:10; 128, integer:3; 129, comment; 130, comment; 131, if_statement; 131, 132; 131, 133; 131, 161; 132, identifier:mrec; 133, block; 133, 134; 134, if_statement; 134, 135; 134, 136; 134, 137; 134, 145; 135, identifier:msrc; 136, comment; 137, block; 137, 138; 137, 144; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:ab_calc; 141, binary_operator:-; 141, 142; 141, 143; 142, identifier:ab; 143, integer:33; 144, comment; 145, else_clause; 145, 146; 145, 147; 146, comment; 147, block; 147, 148; 147, 160; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:ab_calc; 151, binary_operator:+; 151, 152; 151, 157; 152, binary_operator:*; 152, 153; 152, 156; 153, binary_operator:%; 153, 154; 153, 155; 154, identifier:ab; 155, integer:10; 156, integer:10; 157, binary_operator://; 157, 158; 157, 159; 158, identifier:ab; 159, integer:10; 160, comment; 161, else_clause; 161, 162; 162, block; 162, 163; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:ab_calc; 166, identifier:ab; 167, comment; 168, if_statement; 168, 169; 168, 172; 169, comparison_operator:>; 169, 170; 169, 171; 170, identifier:verb; 171, integer:2; 172, block; 172, 173; 173, if_statement; 173, 174; 173, 179; 173, 192; 174, comparison_operator:in; 174, 175; 174, 176; 175, identifier:ab; 176, list:[36, 63]; 176, 177; 176, 178; 177, integer:36; 178, integer:63; 179, block; 179, 180; 180, expression_statement; 180, 181; 181, call; 181, 182; 181, 183; 182, identifier:print; 183, argument_list; 183, 184; 184, binary_operator:+; 184, 185; 184, 191; 185, binary_operator:+; 185, 186; 185, 187; 186, string:"\n> <ab> IS "; 187, call; 187, 188; 187, 189; 188, identifier:str; 189, argument_list; 189, 190; 190, identifier:ab; 191, string:" WHICH IS ZERO; returning"; 192, else_clause; 192, 193; 193, block; 193, 194; 194, expression_statement; 194, 195; 195, call; 195, 196; 195, 197; 196, identifier:print; 197, argument_list; 197, 198; 197, 199; 198, string:" Calculated ab : "; 199, identifier:ab_calc; 200, return_statement; 200, 201; 201, expression_list; 201, 202; 201, 203; 201, 204; 202, identifier:ab_calc; 203, identifier:msrc; 204, identifier:mrec
def check_ab(ab, verb): r"""Check source-receiver configuration. This check-function is called from one of the modelling routines in :mod:`model`. Consult these modelling routines for a detailed description of the input parameters. Parameters ---------- ab : int Source-receiver configuration. verb : {0, 1, 2, 3, 4} Level of verbosity. Returns ------- ab_calc : int Adjusted source-receiver configuration using reciprocity. msrc, mrec : bool If True, src/rec is magnetic; if False, src/rec is electric. """ # Try to cast ab into an integer try: ab = int(ab) except VariableCatch: print('* ERROR :: <ab> must be an integer') raise # Check src and rec orientation (<ab> for alpha-beta) # pab: all possible values that <ab> can take pab = [11, 12, 13, 14, 15, 16, 21, 22, 23, 24, 25, 26, 31, 32, 33, 34, 35, 36, 41, 42, 43, 44, 45, 46, 51, 52, 53, 54, 55, 56, 61, 62, 63, 64, 65, 66] if ab not in pab: print('* ERROR :: <ab> must be one of: ' + str(pab) + ';' + ' <ab> provided: ' + str(ab)) raise ValueError('ab') # Print input <ab> if verb > 2: print(" Input ab : ", ab) # Check if src and rec are magnetic or electric msrc = ab % 10 > 3 # If True: magnetic src mrec = ab // 10 > 3 # If True: magnetic rec # If rec is magnetic, switch <ab> using reciprocity. if mrec: if msrc: # G^mm_ab(s, r, e, z) = -G^ee_ab(s, r, -z, -e) ab_calc = ab - 33 # -30 : mrec->erec; -3: msrc->esrc else: # G^me_ab(s, r, e, z) = -G^em_ba(r, s, e, z) ab_calc = ab % 10*10 + ab // 10 # Swap alpha/beta else: ab_calc = ab # Print actual calculated <ab> if verb > 2: if ab in [36, 63]: print("\n> <ab> IS "+str(ab)+" WHICH IS ZERO; returning") else: print(" Calculated ab : ", ab_calc) return ab_calc, msrc, mrec
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:check_opt; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:opt; 5, identifier:loop; 6, identifier:ht; 7, identifier:htarg; 8, identifier:verb; 9, block; 9, 10; 9, 12; 9, 13; 9, 17; 9, 41; 9, 42; 9, 46; 9, 62; 9, 93; 9, 94; 9, 137; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:use_ne_eval; 16, False; 17, if_statement; 17, 18; 17, 21; 18, comparison_operator:==; 18, 19; 18, 20; 19, identifier:opt; 20, string:'parallel'; 21, block; 21, 22; 22, if_statement; 22, 23; 22, 24; 22, 31; 23, identifier:numexpr; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:use_ne_eval; 28, attribute; 28, 29; 28, 30; 29, identifier:numexpr; 30, identifier:evaluate; 31, elif_clause; 31, 32; 31, 35; 32, comparison_operator:>; 32, 33; 32, 34; 33, identifier:verb; 34, integer:0; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 39; 38, identifier:print; 39, argument_list; 39, 40; 40, identifier:numexpr_msg; 41, comment; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:lagged_splined_fht; 45, False; 46, if_statement; 46, 47; 46, 50; 47, comparison_operator:==; 47, 48; 47, 49; 48, identifier:ht; 49, string:'fht'; 50, block; 50, 51; 51, if_statement; 51, 52; 51, 57; 52, comparison_operator:!=; 52, 53; 52, 56; 53, subscript; 53, 54; 53, 55; 54, identifier:htarg; 55, integer:1; 56, integer:0; 57, block; 57, 58; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:lagged_splined_fht; 61, True; 62, if_statement; 62, 63; 62, 70; 62, 79; 63, boolean_operator:or; 63, 64; 63, 69; 64, comparison_operator:in; 64, 65; 64, 66; 65, identifier:ht; 66, list:['hqwe', 'hquad']; 66, 67; 66, 68; 67, string:'hqwe'; 68, string:'hquad'; 69, identifier:lagged_splined_fht; 70, block; 70, 71; 70, 75; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:loop_freq; 74, True; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:loop_off; 78, False; 79, else_clause; 79, 80; 80, block; 80, 81; 80, 87; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:loop_off; 84, comparison_operator:==; 84, 85; 84, 86; 85, identifier:loop; 86, string:'off'; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:loop_freq; 90, comparison_operator:==; 90, 91; 90, 92; 91, identifier:loop; 92, string:'freq'; 93, comment; 94, if_statement; 94, 95; 94, 98; 95, comparison_operator:>; 95, 96; 95, 97; 96, identifier:verb; 97, integer:2; 98, block; 98, 99; 98, 114; 99, if_statement; 99, 100; 99, 101; 99, 107; 100, identifier:use_ne_eval; 101, block; 101, 102; 102, expression_statement; 102, 103; 103, call; 103, 104; 103, 105; 104, identifier:print; 105, argument_list; 105, 106; 106, string:" Kernel Opt. : Use parallel"; 107, else_clause; 107, 108; 108, block; 108, 109; 109, expression_statement; 109, 110; 110, call; 110, 111; 110, 112; 111, identifier:print; 112, argument_list; 112, 113; 113, string:" Kernel Opt. : None"; 114, if_statement; 114, 115; 114, 116; 114, 122; 114, 130; 115, identifier:loop_off; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, call; 118, 119; 118, 120; 119, identifier:print; 120, argument_list; 120, 121; 121, string:" Loop over : Offsets"; 122, elif_clause; 122, 123; 122, 124; 123, identifier:loop_freq; 124, block; 124, 125; 125, expression_statement; 125, 126; 126, call; 126, 127; 126, 128; 127, identifier:print; 128, argument_list; 128, 129; 129, string:" Loop over : Frequencies"; 130, else_clause; 130, 131; 131, block; 131, 132; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 135; 134, identifier:print; 135, argument_list; 135, 136; 136, string:" Loop over : None (all vectorized)"; 137, return_statement; 137, 138; 138, expression_list; 138, 139; 138, 140; 138, 141; 139, identifier:use_ne_eval; 140, identifier:loop_freq; 141, identifier:loop_off
def check_opt(opt, loop, ht, htarg, verb): r"""Check optimization parameters. This check-function is called from one of the modelling routines in :mod:`model`. Consult these modelling routines for a detailed description of the input parameters. Parameters ---------- opt : {None, 'parallel'} Optimization flag; use ``numexpr`` or not. loop : {None, 'freq', 'off'} Loop flag. ht : str Flag to choose the Hankel transform. htarg : array_like, Depends on the value for ``ht``. verb : {0, 1, 2, 3, 4} Level of verbosity. Returns ------- use_ne_eval : bool Boolean if to use ``numexpr``. loop_freq : bool Boolean if to loop over frequencies. loop_off : bool Boolean if to loop over offsets. """ # Check optimization flag use_ne_eval = False if opt == 'parallel': if numexpr: use_ne_eval = numexpr.evaluate elif verb > 0: print(numexpr_msg) # Define if to loop over frequencies or over offsets lagged_splined_fht = False if ht == 'fht': if htarg[1] != 0: lagged_splined_fht = True if ht in ['hqwe', 'hquad'] or lagged_splined_fht: loop_freq = True loop_off = False else: loop_off = loop == 'off' loop_freq = loop == 'freq' # If verbose, print optimization information if verb > 2: if use_ne_eval: print(" Kernel Opt. : Use parallel") else: print(" Kernel Opt. : None") if loop_off: print(" Loop over : Offsets") elif loop_freq: print(" Loop over : Frequencies") else: print(" Loop over : None (all vectorized)") return use_ne_eval, loop_freq, loop_off
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:get_abs; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 4, identifier:msrc; 5, identifier:mrec; 6, identifier:srcazm; 7, identifier:srcdip; 8, identifier:recazm; 9, identifier:recdip; 10, identifier:verb; 11, block; 11, 12; 11, 14; 11, 15; 11, 36; 11, 43; 11, 76; 11, 77; 11, 93; 11, 94; 11, 105; 11, 162; 11, 163; 11, 174; 11, 233; 11, 234; 11, 245; 11, 302; 11, 303; 11, 314; 11, 373; 11, 374; 11, 384; 11, 385; 11, 399; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:ab_calc; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:np; 21, identifier:array; 22, argument_list; 22, 23; 23, list:[[11, 12, 13], [21, 22, 23], [31, 32, 33]]; 23, 24; 23, 28; 23, 32; 24, list:[11, 12, 13]; 24, 25; 24, 26; 24, 27; 25, integer:11; 26, integer:12; 27, integer:13; 28, list:[21, 22, 23]; 28, 29; 28, 30; 28, 31; 29, integer:21; 30, integer:22; 31, integer:23; 32, list:[31, 32, 33]; 32, 33; 32, 34; 32, 35; 33, integer:31; 34, integer:32; 35, integer:33; 36, if_statement; 36, 37; 36, 38; 37, identifier:msrc; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, augmented_assignment:+=; 40, 41; 40, 42; 41, identifier:ab_calc; 42, integer:3; 43, if_statement; 43, 44; 43, 45; 44, identifier:mrec; 45, block; 45, 46; 45, 50; 45, 51; 46, expression_statement; 46, 47; 47, augmented_assignment:+=; 47, 48; 47, 49; 48, identifier:ab_calc; 49, integer:30; 50, comment; 51, if_statement; 51, 52; 51, 53; 51, 54; 51, 60; 52, identifier:msrc; 53, comment; 54, block; 54, 55; 54, 59; 55, expression_statement; 55, 56; 56, augmented_assignment:-=; 56, 57; 56, 58; 57, identifier:ab_calc; 58, integer:33; 59, comment; 60, else_clause; 60, 61; 60, 62; 61, comment; 62, block; 62, 63; 62, 75; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:ab_calc; 66, binary_operator:+; 66, 67; 66, 72; 67, binary_operator:*; 67, 68; 67, 71; 68, binary_operator:%; 68, 69; 68, 70; 69, identifier:ab_calc; 70, integer:10; 71, integer:10; 72, binary_operator://; 72, 73; 72, 74; 73, identifier:ab_calc; 74, integer:10; 75, comment; 76, comment; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:bab; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:np; 83, identifier:asarray; 84, argument_list; 84, 85; 84, 90; 85, binary_operator:+; 85, 86; 85, 89; 86, binary_operator:*; 86, 87; 86, 88; 87, identifier:ab_calc; 88, integer:0; 89, integer:1; 90, keyword_argument; 90, 91; 90, 92; 91, identifier:dtype; 92, identifier:bool; 93, comment; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:check; 97, subscript; 97, 98; 97, 104; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:np; 101, identifier:atleast_1d; 102, argument_list; 102, 103; 103, identifier:srcazm; 104, integer:0; 105, if_statement; 105, 106; 105, 120; 105, 121; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:np; 109, identifier:allclose; 110, argument_list; 110, 111; 110, 119; 111, binary_operator:%; 111, 112; 111, 113; 112, identifier:srcazm; 113, parenthesized_expression; 113, 114; 114, binary_operator:/; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:np; 117, identifier:pi; 118, integer:2; 119, integer:0; 120, comment; 121, block; 121, 122; 122, if_statement; 122, 123; 122, 139; 122, 140; 122, 150; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:np; 126, identifier:isclose; 127, argument_list; 127, 128; 127, 138; 128, binary_operator:%; 128, 129; 128, 137; 129, binary_operator://; 129, 130; 129, 131; 130, identifier:check; 131, parenthesized_expression; 131, 132; 132, binary_operator:/; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:np; 135, identifier:pi; 136, integer:2; 137, integer:2; 138, integer:0; 139, comment; 140, block; 140, 141; 140, 149; 141, expression_statement; 141, 142; 142, augmented_assignment:*=; 142, 143; 142, 148; 143, subscript; 143, 144; 143, 145; 143, 147; 144, identifier:bab; 145, slice; 145, 146; 146, colon; 147, integer:1; 148, False; 149, comment; 150, else_clause; 150, 151; 150, 152; 151, comment; 152, block; 152, 153; 152, 161; 153, expression_statement; 153, 154; 154, augmented_assignment:*=; 154, 155; 154, 160; 155, subscript; 155, 156; 155, 157; 155, 159; 156, identifier:bab; 157, slice; 157, 158; 158, colon; 159, integer:0; 160, False; 161, comment; 162, comment; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:check; 166, subscript; 166, 167; 166, 173; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:np; 170, identifier:atleast_1d; 171, argument_list; 171, 172; 172, identifier:srcdip; 173, integer:0; 174, if_statement; 174, 175; 174, 189; 174, 190; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:np; 178, identifier:allclose; 179, argument_list; 179, 180; 179, 188; 180, binary_operator:%; 180, 181; 180, 182; 181, identifier:srcdip; 182, parenthesized_expression; 182, 183; 183, binary_operator:/; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:np; 186, identifier:pi; 187, integer:2; 188, integer:0; 189, comment; 190, block; 190, 191; 191, if_statement; 191, 192; 191, 208; 191, 209; 191, 219; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:np; 195, identifier:isclose; 196, argument_list; 196, 197; 196, 207; 197, binary_operator:%; 197, 198; 197, 206; 198, binary_operator://; 198, 199; 198, 200; 199, identifier:check; 200, parenthesized_expression; 200, 201; 201, binary_operator:/; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:np; 204, identifier:pi; 205, integer:2; 206, integer:2; 207, integer:0; 208, comment; 209, block; 209, 210; 209, 218; 210, expression_statement; 210, 211; 211, augmented_assignment:*=; 211, 212; 211, 217; 212, subscript; 212, 213; 212, 214; 212, 216; 213, identifier:bab; 214, slice; 214, 215; 215, colon; 216, integer:2; 217, False; 218, comment; 219, else_clause; 219, 220; 219, 221; 220, comment; 221, block; 221, 222; 221, 232; 222, expression_statement; 222, 223; 223, augmented_assignment:*=; 223, 224; 223, 231; 224, subscript; 224, 225; 224, 226; 224, 228; 225, identifier:bab; 226, slice; 226, 227; 227, colon; 228, slice; 228, 229; 228, 230; 229, colon; 230, integer:2; 231, False; 232, comment; 233, comment; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 237; 236, identifier:check; 237, subscript; 237, 238; 237, 244; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:np; 241, identifier:atleast_1d; 242, argument_list; 242, 243; 243, identifier:recazm; 244, integer:0; 245, if_statement; 245, 246; 245, 260; 245, 261; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:np; 249, identifier:allclose; 250, argument_list; 250, 251; 250, 259; 251, binary_operator:%; 251, 252; 251, 253; 252, identifier:recazm; 253, parenthesized_expression; 253, 254; 254, binary_operator:/; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:np; 257, identifier:pi; 258, integer:2; 259, integer:0; 260, comment; 261, block; 261, 262; 262, if_statement; 262, 263; 262, 279; 262, 280; 262, 290; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:np; 266, identifier:isclose; 267, argument_list; 267, 268; 267, 278; 268, binary_operator:%; 268, 269; 268, 277; 269, binary_operator://; 269, 270; 269, 271; 270, identifier:check; 271, parenthesized_expression; 271, 272; 272, binary_operator:/; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:np; 275, identifier:pi; 276, integer:2; 277, integer:2; 278, integer:0; 279, comment; 280, block; 280, 281; 280, 289; 281, expression_statement; 281, 282; 282, augmented_assignment:*=; 282, 283; 282, 288; 283, subscript; 283, 284; 283, 285; 283, 286; 284, identifier:bab; 285, integer:1; 286, slice; 286, 287; 287, colon; 288, False; 289, comment; 290, else_clause; 290, 291; 290, 292; 291, comment; 292, block; 292, 293; 292, 301; 293, expression_statement; 293, 294; 294, augmented_assignment:*=; 294, 295; 294, 300; 295, subscript; 295, 296; 295, 297; 295, 298; 296, identifier:bab; 297, integer:0; 298, slice; 298, 299; 299, colon; 300, False; 301, comment; 302, comment; 303, expression_statement; 303, 304; 304, assignment; 304, 305; 304, 306; 305, identifier:check; 306, subscript; 306, 307; 306, 313; 307, call; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:np; 310, identifier:atleast_1d; 311, argument_list; 311, 312; 312, identifier:recdip; 313, integer:0; 314, if_statement; 314, 315; 314, 329; 314, 330; 315, call; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:np; 318, identifier:allclose; 319, argument_list; 319, 320; 319, 328; 320, binary_operator:%; 320, 321; 320, 322; 321, identifier:recdip; 322, parenthesized_expression; 322, 323; 323, binary_operator:/; 323, 324; 323, 327; 324, attribute; 324, 325; 324, 326; 325, identifier:np; 326, identifier:pi; 327, integer:2; 328, integer:0; 329, comment; 330, block; 330, 331; 331, if_statement; 331, 332; 331, 348; 331, 349; 331, 359; 332, call; 332, 333; 332, 336; 333, attribute; 333, 334; 333, 335; 334, identifier:np; 335, identifier:isclose; 336, argument_list; 336, 337; 336, 347; 337, binary_operator:%; 337, 338; 337, 346; 338, binary_operator://; 338, 339; 338, 340; 339, identifier:check; 340, parenthesized_expression; 340, 341; 341, binary_operator:/; 341, 342; 341, 345; 342, attribute; 342, 343; 342, 344; 343, identifier:np; 344, identifier:pi; 345, integer:2; 346, integer:2; 347, integer:0; 348, comment; 349, block; 349, 350; 349, 358; 350, expression_statement; 350, 351; 351, augmented_assignment:*=; 351, 352; 351, 357; 352, subscript; 352, 353; 352, 354; 352, 355; 353, identifier:bab; 354, integer:2; 355, slice; 355, 356; 356, colon; 357, False; 358, comment; 359, else_clause; 359, 360; 359, 361; 360, comment; 361, block; 361, 362; 361, 372; 362, expression_statement; 362, 363; 363, augmented_assignment:*=; 363, 364; 363, 371; 364, subscript; 364, 365; 364, 366; 364, 369; 365, identifier:bab; 366, slice; 366, 367; 366, 368; 367, colon; 368, integer:2; 369, slice; 369, 370; 370, colon; 371, False; 372, comment; 373, comment; 374, expression_statement; 374, 375; 375, assignment; 375, 376; 375, 377; 376, identifier:ab_calc; 377, call; 377, 378; 377, 383; 378, attribute; 378, 379; 378, 382; 379, subscript; 379, 380; 379, 381; 380, identifier:ab_calc; 381, identifier:bab; 382, identifier:ravel; 383, argument_list; 384, comment; 385, if_statement; 385, 386; 385, 389; 386, comparison_operator:>; 386, 387; 386, 388; 387, identifier:verb; 388, integer:2; 389, block; 389, 390; 390, expression_statement; 390, 391; 391, call; 391, 392; 391, 393; 392, identifier:print; 393, argument_list; 393, 394; 393, 395; 394, string:" Required ab's : "; 395, call; 395, 396; 395, 397; 396, identifier:_strvar; 397, argument_list; 397, 398; 398, identifier:ab_calc; 399, return_statement; 399, 400; 400, identifier:ab_calc
def get_abs(msrc, mrec, srcazm, srcdip, recazm, recdip, verb): r"""Get required ab's for given angles. This check-function is called from one of the modelling routines in :mod:`model`. Consult these modelling routines for a detailed description of the input parameters. Parameters ---------- msrc, mrec : bool True if src/rec is magnetic, else False. srcazm, recazm : float Horizontal source/receiver angle (azimuth). srcdip, recdip : float Vertical source/receiver angle (dip). verb : {0, 1, 2, 3, 4} Level of verbosity. Returns ------- ab_calc : array of int ab's to calculate for this bipole. """ # Get required ab's (9 at most) ab_calc = np.array([[11, 12, 13], [21, 22, 23], [31, 32, 33]]) if msrc: ab_calc += 3 if mrec: ab_calc += 30 # Switch <ab> using reciprocity. if msrc: # G^mm_ab(s, r, e, z) = -G^ee_ab(s, r, -z, -e) ab_calc -= 33 # -30 : mrec->erec; -3: msrc->esrc else: # G^me_ab(s, r, e, z) = -G^em_ba(r, s, e, z) ab_calc = ab_calc % 10*10 + ab_calc // 10 # Swap alpha/beta # Remove unnecessary ab's bab = np.asarray(ab_calc*0+1, dtype=bool) # Remove if source is x- or y-directed check = np.atleast_1d(srcazm)[0] if np.allclose(srcazm % (np.pi/2), 0): # if all angles are multiples of 90 if np.isclose(check // (np.pi/2) % 2, 0): # Multiples of pi (180) bab[:, 1] *= False # x-directed source, remove y else: # Multiples of pi/2 (90) bab[:, 0] *= False # y-directed source, remove x # Remove if source is vertical check = np.atleast_1d(srcdip)[0] if np.allclose(srcdip % (np.pi/2), 0): # if all angles are multiples of 90 if np.isclose(check // (np.pi/2) % 2, 0): # Multiples of pi (180) bab[:, 2] *= False # Horizontal, remove z else: # Multiples of pi/2 (90) bab[:, :2] *= False # Vertical, remove x/y # Remove if receiver is x- or y-directed check = np.atleast_1d(recazm)[0] if np.allclose(recazm % (np.pi/2), 0): # if all angles are multiples of 90 if np.isclose(check // (np.pi/2) % 2, 0): # Multiples of pi (180) bab[1, :] *= False # x-directed receiver, remove y else: # Multiples of pi/2 (90) bab[0, :] *= False # y-directed receiver, remove x # Remove if receiver is vertical check = np.atleast_1d(recdip)[0] if np.allclose(recdip % (np.pi/2), 0): # if all angles are multiples of 90 if np.isclose(check // (np.pi/2) % 2, 0): # Multiples of pi (180) bab[2, :] *= False # Horizontal, remove z else: # Multiples of pi/2 (90) bab[:2, :] *= False # Vertical, remove x/y # Reduce ab_calc = ab_calc[bab].ravel() # Print actual calculated <ab> if verb > 2: print(" Required ab's : ", _strvar(ab_calc)) return ab_calc
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:spline_backwards_hankel; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:ht; 5, identifier:htarg; 6, identifier:opt; 7, block; 7, 8; 7, 10; 7, 11; 7, 19; 7, 20; 7, 130; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:ht; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:ht; 17, identifier:lower; 18, argument_list; 19, comment; 20, if_statement; 20, 21; 20, 27; 20, 28; 21, comparison_operator:in; 21, 22; 21, 23; 22, identifier:ht; 23, list:['fht', 'qwe', 'hqwe']; 23, 24; 23, 25; 23, 26; 24, string:'fht'; 25, string:'qwe'; 26, string:'hqwe'; 27, comment; 28, block; 28, 29; 28, 68; 28, 69; 29, if_statement; 29, 30; 29, 33; 29, 44; 30, comparison_operator:==; 30, 31; 30, 32; 31, identifier:ht; 32, string:'fht'; 33, block; 33, 34; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:htarg; 37, call; 37, 38; 37, 39; 38, identifier:_check_targ; 39, argument_list; 39, 40; 39, 41; 40, identifier:htarg; 41, list:['fhtfilt', 'pts_per_dec']; 41, 42; 41, 43; 42, string:'fhtfilt'; 43, string:'pts_per_dec'; 44, elif_clause; 44, 45; 44, 50; 45, comparison_operator:in; 45, 46; 45, 47; 46, identifier:ht; 47, list:['qwe', 'hqwe']; 47, 48; 47, 49; 48, string:'qwe'; 49, string:'hqwe'; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:htarg; 54, call; 54, 55; 54, 56; 55, identifier:_check_targ; 56, argument_list; 56, 57; 56, 58; 57, identifier:htarg; 58, list:['rtol', 'atol', 'nquad', 'maxint', 'pts_per_dec', 'diff_quad', 'a', 'b', 'limit']; 58, 59; 58, 60; 58, 61; 58, 62; 58, 63; 58, 64; 58, 65; 58, 66; 58, 67; 59, string:'rtol'; 60, string:'atol'; 61, string:'nquad'; 62, string:'maxint'; 63, string:'pts_per_dec'; 64, string:'diff_quad'; 65, string:'a'; 66, string:'b'; 67, string:'limit'; 68, comment; 69, if_statement; 69, 70; 69, 73; 69, 74; 70, comparison_operator:==; 70, 71; 70, 72; 71, identifier:opt; 72, string:'spline'; 73, comment; 74, block; 74, 75; 74, 84; 74, 92; 74, 93; 74, 97; 74, 98; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:mesg; 78, parenthesized_expression; 78, 79; 79, binary_operator:+; 79, 80; 79, 83; 80, binary_operator:+; 80, 81; 80, 82; 81, string:"\n The use of `opt='spline'` is deprecated and will "; 82, string:"be removed\n in v2.0.0; use the corresponding "; 83, string:"setting in `htarg`."; 84, expression_statement; 84, 85; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:warnings; 88, identifier:warn; 89, argument_list; 89, 90; 89, 91; 90, identifier:mesg; 91, identifier:DeprecationWarning; 92, comment; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:opt; 96, None; 97, comment; 98, if_statement; 98, 99; 98, 102; 99, comparison_operator:not; 99, 100; 99, 101; 100, string:'pts_per_dec'; 101, identifier:htarg; 102, block; 102, 103; 103, if_statement; 103, 104; 103, 107; 103, 116; 104, comparison_operator:==; 104, 105; 104, 106; 105, identifier:ht; 106, string:'fht'; 107, block; 107, 108; 107, 115; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 113; 110, subscript; 110, 111; 110, 112; 111, identifier:htarg; 112, string:'pts_per_dec'; 113, unary_operator:-; 113, 114; 114, integer:1; 115, comment; 116, elif_clause; 116, 117; 116, 122; 117, comparison_operator:in; 117, 118; 117, 119; 118, identifier:ht; 119, list:['qwe', 'hqwe']; 119, 120; 119, 121; 120, string:'qwe'; 121, string:'hqwe'; 122, block; 122, 123; 122, 129; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 128; 125, subscript; 125, 126; 125, 127; 126, identifier:htarg; 127, string:'pts_per_dec'; 128, integer:80; 129, comment; 130, return_statement; 130, 131; 131, expression_list; 131, 132; 131, 133; 132, identifier:htarg; 133, identifier:opt
def spline_backwards_hankel(ht, htarg, opt): r"""Check opt if deprecated 'spline' is used. Returns corrected htarg, opt. r""" # Ensure ht is all lowercase ht = ht.lower() # Only relevant for 'fht' and 'hqwe', not for 'quad' if ht in ['fht', 'qwe', 'hqwe']: # Get corresponding htarg if ht == 'fht': htarg = _check_targ(htarg, ['fhtfilt', 'pts_per_dec']) elif ht in ['qwe', 'hqwe']: htarg = _check_targ(htarg, ['rtol', 'atol', 'nquad', 'maxint', 'pts_per_dec', 'diff_quad', 'a', 'b', 'limit']) # If spline (qwe, fht) or lagged (fht) if opt == 'spline': # Issue warning mesg = ("\n The use of `opt='spline'` is deprecated and will " + "be removed\n in v2.0.0; use the corresponding " + "setting in `htarg`.") warnings.warn(mesg, DeprecationWarning) # Reset opt opt = None # Check pts_per_dec; set to old default values if not given if 'pts_per_dec' not in htarg: if ht == 'fht': htarg['pts_per_dec'] = -1 # Lagged Convolution DLF elif ht in ['qwe', 'hqwe']: htarg['pts_per_dec'] = 80 # Splined QWE; old default value return htarg, opt
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 31; 2, function_name:dipole_k; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 13; 3, 16; 3, 19; 3, 22; 3, 25; 3, 28; 4, identifier:src; 5, identifier:rec; 6, identifier:depth; 7, identifier:res; 8, identifier:freq; 9, identifier:wavenumber; 10, default_parameter; 10, 11; 10, 12; 11, identifier:ab; 12, integer:11; 13, default_parameter; 13, 14; 13, 15; 14, identifier:aniso; 15, None; 16, default_parameter; 16, 17; 16, 18; 17, identifier:epermH; 18, None; 19, default_parameter; 19, 20; 19, 21; 20, identifier:epermV; 21, None; 22, default_parameter; 22, 23; 22, 24; 23, identifier:mpermH; 24, None; 25, default_parameter; 25, 26; 25, 27; 26, identifier:mpermV; 27, None; 28, default_parameter; 28, 29; 28, 30; 29, identifier:verb; 30, integer:2; 31, block; 31, 32; 31, 34; 31, 35; 31, 42; 31, 43; 31, 44; 31, 59; 31, 71; 31, 72; 31, 86; 31, 95; 31, 96; 31, 97; 31, 108; 31, 109; 31, 120; 31, 131; 31, 132; 31, 145; 31, 157; 31, 158; 31, 168; 31, 178; 31, 179; 31, 180; 31, 290; 31, 291; 31, 292; 31, 293; 31, 398; 31, 399; 31, 406; 32, expression_statement; 32, 33; 33, comment; 34, comment; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:t0; 38, call; 38, 39; 38, 40; 39, identifier:printstartfinish; 40, argument_list; 40, 41; 41, identifier:verb; 42, comment; 43, comment; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:modl; 47, call; 47, 48; 47, 49; 48, identifier:check_model; 49, argument_list; 49, 50; 49, 51; 49, 52; 49, 53; 49, 54; 49, 55; 49, 56; 49, 57; 49, 58; 50, identifier:depth; 51, identifier:res; 52, identifier:aniso; 53, identifier:epermH; 54, identifier:epermV; 55, identifier:mpermH; 56, identifier:mpermV; 57, False; 58, identifier:verb; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 70; 61, pattern_list; 61, 62; 61, 63; 61, 64; 61, 65; 61, 66; 61, 67; 61, 68; 61, 69; 62, identifier:depth; 63, identifier:res; 64, identifier:aniso; 65, identifier:epermH; 66, identifier:epermV; 67, identifier:mpermH; 68, identifier:mpermV; 69, identifier:_; 70, identifier:modl; 71, comment; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:f; 75, call; 75, 76; 75, 77; 76, identifier:check_frequency; 77, argument_list; 77, 78; 77, 79; 77, 80; 77, 81; 77, 82; 77, 83; 77, 84; 77, 85; 78, identifier:freq; 79, identifier:res; 80, identifier:aniso; 81, identifier:epermH; 82, identifier:epermV; 83, identifier:mpermH; 84, identifier:mpermV; 85, identifier:verb; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 94; 88, pattern_list; 88, 89; 88, 90; 88, 91; 88, 92; 88, 93; 89, identifier:freq; 90, identifier:etaH; 91, identifier:etaV; 92, identifier:zetaH; 93, identifier:zetaV; 94, identifier:f; 95, comment; 96, comment; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 103; 99, pattern_list; 99, 100; 99, 101; 99, 102; 100, identifier:ab_calc; 101, identifier:msrc; 102, identifier:mrec; 103, call; 103, 104; 103, 105; 104, identifier:check_ab; 105, argument_list; 105, 106; 105, 107; 106, identifier:ab; 107, identifier:verb; 108, comment; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 114; 111, pattern_list; 111, 112; 111, 113; 112, identifier:src; 113, identifier:nsrc; 114, call; 114, 115; 114, 116; 115, identifier:check_dipole; 116, argument_list; 116, 117; 116, 118; 116, 119; 117, identifier:src; 118, string:'src'; 119, identifier:verb; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 125; 122, pattern_list; 122, 123; 122, 124; 123, identifier:rec; 124, identifier:nrec; 125, call; 125, 126; 125, 127; 126, identifier:check_dipole; 127, argument_list; 127, 128; 127, 129; 127, 130; 128, identifier:rec; 129, string:'rec'; 130, identifier:verb; 131, comment; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 137; 134, pattern_list; 134, 135; 134, 136; 135, identifier:off; 136, identifier:angle; 137, call; 137, 138; 137, 139; 138, identifier:get_off_ang; 139, argument_list; 139, 140; 139, 141; 139, 142; 139, 143; 139, 144; 140, identifier:src; 141, identifier:rec; 142, identifier:nsrc; 143, identifier:nrec; 144, identifier:verb; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:factAng; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:kernel; 151, identifier:angle_factor; 152, argument_list; 152, 153; 152, 154; 152, 155; 152, 156; 153, identifier:angle; 154, identifier:ab; 155, identifier:msrc; 156, identifier:mrec; 157, comment; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 163; 160, pattern_list; 160, 161; 160, 162; 161, identifier:lsrc; 162, identifier:zsrc; 163, call; 163, 164; 163, 165; 164, identifier:get_layer_nr; 165, argument_list; 165, 166; 165, 167; 166, identifier:src; 167, identifier:depth; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 173; 170, pattern_list; 170, 171; 170, 172; 171, identifier:lrec; 172, identifier:zrec; 173, call; 173, 174; 173, 175; 174, identifier:get_layer_nr; 175, argument_list; 175, 176; 175, 177; 176, identifier:rec; 177, identifier:depth; 178, comment; 179, comment; 180, if_statement; 180, 181; 180, 195; 180, 246; 181, boolean_operator:and; 181, 182; 181, 187; 182, comparison_operator:==; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:off; 185, identifier:size; 186, integer:1; 187, comparison_operator:==; 187, 188; 187, 194; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:np; 191, identifier:ndim; 192, argument_list; 192, 193; 193, identifier:wavenumber; 194, integer:2; 195, block; 195, 196; 195, 221; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 199; 198, identifier:PJ0; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:np; 202, identifier:zeros; 203, argument_list; 203, 204; 203, 218; 204, tuple; 204, 205; 204, 208; 204, 213; 205, attribute; 205, 206; 205, 207; 206, identifier:freq; 207, identifier:size; 208, subscript; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:wavenumber; 211, identifier:shape; 212, integer:0; 213, subscript; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:wavenumber; 216, identifier:shape; 217, integer:1; 218, keyword_argument; 218, 219; 218, 220; 219, identifier:dtype; 220, identifier:complex; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:PJ1; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:np; 227, identifier:zeros; 228, argument_list; 228, 229; 228, 243; 229, tuple; 229, 230; 229, 233; 229, 238; 230, attribute; 230, 231; 230, 232; 231, identifier:freq; 232, identifier:size; 233, subscript; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:wavenumber; 236, identifier:shape; 237, integer:0; 238, subscript; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:wavenumber; 241, identifier:shape; 242, integer:1; 243, keyword_argument; 243, 244; 243, 245; 244, identifier:dtype; 245, identifier:complex; 246, else_clause; 246, 247; 247, block; 247, 248; 247, 269; 248, expression_statement; 248, 249; 249, assignment; 249, 250; 249, 251; 250, identifier:PJ0; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:np; 254, identifier:zeros; 255, argument_list; 255, 256; 255, 266; 256, tuple; 256, 257; 256, 260; 256, 263; 257, attribute; 257, 258; 257, 259; 258, identifier:freq; 259, identifier:size; 260, attribute; 260, 261; 260, 262; 261, identifier:off; 262, identifier:size; 263, attribute; 263, 264; 263, 265; 264, identifier:wavenumber; 265, identifier:size; 266, keyword_argument; 266, 267; 266, 268; 267, identifier:dtype; 268, identifier:complex; 269, expression_statement; 269, 270; 270, assignment; 270, 271; 270, 272; 271, identifier:PJ1; 272, call; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:np; 275, identifier:zeros; 276, argument_list; 276, 277; 276, 287; 277, tuple; 277, 278; 277, 281; 277, 284; 278, attribute; 278, 279; 278, 280; 279, identifier:freq; 280, identifier:size; 281, attribute; 281, 282; 281, 283; 282, identifier:off; 283, identifier:size; 284, attribute; 284, 285; 284, 286; 285, identifier:wavenumber; 286, identifier:size; 287, keyword_argument; 287, 288; 287, 289; 288, identifier:dtype; 289, identifier:complex; 290, comment; 291, comment; 292, comment; 293, if_statement; 293, 294; 293, 298; 293, 299; 294, comparison_operator:not; 294, 295; 294, 296; 295, identifier:ab_calc; 296, list:[36, ]; 296, 297; 297, integer:36; 298, comment; 299, block; 299, 300; 299, 331; 299, 332; 299, 372; 299, 381; 300, expression_statement; 300, 301; 301, assignment; 301, 302; 301, 306; 302, pattern_list; 302, 303; 302, 304; 302, 305; 303, identifier:J0; 304, identifier:J1; 305, identifier:J0b; 306, call; 306, 307; 306, 310; 307, attribute; 307, 308; 307, 309; 308, identifier:kernel; 309, identifier:wavenumber; 310, argument_list; 310, 311; 310, 312; 310, 313; 310, 314; 310, 315; 310, 316; 310, 317; 310, 318; 310, 319; 310, 320; 310, 326; 310, 327; 310, 328; 310, 329; 310, 330; 311, identifier:zsrc; 312, identifier:zrec; 313, identifier:lsrc; 314, identifier:lrec; 315, identifier:depth; 316, identifier:etaH; 317, identifier:etaV; 318, identifier:zetaH; 319, identifier:zetaV; 320, call; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:np; 323, identifier:atleast_2d; 324, argument_list; 324, 325; 325, identifier:wavenumber; 326, identifier:ab_calc; 327, False; 328, identifier:msrc; 329, identifier:mrec; 330, False; 331, comment; 332, if_statement; 332, 333; 332, 336; 333, comparison_operator:is; 333, 334; 333, 335; 334, identifier:J1; 335, None; 336, block; 336, 337; 336, 349; 337, expression_statement; 337, 338; 338, augmented_assignment:+=; 338, 339; 338, 340; 339, identifier:PJ1; 340, binary_operator:*; 340, 341; 340, 348; 341, subscript; 341, 342; 341, 343; 341, 345; 342, identifier:factAng; 343, slice; 343, 344; 344, colon; 345, attribute; 345, 346; 345, 347; 346, identifier:np; 347, identifier:newaxis; 348, identifier:J1; 349, if_statement; 349, 350; 349, 361; 349, 362; 349, 363; 350, comparison_operator:in; 350, 351; 350, 352; 351, identifier:ab; 352, list:[11, 12, 21, 22, 14, 24, 15, 25]; 352, 353; 352, 354; 352, 355; 352, 356; 352, 357; 352, 358; 352, 359; 352, 360; 353, integer:11; 354, integer:12; 355, integer:21; 356, integer:22; 357, integer:14; 358, integer:24; 359, integer:15; 360, integer:25; 361, comment; 362, comment; 363, block; 363, 364; 364, expression_statement; 364, 365; 365, augmented_assignment:/=; 365, 366; 365, 367; 366, identifier:PJ1; 367, subscript; 367, 368; 367, 369; 367, 371; 368, identifier:off; 369, slice; 369, 370; 370, colon; 371, None; 372, if_statement; 372, 373; 372, 376; 373, comparison_operator:is; 373, 374; 373, 375; 374, identifier:J0; 375, None; 376, block; 376, 377; 377, expression_statement; 377, 378; 378, augmented_assignment:+=; 378, 379; 378, 380; 379, identifier:PJ0; 380, identifier:J0; 381, if_statement; 381, 382; 381, 385; 382, comparison_operator:is; 382, 383; 382, 384; 383, identifier:J0b; 384, None; 385, block; 385, 386; 386, expression_statement; 386, 387; 387, augmented_assignment:+=; 387, 388; 387, 389; 388, identifier:PJ0; 389, binary_operator:*; 389, 390; 389, 397; 390, subscript; 390, 391; 390, 392; 390, 394; 391, identifier:factAng; 392, slice; 392, 393; 393, colon; 394, attribute; 394, 395; 394, 396; 395, identifier:np; 396, identifier:newaxis; 397, identifier:J0b; 398, comment; 399, expression_statement; 399, 400; 400, call; 400, 401; 400, 402; 401, identifier:printstartfinish; 402, argument_list; 402, 403; 402, 404; 402, 405; 403, identifier:verb; 404, identifier:t0; 405, integer:1; 406, return_statement; 406, 407; 407, expression_list; 407, 408; 407, 414; 408, call; 408, 409; 408, 412; 409, attribute; 409, 410; 409, 411; 410, identifier:np; 411, identifier:squeeze; 412, argument_list; 412, 413; 413, identifier:PJ0; 414, call; 414, 415; 414, 418; 415, attribute; 415, 416; 415, 417; 416, identifier:np; 417, identifier:squeeze; 418, argument_list; 418, 419; 419, identifier:PJ1
def dipole_k(src, rec, depth, res, freq, wavenumber, ab=11, aniso=None, epermH=None, epermV=None, mpermH=None, mpermV=None, verb=2): r"""Return the electromagnetic wavenumber-domain field. Calculate the electromagnetic wavenumber-domain field due to infinitesimal small electric or magnetic dipole source(s), measured by infinitesimal small electric or magnetic dipole receiver(s); sources and receivers are directed along the principal directions x, y, or z, and all sources are at the same depth, as well as all receivers are at the same depth. See Also -------- dipole : Electromagnetic field due to an electromagnetic source (dipoles). bipole : Electromagnetic field due to an electromagnetic source (bipoles). fem : Electromagnetic frequency-domain response. tem : Electromagnetic time-domain response. Parameters ---------- src, rec : list of floats or arrays Source and receiver coordinates (m): [x, y, z]. The x- and y-coordinates can be arrays, z is a single value. The x- and y-coordinates must have the same dimension. The x- and y-coordinates only matter for the angle-dependent factor. Sources or receivers placed on a layer interface are considered in the upper layer. depth : list Absolute layer interfaces z (m); #depth = #res - 1 (excluding +/- infinity). res : array_like Horizontal resistivities rho_h (Ohm.m); #res = #depth + 1. freq : array_like Frequencies f (Hz), used to calculate etaH/V and zetaH/V. wavenumber : array Wavenumbers lambda (1/m) ab : int, optional Source-receiver configuration, defaults to 11. +---------------+-------+------+------+------+------+------+------+ | | electric source | magnetic source | +===============+=======+======+======+======+======+======+======+ | | **x**| **y**| **z**| **x**| **y**| **z**| +---------------+-------+------+------+------+------+------+------+ | | **x** | 11 | 12 | 13 | 14 | 15 | 16 | + **electric** +-------+------+------+------+------+------+------+ | | **y** | 21 | 22 | 23 | 24 | 25 | 26 | + **receiver** +-------+------+------+------+------+------+------+ | | **z** | 31 | 32 | 33 | 34 | 35 | 36 | +---------------+-------+------+------+------+------+------+------+ | | **x** | 41 | 42 | 43 | 44 | 45 | 46 | + **magnetic** +-------+------+------+------+------+------+------+ | | **y** | 51 | 52 | 53 | 54 | 55 | 56 | + **receiver** +-------+------+------+------+------+------+------+ | | **z** | 61 | 62 | 63 | 64 | 65 | 66 | +---------------+-------+------+------+------+------+------+------+ aniso : array_like, optional Anisotropies lambda = sqrt(rho_v/rho_h) (-); #aniso = #res. Defaults to ones. epermH, epermV : array_like, optional Relative horizontal/vertical electric permittivities epsilon_h/epsilon_v (-); #epermH = #epermV = #res. Default is ones. mpermH, mpermV : array_like, optional Relative horizontal/vertical magnetic permeabilities mu_h/mu_v (-); #mpermH = #mpermV = #res. Default is ones. verb : {0, 1, 2, 3, 4}, optional Level of verbosity, default is 2: - 0: Print nothing. - 1: Print warnings. - 2: Print additional runtime and kernel calls - 3: Print additional start/stop, condensed parameter information. - 4: Print additional full parameter information Returns ------- PJ0, PJ1 : array Wavenumber-domain EM responses: - PJ0: Wavenumber-domain solution for the kernel with a Bessel function of the first kind of order zero. - PJ1: Wavenumber-domain solution for the kernel with a Bessel function of the first kind of order one. Examples -------- >>> import numpy as np >>> from empymod.model import dipole_k >>> src = [0, 0, 100] >>> rec = [5000, 0, 200] >>> depth = [0, 300, 1000, 1050] >>> res = [1e20, .3, 1, 50, 1] >>> freq = 1 >>> wavenrs = np.logspace(-3.7, -3.6, 10) >>> PJ0, PJ1 = dipole_k(src, rec, depth, res, freq, wavenrs, verb=0) >>> print(PJ0) [ -1.02638329e-08 +4.91531529e-09j -1.05289724e-08 +5.04222413e-09j -1.08009148e-08 +5.17238608e-09j -1.10798310e-08 +5.30588284e-09j -1.13658957e-08 +5.44279805e-09j -1.16592877e-08 +5.58321732e-09j -1.19601897e-08 +5.72722830e-09j -1.22687889e-08 +5.87492067e-09j -1.25852765e-08 +6.02638626e-09j -1.29098481e-08 +6.18171904e-09j] >>> print(PJ1) [ 1.79483705e-10 -6.59235332e-10j 1.88672497e-10 -6.93749344e-10j 1.98325814e-10 -7.30068377e-10j 2.08466693e-10 -7.68286748e-10j 2.19119282e-10 -8.08503709e-10j 2.30308887e-10 -8.50823701e-10j 2.42062030e-10 -8.95356636e-10j 2.54406501e-10 -9.42218177e-10j 2.67371420e-10 -9.91530051e-10j 2.80987292e-10 -1.04342036e-09j] """ # === 1. LET'S START ============ t0 = printstartfinish(verb) # === 2. CHECK INPUT ============ # Check layer parameters (isfullspace not required) modl = check_model(depth, res, aniso, epermH, epermV, mpermH, mpermV, False, verb) depth, res, aniso, epermH, epermV, mpermH, mpermV, _ = modl # Check frequency => get etaH, etaV, zetaH, and zetaV f = check_frequency(freq, res, aniso, epermH, epermV, mpermH, mpermV, verb) freq, etaH, etaV, zetaH, zetaV = f # Check src-rec configuration # => Get flags if src or rec or both are magnetic (msrc, mrec) ab_calc, msrc, mrec = check_ab(ab, verb) # Check src and rec src, nsrc = check_dipole(src, 'src', verb) rec, nrec = check_dipole(rec, 'rec', verb) # Get angle-dependent factor off, angle = get_off_ang(src, rec, nsrc, nrec, verb) factAng = kernel.angle_factor(angle, ab, msrc, mrec) # Get layer number in which src and rec reside (lsrc/lrec) lsrc, zsrc = get_layer_nr(src, depth) lrec, zrec = get_layer_nr(rec, depth) # === 3. EM-FIELD CALCULATION ============ # Pre-allocate if off.size == 1 and np.ndim(wavenumber) == 2: PJ0 = np.zeros((freq.size, wavenumber.shape[0], wavenumber.shape[1]), dtype=complex) PJ1 = np.zeros((freq.size, wavenumber.shape[0], wavenumber.shape[1]), dtype=complex) else: PJ0 = np.zeros((freq.size, off.size, wavenumber.size), dtype=complex) PJ1 = np.zeros((freq.size, off.size, wavenumber.size), dtype=complex) # If <ab> = 36 (or 63), field is zero # In `bipole` and in `dipole`, this is taken care of in `fem`. Here we # have to take care of it separately if ab_calc not in [36, ]: # Calculate wavenumber response J0, J1, J0b = kernel.wavenumber(zsrc, zrec, lsrc, lrec, depth, etaH, etaV, zetaH, zetaV, np.atleast_2d(wavenumber), ab_calc, False, msrc, mrec, False) # Collect output if J1 is not None: PJ1 += factAng[:, np.newaxis]*J1 if ab in [11, 12, 21, 22, 14, 24, 15, 25]: # Because of J2 # J2(kr) = 2/(kr)*J1(kr) - J0(kr) PJ1 /= off[:, None] if J0 is not None: PJ0 += J0 if J0b is not None: PJ0 += factAng[:, np.newaxis]*J0b # === 4. FINISHED ============ printstartfinish(verb, t0, 1) return np.squeeze(PJ0), np.squeeze(PJ1)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:plot_result; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:filt; 5, identifier:full; 6, default_parameter; 6, 7; 6, 8; 7, identifier:prntres; 8, True; 9, block; 9, 10; 9, 12; 9, 13; 9, 23; 9, 32; 9, 33; 9, 44; 9, 55; 9, 56; 9, 67; 9, 79; 9, 91; 9, 92; 9, 93; 9, 289; 9, 290; 9, 310; 9, 317; 9, 404; 9, 424; 9, 431; 9, 438; 9, 447; 9, 459; 9, 460; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, if_statement; 13, 14; 13, 16; 14, not_operator; 14, 15; 15, identifier:plt; 16, block; 16, 17; 16, 22; 17, expression_statement; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:print; 20, argument_list; 20, 21; 21, identifier:plt_msg; 22, return_statement; 23, if_statement; 23, 24; 23, 25; 24, identifier:prntres; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:print_result; 29, argument_list; 29, 30; 29, 31; 30, identifier:filt; 31, identifier:full; 32, comment; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:spacing; 36, subscript; 36, 37; 36, 40; 36, 41; 36, 43; 37, subscript; 37, 38; 37, 39; 38, identifier:full; 39, integer:2; 40, integer:0; 41, slice; 41, 42; 42, colon; 43, integer:0; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:shift; 47, subscript; 47, 48; 47, 51; 47, 52; 47, 53; 48, subscript; 48, 49; 48, 50; 49, identifier:full; 50, integer:2; 51, integer:1; 52, integer:0; 53, slice; 53, 54; 54, colon; 55, comment; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:minfield; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:np; 62, identifier:squeeze; 63, argument_list; 63, 64; 64, subscript; 64, 65; 64, 66; 65, identifier:full; 66, integer:3; 67, expression_statement; 67, 68; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:plt; 71, identifier:figure; 72, argument_list; 72, 73; 72, 74; 73, string:"Brute force result"; 74, keyword_argument; 74, 75; 74, 76; 75, identifier:figsize; 76, tuple; 76, 77; 76, 78; 77, float:9.5; 78, float:4.5; 79, expression_statement; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:plt; 83, identifier:subplots_adjust; 84, argument_list; 84, 85; 84, 88; 85, keyword_argument; 85, 86; 85, 87; 86, identifier:wspace; 87, float:.4; 88, keyword_argument; 88, 89; 88, 90; 89, identifier:bottom; 90, float:0.2; 91, comment; 92, comment; 93, if_statement; 93, 94; 93, 105; 94, boolean_operator:or; 94, 95; 94, 100; 95, comparison_operator:>; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:spacing; 98, identifier:size; 99, integer:1; 100, comparison_operator:>; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:shift; 103, identifier:size; 104, integer:1; 105, block; 105, 106; 105, 113; 105, 177; 106, expression_statement; 106, 107; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:plt; 110, identifier:subplot; 111, argument_list; 111, 112; 112, integer:121; 113, if_statement; 113, 114; 113, 119; 113, 120; 113, 149; 114, comparison_operator:==; 114, 115; 114, 118; 115, subscript; 115, 116; 115, 117; 116, identifier:full; 117, integer:4; 118, integer:0; 119, comment; 120, block; 120, 121; 120, 128; 120, 132; 120, 141; 121, expression_statement; 121, 122; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:plt; 125, identifier:title; 126, argument_list; 126, 127; 127, string:"Minimal recovered fields"; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:ylabel; 131, string:'Minimal recovered amplitude (log10)'; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:field; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:np; 138, identifier:log10; 139, argument_list; 139, 140; 140, identifier:minfield; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:cmap; 144, attribute; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:plt; 147, identifier:cm; 148, identifier:viridis; 149, else_clause; 149, 150; 149, 151; 150, comment; 151, block; 151, 152; 151, 159; 151, 163; 151, 169; 152, expression_statement; 152, 153; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:plt; 156, identifier:title; 157, argument_list; 157, 158; 158, string:"Maximum recovered r"; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:ylabel; 162, string:'Maximum recovered r'; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:field; 166, binary_operator:/; 166, 167; 166, 168; 167, integer:1; 168, identifier:minfield; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:cmap; 172, attribute; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:plt; 175, identifier:cm; 176, identifier:viridis_r; 177, if_statement; 177, 178; 177, 183; 177, 184; 177, 207; 177, 237; 178, comparison_operator:==; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:shift; 181, identifier:size; 182, integer:1; 183, comment; 184, block; 184, 185; 184, 193; 184, 200; 185, expression_statement; 185, 186; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:plt; 189, identifier:plot; 190, argument_list; 190, 191; 190, 192; 191, identifier:spacing; 192, identifier:field; 193, expression_statement; 193, 194; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:plt; 197, identifier:xlabel; 198, argument_list; 198, 199; 199, string:'Spacing'; 200, expression_statement; 200, 201; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:plt; 204, identifier:ylabel; 205, argument_list; 205, 206; 206, identifier:ylabel; 207, elif_clause; 207, 208; 207, 213; 207, 214; 208, comparison_operator:==; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:spacing; 211, identifier:size; 212, integer:1; 213, comment; 214, block; 214, 215; 214, 223; 214, 230; 215, expression_statement; 215, 216; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:plt; 219, identifier:plot; 220, argument_list; 220, 221; 220, 222; 221, identifier:shift; 222, identifier:field; 223, expression_statement; 223, 224; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:plt; 227, identifier:xlabel; 228, argument_list; 228, 229; 229, string:'Shift'; 230, expression_statement; 230, 231; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:plt; 234, identifier:ylabel; 235, argument_list; 235, 236; 236, identifier:ylabel; 237, else_clause; 237, 238; 237, 239; 238, comment; 239, block; 239, 240; 239, 257; 239, 269; 239, 276; 239, 283; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:field; 243, call; 243, 244; 243, 249; 244, attribute; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:np; 247, identifier:ma; 248, identifier:masked_where; 249, argument_list; 249, 250; 249, 256; 250, call; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:np; 253, identifier:isinf; 254, argument_list; 254, 255; 255, identifier:minfield; 256, identifier:field; 257, expression_statement; 257, 258; 258, call; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:plt; 261, identifier:pcolormesh; 262, argument_list; 262, 263; 262, 264; 262, 265; 262, 266; 263, identifier:shift; 264, identifier:spacing; 265, identifier:field; 266, keyword_argument; 266, 267; 266, 268; 267, identifier:cmap; 268, identifier:cmap; 269, expression_statement; 269, 270; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:plt; 273, identifier:ylabel; 274, argument_list; 274, 275; 275, string:'Spacing'; 276, expression_statement; 276, 277; 277, call; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:plt; 280, identifier:xlabel; 281, argument_list; 281, 282; 282, string:'Shift'; 283, expression_statement; 283, 284; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:plt; 287, identifier:colorbar; 288, argument_list; 289, comment; 290, if_statement; 290, 291; 290, 302; 291, boolean_operator:or; 291, 292; 291, 297; 292, comparison_operator:>; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:spacing; 295, identifier:size; 296, integer:1; 297, comparison_operator:>; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:shift; 300, identifier:size; 301, integer:1; 302, block; 302, 303; 303, expression_statement; 303, 304; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:plt; 307, identifier:subplot; 308, argument_list; 308, 309; 309, integer:122; 310, expression_statement; 310, 311; 311, call; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:plt; 314, identifier:title; 315, argument_list; 315, 316; 316, string:'Filter values of best filter'; 317, for_statement; 317, 318; 317, 319; 317, 324; 318, identifier:attr; 319, list:['j0', 'j1', 'sin', 'cos']; 319, 320; 319, 321; 319, 322; 319, 323; 320, string:'j0'; 321, string:'j1'; 322, string:'sin'; 323, string:'cos'; 324, block; 324, 325; 325, if_statement; 325, 326; 325, 331; 326, call; 326, 327; 326, 328; 327, identifier:hasattr; 328, argument_list; 328, 329; 328, 330; 329, identifier:filt; 330, identifier:attr; 331, block; 331, 332; 331, 372; 332, expression_statement; 332, 333; 333, call; 333, 334; 333, 337; 334, attribute; 334, 335; 334, 336; 335, identifier:plt; 336, identifier:plot; 337, argument_list; 337, 338; 337, 346; 337, 361; 337, 362; 337, 365; 338, call; 338, 339; 338, 342; 339, attribute; 339, 340; 339, 341; 340, identifier:np; 341, identifier:log10; 342, argument_list; 342, 343; 343, attribute; 343, 344; 343, 345; 344, identifier:filt; 345, identifier:base; 346, call; 346, 347; 346, 350; 347, attribute; 347, 348; 347, 349; 348, identifier:np; 349, identifier:log10; 350, argument_list; 350, 351; 351, call; 351, 352; 351, 355; 352, attribute; 352, 353; 352, 354; 353, identifier:np; 354, identifier:abs; 355, argument_list; 355, 356; 356, call; 356, 357; 356, 358; 357, identifier:getattr; 358, argument_list; 358, 359; 358, 360; 359, identifier:filt; 360, identifier:attr; 361, string:'.-'; 362, keyword_argument; 362, 363; 362, 364; 363, identifier:lw; 364, float:.5; 365, keyword_argument; 365, 366; 365, 367; 366, identifier:label; 367, binary_operator:+; 367, 368; 367, 371; 368, binary_operator:+; 368, 369; 368, 370; 369, string:'abs('; 370, identifier:attr; 371, string:')'; 372, expression_statement; 372, 373; 373, call; 373, 374; 373, 377; 374, attribute; 374, 375; 374, 376; 375, identifier:plt; 376, identifier:plot; 377, argument_list; 377, 378; 377, 386; 377, 397; 377, 398; 377, 401; 378, call; 378, 379; 378, 382; 379, attribute; 379, 380; 379, 381; 380, identifier:np; 381, identifier:log10; 382, argument_list; 382, 383; 383, attribute; 383, 384; 383, 385; 384, identifier:filt; 385, identifier:base; 386, call; 386, 387; 386, 390; 387, attribute; 387, 388; 387, 389; 388, identifier:np; 389, identifier:log10; 390, argument_list; 390, 391; 391, unary_operator:-; 391, 392; 392, call; 392, 393; 392, 394; 393, identifier:getattr; 394, argument_list; 394, 395; 394, 396; 395, identifier:filt; 396, identifier:attr; 397, string:'.'; 398, keyword_argument; 398, 399; 398, 400; 399, identifier:color; 400, string:'k'; 401, keyword_argument; 401, 402; 401, 403; 402, identifier:ms; 403, integer:4; 404, expression_statement; 404, 405; 405, call; 405, 406; 405, 409; 406, attribute; 406, 407; 406, 408; 407, identifier:plt; 408, identifier:plot; 409, argument_list; 409, 410; 409, 413; 409, 414; 409, 415; 409, 418; 409, 421; 410, attribute; 410, 411; 410, 412; 411, identifier:np; 412, identifier:inf; 413, integer:0; 414, string:'.'; 415, keyword_argument; 415, 416; 415, 417; 416, identifier:color; 417, string:'k'; 418, keyword_argument; 418, 419; 418, 420; 419, identifier:ms; 420, integer:4; 421, keyword_argument; 421, 422; 421, 423; 422, identifier:label; 423, string:'Neg. values'; 424, expression_statement; 424, 425; 425, call; 425, 426; 425, 429; 426, attribute; 426, 427; 426, 428; 427, identifier:plt; 428, identifier:xlabel; 429, argument_list; 429, 430; 430, string:'Base (log10)'; 431, expression_statement; 431, 432; 432, call; 432, 433; 432, 436; 433, attribute; 433, 434; 433, 435; 434, identifier:plt; 435, identifier:ylabel; 436, argument_list; 436, 437; 437, string:'Abs(Amplitude) (log10)'; 438, expression_statement; 438, 439; 439, call; 439, 440; 439, 443; 440, attribute; 440, 441; 440, 442; 441, identifier:plt; 442, identifier:legend; 443, argument_list; 443, 444; 444, keyword_argument; 444, 445; 444, 446; 445, identifier:loc; 446, string:'best'; 447, expression_statement; 447, 448; 448, call; 448, 449; 448, 458; 449, attribute; 449, 450; 449, 457; 450, attribute; 450, 451; 450, 456; 451, call; 451, 452; 451, 455; 452, attribute; 452, 453; 452, 454; 453, identifier:plt; 454, identifier:gcf; 455, argument_list; 456, identifier:canvas; 457, identifier:draw; 458, argument_list; 459, comment; 460, expression_statement; 460, 461; 461, call; 461, 462; 461, 465; 462, attribute; 462, 463; 462, 464; 463, identifier:plt; 464, identifier:show; 465, argument_list
def plot_result(filt, full, prntres=True): r"""QC the inversion result. Parameters ---------- - filt, full as returned from fdesign.design with full_output=True - If prntres is True, it calls fdesign.print_result as well. r""" # Check matplotlib (soft dependency) if not plt: print(plt_msg) return if prntres: print_result(filt, full) # Get spacing and shift values from full output of brute spacing = full[2][0, :, 0] shift = full[2][1, 0, :] # Get minimum field values from full output of brute minfield = np.squeeze(full[3]) plt.figure("Brute force result", figsize=(9.5, 4.5)) plt.subplots_adjust(wspace=.4, bottom=0.2) # Figure 1: Only if more than 1 spacing or more than 1 shift # Figure of minfield, depending if spacing/shift are vectors or floats if spacing.size > 1 or shift.size > 1: plt.subplot(121) if full[4] == 0: # Min amp plt.title("Minimal recovered fields") ylabel = 'Minimal recovered amplitude (log10)' field = np.log10(minfield) cmap = plt.cm.viridis else: # Max r plt.title("Maximum recovered r") ylabel = 'Maximum recovered r' field = 1/minfield cmap = plt.cm.viridis_r if shift.size == 1: # (a) if only one shift value, plt.plot(spacing, field) plt.xlabel('Spacing') plt.ylabel(ylabel) elif spacing.size == 1: # (b) if only one spacing value plt.plot(shift, field) plt.xlabel('Shift') plt.ylabel(ylabel) else: # (c) if several spacing and several shift values field = np.ma.masked_where(np.isinf(minfield), field) plt.pcolormesh(shift, spacing, field, cmap=cmap) plt.ylabel('Spacing') plt.xlabel('Shift') plt.colorbar() # Figure 2: Filter values if spacing.size > 1 or shift.size > 1: plt.subplot(122) plt.title('Filter values of best filter') for attr in ['j0', 'j1', 'sin', 'cos']: if hasattr(filt, attr): plt.plot(np.log10(filt.base), np.log10(np.abs(getattr(filt, attr))), '.-', lw=.5, label='abs('+attr+')') plt.plot(np.log10(filt.base), np.log10(-getattr(filt, attr)), '.', color='k', ms=4) plt.plot(np.inf, 0, '.', color='k', ms=4, label='Neg. values') plt.xlabel('Base (log10)') plt.ylabel('Abs(Amplitude) (log10)') plt.legend(loc='best') plt.gcf().canvas.draw() # To force draw in notebook while running plt.show()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:_plot_transform_pairs; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:fCI; 5, identifier:r; 6, identifier:k; 7, identifier:axes; 8, identifier:tit; 9, block; 9, 10; 9, 12; 9, 13; 9, 22; 9, 33; 9, 123; 9, 135; 9, 144; 9, 145; 9, 154; 9, 165; 9, 166; 9, 226; 9, 227; 9, 376; 9, 388; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, expression_statement; 13, 14; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:plt; 17, identifier:sca; 18, argument_list; 18, 19; 19, subscript; 19, 20; 19, 21; 20, identifier:axes; 21, integer:0; 22, expression_statement; 22, 23; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:plt; 26, identifier:title; 27, argument_list; 27, 28; 28, binary_operator:+; 28, 29; 28, 32; 29, binary_operator:+; 29, 30; 29, 31; 30, string:'|'; 31, identifier:tit; 32, string:' lhs|'; 33, for_statement; 33, 34; 33, 35; 33, 36; 34, identifier:f; 35, identifier:fCI; 36, block; 36, 37; 37, if_statement; 37, 38; 37, 43; 37, 95; 38, comparison_operator:==; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:f; 41, identifier:name; 42, string:'j2'; 43, block; 43, 44; 43, 53; 43, 74; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:lhs; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:f; 50, identifier:lhs; 51, argument_list; 51, 52; 52, identifier:k; 53, expression_statement; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:plt; 57, identifier:loglog; 58, argument_list; 58, 59; 58, 60; 58, 68; 58, 71; 59, identifier:k; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:np; 63, identifier:abs; 64, argument_list; 64, 65; 65, subscript; 65, 66; 65, 67; 66, identifier:lhs; 67, integer:0; 68, keyword_argument; 68, 69; 68, 70; 69, identifier:lw; 70, integer:2; 71, keyword_argument; 71, 72; 71, 73; 72, identifier:label; 73, string:'j0'; 74, expression_statement; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:plt; 78, identifier:loglog; 79, argument_list; 79, 80; 79, 81; 79, 89; 79, 92; 80, identifier:k; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:np; 84, identifier:abs; 85, argument_list; 85, 86; 86, subscript; 86, 87; 86, 88; 87, identifier:lhs; 88, integer:1; 89, keyword_argument; 89, 90; 89, 91; 90, identifier:lw; 91, integer:2; 92, keyword_argument; 92, 93; 92, 94; 93, identifier:label; 94, string:'j1'; 95, else_clause; 95, 96; 96, block; 96, 97; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:plt; 101, identifier:loglog; 102, argument_list; 102, 103; 102, 104; 102, 115; 102, 118; 103, identifier:k; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:np; 107, identifier:abs; 108, argument_list; 108, 109; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:f; 112, identifier:lhs; 113, argument_list; 113, 114; 114, identifier:k; 115, keyword_argument; 115, 116; 115, 117; 116, identifier:lw; 117, integer:2; 118, keyword_argument; 118, 119; 118, 120; 119, identifier:label; 120, attribute; 120, 121; 120, 122; 121, identifier:f; 122, identifier:name; 123, if_statement; 123, 124; 123, 127; 124, comparison_operator:!=; 124, 125; 124, 126; 125, identifier:tit; 126, string:'fC'; 127, block; 127, 128; 128, expression_statement; 128, 129; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:plt; 132, identifier:xlabel; 133, argument_list; 133, 134; 134, string:'l'; 135, expression_statement; 135, 136; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:plt; 139, identifier:legend; 140, argument_list; 140, 141; 141, keyword_argument; 141, 142; 141, 143; 142, identifier:loc; 143, string:'best'; 144, comment; 145, expression_statement; 145, 146; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:plt; 149, identifier:sca; 150, argument_list; 150, 151; 151, subscript; 151, 152; 151, 153; 152, identifier:axes; 153, integer:1; 154, expression_statement; 154, 155; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:plt; 158, identifier:title; 159, argument_list; 159, 160; 160, binary_operator:+; 160, 161; 160, 164; 161, binary_operator:+; 161, 162; 161, 163; 162, string:'|'; 163, identifier:tit; 164, string:' rhs|'; 165, comment; 166, for_statement; 166, 167; 166, 168; 166, 169; 167, identifier:f; 168, identifier:fCI; 169, block; 169, 170; 170, if_statement; 170, 171; 170, 174; 170, 198; 171, comparison_operator:==; 171, 172; 171, 173; 172, identifier:tit; 173, string:'fC'; 174, block; 174, 175; 175, expression_statement; 175, 176; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:plt; 179, identifier:loglog; 180, argument_list; 180, 181; 180, 182; 180, 190; 180, 193; 181, identifier:r; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:np; 185, identifier:abs; 186, argument_list; 186, 187; 187, attribute; 187, 188; 187, 189; 188, identifier:f; 189, identifier:rhs; 190, keyword_argument; 190, 191; 190, 192; 191, identifier:lw; 192, integer:2; 193, keyword_argument; 193, 194; 193, 195; 194, identifier:label; 195, attribute; 195, 196; 195, 197; 196, identifier:f; 197, identifier:name; 198, else_clause; 198, 199; 199, block; 199, 200; 200, expression_statement; 200, 201; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:plt; 204, identifier:loglog; 205, argument_list; 205, 206; 205, 207; 205, 218; 205, 221; 206, identifier:r; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:np; 210, identifier:abs; 211, argument_list; 211, 212; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:f; 215, identifier:rhs; 216, argument_list; 216, 217; 217, identifier:r; 218, keyword_argument; 218, 219; 218, 220; 219, identifier:lw; 220, integer:2; 221, keyword_argument; 221, 222; 221, 223; 222, identifier:label; 223, attribute; 223, 224; 223, 225; 224, identifier:f; 225, identifier:name; 226, comment; 227, for_statement; 227, 228; 227, 229; 227, 230; 228, identifier:f; 229, identifier:fCI; 230, block; 230, 231; 230, 257; 230, 269; 230, 354; 231, if_statement; 231, 232; 231, 242; 231, 249; 232, comparison_operator:in; 232, 233; 232, 238; 233, subscript; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:f; 236, identifier:name; 237, integer:1; 238, list:['0', '1', '2']; 238, 239; 238, 240; 238, 241; 239, string:'0'; 240, string:'1'; 241, string:'2'; 242, block; 242, 243; 243, expression_statement; 243, 244; 244, assignment; 244, 245; 244, 246; 245, identifier:filt; 246, call; 246, 247; 246, 248; 247, identifier:j0j1filt; 248, argument_list; 249, else_clause; 249, 250; 250, block; 250, 251; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 254; 253, identifier:filt; 254, call; 254, 255; 254, 256; 255, identifier:sincosfilt; 256, argument_list; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 260; 259, identifier:kk; 260, binary_operator:/; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:filt; 263, identifier:base; 264, subscript; 264, 265; 264, 266; 264, 268; 265, identifier:r; 266, slice; 266, 267; 267, colon; 268, None; 269, if_statement; 269, 270; 269, 275; 269, 329; 270, comparison_operator:==; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:f; 273, identifier:name; 274, string:'j2'; 275, block; 275, 276; 275, 285; 275, 303; 275, 323; 276, expression_statement; 276, 277; 277, assignment; 277, 278; 277, 279; 278, identifier:lhs; 279, call; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:f; 282, identifier:lhs; 283, argument_list; 283, 284; 284, identifier:kk; 285, expression_statement; 285, 286; 286, assignment; 286, 287; 286, 288; 287, identifier:kr0; 288, binary_operator:/; 288, 289; 288, 302; 289, call; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:np; 292, identifier:dot; 293, argument_list; 293, 294; 293, 297; 294, subscript; 294, 295; 294, 296; 295, identifier:lhs; 296, integer:0; 297, call; 297, 298; 297, 299; 298, identifier:getattr; 299, argument_list; 299, 300; 299, 301; 300, identifier:filt; 301, string:'j0'; 302, identifier:r; 303, expression_statement; 303, 304; 304, assignment; 304, 305; 304, 306; 305, identifier:kr1; 306, binary_operator:/; 306, 307; 306, 320; 307, call; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:np; 310, identifier:dot; 311, argument_list; 311, 312; 311, 315; 312, subscript; 312, 313; 312, 314; 313, identifier:lhs; 314, integer:1; 315, call; 315, 316; 315, 317; 316, identifier:getattr; 317, argument_list; 317, 318; 317, 319; 318, identifier:filt; 319, string:'j1'; 320, binary_operator:**; 320, 321; 320, 322; 321, identifier:r; 322, integer:2; 323, expression_statement; 323, 324; 324, assignment; 324, 325; 324, 326; 325, identifier:kr; 326, binary_operator:+; 326, 327; 326, 328; 327, identifier:kr0; 328, identifier:kr1; 329, else_clause; 329, 330; 330, block; 330, 331; 331, expression_statement; 331, 332; 332, assignment; 332, 333; 332, 334; 333, identifier:kr; 334, binary_operator:/; 334, 335; 334, 353; 335, call; 335, 336; 335, 339; 336, attribute; 336, 337; 336, 338; 337, identifier:np; 338, identifier:dot; 339, argument_list; 339, 340; 339, 346; 340, call; 340, 341; 340, 344; 341, attribute; 341, 342; 341, 343; 342, identifier:f; 343, identifier:lhs; 344, argument_list; 344, 345; 345, identifier:kk; 346, call; 346, 347; 346, 348; 347, identifier:getattr; 348, argument_list; 348, 349; 348, 350; 349, identifier:filt; 350, attribute; 350, 351; 350, 352; 351, identifier:f; 352, identifier:name; 353, identifier:r; 354, expression_statement; 354, 355; 355, call; 355, 356; 355, 359; 356, attribute; 356, 357; 356, 358; 357, identifier:plt; 358, identifier:loglog; 359, argument_list; 359, 360; 359, 361; 359, 367; 359, 368; 359, 371; 360, identifier:r; 361, call; 361, 362; 361, 365; 362, attribute; 362, 363; 362, 364; 363, identifier:np; 364, identifier:abs; 365, argument_list; 365, 366; 366, identifier:kr; 367, string:'-.'; 368, keyword_argument; 368, 369; 368, 370; 369, identifier:lw; 370, integer:2; 371, keyword_argument; 371, 372; 371, 373; 372, identifier:label; 373, attribute; 373, 374; 373, 375; 374, identifier:filt; 375, identifier:name; 376, if_statement; 376, 377; 376, 380; 377, comparison_operator:!=; 377, 378; 377, 379; 378, identifier:tit; 379, string:'fC'; 380, block; 380, 381; 381, expression_statement; 381, 382; 382, call; 382, 383; 382, 386; 383, attribute; 383, 384; 383, 385; 384, identifier:plt; 385, identifier:xlabel; 386, argument_list; 386, 387; 387, string:'r'; 388, expression_statement; 388, 389; 389, call; 389, 390; 389, 393; 390, attribute; 390, 391; 390, 392; 391, identifier:plt; 392, identifier:legend; 393, argument_list; 393, 394; 394, keyword_argument; 394, 395; 394, 396; 395, identifier:loc; 396, string:'best'
def _plot_transform_pairs(fCI, r, k, axes, tit): r"""Plot the input transform pairs.""" # Plot lhs plt.sca(axes[0]) plt.title('|' + tit + ' lhs|') for f in fCI: if f.name == 'j2': lhs = f.lhs(k) plt.loglog(k, np.abs(lhs[0]), lw=2, label='j0') plt.loglog(k, np.abs(lhs[1]), lw=2, label='j1') else: plt.loglog(k, np.abs(f.lhs(k)), lw=2, label=f.name) if tit != 'fC': plt.xlabel('l') plt.legend(loc='best') # Plot rhs plt.sca(axes[1]) plt.title('|' + tit + ' rhs|') # Transform pair rhs for f in fCI: if tit == 'fC': plt.loglog(r, np.abs(f.rhs), lw=2, label=f.name) else: plt.loglog(r, np.abs(f.rhs(r)), lw=2, label=f.name) # Transform with Key for f in fCI: if f.name[1] in ['0', '1', '2']: filt = j0j1filt() else: filt = sincosfilt() kk = filt.base/r[:, None] if f.name == 'j2': lhs = f.lhs(kk) kr0 = np.dot(lhs[0], getattr(filt, 'j0'))/r kr1 = np.dot(lhs[1], getattr(filt, 'j1'))/r**2 kr = kr0+kr1 else: kr = np.dot(f.lhs(kk), getattr(filt, f.name))/r plt.loglog(r, np.abs(kr), '-.', lw=2, label=filt.name) if tit != 'fC': plt.xlabel('r') plt.legend(loc='best')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 36; 2, function_name:empy_hankel; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 3, 24; 3, 27; 3, 30; 3, 33; 4, identifier:ftype; 5, identifier:zsrc; 6, identifier:zrec; 7, identifier:res; 8, identifier:freqtime; 9, default_parameter; 9, 10; 9, 11; 10, identifier:depth; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:aniso; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:epermH; 17, None; 18, default_parameter; 18, 19; 18, 20; 19, identifier:epermV; 20, None; 21, default_parameter; 21, 22; 21, 23; 22, identifier:mpermH; 23, None; 24, default_parameter; 24, 25; 24, 26; 25, identifier:mpermV; 26, None; 27, default_parameter; 27, 28; 27, 29; 28, identifier:htarg; 29, None; 30, default_parameter; 30, 31; 30, 32; 31, identifier:verblhs; 32, integer:0; 33, default_parameter; 33, 34; 33, 35; 34, identifier:verbrhs; 35, integer:0; 36, block; 36, 37; 36, 39; 36, 40; 36, 80; 36, 81; 36, 112; 36, 113; 36, 201; 36, 202; 36, 203; 36, 243; 36, 244; 36, 297; 37, expression_statement; 37, 38; 38, comment; 39, comment; 40, if_statement; 40, 41; 40, 46; 41, call; 41, 42; 41, 43; 42, identifier:isinstance; 43, argument_list; 43, 44; 43, 45; 44, identifier:ftype; 45, identifier:list; 46, block; 46, 47; 46, 51; 46, 78; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:out; 50, list:[]; 51, for_statement; 51, 52; 51, 53; 51, 54; 52, identifier:f; 53, identifier:ftype; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:out; 59, identifier:append; 60, argument_list; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:empy_hankel; 63, argument_list; 63, 64; 63, 65; 63, 66; 63, 67; 63, 68; 63, 69; 63, 70; 63, 71; 63, 72; 63, 73; 63, 74; 63, 75; 63, 76; 63, 77; 64, identifier:f; 65, identifier:zsrc; 66, identifier:zrec; 67, identifier:res; 68, identifier:freqtime; 69, identifier:depth; 70, identifier:aniso; 71, identifier:epermH; 72, identifier:epermV; 73, identifier:mpermH; 74, identifier:mpermV; 75, identifier:htarg; 76, identifier:verblhs; 77, identifier:verbrhs; 78, return_statement; 78, 79; 79, identifier:out; 80, comment; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:model; 84, dictionary; 84, 85; 84, 91; 84, 94; 84, 97; 84, 100; 84, 103; 84, 106; 84, 109; 85, pair; 85, 86; 85, 87; 86, string:'src'; 87, list:[0, 0, zsrc]; 87, 88; 87, 89; 87, 90; 88, integer:0; 89, integer:0; 90, identifier:zsrc; 91, pair; 91, 92; 91, 93; 92, string:'depth'; 93, identifier:depth; 94, pair; 94, 95; 94, 96; 95, string:'res'; 96, identifier:res; 97, pair; 97, 98; 97, 99; 98, string:'aniso'; 99, identifier:aniso; 100, pair; 100, 101; 100, 102; 101, string:'epermH'; 102, identifier:epermH; 103, pair; 103, 104; 103, 105; 104, string:'epermV'; 105, identifier:epermV; 106, pair; 106, 107; 106, 108; 107, string:'mpermH'; 108, identifier:mpermH; 109, pair; 109, 110; 109, 111; 110, string:'mpermV'; 111, identifier:mpermV; 112, comment; 113, if_statement; 113, 114; 113, 117; 113, 118; 113, 147; 113, 167; 114, comparison_operator:==; 114, 115; 114, 116; 115, identifier:ftype; 116, string:'j0'; 117, comment; 118, block; 118, 119; 118, 125; 118, 136; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 124; 121, subscript; 121, 122; 121, 123; 122, identifier:model; 123, string:'ab'; 124, integer:11; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:x; 128, binary_operator:/; 128, 129; 128, 130; 129, integer:1; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:np; 133, identifier:sqrt; 134, argument_list; 134, 135; 135, integer:2; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:y; 139, binary_operator:/; 139, 140; 139, 141; 140, integer:1; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:np; 144, identifier:sqrt; 145, argument_list; 145, 146; 146, integer:2; 147, elif_clause; 147, 148; 147, 151; 147, 152; 148, comparison_operator:==; 148, 149; 148, 150; 149, identifier:ftype; 150, string:'j1'; 151, comment; 152, block; 152, 153; 152, 159; 152, 163; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 158; 155, subscript; 155, 156; 155, 157; 156, identifier:model; 157, string:'ab'; 158, integer:31; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:x; 162, integer:1; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:y; 166, integer:0; 167, elif_clause; 167, 168; 167, 171; 167, 172; 168, comparison_operator:==; 168, 169; 168, 170; 169, identifier:ftype; 170, string:'j2'; 171, comment; 172, block; 172, 173; 172, 179; 172, 190; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 178; 175, subscript; 175, 176; 175, 177; 176, identifier:model; 177, string:'ab'; 178, integer:12; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:x; 182, binary_operator:/; 182, 183; 182, 184; 183, integer:1; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:np; 187, identifier:sqrt; 188, argument_list; 188, 189; 189, integer:2; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:y; 193, binary_operator:/; 193, 194; 193, 195; 194, integer:1; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:np; 198, identifier:sqrt; 199, argument_list; 199, 200; 200, integer:2; 201, comment; 202, comment; 203, function_definition; 203, 204; 203, 205; 203, 207; 204, function_name:rhs; 205, parameters; 205, 206; 206, identifier:r; 207, block; 207, 208; 207, 241; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 211; 210, identifier:out; 211, call; 211, 212; 211, 213; 212, identifier:dipole; 213, argument_list; 213, 214; 213, 224; 213, 227; 213, 230; 213, 233; 213, 236; 213, 239; 214, keyword_argument; 214, 215; 214, 216; 215, identifier:rec; 216, list:[r*x, r*y, zrec]; 216, 217; 216, 220; 216, 223; 217, binary_operator:*; 217, 218; 217, 219; 218, identifier:r; 219, identifier:x; 220, binary_operator:*; 220, 221; 220, 222; 221, identifier:r; 222, identifier:y; 223, identifier:zrec; 224, keyword_argument; 224, 225; 224, 226; 225, identifier:ht; 226, string:'qwe'; 227, keyword_argument; 227, 228; 227, 229; 228, identifier:xdirect; 229, True; 230, keyword_argument; 230, 231; 230, 232; 231, identifier:verb; 232, identifier:verbrhs; 233, keyword_argument; 233, 234; 233, 235; 234, identifier:htarg; 235, identifier:htarg; 236, keyword_argument; 236, 237; 236, 238; 237, identifier:freqtime; 238, identifier:freqtime; 239, dictionary_splat; 239, 240; 240, identifier:model; 241, return_statement; 241, 242; 242, identifier:out; 243, comment; 244, function_definition; 244, 245; 244, 246; 244, 248; 245, function_name:lhs; 246, parameters; 246, 247; 247, identifier:k; 248, block; 248, 249; 248, 274; 249, expression_statement; 249, 250; 250, assignment; 250, 251; 250, 254; 251, pattern_list; 251, 252; 251, 253; 252, identifier:lhs0; 253, identifier:lhs1; 254, call; 254, 255; 254, 256; 255, identifier:dipole_k; 256, argument_list; 256, 257; 256, 263; 256, 266; 256, 269; 256, 272; 257, keyword_argument; 257, 258; 257, 259; 258, identifier:rec; 259, list:[x, y, zrec]; 259, 260; 259, 261; 259, 262; 260, identifier:x; 261, identifier:y; 262, identifier:zrec; 263, keyword_argument; 263, 264; 263, 265; 264, identifier:wavenumber; 265, identifier:k; 266, keyword_argument; 266, 267; 266, 268; 267, identifier:verb; 268, identifier:verblhs; 269, keyword_argument; 269, 270; 269, 271; 270, identifier:freq; 271, identifier:freqtime; 272, dictionary_splat; 272, 273; 273, identifier:model; 274, if_statement; 274, 275; 274, 278; 274, 281; 274, 288; 275, comparison_operator:==; 275, 276; 275, 277; 276, identifier:ftype; 277, string:'j0'; 278, block; 278, 279; 279, return_statement; 279, 280; 280, identifier:lhs0; 281, elif_clause; 281, 282; 281, 285; 282, comparison_operator:==; 282, 283; 282, 284; 283, identifier:ftype; 284, string:'j1'; 285, block; 285, 286; 286, return_statement; 286, 287; 287, identifier:lhs1; 288, elif_clause; 288, 289; 288, 292; 289, comparison_operator:==; 289, 290; 289, 291; 290, identifier:ftype; 291, string:'j2'; 292, block; 292, 293; 293, return_statement; 293, 294; 294, tuple; 294, 295; 294, 296; 295, identifier:lhs0; 296, identifier:lhs1; 297, return_statement; 297, 298; 298, call; 298, 299; 298, 300; 299, identifier:Ghosh; 300, argument_list; 300, 301; 300, 302; 300, 303; 301, identifier:ftype; 302, identifier:lhs; 303, identifier:rhs
def empy_hankel(ftype, zsrc, zrec, res, freqtime, depth=None, aniso=None, epermH=None, epermV=None, mpermH=None, mpermV=None, htarg=None, verblhs=0, verbrhs=0): r"""Numerical transform pair with empymod. All parameters except ``ftype``, ``verblhs``, and ``verbrhs`` correspond to the input parameters to ``empymod.dipole``. See there for more information. Note that if depth=None or [], the analytical full-space solutions will be used (much faster). Parameters ---------- ftype : str or list of strings Either of: {'j0', 'j1', 'j2', ['j0', 'j1']} - 'j0': Analyze J0-term with ab=11, angle=45° - 'j1': Analyze J1-term with ab=31, angle=0° - 'j2': Analyze J0- and J1-terms jointly with ab=12, angle=45° - ['j0', 'j1']: Same as calling empy_hankel twice, once with 'j0' and one with 'j1'; can be provided like this to fdesign.design. verblhs, verbrhs: int verb-values provided to empymod for lhs and rhs. Note that ftype='j2' only works for fC, not for fI. """ # Loop over ftypes, if there are several if isinstance(ftype, list): out = [] for f in ftype: out.append(empy_hankel(f, zsrc, zrec, res, freqtime, depth, aniso, epermH, epermV, mpermH, mpermV, htarg, verblhs, verbrhs)) return out # Collect model model = {'src': [0, 0, zsrc], 'depth': depth, 'res': res, 'aniso': aniso, 'epermH': epermH, 'epermV': epermV, 'mpermH': mpermH, 'mpermV': mpermV} # Finalize model depending on ftype if ftype == 'j0': # J0: 11, 45° model['ab'] = 11 x = 1/np.sqrt(2) y = 1/np.sqrt(2) elif ftype == 'j1': # J1: 31, 0° model['ab'] = 31 x = 1 y = 0 elif ftype == 'j2': # J2: 12, 45° model['ab'] = 12 x = 1/np.sqrt(2) y = 1/np.sqrt(2) # rhs: empymod.model.dipole # If depth=[], the analytical full-space solution will be used internally def rhs(r): out = dipole(rec=[r*x, r*y, zrec], ht='qwe', xdirect=True, verb=verbrhs, htarg=htarg, freqtime=freqtime, **model) return out # lhs: empymod.model.dipole_k def lhs(k): lhs0, lhs1 = dipole_k(rec=[x, y, zrec], wavenumber=k, verb=verblhs, freq=freqtime, **model) if ftype == 'j0': return lhs0 elif ftype == 'j1': return lhs1 elif ftype == 'j2': return (lhs0, lhs1) return Ghosh(ftype, lhs, rhs)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_get_min_val; 3, parameters; 3, 4; 3, 5; 4, identifier:spaceshift; 5, list_splat_pattern; 5, 6; 6, identifier:params; 7, block; 7, 8; 7, 10; 7, 11; 7, 17; 7, 32; 7, 33; 7, 46; 7, 47; 7, 59; 7, 60; 7, 394; 7, 395; 7, 407; 7, 408; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 16; 13, pattern_list; 13, 14; 13, 15; 14, identifier:spacing; 15, identifier:shift; 16, identifier:spaceshift; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 31; 19, pattern_list; 19, 20; 19, 21; 19, 22; 19, 23; 19, 24; 19, 25; 19, 26; 19, 27; 19, 28; 19, 29; 19, 30; 20, identifier:n; 21, identifier:fI; 22, identifier:fC; 23, identifier:r; 24, identifier:r_def; 25, identifier:error; 26, identifier:reim; 27, identifier:cvar; 28, identifier:verb; 29, identifier:plot; 30, identifier:log; 31, identifier:params; 32, comment; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:dlf; 36, call; 36, 37; 36, 38; 37, identifier:_calculate_filter; 38, argument_list; 38, 39; 38, 40; 38, 41; 38, 42; 38, 43; 38, 44; 38, 45; 39, identifier:n; 40, identifier:spacing; 41, identifier:shift; 42, identifier:fI; 43, identifier:r_def; 44, identifier:reim; 45, string:'filt'; 46, comment; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:k; 50, binary_operator:/; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:dlf; 53, identifier:base; 54, subscript; 54, 55; 54, 56; 54, 58; 55, identifier:r; 56, slice; 56, 57; 57, colon; 58, None; 59, comment; 60, for_statement; 60, 61; 60, 64; 60, 68; 60, 69; 61, pattern_list; 61, 62; 61, 63; 62, identifier:i; 63, identifier:f; 64, call; 64, 65; 64, 66; 65, identifier:enumerate; 66, argument_list; 66, 67; 67, identifier:fC; 68, comment; 69, block; 69, 70; 69, 79; 69, 150; 69, 151; 69, 169; 69, 170; 69, 183; 69, 184; 69, 306; 69, 307; 69, 333; 69, 334; 69, 376; 69, 377; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:lhs; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:f; 76, identifier:lhs; 77, argument_list; 77, 78; 78, identifier:k; 79, if_statement; 79, 80; 79, 85; 79, 130; 80, comparison_operator:==; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:f; 83, identifier:name; 84, string:'j2'; 85, block; 85, 86; 85, 104; 85, 124; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:rhs0; 89, binary_operator:/; 89, 90; 89, 103; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:np; 93, identifier:dot; 94, argument_list; 94, 95; 94, 98; 95, subscript; 95, 96; 95, 97; 96, identifier:lhs; 97, integer:0; 98, call; 98, 99; 98, 100; 99, identifier:getattr; 100, argument_list; 100, 101; 100, 102; 101, identifier:dlf; 102, string:'j0'; 103, identifier:r; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:rhs1; 107, binary_operator:/; 107, 108; 107, 121; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:np; 111, identifier:dot; 112, argument_list; 112, 113; 112, 116; 113, subscript; 113, 114; 113, 115; 114, identifier:lhs; 115, integer:1; 116, call; 116, 117; 116, 118; 117, identifier:getattr; 118, argument_list; 118, 119; 118, 120; 119, identifier:dlf; 120, string:'j1'; 121, binary_operator:**; 121, 122; 121, 123; 122, identifier:r; 123, integer:2; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:rhs; 127, binary_operator:+; 127, 128; 127, 129; 128, identifier:rhs0; 129, identifier:rhs1; 130, else_clause; 130, 131; 131, block; 131, 132; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:rhs; 135, binary_operator:/; 135, 136; 135, 149; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:np; 139, identifier:dot; 140, argument_list; 140, 141; 140, 142; 141, identifier:lhs; 142, call; 142, 143; 142, 144; 143, identifier:getattr; 144, argument_list; 144, 145; 144, 146; 145, identifier:dlf; 146, attribute; 146, 147; 146, 148; 147, identifier:f; 148, identifier:name; 149, identifier:r; 150, comment; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:rel_error; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:np; 157, identifier:abs; 158, argument_list; 158, 159; 159, binary_operator:/; 159, 160; 159, 166; 160, parenthesized_expression; 160, 161; 161, binary_operator:-; 161, 162; 161, 163; 162, identifier:rhs; 163, attribute; 163, 164; 163, 165; 164, identifier:f; 165, identifier:rhs; 166, attribute; 166, 167; 166, 168; 167, identifier:f; 168, identifier:rhs; 169, comment; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:imin0; 173, subscript; 173, 174; 173, 182; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:np; 177, identifier:where; 178, argument_list; 178, 179; 179, comparison_operator:>; 179, 180; 179, 181; 180, identifier:rel_error; 181, identifier:error; 182, integer:0; 183, comment; 184, if_statement; 184, 185; 184, 205; 184, 206; 184, 211; 184, 258; 185, boolean_operator:or; 185, 186; 185, 194; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:np; 189, identifier:all; 190, argument_list; 190, 191; 191, comparison_operator:==; 191, 192; 191, 193; 192, identifier:rhs; 193, integer:0; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:np; 197, identifier:all; 198, argument_list; 198, 199; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:np; 202, identifier:isnan; 203, argument_list; 203, 204; 204, identifier:rhs; 205, comment; 206, block; 206, 207; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:imin0; 210, integer:0; 211, elif_clause; 211, 212; 211, 217; 211, 218; 212, comparison_operator:==; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:imin0; 215, identifier:size; 216, integer:0; 217, comment; 218, block; 218, 219; 218, 227; 218, 228; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 222; 221, identifier:imin0; 222, binary_operator:-; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:rhs; 225, identifier:size; 226, integer:1; 227, comment; 228, if_statement; 228, 229; 228, 238; 229, boolean_operator:and; 229, 230; 229, 233; 230, comparison_operator:>; 230, 231; 230, 232; 231, identifier:verb; 232, integer:0; 233, comparison_operator:==; 233, 234; 233, 237; 234, subscript; 234, 235; 234, 236; 235, identifier:log; 236, string:'warn-r'; 237, integer:0; 238, block; 238, 239; 238, 251; 238, 257; 239, expression_statement; 239, 240; 240, call; 240, 241; 240, 242; 241, identifier:print; 242, argument_list; 242, 243; 243, binary_operator:+; 243, 244; 243, 250; 244, binary_operator:+; 244, 245; 244, 246; 245, string:'* WARNING :: all data have error < '; 246, call; 246, 247; 246, 248; 247, identifier:str; 248, argument_list; 248, 249; 249, identifier:error; 250, string:'; choose larger r or set error-level higher.'; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 256; 253, subscript; 253, 254; 253, 255; 254, identifier:log; 255, string:'warn-r'; 256, integer:1; 257, comment; 258, else_clause; 258, 259; 258, 260; 258, 261; 258, 262; 258, 263; 259, comment; 260, comment; 261, comment; 262, comment; 263, block; 263, 264; 263, 304; 263, 305; 264, if_statement; 264, 265; 264, 270; 264, 286; 265, comparison_operator:>; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, identifier:imin0; 268, identifier:size; 269, integer:4; 270, block; 270, 271; 271, expression_statement; 271, 272; 272, assignment; 272, 273; 272, 274; 273, identifier:imin0; 274, call; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:np; 277, identifier:max; 278, argument_list; 278, 279; 279, list:[0, imin0[4]-5]; 279, 280; 279, 281; 280, integer:0; 281, binary_operator:-; 281, 282; 281, 285; 282, subscript; 282, 283; 282, 284; 283, identifier:imin0; 284, integer:4; 285, integer:5; 286, else_clause; 286, 287; 286, 288; 287, comment; 288, block; 288, 289; 289, expression_statement; 289, 290; 290, assignment; 290, 291; 290, 292; 291, identifier:imin0; 292, call; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:np; 295, identifier:max; 296, argument_list; 296, 297; 297, list:[0, imin0[0]-1]; 297, 298; 297, 299; 298, integer:0; 299, binary_operator:-; 299, 300; 299, 303; 300, subscript; 300, 301; 300, 302; 301, identifier:imin0; 302, integer:0; 303, integer:1; 304, comment; 305, comment; 306, comment; 307, if_statement; 307, 308; 307, 311; 307, 323; 308, comparison_operator:==; 308, 309; 308, 310; 309, identifier:cvar; 310, string:'amp'; 311, block; 311, 312; 312, expression_statement; 312, 313; 313, assignment; 313, 314; 313, 315; 314, identifier:min_val0; 315, call; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:np; 318, identifier:abs; 319, argument_list; 319, 320; 320, subscript; 320, 321; 320, 322; 321, identifier:rhs; 322, identifier:imin0; 323, else_clause; 323, 324; 324, block; 324, 325; 325, expression_statement; 325, 326; 326, assignment; 326, 327; 326, 328; 327, identifier:min_val0; 328, binary_operator:/; 328, 329; 328, 330; 329, integer:1; 330, subscript; 330, 331; 330, 332; 331, identifier:r; 332, identifier:imin0; 333, comment; 334, if_statement; 334, 335; 334, 338; 334, 339; 334, 354; 335, comparison_operator:==; 335, 336; 335, 337; 336, identifier:i; 337, integer:0; 338, comment; 339, block; 339, 340; 339, 347; 340, expression_statement; 340, 341; 341, assignment; 341, 342; 341, 343; 342, identifier:imin; 343, call; 343, 344; 343, 345; 344, identifier:dc; 345, argument_list; 345, 346; 346, identifier:imin0; 347, expression_statement; 347, 348; 348, assignment; 348, 349; 348, 350; 349, identifier:min_val; 350, call; 350, 351; 350, 352; 351, identifier:dc; 352, argument_list; 352, 353; 353, identifier:min_val0; 354, else_clause; 354, 355; 354, 356; 355, comment; 356, block; 356, 357; 357, if_statement; 357, 358; 357, 361; 358, comparison_operator:>; 358, 359; 358, 360; 359, identifier:min_val0; 360, identifier:min_val; 361, block; 361, 362; 361, 369; 362, expression_statement; 362, 363; 363, assignment; 363, 364; 363, 365; 364, identifier:min_val; 365, call; 365, 366; 365, 367; 366, identifier:dc; 367, argument_list; 367, 368; 368, identifier:min_val0; 369, expression_statement; 369, 370; 370, assignment; 370, 371; 370, 372; 371, identifier:imin; 372, call; 372, 373; 372, 374; 373, identifier:dc; 374, argument_list; 374, 375; 375, identifier:imin0; 376, comment; 377, if_statement; 377, 378; 377, 381; 378, comparison_operator:>; 378, 379; 378, 380; 379, identifier:plot; 380, integer:2; 381, block; 381, 382; 382, expression_statement; 382, 383; 383, call; 383, 384; 383, 385; 384, identifier:_plot_inversion; 385, argument_list; 385, 386; 385, 387; 385, 388; 385, 389; 385, 390; 385, 391; 385, 392; 385, 393; 386, identifier:f; 387, identifier:rhs; 388, identifier:r; 389, identifier:k; 390, identifier:imin0; 391, identifier:spacing; 392, identifier:shift; 393, identifier:cvar; 394, comment; 395, if_statement; 395, 396; 395, 399; 396, comparison_operator:>; 396, 397; 396, 398; 397, identifier:verb; 398, integer:1; 399, block; 399, 400; 400, expression_statement; 400, 401; 401, assignment; 401, 402; 401, 403; 402, identifier:log; 403, call; 403, 404; 403, 405; 404, identifier:_print_count; 405, argument_list; 405, 406; 406, identifier:log; 407, comment; 408, return_statement; 408, 409; 409, call; 409, 410; 409, 413; 410, attribute; 410, 411; 410, 412; 411, identifier:np; 412, identifier:where; 413, argument_list; 413, 414; 413, 417; 413, 420; 414, comparison_operator:==; 414, 415; 414, 416; 415, identifier:imin; 416, integer:0; 417, attribute; 417, 418; 417, 419; 418, identifier:np; 419, identifier:inf; 420, identifier:min_val
def _get_min_val(spaceshift, *params): r"""Calculate minimum resolved amplitude or maximum r.""" # Get parameters from tuples spacing, shift = spaceshift n, fI, fC, r, r_def, error, reim, cvar, verb, plot, log = params # Get filter for these parameters dlf = _calculate_filter(n, spacing, shift, fI, r_def, reim, 'filt') # Calculate rhs-response with this filter k = dlf.base/r[:, None] # Loop over transforms for i, f in enumerate(fC): # Calculate lhs and rhs; rhs depends on ftype lhs = f.lhs(k) if f.name == 'j2': rhs0 = np.dot(lhs[0], getattr(dlf, 'j0'))/r rhs1 = np.dot(lhs[1], getattr(dlf, 'j1'))/r**2 rhs = rhs0 + rhs1 else: rhs = np.dot(lhs, getattr(dlf, f.name))/r # Get relative error rel_error = np.abs((rhs - f.rhs)/f.rhs) # Get indices where relative error is bigger than error imin0 = np.where(rel_error > error)[0] # Find first occurrence of failure if np.all(rhs == 0) or np.all(np.isnan(rhs)): # if all rhs are zeros or nans, the filter is useless imin0 = 0 elif imin0.size == 0: # if imin0.size == 0: # empty array, all rel_error < error. imin0 = rhs.size-1 # set to last r if verb > 0 and log['warn-r'] == 0: print('* WARNING :: all data have error < ' + str(error) + '; choose larger r or set error-level higher.') log['warn-r'] = 1 # Only do this once else: # Kind of a dirty hack: Permit to jump up to four bad values, # resulting for instance from high rel_error from zero crossings # of the transform pair. Should be made an input argument or # generally improved. if imin0.size > 4: imin0 = np.max([0, imin0[4]-5]) else: # just take the first one (no jumping allowed; normal case) imin0 = np.max([0, imin0[0]-1]) # Note that both version yield the same result if the failure is # consistent. # Depending on cvar, store minimum amplitude or 1/maxr if cvar == 'amp': min_val0 = np.abs(rhs[imin0]) else: min_val0 = 1/r[imin0] # Check if this inversion is better than previous ones if i == 0: # First run, store these values imin = dc(imin0) min_val = dc(min_val0) else: # Replace imin, min_val if this one is better if min_val0 > min_val: min_val = dc(min_val0) imin = dc(imin0) # QC plot if plot > 2: _plot_inversion(f, rhs, r, k, imin0, spacing, shift, cvar) # If verbose, print progress if verb > 1: log = _print_count(log) # If there is no point with rel_error < error (imin=0) it returns np.inf. return np.where(imin == 0, np.inf, min_val)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 19; 2, function_name:wavenumber; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 3, 12; 3, 13; 3, 14; 3, 15; 3, 16; 3, 17; 3, 18; 4, identifier:zsrc; 5, identifier:zrec; 6, identifier:lsrc; 7, identifier:lrec; 8, identifier:depth; 9, identifier:etaH; 10, identifier:etaV; 11, identifier:zetaH; 12, identifier:zetaV; 13, identifier:lambd; 14, identifier:ab; 15, identifier:xdirect; 16, identifier:msrc; 17, identifier:mrec; 18, identifier:use_ne_eval; 19, block; 19, 20; 19, 22; 19, 23; 19, 24; 19, 47; 19, 48; 19, 49; 19, 53; 19, 57; 19, 61; 19, 62; 19, 76; 19, 77; 19, 91; 19, 92; 19, 231; 19, 232; 20, expression_statement; 20, 21; 21, comment; 22, comment; 23, comment; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 29; 26, pattern_list; 26, 27; 26, 28; 27, identifier:PTM; 28, identifier:PTE; 29, call; 29, 30; 29, 31; 30, identifier:greenfct; 31, argument_list; 31, 32; 31, 33; 31, 34; 31, 35; 31, 36; 31, 37; 31, 38; 31, 39; 31, 40; 31, 41; 31, 42; 31, 43; 31, 44; 31, 45; 31, 46; 32, identifier:zsrc; 33, identifier:zrec; 34, identifier:lsrc; 35, identifier:lrec; 36, identifier:depth; 37, identifier:etaH; 38, identifier:etaV; 39, identifier:zetaH; 40, identifier:zetaV; 41, identifier:lambd; 42, identifier:ab; 43, identifier:xdirect; 44, identifier:msrc; 45, identifier:mrec; 46, identifier:use_ne_eval; 47, comment; 48, comment; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:PJ0; 52, None; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:PJ1; 56, None; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:PJ0b; 60, None; 61, comment; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:Ptot; 65, binary_operator:/; 65, 66; 65, 70; 66, parenthesized_expression; 66, 67; 67, binary_operator:+; 67, 68; 67, 69; 68, identifier:PTM; 69, identifier:PTE; 70, parenthesized_expression; 70, 71; 71, binary_operator:*; 71, 72; 71, 73; 72, integer:4; 73, attribute; 73, 74; 73, 75; 74, identifier:np; 75, identifier:pi; 76, comment; 77, if_statement; 77, 78; 77, 79; 77, 85; 78, identifier:mrec; 79, block; 79, 80; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:sign; 83, unary_operator:-; 83, 84; 84, integer:1; 85, else_clause; 85, 86; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:sign; 90, integer:1; 91, comment; 92, if_statement; 92, 93; 92, 104; 92, 105; 92, 106; 92, 175; 92, 212; 93, comparison_operator:in; 93, 94; 93, 95; 94, identifier:ab; 95, list:[11, 12, 21, 22, 14, 24, 15, 25]; 95, 96; 95, 97; 95, 98; 95, 99; 95, 100; 95, 101; 95, 102; 95, 103; 96, integer:11; 97, integer:12; 98, integer:21; 99, integer:22; 100, integer:14; 101, integer:24; 102, integer:15; 103, integer:25; 104, comment; 105, comment; 106, block; 106, 107; 106, 119; 106, 129; 106, 136; 107, if_statement; 107, 108; 107, 113; 108, comparison_operator:in; 108, 109; 108, 110; 109, identifier:ab; 110, list:[14, 22]; 110, 111; 110, 112; 111, integer:14; 112, integer:22; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, augmented_assignment:*=; 115, 116; 115, 117; 116, identifier:sign; 117, unary_operator:-; 117, 118; 118, integer:1; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:PJ0b; 122, binary_operator:*; 122, 123; 122, 128; 123, binary_operator:*; 123, 124; 123, 127; 124, binary_operator:/; 124, 125; 124, 126; 125, identifier:sign; 126, integer:2; 127, identifier:Ptot; 128, identifier:lambd; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:PJ1; 132, binary_operator:*; 132, 133; 132, 135; 133, unary_operator:-; 133, 134; 134, identifier:sign; 135, identifier:Ptot; 136, if_statement; 136, 137; 136, 144; 137, comparison_operator:in; 137, 138; 137, 139; 138, identifier:ab; 139, list:[11, 22, 24, 15]; 139, 140; 139, 141; 139, 142; 139, 143; 140, integer:11; 141, integer:22; 142, integer:24; 143, integer:15; 144, block; 144, 145; 144, 157; 145, if_statement; 145, 146; 145, 151; 146, comparison_operator:in; 146, 147; 146, 148; 147, identifier:ab; 148, list:[22, 24]; 148, 149; 148, 150; 149, integer:22; 150, integer:24; 151, block; 151, 152; 152, expression_statement; 152, 153; 153, augmented_assignment:*=; 153, 154; 153, 155; 154, identifier:sign; 155, unary_operator:-; 155, 156; 156, integer:1; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:PJ0; 160, binary_operator:*; 160, 161; 160, 174; 161, binary_operator:/; 161, 162; 161, 168; 162, binary_operator:*; 162, 163; 162, 164; 163, identifier:sign; 164, parenthesized_expression; 164, 165; 165, binary_operator:-; 165, 166; 165, 167; 166, identifier:PTM; 167, identifier:PTE; 168, parenthesized_expression; 168, 169; 169, binary_operator:*; 169, 170; 169, 171; 170, integer:8; 171, attribute; 171, 172; 171, 173; 172, identifier:np; 173, identifier:pi; 174, identifier:lambd; 175, elif_clause; 175, 176; 175, 187; 175, 188; 176, comparison_operator:in; 176, 177; 176, 178; 177, identifier:ab; 178, list:[13, 23, 31, 32, 34, 35, 16, 26]; 178, 179; 178, 180; 178, 181; 178, 182; 178, 183; 178, 184; 178, 185; 178, 186; 179, integer:13; 180, integer:23; 181, integer:31; 182, integer:32; 183, integer:34; 184, integer:35; 185, integer:16; 186, integer:26; 187, comment; 188, block; 188, 189; 188, 199; 188, 200; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:PJ1; 192, binary_operator:*; 192, 193; 192, 198; 193, binary_operator:*; 193, 194; 193, 197; 194, binary_operator:*; 194, 195; 194, 196; 195, identifier:sign; 196, identifier:Ptot; 197, identifier:lambd; 198, identifier:lambd; 199, comment; 200, if_statement; 200, 201; 200, 206; 201, comparison_operator:in; 201, 202; 201, 203; 202, identifier:ab; 203, list:[34, 26]; 203, 204; 203, 205; 204, integer:34; 205, integer:26; 206, block; 206, 207; 207, expression_statement; 207, 208; 208, augmented_assignment:*=; 208, 209; 208, 210; 209, identifier:PJ1; 210, unary_operator:-; 210, 211; 211, integer:1; 212, elif_clause; 212, 213; 212, 217; 212, 218; 213, comparison_operator:in; 213, 214; 213, 215; 214, identifier:ab; 215, list:[33, ]; 215, 216; 216, integer:33; 217, comment; 218, block; 218, 219; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 222; 221, identifier:PJ0; 222, binary_operator:*; 222, 223; 222, 230; 223, binary_operator:*; 223, 224; 223, 229; 224, binary_operator:*; 224, 225; 224, 228; 225, binary_operator:*; 225, 226; 225, 227; 226, identifier:sign; 227, identifier:Ptot; 228, identifier:lambd; 229, identifier:lambd; 230, identifier:lambd; 231, comment; 232, return_statement; 232, 233; 233, expression_list; 233, 234; 233, 235; 233, 236; 234, identifier:PJ0; 235, identifier:PJ1; 236, identifier:PJ0b
def wavenumber(zsrc, zrec, lsrc, lrec, depth, etaH, etaV, zetaH, zetaV, lambd, ab, xdirect, msrc, mrec, use_ne_eval): r"""Calculate wavenumber domain solution. Return the wavenumber domain solutions ``PJ0``, ``PJ1``, and ``PJ0b``, which have to be transformed with a Hankel transform to the frequency domain. ``PJ0``/``PJ0b`` and ``PJ1`` have to be transformed with Bessel functions of order 0 (:math:`J_0`) and 1 (:math:`J_1`), respectively. This function corresponds loosely to equations 105--107, 111--116, 119--121, and 123--128 in [HuTS15]_, and equally loosely to the file ``kxwmod.c``. [HuTS15]_ uses Bessel functions of orders 0, 1, and 2 (:math:`J_0, J_1, J_2`). The implementations of the *Fast Hankel Transform* and the *Quadrature-with-Extrapolation* in ``transform`` are set-up with Bessel functions of order 0 and 1 only. This is achieved by applying the recurrence formula .. math:: J_2(kr) = \frac{2}{kr} J_1(kr) - J_0(kr) \ . .. note:: ``PJ0`` and ``PJ0b`` could theoretically be added here into one, and then be transformed in one go. However, ``PJ0b`` has to be multiplied by ``factAng`` later. This has to be done after the Hankel transform for methods which make use of spline interpolation, in order to work for offsets that are not in line with each other. This function is called from one of the Hankel functions in :mod:`transform`. Consult the modelling routines in :mod:`model` for a description of the input and output parameters. If you are solely interested in the wavenumber-domain solution you can call this function directly. However, you have to make sure all input arguments are correct, as no checks are carried out here. """ # ** CALCULATE GREEN'S FUNCTIONS # Shape of PTM, PTE: (nfreq, noffs, nfilt) PTM, PTE = greenfct(zsrc, zrec, lsrc, lrec, depth, etaH, etaV, zetaH, zetaV, lambd, ab, xdirect, msrc, mrec, use_ne_eval) # ** AB-SPECIFIC COLLECTION OF PJ0, PJ1, AND PJ0b # Pre-allocate output PJ0 = None PJ1 = None PJ0b = None # Calculate Ptot which is used in all cases Ptot = (PTM + PTE)/(4*np.pi) # If rec is magnetic switch sign (reciprocity MM/ME => EE/EM). if mrec: sign = -1 else: sign = 1 # Group into PJ0 and PJ1 for J0/J1 Hankel Transform if ab in [11, 12, 21, 22, 14, 24, 15, 25]: # Eqs 105, 106, 111, 112, # J2(kr) = 2/(kr)*J1(kr) - J0(kr) # 119, 120, 123, 124 if ab in [14, 22]: sign *= -1 PJ0b = sign/2*Ptot*lambd PJ1 = -sign*Ptot if ab in [11, 22, 24, 15]: if ab in [22, 24]: sign *= -1 PJ0 = sign*(PTM - PTE)/(8*np.pi)*lambd elif ab in [13, 23, 31, 32, 34, 35, 16, 26]: # Eqs 107, 113, 114, 115, PJ1 = sign*Ptot*lambd*lambd # . 121, 125, 126, 127 if ab in [34, 26]: PJ1 *= -1 elif ab in [33, ]: # Eq 116 PJ0 = sign*Ptot*lambd*lambd*lambd # Return PJ0, PJ1, PJ0b return PJ0, PJ1, PJ0b
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:reflections; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 4, identifier:depth; 5, identifier:e_zH; 6, identifier:Gam; 7, identifier:lrec; 8, identifier:lsrc; 9, identifier:use_ne_eval; 10, block; 10, 11; 10, 13; 10, 14; 10, 421; 10, 422; 11, expression_statement; 11, 12; 12, comment; 13, comment; 14, for_statement; 14, 15; 14, 16; 14, 19; 14, 20; 15, identifier:plus; 16, list:[True, False]; 16, 17; 16, 18; 17, True; 18, False; 19, comment; 20, block; 20, 21; 20, 104; 20, 105; 20, 106; 20, 119; 20, 135; 20, 144; 20, 145; 20, 180; 20, 181; 20, 391; 20, 392; 20, 407; 20, 408; 21, if_statement; 21, 22; 21, 23; 21, 67; 22, identifier:plus; 23, block; 23, 24; 23, 28; 23, 50; 23, 59; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:pm; 27, integer:1; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:layer_count; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:np; 34, identifier:arange; 35, argument_list; 35, 36; 35, 41; 35, 48; 36, binary_operator:-; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:depth; 39, identifier:size; 40, integer:2; 41, binary_operator:-; 41, 42; 41, 47; 42, call; 42, 43; 42, 44; 43, identifier:min; 44, argument_list; 44, 45; 44, 46; 45, identifier:lrec; 46, identifier:lsrc; 47, integer:1; 48, unary_operator:-; 48, 49; 49, integer:1; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:izout; 53, call; 53, 54; 53, 55; 54, identifier:abs; 55, argument_list; 55, 56; 56, binary_operator:-; 56, 57; 56, 58; 57, identifier:lsrc; 58, identifier:lrec; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:minmax; 62, call; 62, 63; 62, 64; 63, identifier:max; 64, argument_list; 64, 65; 64, 66; 65, identifier:lrec; 66, identifier:lsrc; 67, else_clause; 67, 68; 68, block; 68, 69; 68, 74; 68, 91; 68, 95; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:pm; 72, unary_operator:-; 72, 73; 73, integer:1; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:layer_count; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:np; 80, identifier:arange; 81, argument_list; 81, 82; 81, 83; 81, 90; 82, integer:1; 83, binary_operator:+; 83, 84; 83, 89; 84, call; 84, 85; 84, 86; 85, identifier:max; 86, argument_list; 86, 87; 86, 88; 87, identifier:lrec; 88, identifier:lsrc; 89, integer:1; 90, integer:1; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:izout; 94, integer:0; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:minmax; 98, unary_operator:-; 98, 99; 99, call; 99, 100; 99, 101; 100, identifier:min; 101, argument_list; 101, 102; 101, 103; 102, identifier:lrec; 103, identifier:lsrc; 104, comment; 105, comment; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:shiftplus; 109, boolean_operator:and; 109, 110; 109, 117; 110, boolean_operator:and; 110, 111; 110, 114; 111, comparison_operator:<; 111, 112; 111, 113; 112, identifier:lrec; 113, identifier:lsrc; 114, comparison_operator:==; 114, 115; 114, 116; 115, identifier:lrec; 116, integer:0; 117, not_operator; 117, 118; 118, identifier:plus; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:shiftminus; 122, boolean_operator:and; 122, 123; 122, 134; 123, boolean_operator:and; 123, 124; 123, 127; 124, comparison_operator:>; 124, 125; 124, 126; 125, identifier:lrec; 126, identifier:lsrc; 127, comparison_operator:==; 127, 128; 127, 129; 128, identifier:lrec; 129, binary_operator:-; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:depth; 132, identifier:size; 133, integer:1; 134, identifier:plus; 135, if_statement; 135, 136; 135, 139; 136, boolean_operator:or; 136, 137; 136, 138; 137, identifier:shiftplus; 138, identifier:shiftminus; 139, block; 139, 140; 140, expression_statement; 140, 141; 141, augmented_assignment:-=; 141, 142; 141, 143; 142, identifier:izout; 143, identifier:pm; 144, comment; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:Ref; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:np; 151, identifier:zeros; 152, argument_list; 152, 153; 152, 177; 153, tuple; 153, 154; 153, 159; 153, 164; 153, 172; 154, subscript; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:Gam; 157, identifier:shape; 158, integer:0; 159, subscript; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:Gam; 162, identifier:shape; 163, integer:1; 164, binary_operator:+; 164, 165; 164, 171; 165, call; 165, 166; 165, 167; 166, identifier:abs; 167, argument_list; 167, 168; 168, binary_operator:-; 168, 169; 168, 170; 169, identifier:lsrc; 170, identifier:lrec; 171, integer:1; 172, subscript; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:Gam; 175, identifier:shape; 176, integer:3; 177, keyword_argument; 177, 178; 177, 179; 178, identifier:dtype; 179, identifier:complex; 180, comment; 181, for_statement; 181, 182; 181, 183; 181, 184; 181, 185; 182, identifier:iz; 183, identifier:layer_count; 184, comment; 185, block; 185, 186; 185, 198; 185, 210; 185, 220; 185, 234; 185, 274; 185, 275; 185, 359; 185, 360; 185, 361; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:e_zHa; 189, subscript; 189, 190; 189, 191; 189, 193; 189, 194; 189, 197; 190, identifier:e_zH; 191, slice; 191, 192; 192, colon; 193, None; 194, binary_operator:+; 194, 195; 194, 196; 195, identifier:iz; 196, identifier:pm; 197, None; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:Gama; 201, subscript; 201, 202; 201, 203; 201, 205; 201, 207; 201, 208; 202, identifier:Gam; 203, slice; 203, 204; 204, colon; 205, slice; 205, 206; 206, colon; 207, identifier:iz; 208, slice; 208, 209; 209, colon; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 213; 212, identifier:e_zHb; 213, subscript; 213, 214; 213, 215; 213, 217; 213, 218; 213, 219; 214, identifier:e_zH; 215, slice; 215, 216; 216, colon; 217, None; 218, identifier:iz; 219, None; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 223; 222, identifier:Gamb; 223, subscript; 223, 224; 223, 225; 223, 227; 223, 229; 223, 232; 224, identifier:Gam; 225, slice; 225, 226; 226, colon; 227, slice; 227, 228; 228, colon; 229, binary_operator:+; 229, 230; 229, 231; 230, identifier:iz; 231, identifier:pm; 232, slice; 232, 233; 233, colon; 234, if_statement; 234, 235; 234, 236; 234, 248; 235, identifier:use_ne_eval; 236, block; 236, 237; 236, 241; 237, expression_statement; 237, 238; 238, assignment; 238, 239; 238, 240; 239, identifier:rlocstr; 240, string:"(e_zHa*Gama - e_zHb*Gamb)/(e_zHa*Gama + e_zHb*Gamb)"; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 244; 243, identifier:rloc; 244, call; 244, 245; 244, 246; 245, identifier:use_ne_eval; 246, argument_list; 246, 247; 247, identifier:rlocstr; 248, else_clause; 248, 249; 249, block; 249, 250; 249, 256; 249, 262; 250, expression_statement; 250, 251; 251, assignment; 251, 252; 251, 253; 252, identifier:rloca; 253, binary_operator:*; 253, 254; 253, 255; 254, identifier:e_zHa; 255, identifier:Gama; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 259; 258, identifier:rlocb; 259, binary_operator:*; 259, 260; 259, 261; 260, identifier:e_zHb; 261, identifier:Gamb; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 265; 264, identifier:rloc; 265, binary_operator:/; 265, 266; 265, 270; 266, parenthesized_expression; 266, 267; 267, binary_operator:-; 267, 268; 267, 269; 268, identifier:rloca; 269, identifier:rlocb; 270, parenthesized_expression; 270, 271; 271, binary_operator:+; 271, 272; 271, 273; 272, identifier:rloca; 273, identifier:rlocb; 274, comment; 275, if_statement; 275, 276; 275, 281; 275, 290; 276, comparison_operator:==; 276, 277; 276, 278; 277, identifier:iz; 278, subscript; 278, 279; 278, 280; 279, identifier:layer_count; 280, integer:0; 281, block; 281, 282; 282, expression_statement; 282, 283; 283, assignment; 283, 284; 283, 285; 284, identifier:tRef; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:rloc; 288, identifier:copy; 289, argument_list; 290, else_clause; 290, 291; 291, block; 291, 292; 291, 308; 291, 309; 292, expression_statement; 292, 293; 293, assignment; 293, 294; 293, 295; 294, identifier:ddepth; 295, binary_operator:-; 295, 296; 295, 303; 296, subscript; 296, 297; 296, 298; 297, identifier:depth; 298, binary_operator:+; 298, 299; 298, 302; 299, binary_operator:+; 299, 300; 299, 301; 300, identifier:iz; 301, integer:1; 302, identifier:pm; 303, subscript; 303, 304; 303, 305; 304, identifier:depth; 305, binary_operator:+; 305, 306; 305, 307; 306, identifier:iz; 307, identifier:pm; 308, comment; 309, if_statement; 309, 310; 309, 311; 309, 326; 310, identifier:use_ne_eval; 311, block; 311, 312; 311, 319; 312, expression_statement; 312, 313; 313, assignment; 313, 314; 313, 315; 314, identifier:term; 315, call; 315, 316; 315, 317; 316, identifier:use_ne_eval; 317, argument_list; 317, 318; 318, string:"tRef*exp(-2*Gamb*ddepth)"; 319, expression_statement; 319, 320; 320, assignment; 320, 321; 320, 322; 321, identifier:tRef; 322, call; 322, 323; 322, 324; 323, identifier:use_ne_eval; 324, argument_list; 324, 325; 325, string:"(rloc + term)/(1 + rloc*term)"; 326, else_clause; 326, 327; 327, block; 327, 328; 327, 344; 327, 345; 328, expression_statement; 328, 329; 329, assignment; 329, 330; 329, 331; 330, identifier:term; 331, binary_operator:*; 331, 332; 331, 333; 332, identifier:tRef; 333, call; 333, 334; 333, 337; 334, attribute; 334, 335; 334, 336; 335, identifier:np; 336, identifier:exp; 337, argument_list; 337, 338; 338, binary_operator:*; 338, 339; 338, 343; 339, binary_operator:*; 339, 340; 339, 342; 340, unary_operator:-; 340, 341; 341, integer:2; 342, identifier:Gamb; 343, identifier:ddepth; 344, comment; 345, expression_statement; 345, 346; 346, assignment; 346, 347; 346, 348; 347, identifier:tRef; 348, binary_operator:/; 348, 349; 348, 353; 349, parenthesized_expression; 349, 350; 350, binary_operator:+; 350, 351; 350, 352; 351, identifier:rloc; 352, identifier:term; 353, parenthesized_expression; 353, 354; 354, binary_operator:+; 354, 355; 354, 356; 355, integer:1; 356, binary_operator:*; 356, 357; 356, 358; 357, identifier:rloc; 358, identifier:term; 359, comment; 360, comment; 361, if_statement; 361, 362; 361, 371; 362, boolean_operator:and; 362, 363; 362, 366; 363, comparison_operator:!=; 363, 364; 363, 365; 364, identifier:lrec; 365, identifier:lsrc; 366, comparison_operator:<=; 366, 367; 366, 370; 367, binary_operator:*; 367, 368; 367, 369; 368, identifier:pm; 369, identifier:iz; 370, identifier:minmax; 371, block; 371, 372; 371, 387; 372, expression_statement; 372, 373; 373, assignment; 373, 374; 373, 383; 374, subscript; 374, 375; 374, 376; 374, 378; 374, 380; 374, 381; 375, identifier:Ref; 376, slice; 376, 377; 377, colon; 378, slice; 378, 379; 379, colon; 380, identifier:izout; 381, slice; 381, 382; 382, colon; 383, subscript; 383, 384; 383, 385; 384, identifier:tRef; 385, slice; 385, 386; 386, colon; 387, expression_statement; 387, 388; 388, augmented_assignment:-=; 388, 389; 388, 390; 389, identifier:izout; 390, identifier:pm; 391, comment; 392, if_statement; 392, 393; 392, 402; 393, boolean_operator:and; 393, 394; 393, 397; 394, comparison_operator:==; 394, 395; 394, 396; 395, identifier:lsrc; 396, identifier:lrec; 397, comparison_operator:>; 397, 398; 397, 401; 398, attribute; 398, 399; 398, 400; 399, identifier:layer_count; 400, identifier:size; 401, integer:0; 402, block; 402, 403; 403, expression_statement; 403, 404; 404, assignment; 404, 405; 404, 406; 405, identifier:Ref; 406, identifier:tRef; 407, comment; 408, if_statement; 408, 409; 408, 410; 408, 415; 409, identifier:plus; 410, block; 410, 411; 411, expression_statement; 411, 412; 412, assignment; 412, 413; 412, 414; 413, identifier:Rm; 414, identifier:Ref; 415, else_clause; 415, 416; 416, block; 416, 417; 417, expression_statement; 417, 418; 418, assignment; 418, 419; 418, 420; 419, identifier:Rp; 420, identifier:Ref; 421, comment; 422, return_statement; 422, 423; 423, expression_list; 423, 424; 423, 425; 424, identifier:Rm; 425, identifier:Rp
def reflections(depth, e_zH, Gam, lrec, lsrc, use_ne_eval): r"""Calculate Rp, Rm. .. math:: R^\pm_n, \bar{R}^\pm_n This function corresponds to equations 64/65 and A-11/A-12 in [HuTS15]_, and loosely to the corresponding files ``Rmin.F90`` and ``Rplus.F90``. This function is called from the function :mod:`kernel.greenfct`. """ # Loop over Rp, Rm for plus in [True, False]: # Switches depending if plus or minus if plus: pm = 1 layer_count = np.arange(depth.size-2, min(lrec, lsrc)-1, -1) izout = abs(lsrc-lrec) minmax = max(lrec, lsrc) else: pm = -1 layer_count = np.arange(1, max(lrec, lsrc)+1, 1) izout = 0 minmax = -min(lrec, lsrc) # If rec in last and rec below src (plus) or # if rec in first and rec above src (minus), shift izout shiftplus = lrec < lsrc and lrec == 0 and not plus shiftminus = lrec > lsrc and lrec == depth.size-1 and plus if shiftplus or shiftminus: izout -= pm # Pre-allocate Ref Ref = np.zeros((Gam.shape[0], Gam.shape[1], abs(lsrc-lrec)+1, Gam.shape[3]), dtype=complex) # Calculate the reflection for iz in layer_count: # Eqs 65, A-12 e_zHa = e_zH[:, None, iz+pm, None] Gama = Gam[:, :, iz, :] e_zHb = e_zH[:, None, iz, None] Gamb = Gam[:, :, iz+pm, :] if use_ne_eval: rlocstr = "(e_zHa*Gama - e_zHb*Gamb)/(e_zHa*Gama + e_zHb*Gamb)" rloc = use_ne_eval(rlocstr) else: rloca = e_zHa*Gama rlocb = e_zHb*Gamb rloc = (rloca - rlocb)/(rloca + rlocb) # In first layer tRef = rloc if iz == layer_count[0]: tRef = rloc.copy() else: ddepth = depth[iz+1+pm]-depth[iz+pm] # Eqs 64, A-11 if use_ne_eval: term = use_ne_eval("tRef*exp(-2*Gamb*ddepth)") tRef = use_ne_eval("(rloc + term)/(1 + rloc*term)") else: term = tRef*np.exp(-2*Gamb*ddepth) # NOQA tRef = (rloc + term)/(1 + rloc*term) # The global reflection coefficient is given back for all layers # between and including src- and rec-layer if lrec != lsrc and pm*iz <= minmax: Ref[:, :, izout, :] = tRef[:] izout -= pm # If lsrc = lrec, we just store the last values if lsrc == lrec and layer_count.size > 0: Ref = tRef # Store Ref in Rm/Rp if plus: Rm = Ref else: Rp = Ref # Return reflections (minus and plus) return Rm, Rp
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 21; 2, function_name:hquad; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 3, 12; 3, 13; 3, 14; 3, 15; 3, 16; 3, 17; 3, 18; 3, 19; 3, 20; 4, identifier:zsrc; 5, identifier:zrec; 6, identifier:lsrc; 7, identifier:lrec; 8, identifier:off; 9, identifier:factAng; 10, identifier:depth; 11, identifier:ab; 12, identifier:etaH; 13, identifier:etaV; 14, identifier:zetaH; 15, identifier:zetaV; 16, identifier:xdirect; 17, identifier:quadargs; 18, identifier:use_ne_eval; 19, identifier:msrc; 20, identifier:mrec; 21, block; 21, 22; 21, 24; 21, 25; 21, 35; 21, 36; 21, 45; 21, 54; 21, 72; 21, 73; 21, 104; 21, 105; 21, 106; 21, 107; 21, 152; 21, 197; 21, 242; 21, 243; 21, 257; 21, 261; 21, 262; 21, 281; 21, 282; 21, 319; 21, 320; 21, 321; 22, expression_statement; 22, 23; 23, comment; 24, comment; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 34; 27, pattern_list; 27, 28; 27, 29; 27, 30; 27, 31; 27, 32; 27, 33; 28, identifier:rtol; 29, identifier:atol; 30, identifier:limit; 31, identifier:a; 32, identifier:b; 33, identifier:pts_per_dec; 34, identifier:quadargs; 35, comment; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:la; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:np; 42, identifier:log10; 43, argument_list; 43, 44; 44, identifier:a; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:lb; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:np; 51, identifier:log10; 52, argument_list; 52, 53; 53, identifier:b; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:ilambd; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:np; 60, identifier:logspace; 61, argument_list; 61, 62; 61, 63; 61, 64; 62, identifier:la; 63, identifier:lb; 64, binary_operator:+; 64, 65; 64, 71; 65, binary_operator:*; 65, 66; 65, 70; 66, parenthesized_expression; 66, 67; 67, binary_operator:-; 67, 68; 67, 69; 68, identifier:lb; 69, identifier:la; 70, identifier:pts_per_dec; 71, integer:1; 72, comment; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 79; 75, pattern_list; 75, 76; 75, 77; 75, 78; 76, identifier:PJ0; 77, identifier:PJ1; 78, identifier:PJ0b; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:kernel; 82, identifier:wavenumber; 83, argument_list; 83, 84; 83, 85; 83, 86; 83, 87; 83, 88; 83, 89; 83, 90; 83, 91; 83, 92; 83, 93; 83, 99; 83, 100; 83, 101; 83, 102; 83, 103; 84, identifier:zsrc; 85, identifier:zrec; 86, identifier:lsrc; 87, identifier:lrec; 88, identifier:depth; 89, identifier:etaH; 90, identifier:etaV; 91, identifier:zetaH; 92, identifier:zetaV; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:np; 96, identifier:atleast_2d; 97, argument_list; 97, 98; 98, identifier:ilambd; 99, identifier:ab; 100, identifier:xdirect; 101, identifier:msrc; 102, identifier:mrec; 103, identifier:use_ne_eval; 104, comment; 105, comment; 106, comment; 107, if_statement; 107, 108; 107, 111; 107, 142; 108, comparison_operator:is; 108, 109; 108, 110; 109, identifier:PJ0; 110, None; 111, block; 111, 112; 111, 127; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:sPJ0r; 115, call; 115, 116; 115, 117; 116, identifier:iuSpline; 117, argument_list; 117, 118; 117, 124; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:np; 121, identifier:log; 122, argument_list; 122, 123; 123, identifier:ilambd; 124, attribute; 124, 125; 124, 126; 125, identifier:PJ0; 126, identifier:real; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:sPJ0i; 130, call; 130, 131; 130, 132; 131, identifier:iuSpline; 132, argument_list; 132, 133; 132, 139; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:np; 136, identifier:log; 137, argument_list; 137, 138; 138, identifier:ilambd; 139, attribute; 139, 140; 139, 141; 140, identifier:PJ0; 141, identifier:imag; 142, else_clause; 142, 143; 143, block; 143, 144; 143, 148; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:sPJ0r; 147, None; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:sPJ0i; 151, None; 152, if_statement; 152, 153; 152, 156; 152, 187; 153, comparison_operator:is; 153, 154; 153, 155; 154, identifier:PJ1; 155, None; 156, block; 156, 157; 156, 172; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:sPJ1r; 160, call; 160, 161; 160, 162; 161, identifier:iuSpline; 162, argument_list; 162, 163; 162, 169; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:np; 166, identifier:log; 167, argument_list; 167, 168; 168, identifier:ilambd; 169, attribute; 169, 170; 169, 171; 170, identifier:PJ1; 171, identifier:real; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:sPJ1i; 175, call; 175, 176; 175, 177; 176, identifier:iuSpline; 177, argument_list; 177, 178; 177, 184; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:np; 181, identifier:log; 182, argument_list; 182, 183; 183, identifier:ilambd; 184, attribute; 184, 185; 184, 186; 185, identifier:PJ1; 186, identifier:imag; 187, else_clause; 187, 188; 188, block; 188, 189; 188, 193; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:sPJ1r; 192, None; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:sPJ1i; 196, None; 197, if_statement; 197, 198; 197, 201; 197, 232; 198, comparison_operator:is; 198, 199; 198, 200; 199, identifier:PJ0b; 200, None; 201, block; 201, 202; 201, 217; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 205; 204, identifier:sPJ0br; 205, call; 205, 206; 205, 207; 206, identifier:iuSpline; 207, argument_list; 207, 208; 207, 214; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:np; 211, identifier:log; 212, argument_list; 212, 213; 213, identifier:ilambd; 214, attribute; 214, 215; 214, 216; 215, identifier:PJ0b; 216, identifier:real; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:sPJ0bi; 220, call; 220, 221; 220, 222; 221, identifier:iuSpline; 222, argument_list; 222, 223; 222, 229; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:np; 226, identifier:log; 227, argument_list; 227, 228; 228, identifier:ilambd; 229, attribute; 229, 230; 229, 231; 230, identifier:PJ0b; 231, identifier:imag; 232, else_clause; 232, 233; 233, block; 233, 234; 233, 238; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 237; 236, identifier:sPJ0br; 237, None; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 241; 240, identifier:sPJ0bi; 241, None; 242, comment; 243, expression_statement; 243, 244; 244, assignment; 244, 245; 244, 246; 245, identifier:fEM; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:np; 249, identifier:zeros; 250, argument_list; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:off; 253, identifier:size; 254, keyword_argument; 254, 255; 254, 256; 255, identifier:dtype; 256, identifier:complex; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 260; 259, identifier:conv; 260, True; 261, comment; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 265; 264, identifier:iinp; 265, dictionary; 265, 266; 265, 269; 265, 272; 265, 275; 265, 278; 266, pair; 266, 267; 266, 268; 267, string:'a'; 268, identifier:a; 269, pair; 269, 270; 269, 271; 270, string:'b'; 271, identifier:b; 272, pair; 272, 273; 272, 274; 273, string:'epsabs'; 274, identifier:atol; 275, pair; 275, 276; 275, 277; 276, string:'epsrel'; 277, identifier:rtol; 278, pair; 278, 279; 278, 280; 279, string:'limit'; 280, identifier:limit; 281, comment; 282, for_statement; 282, 283; 282, 284; 282, 290; 283, identifier:i; 284, call; 284, 285; 284, 286; 285, identifier:range; 286, argument_list; 286, 287; 287, attribute; 287, 288; 287, 289; 288, identifier:off; 289, identifier:size; 290, block; 290, 291; 290, 315; 291, expression_statement; 291, 292; 292, assignment; 292, 293; 292, 298; 293, pattern_list; 293, 294; 293, 297; 294, subscript; 294, 295; 294, 296; 295, identifier:fEM; 296, identifier:i; 297, identifier:tc; 298, call; 298, 299; 298, 300; 299, identifier:quad; 300, argument_list; 300, 301; 300, 302; 300, 303; 300, 304; 300, 305; 300, 306; 300, 307; 300, 308; 300, 311; 300, 314; 301, identifier:sPJ0r; 302, identifier:sPJ0i; 303, identifier:sPJ1r; 304, identifier:sPJ1i; 305, identifier:sPJ0br; 306, identifier:sPJ0bi; 307, identifier:ab; 308, subscript; 308, 309; 308, 310; 309, identifier:off; 310, identifier:i; 311, subscript; 311, 312; 311, 313; 312, identifier:factAng; 313, identifier:i; 314, identifier:iinp; 315, expression_statement; 315, 316; 316, augmented_assignment:*=; 316, 317; 316, 318; 317, identifier:conv; 318, identifier:tc; 319, comment; 320, comment; 321, return_statement; 321, 322; 322, expression_list; 322, 323; 322, 324; 322, 325; 323, identifier:fEM; 324, integer:1; 325, identifier:conv
def hquad(zsrc, zrec, lsrc, lrec, off, factAng, depth, ab, etaH, etaV, zetaH, zetaV, xdirect, quadargs, use_ne_eval, msrc, mrec): r"""Hankel Transform using the ``QUADPACK`` library. This routine uses the ``scipy.integrate.quad`` module, which in turn makes use of the Fortran library ``QUADPACK`` (``qagse``). It is massively (orders of magnitudes) slower than either ``fht`` or ``hqwe``, and is mainly here for completeness and comparison purposes. It always uses interpolation in the wavenumber domain, hence it generally will not be as precise as the other methods. However, it might work in some areas where the others fail. The function is called from one of the modelling routines in :mod:`model`. Consult these modelling routines for a description of the input and output parameters. Returns ------- fEM : array Returns frequency-domain EM response. kcount : int Kernel count. For HQUAD, this is 1. conv : bool If true, QUAD converged. If not, <htarg> might have to be adjusted. """ # Get quadargs rtol, atol, limit, a, b, pts_per_dec = quadargs # Get required lambdas la = np.log10(a) lb = np.log10(b) ilambd = np.logspace(la, lb, (lb-la)*pts_per_dec + 1) # Call the kernel PJ0, PJ1, PJ0b = kernel.wavenumber(zsrc, zrec, lsrc, lrec, depth, etaH, etaV, zetaH, zetaV, np.atleast_2d(ilambd), ab, xdirect, msrc, mrec, use_ne_eval) # Interpolation in wavenumber domain: Has to be done separately on each PJ, # in order to work with multiple offsets which have different angles. # We check if the kernels are zero, to avoid unnecessary calculations. if PJ0 is not None: sPJ0r = iuSpline(np.log(ilambd), PJ0.real) sPJ0i = iuSpline(np.log(ilambd), PJ0.imag) else: sPJ0r = None sPJ0i = None if PJ1 is not None: sPJ1r = iuSpline(np.log(ilambd), PJ1.real) sPJ1i = iuSpline(np.log(ilambd), PJ1.imag) else: sPJ1r = None sPJ1i = None if PJ0b is not None: sPJ0br = iuSpline(np.log(ilambd), PJ0b.real) sPJ0bi = iuSpline(np.log(ilambd), PJ0b.imag) else: sPJ0br = None sPJ0bi = None # Pre-allocate output array fEM = np.zeros(off.size, dtype=complex) conv = True # Input-dictionary for quad iinp = {'a': a, 'b': b, 'epsabs': atol, 'epsrel': rtol, 'limit': limit} # Loop over offsets for i in range(off.size): fEM[i], tc = quad(sPJ0r, sPJ0i, sPJ1r, sPJ1i, sPJ0br, sPJ0bi, ab, off[i], factAng[i], iinp) conv *= tc # Return the electromagnetic field # Second argument (1) is the kernel count, last argument is only for QWE. return fEM, 1, conv
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:quad; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 3, 12; 3, 13; 4, identifier:sPJ0r; 5, identifier:sPJ0i; 6, identifier:sPJ1r; 7, identifier:sPJ1i; 8, identifier:sPJ0br; 9, identifier:sPJ0bi; 10, identifier:ab; 11, identifier:off; 12, identifier:factAng; 13, identifier:iinp; 14, block; 14, 15; 14, 17; 14, 18; 14, 46; 14, 101; 14, 132; 14, 133; 14, 137; 14, 148; 14, 149; 14, 155; 14, 223; 14, 295; 14, 365; 14, 366; 15, expression_statement; 15, 16; 16, comment; 17, comment; 18, function_definition; 18, 19; 18, 20; 18, 24; 19, function_name:quad_PJ0; 20, parameters; 20, 21; 20, 22; 20, 23; 21, identifier:klambd; 22, identifier:sPJ0; 23, identifier:koff; 24, block; 24, 25; 24, 27; 25, expression_statement; 25, 26; 26, comment; 27, return_statement; 27, 28; 28, binary_operator:*; 28, 29; 28, 38; 29, call; 29, 30; 29, 31; 30, identifier:sPJ0; 31, argument_list; 31, 32; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:np; 35, identifier:log; 36, argument_list; 36, 37; 37, identifier:klambd; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:special; 41, identifier:j0; 42, argument_list; 42, 43; 43, binary_operator:*; 43, 44; 43, 45; 44, identifier:koff; 45, identifier:klambd; 46, function_definition; 46, 47; 46, 48; 46, 54; 47, function_name:quad_PJ1; 48, parameters; 48, 49; 48, 50; 48, 51; 48, 52; 48, 53; 49, identifier:klambd; 50, identifier:sPJ1; 51, identifier:ab; 52, identifier:koff; 53, identifier:kang; 54, block; 54, 55; 54, 57; 54, 71; 54, 90; 55, expression_statement; 55, 56; 56, comment; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:tP1; 60, binary_operator:*; 60, 61; 60, 62; 61, identifier:kang; 62, call; 62, 63; 62, 64; 63, identifier:sPJ1; 64, argument_list; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:np; 68, identifier:log; 69, argument_list; 69, 70; 70, identifier:klambd; 71, if_statement; 71, 72; 71, 83; 71, 84; 71, 85; 72, comparison_operator:in; 72, 73; 72, 74; 73, identifier:ab; 74, list:[11, 12, 21, 22, 14, 24, 15, 25]; 74, 75; 74, 76; 74, 77; 74, 78; 74, 79; 74, 80; 74, 81; 74, 82; 75, integer:11; 76, integer:12; 77, integer:21; 78, integer:22; 79, integer:14; 80, integer:24; 81, integer:15; 82, integer:25; 83, comment; 84, comment; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, augmented_assignment:/=; 87, 88; 87, 89; 88, identifier:tP1; 89, identifier:koff; 90, return_statement; 90, 91; 91, binary_operator:*; 91, 92; 91, 93; 92, identifier:tP1; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:special; 96, identifier:j1; 97, argument_list; 97, 98; 98, binary_operator:*; 98, 99; 98, 100; 99, identifier:koff; 100, identifier:klambd; 101, function_definition; 101, 102; 101, 103; 101, 108; 102, function_name:quad_PJ0b; 103, parameters; 103, 104; 103, 105; 103, 106; 103, 107; 104, identifier:klambd; 105, identifier:sPJ0b; 106, identifier:koff; 107, identifier:kang; 108, block; 108, 109; 108, 111; 109, expression_statement; 109, 110; 110, comment; 111, return_statement; 111, 112; 112, binary_operator:*; 112, 113; 112, 124; 113, binary_operator:*; 113, 114; 113, 115; 114, identifier:kang; 115, call; 115, 116; 115, 117; 116, identifier:sPJ0b; 117, argument_list; 117, 118; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:np; 121, identifier:log; 122, argument_list; 122, 123; 123, identifier:klambd; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:special; 127, identifier:j0; 128, argument_list; 128, 129; 129, binary_operator:*; 129, 130; 129, 131; 130, identifier:koff; 131, identifier:klambd; 132, comment; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:conv; 136, True; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:out; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:np; 143, identifier:array; 144, argument_list; 144, 145; 145, binary_operator:+; 145, 146; 145, 147; 146, float:0.0; 147, float:0.0j; 148, comment; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 154; 151, subscript; 151, 152; 151, 153; 152, identifier:iinp; 153, string:'full_output'; 154, integer:1; 155, if_statement; 155, 156; 155, 159; 156, comparison_operator:is; 156, 157; 156, 158; 157, identifier:sPJ0r; 158, None; 159, block; 159, 160; 159, 176; 159, 192; 159, 204; 159, 205; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:re; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:integrate; 166, identifier:quad; 167, argument_list; 167, 168; 167, 169; 167, 174; 168, identifier:quad_PJ0; 169, keyword_argument; 169, 170; 169, 171; 170, identifier:args; 171, tuple; 171, 172; 171, 173; 172, identifier:sPJ0r; 173, identifier:off; 174, dictionary_splat; 174, 175; 175, identifier:iinp; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:im; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:integrate; 182, identifier:quad; 183, argument_list; 183, 184; 183, 185; 183, 190; 184, identifier:quad_PJ0; 185, keyword_argument; 185, 186; 185, 187; 186, identifier:args; 187, tuple; 187, 188; 187, 189; 188, identifier:sPJ0i; 189, identifier:off; 190, dictionary_splat; 190, 191; 191, identifier:iinp; 192, expression_statement; 192, 193; 193, augmented_assignment:+=; 193, 194; 193, 195; 194, identifier:out; 195, binary_operator:+; 195, 196; 195, 199; 196, subscript; 196, 197; 196, 198; 197, identifier:re; 198, integer:0; 199, binary_operator:*; 199, 200; 199, 201; 200, integer:1j; 201, subscript; 201, 202; 201, 203; 202, identifier:im; 203, integer:0; 204, comment; 205, if_statement; 205, 206; 205, 218; 206, comparison_operator:>; 206, 207; 206, 217; 207, parenthesized_expression; 207, 208; 208, boolean_operator:or; 208, 209; 208, 213; 209, call; 209, 210; 209, 211; 210, identifier:len; 211, argument_list; 211, 212; 212, identifier:re; 213, call; 213, 214; 213, 215; 214, identifier:len; 215, argument_list; 215, 216; 216, identifier:im; 217, integer:3; 218, block; 218, 219; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 222; 221, identifier:conv; 222, False; 223, if_statement; 223, 224; 223, 227; 224, comparison_operator:is; 224, 225; 224, 226; 225, identifier:sPJ1r; 226, None; 227, block; 227, 228; 227, 246; 227, 264; 227, 276; 227, 277; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:re; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:integrate; 234, identifier:quad; 235, argument_list; 235, 236; 235, 237; 235, 244; 236, identifier:quad_PJ1; 237, keyword_argument; 237, 238; 237, 239; 238, identifier:args; 239, tuple; 239, 240; 239, 241; 239, 242; 239, 243; 240, identifier:sPJ1r; 241, identifier:ab; 242, identifier:off; 243, identifier:factAng; 244, dictionary_splat; 244, 245; 245, identifier:iinp; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:im; 249, call; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:integrate; 252, identifier:quad; 253, argument_list; 253, 254; 253, 255; 253, 262; 254, identifier:quad_PJ1; 255, keyword_argument; 255, 256; 255, 257; 256, identifier:args; 257, tuple; 257, 258; 257, 259; 257, 260; 257, 261; 258, identifier:sPJ1i; 259, identifier:ab; 260, identifier:off; 261, identifier:factAng; 262, dictionary_splat; 262, 263; 263, identifier:iinp; 264, expression_statement; 264, 265; 265, augmented_assignment:+=; 265, 266; 265, 267; 266, identifier:out; 267, binary_operator:+; 267, 268; 267, 271; 268, subscript; 268, 269; 268, 270; 269, identifier:re; 270, integer:0; 271, binary_operator:*; 271, 272; 271, 273; 272, integer:1j; 273, subscript; 273, 274; 273, 275; 274, identifier:im; 275, integer:0; 276, comment; 277, if_statement; 277, 278; 277, 290; 278, comparison_operator:>; 278, 279; 278, 289; 279, parenthesized_expression; 279, 280; 280, boolean_operator:or; 280, 281; 280, 285; 281, call; 281, 282; 281, 283; 282, identifier:len; 283, argument_list; 283, 284; 284, identifier:re; 285, call; 285, 286; 285, 287; 286, identifier:len; 287, argument_list; 287, 288; 288, identifier:im; 289, integer:3; 290, block; 290, 291; 291, expression_statement; 291, 292; 292, assignment; 292, 293; 292, 294; 293, identifier:conv; 294, False; 295, if_statement; 295, 296; 295, 299; 296, comparison_operator:is; 296, 297; 296, 298; 297, identifier:sPJ0br; 298, None; 299, block; 299, 300; 299, 317; 299, 334; 299, 346; 299, 347; 300, expression_statement; 300, 301; 301, assignment; 301, 302; 301, 303; 302, identifier:re; 303, call; 303, 304; 303, 307; 304, attribute; 304, 305; 304, 306; 305, identifier:integrate; 306, identifier:quad; 307, argument_list; 307, 308; 307, 309; 307, 315; 308, identifier:quad_PJ0b; 309, keyword_argument; 309, 310; 309, 311; 310, identifier:args; 311, tuple; 311, 312; 311, 313; 311, 314; 312, identifier:sPJ0br; 313, identifier:off; 314, identifier:factAng; 315, dictionary_splat; 315, 316; 316, identifier:iinp; 317, expression_statement; 317, 318; 318, assignment; 318, 319; 318, 320; 319, identifier:im; 320, call; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:integrate; 323, identifier:quad; 324, argument_list; 324, 325; 324, 326; 324, 332; 325, identifier:quad_PJ0b; 326, keyword_argument; 326, 327; 326, 328; 327, identifier:args; 328, tuple; 328, 329; 328, 330; 328, 331; 329, identifier:sPJ0bi; 330, identifier:off; 331, identifier:factAng; 332, dictionary_splat; 332, 333; 333, identifier:iinp; 334, expression_statement; 334, 335; 335, augmented_assignment:+=; 335, 336; 335, 337; 336, identifier:out; 337, binary_operator:+; 337, 338; 337, 341; 338, subscript; 338, 339; 338, 340; 339, identifier:re; 340, integer:0; 341, binary_operator:*; 341, 342; 341, 343; 342, integer:1j; 343, subscript; 343, 344; 343, 345; 344, identifier:im; 345, integer:0; 346, comment; 347, if_statement; 347, 348; 347, 360; 348, comparison_operator:>; 348, 349; 348, 359; 349, parenthesized_expression; 349, 350; 350, boolean_operator:or; 350, 351; 350, 355; 351, call; 351, 352; 351, 353; 352, identifier:len; 353, argument_list; 353, 354; 354, identifier:re; 355, call; 355, 356; 355, 357; 356, identifier:len; 357, argument_list; 357, 358; 358, identifier:im; 359, integer:3; 360, block; 360, 361; 361, expression_statement; 361, 362; 362, assignment; 362, 363; 362, 364; 363, identifier:conv; 364, False; 365, comment; 366, return_statement; 366, 367; 367, expression_list; 367, 368; 367, 369; 368, identifier:out; 369, identifier:conv
def quad(sPJ0r, sPJ0i, sPJ1r, sPJ1i, sPJ0br, sPJ0bi, ab, off, factAng, iinp): r"""Quadrature for Hankel transform. This is the kernel of the QUAD method, used for the Hankel transforms ``hquad`` and ``hqwe`` (where the integral is not suited for QWE). """ # Define the quadrature kernels def quad_PJ0(klambd, sPJ0, koff): r"""Quadrature for PJ0.""" return sPJ0(np.log(klambd))*special.j0(koff*klambd) def quad_PJ1(klambd, sPJ1, ab, koff, kang): r"""Quadrature for PJ1.""" tP1 = kang*sPJ1(np.log(klambd)) if ab in [11, 12, 21, 22, 14, 24, 15, 25]: # Because of J2 # J2(kr) = 2/(kr)*J1(kr) - J0(kr) tP1 /= koff return tP1*special.j1(koff*klambd) def quad_PJ0b(klambd, sPJ0b, koff, kang): r"""Quadrature for PJ0b.""" return kang*sPJ0b(np.log(klambd))*special.j0(koff*klambd) # Pre-allocate output conv = True out = np.array(0.0+0.0j) # Carry out quadrature for required kernels iinp['full_output'] = 1 if sPJ0r is not None: re = integrate.quad(quad_PJ0, args=(sPJ0r, off), **iinp) im = integrate.quad(quad_PJ0, args=(sPJ0i, off), **iinp) out += re[0] + 1j*im[0] # If there is a fourth output from QUAD, it means it did not converge if (len(re) or len(im)) > 3: conv = False if sPJ1r is not None: re = integrate.quad(quad_PJ1, args=(sPJ1r, ab, off, factAng), **iinp) im = integrate.quad(quad_PJ1, args=(sPJ1i, ab, off, factAng), **iinp) out += re[0] + 1j*im[0] # If there is a fourth output from QUAD, it means it did not converge if (len(re) or len(im)) > 3: conv = False if sPJ0br is not None: re = integrate.quad(quad_PJ0b, args=(sPJ0br, off, factAng), **iinp) im = integrate.quad(quad_PJ0b, args=(sPJ0bi, off, factAng), **iinp) out += re[0] + 1j*im[0] # If there is a fourth output from QUAD, it means it did not converge if (len(re) or len(im)) > 3: conv = False # Collect the results return out, conv
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:is_participle_clause_fragment; 3, parameters; 3, 4; 4, identifier:sentence; 5, block; 5, 6; 5, 8; 5, 9; 5, 10; 5, 23; 5, 58; 5, 59; 5, 60; 5, 122; 5, 147; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, comment; 10, if_statement; 10, 11; 10, 20; 11, not_operator; 11, 12; 12, call; 12, 13; 12, 14; 13, identifier:_begins_with_one_of; 14, argument_list; 14, 15; 14, 16; 15, identifier:sentence; 16, list:['VBG', 'VBN', 'JJ']; 16, 17; 16, 18; 16, 19; 17, string:'VBG'; 18, string:'VBN'; 19, string:'JJ'; 20, block; 20, 21; 21, return_statement; 21, 22; 22, float:0.0; 23, if_statement; 23, 24; 23, 30; 24, call; 24, 25; 24, 26; 25, identifier:_begins_with_one_of; 26, argument_list; 26, 27; 26, 28; 27, identifier:sentence; 28, list:['JJ']; 28, 29; 29, string:'JJ'; 30, block; 30, 31; 30, 38; 30, 48; 30, 49; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:doc; 34, call; 34, 35; 34, 36; 35, identifier:nlp; 36, argument_list; 36, 37; 37, identifier:sentence; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:fw; 41, subscript; 41, 42; 41, 47; 42, list_comprehension; 42, 43; 42, 44; 43, identifier:w; 44, for_in_clause; 44, 45; 44, 46; 45, identifier:w; 46, identifier:doc; 47, integer:0; 48, comment; 49, if_statement; 49, 50; 49, 55; 50, comparison_operator:==; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:fw; 53, identifier:dep_; 54, string:'amod'; 55, block; 55, 56; 56, return_statement; 56, 57; 57, float:0.0; 58, comment; 59, comment; 60, if_statement; 60, 61; 60, 67; 61, call; 61, 62; 61, 63; 62, identifier:_begins_with_one_of; 63, argument_list; 63, 64; 63, 65; 64, identifier:sentence; 65, list:['VBG']; 65, 66; 66, string:'VBG'; 67, block; 67, 68; 67, 75; 67, 85; 67, 86; 67, 98; 67, 108; 67, 109; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:doc; 71, call; 71, 72; 71, 73; 72, identifier:nlp; 73, argument_list; 73, 74; 74, identifier:sentence; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:fw; 78, subscript; 78, 79; 78, 84; 79, list_comprehension; 79, 80; 79, 81; 80, identifier:w; 81, for_in_clause; 81, 82; 81, 83; 82, identifier:w; 83, identifier:doc; 84, integer:0; 85, comment; 86, if_statement; 86, 87; 86, 95; 87, call; 87, 88; 87, 93; 88, attribute; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:fw; 91, identifier:dep_; 92, identifier:endswith; 93, argument_list; 93, 94; 94, string:'subj'; 95, block; 95, 96; 96, return_statement; 96, 97; 97, float:0.0; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:fc; 101, list_comprehension; 101, 102; 101, 103; 102, identifier:c; 103, for_in_clause; 103, 104; 103, 105; 104, identifier:c; 105, attribute; 105, 106; 105, 107; 106, identifier:doc; 107, identifier:noun_chunks; 108, comment; 109, if_statement; 109, 110; 109, 119; 110, comparison_operator:in; 110, 111; 110, 115; 111, call; 111, 112; 111, 113; 112, identifier:str; 113, argument_list; 113, 114; 114, identifier:fw; 115, call; 115, 116; 115, 117; 116, identifier:str; 117, argument_list; 117, 118; 118, identifier:fc; 119, block; 119, 120; 120, return_statement; 120, 121; 121, float:0.0; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:positive_prob; 125, subscript; 125, 126; 125, 146; 126, subscript; 126, 127; 126, 145; 127, call; 127, 128; 127, 133; 128, attribute; 128, 129; 128, 132; 129, subscript; 129, 130; 129, 131; 130, identifier:models; 131, string:'participle'; 132, identifier:predict; 133, argument_list; 133, 134; 134, list:[_text_to_vector(sentence, trigram2idx['participle'], trigram_count['participle'])]; 134, 135; 135, call; 135, 136; 135, 137; 136, identifier:_text_to_vector; 137, argument_list; 137, 138; 137, 139; 137, 142; 138, identifier:sentence; 139, subscript; 139, 140; 139, 141; 140, identifier:trigram2idx; 141, string:'participle'; 142, subscript; 142, 143; 142, 144; 143, identifier:trigram_count; 144, string:'participle'; 145, integer:0; 146, integer:1; 147, return_statement; 147, 148; 148, call; 148, 149; 148, 150; 149, identifier:float; 150, argument_list; 150, 151; 151, identifier:positive_prob
def is_participle_clause_fragment(sentence): """Supply a sentence or fragment and recieve a confidence interval""" # short circuit if sentence or fragment doesn't start with a participle # past participles can sometimes look like adjectives -- ie, Tired if not _begins_with_one_of(sentence, ['VBG', 'VBN', 'JJ']): return 0.0 if _begins_with_one_of(sentence, ['JJ']): doc = nlp(sentence) fw = [w for w in doc][0] # Beautiful toy birds if fw.dep_ == 'amod': return 0.0 # short circuit if sentence starts with a gerund and the gerund is the # subject. if _begins_with_one_of(sentence, ['VBG']): doc = nlp(sentence) fw = [w for w in doc][0] # Running is fun if fw.dep_.endswith('subj'): return 0.0 fc = [c for c in doc.noun_chunks] # Dancing boys can never sing if str(fw) in str(fc): return 0.0 positive_prob = models['participle'].predict([_text_to_vector(sentence, trigram2idx['participle'], trigram_count['participle'])])[0][1] return float(positive_prob)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:check; 3, parameters; 3, 4; 4, identifier:sentence; 5, block; 5, 6; 5, 8; 5, 9; 5, 10; 5, 11; 5, 12; 5, 13; 5, 14; 5, 15; 5, 16; 5, 17; 5, 18; 5, 24; 5, 31; 5, 38; 5, 45; 5, 52; 5, 59; 5, 60; 5, 96; 5, 133; 5, 179; 5, 214; 5, 250; 5, 325; 5, 326; 5, 344; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, comment; 10, comment; 11, comment; 12, comment; 13, comment; 14, comment; 15, comment; 16, comment; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:result; 21, call; 21, 22; 21, 23; 22, identifier:Feedback; 23, argument_list; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:is_missing_verb; 27, call; 27, 28; 27, 29; 28, identifier:detect_missing_verb; 29, argument_list; 29, 30; 30, identifier:sentence; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:is_infinitive; 34, call; 34, 35; 34, 36; 35, identifier:detect_infinitive_phrase; 36, argument_list; 36, 37; 37, identifier:sentence; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:is_participle; 41, call; 41, 42; 41, 43; 42, identifier:is_participle_clause_fragment; 43, argument_list; 43, 44; 44, identifier:sentence; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:lang_tool_feedback; 48, call; 48, 49; 48, 50; 49, identifier:get_language_tool_feedback; 50, argument_list; 50, 51; 51, identifier:sentence; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:subject_and_verb_agree; 55, call; 55, 56; 55, 57; 56, identifier:get_subject_verb_agreement_feedback; 57, argument_list; 57, 58; 58, identifier:sentence; 59, comment; 60, if_statement; 60, 61; 60, 62; 60, 63; 61, identifier:is_missing_verb; 62, comment; 63, block; 63, 64; 63, 72; 63, 84; 63, 90; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 71; 66, subscript; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:result; 69, identifier:matches; 70, string:'missing_verb'; 71, True; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:result; 76, identifier:human_readable; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:MISSING_VERB_ADVICE; 80, identifier:replace; 81, argument_list; 81, 82; 81, 83; 82, string:'\n'; 83, string:''; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:result; 88, identifier:primary_error; 89, string:'MISSING_VERB_ERROR'; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:result; 94, identifier:specific_error; 95, string:'MISSING_VERB'; 96, if_statement; 96, 97; 96, 100; 97, comparison_operator:>; 97, 98; 97, 99; 98, identifier:is_participle; 99, float:.5; 100, block; 100, 101; 100, 109; 100, 121; 100, 127; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 108; 103, subscript; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:result; 106, identifier:matches; 107, string:'participle_phrase'; 108, identifier:is_participle; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:result; 113, identifier:human_readable; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:PARTICIPLE_FRAGMENT_ADVICE; 117, identifier:replace; 118, argument_list; 118, 119; 118, 120; 119, string:'\n'; 120, string:''; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:result; 125, identifier:primary_error; 126, string:'FRAGMENT_ERROR'; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:result; 131, identifier:specific_error; 132, string:'PARTICIPLE_PHRASE'; 133, if_statement; 133, 134; 133, 135; 134, identifier:lang_tool_feedback; 135, block; 135, 136; 135, 144; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 143; 138, subscript; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:result; 141, identifier:matches; 142, string:'lang_tool'; 143, identifier:lang_tool_feedback; 144, for_statement; 144, 145; 144, 146; 144, 147; 145, identifier:ltf; 146, identifier:lang_tool_feedback; 147, block; 147, 148; 148, if_statement; 148, 149; 148, 156; 149, comparison_operator:==; 149, 150; 149, 155; 150, subscript; 150, 151; 150, 154; 151, subscript; 151, 152; 151, 153; 152, identifier:ltf; 153, string:'rule'; 154, string:'id'; 155, string:'SENTENCE_FRAGMENT'; 156, block; 156, 157; 156, 167; 156, 173; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:result; 161, identifier:human_readable; 162, subscript; 162, 163; 162, 166; 163, subscript; 163, 164; 163, 165; 164, identifier:lang_tool_feedback; 165, integer:0; 166, string:'message'; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:result; 171, identifier:primary_error; 172, string:'FRAGMENT_ERROR'; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:result; 177, identifier:specific_error; 178, string:'SUBORDINATE_CLAUSE'; 179, if_statement; 179, 180; 179, 181; 180, identifier:is_infinitive; 181, block; 181, 182; 181, 190; 181, 202; 181, 208; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 189; 184, subscript; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:result; 187, identifier:matches; 188, string:'infinitive_phrase'; 189, True; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:result; 194, identifier:human_readable; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:INFINITIVE_PHRASE_ADVICE; 198, identifier:replace; 199, argument_list; 199, 200; 199, 201; 200, string:'\n'; 201, string:''; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:result; 206, identifier:primary_error; 207, string:'INFINITIVE_PHRASE_ERROR'; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:result; 212, identifier:specific_error; 213, string:'INFINITIVE_PHRASE'; 214, if_statement; 214, 215; 214, 217; 215, not_operator; 215, 216; 216, identifier:subject_and_verb_agree; 217, block; 217, 218; 217, 226; 217, 238; 217, 244; 218, expression_statement; 218, 219; 219, assignment; 219, 220; 219, 225; 220, subscript; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:result; 223, identifier:matches; 224, string:'subject_verb_agreement'; 225, identifier:subject_and_verb_agree; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:result; 230, identifier:human_readable; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:SUBJECT_VERB_AGREEMENT_ADVICE; 234, identifier:replace; 235, argument_list; 235, 236; 235, 237; 236, string:'\n'; 237, string:''; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:result; 242, identifier:primary_error; 243, string:'SUBJECT_VERB_AGREEMENT_ERROR'; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:result; 248, identifier:specific_error; 249, string:'SUBJECT_VERB_AGREEMENT'; 250, if_statement; 250, 251; 250, 252; 250, 253; 251, identifier:lang_tool_feedback; 252, comment; 253, block; 253, 254; 253, 262; 253, 295; 254, expression_statement; 254, 255; 255, assignment; 255, 256; 255, 261; 256, subscript; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:result; 259, identifier:matches; 260, string:'lang_tool'; 261, identifier:lang_tool_feedback; 262, for_statement; 262, 263; 262, 264; 262, 265; 263, identifier:ltf; 264, identifier:lang_tool_feedback; 265, block; 265, 266; 266, if_statement; 266, 267; 266, 274; 267, comparison_operator:==; 267, 268; 267, 273; 268, subscript; 268, 269; 268, 272; 269, subscript; 269, 270; 269, 271; 270, identifier:ltf; 271, string:'rule'; 272, string:'id'; 273, string:'MORFOLOGIK_RULE_EN_US'; 274, block; 274, 275; 274, 283; 274, 289; 275, expression_statement; 275, 276; 276, assignment; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:result; 279, identifier:human_readable; 280, subscript; 280, 281; 280, 282; 281, identifier:ltf; 282, string:'message'; 283, expression_statement; 283, 284; 284, assignment; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:result; 287, identifier:primary_error; 288, string:'SPELLING_ERROR'; 289, expression_statement; 289, 290; 290, assignment; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:result; 293, identifier:specific_error; 294, string:'SPELLING_ERROR'; 295, if_statement; 295, 296; 295, 300; 296, not_operator; 296, 297; 297, attribute; 297, 298; 297, 299; 298, identifier:result; 299, identifier:primary_error; 300, block; 300, 301; 300, 309; 300, 315; 301, expression_statement; 301, 302; 302, assignment; 302, 303; 302, 306; 303, attribute; 303, 304; 303, 305; 304, identifier:result; 305, identifier:human_readable; 306, subscript; 306, 307; 306, 308; 307, identifier:ltf; 308, string:'message'; 309, expression_statement; 309, 310; 310, assignment; 310, 311; 310, 314; 311, attribute; 311, 312; 311, 313; 312, identifier:result; 313, identifier:primary_error; 314, string:'OTHER_ERROR'; 315, expression_statement; 315, 316; 316, assignment; 316, 317; 316, 320; 317, attribute; 317, 318; 317, 319; 318, identifier:result; 319, identifier:specific_error; 320, subscript; 320, 321; 320, 324; 321, subscript; 321, 322; 321, 323; 322, identifier:ltf; 323, string:'rule'; 324, string:'id'; 325, comment; 326, if_statement; 326, 327; 326, 331; 327, not_operator; 327, 328; 328, attribute; 328, 329; 328, 330; 329, identifier:result; 330, identifier:matches; 331, block; 331, 332; 332, expression_statement; 332, 333; 333, assignment; 333, 334; 333, 337; 334, attribute; 334, 335; 334, 336; 335, identifier:result; 336, identifier:human_readable; 337, call; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, identifier:STRONG_SENTENCE_ADVICE; 340, identifier:replace; 341, argument_list; 341, 342; 341, 343; 342, string:'\n'; 343, string:''; 344, return_statement; 344, 345; 345, identifier:result
def check(sentence): """Supply a sentence or fragment and recieve feedback""" # How we decide what to put as the human readable feedback # # Our order of prefence is, # # 1. Spelling errors. # - A spelling error can change the sentence meaning # 2. Subject-verb agreement errors # 3. Subordinate conjunction starting a sentence # 4. Participle phrase fragment # 5. Other errors result = Feedback() is_missing_verb = detect_missing_verb(sentence) is_infinitive = detect_infinitive_phrase(sentence) is_participle = is_participle_clause_fragment(sentence) lang_tool_feedback = get_language_tool_feedback(sentence) subject_and_verb_agree = get_subject_verb_agreement_feedback(sentence) #### if is_missing_verb: # Lowest priority result.matches['missing_verb'] = True result.human_readable = MISSING_VERB_ADVICE.replace('\n', '') result.primary_error = 'MISSING_VERB_ERROR' result.specific_error = 'MISSING_VERB' if is_participle > .5: result.matches['participle_phrase'] = is_participle result.human_readable = PARTICIPLE_FRAGMENT_ADVICE.replace('\n', '') result.primary_error = 'FRAGMENT_ERROR' result.specific_error = 'PARTICIPLE_PHRASE' if lang_tool_feedback: result.matches['lang_tool'] = lang_tool_feedback for ltf in lang_tool_feedback: if ltf['rule']['id'] == 'SENTENCE_FRAGMENT': result.human_readable = lang_tool_feedback[0]['message'] result.primary_error = 'FRAGMENT_ERROR' result.specific_error = 'SUBORDINATE_CLAUSE' if is_infinitive: result.matches['infinitive_phrase'] = True result.human_readable = INFINITIVE_PHRASE_ADVICE.replace('\n', '') result.primary_error = 'INFINITIVE_PHRASE_ERROR' result.specific_error = 'INFINITIVE_PHRASE' if not subject_and_verb_agree: result.matches['subject_verb_agreement'] = subject_and_verb_agree result.human_readable = SUBJECT_VERB_AGREEMENT_ADVICE.replace('\n', '') result.primary_error = 'SUBJECT_VERB_AGREEMENT_ERROR' result.specific_error = 'SUBJECT_VERB_AGREEMENT' if lang_tool_feedback: # Highest priority (spelling, other lang tool errors) result.matches['lang_tool'] = lang_tool_feedback for ltf in lang_tool_feedback: if ltf['rule']['id'] == 'MORFOLOGIK_RULE_EN_US': result.human_readable = ltf['message'] result.primary_error = 'SPELLING_ERROR' result.specific_error = 'SPELLING_ERROR' if not result.primary_error: result.human_readable = ltf['message'] result.primary_error = 'OTHER_ERROR' result.specific_error = ltf['rule']['id'] #### if not result.matches: result.human_readable = STRONG_SENTENCE_ADVICE.replace('\n', '') return result
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:execute; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 14; 5, 20; 5, 26; 5, 36; 5, 37; 5, 74; 5, 82; 5, 83; 5, 144; 5, 145; 5, 196; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:count; 13, integer:0; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:taskset; 19, list:[]; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:results; 25, dictionary; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:totaltime; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:time; 34, identifier:time; 35, argument_list; 36, comment; 37, for_statement; 37, 38; 37, 39; 37, 45; 38, identifier:task; 39, call; 39, 40; 39, 41; 40, identifier:list; 41, argument_list; 41, 42; 42, attribute; 42, 43; 42, 44; 43, identifier:self; 44, identifier:taskseq; 45, block; 45, 46; 45, 55; 45, 68; 46, expression_statement; 46, 47; 47, call; 47, 48; 47, 53; 48, attribute; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:taskset; 52, identifier:append; 53, argument_list; 53, 54; 54, identifier:task; 55, expression_statement; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:task; 59, identifier:add_callback; 60, argument_list; 60, 61; 60, 62; 60, 65; 61, string:'resolved'; 62, attribute; 62, 63; 62, 64; 63, identifier:self; 64, identifier:child_done; 65, attribute; 65, 66; 65, 67; 66, identifier:self; 67, identifier:count; 68, expression_statement; 68, 69; 69, augmented_assignment:+=; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:count; 73, integer:1; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:self; 78, identifier:numtasks; 79, attribute; 79, 80; 79, 81; 80, identifier:self; 81, identifier:count; 82, comment; 83, with_statement; 83, 84; 83, 89; 84, with_clause; 84, 85; 85, with_item; 85, 86; 86, attribute; 86, 87; 86, 88; 87, identifier:self; 88, identifier:regcond; 89, block; 89, 90; 89, 112; 89, 113; 89, 127; 89, 128; 89, 129; 90, for_statement; 90, 91; 90, 92; 90, 98; 91, identifier:task; 92, call; 92, 93; 92, 94; 93, identifier:list; 94, argument_list; 94, 95; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:taskset; 98, block; 98, 99; 98, 106; 99, expression_statement; 99, 100; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:task; 103, identifier:initialize; 104, argument_list; 104, 105; 105, identifier:self; 106, expression_statement; 106, 107; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:task; 110, identifier:start; 111, argument_list; 112, comment; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:totaltime; 118, binary_operator:-; 118, 119; 118, 124; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:time; 122, identifier:time; 123, argument_list; 124, attribute; 124, 125; 124, 126; 125, identifier:self; 126, identifier:totaltime; 127, comment; 128, comment; 129, while_statement; 129, 130; 129, 135; 130, comparison_operator:>; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:count; 134, integer:0; 135, block; 135, 136; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 143; 138, attribute; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:self; 141, identifier:regcond; 142, identifier:wait; 143, argument_list; 144, comment; 145, for_statement; 145, 146; 145, 147; 145, 154; 146, identifier:key; 147, call; 147, 148; 147, 153; 148, attribute; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:self; 151, identifier:results; 152, identifier:keys; 153, argument_list; 154, block; 154, 155; 154, 163; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:value; 158, subscript; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:self; 161, identifier:results; 162, identifier:key; 163, if_statement; 163, 164; 163, 169; 164, call; 164, 165; 164, 166; 165, identifier:isinstance; 166, argument_list; 166, 167; 166, 168; 167, identifier:value; 168, identifier:Exception; 169, block; 169, 170; 169, 176; 169, 194; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 175; 172, tuple_pattern; 172, 173; 172, 174; 173, identifier:count; 174, identifier:task; 175, identifier:key; 176, expression_statement; 176, 177; 177, call; 177, 178; 177, 183; 178, attribute; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:self; 181, identifier:logger; 182, identifier:error; 183, argument_list; 183, 184; 184, binary_operator:%; 184, 185; 184, 186; 185, string:"Child task %s terminated with exception: %s"; 186, tuple; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:task; 189, identifier:tag; 190, call; 190, 191; 190, 192; 191, identifier:str; 192, argument_list; 192, 193; 193, identifier:value; 194, raise_statement; 194, 195; 195, identifier:value; 196, return_statement; 196, 197; 197, integer:0
def execute(self): """Run all child tasks concurrently in separate threads. Return 0 after all child tasks have completed execution. """ self.count = 0 self.taskset = [] self.results = {} self.totaltime = time.time() # Register termination callbacks for all my child tasks. for task in list(self.taskseq): self.taskset.append(task) task.add_callback('resolved', self.child_done, self.count) self.count += 1 self.numtasks = self.count # Now start each child task. with self.regcond: for task in list(self.taskset): task.initialize(self) task.start() # Account for time needed to start subtasks self.totaltime = time.time() - self.totaltime # Now give up the critical section and wait for last child # task to terminate. while self.count > 0: self.regcond.wait() # Scan results for errors (exceptions) and raise the first one we find for key in self.results.keys(): value = self.results[key] if isinstance(value, Exception): (count, task) = key self.logger.error("Child task %s terminated with exception: %s" % ( task.tag, str(value))) raise value return 0
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:startall; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:wait; 7, False; 8, dictionary_splat_pattern; 8, 9; 9, identifier:kwdargs; 10, block; 10, 11; 10, 13; 10, 22; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, call; 14, 15; 14, 20; 15, attribute; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:logger; 19, identifier:debug; 20, argument_list; 20, 21; 21, string:"startall called"; 22, with_statement; 22, 23; 22, 28; 23, with_clause; 23, 24; 24, with_item; 24, 25; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:regcond; 28, block; 28, 29; 28, 85; 28, 86; 28, 96; 28, 102; 28, 108; 28, 114; 28, 127; 28, 128; 28, 137; 28, 187; 28, 188; 28, 189; 28, 239; 29, while_statement; 29, 30; 29, 35; 30, comparison_operator:!=; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:status; 34, string:'down'; 35, block; 35, 36; 35, 64; 35, 77; 36, if_statement; 36, 37; 36, 52; 36, 53; 37, boolean_operator:or; 37, 38; 37, 45; 38, comparison_operator:in; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:status; 42, tuple; 42, 43; 42, 44; 43, string:'start'; 44, string:'up'; 45, call; 45, 46; 45, 51; 46, attribute; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:ev_quit; 50, identifier:is_set; 51, argument_list; 52, comment; 53, block; 53, 54; 53, 63; 54, expression_statement; 54, 55; 55, call; 55, 56; 55, 61; 56, attribute; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:logger; 60, identifier:error; 61, argument_list; 61, 62; 62, string:"ignoring duplicate request to start thread pool"; 63, return_statement; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 71; 66, attribute; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:self; 69, identifier:logger; 70, identifier:debug; 71, argument_list; 71, 72; 72, binary_operator:%; 72, 73; 72, 74; 73, string:"waiting for threads: count=%d"; 74, attribute; 74, 75; 74, 76; 75, identifier:self; 76, identifier:runningcount; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 84; 79, attribute; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:self; 82, identifier:regcond; 83, identifier:wait; 84, argument_list; 85, comment; 86, if_statement; 86, 87; 86, 94; 87, call; 87, 88; 87, 93; 88, attribute; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:ev_quit; 92, identifier:is_set; 93, argument_list; 94, block; 94, 95; 95, return_statement; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:runningcount; 101, integer:0; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:self; 106, identifier:status; 107, string:'start'; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:self; 112, identifier:workers; 113, list:[]; 114, if_statement; 114, 115; 114, 116; 114, 121; 115, identifier:wait; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:tpool; 120, identifier:self; 121, else_clause; 121, 122; 122, block; 122, 123; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:tpool; 126, None; 127, comment; 128, expression_statement; 128, 129; 129, call; 129, 130; 129, 135; 130, attribute; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:logger; 134, identifier:debug; 135, argument_list; 135, 136; 136, string:"starting threads in thread pool"; 137, for_statement; 137, 138; 137, 139; 137, 145; 138, identifier:i; 139, call; 139, 140; 139, 141; 140, identifier:range; 141, argument_list; 141, 142; 142, attribute; 142, 143; 142, 144; 143, identifier:self; 144, identifier:numthreads; 145, block; 145, 146; 145, 172; 145, 181; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:t; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:self; 152, identifier:workerClass; 153, argument_list; 153, 154; 153, 157; 153, 162; 153, 167; 153, 170; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:queue; 157, keyword_argument; 157, 158; 157, 159; 158, identifier:logger; 159, attribute; 159, 160; 159, 161; 160, identifier:self; 161, identifier:logger; 162, keyword_argument; 162, 163; 162, 164; 163, identifier:ev_quit; 164, attribute; 164, 165; 164, 166; 165, identifier:self; 166, identifier:ev_quit; 167, keyword_argument; 167, 168; 167, 169; 168, identifier:tpool; 169, identifier:tpool; 170, dictionary_splat; 170, 171; 171, identifier:kwdargs; 172, expression_statement; 172, 173; 173, call; 173, 174; 173, 179; 174, attribute; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:self; 177, identifier:workers; 178, identifier:append; 179, argument_list; 179, 180; 180, identifier:t; 181, expression_statement; 181, 182; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:t; 185, identifier:start; 186, argument_list; 187, comment; 188, comment; 189, if_statement; 189, 190; 189, 191; 189, 192; 189, 230; 190, identifier:wait; 191, comment; 192, block; 192, 193; 193, while_statement; 193, 194; 193, 208; 194, boolean_operator:and; 194, 195; 194, 200; 195, comparison_operator:!=; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:self; 198, identifier:status; 199, string:'up'; 200, not_operator; 200, 201; 201, call; 201, 202; 201, 207; 202, attribute; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:self; 205, identifier:ev_quit; 206, identifier:is_set; 207, argument_list; 208, block; 208, 209; 208, 222; 209, expression_statement; 209, 210; 210, call; 210, 211; 210, 216; 211, attribute; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:self; 214, identifier:logger; 215, identifier:debug; 216, argument_list; 216, 217; 217, binary_operator:%; 217, 218; 217, 219; 218, string:"waiting for threads: count=%d"; 219, attribute; 219, 220; 219, 221; 220, identifier:self; 221, identifier:runningcount; 222, expression_statement; 222, 223; 223, call; 223, 224; 223, 229; 224, attribute; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:self; 227, identifier:regcond; 228, identifier:wait; 229, argument_list; 230, else_clause; 230, 231; 230, 232; 231, comment; 232, block; 232, 233; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:self; 237, identifier:status; 238, string:'up'; 239, expression_statement; 239, 240; 240, call; 240, 241; 240, 246; 241, attribute; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:self; 244, identifier:logger; 245, identifier:debug; 246, argument_list; 246, 247; 247, string:"startall done"
def startall(self, wait=False, **kwdargs): """Start all of the threads in the thread pool. If _wait_ is True then don't return until all threads are up and running. Any extra keyword arguments are passed to the worker thread constructor. """ self.logger.debug("startall called") with self.regcond: while self.status != 'down': if self.status in ('start', 'up') or self.ev_quit.is_set(): # For now, abandon additional request to start self.logger.error("ignoring duplicate request to start thread pool") return self.logger.debug("waiting for threads: count=%d" % self.runningcount) self.regcond.wait() #assert(self.status == 'down') if self.ev_quit.is_set(): return self.runningcount = 0 self.status = 'start' self.workers = [] if wait: tpool = self else: tpool = None # Start all worker threads self.logger.debug("starting threads in thread pool") for i in range(self.numthreads): t = self.workerClass(self.queue, logger=self.logger, ev_quit=self.ev_quit, tpool=tpool, **kwdargs) self.workers.append(t) t.start() # if started with wait=True, then expect that threads will register # themselves and last one up will set status to "up" if wait: # Threads are on the way up. Wait until last one starts. while self.status != 'up' and not self.ev_quit.is_set(): self.logger.debug("waiting for threads: count=%d" % self.runningcount) self.regcond.wait() else: # otherwise, we just assume the pool is up self.status = 'up' self.logger.debug("startall done")
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:handle; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 18; 5, 19; 5, 25; 5, 33; 5, 41; 5, 42; 5, 51; 5, 62; 5, 69; 5, 75; 5, 507; 5, 508; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:logger; 13, attribute; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:server; 17, identifier:logger; 18, comment; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:packet; 22, call; 22, 23; 22, 24; 23, identifier:iis; 24, argument_list; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:packet; 29, identifier:datain; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:rfile; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:packet; 37, identifier:dataout; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:wfile; 41, comment; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:size; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:struct; 48, identifier:calcsize; 49, argument_list; 49, 50; 50, string:'8h'; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:line; 54, call; 54, 55; 54, 60; 55, attribute; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:packet; 58, identifier:datain; 59, identifier:read; 60, argument_list; 60, 61; 61, identifier:size; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:n; 65, call; 65, 66; 65, 67; 66, identifier:len; 67, argument_list; 67, 68; 68, identifier:line; 69, if_statement; 69, 70; 69, 73; 70, comparison_operator:<; 70, 71; 70, 72; 71, identifier:n; 72, identifier:size; 73, block; 73, 74; 74, return_statement; 75, while_statement; 75, 76; 75, 79; 76, comparison_operator:>; 76, 77; 76, 78; 77, identifier:n; 78, integer:0; 79, block; 79, 80; 79, 117; 79, 118; 79, 119; 79, 125; 79, 131; 79, 137; 79, 145; 79, 153; 79, 161; 79, 169; 79, 176; 79, 177; 79, 189; 79, 190; 79, 196; 79, 202; 79, 208; 79, 214; 79, 220; 79, 226; 79, 232; 79, 238; 79, 239; 79, 240; 79, 253; 79, 424; 79, 482; 79, 483; 79, 494; 79, 501; 80, try_statement; 80, 81; 80, 92; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:bytes; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:struct; 88, identifier:unpack; 89, argument_list; 89, 90; 89, 91; 90, string:'8h'; 91, identifier:line; 92, except_clause; 92, 93; 92, 94; 93, identifier:Exception; 94, block; 94, 95; 94, 104; 95, expression_statement; 95, 96; 96, call; 96, 97; 96, 102; 97, attribute; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:logger; 101, identifier:error; 102, argument_list; 102, 103; 103, string:'error unpacking the data.'; 104, for_statement; 104, 105; 104, 106; 104, 111; 105, identifier:exctn; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:sys; 109, identifier:exc_info; 110, argument_list; 111, block; 111, 112; 112, expression_statement; 112, 113; 113, call; 113, 114; 113, 115; 114, identifier:print; 115, argument_list; 115, 116; 116, identifier:exctn; 117, comment; 118, comment; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:subunit; 122, subscript; 122, 123; 122, 124; 123, identifier:bytes; 124, integer:2; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:subunit077; 128, binary_operator:&; 128, 129; 128, 130; 129, identifier:subunit; 130, integer:0o77; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:tid; 134, subscript; 134, 135; 134, 136; 135, identifier:bytes; 136, integer:0; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:x; 140, binary_operator:&; 140, 141; 140, 144; 141, subscript; 141, 142; 141, 143; 142, identifier:bytes; 143, integer:4; 144, integer:0o177777; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:y; 148, binary_operator:&; 148, 149; 148, 152; 149, subscript; 149, 150; 149, 151; 150, identifier:bytes; 151, integer:5; 152, integer:0o177777; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:z; 156, binary_operator:&; 156, 157; 156, 160; 157, subscript; 157, 158; 157, 159; 158, identifier:bytes; 159, integer:6; 160, integer:0o177777; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:t; 164, binary_operator:&; 164, 165; 164, 168; 165, subscript; 165, 166; 165, 167; 166, identifier:bytes; 167, integer:7; 168, integer:0o17777; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:ndatabytes; 172, unary_operator:-; 172, 173; 173, subscript; 173, 174; 173, 175; 174, identifier:bytes; 175, integer:1; 176, comment; 177, if_statement; 177, 178; 177, 184; 178, parenthesized_expression; 178, 179; 179, not_operator; 179, 180; 180, parenthesized_expression; 180, 181; 181, binary_operator:&; 181, 182; 181, 183; 182, identifier:tid; 183, identifier:PACKED; 184, block; 184, 185; 185, expression_statement; 185, 186; 186, augmented_assignment:*=; 186, 187; 186, 188; 187, identifier:ndatabytes; 188, integer:2; 189, comment; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:packet; 194, identifier:subunit; 195, identifier:subunit; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:packet; 200, identifier:subunit077; 201, identifier:subunit077; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:packet; 206, identifier:tid; 207, identifier:tid; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:packet; 212, identifier:x; 213, identifier:x; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:packet; 218, identifier:y; 219, identifier:y; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:packet; 224, identifier:z; 225, identifier:z; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:packet; 230, identifier:t; 231, identifier:t; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:packet; 236, identifier:nbytes; 237, identifier:ndatabytes; 238, comment; 239, comment; 240, expression_statement; 240, 241; 241, call; 241, 242; 241, 247; 242, attribute; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:self; 245, identifier:logger; 246, identifier:debug; 247, argument_list; 247, 248; 248, binary_operator:%; 248, 249; 248, 250; 249, string:"PACKET IS %o"; 250, attribute; 250, 251; 250, 252; 251, identifier:packet; 252, identifier:subunit; 253, if_statement; 253, 254; 253, 259; 253, 267; 253, 301; 253, 342; 253, 375; 253, 408; 254, comparison_operator:==; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:packet; 257, identifier:subunit077; 258, identifier:FEEDBACK; 259, block; 259, 260; 260, expression_statement; 260, 261; 261, call; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:self; 264, identifier:handle_feedback; 265, argument_list; 265, 266; 266, identifier:packet; 267, elif_clause; 267, 268; 267, 273; 268, comparison_operator:==; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:packet; 271, identifier:subunit077; 272, identifier:LUT; 273, block; 273, 274; 273, 281; 273, 282; 273, 293; 273, 300; 274, expression_statement; 274, 275; 275, call; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:self; 278, identifier:handle_lut; 279, argument_list; 279, 280; 280, identifier:packet; 281, comment; 282, expression_statement; 282, 283; 283, assignment; 283, 284; 283, 285; 284, identifier:line; 285, call; 285, 286; 285, 291; 286, attribute; 286, 287; 286, 290; 287, attribute; 287, 288; 287, 289; 288, identifier:packet; 289, identifier:datain; 290, identifier:read; 291, argument_list; 291, 292; 292, identifier:size; 293, expression_statement; 293, 294; 294, assignment; 294, 295; 294, 296; 295, identifier:n; 296, call; 296, 297; 296, 298; 297, identifier:len; 298, argument_list; 298, 299; 299, identifier:line; 300, continue_statement; 301, elif_clause; 301, 302; 301, 307; 302, comparison_operator:==; 302, 303; 302, 306; 303, attribute; 303, 304; 303, 305; 304, identifier:packet; 305, identifier:subunit077; 306, identifier:MEMORY; 307, block; 307, 308; 307, 315; 307, 322; 307, 323; 307, 334; 307, 341; 308, expression_statement; 308, 309; 309, call; 309, 310; 309, 313; 310, attribute; 310, 311; 310, 312; 311, identifier:self; 312, identifier:handle_memory; 313, argument_list; 313, 314; 314, identifier:packet; 315, if_statement; 315, 316; 315, 319; 315, 320; 316, attribute; 316, 317; 316, 318; 317, identifier:self; 318, identifier:needs_update; 319, comment; 320, block; 320, 321; 321, pass_statement; 322, comment; 323, expression_statement; 323, 324; 324, assignment; 324, 325; 324, 326; 325, identifier:line; 326, call; 326, 327; 326, 332; 327, attribute; 327, 328; 327, 331; 328, attribute; 328, 329; 328, 330; 329, identifier:packet; 330, identifier:datain; 331, identifier:read; 332, argument_list; 332, 333; 333, identifier:size; 334, expression_statement; 334, 335; 335, assignment; 335, 336; 335, 337; 336, identifier:n; 337, call; 337, 338; 337, 339; 338, identifier:len; 339, argument_list; 339, 340; 340, identifier:line; 341, continue_statement; 342, elif_clause; 342, 343; 342, 348; 343, comparison_operator:==; 343, 344; 343, 347; 344, attribute; 344, 345; 344, 346; 345, identifier:packet; 346, identifier:subunit077; 347, identifier:WCS; 348, block; 348, 349; 348, 356; 348, 367; 348, 374; 349, expression_statement; 349, 350; 350, call; 350, 351; 350, 354; 351, attribute; 351, 352; 351, 353; 352, identifier:self; 353, identifier:handle_wcs; 354, argument_list; 354, 355; 355, identifier:packet; 356, expression_statement; 356, 357; 357, assignment; 357, 358; 357, 359; 358, identifier:line; 359, call; 359, 360; 359, 365; 360, attribute; 360, 361; 360, 364; 361, attribute; 361, 362; 361, 363; 362, identifier:packet; 363, identifier:datain; 364, identifier:read; 365, argument_list; 365, 366; 366, identifier:size; 367, expression_statement; 367, 368; 368, assignment; 368, 369; 368, 370; 369, identifier:n; 370, call; 370, 371; 370, 372; 371, identifier:len; 372, argument_list; 372, 373; 373, identifier:line; 374, continue_statement; 375, elif_clause; 375, 376; 375, 381; 376, comparison_operator:==; 376, 377; 376, 380; 377, attribute; 377, 378; 377, 379; 378, identifier:packet; 379, identifier:subunit077; 380, identifier:IMCURSOR; 381, block; 381, 382; 381, 389; 381, 400; 381, 407; 382, expression_statement; 382, 383; 383, call; 383, 384; 383, 387; 384, attribute; 384, 385; 384, 386; 385, identifier:self; 386, identifier:handle_imcursor; 387, argument_list; 387, 388; 388, identifier:packet; 389, expression_statement; 389, 390; 390, assignment; 390, 391; 390, 392; 391, identifier:line; 392, call; 392, 393; 392, 398; 393, attribute; 393, 394; 393, 397; 394, attribute; 394, 395; 394, 396; 395, identifier:packet; 396, identifier:datain; 397, identifier:read; 398, argument_list; 398, 399; 399, identifier:size; 400, expression_statement; 400, 401; 401, assignment; 401, 402; 401, 403; 402, identifier:n; 403, call; 403, 404; 403, 405; 404, identifier:len; 405, argument_list; 405, 406; 406, identifier:line; 407, continue_statement; 408, else_clause; 408, 409; 409, block; 409, 410; 410, expression_statement; 410, 411; 411, call; 411, 412; 411, 417; 412, attribute; 412, 413; 412, 416; 413, attribute; 413, 414; 413, 415; 414, identifier:self; 415, identifier:logger; 416, identifier:debug; 417, argument_list; 417, 418; 418, binary_operator:%; 418, 419; 418, 420; 419, string:'?NO OP (0%o)'; 420, parenthesized_expression; 420, 421; 421, attribute; 421, 422; 421, 423; 422, identifier:packet; 423, identifier:subunit077; 424, if_statement; 424, 425; 424, 432; 424, 433; 425, not_operator; 425, 426; 426, parenthesized_expression; 426, 427; 427, binary_operator:&; 427, 428; 427, 431; 428, attribute; 428, 429; 428, 430; 429, identifier:packet; 430, identifier:tid; 431, identifier:IIS_READ; 432, comment; 433, block; 433, 434; 433, 440; 434, expression_statement; 434, 435; 435, assignment; 435, 436; 435, 437; 436, identifier:nbytes; 437, attribute; 437, 438; 437, 439; 438, identifier:packet; 439, identifier:nbytes; 440, while_statement; 440, 441; 440, 444; 440, 445; 441, comparison_operator:>; 441, 442; 441, 443; 442, identifier:nbytes; 443, integer:0; 444, comment; 445, block; 445, 446; 445, 461; 445, 472; 445, 478; 446, if_statement; 446, 447; 446, 450; 446, 455; 447, comparison_operator:<; 447, 448; 447, 449; 448, identifier:nbytes; 449, identifier:SZ_FIFOBUF; 450, block; 450, 451; 451, expression_statement; 451, 452; 452, assignment; 452, 453; 452, 454; 453, identifier:n; 454, identifier:nbytes; 455, else_clause; 455, 456; 456, block; 456, 457; 457, expression_statement; 457, 458; 458, assignment; 458, 459; 458, 460; 459, identifier:n; 460, identifier:SZ_FIFOBUF; 461, expression_statement; 461, 462; 462, assignment; 462, 463; 462, 464; 463, identifier:m; 464, call; 464, 465; 464, 470; 465, attribute; 465, 466; 465, 469; 466, attribute; 466, 467; 466, 468; 467, identifier:self; 468, identifier:rfile; 469, identifier:read; 470, argument_list; 470, 471; 471, identifier:n; 472, if_statement; 472, 473; 472, 476; 473, comparison_operator:<=; 473, 474; 473, 475; 474, identifier:m; 475, integer:0; 476, block; 476, 477; 477, break_statement; 478, expression_statement; 478, 479; 479, augmented_assignment:-=; 479, 480; 479, 481; 480, identifier:nbytes; 481, identifier:n; 482, comment; 483, expression_statement; 483, 484; 484, assignment; 484, 485; 484, 486; 485, identifier:line; 486, call; 486, 487; 486, 492; 487, attribute; 487, 488; 487, 491; 488, attribute; 488, 489; 488, 490; 489, identifier:packet; 490, identifier:datain; 491, identifier:read; 492, argument_list; 492, 493; 493, identifier:size; 494, expression_statement; 494, 495; 495, assignment; 495, 496; 495, 497; 496, identifier:n; 497, call; 497, 498; 497, 499; 498, identifier:len; 499, argument_list; 499, 500; 500, identifier:line; 501, if_statement; 501, 502; 501, 505; 502, comparison_operator:<; 502, 503; 502, 504; 503, identifier:n; 504, identifier:size; 505, block; 505, 506; 506, return_statement; 507, comment; 508, if_statement; 508, 509; 508, 512; 509, attribute; 509, 510; 509, 511; 510, identifier:self; 511, identifier:needs_update; 512, block; 512, 513; 512, 519; 513, expression_statement; 513, 514; 514, call; 514, 515; 514, 518; 515, attribute; 515, 516; 515, 517; 516, identifier:self; 517, identifier:display_image; 518, argument_list; 519, expression_statement; 519, 520; 520, assignment; 520, 521; 520, 524; 521, attribute; 521, 522; 521, 523; 522, identifier:self; 523, identifier:needs_update; 524, False
def handle(self): """ This is where the action starts. """ self.logger = self.server.logger # create a packet structure packet = iis() packet.datain = self.rfile packet.dataout = self.wfile # decode the header size = struct.calcsize('8h') line = packet.datain.read(size) n = len(line) if n < size: return while n > 0: try: bytes = struct.unpack('8h', line) except Exception: self.logger.error('error unpacking the data.') for exctn in sys.exc_info(): print(exctn) # TODO: verify checksum # decode the packet fields subunit = bytes[2] subunit077 = subunit & 0o77 tid = bytes[0] x = bytes[4] & 0o177777 y = bytes[5] & 0o177777 z = bytes[6] & 0o177777 t = bytes[7] & 0o17777 ndatabytes = - bytes[1] # are the bytes packed? if (not(tid & PACKED)): ndatabytes *= 2 # populate the packet structure packet.subunit = subunit packet.subunit077 = subunit077 packet.tid = tid packet.x = x packet.y = y packet.z = z packet.t = t packet.nbytes = ndatabytes # decide what to do, depending on the # value of subunit self.logger.debug("PACKET IS %o" % packet.subunit) if packet.subunit077 == FEEDBACK: self.handle_feedback(packet) elif packet.subunit077 == LUT: self.handle_lut(packet) # read the next packet line = packet.datain.read(size) n = len(line) continue elif packet.subunit077 == MEMORY: self.handle_memory(packet) if self.needs_update: #self.display_image() pass # read the next packet line = packet.datain.read(size) n = len(line) continue elif packet.subunit077 == WCS: self.handle_wcs(packet) line = packet.datain.read(size) n = len(line) continue elif packet.subunit077 == IMCURSOR: self.handle_imcursor(packet) line = packet.datain.read(size) n = len(line) continue else: self.logger.debug('?NO OP (0%o)' % (packet.subunit077)) if not (packet.tid & IIS_READ): # OK, discard the rest of the data nbytes = packet.nbytes while nbytes > 0: # for (nbytes = ndatabytes; nbytes > 0; nbytes -= n): if nbytes < SZ_FIFOBUF: n = nbytes else: n = SZ_FIFOBUF m = self.rfile.read(n) if m <= 0: break nbytes -= n # read the next packet line = packet.datain.read(size) n = len(line) if n < size: return # <--- end of the while (n) loop if self.needs_update: self.display_image() self.needs_update = False
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 21; 2, function_name:load_file; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 4, identifier:self; 5, identifier:filepath; 6, default_parameter; 6, 7; 6, 8; 7, identifier:chname; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:wait; 11, True; 12, default_parameter; 12, 13; 12, 14; 13, identifier:create_channel; 14, True; 15, default_parameter; 15, 16; 15, 17; 16, identifier:display_image; 17, True; 18, default_parameter; 18, 19; 18, 20; 19, identifier:image_loader; 20, None; 21, block; 21, 22; 21, 24; 21, 68; 21, 74; 21, 85; 21, 99; 21, 111; 21, 112; 21, 136; 21, 142; 21, 146; 21, 150; 21, 165; 21, 204; 21, 212; 21, 222; 21, 223; 21, 224; 21, 236; 21, 256; 21, 257; 21, 267; 21, 293; 21, 345; 21, 346; 22, expression_statement; 22, 23; 23, comment; 24, if_statement; 24, 25; 24, 27; 24, 36; 25, not_operator; 25, 26; 26, identifier:chname; 27, block; 27, 28; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:channel; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:get_current_channel; 35, argument_list; 36, else_clause; 36, 37; 37, block; 37, 38; 37, 59; 38, if_statement; 38, 39; 38, 48; 39, boolean_operator:and; 39, 40; 39, 47; 40, not_operator; 40, 41; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:self; 44, identifier:has_channel; 45, argument_list; 45, 46; 46, identifier:chname; 47, identifier:create_channel; 48, block; 48, 49; 49, expression_statement; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:gui_call; 54, argument_list; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:add_channel; 58, identifier:chname; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:channel; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:get_channel; 66, argument_list; 66, 67; 67, identifier:chname; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:chname; 71, attribute; 71, 72; 71, 73; 72, identifier:channel; 73, identifier:name; 74, if_statement; 74, 75; 74, 78; 75, comparison_operator:is; 75, 76; 75, 77; 76, identifier:image_loader; 77, None; 78, block; 78, 79; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:image_loader; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:load_image; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:cache_dir; 88, call; 88, 89; 88, 94; 89, attribute; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:self; 92, identifier:settings; 93, identifier:get; 94, argument_list; 94, 95; 94, 96; 95, string:'download_folder'; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:tmpdir; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:info; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:iohelper; 105, identifier:get_fileinfo; 106, argument_list; 106, 107; 106, 108; 107, identifier:filepath; 108, keyword_argument; 108, 109; 108, 110; 109, identifier:cache_dir; 110, identifier:cache_dir; 111, comment; 112, if_statement; 112, 113; 112, 117; 113, not_operator; 113, 114; 114, attribute; 114, 115; 114, 116; 115, identifier:info; 116, identifier:ondisk; 117, block; 117, 118; 117, 125; 117, 135; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:errmsg; 121, binary_operator:%; 121, 122; 121, 123; 122, string:"File must be locally loadable: %s"; 123, parenthesized_expression; 123, 124; 124, identifier:filepath; 125, expression_statement; 125, 126; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:self; 129, identifier:gui_do; 130, argument_list; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:show_error; 134, identifier:errmsg; 135, return_statement; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:filepath; 139, attribute; 139, 140; 139, 141; 140, identifier:info; 141, identifier:filepath; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:kwargs; 145, dictionary; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:idx; 149, None; 150, if_statement; 150, 151; 150, 156; 151, comparison_operator:is; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:info; 154, identifier:numhdu; 155, None; 156, block; 156, 157; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 162; 159, subscript; 159, 160; 159, 161; 160, identifier:kwargs; 161, string:'idx'; 162, attribute; 162, 163; 162, 164; 163, identifier:info; 164, identifier:numhdu; 165, try_statement; 165, 166; 165, 176; 166, block; 166, 167; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:image; 170, call; 170, 171; 170, 172; 171, identifier:image_loader; 172, argument_list; 172, 173; 172, 174; 173, identifier:filepath; 174, dictionary_splat; 174, 175; 175, identifier:kwargs; 176, except_clause; 176, 177; 176, 181; 177, as_pattern; 177, 178; 177, 179; 178, identifier:Exception; 179, as_pattern_target; 179, 180; 180, identifier:e; 181, block; 181, 182; 181, 193; 181, 203; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:errmsg; 185, binary_operator:%; 185, 186; 185, 187; 186, string:"Failed to load '%s': %s"; 187, tuple; 187, 188; 187, 189; 188, identifier:filepath; 189, call; 189, 190; 189, 191; 190, identifier:str; 191, argument_list; 191, 192; 192, identifier:e; 193, expression_statement; 193, 194; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:self; 197, identifier:gui_do; 198, argument_list; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:self; 201, identifier:show_error; 202, identifier:errmsg; 203, return_statement; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:future; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:Future; 210, identifier:Future; 211, argument_list; 212, expression_statement; 212, 213; 213, call; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:future; 216, identifier:freeze; 217, argument_list; 217, 218; 217, 219; 217, 220; 218, identifier:image_loader; 219, identifier:filepath; 220, dictionary_splat; 220, 221; 221, identifier:kwargs; 222, comment; 223, comment; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:image; 228, identifier:set; 229, argument_list; 229, 230; 229, 233; 230, keyword_argument; 230, 231; 230, 232; 231, identifier:loader; 232, identifier:image_loader; 233, keyword_argument; 233, 234; 233, 235; 234, identifier:image_future; 235, identifier:future; 236, if_statement; 236, 237; 236, 246; 237, comparison_operator:is; 237, 238; 237, 245; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:image; 241, identifier:get; 242, argument_list; 242, 243; 242, 244; 243, string:'path'; 244, None; 245, None; 246, block; 246, 247; 247, expression_statement; 247, 248; 248, call; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:image; 251, identifier:set; 252, argument_list; 252, 253; 253, keyword_argument; 253, 254; 253, 255; 254, identifier:path; 255, identifier:filepath; 256, comment; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 260; 259, identifier:name; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:image; 263, identifier:get; 264, argument_list; 264, 265; 264, 266; 265, string:'name'; 266, None; 267, if_statement; 267, 268; 267, 271; 268, comparison_operator:is; 268, 269; 268, 270; 269, identifier:name; 270, None; 271, block; 271, 272; 271, 284; 272, expression_statement; 272, 273; 273, assignment; 273, 274; 273, 275; 274, identifier:name; 275, call; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:iohelper; 278, identifier:name_image_from_path; 279, argument_list; 279, 280; 279, 281; 280, identifier:filepath; 281, keyword_argument; 281, 282; 281, 283; 282, identifier:idx; 283, identifier:idx; 284, expression_statement; 284, 285; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:image; 288, identifier:set; 289, argument_list; 289, 290; 290, keyword_argument; 290, 291; 290, 292; 291, identifier:name; 292, identifier:name; 293, if_statement; 293, 294; 293, 295; 293, 296; 293, 297; 293, 331; 294, identifier:display_image; 295, comment; 296, comment; 297, block; 297, 298; 298, if_statement; 298, 299; 298, 300; 298, 315; 299, identifier:wait; 300, block; 300, 301; 301, expression_statement; 301, 302; 302, call; 302, 303; 302, 306; 303, attribute; 303, 304; 303, 305; 304, identifier:self; 305, identifier:gui_call; 306, argument_list; 306, 307; 306, 310; 306, 311; 306, 312; 307, attribute; 307, 308; 307, 309; 308, identifier:self; 309, identifier:add_image; 310, identifier:name; 311, identifier:image; 312, keyword_argument; 312, 313; 312, 314; 313, identifier:chname; 314, identifier:chname; 315, else_clause; 315, 316; 316, block; 316, 317; 317, expression_statement; 317, 318; 318, call; 318, 319; 318, 322; 319, attribute; 319, 320; 319, 321; 320, identifier:self; 321, identifier:gui_do; 322, argument_list; 322, 323; 322, 326; 322, 327; 322, 328; 323, attribute; 323, 324; 323, 325; 324, identifier:self; 325, identifier:add_image; 326, identifier:name; 327, identifier:image; 328, keyword_argument; 328, 329; 328, 330; 329, identifier:chname; 330, identifier:chname; 331, else_clause; 331, 332; 332, block; 332, 333; 333, expression_statement; 333, 334; 334, call; 334, 335; 334, 338; 335, attribute; 335, 336; 335, 337; 336, identifier:self; 337, identifier:gui_do; 338, argument_list; 338, 339; 338, 342; 338, 343; 338, 344; 339, attribute; 339, 340; 339, 341; 340, identifier:self; 341, identifier:bulk_add_image; 342, identifier:name; 343, identifier:image; 344, identifier:chname; 345, comment; 346, return_statement; 346, 347; 347, identifier:image
def load_file(self, filepath, chname=None, wait=True, create_channel=True, display_image=True, image_loader=None): """Load a file and display it. Parameters ---------- filepath : str The path of the file to load (must reference a local file). chname : str, optional The name of the channel in which to display the image. wait : bool, optional If `True`, then wait for the file to be displayed before returning (synchronous behavior). create_channel : bool, optional Create channel. display_image : bool, optional If not `False`, then will load the image. image_loader : func, optional A special image loader, if provided. Returns ------- image The image object that was loaded. """ if not chname: channel = self.get_current_channel() else: if not self.has_channel(chname) and create_channel: self.gui_call(self.add_channel, chname) channel = self.get_channel(chname) chname = channel.name if image_loader is None: image_loader = self.load_image cache_dir = self.settings.get('download_folder', self.tmpdir) info = iohelper.get_fileinfo(filepath, cache_dir=cache_dir) # check that file is locally accessible if not info.ondisk: errmsg = "File must be locally loadable: %s" % (filepath) self.gui_do(self.show_error, errmsg) return filepath = info.filepath kwargs = {} idx = None if info.numhdu is not None: kwargs['idx'] = info.numhdu try: image = image_loader(filepath, **kwargs) except Exception as e: errmsg = "Failed to load '%s': %s" % (filepath, str(e)) self.gui_do(self.show_error, errmsg) return future = Future.Future() future.freeze(image_loader, filepath, **kwargs) # Save a future for this image to reload it later if we # have to remove it from memory image.set(loader=image_loader, image_future=future) if image.get('path', None) is None: image.set(path=filepath) # Assign a name to the image if the loader did not. name = image.get('name', None) if name is None: name = iohelper.name_image_from_path(filepath, idx=idx) image.set(name=name) if display_image: # Display image. If the wait parameter is False then don't wait # for the image to load into the viewer if wait: self.gui_call(self.add_image, name, image, chname=chname) else: self.gui_do(self.add_image, name, image, chname=chname) else: self.gui_do(self.bulk_add_image, name, image, chname) # Return the image return image
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:open_uris; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:uris; 6, default_parameter; 6, 7; 6, 8; 7, identifier:chname; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:bulk_add; 11, False; 12, block; 12, 13; 12, 15; 12, 24; 12, 50; 12, 59; 12, 77; 12, 93; 12, 111; 12, 127; 12, 128; 12, 153; 12, 159; 13, expression_statement; 13, 14; 14, comment; 15, if_statement; 15, 16; 15, 22; 16, comparison_operator:==; 16, 17; 16, 21; 17, call; 17, 18; 17, 19; 18, identifier:len; 19, argument_list; 19, 20; 20, identifier:uris; 21, integer:0; 22, block; 22, 23; 23, return_statement; 24, if_statement; 24, 25; 24, 28; 25, comparison_operator:is; 25, 26; 25, 27; 26, identifier:chname; 27, None; 28, block; 28, 29; 28, 37; 28, 44; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:channel; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:self; 35, identifier:get_channel_info; 36, argument_list; 37, if_statement; 37, 38; 37, 41; 37, 42; 38, comparison_operator:is; 38, 39; 38, 40; 39, identifier:channel; 40, None; 41, comment; 42, block; 42, 43; 43, return_statement; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:chname; 47, attribute; 47, 48; 47, 49; 48, identifier:channel; 49, identifier:name; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:channel; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:get_channel_on_demand; 57, argument_list; 57, 58; 58, identifier:chname; 59, function_definition; 59, 60; 59, 61; 59, 63; 60, function_name:show_dataobj_bulk; 61, parameters; 61, 62; 62, identifier:data_obj; 63, block; 63, 64; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:gui_do; 69, argument_list; 69, 70; 69, 73; 69, 74; 70, attribute; 70, 71; 70, 72; 71, identifier:channel; 72, identifier:add_image; 73, identifier:data_obj; 74, keyword_argument; 74, 75; 74, 76; 75, identifier:bulk_add; 76, True; 77, function_definition; 77, 78; 77, 79; 77, 81; 78, function_name:load_file_bulk; 79, parameters; 79, 80; 80, identifier:filepath; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:self; 86, identifier:nongui_do; 87, argument_list; 87, 88; 87, 91; 87, 92; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:open_file_cont; 91, identifier:filepath; 92, identifier:show_dataobj_bulk; 93, function_definition; 93, 94; 93, 95; 93, 97; 94, function_name:show_dataobj; 95, parameters; 95, 96; 96, identifier:data_obj; 97, block; 97, 98; 98, expression_statement; 98, 99; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:self; 102, identifier:gui_do; 103, argument_list; 103, 104; 103, 107; 103, 108; 104, attribute; 104, 105; 104, 106; 105, identifier:channel; 106, identifier:add_image; 107, identifier:data_obj; 108, keyword_argument; 108, 109; 108, 110; 109, identifier:bulk_add; 110, False; 111, function_definition; 111, 112; 111, 113; 111, 115; 112, function_name:load_file; 113, parameters; 113, 114; 114, identifier:filepath; 115, block; 115, 116; 116, expression_statement; 116, 117; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:self; 120, identifier:nongui_do; 121, argument_list; 121, 122; 121, 125; 121, 126; 122, attribute; 122, 123; 122, 124; 123, identifier:self; 124, identifier:open_file_cont; 125, identifier:filepath; 126, identifier:show_dataobj; 127, comment; 128, if_statement; 128, 129; 128, 130; 128, 141; 129, identifier:bulk_add; 130, block; 130, 131; 131, expression_statement; 131, 132; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:self; 135, identifier:open_uri_cont; 136, argument_list; 136, 137; 136, 140; 137, subscript; 137, 138; 137, 139; 138, identifier:uris; 139, integer:0; 140, identifier:load_file_bulk; 141, else_clause; 141, 142; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:self; 147, identifier:open_uri_cont; 148, argument_list; 148, 149; 148, 152; 149, subscript; 149, 150; 149, 151; 150, identifier:uris; 151, integer:0; 152, identifier:load_file; 153, expression_statement; 153, 154; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:self; 157, identifier:update_pending; 158, argument_list; 159, for_statement; 159, 160; 159, 161; 159, 166; 159, 167; 160, identifier:uri; 161, subscript; 161, 162; 161, 163; 162, identifier:uris; 163, slice; 163, 164; 163, 165; 164, integer:1; 165, colon; 166, comment; 167, block; 167, 168; 167, 176; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:self; 172, identifier:open_uri_cont; 173, argument_list; 173, 174; 173, 175; 174, identifier:uri; 175, identifier:load_file_bulk; 176, expression_statement; 176, 177; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:self; 180, identifier:update_pending; 181, argument_list
def open_uris(self, uris, chname=None, bulk_add=False): """Open a set of URIs. Parameters ---------- uris : list of str The URIs of the files to load chname: str, optional (defaults to channel with focus) The name of the channel in which to load the items bulk_add : bool, optional (defaults to False) If True, then all the data items are loaded into the channel without disturbing the current item there. If False, then the first item loaded will be displayed and the rest of the items will be loaded as bulk. """ if len(uris) == 0: return if chname is None: channel = self.get_channel_info() if channel is None: # No active channel to load these into return chname = channel.name channel = self.get_channel_on_demand(chname) def show_dataobj_bulk(data_obj): self.gui_do(channel.add_image, data_obj, bulk_add=True) def load_file_bulk(filepath): self.nongui_do(self.open_file_cont, filepath, show_dataobj_bulk) def show_dataobj(data_obj): self.gui_do(channel.add_image, data_obj, bulk_add=False) def load_file(filepath): self.nongui_do(self.open_file_cont, filepath, show_dataobj) # determine whether first file is loaded as a bulk load if bulk_add: self.open_uri_cont(uris[0], load_file_bulk) else: self.open_uri_cont(uris[0], load_file) self.update_pending() for uri in uris[1:]: # rest of files are all loaded using bulk load self.open_uri_cont(uri, load_file_bulk) self.update_pending()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 24; 2, function_name:add_channel; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 4, identifier:self; 5, identifier:chname; 6, default_parameter; 6, 7; 6, 8; 7, identifier:workspace; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:num_images; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:settings; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:settings_template; 17, None; 18, default_parameter; 18, 19; 18, 20; 19, identifier:settings_share; 20, None; 21, default_parameter; 21, 22; 21, 23; 22, identifier:share_keylist; 23, None; 24, block; 24, 25; 24, 27; 25, expression_statement; 25, 26; 26, comment; 27, with_statement; 27, 28; 27, 33; 28, with_clause; 28, 29; 29, with_item; 29, 30; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:lock; 33, block; 33, 34; 33, 49; 33, 69; 33, 73; 33, 185; 33, 207; 33, 208; 33, 231; 33, 258; 33, 270; 33, 284; 33, 297; 33, 298; 33, 310; 33, 328; 33, 336; 33, 344; 33, 352; 33, 361; 33, 369; 33, 370; 33, 378; 33, 384; 33, 392; 33, 400; 33, 401; 33, 410; 33, 418; 33, 434; 33, 435; 33, 480; 33, 488; 34, if_statement; 34, 35; 34, 41; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:has_channel; 39, argument_list; 39, 40; 40, identifier:chname; 41, block; 41, 42; 42, return_statement; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:self; 46, identifier:get_channel; 47, argument_list; 47, 48; 48, identifier:chname; 49, if_statement; 49, 50; 49, 60; 50, comparison_operator:in; 50, 51; 50, 52; 51, identifier:chname; 52, call; 52, 53; 52, 58; 53, attribute; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:ds; 57, identifier:get_tabnames; 58, argument_list; 58, 59; 59, None; 60, block; 60, 61; 61, raise_statement; 61, 62; 62, call; 62, 63; 62, 64; 63, identifier:ValueError; 64, argument_list; 64, 65; 65, binary_operator:%; 65, 66; 65, 67; 66, string:"Tab name already in use: '%s'"; 67, parenthesized_expression; 67, 68; 68, identifier:chname; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:name; 72, identifier:chname; 73, if_statement; 73, 74; 73, 77; 74, comparison_operator:is; 74, 75; 74, 76; 75, identifier:settings; 76, None; 77, block; 77, 78; 77, 91; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:settings; 81, call; 81, 82; 81, 87; 82, attribute; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:self; 85, identifier:prefs; 86, identifier:create_category; 87, argument_list; 87, 88; 88, binary_operator:+; 88, 89; 88, 90; 89, string:'channel_'; 90, identifier:name; 91, try_statement; 91, 92; 91, 102; 92, block; 92, 93; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:settings; 97, identifier:load; 98, argument_list; 98, 99; 99, keyword_argument; 99, 100; 99, 101; 100, identifier:onError; 101, string:'raise'; 102, except_clause; 102, 103; 102, 107; 103, as_pattern; 103, 104; 103, 105; 104, identifier:Exception; 105, as_pattern_target; 105, 106; 106, identifier:e; 107, block; 107, 108; 107, 126; 107, 127; 108, expression_statement; 108, 109; 109, call; 109, 110; 109, 115; 110, attribute; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:self; 113, identifier:logger; 114, identifier:warning; 115, argument_list; 115, 116; 116, binary_operator:%; 116, 117; 116, 120; 117, concatenated_string; 117, 118; 117, 119; 118, string:"no saved preferences found for channel "; 119, string:"'%s': %s"; 120, tuple; 120, 121; 120, 122; 121, identifier:name; 122, call; 122, 123; 122, 124; 123, identifier:str; 124, argument_list; 124, 125; 125, identifier:e; 126, comment; 127, if_statement; 127, 128; 127, 131; 127, 143; 128, comparison_operator:is; 128, 129; 128, 130; 129, identifier:settings_template; 130, None; 131, block; 131, 132; 131, 136; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:osettings; 135, identifier:settings_template; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:osettings; 140, identifier:copy_settings; 141, argument_list; 141, 142; 142, identifier:settings; 143, else_clause; 143, 144; 144, block; 144, 145; 145, try_statement; 145, 146; 145, 147; 145, 148; 145, 181; 146, comment; 147, comment; 148, block; 148, 149; 148, 160; 148, 174; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:osettings; 152, call; 152, 153; 152, 158; 153, attribute; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:prefs; 157, identifier:get_settings; 158, argument_list; 158, 159; 159, string:'channel_Image'; 160, expression_statement; 160, 161; 161, call; 161, 162; 161, 167; 162, attribute; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:self; 165, identifier:logger; 166, identifier:debug; 167, argument_list; 167, 168; 168, binary_operator:%; 168, 169; 168, 172; 169, concatenated_string; 169, 170; 169, 171; 170, string:"Copying settings from 'Image' to "; 171, string:"'%s'"; 172, parenthesized_expression; 172, 173; 173, identifier:name; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:osettings; 178, identifier:copy_settings; 179, argument_list; 179, 180; 180, identifier:settings; 181, except_clause; 181, 182; 181, 183; 182, identifier:KeyError; 183, block; 183, 184; 184, pass_statement; 185, if_statement; 185, 186; 185, 195; 185, 196; 186, boolean_operator:and; 186, 187; 186, 191; 187, parenthesized_expression; 187, 188; 188, comparison_operator:is; 188, 189; 188, 190; 189, identifier:share_keylist; 190, None; 191, parenthesized_expression; 191, 192; 192, comparison_operator:is; 192, 193; 192, 194; 193, identifier:settings_share; 194, None; 195, comment; 196, block; 196, 197; 197, expression_statement; 197, 198; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:settings_share; 201, identifier:share_settings; 202, argument_list; 202, 203; 202, 204; 203, identifier:settings; 204, keyword_argument; 204, 205; 204, 206; 205, identifier:keylist; 206, identifier:share_keylist; 207, comment; 208, if_statement; 208, 209; 208, 212; 209, comparison_operator:is; 209, 210; 209, 211; 210, identifier:num_images; 211, None; 212, block; 212, 213; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 216; 215, identifier:num_images; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:settings; 219, identifier:get; 220, argument_list; 220, 221; 220, 222; 221, string:'numImages'; 222, call; 222, 223; 222, 228; 223, attribute; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:self; 226, identifier:settings; 227, identifier:get; 228, argument_list; 228, 229; 228, 230; 229, string:'numImages'; 230, integer:1; 231, expression_statement; 231, 232; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:settings; 235, identifier:set_defaults; 236, argument_list; 236, 237; 236, 240; 236, 243; 236, 246; 236, 249; 236, 252; 236, 255; 237, keyword_argument; 237, 238; 237, 239; 238, identifier:switchnew; 239, True; 240, keyword_argument; 240, 241; 240, 242; 241, identifier:numImages; 242, identifier:num_images; 243, keyword_argument; 243, 244; 243, 245; 244, identifier:raisenew; 245, True; 246, keyword_argument; 246, 247; 246, 248; 247, identifier:genthumb; 248, True; 249, keyword_argument; 249, 250; 249, 251; 250, identifier:focus_indicator; 251, False; 252, keyword_argument; 252, 253; 252, 254; 253, identifier:preload_images; 254, False; 255, keyword_argument; 255, 256; 255, 257; 256, identifier:sort_order; 257, string:'loadtime'; 258, expression_statement; 258, 259; 259, call; 259, 260; 259, 265; 260, attribute; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:self; 263, identifier:logger; 264, identifier:debug; 265, argument_list; 265, 266; 266, binary_operator:%; 266, 267; 266, 268; 267, string:"Adding channel '%s'"; 268, parenthesized_expression; 268, 269; 269, identifier:chname; 270, expression_statement; 270, 271; 271, assignment; 271, 272; 271, 273; 272, identifier:channel; 273, call; 273, 274; 273, 275; 274, identifier:Channel; 275, argument_list; 275, 276; 275, 277; 275, 278; 275, 281; 276, identifier:chname; 277, identifier:self; 278, keyword_argument; 278, 279; 278, 280; 279, identifier:datasrc; 280, None; 281, keyword_argument; 281, 282; 281, 283; 282, identifier:settings; 283, identifier:settings; 284, expression_statement; 284, 285; 285, assignment; 285, 286; 285, 287; 286, identifier:bnch; 287, call; 287, 288; 287, 291; 288, attribute; 288, 289; 288, 290; 289, identifier:self; 290, identifier:add_viewer; 291, argument_list; 291, 292; 291, 293; 291, 294; 292, identifier:chname; 293, identifier:settings; 294, keyword_argument; 294, 295; 294, 296; 295, identifier:workspace; 296, identifier:workspace; 297, comment; 298, expression_statement; 298, 299; 299, call; 299, 300; 299, 305; 300, attribute; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:bnch; 303, identifier:image_viewer; 304, identifier:set_name; 305, argument_list; 305, 306; 306, binary_operator:%; 306, 307; 306, 308; 307, string:'channel:%s'; 308, parenthesized_expression; 308, 309; 309, identifier:chname; 310, expression_statement; 310, 311; 311, assignment; 311, 312; 311, 313; 312, identifier:opmon; 313, call; 313, 314; 313, 317; 314, attribute; 314, 315; 314, 316; 315, identifier:self; 316, identifier:get_plugin_manager; 317, argument_list; 317, 318; 317, 321; 317, 322; 317, 325; 318, attribute; 318, 319; 318, 320; 319, identifier:self; 320, identifier:logger; 321, identifier:self; 322, attribute; 322, 323; 322, 324; 323, identifier:self; 324, identifier:ds; 325, attribute; 325, 326; 325, 327; 326, identifier:self; 327, identifier:mm; 328, expression_statement; 328, 329; 329, assignment; 329, 330; 329, 333; 330, attribute; 330, 331; 330, 332; 331, identifier:channel; 332, identifier:widget; 333, attribute; 333, 334; 333, 335; 334, identifier:bnch; 335, identifier:widget; 336, expression_statement; 336, 337; 337, assignment; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, identifier:channel; 340, identifier:container; 341, attribute; 341, 342; 341, 343; 342, identifier:bnch; 343, identifier:container; 344, expression_statement; 344, 345; 345, assignment; 345, 346; 345, 349; 346, attribute; 346, 347; 346, 348; 347, identifier:channel; 348, identifier:workspace; 349, attribute; 349, 350; 349, 351; 350, identifier:bnch; 351, identifier:workspace; 352, expression_statement; 352, 353; 353, call; 353, 354; 353, 357; 354, attribute; 354, 355; 354, 356; 355, identifier:channel; 356, identifier:connect_viewer; 357, argument_list; 357, 358; 358, attribute; 358, 359; 358, 360; 359, identifier:bnch; 360, identifier:image_viewer; 361, expression_statement; 361, 362; 362, assignment; 362, 363; 362, 366; 363, attribute; 363, 364; 363, 365; 364, identifier:channel; 365, identifier:viewer; 366, attribute; 366, 367; 366, 368; 367, identifier:bnch; 368, identifier:image_viewer; 369, comment; 370, expression_statement; 370, 371; 371, assignment; 371, 372; 371, 375; 372, attribute; 372, 373; 372, 374; 373, identifier:channel; 374, identifier:fitsimage; 375, attribute; 375, 376; 375, 377; 376, identifier:bnch; 377, identifier:image_viewer; 378, expression_statement; 378, 379; 379, assignment; 379, 380; 379, 383; 380, attribute; 380, 381; 380, 382; 381, identifier:channel; 382, identifier:opmon; 383, identifier:opmon; 384, expression_statement; 384, 385; 385, assignment; 385, 386; 385, 387; 386, identifier:name; 387, call; 387, 388; 387, 391; 388, attribute; 388, 389; 388, 390; 389, identifier:chname; 390, identifier:lower; 391, argument_list; 392, expression_statement; 392, 393; 393, assignment; 393, 394; 393, 399; 394, subscript; 394, 395; 394, 398; 395, attribute; 395, 396; 395, 397; 396, identifier:self; 397, identifier:channel; 398, identifier:name; 399, identifier:channel; 400, comment; 401, expression_statement; 401, 402; 402, call; 402, 403; 402, 408; 403, attribute; 403, 404; 403, 407; 404, attribute; 404, 405; 404, 406; 405, identifier:self; 406, identifier:channel_names; 407, identifier:append; 408, argument_list; 408, 409; 409, identifier:chname; 410, expression_statement; 410, 411; 411, call; 411, 412; 411, 417; 412, attribute; 412, 413; 412, 416; 413, attribute; 413, 414; 413, 415; 414, identifier:self; 415, identifier:channel_names; 416, identifier:sort; 417, argument_list; 418, if_statement; 418, 419; 418, 427; 419, comparison_operator:==; 419, 420; 419, 426; 420, call; 420, 421; 420, 422; 421, identifier:len; 422, argument_list; 422, 423; 423, attribute; 423, 424; 423, 425; 424, identifier:self; 425, identifier:channel_names; 426, integer:1; 427, block; 427, 428; 428, expression_statement; 428, 429; 429, assignment; 429, 430; 429, 433; 430, attribute; 430, 431; 430, 432; 431, identifier:self; 432, identifier:cur_channel; 433, identifier:channel; 434, comment; 435, for_statement; 435, 436; 435, 437; 435, 442; 436, identifier:spec; 437, call; 437, 438; 437, 441; 438, attribute; 438, 439; 438, 440; 439, identifier:self; 440, identifier:get_plugins; 441, argument_list; 442, block; 442, 443; 442, 458; 443, expression_statement; 443, 444; 444, assignment; 444, 445; 444, 446; 445, identifier:opname; 446, call; 446, 447; 446, 450; 447, attribute; 447, 448; 447, 449; 448, identifier:spec; 449, identifier:get; 450, argument_list; 450, 451; 450, 452; 451, string:'klass'; 452, call; 452, 453; 452, 456; 453, attribute; 453, 454; 453, 455; 454, identifier:spec; 455, identifier:get; 456, argument_list; 456, 457; 457, string:'module'; 458, if_statement; 458, 459; 458, 468; 459, comparison_operator:==; 459, 460; 459, 467; 460, call; 460, 461; 460, 464; 461, attribute; 461, 462; 461, 463; 462, identifier:spec; 463, identifier:get; 464, argument_list; 464, 465; 464, 466; 465, string:'ptype'; 466, string:'global'; 467, string:'local'; 468, block; 468, 469; 469, expression_statement; 469, 470; 470, call; 470, 471; 470, 474; 471, attribute; 471, 472; 471, 473; 472, identifier:opmon; 473, identifier:load_plugin; 474, argument_list; 474, 475; 474, 476; 474, 477; 475, identifier:opname; 476, identifier:spec; 477, keyword_argument; 477, 478; 477, 479; 478, identifier:chinfo; 479, identifier:channel; 480, expression_statement; 480, 481; 481, call; 481, 482; 481, 485; 482, attribute; 482, 483; 482, 484; 483, identifier:self; 484, identifier:make_gui_callback; 485, argument_list; 485, 486; 485, 487; 486, string:'add-channel'; 487, identifier:channel; 488, return_statement; 488, 489; 489, identifier:channel
def add_channel(self, chname, workspace=None, num_images=None, settings=None, settings_template=None, settings_share=None, share_keylist=None): """Create a new Ginga channel. Parameters ---------- chname : str The name of the channel to create. workspace : str or None The name of the workspace in which to create the channel num_images : int or None The cache size for the number of images to keep in memory settings : `~ginga.misc.Settings.SettingGroup` or `None` Viewer preferences. If not given, one will be created. settings_template : `~ginga.misc.Settings.SettingGroup` or `None` Viewer preferences template settings_share : `~ginga.misc.Settings.SettingGroup` or `None` Viewer preferences instance to share with newly created settings share_keylist : list of str List of names of settings that should be shared Returns ------- channel : `~ginga.misc.Bunch.Bunch` The channel info bunch. """ with self.lock: if self.has_channel(chname): return self.get_channel(chname) if chname in self.ds.get_tabnames(None): raise ValueError("Tab name already in use: '%s'" % (chname)) name = chname if settings is None: settings = self.prefs.create_category('channel_' + name) try: settings.load(onError='raise') except Exception as e: self.logger.warning("no saved preferences found for channel " "'%s': %s" % (name, str(e))) # copy template settings to new channel if settings_template is not None: osettings = settings_template osettings.copy_settings(settings) else: try: # use channel_Image as a template if one was not # provided osettings = self.prefs.get_settings('channel_Image') self.logger.debug("Copying settings from 'Image' to " "'%s'" % (name)) osettings.copy_settings(settings) except KeyError: pass if (share_keylist is not None) and (settings_share is not None): # caller wants us to share settings with another viewer settings_share.share_settings(settings, keylist=share_keylist) # Make sure these preferences are at least defined if num_images is None: num_images = settings.get('numImages', self.settings.get('numImages', 1)) settings.set_defaults(switchnew=True, numImages=num_images, raisenew=True, genthumb=True, focus_indicator=False, preload_images=False, sort_order='loadtime') self.logger.debug("Adding channel '%s'" % (chname)) channel = Channel(chname, self, datasrc=None, settings=settings) bnch = self.add_viewer(chname, settings, workspace=workspace) # for debugging bnch.image_viewer.set_name('channel:%s' % (chname)) opmon = self.get_plugin_manager(self.logger, self, self.ds, self.mm) channel.widget = bnch.widget channel.container = bnch.container channel.workspace = bnch.workspace channel.connect_viewer(bnch.image_viewer) channel.viewer = bnch.image_viewer # older name, should eventually be deprecated channel.fitsimage = bnch.image_viewer channel.opmon = opmon name = chname.lower() self.channel[name] = channel # Update the channels control self.channel_names.append(chname) self.channel_names.sort() if len(self.channel_names) == 1: self.cur_channel = channel # Prepare local plugins for this channel for spec in self.get_plugins(): opname = spec.get('klass', spec.get('module')) if spec.get('ptype', 'global') == 'local': opmon.load_plugin(opname, spec, chinfo=channel) self.make_gui_callback('add-channel', channel) return channel
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:mode_key_down; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:viewer; 6, identifier:keyname; 7, block; 7, 8; 7, 10; 7, 11; 7, 54; 7, 60; 7, 75; 7, 94; 7, 109; 7, 199; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, if_statement; 11, 12; 11, 17; 11, 44; 12, comparison_operator:not; 12, 13; 12, 14; 13, identifier:keyname; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:mode_map; 17, block; 17, 18; 17, 36; 18, if_statement; 18, 19; 18, 32; 18, 33; 19, boolean_operator:or; 19, 20; 19, 26; 20, parenthesized_expression; 20, 21; 21, comparison_operator:not; 21, 22; 21, 23; 22, identifier:keyname; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:mode_tbl; 26, parenthesized_expression; 26, 27; 27, comparison_operator:!=; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:_kbdmode; 31, string:'meta'; 32, comment; 33, block; 33, 34; 34, return_statement; 34, 35; 35, False; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:bnch; 39, subscript; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:mode_tbl; 43, identifier:keyname; 44, else_clause; 44, 45; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:bnch; 49, subscript; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:mode_map; 53, identifier:keyname; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:mode_name; 57, attribute; 57, 58; 57, 59; 58, identifier:bnch; 59, identifier:name; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 67; 62, attribute; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:logger; 66, identifier:debug; 67, argument_list; 67, 68; 68, binary_operator:%; 68, 69; 68, 70; 69, string:"cur mode='%s' mode pressed='%s'"; 70, tuple; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:_kbdmode; 74, identifier:mode_name; 75, if_statement; 75, 76; 75, 81; 75, 82; 75, 83; 75, 84; 76, comparison_operator:==; 76, 77; 76, 78; 77, identifier:mode_name; 78, attribute; 78, 79; 78, 80; 79, identifier:self; 80, identifier:_kbdmode; 81, comment; 82, comment; 83, comment; 84, block; 84, 85; 84, 92; 85, expression_statement; 85, 86; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:reset_mode; 90, argument_list; 90, 91; 91, identifier:viewer; 92, return_statement; 92, 93; 93, True; 94, if_statement; 94, 95; 94, 98; 94, 99; 94, 100; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:_delayed_reset; 98, comment; 99, comment; 100, block; 100, 101; 100, 107; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:self; 105, identifier:_delayed_reset; 106, False; 107, return_statement; 107, 108; 108, True; 109, if_statement; 109, 110; 109, 131; 110, parenthesized_expression; 110, 111; 111, boolean_operator:or; 111, 112; 111, 127; 112, boolean_operator:or; 112, 113; 112, 121; 113, parenthesized_expression; 113, 114; 114, comparison_operator:in; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:_kbdmode; 118, tuple; 118, 119; 118, 120; 119, None; 120, string:'meta'; 121, parenthesized_expression; 121, 122; 122, comparison_operator:!=; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:_kbdmode_type; 126, string:'locked'; 127, parenthesized_expression; 127, 128; 128, comparison_operator:==; 128, 129; 128, 130; 129, identifier:mode_name; 130, string:'meta'; 131, block; 131, 132; 131, 146; 131, 147; 132, if_statement; 132, 133; 132, 138; 133, comparison_operator:is; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:self; 136, identifier:_kbdmode; 137, None; 138, block; 138, 139; 139, expression_statement; 139, 140; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:self; 143, identifier:reset_mode; 144, argument_list; 144, 145; 145, identifier:viewer; 146, comment; 147, if_statement; 147, 148; 147, 155; 148, comparison_operator:in; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:self; 151, identifier:_kbdmode; 152, tuple; 152, 153; 152, 154; 153, None; 154, string:'meta'; 155, block; 155, 156; 155, 162; 155, 173; 155, 181; 155, 197; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:mode_type; 159, attribute; 159, 160; 159, 161; 160, identifier:bnch; 161, identifier:type; 162, if_statement; 162, 163; 162, 166; 163, comparison_operator:is; 163, 164; 163, 165; 164, identifier:mode_type; 165, None; 166, block; 166, 167; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:mode_type; 170, attribute; 170, 171; 170, 172; 171, identifier:self; 172, identifier:_kbdmode_type_default; 173, expression_statement; 173, 174; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:self; 177, identifier:set_mode; 178, argument_list; 178, 179; 178, 180; 179, identifier:mode_name; 180, identifier:mode_type; 181, if_statement; 181, 182; 181, 187; 182, comparison_operator:is; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:bnch; 185, identifier:msg; 186, None; 187, block; 187, 188; 188, expression_statement; 188, 189; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:viewer; 192, identifier:onscreen_message; 193, argument_list; 193, 194; 194, attribute; 194, 195; 194, 196; 195, identifier:bnch; 196, identifier:msg; 197, return_statement; 197, 198; 198, True; 199, return_statement; 199, 200; 200, False
def mode_key_down(self, viewer, keyname): """This method is called when a key is pressed and was not handled by some other handler with precedence, such as a subcanvas. """ # Is this a mode key? if keyname not in self.mode_map: if (keyname not in self.mode_tbl) or (self._kbdmode != 'meta'): # No return False bnch = self.mode_tbl[keyname] else: bnch = self.mode_map[keyname] mode_name = bnch.name self.logger.debug("cur mode='%s' mode pressed='%s'" % ( self._kbdmode, mode_name)) if mode_name == self._kbdmode: # <== same key was pressed that started the mode we're in # standard handling is to close the mode when we press the # key again that started that mode self.reset_mode(viewer) return True if self._delayed_reset: # <== this shouldn't happen, but put here to reset handling # of delayed_reset just in case (see cursor up handling) self._delayed_reset = False return True if ((self._kbdmode in (None, 'meta')) or (self._kbdmode_type != 'locked') or (mode_name == 'meta')): if self._kbdmode is not None: self.reset_mode(viewer) # activate this mode if self._kbdmode in (None, 'meta'): mode_type = bnch.type if mode_type is None: mode_type = self._kbdmode_type_default self.set_mode(mode_name, mode_type) if bnch.msg is not None: viewer.onscreen_message(bnch.msg) return True return False
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:set_sort_cb; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:w; 6, identifier:index; 7, block; 7, 8; 7, 10; 7, 18; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:name; 13, subscript; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:sort_options; 17, identifier:index; 18, expression_statement; 18, 19; 19, call; 19, 20; 19, 25; 20, attribute; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:t_; 24, identifier:set; 25, argument_list; 25, 26; 26, keyword_argument; 26, 27; 26, 28; 27, identifier:sort_order; 28, identifier:name
def set_sort_cb(self, w, index): """This callback is invoked when the user selects a new sort order from the preferences pane.""" name = self.sort_options[index] self.t_.set(sort_order=name)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:redo; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 18; 5, 25; 5, 35; 5, 50; 5, 51; 5, 52; 5, 53; 5, 63; 5, 205; 5, 220; 5, 230; 5, 231; 5, 241; 5, 253; 5, 259; 5, 263; 5, 317; 5, 318; 5, 319; 5, 320; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:image; 11, call; 11, 12; 11, 17; 12, attribute; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:channel; 16, identifier:get_current_image; 17, argument_list; 18, if_statement; 18, 19; 18, 22; 19, comparison_operator:is; 19, 20; 19, 21; 20, identifier:image; 21, None; 22, block; 22, 23; 23, return_statement; 23, 24; 24, True; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:path; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:image; 31, identifier:get; 32, argument_list; 32, 33; 32, 34; 33, string:'path'; 34, None; 35, if_statement; 35, 36; 35, 39; 36, comparison_operator:is; 36, 37; 36, 38; 37, identifier:path; 38, None; 39, block; 39, 40; 39, 49; 40, expression_statement; 40, 41; 41, call; 41, 42; 41, 47; 42, attribute; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:fv; 46, identifier:show_error; 47, argument_list; 47, 48; 48, string:"Cannot open image: no value for metadata key 'path'"; 49, return_statement; 50, comment; 51, comment; 52, comment; 53, if_statement; 53, 54; 53, 60; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:path; 57, identifier:endswith; 58, argument_list; 58, 59; 59, string:'asdf'; 60, block; 60, 61; 61, return_statement; 61, 62; 62, True; 63, if_statement; 63, 64; 63, 69; 63, 70; 64, comparison_operator:!=; 64, 65; 64, 66; 65, identifier:path; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:img_path; 69, comment; 70, block; 70, 71; 70, 77; 70, 78; 70, 99; 70, 114; 70, 115; 70, 124; 70, 135; 70, 151; 70, 157; 70, 171; 70, 187; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:self; 75, identifier:img_path; 76, identifier:path; 77, comment; 78, if_statement; 78, 79; 78, 84; 79, comparison_operator:is; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:self; 82, identifier:file_obj; 83, None; 84, block; 84, 85; 85, try_statement; 85, 86; 85, 95; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, call; 88, 89; 88, 94; 89, attribute; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:self; 92, identifier:file_obj; 93, identifier:close; 94, argument_list; 95, except_clause; 95, 96; 95, 97; 96, identifier:Exception; 97, block; 97, 98; 98, pass_statement; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:self; 103, identifier:file_obj; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:io_fits; 107, identifier:get_fitsloader; 108, argument_list; 108, 109; 109, keyword_argument; 109, 110; 109, 111; 110, identifier:logger; 111, attribute; 111, 112; 111, 113; 112, identifier:self; 113, identifier:logger; 114, comment; 115, expression_statement; 115, 116; 116, call; 116, 117; 116, 122; 117, attribute; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:self; 120, identifier:file_obj; 121, identifier:open_file; 122, argument_list; 122, 123; 123, identifier:path; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:upper; 127, binary_operator:-; 127, 128; 127, 134; 128, call; 128, 129; 128, 130; 129, identifier:len; 130, argument_list; 130, 131; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:file_obj; 134, integer:1; 135, expression_statement; 135, 136; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:self; 139, identifier:prep_hdu_menu; 140, argument_list; 140, 141; 140, 146; 141, attribute; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:self; 144, identifier:w; 145, identifier:hdu; 146, attribute; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:self; 149, identifier:file_obj; 150, identifier:hdu_info; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:self; 155, identifier:num_hdu; 156, identifier:upper; 157, expression_statement; 157, 158; 158, call; 158, 159; 158, 164; 159, attribute; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:self; 162, identifier:logger; 163, identifier:debug; 164, argument_list; 164, 165; 165, binary_operator:%; 165, 166; 165, 167; 166, string:"there are %d hdus"; 167, parenthesized_expression; 167, 168; 168, binary_operator:+; 168, 169; 168, 170; 169, identifier:upper; 170, integer:1; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 180; 173, attribute; 173, 174; 173, 179; 174, attribute; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:self; 177, identifier:w; 178, identifier:numhdu; 179, identifier:set_text; 180, argument_list; 180, 181; 181, binary_operator:%; 181, 182; 181, 183; 182, string:"%d"; 183, parenthesized_expression; 183, 184; 184, binary_operator:+; 184, 185; 184, 186; 185, identifier:upper; 186, integer:1; 187, expression_statement; 187, 188; 188, call; 188, 189; 188, 196; 189, attribute; 189, 190; 189, 195; 190, attribute; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:self; 193, identifier:w; 194, identifier:hdu; 195, identifier:set_enabled; 196, argument_list; 196, 197; 197, comparison_operator:>; 197, 198; 197, 204; 198, call; 198, 199; 198, 200; 199, identifier:len; 200, argument_list; 200, 201; 201, attribute; 201, 202; 201, 203; 202, identifier:self; 203, identifier:file_obj; 204, integer:0; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:name; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:image; 211, identifier:get; 212, argument_list; 212, 213; 212, 214; 213, string:'name'; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:iohelper; 217, identifier:name_image_from_path; 218, argument_list; 218, 219; 219, identifier:path; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 223; 222, identifier:idx; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:image; 226, identifier:get; 227, argument_list; 227, 228; 227, 229; 228, string:'idx'; 229, None; 230, comment; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:match; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:re; 237, identifier:match; 238, argument_list; 238, 239; 238, 240; 239, string:r'^(.+)\[(.+)\]$'; 240, identifier:name; 241, if_statement; 241, 242; 241, 243; 242, identifier:match; 243, block; 243, 244; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 247; 246, identifier:name; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:match; 250, identifier:group; 251, argument_list; 251, 252; 252, integer:1; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:self; 257, identifier:name_pfx; 258, identifier:name; 259, expression_statement; 259, 260; 260, assignment; 260, 261; 260, 262; 261, identifier:htype; 262, None; 263, if_statement; 263, 264; 263, 267; 263, 268; 264, comparison_operator:is; 264, 265; 264, 266; 265, identifier:idx; 266, None; 267, comment; 268, block; 268, 269; 268, 283; 269, expression_statement; 269, 270; 270, assignment; 270, 271; 270, 272; 271, identifier:info; 272, call; 272, 273; 272, 280; 273, attribute; 273, 274; 273, 279; 274, attribute; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:self; 277, identifier:file_obj; 278, identifier:hdu_db; 279, identifier:get; 280, argument_list; 280, 281; 280, 282; 281, identifier:idx; 282, None; 283, if_statement; 283, 284; 283, 287; 284, comparison_operator:is; 284, 285; 284, 286; 285, identifier:info; 286, None; 287, block; 287, 288; 287, 298; 287, 306; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 291; 290, identifier:htype; 291, call; 291, 292; 291, 297; 292, attribute; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:info; 295, identifier:htype; 296, identifier:lower; 297, argument_list; 298, expression_statement; 298, 299; 299, assignment; 299, 300; 299, 301; 300, identifier:toc_ent; 301, binary_operator:%; 301, 302; 301, 305; 302, attribute; 302, 303; 302, 304; 303, identifier:self; 304, identifier:_toc_fmt; 305, identifier:info; 306, expression_statement; 306, 307; 307, call; 307, 308; 307, 315; 308, attribute; 308, 309; 308, 314; 309, attribute; 309, 310; 309, 313; 310, attribute; 310, 311; 310, 312; 311, identifier:self; 312, identifier:w; 313, identifier:hdu; 314, identifier:show_text; 315, argument_list; 315, 316; 316, identifier:toc_ent; 317, comment; 318, comment; 319, comment; 320, if_statement; 320, 321; 320, 326; 321, comparison_operator:!=; 321, 322; 321, 325; 322, attribute; 322, 323; 322, 324; 323, identifier:self; 324, identifier:img_name; 325, identifier:name; 326, block; 326, 327; 326, 333; 326, 339; 326, 347; 326, 430; 327, expression_statement; 327, 328; 328, assignment; 328, 329; 328, 332; 329, attribute; 329, 330; 329, 331; 330, identifier:self; 331, identifier:img_name; 332, identifier:name; 333, expression_statement; 333, 334; 334, assignment; 334, 335; 334, 336; 335, identifier:dims; 336, list:[0, 0]; 336, 337; 336, 338; 337, integer:0; 338, integer:0; 339, expression_statement; 339, 340; 340, assignment; 340, 341; 340, 342; 341, identifier:data; 342, call; 342, 343; 342, 346; 343, attribute; 343, 344; 343, 345; 344, identifier:image; 345, identifier:get_data; 346, argument_list; 347, if_statement; 347, 348; 347, 351; 347, 352; 347, 368; 347, 376; 347, 400; 348, comparison_operator:is; 348, 349; 348, 350; 349, identifier:data; 350, None; 351, comment; 352, block; 352, 353; 353, expression_statement; 353, 354; 354, call; 354, 355; 354, 360; 355, attribute; 355, 356; 355, 359; 356, attribute; 356, 357; 356, 358; 357, identifier:self; 358, identifier:logger; 359, identifier:warning; 360, argument_list; 360, 361; 361, binary_operator:%; 361, 362; 361, 363; 362, string:"Empty data part in HDU %s"; 363, parenthesized_expression; 363, 364; 364, call; 364, 365; 364, 366; 365, identifier:str; 366, argument_list; 366, 367; 367, identifier:idx; 368, elif_clause; 368, 369; 368, 374; 369, comparison_operator:in; 369, 370; 369, 371; 370, identifier:htype; 371, tuple; 371, 372; 371, 373; 372, string:'bintablehdu'; 373, string:'tablehdu'; 374, block; 374, 375; 375, pass_statement; 376, elif_clause; 376, 377; 376, 383; 377, comparison_operator:not; 377, 378; 377, 379; 378, identifier:htype; 379, tuple; 379, 380; 379, 381; 379, 382; 380, string:'imagehdu'; 381, string:'primaryhdu'; 382, string:'compimagehdu'; 383, block; 383, 384; 384, expression_statement; 384, 385; 385, call; 385, 386; 385, 391; 386, attribute; 386, 387; 386, 390; 387, attribute; 387, 388; 387, 389; 388, identifier:self; 389, identifier:logger; 390, identifier:warning; 391, argument_list; 391, 392; 392, binary_operator:%; 392, 393; 392, 394; 393, string:"HDU %s is not an image (%s)"; 394, tuple; 394, 395; 394, 399; 395, call; 395, 396; 395, 397; 396, identifier:str; 397, argument_list; 397, 398; 398, identifier:idx; 399, identifier:htype; 400, else_clause; 400, 401; 401, block; 401, 402; 401, 410; 402, expression_statement; 402, 403; 403, assignment; 403, 404; 403, 405; 404, identifier:mddata; 405, call; 405, 406; 405, 409; 406, attribute; 406, 407; 406, 408; 407, identifier:image; 408, identifier:get_mddata; 409, argument_list; 410, if_statement; 410, 411; 410, 414; 411, comparison_operator:is; 411, 412; 411, 413; 412, identifier:mddata; 413, None; 414, block; 414, 415; 414, 424; 415, expression_statement; 415, 416; 416, assignment; 416, 417; 416, 418; 417, identifier:dims; 418, call; 418, 419; 418, 420; 419, identifier:list; 420, argument_list; 420, 421; 421, attribute; 421, 422; 421, 423; 422, identifier:mddata; 423, identifier:shape; 424, expression_statement; 424, 425; 425, call; 425, 426; 425, 429; 426, attribute; 426, 427; 426, 428; 427, identifier:dims; 428, identifier:reverse; 429, argument_list; 430, expression_statement; 430, 431; 431, call; 431, 432; 431, 435; 432, attribute; 432, 433; 432, 434; 433, identifier:self; 434, identifier:build_naxis; 435, argument_list; 435, 436; 435, 437; 436, identifier:dims; 437, identifier:image
def redo(self): """Called when an image is set in the channel.""" image = self.channel.get_current_image() if image is None: return True path = image.get('path', None) if path is None: self.fv.show_error( "Cannot open image: no value for metadata key 'path'") return # TODO: How to properly reset GUI components? # They are still showing info from prev FITS. # No-op for ASDF if path.endswith('asdf'): return True if path != self.img_path: # <-- New file is being looked at self.img_path = path # close previous file opener, if any if self.file_obj is not None: try: self.file_obj.close() except Exception: pass self.file_obj = io_fits.get_fitsloader(logger=self.logger) # TODO: specify 'readonly' somehow? self.file_obj.open_file(path) upper = len(self.file_obj) - 1 self.prep_hdu_menu(self.w.hdu, self.file_obj.hdu_info) self.num_hdu = upper self.logger.debug("there are %d hdus" % (upper + 1)) self.w.numhdu.set_text("%d" % (upper + 1)) self.w.hdu.set_enabled(len(self.file_obj) > 0) name = image.get('name', iohelper.name_image_from_path(path)) idx = image.get('idx', None) # remove index designation from root of name, if any match = re.match(r'^(.+)\[(.+)\]$', name) if match: name = match.group(1) self.name_pfx = name htype = None if idx is not None: # set the HDU in the drop down if known info = self.file_obj.hdu_db.get(idx, None) if info is not None: htype = info.htype.lower() toc_ent = self._toc_fmt % info self.w.hdu.show_text(toc_ent) # rebuild the NAXIS controls, if necessary # No two images in the same channel can have the same name. # Here we keep track of the name to decide if we need to rebuild if self.img_name != name: self.img_name = name dims = [0, 0] data = image.get_data() if data is None: # <- empty data part to this HDU self.logger.warning("Empty data part in HDU %s" % (str(idx))) elif htype in ('bintablehdu', 'tablehdu',): pass elif htype not in ('imagehdu', 'primaryhdu', 'compimagehdu'): self.logger.warning("HDU %s is not an image (%s)" % ( str(idx), htype)) else: mddata = image.get_mddata() if mddata is not None: dims = list(mddata.shape) dims.reverse() self.build_naxis(dims, image)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:_download_rtd_zip; 3, parameters; 3, 4; 3, 7; 4, default_parameter; 4, 5; 4, 6; 5, identifier:rtd_version; 6, None; 7, dictionary_splat_pattern; 7, 8; 8, identifier:kwargs; 9, block; 9, 10; 9, 12; 9, 13; 9, 38; 9, 49; 9, 66; 9, 78; 9, 79; 9, 80; 9, 81; 9, 93; 9, 105; 9, 120; 9, 141; 9, 142; 9, 143; 9, 160; 9, 218; 9, 225; 9, 246; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, if_statement; 13, 14; 13, 23; 14, not_operator; 14, 15; 15, call; 15, 16; 15, 21; 16, attribute; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:toolkit; 19, identifier:family; 20, identifier:startswith; 21, argument_list; 21, 22; 22, string:'qt'; 23, block; 23, 24; 24, raise_statement; 24, 25; 25, call; 25, 26; 25, 27; 26, identifier:ValueError; 27, argument_list; 27, 28; 28, call; 28, 29; 28, 34; 29, attribute; 29, 30; 29, 33; 30, concatenated_string; 30, 31; 30, 32; 31, string:'Downloaded documentation not compatible with {} '; 32, string:'UI toolkit browser'; 33, identifier:format; 34, argument_list; 34, 35; 35, attribute; 35, 36; 35, 37; 36, identifier:toolkit; 37, identifier:family; 38, if_statement; 38, 39; 38, 42; 39, comparison_operator:is; 39, 40; 39, 41; 40, identifier:rtd_version; 41, None; 42, block; 42, 43; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:rtd_version; 46, call; 46, 47; 46, 48; 47, identifier:_find_rtd_version; 48, argument_list; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:data_path; 52, call; 52, 53; 52, 58; 53, attribute; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:os; 56, identifier:path; 57, identifier:dirname; 58, argument_list; 58, 59; 59, call; 59, 60; 59, 61; 60, identifier:_find_pkg_data_path; 61, argument_list; 61, 62; 61, 63; 62, string:'help.html'; 63, keyword_argument; 63, 64; 63, 65; 64, identifier:package; 65, string:'ginga.doc'; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:index_html; 69, call; 69, 70; 69, 75; 70, attribute; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:os; 73, identifier:path; 74, identifier:join; 75, argument_list; 75, 76; 75, 77; 76, identifier:data_path; 77, string:'index.html'; 78, comment; 79, comment; 80, comment; 81, if_statement; 81, 82; 81, 90; 82, call; 82, 83; 82, 88; 83, attribute; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:os; 86, identifier:path; 87, identifier:isfile; 88, argument_list; 88, 89; 89, identifier:index_html; 90, block; 90, 91; 91, return_statement; 91, 92; 92, identifier:index_html; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:url; 96, parenthesized_expression; 96, 97; 97, call; 97, 98; 97, 103; 98, attribute; 98, 99; 98, 102; 99, concatenated_string; 99, 100; 99, 101; 100, string:'https://readthedocs.org/projects/ginga/downloads/htmlzip/'; 101, string:'{}/'; 102, identifier:format; 103, argument_list; 103, 104; 104, identifier:rtd_version; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:local_path; 108, subscript; 108, 109; 108, 119; 109, call; 109, 110; 109, 115; 110, attribute; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:urllib; 113, identifier:request; 114, identifier:urlretrieve; 115, argument_list; 115, 116; 115, 117; 116, identifier:url; 117, dictionary_splat; 117, 118; 118, identifier:kwargs; 119, integer:0; 120, with_statement; 120, 121; 120, 133; 121, with_clause; 121, 122; 122, with_item; 122, 123; 123, as_pattern; 123, 124; 123, 131; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:zipfile; 127, identifier:ZipFile; 128, argument_list; 128, 129; 128, 130; 129, identifier:local_path; 130, string:'r'; 131, as_pattern_target; 131, 132; 132, identifier:zf; 133, block; 133, 134; 134, expression_statement; 134, 135; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:zf; 138, identifier:extractall; 139, argument_list; 139, 140; 140, identifier:data_path; 141, comment; 142, comment; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:subdir; 146, call; 146, 147; 146, 152; 147, attribute; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:os; 150, identifier:path; 151, identifier:join; 152, argument_list; 152, 153; 152, 154; 153, identifier:data_path; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, string:'ginga-{}'; 157, identifier:format; 158, argument_list; 158, 159; 159, identifier:rtd_version; 160, for_statement; 160, 161; 160, 162; 160, 168; 161, identifier:s; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:os; 165, identifier:listdir; 166, argument_list; 166, 167; 167, identifier:subdir; 168, block; 168, 169; 168, 181; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:src; 172, call; 172, 173; 172, 178; 173, attribute; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:os; 176, identifier:path; 177, identifier:join; 178, argument_list; 178, 179; 178, 180; 179, identifier:subdir; 180, identifier:s; 181, if_statement; 181, 182; 181, 190; 181, 199; 182, call; 182, 183; 182, 188; 183, attribute; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:os; 186, identifier:path; 187, identifier:isfile; 188, argument_list; 188, 189; 189, identifier:src; 190, block; 190, 191; 191, expression_statement; 191, 192; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:shutil; 195, identifier:copy; 196, argument_list; 196, 197; 196, 198; 197, identifier:src; 198, identifier:data_path; 199, else_clause; 199, 200; 199, 201; 200, comment; 201, block; 201, 202; 202, expression_statement; 202, 203; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:shutil; 206, identifier:copytree; 207, argument_list; 207, 208; 207, 209; 208, identifier:src; 209, call; 209, 210; 209, 215; 210, attribute; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:os; 213, identifier:path; 214, identifier:join; 215, argument_list; 215, 216; 215, 217; 216, identifier:data_path; 217, identifier:s; 218, expression_statement; 218, 219; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:shutil; 222, identifier:rmtree; 223, argument_list; 223, 224; 224, identifier:subdir; 225, if_statement; 225, 226; 225, 235; 226, not_operator; 226, 227; 227, call; 227, 228; 227, 233; 228, attribute; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:os; 231, identifier:path; 232, identifier:isfile; 233, argument_list; 233, 234; 234, identifier:index_html; 235, block; 235, 236; 236, raise_statement; 236, 237; 237, call; 237, 238; 237, 239; 238, identifier:OSError; 239, argument_list; 239, 240; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, string:'{} is missing; Ginga doc download failed'; 243, identifier:format; 244, argument_list; 244, 245; 245, identifier:index_html; 246, return_statement; 246, 247; 247, identifier:index_html
def _download_rtd_zip(rtd_version=None, **kwargs): """ Download and extract HTML ZIP from RTD to installed doc data path. Download is skipped if content already exists. Parameters ---------- rtd_version : str or `None` RTD version to download; e.g., "latest", "stable", or "v2.6.0". If not given, download closest match to software version. kwargs : dict Keywords for ``urlretrieve()``. Returns ------- index_html : str Path to local "index.html". """ # https://github.com/ejeschke/ginga/pull/451#issuecomment-298403134 if not toolkit.family.startswith('qt'): raise ValueError('Downloaded documentation not compatible with {} ' 'UI toolkit browser'.format(toolkit.family)) if rtd_version is None: rtd_version = _find_rtd_version() data_path = os.path.dirname( _find_pkg_data_path('help.html', package='ginga.doc')) index_html = os.path.join(data_path, 'index.html') # There is a previous download of documentation; Do nothing. # There is no check if downloaded version is outdated; The idea is that # this folder would be empty again when installing new version. if os.path.isfile(index_html): return index_html url = ('https://readthedocs.org/projects/ginga/downloads/htmlzip/' '{}/'.format(rtd_version)) local_path = urllib.request.urlretrieve(url, **kwargs)[0] with zipfile.ZipFile(local_path, 'r') as zf: zf.extractall(data_path) # RTD makes an undesirable sub-directory, so move everything there # up one level and delete it. subdir = os.path.join(data_path, 'ginga-{}'.format(rtd_version)) for s in os.listdir(subdir): src = os.path.join(subdir, s) if os.path.isfile(src): shutil.copy(src, data_path) else: # directory shutil.copytree(src, os.path.join(data_path, s)) shutil.rmtree(subdir) if not os.path.isfile(index_html): raise OSError( '{} is missing; Ginga doc download failed'.format(index_html)) return index_html
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:get_doc; 3, parameters; 3, 4; 3, 7; 3, 10; 4, default_parameter; 4, 5; 4, 6; 5, identifier:logger; 6, None; 7, default_parameter; 7, 8; 7, 9; 8, identifier:plugin; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:reporthook; 12, None; 13, block; 13, 14; 13, 16; 13, 24; 13, 70; 13, 172; 14, expression_statement; 14, 15; 15, comment; 16, import_from_statement; 16, 17; 16, 20; 16, 22; 17, dotted_name; 17, 18; 17, 19; 18, identifier:ginga; 19, identifier:GingaPlugin; 20, dotted_name; 20, 21; 21, identifier:GlobalPlugin; 22, dotted_name; 22, 23; 23, identifier:LocalPlugin; 24, if_statement; 24, 25; 24, 30; 24, 42; 24, 60; 25, call; 25, 26; 25, 27; 26, identifier:isinstance; 27, argument_list; 27, 28; 27, 29; 28, identifier:plugin; 29, identifier:GlobalPlugin; 30, block; 30, 31; 30, 35; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:plugin_page; 34, string:'plugins_global'; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:plugin_name; 38, call; 38, 39; 38, 40; 39, identifier:str; 40, argument_list; 40, 41; 41, identifier:plugin; 42, elif_clause; 42, 43; 42, 48; 43, call; 43, 44; 43, 45; 44, identifier:isinstance; 45, argument_list; 45, 46; 45, 47; 46, identifier:plugin; 47, identifier:LocalPlugin; 48, block; 48, 49; 48, 53; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:plugin_page; 52, string:'plugins_local'; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:plugin_name; 56, call; 56, 57; 56, 58; 57, identifier:str; 58, argument_list; 58, 59; 59, identifier:plugin; 60, else_clause; 60, 61; 61, block; 61, 62; 61, 66; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:plugin_page; 65, None; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:plugin_name; 69, None; 70, try_statement; 70, 71; 70, 81; 70, 82; 70, 140; 70, 141; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:index_html; 75, call; 75, 76; 75, 77; 76, identifier:_download_rtd_zip; 77, argument_list; 77, 78; 78, keyword_argument; 78, 79; 78, 80; 79, identifier:reporthook; 80, identifier:reporthook; 81, comment; 82, except_clause; 82, 83; 82, 87; 83, as_pattern; 83, 84; 83, 85; 84, identifier:Exception; 85, as_pattern_target; 85, 86; 86, identifier:e; 87, block; 87, 88; 87, 92; 87, 125; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:url; 91, string:'https://ginga.readthedocs.io/en/latest/'; 92, if_statement; 92, 93; 92, 96; 93, comparison_operator:is; 93, 94; 93, 95; 94, identifier:plugin_name; 95, None; 96, block; 96, 97; 97, if_statement; 97, 98; 97, 106; 97, 107; 97, 112; 98, call; 98, 99; 98, 104; 99, attribute; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:toolkit; 102, identifier:family; 103, identifier:startswith; 104, argument_list; 104, 105; 105, string:'qt'; 106, comment; 107, block; 107, 108; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:url; 111, None; 112, else_clause; 112, 113; 112, 114; 113, comment; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, augmented_assignment:+=; 116, 117; 116, 118; 117, identifier:url; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, string:'manual/{}/{}.html'; 121, identifier:format; 122, argument_list; 122, 123; 122, 124; 123, identifier:plugin_page; 124, identifier:plugin_name; 125, if_statement; 125, 126; 125, 129; 126, comparison_operator:is; 126, 127; 126, 128; 127, identifier:logger; 128, None; 129, block; 129, 130; 130, expression_statement; 130, 131; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:logger; 134, identifier:error; 135, argument_list; 135, 136; 136, call; 136, 137; 136, 138; 137, identifier:str; 138, argument_list; 138, 139; 139, identifier:e; 140, comment; 141, else_clause; 141, 142; 142, block; 142, 143; 142, 147; 142, 157; 142, 158; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:pfx; 146, string:'file:'; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:url; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, string:'{}{}'; 153, identifier:format; 154, argument_list; 154, 155; 154, 156; 155, identifier:pfx; 156, identifier:index_html; 157, comment; 158, if_statement; 158, 159; 158, 162; 159, comparison_operator:is; 159, 160; 159, 161; 160, identifier:plugin_name; 161, None; 162, block; 162, 163; 163, expression_statement; 163, 164; 164, augmented_assignment:+=; 164, 165; 164, 166; 165, identifier:url; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, string:'#{}'; 169, identifier:format; 170, argument_list; 170, 171; 171, identifier:plugin_name; 172, return_statement; 172, 173; 173, identifier:url
def get_doc(logger=None, plugin=None, reporthook=None): """ Return URL to documentation. Attempt download if does not exist. Parameters ---------- logger : obj or `None` Ginga logger. plugin : obj or `None` Plugin object. If given, URL points to plugin doc directly. If this function is called from within plugin class, pass ``self`` here. reporthook : callable or `None` Report hook for ``urlretrieve()``. Returns ------- url : str or `None` URL to local documentation, if available. """ from ginga.GingaPlugin import GlobalPlugin, LocalPlugin if isinstance(plugin, GlobalPlugin): plugin_page = 'plugins_global' plugin_name = str(plugin) elif isinstance(plugin, LocalPlugin): plugin_page = 'plugins_local' plugin_name = str(plugin) else: plugin_page = None plugin_name = None try: index_html = _download_rtd_zip(reporthook=reporthook) # Download failed, use online resource except Exception as e: url = 'https://ginga.readthedocs.io/en/latest/' if plugin_name is not None: if toolkit.family.startswith('qt'): # This displays plugin docstring. url = None else: # This redirects to online doc. url += 'manual/{}/{}.html'.format(plugin_page, plugin_name) if logger is not None: logger.error(str(e)) # Use local resource else: pfx = 'file:' url = '{}{}'.format(pfx, index_html) # https://github.com/rtfd/readthedocs.org/issues/2803 if plugin_name is not None: url += '#{}'.format(plugin_name) return url
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:redo; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, list_splat_pattern; 5, 6; 6, identifier:args; 7, block; 7, 8; 7, 10; 7, 17; 7, 29; 7, 37; 7, 45; 7, 56; 7, 69; 7, 75; 7, 76; 7, 103; 7, 104; 7, 331; 7, 340; 7, 341; 7, 348; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 15; 11, not_operator; 11, 12; 12, attribute; 12, 13; 12, 14; 13, identifier:self; 14, identifier:gui_up; 15, block; 15, 16; 16, return_statement; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:mod_only; 20, call; 20, 21; 20, 28; 21, attribute; 21, 22; 21, 27; 22, attribute; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:w; 26, identifier:modified_only; 27, identifier:get_state; 28, argument_list; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:treedict; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:Bunch; 35, identifier:caselessDict; 36, argument_list; 37, expression_statement; 37, 38; 38, call; 38, 39; 38, 44; 39, attribute; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:treeview; 43, identifier:clear; 44, argument_list; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 54; 47, attribute; 47, 48; 47, 53; 48, attribute; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:w; 52, identifier:status; 53, identifier:set_text; 54, argument_list; 54, 55; 55, string:''; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:channel; 59, call; 59, 60; 59, 65; 60, attribute; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:self; 63, identifier:fv; 64, identifier:get_channel; 65, argument_list; 65, 66; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:chname; 69, if_statement; 69, 70; 69, 73; 70, comparison_operator:is; 70, 71; 70, 72; 71, identifier:channel; 72, None; 73, block; 73, 74; 74, return_statement; 75, comment; 76, if_statement; 76, 77; 76, 78; 76, 92; 76, 93; 77, identifier:mod_only; 78, block; 78, 79; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:all_keys; 82, call; 82, 83; 82, 88; 83, attribute; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:channel; 86, identifier:datasrc; 87, identifier:keys; 88, argument_list; 88, 89; 89, keyword_argument; 89, 90; 89, 91; 90, identifier:sort; 91, string:'alpha'; 92, comment; 93, else_clause; 93, 94; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:all_keys; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:channel; 101, identifier:get_image_names; 102, argument_list; 103, comment; 104, for_statement; 104, 105; 104, 106; 104, 107; 105, identifier:key; 106, identifier:all_keys; 107, block; 107, 108; 107, 117; 107, 126; 107, 135; 107, 144; 107, 205; 107, 206; 107, 211; 107, 212; 107, 220; 107, 221; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:iminfo; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:channel; 114, identifier:get_image_info; 115, argument_list; 115, 116; 116, identifier:key; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:path; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:iminfo; 123, identifier:get; 124, argument_list; 124, 125; 125, string:'path'; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:idx; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:iminfo; 132, identifier:get; 133, argument_list; 133, 134; 134, string:'idx'; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:t; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:iminfo; 141, identifier:get; 142, argument_list; 142, 143; 143, string:'time_modified'; 144, if_statement; 144, 145; 144, 148; 144, 149; 144, 158; 145, comparison_operator:is; 145, 146; 145, 147; 146, identifier:path; 147, None; 148, comment; 149, block; 149, 150; 149, 154; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:infile; 153, identifier:key; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:is_fits; 157, True; 158, else_clause; 158, 159; 159, block; 159, 160; 159, 171; 159, 184; 159, 192; 159, 196; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:infile; 163, call; 163, 164; 163, 169; 164, attribute; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:os; 167, identifier:path; 168, identifier:basename; 169, argument_list; 169, 170; 170, identifier:path; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:infile_ext; 174, subscript; 174, 175; 174, 183; 175, call; 175, 176; 175, 181; 176, attribute; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:os; 179, identifier:path; 180, identifier:splitext; 181, argument_list; 181, 182; 182, identifier:path; 183, integer:1; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:infile_ext; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:infile_ext; 190, identifier:lower; 191, argument_list; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:is_fits; 195, False; 196, if_statement; 196, 197; 196, 200; 197, comparison_operator:in; 197, 198; 197, 199; 198, string:'fit'; 199, identifier:infile_ext; 200, block; 200, 201; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 204; 203, identifier:is_fits; 204, True; 205, comment; 206, if_statement; 206, 207; 206, 209; 207, not_operator; 207, 208; 208, identifier:is_fits; 209, block; 209, 210; 210, continue_statement; 211, comment; 212, if_statement; 212, 213; 212, 218; 213, boolean_operator:and; 213, 214; 213, 215; 214, identifier:mod_only; 215, comparison_operator:is; 215, 216; 215, 217; 216, identifier:t; 217, None; 218, block; 218, 219; 219, continue_statement; 220, comment; 221, if_statement; 221, 222; 221, 225; 221, 272; 221, 273; 222, comparison_operator:in; 222, 223; 222, 224; 223, identifier:infile; 224, identifier:treedict; 225, block; 225, 226; 226, if_statement; 226, 227; 226, 230; 227, comparison_operator:is; 227, 228; 227, 229; 228, identifier:t; 229, None; 230, block; 230, 231; 230, 242; 230, 253; 231, expression_statement; 231, 232; 232, call; 232, 233; 232, 240; 233, attribute; 233, 234; 233, 239; 234, attribute; 234, 235; 234, 238; 235, subscript; 235, 236; 235, 237; 236, identifier:treedict; 237, identifier:infile; 238, identifier:extlist; 239, identifier:add; 240, argument_list; 240, 241; 241, identifier:idx; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 245; 244, identifier:elist; 245, call; 245, 246; 245, 247; 246, identifier:sorted; 247, argument_list; 247, 248; 248, attribute; 248, 249; 248, 252; 249, subscript; 249, 250; 249, 251; 250, identifier:treedict; 251, identifier:infile; 252, identifier:extlist; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 260; 255, attribute; 255, 256; 255, 259; 256, subscript; 256, 257; 256, 258; 257, identifier:treedict; 258, identifier:infile; 259, identifier:MODEXT; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, string:';'; 263, identifier:join; 264, argument_list; 264, 265; 265, call; 265, 266; 265, 267; 266, identifier:map; 267, argument_list; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:self; 270, identifier:_format_extname; 271, identifier:elist; 272, comment; 273, else_clause; 273, 274; 274, block; 274, 275; 274, 309; 275, if_statement; 275, 276; 275, 279; 275, 290; 276, comparison_operator:is; 276, 277; 276, 278; 277, identifier:t; 278, None; 279, block; 279, 280; 279, 284; 280, expression_statement; 280, 281; 281, assignment; 281, 282; 281, 283; 282, identifier:s; 283, string:''; 284, expression_statement; 284, 285; 285, assignment; 285, 286; 285, 287; 286, identifier:extlist; 287, call; 287, 288; 287, 289; 288, identifier:set; 289, argument_list; 290, else_clause; 290, 291; 291, block; 291, 292; 291, 301; 292, expression_statement; 292, 293; 293, assignment; 293, 294; 293, 295; 294, identifier:s; 295, call; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:self; 298, identifier:_format_extname; 299, argument_list; 299, 300; 300, identifier:idx; 301, expression_statement; 301, 302; 302, assignment; 302, 303; 302, 304; 303, identifier:extlist; 304, call; 304, 305; 304, 306; 305, identifier:set; 306, argument_list; 306, 307; 307, list:[idx]; 307, 308; 308, identifier:idx; 309, expression_statement; 309, 310; 310, assignment; 310, 311; 310, 314; 311, subscript; 311, 312; 311, 313; 312, identifier:treedict; 313, identifier:infile; 314, call; 314, 315; 314, 318; 315, attribute; 315, 316; 315, 317; 316, identifier:Bunch; 317, identifier:Bunch; 318, argument_list; 318, 319; 318, 322; 318, 325; 318, 328; 319, keyword_argument; 319, 320; 319, 321; 320, identifier:IMAGE; 321, identifier:infile; 322, keyword_argument; 322, 323; 322, 324; 323, identifier:MODEXT; 324, identifier:s; 325, keyword_argument; 325, 326; 325, 327; 326, identifier:extlist; 327, identifier:extlist; 328, keyword_argument; 328, 329; 328, 330; 329, identifier:path; 330, identifier:path; 331, expression_statement; 331, 332; 332, call; 332, 333; 332, 338; 333, attribute; 333, 334; 333, 337; 334, attribute; 334, 335; 334, 336; 335, identifier:self; 336, identifier:treeview; 337, identifier:set_tree; 338, argument_list; 338, 339; 339, identifier:treedict; 340, comment; 341, expression_statement; 341, 342; 342, assignment; 342, 343; 342, 344; 343, identifier:n_rows; 344, call; 344, 345; 344, 346; 345, identifier:len; 346, argument_list; 346, 347; 347, identifier:treedict; 348, if_statement; 348, 349; 348, 352; 348, 364; 349, comparison_operator:==; 349, 350; 349, 351; 350, identifier:n_rows; 351, integer:0; 352, block; 352, 353; 353, expression_statement; 353, 354; 354, call; 354, 355; 354, 362; 355, attribute; 355, 356; 355, 361; 356, attribute; 356, 357; 356, 360; 357, attribute; 357, 358; 357, 359; 358, identifier:self; 359, identifier:w; 360, identifier:status; 361, identifier:set_text; 362, argument_list; 362, 363; 363, string:'Nothing available for saving'; 364, elif_clause; 364, 365; 364, 376; 365, comparison_operator:<; 365, 366; 365, 367; 366, identifier:n_rows; 367, call; 367, 368; 367, 373; 368, attribute; 368, 369; 368, 372; 369, attribute; 369, 370; 369, 371; 370, identifier:self; 371, identifier:settings; 372, identifier:get; 373, argument_list; 373, 374; 373, 375; 374, string:'max_rows_for_col_resize'; 375, integer:5000; 376, block; 376, 377; 376, 385; 377, expression_statement; 377, 378; 378, call; 378, 379; 378, 384; 379, attribute; 379, 380; 379, 383; 380, attribute; 380, 381; 380, 382; 381, identifier:self; 382, identifier:treeview; 383, identifier:set_optimal_column_widths; 384, argument_list; 385, expression_statement; 385, 386; 386, call; 386, 387; 386, 392; 387, attribute; 387, 388; 387, 391; 388, attribute; 388, 389; 388, 390; 389, identifier:self; 390, identifier:logger; 391, identifier:debug; 392, argument_list; 392, 393; 393, call; 393, 394; 393, 397; 394, attribute; 394, 395; 394, 396; 395, string:'Resized columns for {0} row(s)'; 396, identifier:format; 397, argument_list; 397, 398; 398, identifier:n_rows
def redo(self, *args): """Generate listing of images that user can save.""" if not self.gui_up: return mod_only = self.w.modified_only.get_state() treedict = Bunch.caselessDict() self.treeview.clear() self.w.status.set_text('') channel = self.fv.get_channel(self.chname) if channel is None: return # Only list modified images for saving. Scanning Datasrc is enough. if mod_only: all_keys = channel.datasrc.keys(sort='alpha') # List all images in the channel. else: all_keys = channel.get_image_names() # Extract info for listing and saving for key in all_keys: iminfo = channel.get_image_info(key) path = iminfo.get('path') idx = iminfo.get('idx') t = iminfo.get('time_modified') if path is None: # Special handling for generated buffer, eg mosaic infile = key is_fits = True else: infile = os.path.basename(path) infile_ext = os.path.splitext(path)[1] infile_ext = infile_ext.lower() is_fits = False if 'fit' in infile_ext: is_fits = True # Only list FITS files unless it is Ginga generated buffer if not is_fits: continue # Only list modified buffers if mod_only and t is None: continue # More than one ext modified, append to existing entry if infile in treedict: if t is not None: treedict[infile].extlist.add(idx) elist = sorted(treedict[infile].extlist) treedict[infile].MODEXT = ';'.join( map(self._format_extname, elist)) # Add new entry else: if t is None: s = '' extlist = set() else: s = self._format_extname(idx) extlist = set([idx]) treedict[infile] = Bunch.Bunch( IMAGE=infile, MODEXT=s, extlist=extlist, path=path) self.treeview.set_tree(treedict) # Resize column widths n_rows = len(treedict) if n_rows == 0: self.w.status.set_text('Nothing available for saving') elif n_rows < self.settings.get('max_rows_for_col_resize', 5000): self.treeview.set_optimal_column_widths() self.logger.debug('Resized columns for {0} row(s)'.format(n_rows))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_imload; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:filepath; 6, identifier:kwds; 7, block; 7, 8; 7, 10; 7, 18; 7, 29; 7, 37; 7, 48; 7, 61; 7, 65; 7, 183; 7, 338; 7, 376; 7, 389; 7, 397; 7, 412; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:start_time; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:time; 16, identifier:time; 17, argument_list; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 23; 20, pattern_list; 20, 21; 20, 22; 21, identifier:typ; 22, identifier:enc; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:mimetypes; 26, identifier:guess_type; 27, argument_list; 27, 28; 28, identifier:filepath; 29, if_statement; 29, 30; 29, 32; 30, not_operator; 30, 31; 31, identifier:typ; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:typ; 36, string:'image/jpeg'; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 42; 39, pattern_list; 39, 40; 39, 41; 40, identifier:typ; 41, identifier:subtyp; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:typ; 45, identifier:split; 46, argument_list; 46, 47; 47, string:'/'; 48, expression_statement; 48, 49; 49, call; 49, 50; 49, 55; 50, attribute; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:logger; 54, identifier:debug; 55, argument_list; 55, 56; 56, binary_operator:%; 56, 57; 56, 58; 57, string:"MIME type is %s/%s"; 58, tuple; 58, 59; 58, 60; 59, identifier:typ; 60, identifier:subtyp; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:data_loaded; 64, False; 65, if_statement; 65, 66; 65, 72; 65, 73; 65, 74; 66, boolean_operator:and; 66, 67; 66, 68; 67, identifier:have_opencv; 68, comparison_operator:not; 68, 69; 68, 70; 69, identifier:subtyp; 70, list:['gif']; 70, 71; 71, string:'gif'; 72, comment; 73, comment; 74, block; 74, 75; 74, 79; 74, 95; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:means; 78, string:'opencv'; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:data_np; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:cv2; 85, identifier:imread; 86, argument_list; 86, 87; 86, 88; 87, identifier:filepath; 88, binary_operator:+; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:cv2; 91, identifier:IMREAD_ANYDEPTH; 92, attribute; 92, 93; 92, 94; 93, identifier:cv2; 94, identifier:IMREAD_ANYCOLOR; 95, if_statement; 95, 96; 95, 99; 96, comparison_operator:is; 96, 97; 96, 98; 97, identifier:data_np; 98, None; 99, block; 99, 100; 99, 104; 99, 105; 99, 106; 99, 135; 99, 136; 99, 137; 99, 145; 99, 146; 99, 147; 99, 148; 99, 149; 99, 155; 99, 161; 99, 162; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:data_loaded; 103, True; 104, comment; 105, comment; 106, if_statement; 106, 107; 106, 123; 107, boolean_operator:and; 107, 108; 107, 116; 108, comparison_operator:>=; 108, 109; 108, 115; 109, call; 109, 110; 109, 111; 110, identifier:len; 111, argument_list; 111, 112; 112, attribute; 112, 113; 112, 114; 113, identifier:data_np; 114, identifier:shape; 115, integer:3; 116, comparison_operator:>=; 116, 117; 116, 122; 117, subscript; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:data_np; 120, identifier:shape; 121, integer:2; 122, integer:3; 123, block; 123, 124; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:data_np; 127, subscript; 127, 128; 127, 129; 127, 130; 128, identifier:data_np; 129, ellipsis:...; 130, slice; 130, 131; 130, 132; 130, 133; 131, colon; 132, colon; 133, unary_operator:-; 133, 134; 134, integer:1; 135, comment; 136, comment; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:self; 141, identifier:piexif_getexif; 142, argument_list; 142, 143; 142, 144; 143, identifier:filepath; 144, identifier:kwds; 145, comment; 146, comment; 147, comment; 148, comment; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 154; 151, subscript; 151, 152; 151, 153; 152, identifier:kwds; 153, string:'Orientation'; 154, integer:1; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 160; 157, subscript; 157, 158; 157, 159; 158, identifier:kwds; 159, string:'Image Orientation'; 160, integer:1; 161, comment; 162, if_statement; 162, 163; 162, 170; 163, call; 163, 164; 163, 169; 164, attribute; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:self; 167, identifier:clr_mgr; 168, identifier:can_profile; 169, argument_list; 170, block; 170, 171; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:data_np; 174, call; 174, 175; 174, 180; 175, attribute; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:self; 178, identifier:clr_mgr; 179, identifier:profile_to_working_numpy; 180, argument_list; 180, 181; 180, 182; 181, identifier:data_np; 182, identifier:kwds; 183, if_statement; 183, 184; 183, 188; 184, boolean_operator:and; 184, 185; 184, 187; 185, not_operator; 185, 186; 186, identifier:data_loaded; 187, identifier:have_pil; 188, block; 188, 189; 188, 193; 188, 202; 188, 297; 188, 298; 188, 319; 188, 320; 188, 329; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:means; 192, string:'PIL'; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:image; 196, call; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:PILimage; 199, identifier:open; 200, argument_list; 200, 201; 201, identifier:filepath; 202, try_statement; 202, 203; 202, 276; 203, block; 203, 204; 204, if_statement; 204, 205; 204, 210; 204, 250; 204, 265; 205, call; 205, 206; 205, 207; 206, identifier:hasattr; 207, argument_list; 207, 208; 207, 209; 208, identifier:image; 209, string:'_getexif'; 210, block; 210, 211; 210, 219; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:info; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:image; 217, identifier:_getexif; 218, argument_list; 219, if_statement; 219, 220; 219, 223; 220, comparison_operator:is; 220, 221; 220, 222; 221, identifier:info; 222, None; 223, block; 223, 224; 224, for_statement; 224, 225; 224, 228; 224, 233; 225, pattern_list; 225, 226; 225, 227; 226, identifier:tag; 227, identifier:value; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:info; 231, identifier:items; 232, argument_list; 233, block; 233, 234; 233, 244; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 237; 236, identifier:kwd; 237, call; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:TAGS; 240, identifier:get; 241, argument_list; 241, 242; 241, 243; 242, identifier:tag; 243, identifier:tag; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 249; 246, subscript; 246, 247; 246, 248; 247, identifier:kwds; 248, identifier:kwd; 249, identifier:value; 250, elif_clause; 250, 251; 250, 252; 251, identifier:have_exif; 252, block; 252, 253; 253, expression_statement; 253, 254; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:self; 257, identifier:piexif_getexif; 258, argument_list; 258, 259; 258, 264; 259, subscript; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:image; 262, identifier:info; 263, string:"exif"; 264, identifier:kwds; 265, else_clause; 265, 266; 266, block; 266, 267; 267, expression_statement; 267, 268; 268, call; 268, 269; 268, 274; 269, attribute; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:self; 272, identifier:logger; 273, identifier:warning; 274, argument_list; 274, 275; 275, string:"Please install 'piexif' module to get image metadata"; 276, except_clause; 276, 277; 276, 281; 277, as_pattern; 277, 278; 277, 279; 278, identifier:Exception; 279, as_pattern_target; 279, 280; 280, identifier:e; 281, block; 281, 282; 282, expression_statement; 282, 283; 283, call; 283, 284; 283, 289; 284, attribute; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:self; 287, identifier:logger; 288, identifier:warning; 289, argument_list; 289, 290; 290, binary_operator:%; 290, 291; 290, 292; 291, string:"Failed to get image metadata: %s"; 292, parenthesized_expression; 292, 293; 293, call; 293, 294; 293, 295; 294, identifier:str; 295, argument_list; 295, 296; 296, identifier:e; 297, comment; 298, if_statement; 298, 299; 298, 306; 299, call; 299, 300; 299, 305; 300, attribute; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:self; 303, identifier:clr_mgr; 304, identifier:can_profile; 305, argument_list; 306, block; 306, 307; 307, expression_statement; 307, 308; 308, assignment; 308, 309; 308, 310; 309, identifier:image; 310, call; 310, 311; 310, 316; 311, attribute; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:self; 314, identifier:clr_mgr; 315, identifier:profile_to_working_pil; 316, argument_list; 316, 317; 316, 318; 317, identifier:image; 318, identifier:kwds; 319, comment; 320, expression_statement; 320, 321; 321, assignment; 321, 322; 321, 323; 322, identifier:data_np; 323, call; 323, 324; 323, 327; 324, attribute; 324, 325; 324, 326; 325, identifier:np; 326, identifier:array; 327, argument_list; 327, 328; 328, identifier:image; 329, if_statement; 329, 330; 329, 333; 330, comparison_operator:is; 330, 331; 330, 332; 331, identifier:data_np; 332, None; 333, block; 333, 334; 334, expression_statement; 334, 335; 335, assignment; 335, 336; 335, 337; 336, identifier:data_loaded; 337, True; 338, if_statement; 338, 339; 338, 354; 338, 355; 339, parenthesized_expression; 339, 340; 340, boolean_operator:and; 340, 341; 340, 348; 341, boolean_operator:and; 341, 342; 341, 344; 342, not_operator; 342, 343; 343, identifier:data_loaded; 344, parenthesized_expression; 344, 345; 345, comparison_operator:==; 345, 346; 345, 347; 346, identifier:typ; 347, string:'image'; 348, parenthesized_expression; 348, 349; 349, comparison_operator:in; 349, 350; 349, 351; 350, identifier:subtyp; 351, tuple; 351, 352; 351, 353; 352, string:'x-portable-pixmap'; 353, string:'x-portable-greymap'; 354, comment; 355, block; 355, 356; 355, 360; 355, 367; 356, expression_statement; 356, 357; 357, assignment; 357, 358; 357, 359; 358, identifier:means; 359, string:'built-in'; 360, expression_statement; 360, 361; 361, assignment; 361, 362; 361, 363; 362, identifier:data_np; 363, call; 363, 364; 363, 365; 364, identifier:open_ppm; 365, argument_list; 365, 366; 366, identifier:filepath; 367, if_statement; 367, 368; 367, 371; 368, comparison_operator:is; 368, 369; 368, 370; 369, identifier:data_np; 370, None; 371, block; 371, 372; 372, expression_statement; 372, 373; 373, assignment; 373, 374; 373, 375; 374, identifier:data_loaded; 375, True; 376, if_statement; 376, 377; 376, 379; 377, not_operator; 377, 378; 378, identifier:data_loaded; 379, block; 379, 380; 380, raise_statement; 380, 381; 381, call; 381, 382; 381, 383; 382, identifier:ImageError; 383, argument_list; 383, 384; 384, binary_operator:%; 384, 385; 384, 386; 385, string:"No way to load image format '%s/%s'"; 386, tuple; 386, 387; 386, 388; 387, identifier:typ; 388, identifier:subtyp; 389, expression_statement; 389, 390; 390, assignment; 390, 391; 390, 392; 391, identifier:end_time; 392, call; 392, 393; 392, 396; 393, attribute; 393, 394; 393, 395; 394, identifier:time; 395, identifier:time; 396, argument_list; 397, expression_statement; 397, 398; 398, call; 398, 399; 398, 404; 399, attribute; 399, 400; 399, 403; 400, attribute; 400, 401; 400, 402; 401, identifier:self; 402, identifier:logger; 403, identifier:debug; 404, argument_list; 404, 405; 405, binary_operator:%; 405, 406; 405, 407; 406, string:"loading (%s) time %.4f sec"; 407, tuple; 407, 408; 407, 409; 408, identifier:means; 409, binary_operator:-; 409, 410; 409, 411; 410, identifier:end_time; 411, identifier:start_time; 412, return_statement; 412, 413; 413, identifier:data_np
def _imload(self, filepath, kwds): """Load an image file, guessing the format, and return a numpy array containing an RGB image. If EXIF keywords can be read they are returned in the dict _kwds_. """ start_time = time.time() typ, enc = mimetypes.guess_type(filepath) if not typ: typ = 'image/jpeg' typ, subtyp = typ.split('/') self.logger.debug("MIME type is %s/%s" % (typ, subtyp)) data_loaded = False if have_opencv and subtyp not in ['gif']: # First choice is OpenCv, because it supports high-bit depth # multiband images means = 'opencv' data_np = cv2.imread(filepath, cv2.IMREAD_ANYDEPTH + cv2.IMREAD_ANYCOLOR) if data_np is not None: data_loaded = True # funky indexing because opencv returns BGR images, # whereas PIL and others return RGB if len(data_np.shape) >= 3 and data_np.shape[2] >= 3: data_np = data_np[..., :: -1] # OpenCv doesn't "do" image metadata, so we punt to piexif # library (if installed) self.piexif_getexif(filepath, kwds) # OpenCv added a feature to do auto-orientation when loading # (see https://github.com/opencv/opencv/issues/4344) # So reset these values to prevent auto-orientation from # happening later kwds['Orientation'] = 1 kwds['Image Orientation'] = 1 # convert to working color profile, if can if self.clr_mgr.can_profile(): data_np = self.clr_mgr.profile_to_working_numpy(data_np, kwds) if not data_loaded and have_pil: means = 'PIL' image = PILimage.open(filepath) try: if hasattr(image, '_getexif'): info = image._getexif() if info is not None: for tag, value in info.items(): kwd = TAGS.get(tag, tag) kwds[kwd] = value elif have_exif: self.piexif_getexif(image.info["exif"], kwds) else: self.logger.warning("Please install 'piexif' module to get image metadata") except Exception as e: self.logger.warning("Failed to get image metadata: %s" % (str(e))) # convert to working color profile, if can if self.clr_mgr.can_profile(): image = self.clr_mgr.profile_to_working_pil(image, kwds) # convert from PIL to numpy data_np = np.array(image) if data_np is not None: data_loaded = True if (not data_loaded and (typ == 'image') and (subtyp in ('x-portable-pixmap', 'x-portable-greymap'))): # Special opener for PPM files, preserves high bit depth means = 'built-in' data_np = open_ppm(filepath) if data_np is not None: data_loaded = True if not data_loaded: raise ImageError("No way to load image format '%s/%s'" % ( typ, subtyp)) end_time = time.time() self.logger.debug("loading (%s) time %.4f sec" % ( means, end_time - start_time)) return data_np
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_coord_system_name; 3, parameters; 3, 4; 4, identifier:header; 5, block; 5, 6; 5, 8; 5, 81; 5, 91; 5, 96; 5, 106; 5, 111; 5, 121; 5, 205; 5, 215; 5, 220; 5, 230; 5, 235; 5, 245; 5, 250; 5, 260; 5, 265; 5, 266; 6, expression_statement; 6, 7; 7, comment; 8, try_statement; 8, 9; 8, 24; 9, block; 9, 10; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:ctype; 13, call; 13, 14; 13, 23; 14, attribute; 14, 15; 14, 22; 15, call; 15, 16; 15, 21; 16, attribute; 16, 17; 16, 20; 17, subscript; 17, 18; 17, 19; 18, identifier:header; 19, string:'CTYPE1'; 20, identifier:strip; 21, argument_list; 22, identifier:upper; 23, argument_list; 24, except_clause; 24, 25; 24, 26; 25, identifier:KeyError; 26, block; 26, 27; 27, try_statement; 27, 28; 27, 29; 27, 76; 28, comment; 29, block; 29, 30; 29, 36; 29, 37; 29, 70; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:ra; 33, subscript; 33, 34; 33, 35; 34, identifier:header; 35, string:'RA'; 36, comment; 37, try_statement; 37, 38; 37, 63; 38, block; 38, 39; 38, 48; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:equinox; 42, call; 42, 43; 42, 44; 43, identifier:float; 44, argument_list; 44, 45; 45, subscript; 45, 46; 45, 47; 46, identifier:header; 47, string:'EQUINOX'; 48, if_statement; 48, 49; 48, 52; 48, 57; 49, comparison_operator:<; 49, 50; 49, 51; 50, identifier:equinox; 51, float:1984.0; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:radecsys; 56, string:'FK4'; 57, else_clause; 57, 58; 58, block; 58, 59; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:radecsys; 62, string:'FK5'; 63, except_clause; 63, 64; 63, 65; 64, identifier:KeyError; 65, block; 65, 66; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:radecsys; 69, string:'ICRS'; 70, return_statement; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:radecsys; 74, identifier:lower; 75, argument_list; 76, except_clause; 76, 77; 76, 78; 77, identifier:KeyError; 78, block; 78, 79; 79, return_statement; 79, 80; 80, string:'raw'; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:match; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:re; 87, identifier:match; 88, argument_list; 88, 89; 88, 90; 89, string:r'^GLON\-.*$'; 90, identifier:ctype; 91, if_statement; 91, 92; 91, 93; 92, identifier:match; 93, block; 93, 94; 94, return_statement; 94, 95; 95, string:'galactic'; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:match; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:re; 102, identifier:match; 103, argument_list; 103, 104; 103, 105; 104, string:r'^ELON\-.*$'; 105, identifier:ctype; 106, if_statement; 106, 107; 106, 108; 107, identifier:match; 108, block; 108, 109; 109, return_statement; 109, 110; 110, string:'ecliptic'; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:match; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:re; 117, identifier:match; 118, argument_list; 118, 119; 118, 120; 119, string:r'^RA\-\-\-.*$'; 120, identifier:ctype; 121, if_statement; 121, 122; 121, 123; 122, identifier:match; 123, block; 123, 124; 123, 128; 123, 191; 123, 199; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:hdkey; 127, string:'RADECSYS'; 128, try_statement; 128, 129; 128, 136; 129, block; 129, 130; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:radecsys; 133, subscript; 133, 134; 133, 135; 134, identifier:header; 135, identifier:hdkey; 136, except_clause; 136, 137; 136, 138; 137, identifier:KeyError; 138, block; 138, 139; 139, try_statement; 139, 140; 139, 151; 140, block; 140, 141; 140, 145; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:hdkey; 144, string:'RADESYS'; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:radecsys; 148, subscript; 148, 149; 148, 150; 149, identifier:header; 150, identifier:hdkey; 151, except_clause; 151, 152; 151, 153; 151, 154; 151, 155; 151, 156; 151, 157; 152, identifier:KeyError; 153, comment; 154, comment; 155, comment; 156, comment; 157, block; 157, 158; 158, try_statement; 158, 159; 158, 184; 159, block; 159, 160; 159, 169; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:equinox; 163, call; 163, 164; 163, 165; 164, identifier:float; 165, argument_list; 165, 166; 166, subscript; 166, 167; 166, 168; 167, identifier:header; 168, string:'EQUINOX'; 169, if_statement; 169, 170; 169, 173; 169, 178; 170, comparison_operator:<; 170, 171; 170, 172; 171, identifier:equinox; 172, float:1984.0; 173, block; 173, 174; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:radecsys; 177, string:'FK4'; 178, else_clause; 178, 179; 179, block; 179, 180; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:radecsys; 183, string:'FK5'; 184, except_clause; 184, 185; 184, 186; 185, identifier:KeyError; 186, block; 186, 187; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:radecsys; 190, string:'ICRS'; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 194; 193, identifier:radecsys; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:radecsys; 197, identifier:strip; 198, argument_list; 199, return_statement; 199, 200; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:radecsys; 203, identifier:lower; 204, argument_list; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:match; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:re; 211, identifier:match; 212, argument_list; 212, 213; 212, 214; 213, string:r'^HPLN\-.*$'; 214, identifier:ctype; 215, if_statement; 215, 216; 215, 217; 216, identifier:match; 217, block; 217, 218; 218, return_statement; 218, 219; 219, string:'helioprojective'; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 223; 222, identifier:match; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:re; 226, identifier:match; 227, argument_list; 227, 228; 227, 229; 228, string:r'^HGLT\-.*$'; 229, identifier:ctype; 230, if_statement; 230, 231; 230, 232; 231, identifier:match; 232, block; 232, 233; 233, return_statement; 233, 234; 234, string:'heliographicstonyhurst'; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 238; 237, identifier:match; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:re; 241, identifier:match; 242, argument_list; 242, 243; 242, 244; 243, string:r'^PIXEL$'; 244, identifier:ctype; 245, if_statement; 245, 246; 245, 247; 246, identifier:match; 247, block; 247, 248; 248, return_statement; 248, 249; 249, string:'pixel'; 250, expression_statement; 250, 251; 251, assignment; 251, 252; 251, 253; 252, identifier:match; 253, call; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:re; 256, identifier:match; 257, argument_list; 257, 258; 257, 259; 258, string:r'^LINEAR$'; 259, identifier:ctype; 260, if_statement; 260, 261; 260, 262; 261, identifier:match; 262, block; 262, 263; 263, return_statement; 263, 264; 264, string:'pixel'; 265, comment; 266, return_statement; 266, 267; 267, string:'icrs'
def get_coord_system_name(header): """Return an appropriate key code for the axes coordinate system by examining the FITS header. """ try: ctype = header['CTYPE1'].strip().upper() except KeyError: try: # see if we have an "RA" header ra = header['RA'] # noqa try: equinox = float(header['EQUINOX']) if equinox < 1984.0: radecsys = 'FK4' else: radecsys = 'FK5' except KeyError: radecsys = 'ICRS' return radecsys.lower() except KeyError: return 'raw' match = re.match(r'^GLON\-.*$', ctype) if match: return 'galactic' match = re.match(r'^ELON\-.*$', ctype) if match: return 'ecliptic' match = re.match(r'^RA\-\-\-.*$', ctype) if match: hdkey = 'RADECSYS' try: radecsys = header[hdkey] except KeyError: try: hdkey = 'RADESYS' radecsys = header[hdkey] except KeyError: # missing keyword # RADESYS defaults to IRCS unless EQUINOX is given # alone, in which case it defaults to FK4 prior to 1984 # and FK5 after 1984. try: equinox = float(header['EQUINOX']) if equinox < 1984.0: radecsys = 'FK4' else: radecsys = 'FK5' except KeyError: radecsys = 'ICRS' radecsys = radecsys.strip() return radecsys.lower() match = re.match(r'^HPLN\-.*$', ctype) if match: return 'helioprojective' match = re.match(r'^HGLT\-.*$', ctype) if match: return 'heliographicstonyhurst' match = re.match(r'^PIXEL$', ctype) if match: return 'pixel' match = re.match(r'^LINEAR$', ctype) if match: return 'pixel' #raise WCSError("Cannot determine appropriate coordinate system from FITS header") # noqa return 'icrs'
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_do_info; 3, parameters; 3, 4; 3, 5; 4, identifier:bz; 5, identifier:opt; 6, block; 6, 7; 6, 9; 6, 10; 6, 11; 6, 47; 6, 62; 6, 68; 6, 80; 6, 90; 6, 101; 6, 123; 6, 140; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, comment; 11, function_definition; 11, 12; 11, 13; 11, 15; 12, function_name:_filter_components; 13, parameters; 13, 14; 14, identifier:compdetails; 15, block; 15, 16; 15, 20; 15, 45; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:ret; 19, dictionary; 20, for_statement; 20, 21; 20, 24; 20, 29; 21, pattern_list; 21, 22; 21, 23; 22, identifier:k; 23, identifier:v; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:compdetails; 27, identifier:items; 28, argument_list; 29, block; 29, 30; 30, if_statement; 30, 31; 30, 38; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:v; 34, identifier:get; 35, argument_list; 35, 36; 35, 37; 36, string:"is_active"; 37, True; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 44; 41, subscript; 41, 42; 41, 43; 42, identifier:ret; 43, identifier:k; 44, identifier:v; 45, return_statement; 45, 46; 46, identifier:ret; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:productname; 50, parenthesized_expression; 50, 51; 51, boolean_operator:or; 51, 52; 51, 59; 52, boolean_operator:or; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:opt; 55, identifier:components; 56, attribute; 56, 57; 56, 58; 57, identifier:opt; 58, identifier:component_owners; 59, attribute; 59, 60; 59, 61; 60, identifier:opt; 61, identifier:versions; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:include_fields; 65, list:["name", "id"]; 65, 66; 65, 67; 66, string:"name"; 67, string:"id"; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:fastcomponents; 71, parenthesized_expression; 71, 72; 72, boolean_operator:and; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:opt; 75, identifier:components; 76, not_operator; 76, 77; 77, attribute; 77, 78; 77, 79; 78, identifier:opt; 79, identifier:active_components; 80, if_statement; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:opt; 83, identifier:versions; 84, block; 84, 85; 85, expression_statement; 85, 86; 86, augmented_assignment:+=; 86, 87; 86, 88; 87, identifier:include_fields; 88, list:["versions"]; 88, 89; 89, string:"versions"; 90, if_statement; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:opt; 93, identifier:component_owners; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, augmented_assignment:+=; 96, 97; 96, 98; 97, identifier:include_fields; 98, list:[ "components.default_assigned_to", "components.name", ]; 98, 99; 98, 100; 99, string:"components.default_assigned_to"; 100, string:"components.name"; 101, if_statement; 101, 102; 101, 117; 102, parenthesized_expression; 102, 103; 103, boolean_operator:and; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:opt; 106, identifier:active_components; 107, call; 107, 108; 107, 109; 108, identifier:any; 109, argument_list; 109, 110; 110, list_comprehension; 110, 111; 110, 114; 111, comparison_operator:in; 111, 112; 111, 113; 112, string:"components"; 113, identifier:i; 114, for_in_clause; 114, 115; 114, 116; 115, identifier:i; 116, identifier:include_fields; 117, block; 117, 118; 118, expression_statement; 118, 119; 119, augmented_assignment:+=; 119, 120; 119, 121; 120, identifier:include_fields; 121, list:["components.is_active"]; 121, 122; 122, string:"components.is_active"; 123, expression_statement; 123, 124; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:bz; 127, identifier:refresh_products; 128, argument_list; 128, 129; 128, 137; 129, keyword_argument; 129, 130; 129, 131; 130, identifier:names; 131, boolean_operator:or; 131, 132; 131, 136; 132, boolean_operator:and; 132, 133; 132, 134; 133, identifier:productname; 134, list:[productname]; 134, 135; 135, identifier:productname; 136, None; 137, keyword_argument; 137, 138; 137, 139; 138, identifier:include_fields; 139, identifier:include_fields; 140, if_statement; 140, 141; 140, 144; 140, 167; 140, 187; 140, 216; 140, 247; 141, attribute; 141, 142; 141, 143; 142, identifier:opt; 143, identifier:products; 144, block; 144, 145; 145, for_statement; 145, 146; 145, 147; 145, 161; 146, identifier:name; 147, call; 147, 148; 147, 149; 148, identifier:sorted; 149, argument_list; 149, 150; 150, list_comprehension; 150, 151; 150, 154; 151, subscript; 151, 152; 151, 153; 152, identifier:p; 153, string:"name"; 154, for_in_clause; 154, 155; 154, 156; 155, identifier:p; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:bz; 159, identifier:getproducts; 160, argument_list; 161, block; 161, 162; 162, expression_statement; 162, 163; 163, call; 163, 164; 163, 165; 164, identifier:print; 165, argument_list; 165, 166; 166, identifier:name; 167, elif_clause; 167, 168; 167, 169; 168, identifier:fastcomponents; 169, block; 169, 170; 170, for_statement; 170, 171; 170, 172; 170, 181; 171, identifier:name; 172, call; 172, 173; 172, 174; 173, identifier:sorted; 174, argument_list; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:bz; 178, identifier:getcomponents; 179, argument_list; 179, 180; 180, identifier:productname; 181, block; 181, 182; 182, expression_statement; 182, 183; 183, call; 183, 184; 183, 185; 184, identifier:print; 185, argument_list; 185, 186; 186, identifier:name; 187, elif_clause; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:opt; 190, identifier:components; 191, block; 191, 192; 191, 201; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:details; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:bz; 198, identifier:getcomponentsdetails; 199, argument_list; 199, 200; 200, identifier:productname; 201, for_statement; 201, 202; 201, 203; 201, 210; 202, identifier:name; 203, call; 203, 204; 203, 205; 204, identifier:sorted; 205, argument_list; 205, 206; 206, call; 206, 207; 206, 208; 207, identifier:_filter_components; 208, argument_list; 208, 209; 209, identifier:details; 210, block; 210, 211; 211, expression_statement; 211, 212; 212, call; 212, 213; 212, 214; 213, identifier:print; 214, argument_list; 214, 215; 215, identifier:name; 216, elif_clause; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:opt; 219, identifier:versions; 220, block; 220, 221; 220, 231; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:proddict; 224, subscript; 224, 225; 224, 230; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:bz; 228, identifier:getproducts; 229, argument_list; 230, integer:0; 231, for_statement; 231, 232; 231, 233; 231, 236; 232, identifier:v; 233, subscript; 233, 234; 233, 235; 234, identifier:proddict; 235, string:'versions'; 236, block; 236, 237; 237, expression_statement; 237, 238; 238, call; 238, 239; 238, 240; 239, identifier:print; 240, argument_list; 240, 241; 241, call; 241, 242; 241, 243; 242, identifier:to_encoding; 243, argument_list; 243, 244; 244, subscript; 244, 245; 244, 246; 245, identifier:v; 246, string:"name"; 247, elif_clause; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:opt; 250, identifier:component_owners; 251, block; 251, 252; 251, 261; 252, expression_statement; 252, 253; 253, assignment; 253, 254; 253, 255; 254, identifier:details; 255, call; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:bz; 258, identifier:getcomponentsdetails; 259, argument_list; 259, 260; 260, identifier:productname; 261, for_statement; 261, 262; 261, 263; 261, 270; 262, identifier:c; 263, call; 263, 264; 263, 265; 264, identifier:sorted; 265, argument_list; 265, 266; 266, call; 266, 267; 266, 268; 267, identifier:_filter_components; 268, argument_list; 268, 269; 269, identifier:details; 270, block; 270, 271; 271, expression_statement; 271, 272; 272, call; 272, 273; 272, 274; 273, identifier:print; 274, argument_list; 274, 275; 275, call; 275, 276; 275, 277; 276, identifier:to_encoding; 277, argument_list; 277, 278; 278, binary_operator:%; 278, 279; 278, 280; 279, string:u"%s: %s"; 280, tuple; 280, 281; 280, 282; 281, identifier:c; 282, subscript; 282, 283; 282, 286; 283, subscript; 283, 284; 283, 285; 284, identifier:details; 285, identifier:c; 286, string:'default_assigned_to'
def _do_info(bz, opt): """ Handle the 'info' subcommand """ # All these commands call getproducts internally, so do it up front # with minimal include_fields for speed def _filter_components(compdetails): ret = {} for k, v in compdetails.items(): if v.get("is_active", True): ret[k] = v return ret productname = (opt.components or opt.component_owners or opt.versions) include_fields = ["name", "id"] fastcomponents = (opt.components and not opt.active_components) if opt.versions: include_fields += ["versions"] if opt.component_owners: include_fields += [ "components.default_assigned_to", "components.name", ] if (opt.active_components and any(["components" in i for i in include_fields])): include_fields += ["components.is_active"] bz.refresh_products(names=productname and [productname] or None, include_fields=include_fields) if opt.products: for name in sorted([p["name"] for p in bz.getproducts()]): print(name) elif fastcomponents: for name in sorted(bz.getcomponents(productname)): print(name) elif opt.components: details = bz.getcomponentsdetails(productname) for name in sorted(_filter_components(details)): print(name) elif opt.versions: proddict = bz.getproducts()[0] for v in proddict['versions']: print(to_encoding(v["name"])) elif opt.component_owners: details = bz.getcomponentsdetails(productname) for c in sorted(_filter_components(details)): print(to_encoding(u"%s: %s" % (c, details[c]['default_assigned_to'])))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:login; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:user; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:password; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:restrict_login; 13, None; 14, block; 14, 15; 14, 17; 14, 27; 14, 36; 14, 45; 14, 56; 14, 67; 14, 77; 15, expression_statement; 15, 16; 16, comment; 17, if_statement; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:api_key; 21, block; 21, 22; 22, raise_statement; 22, 23; 23, call; 23, 24; 23, 25; 24, identifier:ValueError; 25, argument_list; 25, 26; 26, string:"cannot login when using an API key"; 27, if_statement; 27, 28; 27, 29; 28, identifier:user; 29, block; 29, 30; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:user; 35, identifier:user; 36, if_statement; 36, 37; 36, 38; 37, identifier:password; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:self; 43, identifier:password; 44, identifier:password; 45, if_statement; 45, 46; 45, 50; 46, not_operator; 46, 47; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:user; 50, block; 50, 51; 51, raise_statement; 51, 52; 52, call; 52, 53; 52, 54; 53, identifier:ValueError; 54, argument_list; 54, 55; 55, string:"missing username"; 56, if_statement; 56, 57; 56, 61; 57, not_operator; 57, 58; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:password; 61, block; 61, 62; 62, raise_statement; 62, 63; 63, call; 63, 64; 63, 65; 64, identifier:ValueError; 65, argument_list; 65, 66; 66, string:"missing password"; 67, if_statement; 67, 68; 67, 69; 68, identifier:restrict_login; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:log; 74, identifier:info; 75, argument_list; 75, 76; 76, string:"logging in with restrict_login=True"; 77, try_statement; 77, 78; 77, 112; 78, block; 78, 79; 78, 94; 78, 100; 78, 110; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:ret; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:self; 85, identifier:_login; 86, argument_list; 86, 87; 86, 90; 86, 93; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:user; 90, attribute; 90, 91; 90, 92; 91, identifier:self; 92, identifier:password; 93, identifier:restrict_login; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:password; 99, string:''; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:log; 104, identifier:info; 105, argument_list; 105, 106; 105, 107; 106, string:"login successful for user=%s"; 107, attribute; 107, 108; 107, 109; 108, identifier:self; 109, identifier:user; 110, return_statement; 110, 111; 111, identifier:ret; 112, except_clause; 112, 113; 112, 117; 113, as_pattern; 113, 114; 113, 115; 114, identifier:Fault; 115, as_pattern_target; 115, 116; 116, identifier:e; 117, block; 117, 118; 118, raise_statement; 118, 119; 119, call; 119, 120; 119, 121; 120, identifier:BugzillaError; 121, argument_list; 121, 122; 122, binary_operator:%; 122, 123; 122, 124; 123, string:"Login failed: %s"; 124, call; 124, 125; 124, 126; 125, identifier:str; 126, argument_list; 126, 127; 127, attribute; 127, 128; 127, 129; 128, identifier:e; 129, identifier:faultString
def login(self, user=None, password=None, restrict_login=None): """ Attempt to log in using the given username and password. Subsequent method calls will use this username and password. Returns False if login fails, otherwise returns some kind of login info - typically either a numeric userid, or a dict of user info. If user is not set, the value of Bugzilla.user will be used. If *that* is not set, ValueError will be raised. If login fails, BugzillaError will be raised. The login session can be restricted to current user IP address with restrict_login argument. (Bugzilla 4.4+) This method will be called implicitly at the end of connect() if user and password are both set. So under most circumstances you won't need to call this yourself. """ if self.api_key: raise ValueError("cannot login when using an API key") if user: self.user = user if password: self.password = password if not self.user: raise ValueError("missing username") if not self.password: raise ValueError("missing password") if restrict_login: log.info("logging in with restrict_login=True") try: ret = self._login(self.user, self.password, restrict_login) self.password = '' log.info("login successful for user=%s", self.user) return ret except Fault as e: raise BugzillaError("Login failed: %s" % str(e.faultString))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_process_include_fields; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:include_fields; 6, identifier:exclude_fields; 7, identifier:extra_fields; 8, block; 8, 9; 8, 11; 8, 58; 8, 62; 8, 115; 8, 132; 9, expression_statement; 9, 10; 10, comment; 11, function_definition; 11, 12; 11, 13; 11, 15; 12, function_name:_convert_fields; 13, parameters; 13, 14; 14, identifier:_in; 15, block; 15, 16; 15, 22; 15, 56; 16, if_statement; 16, 17; 16, 19; 17, not_operator; 17, 18; 18, identifier:_in; 19, block; 19, 20; 20, return_statement; 20, 21; 21, identifier:_in; 22, for_statement; 22, 23; 22, 26; 22, 31; 23, pattern_list; 23, 24; 23, 25; 24, identifier:newname; 25, identifier:oldname; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:_get_api_aliases; 30, argument_list; 31, block; 31, 32; 32, if_statement; 32, 33; 32, 36; 33, comparison_operator:in; 33, 34; 33, 35; 34, identifier:oldname; 35, identifier:_in; 36, block; 36, 37; 36, 44; 37, expression_statement; 37, 38; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:_in; 41, identifier:remove; 42, argument_list; 42, 43; 43, identifier:oldname; 44, if_statement; 44, 45; 44, 48; 45, comparison_operator:not; 45, 46; 45, 47; 46, identifier:newname; 47, identifier:_in; 48, block; 48, 49; 49, expression_statement; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:_in; 53, identifier:append; 54, argument_list; 54, 55; 55, identifier:newname; 56, return_statement; 56, 57; 57, identifier:_in; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:ret; 61, dictionary; 62, if_statement; 62, 63; 62, 70; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:self; 66, identifier:_check_version; 67, argument_list; 67, 68; 67, 69; 68, integer:4; 69, integer:0; 70, block; 70, 71; 70, 99; 71, if_statement; 71, 72; 71, 73; 72, identifier:include_fields; 73, block; 73, 74; 73, 81; 73, 93; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:include_fields; 77, call; 77, 78; 77, 79; 78, identifier:_convert_fields; 79, argument_list; 79, 80; 80, identifier:include_fields; 81, if_statement; 81, 82; 81, 85; 82, comparison_operator:not; 82, 83; 82, 84; 83, string:"id"; 84, identifier:include_fields; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:include_fields; 90, identifier:append; 91, argument_list; 91, 92; 92, string:"id"; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 98; 95, subscript; 95, 96; 95, 97; 96, identifier:ret; 97, string:"include_fields"; 98, identifier:include_fields; 99, if_statement; 99, 100; 99, 101; 100, identifier:exclude_fields; 101, block; 101, 102; 101, 109; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:exclude_fields; 105, call; 105, 106; 105, 107; 106, identifier:_convert_fields; 107, argument_list; 107, 108; 108, identifier:exclude_fields; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 114; 111, subscript; 111, 112; 111, 113; 112, identifier:ret; 113, string:"exclude_fields"; 114, identifier:exclude_fields; 115, if_statement; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:self; 118, identifier:_supports_getbug_extra_fields; 119, block; 119, 120; 120, if_statement; 120, 121; 120, 122; 121, identifier:extra_fields; 122, block; 122, 123; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 128; 125, subscript; 125, 126; 125, 127; 126, identifier:ret; 127, string:"extra_fields"; 128, call; 128, 129; 128, 130; 129, identifier:_convert_fields; 130, argument_list; 130, 131; 131, identifier:extra_fields; 132, return_statement; 132, 133; 133, identifier:ret
def _process_include_fields(self, include_fields, exclude_fields, extra_fields): """ Internal helper to process include_fields lists """ def _convert_fields(_in): if not _in: return _in for newname, oldname in self._get_api_aliases(): if oldname in _in: _in.remove(oldname) if newname not in _in: _in.append(newname) return _in ret = {} if self._check_version(4, 0): if include_fields: include_fields = _convert_fields(include_fields) if "id" not in include_fields: include_fields.append("id") ret["include_fields"] = include_fields if exclude_fields: exclude_fields = _convert_fields(exclude_fields) ret["exclude_fields"] = exclude_fields if self._supports_getbug_extra_fields: if extra_fields: ret["extra_fields"] = _convert_fields(extra_fields) return ret
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:_getbugs; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 4, identifier:self; 5, identifier:idlist; 6, identifier:permissive; 7, default_parameter; 7, 8; 7, 9; 8, identifier:include_fields; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:exclude_fields; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:extra_fields; 15, None; 16, block; 16, 17; 16, 19; 16, 23; 16, 27; 16, 54; 16, 65; 16, 71; 16, 78; 16, 87; 16, 101; 16, 114; 16, 161; 16, 165; 16, 223; 17, expression_statement; 17, 18; 18, comment; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:oldidlist; 22, identifier:idlist; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:idlist; 26, list:[]; 27, for_statement; 27, 28; 27, 29; 27, 30; 28, identifier:i; 29, identifier:oldidlist; 30, block; 30, 31; 31, try_statement; 31, 32; 31, 43; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:idlist; 37, identifier:append; 38, argument_list; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:int; 41, argument_list; 41, 42; 42, identifier:i; 43, except_clause; 43, 44; 43, 45; 43, 46; 44, identifier:ValueError; 45, comment; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:idlist; 51, identifier:append; 52, argument_list; 52, 53; 53, identifier:i; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:extra_fields; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:_listify; 61, argument_list; 61, 62; 62, boolean_operator:or; 62, 63; 62, 64; 63, identifier:extra_fields; 64, list:[]; 65, expression_statement; 65, 66; 66, augmented_assignment:+=; 66, 67; 66, 68; 67, identifier:extra_fields; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:_getbug_extra_fields; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:getbugdata; 74, dictionary; 74, 75; 75, pair; 75, 76; 75, 77; 76, string:"ids"; 77, identifier:idlist; 78, if_statement; 78, 79; 78, 80; 79, identifier:permissive; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 86; 83, subscript; 83, 84; 83, 85; 84, identifier:getbugdata; 85, string:"permissive"; 86, integer:1; 87, expression_statement; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:getbugdata; 91, identifier:update; 92, argument_list; 92, 93; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:self; 96, identifier:_process_include_fields; 97, argument_list; 97, 98; 97, 99; 97, 100; 98, identifier:include_fields; 99, identifier:exclude_fields; 100, identifier:extra_fields; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:r; 104, call; 104, 105; 104, 112; 105, attribute; 105, 106; 105, 111; 106, attribute; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:self; 109, identifier:_proxy; 110, identifier:Bug; 111, identifier:get; 112, argument_list; 112, 113; 113, identifier:getbugdata; 114, if_statement; 114, 115; 114, 122; 114, 140; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:self; 118, identifier:_check_version; 119, argument_list; 119, 120; 119, 121; 120, integer:4; 121, integer:0; 122, block; 122, 123; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:bugdict; 126, call; 126, 127; 126, 128; 127, identifier:dict; 128, argument_list; 128, 129; 129, list_comprehension; 129, 130; 129, 135; 130, tuple; 130, 131; 130, 134; 131, subscript; 131, 132; 131, 133; 132, identifier:b; 133, string:'id'; 134, identifier:b; 135, for_in_clause; 135, 136; 135, 137; 136, identifier:b; 137, subscript; 137, 138; 137, 139; 138, identifier:r; 139, string:'bugs'; 140, else_clause; 140, 141; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:bugdict; 145, call; 145, 146; 145, 147; 146, identifier:dict; 147, argument_list; 147, 148; 148, list_comprehension; 148, 149; 148, 156; 149, tuple; 149, 150; 149, 153; 150, subscript; 150, 151; 150, 152; 151, identifier:b; 152, string:'id'; 153, subscript; 153, 154; 153, 155; 154, identifier:b; 155, string:'internals'; 156, for_in_clause; 156, 157; 156, 158; 157, identifier:b; 158, subscript; 158, 159; 158, 160; 159, identifier:r; 160, string:'bugs'; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:ret; 164, list:[]; 165, for_statement; 165, 166; 165, 167; 165, 168; 166, identifier:i; 167, identifier:idlist; 168, block; 168, 169; 168, 173; 168, 216; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:found; 172, None; 173, if_statement; 173, 174; 173, 177; 173, 184; 174, comparison_operator:in; 174, 175; 174, 176; 175, identifier:i; 176, identifier:bugdict; 177, block; 177, 178; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:found; 181, subscript; 181, 182; 181, 183; 182, identifier:bugdict; 183, identifier:i; 184, else_clause; 184, 185; 184, 186; 185, comment; 186, block; 186, 187; 187, for_statement; 187, 188; 187, 189; 187, 194; 188, identifier:valdict; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:bugdict; 192, identifier:values; 193, argument_list; 194, block; 194, 195; 195, if_statement; 195, 196; 195, 210; 196, comparison_operator:in; 196, 197; 196, 198; 197, identifier:i; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:self; 201, identifier:_listify; 202, argument_list; 202, 203; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:valdict; 206, identifier:get; 207, argument_list; 207, 208; 207, 209; 208, string:"alias"; 209, None; 210, block; 210, 211; 210, 215; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:found; 214, identifier:valdict; 215, break_statement; 216, expression_statement; 216, 217; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:ret; 220, identifier:append; 221, argument_list; 221, 222; 222, identifier:found; 223, return_statement; 223, 224; 224, identifier:ret
def _getbugs(self, idlist, permissive, include_fields=None, exclude_fields=None, extra_fields=None): """ Return a list of dicts of full bug info for each given bug id. bug ids that couldn't be found will return None instead of a dict. """ oldidlist = idlist idlist = [] for i in oldidlist: try: idlist.append(int(i)) except ValueError: # String aliases can be passed as well idlist.append(i) extra_fields = self._listify(extra_fields or []) extra_fields += self._getbug_extra_fields getbugdata = {"ids": idlist} if permissive: getbugdata["permissive"] = 1 getbugdata.update(self._process_include_fields( include_fields, exclude_fields, extra_fields)) r = self._proxy.Bug.get(getbugdata) if self._check_version(4, 0): bugdict = dict([(b['id'], b) for b in r['bugs']]) else: bugdict = dict([(b['id'], b['internals']) for b in r['bugs']]) ret = [] for i in idlist: found = None if i in bugdict: found = bugdict[i] else: # Need to map an alias for valdict in bugdict.values(): if i in self._listify(valdict.get("alias", None)): found = valdict break ret.append(found) return ret
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:attachfile; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:self; 5, identifier:idlist; 6, identifier:attachfile; 7, identifier:description; 8, dictionary_splat_pattern; 8, 9; 9, identifier:kwargs; 10, block; 10, 11; 10, 13; 10, 46; 10, 47; 10, 63; 10, 79; 10, 95; 10, 111; 10, 117; 10, 125; 10, 146; 10, 155; 10, 166; 10, 192; 10, 230; 10, 243; 10, 278; 10, 298; 11, expression_statement; 11, 12; 12, comment; 13, if_statement; 13, 14; 13, 19; 13, 28; 13, 39; 14, call; 14, 15; 14, 16; 15, identifier:isinstance; 16, argument_list; 16, 17; 16, 18; 17, identifier:attachfile; 18, identifier:str; 19, block; 19, 20; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:f; 23, call; 23, 24; 23, 25; 24, identifier:open; 25, argument_list; 25, 26; 25, 27; 26, identifier:attachfile; 27, string:"rb"; 28, elif_clause; 28, 29; 28, 34; 29, call; 29, 30; 29, 31; 30, identifier:hasattr; 31, argument_list; 31, 32; 31, 33; 32, identifier:attachfile; 33, string:'read'; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:f; 38, identifier:attachfile; 39, else_clause; 39, 40; 40, block; 40, 41; 41, raise_statement; 41, 42; 42, call; 42, 43; 42, 44; 43, identifier:TypeError; 44, argument_list; 44, 45; 45, string:"attachfile must be filename or file-like object"; 46, comment; 47, if_statement; 47, 48; 47, 51; 48, comparison_operator:in; 48, 49; 48, 50; 49, string:"contenttype"; 50, identifier:kwargs; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 57; 54, subscript; 54, 55; 54, 56; 55, identifier:kwargs; 56, string:"content_type"; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:kwargs; 60, identifier:pop; 61, argument_list; 61, 62; 62, string:"contenttype"; 63, if_statement; 63, 64; 63, 67; 64, comparison_operator:in; 64, 65; 64, 66; 65, string:"ispatch"; 66, identifier:kwargs; 67, block; 67, 68; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 73; 70, subscript; 70, 71; 70, 72; 71, identifier:kwargs; 72, string:"is_patch"; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:kwargs; 76, identifier:pop; 77, argument_list; 77, 78; 78, string:"ispatch"; 79, if_statement; 79, 80; 79, 83; 80, comparison_operator:in; 80, 81; 80, 82; 81, string:"isprivate"; 82, identifier:kwargs; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 89; 86, subscript; 86, 87; 86, 88; 87, identifier:kwargs; 88, string:"is_private"; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:kwargs; 92, identifier:pop; 93, argument_list; 93, 94; 94, string:"isprivate"; 95, if_statement; 95, 96; 95, 99; 96, comparison_operator:in; 96, 97; 96, 98; 97, string:"filename"; 98, identifier:kwargs; 99, block; 99, 100; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 105; 102, subscript; 102, 103; 102, 104; 103, identifier:kwargs; 104, string:"file_name"; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:kwargs; 108, identifier:pop; 109, argument_list; 109, 110; 110, string:"filename"; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 116; 113, subscript; 113, 114; 113, 115; 114, identifier:kwargs; 115, string:'summary'; 116, identifier:description; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:data; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:f; 123, identifier:read; 124, argument_list; 125, if_statement; 125, 126; 125, 132; 126, not_operator; 126, 127; 127, call; 127, 128; 127, 129; 128, identifier:isinstance; 129, argument_list; 129, 130; 129, 131; 130, identifier:data; 131, identifier:bytes; 132, block; 132, 133; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:data; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:data; 139, identifier:encode; 140, argument_list; 140, 141; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:locale; 144, identifier:getpreferredencoding; 145, argument_list; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 151; 148, subscript; 148, 149; 148, 150; 149, identifier:kwargs; 150, string:'data'; 151, call; 151, 152; 151, 153; 152, identifier:Binary; 153, argument_list; 153, 154; 154, identifier:data; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 160; 157, subscript; 157, 158; 157, 159; 158, identifier:kwargs; 159, string:'ids'; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:self; 163, identifier:_listify; 164, argument_list; 164, 165; 165, identifier:idlist; 166, if_statement; 166, 167; 166, 176; 167, boolean_operator:and; 167, 168; 167, 171; 168, comparison_operator:not; 168, 169; 168, 170; 169, string:'file_name'; 170, identifier:kwargs; 171, call; 171, 172; 171, 173; 172, identifier:hasattr; 173, argument_list; 173, 174; 173, 175; 174, identifier:f; 175, string:"name"; 176, block; 176, 177; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 182; 179, subscript; 179, 180; 179, 181; 180, identifier:kwargs; 181, string:'file_name'; 182, call; 182, 183; 182, 188; 183, attribute; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:os; 186, identifier:path; 187, identifier:basename; 188, argument_list; 188, 189; 189, attribute; 189, 190; 189, 191; 190, identifier:f; 191, identifier:name; 192, if_statement; 192, 193; 192, 196; 193, comparison_operator:not; 193, 194; 193, 195; 194, string:'content_type'; 195, identifier:kwargs; 196, block; 196, 197; 196, 201; 196, 222; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:ctype; 200, None; 201, if_statement; 201, 202; 201, 205; 202, subscript; 202, 203; 202, 204; 203, identifier:kwargs; 204, string:'file_name'; 205, block; 205, 206; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:ctype; 209, subscript; 209, 210; 209, 221; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:mimetypes; 213, identifier:guess_type; 214, argument_list; 214, 215; 214, 218; 215, subscript; 215, 216; 215, 217; 216, identifier:kwargs; 217, string:'file_name'; 218, keyword_argument; 218, 219; 218, 220; 219, identifier:strict; 220, False; 221, integer:0; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 227; 224, subscript; 224, 225; 224, 226; 225, identifier:kwargs; 226, string:'content_type'; 227, boolean_operator:or; 227, 228; 227, 229; 228, identifier:ctype; 229, string:'application/octet-stream'; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:ret; 233, call; 233, 234; 233, 241; 234, attribute; 234, 235; 234, 240; 235, attribute; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:self; 238, identifier:_proxy; 239, identifier:Bug; 240, identifier:add_attachment; 241, argument_list; 241, 242; 242, identifier:kwargs; 243, if_statement; 243, 244; 243, 247; 243, 248; 243, 266; 244, comparison_operator:in; 244, 245; 244, 246; 245, string:"attachments"; 246, identifier:ret; 247, comment; 248, block; 248, 249; 249, expression_statement; 249, 250; 250, assignment; 250, 251; 250, 252; 251, identifier:ret; 252, list_comprehension; 252, 253; 252, 257; 253, call; 253, 254; 253, 255; 254, identifier:int; 255, argument_list; 255, 256; 256, identifier:k; 257, for_in_clause; 257, 258; 257, 259; 258, identifier:k; 259, call; 259, 260; 259, 265; 260, attribute; 260, 261; 260, 264; 261, subscript; 261, 262; 261, 263; 262, identifier:ret; 263, string:"attachments"; 264, identifier:keys; 265, argument_list; 266, elif_clause; 266, 267; 266, 270; 266, 271; 267, comparison_operator:in; 267, 268; 267, 269; 268, string:"ids"; 269, identifier:ret; 270, comment; 271, block; 271, 272; 272, expression_statement; 272, 273; 273, assignment; 273, 274; 273, 275; 274, identifier:ret; 275, subscript; 275, 276; 275, 277; 276, identifier:ret; 277, string:"ids"; 278, if_statement; 278, 279; 278, 291; 279, boolean_operator:and; 279, 280; 279, 285; 280, call; 280, 281; 280, 282; 281, identifier:isinstance; 282, argument_list; 282, 283; 282, 284; 283, identifier:ret; 284, identifier:list; 285, comparison_operator:==; 285, 286; 285, 290; 286, call; 286, 287; 286, 288; 287, identifier:len; 288, argument_list; 288, 289; 289, identifier:ret; 290, integer:1; 291, block; 291, 292; 292, expression_statement; 292, 293; 293, assignment; 293, 294; 293, 295; 294, identifier:ret; 295, subscript; 295, 296; 295, 297; 296, identifier:ret; 297, integer:0; 298, return_statement; 298, 299; 299, identifier:ret
def attachfile(self, idlist, attachfile, description, **kwargs): """ Attach a file to the given bug IDs. Returns the ID of the attachment or raises XMLRPC Fault if something goes wrong. attachfile may be a filename (which will be opened) or a file-like object, which must provide a 'read' method. If it's not one of these, this method will raise a TypeError. description is the short description of this attachment. Optional keyword args are as follows: file_name: this will be used as the filename for the attachment. REQUIRED if attachfile is a file-like object with no 'name' attribute, otherwise the filename or .name attribute will be used. comment: An optional comment about this attachment. is_private: Set to True if the attachment should be marked private. is_patch: Set to True if the attachment is a patch. content_type: The mime-type of the attached file. Defaults to application/octet-stream if not set. NOTE that text files will *not* be viewable in bugzilla unless you remember to set this to text/plain. So remember that! Returns the list of attachment ids that were added. If only one attachment was added, we return the single int ID for back compat """ if isinstance(attachfile, str): f = open(attachfile, "rb") elif hasattr(attachfile, 'read'): f = attachfile else: raise TypeError("attachfile must be filename or file-like object") # Back compat if "contenttype" in kwargs: kwargs["content_type"] = kwargs.pop("contenttype") if "ispatch" in kwargs: kwargs["is_patch"] = kwargs.pop("ispatch") if "isprivate" in kwargs: kwargs["is_private"] = kwargs.pop("isprivate") if "filename" in kwargs: kwargs["file_name"] = kwargs.pop("filename") kwargs['summary'] = description data = f.read() if not isinstance(data, bytes): data = data.encode(locale.getpreferredencoding()) kwargs['data'] = Binary(data) kwargs['ids'] = self._listify(idlist) if 'file_name' not in kwargs and hasattr(f, "name"): kwargs['file_name'] = os.path.basename(f.name) if 'content_type' not in kwargs: ctype = None if kwargs['file_name']: ctype = mimetypes.guess_type( kwargs['file_name'], strict=False)[0] kwargs['content_type'] = ctype or 'application/octet-stream' ret = self._proxy.Bug.add_attachment(kwargs) if "attachments" in ret: # Up to BZ 4.2 ret = [int(k) for k in ret["attachments"].keys()] elif "ids" in ret: # BZ 4.4+ ret = ret["ids"] if isinstance(ret, list) and len(ret) == 1: ret = ret[0] return ret
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:pre_translation; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:query; 6, block; 6, 7; 6, 9; 6, 17; 6, 59; 6, 87; 6, 97; 6, 125; 6, 126; 6, 127; 6, 143; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:old; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:query; 15, identifier:copy; 16, argument_list; 17, if_statement; 17, 18; 17, 21; 18, comparison_operator:in; 18, 19; 18, 20; 19, string:'bug_id'; 20, identifier:query; 21, block; 21, 22; 21, 55; 22, if_statement; 22, 23; 22, 31; 22, 45; 23, not_operator; 23, 24; 24, call; 24, 25; 24, 26; 25, identifier:isinstance; 26, argument_list; 26, 27; 26, 30; 27, subscript; 27, 28; 27, 29; 28, identifier:query; 29, string:'bug_id'; 30, identifier:list; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 37; 34, subscript; 34, 35; 34, 36; 35, identifier:query; 36, string:'id'; 37, call; 37, 38; 37, 43; 38, attribute; 38, 39; 38, 42; 39, subscript; 39, 40; 39, 41; 40, identifier:query; 41, string:'bug_id'; 42, identifier:split; 43, argument_list; 43, 44; 44, string:','; 45, else_clause; 45, 46; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 52; 49, subscript; 49, 50; 49, 51; 50, identifier:query; 51, string:'id'; 52, subscript; 52, 53; 52, 54; 53, identifier:query; 54, string:'bug_id'; 55, delete_statement; 55, 56; 56, subscript; 56, 57; 56, 58; 57, identifier:query; 58, string:'bug_id'; 59, if_statement; 59, 60; 59, 63; 60, comparison_operator:in; 60, 61; 60, 62; 61, string:'component'; 62, identifier:query; 63, block; 63, 64; 64, if_statement; 64, 65; 64, 73; 65, not_operator; 65, 66; 66, call; 66, 67; 66, 68; 67, identifier:isinstance; 68, argument_list; 68, 69; 68, 72; 69, subscript; 69, 70; 69, 71; 70, identifier:query; 71, string:'component'; 72, identifier:list; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 79; 76, subscript; 76, 77; 76, 78; 77, identifier:query; 78, string:'component'; 79, call; 79, 80; 79, 85; 80, attribute; 80, 81; 80, 84; 81, subscript; 81, 82; 81, 83; 82, identifier:query; 83, string:'component'; 84, identifier:split; 85, argument_list; 85, 86; 86, string:','; 87, if_statement; 87, 88; 87, 95; 88, boolean_operator:and; 88, 89; 88, 92; 89, comparison_operator:not; 89, 90; 89, 91; 90, string:'include_fields'; 91, identifier:query; 92, comparison_operator:not; 92, 93; 92, 94; 93, string:'column_list'; 94, identifier:query; 95, block; 95, 96; 96, return_statement; 97, if_statement; 97, 98; 97, 101; 98, comparison_operator:not; 98, 99; 98, 100; 99, string:'include_fields'; 100, identifier:query; 101, block; 101, 102; 101, 108; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 107; 104, subscript; 104, 105; 104, 106; 105, identifier:query; 106, string:'include_fields'; 107, list:[]; 108, if_statement; 108, 109; 108, 112; 109, comparison_operator:in; 109, 110; 109, 111; 110, string:'column_list'; 111, identifier:query; 112, block; 112, 113; 112, 121; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 118; 115, subscript; 115, 116; 115, 117; 116, identifier:query; 117, string:'include_fields'; 118, subscript; 118, 119; 118, 120; 119, identifier:query; 120, string:'column_list'; 121, delete_statement; 121, 122; 122, subscript; 122, 123; 122, 124; 123, identifier:query; 124, string:'column_list'; 125, comment; 126, comment; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:query; 131, identifier:update; 132, argument_list; 132, 133; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:self; 136, identifier:_process_include_fields; 137, argument_list; 137, 138; 137, 141; 137, 142; 138, subscript; 138, 139; 138, 140; 139, identifier:query; 140, string:"include_fields"; 141, None; 142, None; 143, if_statement; 143, 144; 143, 147; 144, comparison_operator:!=; 144, 145; 144, 146; 145, identifier:old; 146, identifier:query; 147, block; 147, 148; 148, expression_statement; 148, 149; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:log; 152, identifier:debug; 153, argument_list; 153, 154; 153, 155; 154, string:"RHBugzilla pretranslated query to: %s"; 155, identifier:query
def pre_translation(self, query): """ Translates the query for possible aliases """ old = query.copy() if 'bug_id' in query: if not isinstance(query['bug_id'], list): query['id'] = query['bug_id'].split(',') else: query['id'] = query['bug_id'] del query['bug_id'] if 'component' in query: if not isinstance(query['component'], list): query['component'] = query['component'].split(',') if 'include_fields' not in query and 'column_list' not in query: return if 'include_fields' not in query: query['include_fields'] = [] if 'column_list' in query: query['include_fields'] = query['column_list'] del query['column_list'] # We need to do this for users here for users that # don't call build_query query.update(self._process_include_fields(query["include_fields"], None, None)) if old != query: log.debug("RHBugzilla pretranslated query to: %s", query)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:check_differences; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 15; 5, 31; 5, 47; 5, 65; 5, 66; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, call; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:logger; 12, identifier:info; 13, argument_list; 13, 14; 14, string:"Check that mail differences are within the limits."; 15, if_statement; 15, 16; 15, 23; 16, comparison_operator:<; 16, 17; 16, 22; 17, attribute; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:conf; 21, identifier:size_threshold; 22, integer:0; 23, block; 23, 24; 24, expression_statement; 24, 25; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:logger; 28, identifier:info; 29, argument_list; 29, 30; 30, string:"Skip checking for size differences."; 31, if_statement; 31, 32; 31, 39; 32, comparison_operator:<; 32, 33; 32, 38; 33, attribute; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:conf; 37, identifier:content_threshold; 38, integer:0; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:logger; 44, identifier:info; 45, argument_list; 45, 46; 46, string:"Skip checking for content differences."; 47, if_statement; 47, 48; 47, 63; 48, boolean_operator:and; 48, 49; 48, 56; 49, comparison_operator:<; 49, 50; 49, 55; 50, attribute; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:conf; 54, identifier:size_threshold; 55, integer:0; 56, comparison_operator:<; 56, 57; 56, 62; 57, attribute; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:conf; 61, identifier:content_threshold; 62, integer:0; 63, block; 63, 64; 64, return_statement; 65, comment; 66, for_statement; 66, 67; 66, 70; 66, 77; 66, 78; 67, pattern_list; 67, 68; 67, 69; 68, identifier:mail_a; 69, identifier:mail_b; 70, call; 70, 71; 70, 72; 71, identifier:combinations; 72, argument_list; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:self; 75, identifier:pool; 76, integer:2; 77, comment; 78, block; 78, 79; 78, 127; 78, 128; 79, if_statement; 79, 80; 79, 88; 80, comparison_operator:>; 80, 81; 80, 86; 81, attribute; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:conf; 85, identifier:size_threshold; 86, unary_operator:-; 86, 87; 87, integer:1; 88, block; 88, 89; 88, 102; 88, 116; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:size_difference; 92, call; 92, 93; 92, 94; 93, identifier:abs; 94, argument_list; 94, 95; 95, binary_operator:-; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:mail_a; 98, identifier:size; 99, attribute; 99, 100; 99, 101; 100, identifier:mail_b; 101, identifier:size; 102, expression_statement; 102, 103; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:logger; 106, identifier:debug; 107, argument_list; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, string:"{} and {} differs by {} bytes in size."; 111, identifier:format; 112, argument_list; 112, 113; 112, 114; 112, 115; 113, identifier:mail_a; 114, identifier:mail_b; 115, identifier:size_difference; 116, if_statement; 116, 117; 116, 124; 117, comparison_operator:>; 117, 118; 117, 119; 118, identifier:size_difference; 119, attribute; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:self; 122, identifier:conf; 123, identifier:size_threshold; 124, block; 124, 125; 125, raise_statement; 125, 126; 126, identifier:SizeDiffAboveThreshold; 127, comment; 128, if_statement; 128, 129; 128, 137; 129, comparison_operator:>; 129, 130; 129, 135; 130, attribute; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:conf; 134, identifier:content_threshold; 135, unary_operator:-; 135, 136; 136, integer:1; 137, block; 137, 138; 137, 148; 137, 162; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:content_difference; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:self; 144, identifier:diff; 145, argument_list; 145, 146; 145, 147; 146, identifier:mail_a; 147, identifier:mail_b; 148, expression_statement; 148, 149; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:logger; 152, identifier:debug; 153, argument_list; 153, 154; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, string:"{} and {} differs by {} bytes in content."; 157, identifier:format; 158, argument_list; 158, 159; 158, 160; 158, 161; 159, identifier:mail_a; 160, identifier:mail_b; 161, identifier:content_difference; 162, if_statement; 162, 163; 162, 170; 163, comparison_operator:>; 163, 164; 163, 165; 164, identifier:content_difference; 165, attribute; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:self; 168, identifier:conf; 169, identifier:content_threshold; 170, block; 170, 171; 170, 191; 171, if_statement; 171, 172; 171, 177; 172, attribute; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:self; 175, identifier:conf; 176, identifier:show_diff; 177, block; 177, 178; 178, expression_statement; 178, 179; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:logger; 182, identifier:info; 183, argument_list; 183, 184; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:self; 187, identifier:pretty_diff; 188, argument_list; 188, 189; 188, 190; 189, identifier:mail_a; 190, identifier:mail_b; 191, raise_statement; 191, 192; 192, identifier:ContentDiffAboveThreshold
def check_differences(self): """ In-depth check of mail differences. Compare all mails of the duplicate set with each other, both in size and content. Raise an error if we're not within the limits imposed by the threshold setting. """ logger.info("Check that mail differences are within the limits.") if self.conf.size_threshold < 0: logger.info("Skip checking for size differences.") if self.conf.content_threshold < 0: logger.info("Skip checking for content differences.") if self.conf.size_threshold < 0 and self.conf.content_threshold < 0: return # Compute differences of mail against one another. for mail_a, mail_b in combinations(self.pool, 2): # Compare mails on size. if self.conf.size_threshold > -1: size_difference = abs(mail_a.size - mail_b.size) logger.debug("{} and {} differs by {} bytes in size.".format( mail_a, mail_b, size_difference)) if size_difference > self.conf.size_threshold: raise SizeDiffAboveThreshold # Compare mails on content. if self.conf.content_threshold > -1: content_difference = self.diff(mail_a, mail_b) logger.debug( "{} and {} differs by {} bytes in content.".format( mail_a, mail_b, content_difference)) if content_difference > self.conf.content_threshold: if self.conf.show_diff: logger.info(self.pretty_diff(mail_a, mail_b)) raise ContentDiffAboveThreshold
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:filecache; 3, parameters; 3, 4; 3, 7; 4, default_parameter; 4, 5; 4, 6; 5, identifier:seconds_of_validity; 6, None; 7, default_parameter; 7, 8; 7, 9; 8, identifier:fail_silently; 9, False; 10, block; 10, 11; 10, 13; 10, 237; 10, 261; 11, expression_statement; 11, 12; 12, string:''' filecache is called and the decorator should be returned. '''; 13, function_definition; 13, 14; 13, 15; 13, 17; 14, function_name:filecache_decorator; 15, parameters; 15, 16; 16, identifier:function; 17, block; 17, 18; 17, 166; 17, 167; 17, 235; 18, decorated_definition; 18, 19; 18, 26; 19, decorator; 19, 20; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:_functools; 23, identifier:wraps; 24, argument_list; 24, 25; 25, identifier:function; 26, function_definition; 26, 27; 26, 28; 26, 33; 27, function_name:function_with_cache; 28, parameters; 28, 29; 28, 31; 29, list_splat_pattern; 29, 30; 30, identifier:args; 31, dictionary_splat_pattern; 31, 32; 32, identifier:kwargs; 33, block; 33, 34; 33, 103; 33, 113; 33, 114; 33, 115; 33, 116; 33, 164; 34, try_statement; 34, 35; 34, 81; 35, block; 35, 36; 35, 45; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:key; 39, call; 39, 40; 39, 41; 40, identifier:_args_key; 41, argument_list; 41, 42; 41, 43; 41, 44; 42, identifier:function; 43, identifier:args; 44, identifier:kwargs; 45, if_statement; 45, 46; 45, 51; 46, comparison_operator:in; 46, 47; 46, 48; 47, identifier:key; 48, attribute; 48, 49; 48, 50; 49, identifier:function; 50, identifier:_db; 51, block; 51, 52; 51, 60; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:rv; 55, subscript; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:function; 58, identifier:_db; 59, identifier:key; 60, if_statement; 60, 61; 60, 76; 61, boolean_operator:or; 61, 62; 61, 65; 62, comparison_operator:is; 62, 63; 62, 64; 63, identifier:seconds_of_validity; 64, None; 65, comparison_operator:<; 65, 66; 65, 75; 66, binary_operator:-; 66, 67; 66, 72; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:_time; 70, identifier:time; 71, argument_list; 72, attribute; 72, 73; 72, 74; 73, identifier:rv; 74, identifier:timesig; 75, identifier:seconds_of_validity; 76, block; 76, 77; 77, return_statement; 77, 78; 78, attribute; 78, 79; 78, 80; 79, identifier:rv; 80, identifier:data; 81, except_clause; 81, 82; 81, 83; 81, 84; 82, identifier:Exception; 83, comment; 84, block; 84, 85; 84, 93; 84, 98; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:error_str; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:_traceback; 91, identifier:format_exc; 92, argument_list; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 96; 95, identifier:_log_error; 96, argument_list; 96, 97; 97, identifier:error_str; 98, if_statement; 98, 99; 98, 101; 99, not_operator; 99, 100; 100, identifier:fail_silently; 101, block; 101, 102; 102, raise_statement; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:retval; 106, call; 106, 107; 106, 108; 107, identifier:function; 108, argument_list; 108, 109; 108, 111; 109, list_splat; 109, 110; 110, identifier:args; 111, dictionary_splat; 111, 112; 112, identifier:kwargs; 113, comment; 114, comment; 115, comment; 116, try_statement; 116, 117; 116, 142; 117, block; 117, 118; 117, 134; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 125; 120, subscript; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:function; 123, identifier:_db; 124, identifier:key; 125, call; 125, 126; 125, 127; 126, identifier:_retval; 127, argument_list; 127, 128; 127, 133; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:_time; 131, identifier:time; 132, argument_list; 133, identifier:retval; 134, expression_statement; 134, 135; 135, call; 135, 136; 135, 141; 136, attribute; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:function; 139, identifier:_db; 140, identifier:sync; 141, argument_list; 142, except_clause; 142, 143; 142, 144; 142, 145; 143, identifier:Exception; 144, comment; 145, block; 145, 146; 145, 154; 145, 159; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:error_str; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:_traceback; 152, identifier:format_exc; 153, argument_list; 154, expression_statement; 154, 155; 155, call; 155, 156; 155, 157; 156, identifier:_log_error; 157, argument_list; 157, 158; 158, identifier:error_str; 159, if_statement; 159, 160; 159, 162; 160, not_operator; 160, 161; 161, identifier:fail_silently; 162, block; 162, 163; 163, raise_statement; 164, return_statement; 164, 165; 165, identifier:retval; 166, comment; 167, if_statement; 167, 168; 167, 174; 168, not_operator; 168, 169; 169, call; 169, 170; 169, 171; 170, identifier:hasattr; 171, argument_list; 171, 172; 171, 173; 172, identifier:function; 173, string:'_db'; 174, block; 174, 175; 174, 182; 174, 227; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:cache_name; 178, call; 178, 179; 178, 180; 179, identifier:_get_cache_name; 180, argument_list; 180, 181; 181, identifier:function; 182, if_statement; 182, 183; 182, 186; 182, 195; 183, comparison_operator:in; 183, 184; 183, 185; 184, identifier:cache_name; 185, identifier:OPEN_DBS; 186, block; 186, 187; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:function; 191, identifier:_db; 192, subscript; 192, 193; 192, 194; 193, identifier:OPEN_DBS; 194, identifier:cache_name; 195, else_clause; 195, 196; 196, block; 196, 197; 196, 208; 196, 216; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:function; 201, identifier:_db; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:_shelve; 205, identifier:open; 206, argument_list; 206, 207; 207, identifier:cache_name; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 213; 210, subscript; 210, 211; 210, 212; 211, identifier:OPEN_DBS; 212, identifier:cache_name; 213, attribute; 213, 214; 213, 215; 214, identifier:function; 215, identifier:_db; 216, expression_statement; 216, 217; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:atexit; 220, identifier:register; 221, argument_list; 221, 222; 222, attribute; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:function; 225, identifier:_db; 226, identifier:close; 227, expression_statement; 227, 228; 228, assignment; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:function_with_cache; 231, identifier:_db; 232, attribute; 232, 233; 232, 234; 233, identifier:function; 234, identifier:_db; 235, return_statement; 235, 236; 236, identifier:function_with_cache; 237, if_statement; 237, 238; 237, 246; 237, 247; 238, comparison_operator:==; 238, 239; 238, 243; 239, call; 239, 240; 239, 241; 240, identifier:type; 241, argument_list; 241, 242; 242, identifier:seconds_of_validity; 243, attribute; 243, 244; 243, 245; 244, identifier:types; 245, identifier:FunctionType; 246, comment; 247, block; 247, 248; 247, 252; 247, 256; 248, expression_statement; 248, 249; 249, assignment; 249, 250; 249, 251; 250, identifier:func; 251, identifier:seconds_of_validity; 252, expression_statement; 252, 253; 253, assignment; 253, 254; 253, 255; 254, identifier:seconds_of_validity; 255, None; 256, return_statement; 256, 257; 257, call; 257, 258; 257, 259; 258, identifier:filecache_decorator; 259, argument_list; 259, 260; 260, identifier:func; 261, return_statement; 261, 262; 262, identifier:filecache_decorator
def filecache(seconds_of_validity=None, fail_silently=False): ''' filecache is called and the decorator should be returned. ''' def filecache_decorator(function): @_functools.wraps(function) def function_with_cache(*args, **kwargs): try: key = _args_key(function, args, kwargs) if key in function._db: rv = function._db[key] if seconds_of_validity is None or _time.time() - rv.timesig < seconds_of_validity: return rv.data except Exception: # in any case of failure, don't let filecache break the program error_str = _traceback.format_exc() _log_error(error_str) if not fail_silently: raise retval = function(*args, **kwargs) # store in cache # NOTE: no need to _db.sync() because there was no mutation # NOTE: it's importatnt to do _db.sync() because otherwise the cache doesn't survive Ctrl-Break! try: function._db[key] = _retval(_time.time(), retval) function._db.sync() except Exception: # in any case of failure, don't let filecache break the program error_str = _traceback.format_exc() _log_error(error_str) if not fail_silently: raise return retval # make sure cache is loaded if not hasattr(function, '_db'): cache_name = _get_cache_name(function) if cache_name in OPEN_DBS: function._db = OPEN_DBS[cache_name] else: function._db = _shelve.open(cache_name) OPEN_DBS[cache_name] = function._db atexit.register(function._db.close) function_with_cache._db = function._db return function_with_cache if type(seconds_of_validity) == types.FunctionType: # support for when people use '@filecache.filecache' instead of '@filecache.filecache()' func = seconds_of_validity seconds_of_validity = None return filecache_decorator(func) return filecache_decorator
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:connect; 3, parameters; 3, 4; 3, 5; 3, 7; 4, identifier:self; 5, list_splat_pattern; 5, 6; 6, identifier:args; 7, dictionary_splat_pattern; 7, 8; 8, identifier:kwargs; 9, block; 9, 10; 9, 12; 9, 19; 9, 257; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, await; 13, 14; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:disconnect; 18, argument_list; 19, if_statement; 19, 20; 19, 30; 19, 76; 20, boolean_operator:and; 20, 21; 20, 24; 21, comparison_operator:not; 21, 22; 21, 23; 22, string:'endpoint'; 23, identifier:kwargs; 24, comparison_operator:<; 24, 25; 24, 29; 25, call; 25, 26; 25, 27; 26, identifier:len; 27, argument_list; 27, 28; 28, identifier:args; 29, integer:2; 30, block; 30, 31; 30, 64; 31, if_statement; 31, 32; 31, 37; 31, 43; 31, 52; 32, boolean_operator:and; 32, 33; 32, 34; 33, identifier:args; 34, comparison_operator:in; 34, 35; 34, 36; 35, string:'model_name'; 36, identifier:kwargs; 37, block; 37, 38; 38, raise_statement; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:TypeError; 41, argument_list; 41, 42; 42, string:'connect() got multiple values for model_name'; 43, elif_clause; 43, 44; 43, 45; 44, identifier:args; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:model_name; 49, subscript; 49, 50; 49, 51; 50, identifier:args; 51, integer:0; 52, else_clause; 52, 53; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:model_name; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:kwargs; 60, identifier:pop; 61, argument_list; 61, 62; 61, 63; 62, string:'model_name'; 63, None; 64, expression_statement; 64, 65; 65, await; 65, 66; 66, call; 66, 67; 66, 72; 67, attribute; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:_connector; 71, identifier:connect_model; 72, argument_list; 72, 73; 72, 74; 73, identifier:model_name; 74, dictionary_splat; 74, 75; 75, identifier:kwargs; 76, else_clause; 76, 77; 77, block; 77, 78; 77, 90; 77, 102; 77, 119; 77, 138; 77, 158; 77, 170; 77, 183; 77, 200; 77, 218; 77, 246; 78, if_statement; 78, 79; 78, 82; 79, comparison_operator:in; 79, 80; 79, 81; 80, string:'model_name'; 81, identifier:kwargs; 82, block; 82, 83; 83, raise_statement; 83, 84; 84, call; 84, 85; 84, 86; 85, identifier:TypeError; 86, argument_list; 86, 87; 87, concatenated_string; 87, 88; 87, 89; 88, string:'connect() got values for both '; 89, string:'model_name and endpoint'; 90, if_statement; 90, 91; 90, 96; 91, boolean_operator:and; 91, 92; 91, 93; 92, identifier:args; 93, comparison_operator:in; 93, 94; 93, 95; 94, string:'endpoint'; 95, identifier:kwargs; 96, block; 96, 97; 97, raise_statement; 97, 98; 98, call; 98, 99; 98, 100; 99, identifier:TypeError; 100, argument_list; 100, 101; 101, string:'connect() got multiple values for endpoint'; 102, if_statement; 102, 103; 102, 113; 103, boolean_operator:and; 103, 104; 103, 110; 104, comparison_operator:<; 104, 105; 104, 109; 105, call; 105, 106; 105, 107; 106, identifier:len; 107, argument_list; 107, 108; 108, identifier:args; 109, integer:2; 110, comparison_operator:not; 110, 111; 110, 112; 111, string:'uuid'; 112, identifier:kwargs; 113, block; 113, 114; 114, raise_statement; 114, 115; 115, call; 115, 116; 115, 117; 116, identifier:TypeError; 117, argument_list; 117, 118; 118, string:'connect() missing value for uuid'; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:has_userpass; 122, parenthesized_expression; 122, 123; 123, boolean_operator:or; 123, 124; 123, 130; 124, comparison_operator:>=; 124, 125; 124, 129; 125, call; 125, 126; 125, 127; 126, identifier:len; 127, argument_list; 127, 128; 128, identifier:args; 129, integer:4; 130, call; 130, 131; 130, 136; 131, attribute; 131, 132; 131, 135; 132, set; 132, 133; 132, 134; 133, string:'username'; 134, string:'password'; 135, identifier:issubset; 136, argument_list; 136, 137; 137, identifier:kwargs; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:has_macaroons; 141, parenthesized_expression; 141, 142; 142, boolean_operator:or; 142, 143; 142, 149; 143, comparison_operator:>=; 143, 144; 143, 148; 144, call; 144, 145; 144, 146; 145, identifier:len; 146, argument_list; 146, 147; 147, identifier:args; 148, integer:6; 149, not_operator; 149, 150; 150, call; 150, 151; 150, 156; 151, attribute; 151, 152; 151, 155; 152, set; 152, 153; 152, 154; 153, string:'bakery_client'; 154, string:'macaroons'; 155, identifier:isdisjoint; 156, argument_list; 156, 157; 157, identifier:kwargs; 158, if_statement; 158, 159; 158, 164; 159, not_operator; 159, 160; 160, parenthesized_expression; 160, 161; 161, boolean_operator:or; 161, 162; 161, 163; 162, identifier:has_userpass; 163, identifier:has_macaroons; 164, block; 164, 165; 165, raise_statement; 165, 166; 166, call; 166, 167; 166, 168; 167, identifier:TypeError; 168, argument_list; 168, 169; 169, string:'connect() missing auth params'; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:arg_names; 173, list:[ 'endpoint', 'uuid', 'username', 'password', 'cacert', 'bakery_client', 'macaroons', 'loop', 'max_frame_size', ]; 173, 174; 173, 175; 173, 176; 173, 177; 173, 178; 173, 179; 173, 180; 173, 181; 173, 182; 174, string:'endpoint'; 175, string:'uuid'; 176, string:'username'; 177, string:'password'; 178, string:'cacert'; 179, string:'bakery_client'; 180, string:'macaroons'; 181, string:'loop'; 182, string:'max_frame_size'; 183, for_statement; 183, 184; 183, 187; 183, 191; 184, pattern_list; 184, 185; 184, 186; 185, identifier:i; 186, identifier:arg; 187, call; 187, 188; 187, 189; 188, identifier:enumerate; 189, argument_list; 189, 190; 190, identifier:args; 191, block; 191, 192; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 199; 194, subscript; 194, 195; 194, 196; 195, identifier:kwargs; 196, subscript; 196, 197; 196, 198; 197, identifier:arg_names; 198, identifier:i; 199, identifier:arg; 200, if_statement; 200, 201; 200, 210; 201, not_operator; 201, 202; 202, call; 202, 203; 202, 208; 203, attribute; 203, 204; 203, 207; 204, set; 204, 205; 204, 206; 205, string:'endpoint'; 206, string:'uuid'; 207, identifier:issubset; 208, argument_list; 208, 209; 209, identifier:kwargs; 210, block; 210, 211; 211, raise_statement; 211, 212; 212, call; 212, 213; 212, 214; 213, identifier:ValueError; 214, argument_list; 214, 215; 215, concatenated_string; 215, 216; 215, 217; 216, string:'endpoint and uuid are required '; 217, string:'if model_name not given'; 218, if_statement; 218, 219; 218, 238; 219, not_operator; 219, 220; 220, parenthesized_expression; 220, 221; 221, boolean_operator:or; 221, 222; 221, 230; 222, call; 222, 223; 222, 228; 223, attribute; 223, 224; 223, 227; 224, set; 224, 225; 224, 226; 225, string:'username'; 226, string:'password'; 227, identifier:issubset; 228, argument_list; 228, 229; 229, identifier:kwargs; 230, call; 230, 231; 230, 236; 231, attribute; 231, 232; 231, 235; 232, set; 232, 233; 232, 234; 233, string:'bakery_client'; 234, string:'macaroons'; 235, identifier:intersection; 236, argument_list; 236, 237; 237, identifier:kwargs; 238, block; 238, 239; 239, raise_statement; 239, 240; 240, call; 240, 241; 240, 242; 241, identifier:ValueError; 242, argument_list; 242, 243; 243, concatenated_string; 243, 244; 243, 245; 244, string:'Authentication parameters are required '; 245, string:'if model_name not given'; 246, expression_statement; 246, 247; 247, await; 247, 248; 248, call; 248, 249; 248, 254; 249, attribute; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:self; 252, identifier:_connector; 253, identifier:connect; 254, argument_list; 254, 255; 255, dictionary_splat; 255, 256; 256, identifier:kwargs; 257, expression_statement; 257, 258; 258, await; 258, 259; 259, call; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:self; 262, identifier:_after_connect; 263, argument_list
async def connect(self, *args, **kwargs): """Connect to a juju model. This supports two calling conventions: The model and (optionally) authentication information can be taken from the data files created by the Juju CLI. This convention will be used if a ``model_name`` is specified, or if the ``endpoint`` and ``uuid`` are not. Otherwise, all of the ``endpoint``, ``uuid``, and authentication information (``username`` and ``password``, or ``bakery_client`` and/or ``macaroons``) are required. If a single positional argument is given, it will be assumed to be the ``model_name``. Otherwise, the first positional argument, if any, must be the ``endpoint``. Available parameters are: :param model_name: Format [controller:][user/]model :param str endpoint: The hostname:port of the controller to connect to. :param str uuid: The model UUID to connect to. :param str username: The username for controller-local users (or None to use macaroon-based login.) :param str password: The password for controller-local users. :param str cacert: The CA certificate of the controller (PEM formatted). :param httpbakery.Client bakery_client: The macaroon bakery client to to use when performing macaroon-based login. Macaroon tokens acquired when logging will be saved to bakery_client.cookies. If this is None, a default bakery_client will be used. :param list macaroons: List of macaroons to load into the ``bakery_client``. :param asyncio.BaseEventLoop loop: The event loop to use for async operations. :param int max_frame_size: The maximum websocket frame size to allow. """ await self.disconnect() if 'endpoint' not in kwargs and len(args) < 2: if args and 'model_name' in kwargs: raise TypeError('connect() got multiple values for model_name') elif args: model_name = args[0] else: model_name = kwargs.pop('model_name', None) await self._connector.connect_model(model_name, **kwargs) else: if 'model_name' in kwargs: raise TypeError('connect() got values for both ' 'model_name and endpoint') if args and 'endpoint' in kwargs: raise TypeError('connect() got multiple values for endpoint') if len(args) < 2 and 'uuid' not in kwargs: raise TypeError('connect() missing value for uuid') has_userpass = (len(args) >= 4 or {'username', 'password'}.issubset(kwargs)) has_macaroons = (len(args) >= 6 or not {'bakery_client', 'macaroons'}.isdisjoint(kwargs)) if not (has_userpass or has_macaroons): raise TypeError('connect() missing auth params') arg_names = [ 'endpoint', 'uuid', 'username', 'password', 'cacert', 'bakery_client', 'macaroons', 'loop', 'max_frame_size', ] for i, arg in enumerate(args): kwargs[arg_names[i]] = arg if not {'endpoint', 'uuid'}.issubset(kwargs): raise ValueError('endpoint and uuid are required ' 'if model_name not given') if not ({'username', 'password'}.issubset(kwargs) or {'bakery_client', 'macaroons'}.intersection(kwargs)): raise ValueError('Authentication parameters are required ' 'if model_name not given') await self._connector.connect(**kwargs) await self._after_connect()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_watch; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 295; 5, 302; 5, 310; 5, 318; 5, 326; 6, expression_statement; 6, 7; 7, comment; 8, function_definition; 8, 9; 8, 10; 8, 11; 9, function_name:_all_watcher; 10, parameters; 11, block; 11, 12; 12, try_statement; 12, 13; 12, 270; 12, 274; 12, 285; 13, block; 13, 14; 13, 29; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:allwatcher; 17, call; 17, 18; 17, 23; 18, attribute; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:client; 21, identifier:AllWatcherFacade; 22, identifier:from_connection; 23, argument_list; 23, 24; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:connection; 28, argument_list; 29, while_statement; 29, 30; 29, 38; 30, not_operator; 30, 31; 31, call; 31, 32; 31, 37; 32, attribute; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:self; 35, identifier:_watch_stopping; 36, identifier:is_set; 37, argument_list; 38, block; 38, 39; 38, 180; 38, 206; 38, 262; 39, try_statement; 39, 40; 39, 65; 39, 108; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:results; 44, await; 44, 45; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:utils; 48, identifier:run_with_interrupt; 49, argument_list; 49, 50; 49, 55; 49, 58; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:allwatcher; 53, identifier:Next; 54, argument_list; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:_watch_stopping; 58, keyword_argument; 58, 59; 58, 60; 59, identifier:loop; 60, attribute; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:self; 63, identifier:_connector; 64, identifier:loop; 65, except_clause; 65, 66; 65, 70; 66, as_pattern; 66, 67; 66, 68; 67, identifier:JujuAPIError; 68, as_pattern_target; 68, 69; 69, identifier:e; 70, block; 70, 71; 70, 80; 70, 94; 70, 95; 70, 96; 70, 103; 70, 107; 71, if_statement; 71, 72; 71, 78; 72, comparison_operator:not; 72, 73; 72, 74; 73, string:'watcher was stopped'; 74, call; 74, 75; 74, 76; 75, identifier:str; 76, argument_list; 76, 77; 77, identifier:e; 78, block; 78, 79; 79, raise_statement; 80, if_statement; 80, 81; 80, 88; 80, 89; 80, 90; 80, 91; 80, 92; 81, call; 81, 82; 81, 87; 82, attribute; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:self; 85, identifier:_watch_stopping; 86, identifier:is_set; 87, argument_list; 88, comment; 89, comment; 90, comment; 91, comment; 92, block; 92, 93; 93, break_statement; 94, comment; 95, comment; 96, expression_statement; 96, 97; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:log; 100, identifier:warning; 101, argument_list; 101, 102; 102, string:'Watcher: watcher stopped, restarting'; 103, delete_statement; 103, 104; 104, attribute; 104, 105; 104, 106; 105, identifier:allwatcher; 106, identifier:Id; 107, continue_statement; 108, except_clause; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:websockets; 111, identifier:ConnectionClosed; 112, block; 112, 113; 112, 123; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:monitor; 116, attribute; 116, 117; 116, 122; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:self; 120, identifier:connection; 121, argument_list; 122, identifier:monitor; 123, if_statement; 123, 124; 123, 131; 123, 132; 123, 176; 124, comparison_operator:==; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:monitor; 127, identifier:status; 128, attribute; 128, 129; 128, 130; 129, identifier:monitor; 130, identifier:ERROR; 131, comment; 132, block; 132, 133; 132, 140; 132, 151; 132, 171; 132, 175; 133, expression_statement; 133, 134; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:log; 137, identifier:warning; 138, argument_list; 138, 139; 139, string:'Watcher: connection closed, reopening'; 140, expression_statement; 140, 141; 141, await; 141, 142; 142, call; 142, 143; 142, 150; 143, attribute; 143, 144; 143, 149; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:self; 147, identifier:connection; 148, argument_list; 149, identifier:reconnect; 150, argument_list; 151, if_statement; 151, 152; 151, 159; 151, 160; 152, comparison_operator:!=; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:monitor; 155, identifier:status; 156, attribute; 156, 157; 156, 158; 157, identifier:monitor; 158, identifier:CONNECTED; 159, comment; 160, block; 160, 161; 160, 170; 161, expression_statement; 161, 162; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:log; 165, identifier:error; 166, argument_list; 166, 167; 167, concatenated_string; 167, 168; 167, 169; 168, string:'Watcher: automatic reconnect '; 169, string:'failed; stopping watcher'; 170, break_statement; 171, delete_statement; 171, 172; 172, attribute; 172, 173; 172, 174; 173, identifier:allwatcher; 174, identifier:Id; 175, continue_statement; 176, else_clause; 176, 177; 176, 178; 177, comment; 178, block; 178, 179; 179, break_statement; 180, if_statement; 180, 181; 180, 188; 181, call; 181, 182; 181, 187; 182, attribute; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:self; 185, identifier:_watch_stopping; 186, identifier:is_set; 187, argument_list; 188, block; 188, 189; 188, 205; 189, try_statement; 189, 190; 189, 198; 190, block; 190, 191; 191, expression_statement; 191, 192; 192, await; 192, 193; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:allwatcher; 196, identifier:Stop; 197, argument_list; 198, except_clause; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:websockets; 201, identifier:ConnectionClosed; 202, block; 202, 203; 202, 204; 203, pass_statement; 204, comment; 205, break_statement; 206, for_statement; 206, 207; 206, 208; 206, 211; 207, identifier:delta; 208, attribute; 208, 209; 208, 210; 209, identifier:results; 210, identifier:deltas; 211, block; 211, 212; 212, try_statement; 212, 213; 212, 244; 213, block; 213, 214; 213, 221; 213, 234; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:delta; 217, call; 217, 218; 217, 219; 218, identifier:get_entity_delta; 219, argument_list; 219, 220; 220, identifier:delta; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 226; 223, pattern_list; 223, 224; 223, 225; 224, identifier:old_obj; 225, identifier:new_obj; 226, call; 226, 227; 226, 232; 227, attribute; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:self; 230, identifier:state; 231, identifier:apply_delta; 232, argument_list; 232, 233; 233, identifier:delta; 234, expression_statement; 234, 235; 235, await; 235, 236; 236, call; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:self; 239, identifier:_notify_observers; 240, argument_list; 240, 241; 240, 242; 240, 243; 241, identifier:delta; 242, identifier:old_obj; 243, identifier:new_obj; 244, except_clause; 244, 245; 244, 249; 245, as_pattern; 245, 246; 245, 247; 246, identifier:KeyError; 247, as_pattern_target; 247, 248; 248, identifier:e; 249, block; 249, 250; 250, expression_statement; 250, 251; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:log; 254, identifier:debug; 255, argument_list; 255, 256; 255, 257; 256, string:"unknown delta type: %s"; 257, subscript; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:e; 260, identifier:args; 261, integer:0; 262, expression_statement; 262, 263; 263, call; 263, 264; 263, 269; 264, attribute; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:self; 267, identifier:_watch_received; 268, identifier:set; 269, argument_list; 270, except_clause; 270, 271; 270, 272; 271, identifier:CancelledError; 272, block; 272, 273; 273, pass_statement; 274, except_clause; 274, 275; 274, 276; 275, identifier:Exception; 276, block; 276, 277; 276, 284; 277, expression_statement; 277, 278; 278, call; 278, 279; 278, 282; 279, attribute; 279, 280; 279, 281; 280, identifier:log; 281, identifier:exception; 282, argument_list; 282, 283; 283, string:'Error in watcher'; 284, raise_statement; 285, finally_clause; 285, 286; 286, block; 286, 287; 287, expression_statement; 287, 288; 288, call; 288, 289; 288, 294; 289, attribute; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:self; 292, identifier:_watch_stopped; 293, identifier:set; 294, argument_list; 295, expression_statement; 295, 296; 296, call; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:log; 299, identifier:debug; 300, argument_list; 300, 301; 301, string:'Starting watcher task'; 302, expression_statement; 302, 303; 303, call; 303, 304; 303, 309; 304, attribute; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:self; 307, identifier:_watch_received; 308, identifier:clear; 309, argument_list; 310, expression_statement; 310, 311; 311, call; 311, 312; 311, 317; 312, attribute; 312, 313; 312, 316; 313, attribute; 313, 314; 313, 315; 314, identifier:self; 315, identifier:_watch_stopping; 316, identifier:clear; 317, argument_list; 318, expression_statement; 318, 319; 319, call; 319, 320; 319, 325; 320, attribute; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:self; 323, identifier:_watch_stopped; 324, identifier:clear; 325, argument_list; 326, expression_statement; 326, 327; 327, call; 327, 328; 327, 335; 328, attribute; 328, 329; 328, 334; 329, attribute; 329, 330; 329, 333; 330, attribute; 330, 331; 330, 332; 331, identifier:self; 332, identifier:_connector; 333, identifier:loop; 334, identifier:create_task; 335, argument_list; 335, 336; 336, call; 336, 337; 336, 338; 337, identifier:_all_watcher; 338, argument_list
def _watch(self): """Start an asynchronous watch against this model. See :meth:`add_observer` to register an onchange callback. """ async def _all_watcher(): try: allwatcher = client.AllWatcherFacade.from_connection( self.connection()) while not self._watch_stopping.is_set(): try: results = await utils.run_with_interrupt( allwatcher.Next(), self._watch_stopping, loop=self._connector.loop) except JujuAPIError as e: if 'watcher was stopped' not in str(e): raise if self._watch_stopping.is_set(): # this shouldn't ever actually happen, because # the event should trigger before the controller # has a chance to tell us the watcher is stopped # but handle it gracefully, just in case break # controller stopped our watcher for some reason # but we're not actually stopping, so just restart it log.warning( 'Watcher: watcher stopped, restarting') del allwatcher.Id continue except websockets.ConnectionClosed: monitor = self.connection().monitor if monitor.status == monitor.ERROR: # closed unexpectedly, try to reopen log.warning( 'Watcher: connection closed, reopening') await self.connection().reconnect() if monitor.status != monitor.CONNECTED: # reconnect failed; abort and shutdown log.error('Watcher: automatic reconnect ' 'failed; stopping watcher') break del allwatcher.Id continue else: # closed on request, go ahead and shutdown break if self._watch_stopping.is_set(): try: await allwatcher.Stop() except websockets.ConnectionClosed: pass # can't stop on a closed conn break for delta in results.deltas: try: delta = get_entity_delta(delta) old_obj, new_obj = self.state.apply_delta(delta) await self._notify_observers(delta, old_obj, new_obj) except KeyError as e: log.debug("unknown delta type: %s", e.args[0]) self._watch_received.set() except CancelledError: pass except Exception: log.exception('Error in watcher') raise finally: self._watch_stopped.set() log.debug('Starting watcher task') self._watch_received.clear() self._watch_stopping.clear() self._watch_stopped.clear() self._connector.loop.create_task(_all_watcher())
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:add_machine; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:spec; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:constraints; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:disks; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:series; 16, None; 17, block; 17, 18; 17, 20; 17, 28; 17, 107; 17, 114; 17, 130; 17, 150; 17, 159; 17, 160; 17, 175; 17, 186; 17, 196; 17, 208; 17, 218; 17, 247; 17, 255; 18, expression_statement; 18, 19; 19, comment; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:params; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:client; 26, identifier:AddMachineParams; 27, argument_list; 28, if_statement; 28, 29; 28, 30; 29, identifier:spec; 30, block; 30, 31; 31, if_statement; 31, 32; 31, 38; 31, 87; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:spec; 35, identifier:startswith; 36, argument_list; 36, 37; 37, string:"ssh:"; 38, block; 38, 39; 38, 51; 38, 62; 38, 79; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 45; 41, pattern_list; 41, 42; 41, 43; 41, 44; 42, identifier:placement; 43, identifier:target; 44, identifier:private_key_path; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:spec; 48, identifier:split; 49, argument_list; 49, 50; 50, string:":"; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 56; 53, pattern_list; 53, 54; 53, 55; 54, identifier:user; 55, identifier:host; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:target; 59, identifier:split; 60, argument_list; 60, 61; 61, string:"@"; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:sshProvisioner; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:provisioner; 68, identifier:SSHProvisioner; 69, argument_list; 69, 70; 69, 73; 69, 76; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:host; 72, identifier:host; 73, keyword_argument; 73, 74; 73, 75; 74, identifier:user; 75, identifier:user; 76, keyword_argument; 76, 77; 76, 78; 77, identifier:private_key_path; 78, identifier:private_key_path; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:params; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:sshProvisioner; 85, identifier:provision_machine; 86, argument_list; 87, else_clause; 87, 88; 88, block; 88, 89; 88, 96; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:placement; 92, call; 92, 93; 92, 94; 93, identifier:parse_placement; 94, argument_list; 94, 95; 95, identifier:spec; 96, if_statement; 96, 97; 96, 98; 97, identifier:placement; 98, block; 98, 99; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:params; 103, identifier:placement; 104, subscript; 104, 105; 104, 106; 105, identifier:placement; 106, integer:0; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:params; 111, identifier:jobs; 112, list:['JobHostUnits']; 112, 113; 113, string:'JobHostUnits'; 114, if_statement; 114, 115; 114, 116; 115, identifier:constraints; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:params; 121, identifier:constraints; 122, call; 122, 123; 122, 128; 123, attribute; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:client; 126, identifier:Value; 127, identifier:from_json; 128, argument_list; 128, 129; 129, identifier:constraints; 130, if_statement; 130, 131; 130, 132; 131, identifier:disks; 132, block; 132, 133; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:params; 137, identifier:disks; 138, list_comprehension; 138, 139; 138, 147; 139, call; 139, 140; 139, 145; 140, attribute; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:client; 143, identifier:Constraints; 144, identifier:from_json; 145, argument_list; 145, 146; 146, identifier:o; 147, for_in_clause; 147, 148; 147, 149; 148, identifier:o; 149, identifier:disks; 150, if_statement; 150, 151; 150, 152; 151, identifier:series; 152, block; 152, 153; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:params; 157, identifier:series; 158, identifier:series; 159, comment; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:client_facade; 163, call; 163, 164; 163, 169; 164, attribute; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:client; 167, identifier:ClientFacade; 168, identifier:from_connection; 169, argument_list; 169, 170; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:self; 173, identifier:connection; 174, argument_list; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:results; 178, await; 178, 179; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:client_facade; 182, identifier:AddMachines; 183, argument_list; 183, 184; 184, list:[params]; 184, 185; 185, identifier:params; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:error; 189, attribute; 189, 190; 189, 195; 190, subscript; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:results; 193, identifier:machines; 194, integer:0; 195, identifier:error; 196, if_statement; 196, 197; 196, 198; 197, identifier:error; 198, block; 198, 199; 199, raise_statement; 199, 200; 200, call; 200, 201; 200, 202; 201, identifier:ValueError; 202, argument_list; 202, 203; 203, binary_operator:%; 203, 204; 203, 205; 204, string:"Error adding machine: %s"; 205, attribute; 205, 206; 205, 207; 206, identifier:error; 207, identifier:message; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 211; 210, identifier:machine_id; 211, attribute; 211, 212; 211, 217; 212, subscript; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:results; 215, identifier:machines; 216, integer:0; 217, identifier:machine; 218, if_statement; 218, 219; 218, 220; 219, identifier:spec; 220, block; 220, 221; 221, if_statement; 221, 222; 221, 228; 221, 229; 221, 230; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:spec; 225, identifier:startswith; 226, argument_list; 226, 227; 227, string:"ssh:"; 228, comment; 229, comment; 230, block; 230, 231; 231, expression_statement; 231, 232; 232, await; 232, 233; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:sshProvisioner; 236, identifier:install_agent; 237, argument_list; 237, 238; 237, 243; 237, 246; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:self; 241, identifier:connection; 242, argument_list; 243, attribute; 243, 244; 243, 245; 244, identifier:params; 245, identifier:nonce; 246, identifier:machine_id; 247, expression_statement; 247, 248; 248, call; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:log; 251, identifier:debug; 252, argument_list; 252, 253; 252, 254; 253, string:'Added new machine %s'; 254, identifier:machine_id; 255, return_statement; 255, 256; 256, await; 256, 257; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:self; 260, identifier:_wait_for_new; 261, argument_list; 261, 262; 261, 263; 262, string:'machine'; 263, identifier:machine_id
async def add_machine( self, spec=None, constraints=None, disks=None, series=None): """Start a new, empty machine and optionally a container, or add a container to a machine. :param str spec: Machine specification Examples:: (None) - starts a new machine 'lxd' - starts a new machine with one lxd container 'lxd:4' - starts a new lxd container on machine 4 'ssh:[email protected]:/path/to/private/key' - manually provision a machine with ssh and the private key used for authentication 'zone=us-east-1a' - starts a machine in zone us-east-1s on AWS 'maas2.name' - acquire machine maas2.name on MAAS :param dict constraints: Machine constraints, which can contain the the following keys:: arch : str container : str cores : int cpu_power : int instance_type : str mem : int root_disk : int spaces : list(str) tags : list(str) virt_type : str Example:: constraints={ 'mem': 256 * MB, 'tags': ['virtual'], } :param list disks: List of disk constraint dictionaries, which can contain the following keys:: count : int pool : str size : int Example:: disks=[{ 'pool': 'rootfs', 'size': 10 * GB, 'count': 1, }] :param str series: Series, e.g. 'xenial' Supported container types are: lxd, kvm When deploying a container to an existing machine, constraints cannot be used. """ params = client.AddMachineParams() if spec: if spec.startswith("ssh:"): placement, target, private_key_path = spec.split(":") user, host = target.split("@") sshProvisioner = provisioner.SSHProvisioner( host=host, user=user, private_key_path=private_key_path, ) params = sshProvisioner.provision_machine() else: placement = parse_placement(spec) if placement: params.placement = placement[0] params.jobs = ['JobHostUnits'] if constraints: params.constraints = client.Value.from_json(constraints) if disks: params.disks = [ client.Constraints.from_json(o) for o in disks] if series: params.series = series # Submit the request. client_facade = client.ClientFacade.from_connection(self.connection()) results = await client_facade.AddMachines([params]) error = results.machines[0].error if error: raise ValueError("Error adding machine: %s" % error.message) machine_id = results.machines[0].machine if spec: if spec.startswith("ssh:"): # Need to run this after AddMachines has been called, # as we need the machine_id await sshProvisioner.install_agent( self.connection(), params.nonce, machine_id, ) log.debug('Added new machine %s', machine_id) return await self._wait_for_new('machine', machine_id)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:get_action_output; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:action_uuid; 6, default_parameter; 6, 7; 6, 8; 7, identifier:wait; 8, None; 9, block; 9, 10; 9, 12; 9, 27; 9, 40; 9, 41; 9, 42; 9, 43; 9, 44; 9, 85; 9, 98; 9, 108; 9, 109; 9, 136; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:action_facade; 15, call; 15, 16; 15, 21; 16, attribute; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:client; 19, identifier:ActionFacade; 20, identifier:from_connection; 21, argument_list; 21, 22; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:connection; 26, argument_list; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:entity; 30, list:[{'tag': tag.action(action_uuid)}]; 30, 31; 31, dictionary; 31, 32; 32, pair; 32, 33; 32, 34; 33, string:'tag'; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:tag; 37, identifier:action; 38, argument_list; 38, 39; 39, identifier:action_uuid; 40, comment; 41, comment; 42, comment; 43, comment; 44, function_definition; 44, 45; 44, 46; 44, 47; 45, function_name:_wait_for_action_status; 46, parameters; 47, block; 47, 48; 48, while_statement; 48, 49; 48, 50; 49, True; 50, block; 50, 51; 50, 61; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:action_output; 54, await; 54, 55; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:action_facade; 58, identifier:Actions; 59, argument_list; 59, 60; 60, identifier:entity; 61, if_statement; 61, 62; 61, 73; 61, 75; 62, comparison_operator:in; 62, 63; 62, 70; 63, attribute; 63, 64; 63, 69; 64, subscript; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:action_output; 67, identifier:results; 68, integer:0; 69, identifier:status; 70, tuple; 70, 71; 70, 72; 71, string:'completed'; 72, string:'failed'; 73, block; 73, 74; 74, return_statement; 75, else_clause; 75, 76; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, await; 78, 79; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:asyncio; 82, identifier:sleep; 83, argument_list; 83, 84; 84, integer:1; 85, expression_statement; 85, 86; 86, await; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:asyncio; 90, identifier:wait_for; 91, argument_list; 91, 92; 91, 95; 92, call; 92, 93; 92, 94; 93, identifier:_wait_for_action_status; 94, argument_list; 95, keyword_argument; 95, 96; 95, 97; 96, identifier:timeout; 97, identifier:wait; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:action_output; 101, await; 101, 102; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:action_facade; 105, identifier:Actions; 106, argument_list; 106, 107; 107, identifier:entity; 108, comment; 109, if_statement; 109, 110; 109, 119; 109, 124; 110, comparison_operator:is; 110, 111; 110, 118; 111, attribute; 111, 112; 111, 117; 112, subscript; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:action_output; 115, identifier:results; 116, integer:0; 117, identifier:output; 118, None; 119, block; 119, 120; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:output; 123, dictionary; 124, else_clause; 124, 125; 125, block; 125, 126; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:output; 129, attribute; 129, 130; 129, 135; 130, subscript; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:action_output; 133, identifier:results; 134, integer:0; 135, identifier:output; 136, return_statement; 136, 137; 137, identifier:output
async def get_action_output(self, action_uuid, wait=None): """Get the results of an action by ID. :param str action_uuid: Id of the action :param int wait: Time in seconds to wait for action to complete. :return dict: Output from action :raises: :class:`JujuError` if invalid action_uuid """ action_facade = client.ActionFacade.from_connection( self.connection() ) entity = [{'tag': tag.action(action_uuid)}] # Cannot use self.wait_for_action as the action event has probably # already happened and self.wait_for_action works by processing # model deltas and checking if they match our type. If the action # has already occured then the delta has gone. async def _wait_for_action_status(): while True: action_output = await action_facade.Actions(entity) if action_output.results[0].status in ('completed', 'failed'): return else: await asyncio.sleep(1) await asyncio.wait_for( _wait_for_action_status(), timeout=wait) action_output = await action_facade.Actions(entity) # ActionResult.output is None if the action produced no output if action_output.results[0].output is None: output = {} else: output = action_output.results[0].output return output
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:connect; 3, parameters; 3, 4; 3, 5; 3, 7; 4, identifier:self; 5, list_splat_pattern; 5, 6; 6, identifier:args; 7, dictionary_splat_pattern; 7, 8; 8, identifier:kwargs; 9, block; 9, 10; 9, 12; 9, 19; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, await; 13, 14; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:disconnect; 18, argument_list; 19, if_statement; 19, 20; 19, 30; 19, 78; 20, boolean_operator:and; 20, 21; 20, 24; 21, comparison_operator:not; 21, 22; 21, 23; 22, string:'endpoint'; 23, identifier:kwargs; 24, comparison_operator:<; 24, 25; 24, 29; 25, call; 25, 26; 25, 27; 26, identifier:len; 27, argument_list; 27, 28; 28, identifier:args; 29, integer:2; 30, block; 30, 31; 30, 66; 31, if_statement; 31, 32; 31, 37; 31, 45; 31, 54; 32, boolean_operator:and; 32, 33; 32, 34; 33, identifier:args; 34, comparison_operator:in; 34, 35; 34, 36; 35, string:'model_name'; 36, identifier:kwargs; 37, block; 37, 38; 38, raise_statement; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:TypeError; 41, argument_list; 41, 42; 42, concatenated_string; 42, 43; 42, 44; 43, string:'connect() got multiple values for '; 44, string:'controller_name'; 45, elif_clause; 45, 46; 45, 47; 46, identifier:args; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:controller_name; 51, subscript; 51, 52; 51, 53; 52, identifier:args; 53, integer:0; 54, else_clause; 54, 55; 55, block; 55, 56; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:controller_name; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:kwargs; 62, identifier:pop; 63, argument_list; 63, 64; 63, 65; 64, string:'controller_name'; 65, None; 66, expression_statement; 66, 67; 67, await; 67, 68; 68, call; 68, 69; 68, 74; 69, attribute; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:_connector; 73, identifier:connect_controller; 74, argument_list; 74, 75; 74, 76; 75, identifier:controller_name; 76, dictionary_splat; 76, 77; 77, identifier:kwargs; 78, else_clause; 78, 79; 79, block; 79, 80; 79, 92; 79, 104; 79, 123; 79, 143; 79, 155; 79, 167; 79, 184; 79, 196; 79, 224; 80, if_statement; 80, 81; 80, 84; 81, comparison_operator:in; 81, 82; 81, 83; 82, string:'controller_name'; 83, identifier:kwargs; 84, block; 84, 85; 85, raise_statement; 85, 86; 86, call; 86, 87; 86, 88; 87, identifier:TypeError; 88, argument_list; 88, 89; 89, concatenated_string; 89, 90; 89, 91; 90, string:'connect() got values for both '; 91, string:'controller_name and endpoint'; 92, if_statement; 92, 93; 92, 98; 93, boolean_operator:and; 93, 94; 93, 95; 94, identifier:args; 95, comparison_operator:in; 95, 96; 95, 97; 96, string:'endpoint'; 97, identifier:kwargs; 98, block; 98, 99; 99, raise_statement; 99, 100; 100, call; 100, 101; 100, 102; 101, identifier:TypeError; 102, argument_list; 102, 103; 103, string:'connect() got multiple values for endpoint'; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:has_userpass; 107, parenthesized_expression; 107, 108; 108, boolean_operator:or; 108, 109; 108, 115; 109, comparison_operator:>=; 109, 110; 109, 114; 110, call; 110, 111; 110, 112; 111, identifier:len; 112, argument_list; 112, 113; 113, identifier:args; 114, integer:3; 115, call; 115, 116; 115, 121; 116, attribute; 116, 117; 116, 120; 117, set; 117, 118; 117, 119; 118, string:'username'; 119, string:'password'; 120, identifier:issubset; 121, argument_list; 121, 122; 122, identifier:kwargs; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:has_macaroons; 126, parenthesized_expression; 126, 127; 127, boolean_operator:or; 127, 128; 127, 134; 128, comparison_operator:>=; 128, 129; 128, 133; 129, call; 129, 130; 129, 131; 130, identifier:len; 131, argument_list; 131, 132; 132, identifier:args; 133, integer:5; 134, not_operator; 134, 135; 135, call; 135, 136; 135, 141; 136, attribute; 136, 137; 136, 140; 137, set; 137, 138; 137, 139; 138, string:'bakery_client'; 139, string:'macaroons'; 140, identifier:isdisjoint; 141, argument_list; 141, 142; 142, identifier:kwargs; 143, if_statement; 143, 144; 143, 149; 144, not_operator; 144, 145; 145, parenthesized_expression; 145, 146; 146, boolean_operator:or; 146, 147; 146, 148; 147, identifier:has_userpass; 148, identifier:has_macaroons; 149, block; 149, 150; 150, raise_statement; 150, 151; 151, call; 151, 152; 151, 153; 152, identifier:TypeError; 153, argument_list; 153, 154; 154, string:'connect() missing auth params'; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:arg_names; 158, list:[ 'endpoint', 'username', 'password', 'cacert', 'bakery_client', 'macaroons', 'loop', 'max_frame_size', ]; 158, 159; 158, 160; 158, 161; 158, 162; 158, 163; 158, 164; 158, 165; 158, 166; 159, string:'endpoint'; 160, string:'username'; 161, string:'password'; 162, string:'cacert'; 163, string:'bakery_client'; 164, string:'macaroons'; 165, string:'loop'; 166, string:'max_frame_size'; 167, for_statement; 167, 168; 167, 171; 167, 175; 168, pattern_list; 168, 169; 168, 170; 169, identifier:i; 170, identifier:arg; 171, call; 171, 172; 171, 173; 172, identifier:enumerate; 173, argument_list; 173, 174; 174, identifier:args; 175, block; 175, 176; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 183; 178, subscript; 178, 179; 178, 180; 179, identifier:kwargs; 180, subscript; 180, 181; 180, 182; 181, identifier:arg_names; 182, identifier:i; 183, identifier:arg; 184, if_statement; 184, 185; 184, 188; 185, comparison_operator:not; 185, 186; 185, 187; 186, string:'endpoint'; 187, identifier:kwargs; 188, block; 188, 189; 189, raise_statement; 189, 190; 190, call; 190, 191; 190, 192; 191, identifier:ValueError; 192, argument_list; 192, 193; 193, concatenated_string; 193, 194; 193, 195; 194, string:'endpoint is required '; 195, string:'if controller_name not given'; 196, if_statement; 196, 197; 196, 216; 197, not_operator; 197, 198; 198, parenthesized_expression; 198, 199; 199, boolean_operator:or; 199, 200; 199, 208; 200, call; 200, 201; 200, 206; 201, attribute; 201, 202; 201, 205; 202, set; 202, 203; 202, 204; 203, string:'username'; 204, string:'password'; 205, identifier:issubset; 206, argument_list; 206, 207; 207, identifier:kwargs; 208, call; 208, 209; 208, 214; 209, attribute; 209, 210; 209, 213; 210, set; 210, 211; 210, 212; 211, string:'bakery_client'; 212, string:'macaroons'; 213, identifier:intersection; 214, argument_list; 214, 215; 215, identifier:kwargs; 216, block; 216, 217; 217, raise_statement; 217, 218; 218, call; 218, 219; 218, 220; 219, identifier:ValueError; 220, argument_list; 220, 221; 221, concatenated_string; 221, 222; 221, 223; 222, string:'Authentication parameters are required '; 223, string:'if controller_name not given'; 224, expression_statement; 224, 225; 225, await; 225, 226; 226, call; 226, 227; 226, 232; 227, attribute; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:self; 230, identifier:_connector; 231, identifier:connect; 232, argument_list; 232, 233; 233, dictionary_splat; 233, 234; 234, identifier:kwargs
async def connect(self, *args, **kwargs): """Connect to a Juju controller. This supports two calling conventions: The controller and (optionally) authentication information can be taken from the data files created by the Juju CLI. This convention will be used if a ``controller_name`` is specified, or if the ``endpoint`` is not. Otherwise, both the ``endpoint`` and authentication information (``username`` and ``password``, or ``bakery_client`` and/or ``macaroons``) are required. If a single positional argument is given, it will be assumed to be the ``controller_name``. Otherwise, the first positional argument, if any, must be the ``endpoint``. Available parameters are: :param str controller_name: Name of controller registered with the Juju CLI. :param str endpoint: The hostname:port of the controller to connect to. :param str username: The username for controller-local users (or None to use macaroon-based login.) :param str password: The password for controller-local users. :param str cacert: The CA certificate of the controller (PEM formatted). :param httpbakery.Client bakery_client: The macaroon bakery client to to use when performing macaroon-based login. Macaroon tokens acquired when logging will be saved to bakery_client.cookies. If this is None, a default bakery_client will be used. :param list macaroons: List of macaroons to load into the ``bakery_client``. :param asyncio.BaseEventLoop loop: The event loop to use for async operations. :param int max_frame_size: The maximum websocket frame size to allow. """ await self.disconnect() if 'endpoint' not in kwargs and len(args) < 2: if args and 'model_name' in kwargs: raise TypeError('connect() got multiple values for ' 'controller_name') elif args: controller_name = args[0] else: controller_name = kwargs.pop('controller_name', None) await self._connector.connect_controller(controller_name, **kwargs) else: if 'controller_name' in kwargs: raise TypeError('connect() got values for both ' 'controller_name and endpoint') if args and 'endpoint' in kwargs: raise TypeError('connect() got multiple values for endpoint') has_userpass = (len(args) >= 3 or {'username', 'password'}.issubset(kwargs)) has_macaroons = (len(args) >= 5 or not {'bakery_client', 'macaroons'}.isdisjoint(kwargs)) if not (has_userpass or has_macaroons): raise TypeError('connect() missing auth params') arg_names = [ 'endpoint', 'username', 'password', 'cacert', 'bakery_client', 'macaroons', 'loop', 'max_frame_size', ] for i, arg in enumerate(args): kwargs[arg_names[i]] = arg if 'endpoint' not in kwargs: raise ValueError('endpoint is required ' 'if controller_name not given') if not ({'username', 'password'}.issubset(kwargs) or {'bakery_client', 'macaroons'}.intersection(kwargs)): raise ValueError('Authentication parameters are required ' 'if controller_name not given') await self._connector.connect(**kwargs)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:add_credential; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:name; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:credential; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:cloud; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:owner; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:force; 19, False; 20, block; 20, 21; 20, 23; 20, 36; 20, 54; 20, 67; 20, 104; 20, 189; 20, 197; 20, 212; 20, 240; 20, 272; 21, expression_statement; 21, 22; 22, comment; 23, if_statement; 23, 24; 23, 26; 24, not_operator; 24, 25; 25, identifier:cloud; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:cloud; 30, await; 30, 31; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:get_cloud; 35, argument_list; 36, if_statement; 36, 37; 36, 39; 37, not_operator; 37, 38; 38, identifier:owner; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:owner; 43, subscript; 43, 44; 43, 53; 44, subscript; 44, 45; 44, 52; 45, attribute; 45, 46; 45, 51; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:connection; 50, argument_list; 51, identifier:info; 52, string:'user-info'; 53, string:'identity'; 54, if_statement; 54, 55; 54, 59; 55, boolean_operator:and; 55, 56; 55, 57; 56, identifier:credential; 57, not_operator; 57, 58; 58, identifier:name; 59, block; 59, 60; 60, raise_statement; 60, 61; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:errors; 64, identifier:JujuError; 65, argument_list; 65, 66; 66, string:'Name must be provided for credential'; 67, if_statement; 67, 68; 67, 70; 68, not_operator; 68, 69; 69, identifier:credential; 70, block; 70, 71; 70, 87; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 76; 73, pattern_list; 73, 74; 73, 75; 74, identifier:name; 75, identifier:credential; 76, call; 76, 77; 76, 84; 77, attribute; 77, 78; 77, 83; 78, attribute; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:self; 81, identifier:_connector; 82, identifier:jujudata; 83, identifier:load_credential; 84, argument_list; 84, 85; 84, 86; 85, identifier:cloud; 86, identifier:name; 87, if_statement; 87, 88; 87, 91; 88, comparison_operator:is; 88, 89; 88, 90; 89, identifier:credential; 90, None; 91, block; 91, 92; 92, raise_statement; 92, 93; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:errors; 96, identifier:JujuError; 97, argument_list; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, string:'Unable to find credential: {}'; 101, identifier:format; 102, argument_list; 102, 103; 103, identifier:name; 104, if_statement; 104, 105; 104, 116; 104, 117; 105, boolean_operator:and; 105, 106; 105, 111; 106, comparison_operator:==; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:credential; 109, identifier:auth_type; 110, string:'jsonfile'; 111, comparison_operator:in; 111, 112; 111, 113; 112, string:'file'; 113, attribute; 113, 114; 113, 115; 114, identifier:credential; 115, identifier:attrs; 116, comment; 117, block; 117, 118; 118, try_statement; 118, 119; 118, 120; 118, 132; 119, comment; 120, block; 120, 121; 121, expression_statement; 121, 122; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:json; 125, identifier:loads; 126, argument_list; 126, 127; 127, subscript; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:credential; 130, identifier:attrs; 131, string:'file'; 132, except_clause; 132, 133; 132, 136; 132, 137; 133, attribute; 133, 134; 133, 135; 134, identifier:json; 135, identifier:JSONDecodeError; 136, comment; 137, block; 137, 138; 137, 149; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:cred_path; 141, call; 141, 142; 141, 143; 142, identifier:Path; 143, argument_list; 143, 144; 144, subscript; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:credential; 147, identifier:attrs; 148, string:'file'; 149, if_statement; 149, 150; 149, 155; 149, 156; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:cred_path; 153, identifier:exists; 154, argument_list; 155, comment; 156, block; 156, 157; 156, 165; 156, 176; 156, 177; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:cred_json; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:credential; 163, identifier:to_json; 164, argument_list; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:credential; 168, call; 168, 169; 168, 174; 169, attribute; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:client; 172, identifier:CloudCredential; 173, identifier:from_json; 174, argument_list; 174, 175; 175, identifier:cred_json; 176, comment; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 184; 179, subscript; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:credential; 182, identifier:attrs; 183, string:'file'; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:cred_path; 187, identifier:read_text; 188, argument_list; 189, expression_statement; 189, 190; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:log; 193, identifier:debug; 194, argument_list; 194, 195; 194, 196; 195, string:'Uploading credential %s'; 196, identifier:name; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:cloud_facade; 200, call; 200, 201; 200, 206; 201, attribute; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:client; 204, identifier:CloudFacade; 205, identifier:from_connection; 206, argument_list; 206, 207; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:self; 210, identifier:connection; 211, argument_list; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:tagged_credentials; 215, list:[ client.UpdateCloudCredential( tag=tag.credential(cloud, tag.untag('user-', owner), name), credential=credential, )]; 215, 216; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:client; 219, identifier:UpdateCloudCredential; 220, argument_list; 220, 221; 220, 237; 221, keyword_argument; 221, 222; 221, 223; 222, identifier:tag; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:tag; 226, identifier:credential; 227, argument_list; 227, 228; 227, 229; 227, 236; 228, identifier:cloud; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:tag; 232, identifier:untag; 233, argument_list; 233, 234; 233, 235; 234, string:'user-'; 235, identifier:owner; 236, identifier:name; 237, keyword_argument; 237, 238; 237, 239; 238, identifier:credential; 239, identifier:credential; 240, if_statement; 240, 241; 240, 246; 240, 247; 240, 248; 240, 262; 241, comparison_operator:>=; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:cloud_facade; 244, identifier:version; 245, integer:3; 246, comment; 247, comment; 248, block; 248, 249; 249, expression_statement; 249, 250; 250, await; 250, 251; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:cloud_facade; 254, identifier:UpdateCredentialsCheckModels; 255, argument_list; 255, 256; 255, 259; 256, keyword_argument; 256, 257; 256, 258; 257, identifier:credentials; 258, identifier:tagged_credentials; 259, keyword_argument; 259, 260; 259, 261; 260, identifier:force; 261, identifier:force; 262, else_clause; 262, 263; 263, block; 263, 264; 264, expression_statement; 264, 265; 265, await; 265, 266; 266, call; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:cloud_facade; 269, identifier:UpdateCredentials; 270, argument_list; 270, 271; 271, identifier:tagged_credentials; 272, return_statement; 272, 273; 273, identifier:name
async def add_credential(self, name=None, credential=None, cloud=None, owner=None, force=False): """Add or update a credential to the controller. :param str name: Name of new credential. If None, the default local credential is used. Name must be provided if a credential is given. :param CloudCredential credential: Credential to add. If not given, it will attempt to read from local data, if available. :param str cloud: Name of cloud to associate the credential with. Defaults to the same cloud as the controller. :param str owner: Username that will own the credential. Defaults to the current user. :param bool force: Force indicates whether the update should be forced. It's only supported for facade v3 or later. Defaults to false. :returns: Name of credential that was uploaded. """ if not cloud: cloud = await self.get_cloud() if not owner: owner = self.connection().info['user-info']['identity'] if credential and not name: raise errors.JujuError('Name must be provided for credential') if not credential: name, credential = self._connector.jujudata.load_credential(cloud, name) if credential is None: raise errors.JujuError( 'Unable to find credential: {}'.format(name)) if credential.auth_type == 'jsonfile' and 'file' in credential.attrs: # file creds have to be loaded before being sent to the controller try: # it might already be JSON json.loads(credential.attrs['file']) except json.JSONDecodeError: # not valid JSON, so maybe it's a file cred_path = Path(credential.attrs['file']) if cred_path.exists(): # make a copy cred_json = credential.to_json() credential = client.CloudCredential.from_json(cred_json) # inline the cred credential.attrs['file'] = cred_path.read_text() log.debug('Uploading credential %s', name) cloud_facade = client.CloudFacade.from_connection(self.connection()) tagged_credentials = [ client.UpdateCloudCredential( tag=tag.credential(cloud, tag.untag('user-', owner), name), credential=credential, )] if cloud_facade.version >= 3: # UpdateCredentials was renamed to UpdateCredentialsCheckModels # in facade version 3. await cloud_facade.UpdateCredentialsCheckModels( credentials=tagged_credentials, force=force, ) else: await cloud_facade.UpdateCredentials(tagged_credentials) return name
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:matches; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, list_splat_pattern; 5, 6; 6, identifier:specs; 7, block; 7, 8; 7, 10; 7, 71; 8, expression_statement; 8, 9; 9, comment; 10, for_statement; 10, 11; 10, 12; 10, 13; 11, identifier:spec; 12, identifier:specs; 13, block; 13, 14; 13, 40; 14, if_statement; 14, 15; 14, 18; 14, 30; 15, comparison_operator:in; 15, 16; 15, 17; 16, string:':'; 17, identifier:spec; 18, block; 18, 19; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 24; 21, pattern_list; 21, 22; 21, 23; 22, identifier:app_name; 23, identifier:endpoint_name; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:spec; 27, identifier:split; 28, argument_list; 28, 29; 29, string:':'; 30, else_clause; 30, 31; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 37; 34, pattern_list; 34, 35; 34, 36; 35, identifier:app_name; 36, identifier:endpoint_name; 37, expression_list; 37, 38; 37, 39; 38, identifier:spec; 39, None; 40, for_statement; 40, 41; 40, 42; 40, 45; 40, 66; 41, identifier:endpoint; 42, attribute; 42, 43; 42, 44; 43, identifier:self; 44, identifier:endpoints; 45, block; 45, 46; 46, if_statement; 46, 47; 46, 63; 46, 64; 47, boolean_operator:and; 47, 48; 47, 55; 47, 56; 48, comparison_operator:==; 48, 49; 48, 50; 49, identifier:app_name; 50, attribute; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:endpoint; 53, identifier:application; 54, identifier:name; 55, line_continuation:\; 56, comparison_operator:in; 56, 57; 56, 58; 57, identifier:endpoint_name; 58, tuple; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:endpoint; 61, identifier:name; 62, None; 63, comment; 64, block; 64, 65; 65, break_statement; 66, else_clause; 66, 67; 66, 68; 67, comment; 68, block; 68, 69; 69, return_statement; 69, 70; 70, False; 71, return_statement; 71, 72; 72, True
def matches(self, *specs): """ Check if this relation matches relationship specs. Relation specs are strings that would be given to Juju to establish a relation, and should be in the form ``<application>[:<endpoint_name>]`` where the ``:<endpoint_name>`` suffix is optional. If the suffix is omitted, this relation will match on any endpoint as long as the given application is involved. In other words, this relation will match a spec if that spec could have created this relation. :return: True if all specs match. """ for spec in specs: if ':' in spec: app_name, endpoint_name = spec.split(':') else: app_name, endpoint_name = spec, None for endpoint in self.endpoints: if app_name == endpoint.application.name and \ endpoint_name in (endpoint.name, None): # found a match for this spec, so move to next one break else: # no match for this spec return False return True
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 32; 2, function_name:create; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 3, 29; 4, identifier:source; 5, default_parameter; 5, 6; 5, 7; 6, identifier:requirement_files; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:force; 10, False; 11, default_parameter; 11, 12; 11, 13; 12, identifier:keep_wheels; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:archive_destination_dir; 16, string:'.'; 17, default_parameter; 17, 18; 17, 19; 18, identifier:python_versions; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:validate_archive; 22, False; 23, default_parameter; 23, 24; 23, 25; 24, identifier:wheel_args; 25, string:''; 26, default_parameter; 26, 27; 26, 28; 27, identifier:archive_format; 28, string:'zip'; 29, default_parameter; 29, 30; 29, 31; 30, identifier:build_tag; 31, string:''; 32, block; 32, 33; 32, 35; 32, 42; 32, 50; 32, 57; 32, 91; 32, 100; 32, 108; 32, 120; 32, 132; 32, 161; 32, 168; 32, 181; 32, 188; 32, 206; 32, 217; 32, 229; 32, 235; 32, 248; 32, 255; 32, 276; 32, 284; 32, 292; 33, expression_statement; 33, 34; 34, comment; 35, if_statement; 35, 36; 35, 37; 36, identifier:validate_archive; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:_assert_virtualenv_is_installed; 41, argument_list; 42, expression_statement; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:logger; 46, identifier:info; 47, argument_list; 47, 48; 47, 49; 48, string:'Creating archive for %s...'; 49, identifier:source; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:processed_source; 53, call; 53, 54; 53, 55; 54, identifier:get_source; 55, argument_list; 55, 56; 56, identifier:source; 57, if_statement; 57, 58; 57, 85; 58, boolean_operator:and; 58, 59; 58, 67; 59, call; 59, 60; 59, 65; 60, attribute; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:os; 63, identifier:path; 64, identifier:isdir; 65, argument_list; 65, 66; 66, identifier:processed_source; 67, not_operator; 67, 68; 67, 69; 68, line_continuation:\; 69, call; 69, 70; 69, 75; 70, attribute; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:os; 73, identifier:path; 74, identifier:isfile; 75, argument_list; 75, 76; 76, call; 76, 77; 76, 82; 77, attribute; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:os; 80, identifier:path; 81, identifier:join; 82, argument_list; 82, 83; 82, 84; 83, identifier:processed_source; 84, string:'setup.py'; 85, block; 85, 86; 86, raise_statement; 86, 87; 87, call; 87, 88; 87, 89; 88, identifier:WagonError; 89, argument_list; 89, 90; 90, string:'Source directory must contain a setup.py file'; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 96; 93, pattern_list; 93, 94; 93, 95; 94, identifier:package_name; 95, identifier:package_version; 96, call; 96, 97; 96, 98; 97, identifier:get_source_name_and_version; 98, argument_list; 98, 99; 99, identifier:processed_source; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:tempdir; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:tempfile; 106, identifier:mkdtemp; 107, argument_list; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:workdir; 111, call; 111, 112; 111, 117; 112, attribute; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:os; 115, identifier:path; 116, identifier:join; 117, argument_list; 117, 118; 117, 119; 118, identifier:tempdir; 119, identifier:package_name; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:wheels_path; 123, call; 123, 124; 123, 129; 124, attribute; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:os; 127, identifier:path; 128, identifier:join; 129, argument_list; 129, 130; 129, 131; 130, identifier:workdir; 131, identifier:DEFAULT_WHEELS_PATH; 132, try_statement; 132, 133; 132, 144; 133, block; 133, 134; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:wheels; 137, call; 137, 138; 137, 139; 138, identifier:wheel; 139, argument_list; 139, 140; 139, 141; 139, 142; 139, 143; 140, identifier:processed_source; 141, identifier:requirement_files; 142, identifier:wheels_path; 143, identifier:wheel_args; 144, finally_clause; 144, 145; 145, block; 145, 146; 146, if_statement; 146, 147; 146, 150; 147, comparison_operator:!=; 147, 148; 147, 149; 148, identifier:processed_source; 149, identifier:source; 150, block; 150, 151; 151, expression_statement; 151, 152; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:shutil; 155, identifier:rmtree; 156, argument_list; 156, 157; 156, 158; 157, identifier:processed_source; 158, keyword_argument; 158, 159; 158, 160; 159, identifier:ignore_errors; 160, True; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:platform; 164, call; 164, 165; 164, 166; 165, identifier:_get_platform_for_set_of_wheels; 166, argument_list; 166, 167; 167, identifier:wheels_path; 168, if_statement; 168, 169; 168, 172; 169, call; 169, 170; 169, 171; 170, identifier:is_verbose; 171, argument_list; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:logger; 177, identifier:debug; 178, argument_list; 178, 179; 178, 180; 179, string:'Platform is: %s'; 180, identifier:platform; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:python_versions; 184, call; 184, 185; 184, 186; 185, identifier:_set_python_versions; 186, argument_list; 186, 187; 187, identifier:python_versions; 188, if_statement; 188, 189; 188, 198; 189, not_operator; 189, 190; 190, call; 190, 191; 190, 196; 191, attribute; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:os; 194, identifier:path; 195, identifier:isdir; 196, argument_list; 196, 197; 197, identifier:archive_destination_dir; 198, block; 198, 199; 199, expression_statement; 199, 200; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:os; 203, identifier:makedirs; 204, argument_list; 204, 205; 205, identifier:archive_destination_dir; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:archive_name; 209, call; 209, 210; 209, 211; 210, identifier:_set_archive_name; 211, argument_list; 211, 212; 211, 213; 211, 214; 211, 215; 211, 216; 212, identifier:package_name; 213, identifier:package_version; 214, identifier:python_versions; 215, identifier:platform; 216, identifier:build_tag; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:archive_path; 220, call; 220, 221; 220, 226; 221, attribute; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:os; 224, identifier:path; 225, identifier:join; 226, argument_list; 226, 227; 226, 228; 227, identifier:archive_destination_dir; 228, identifier:archive_name; 229, expression_statement; 229, 230; 230, call; 230, 231; 230, 232; 231, identifier:_handle_output_file; 232, argument_list; 232, 233; 232, 234; 233, identifier:archive_path; 234, identifier:force; 235, expression_statement; 235, 236; 236, call; 236, 237; 236, 238; 237, identifier:_generate_metadata_file; 238, argument_list; 238, 239; 238, 240; 238, 241; 238, 242; 238, 243; 238, 244; 238, 245; 238, 246; 238, 247; 239, identifier:workdir; 240, identifier:archive_name; 241, identifier:platform; 242, identifier:python_versions; 243, identifier:package_name; 244, identifier:package_version; 245, identifier:build_tag; 246, identifier:source; 247, identifier:wheels; 248, expression_statement; 248, 249; 249, call; 249, 250; 249, 251; 250, identifier:_create_wagon_archive; 251, argument_list; 251, 252; 251, 253; 251, 254; 252, identifier:workdir; 253, identifier:archive_path; 254, identifier:archive_format; 255, if_statement; 255, 256; 255, 258; 256, not_operator; 256, 257; 257, identifier:keep_wheels; 258, block; 258, 259; 258, 266; 259, expression_statement; 259, 260; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:logger; 263, identifier:debug; 264, argument_list; 264, 265; 265, string:'Removing work directory...'; 266, expression_statement; 266, 267; 267, call; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:shutil; 270, identifier:rmtree; 271, argument_list; 271, 272; 271, 273; 272, identifier:tempdir; 273, keyword_argument; 273, 274; 273, 275; 274, identifier:ignore_errors; 275, True; 276, if_statement; 276, 277; 276, 278; 277, identifier:validate_archive; 278, block; 278, 279; 279, expression_statement; 279, 280; 280, call; 280, 281; 280, 282; 281, identifier:validate; 282, argument_list; 282, 283; 283, identifier:archive_path; 284, expression_statement; 284, 285; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:logger; 288, identifier:info; 289, argument_list; 289, 290; 289, 291; 290, string:'Wagon created successfully at: %s'; 291, identifier:archive_path; 292, return_statement; 292, 293; 293, identifier:archive_path
def create(source, requirement_files=None, force=False, keep_wheels=False, archive_destination_dir='.', python_versions=None, validate_archive=False, wheel_args='', archive_format='zip', build_tag=''): """Create a Wagon archive and returns its path. Package name and version are extracted from the setup.py file of the `source` or from the PACKAGE_NAME==PACKAGE_VERSION if the source is a PyPI package. Supported `python_versions` must be in the format e.g [33, 27, 2, 3].. `force` will remove any excess dirs or archives before creation. `requirement_files` can be either a link/local path to a requirements.txt file or just `.`, in which case requirement files will be automatically extracted from either the GitHub archive URL or the local path provided provided in `source`. """ if validate_archive: _assert_virtualenv_is_installed() logger.info('Creating archive for %s...', source) processed_source = get_source(source) if os.path.isdir(processed_source) and not \ os.path.isfile(os.path.join(processed_source, 'setup.py')): raise WagonError( 'Source directory must contain a setup.py file') package_name, package_version = get_source_name_and_version( processed_source) tempdir = tempfile.mkdtemp() workdir = os.path.join(tempdir, package_name) wheels_path = os.path.join(workdir, DEFAULT_WHEELS_PATH) try: wheels = wheel( processed_source, requirement_files, wheels_path, wheel_args) finally: if processed_source != source: shutil.rmtree(processed_source, ignore_errors=True) platform = _get_platform_for_set_of_wheels(wheels_path) if is_verbose(): logger.debug('Platform is: %s', platform) python_versions = _set_python_versions(python_versions) if not os.path.isdir(archive_destination_dir): os.makedirs(archive_destination_dir) archive_name = _set_archive_name( package_name, package_version, python_versions, platform, build_tag) archive_path = os.path.join(archive_destination_dir, archive_name) _handle_output_file(archive_path, force) _generate_metadata_file( workdir, archive_name, platform, python_versions, package_name, package_version, build_tag, source, wheels) _create_wagon_archive(workdir, archive_path, archive_format) if not keep_wheels: logger.debug('Removing work directory...') shutil.rmtree(tempdir, ignore_errors=True) if validate_archive: validate(archive_path) logger.info('Wagon created successfully at: %s', archive_path) return archive_path
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:build_trading_timeline; 3, parameters; 3, 4; 3, 5; 4, identifier:start; 5, identifier:end; 6, block; 6, 7; 6, 9; 6, 26; 6, 41; 6, 333; 7, expression_statement; 7, 8; 8, string:''' Build the daily-based index we will trade on '''; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:EMPTY_DATES; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:pd; 15, identifier:date_range; 16, argument_list; 16, 17; 16, 18; 16, 21; 17, string:'2000/01/01'; 18, keyword_argument; 18, 19; 18, 20; 19, identifier:periods; 20, integer:0; 21, keyword_argument; 21, 22; 21, 23; 22, identifier:tz; 23, attribute; 23, 24; 23, 25; 24, identifier:pytz; 25, identifier:utc; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:now; 29, call; 29, 30; 29, 35; 30, attribute; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:dt; 33, identifier:datetime; 34, identifier:now; 35, argument_list; 35, 36; 36, keyword_argument; 36, 37; 36, 38; 37, identifier:tz; 38, attribute; 38, 39; 38, 40; 39, identifier:pytz; 40, identifier:utc; 41, if_statement; 41, 42; 41, 44; 41, 136; 42, not_operator; 42, 43; 43, identifier:start; 44, block; 44, 45; 45, if_statement; 45, 46; 45, 48; 45, 49; 45, 71; 46, not_operator; 46, 47; 47, identifier:end; 48, comment; 49, block; 49, 50; 49, 54; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:bt_dates; 53, identifier:EMPTY_DATES; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:live_dates; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:pd; 60, identifier:date_range; 61, argument_list; 61, 62; 61, 65; 62, keyword_argument; 62, 63; 62, 64; 63, identifier:start; 64, identifier:now; 65, keyword_argument; 65, 66; 65, 67; 66, identifier:end; 67, call; 67, 68; 67, 69; 68, identifier:normalize_date_format; 69, argument_list; 69, 70; 70, string:'23h59'; 71, else_clause; 71, 72; 72, block; 72, 73; 72, 80; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:end; 76, call; 76, 77; 76, 78; 77, identifier:normalize_date_format; 78, argument_list; 78, 79; 79, identifier:end; 80, if_statement; 80, 81; 80, 84; 80, 85; 80, 112; 81, comparison_operator:<; 81, 82; 81, 83; 82, identifier:end; 83, identifier:now; 84, comment; 85, block; 85, 86; 85, 108; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:bt_dates; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:pd; 92, identifier:date_range; 93, argument_list; 93, 94; 93, 105; 94, keyword_argument; 94, 95; 94, 96; 95, identifier:start; 96, binary_operator:-; 96, 97; 96, 98; 97, identifier:end; 98, binary_operator:*; 98, 99; 98, 100; 99, integer:360; 100, attribute; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:pd; 103, identifier:datetools; 104, identifier:day; 105, keyword_argument; 105, 106; 105, 107; 106, identifier:end; 107, identifier:end; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:live_dates; 111, identifier:EMPTY_DATES; 112, elif_clause; 112, 113; 112, 116; 112, 117; 113, comparison_operator:>; 113, 114; 113, 115; 114, identifier:end; 115, identifier:now; 116, comment; 117, block; 117, 118; 117, 122; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:bt_dates; 121, identifier:EMPTY_DATES; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:live_dates; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:pd; 128, identifier:date_range; 129, argument_list; 129, 130; 129, 133; 130, keyword_argument; 130, 131; 130, 132; 131, identifier:start; 132, identifier:now; 133, keyword_argument; 133, 134; 133, 135; 134, identifier:end; 135, identifier:end; 136, else_clause; 136, 137; 137, block; 137, 138; 137, 145; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:start; 141, call; 141, 142; 141, 143; 142, identifier:normalize_date_format; 143, argument_list; 143, 144; 144, identifier:start; 145, if_statement; 145, 146; 145, 149; 145, 274; 146, comparison_operator:<; 146, 147; 146, 148; 147, identifier:start; 148, identifier:now; 149, block; 149, 150; 150, if_statement; 150, 151; 150, 153; 150, 154; 150, 200; 151, not_operator; 151, 152; 152, identifier:end; 153, comment; 154, block; 154, 155; 154, 167; 154, 182; 154, 186; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:end; 158, binary_operator:+; 158, 159; 158, 160; 159, identifier:start; 160, binary_operator:*; 160, 161; 160, 162; 161, integer:360; 162, attribute; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:pd; 165, identifier:datetools; 166, identifier:day; 167, if_statement; 167, 168; 167, 171; 168, comparison_operator:>; 168, 169; 168, 170; 169, identifier:end; 170, identifier:now; 171, block; 171, 172; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:end; 175, binary_operator:-; 175, 176; 175, 177; 176, identifier:now; 177, attribute; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:pd; 180, identifier:datetools; 181, identifier:day; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:live_dates; 185, identifier:EMPTY_DATES; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:bt_dates; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:pd; 192, identifier:date_range; 193, argument_list; 193, 194; 193, 197; 194, keyword_argument; 194, 195; 194, 196; 195, identifier:start; 196, identifier:start; 197, keyword_argument; 197, 198; 197, 199; 198, identifier:end; 199, identifier:end; 200, else_clause; 200, 201; 201, block; 201, 202; 201, 209; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 205; 204, identifier:end; 205, call; 205, 206; 205, 207; 206, identifier:normalize_date_format; 207, argument_list; 207, 208; 208, identifier:end; 209, if_statement; 209, 210; 209, 213; 209, 214; 209, 233; 210, comparison_operator:<; 210, 211; 210, 212; 211, identifier:end; 212, identifier:now; 213, comment; 214, block; 214, 215; 214, 219; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:live_dates; 218, identifier:EMPTY_DATES; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 222; 221, identifier:bt_dates; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:pd; 225, identifier:date_range; 226, argument_list; 226, 227; 226, 230; 227, keyword_argument; 227, 228; 227, 229; 228, identifier:start; 229, identifier:start; 230, keyword_argument; 230, 231; 230, 232; 231, identifier:end; 232, identifier:end; 233, elif_clause; 233, 234; 233, 237; 233, 238; 233, 239; 234, comparison_operator:>; 234, 235; 234, 236; 235, identifier:end; 236, identifier:now; 237, comment; 238, comment; 239, block; 239, 240; 239, 260; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:bt_dates; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:pd; 246, identifier:date_range; 247, argument_list; 247, 248; 247, 251; 248, keyword_argument; 248, 249; 248, 250; 249, identifier:start; 250, identifier:start; 251, keyword_argument; 251, 252; 251, 253; 252, identifier:end; 253, binary_operator:-; 253, 254; 253, 255; 254, identifier:now; 255, attribute; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:pd; 258, identifier:datetools; 259, identifier:day; 260, expression_statement; 260, 261; 261, assignment; 261, 262; 261, 263; 262, identifier:live_dates; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:pd; 266, identifier:date_range; 267, argument_list; 267, 268; 267, 271; 268, keyword_argument; 268, 269; 268, 270; 269, identifier:start; 270, identifier:now; 271, keyword_argument; 271, 272; 271, 273; 272, identifier:end; 273, identifier:end; 274, elif_clause; 274, 275; 274, 278; 275, comparison_operator:>; 275, 276; 275, 277; 276, identifier:start; 277, identifier:now; 278, block; 278, 279; 279, if_statement; 279, 280; 279, 282; 279, 283; 279, 305; 280, not_operator; 280, 281; 281, identifier:end; 282, comment; 283, block; 283, 284; 283, 288; 284, expression_statement; 284, 285; 285, assignment; 285, 286; 285, 287; 286, identifier:bt_dates; 287, identifier:EMPTY_DATES; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 291; 290, identifier:live_dates; 291, call; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, identifier:pd; 294, identifier:date_range; 295, argument_list; 295, 296; 295, 299; 296, keyword_argument; 296, 297; 296, 298; 297, identifier:start; 298, identifier:start; 299, keyword_argument; 299, 300; 299, 301; 300, identifier:end; 301, call; 301, 302; 301, 303; 302, identifier:normalize_date_format; 303, argument_list; 303, 304; 304, string:'23h59'; 305, else_clause; 305, 306; 305, 307; 306, comment; 307, block; 307, 308; 307, 315; 307, 319; 308, expression_statement; 308, 309; 309, assignment; 309, 310; 309, 311; 310, identifier:end; 311, call; 311, 312; 311, 313; 312, identifier:normalize_date_format; 313, argument_list; 313, 314; 314, identifier:end; 315, expression_statement; 315, 316; 316, assignment; 316, 317; 316, 318; 317, identifier:bt_dates; 318, identifier:EMPTY_DATES; 319, expression_statement; 319, 320; 320, assignment; 320, 321; 320, 322; 321, identifier:live_dates; 322, call; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:pd; 325, identifier:date_range; 326, argument_list; 326, 327; 326, 330; 327, keyword_argument; 327, 328; 327, 329; 328, identifier:start; 329, identifier:start; 330, keyword_argument; 330, 331; 330, 332; 331, identifier:end; 332, identifier:end; 333, return_statement; 333, 334; 334, binary_operator:+; 334, 335; 334, 336; 335, identifier:bt_dates; 336, identifier:live_dates
def build_trading_timeline(start, end): ''' Build the daily-based index we will trade on ''' EMPTY_DATES = pd.date_range('2000/01/01', periods=0, tz=pytz.utc) now = dt.datetime.now(tz=pytz.utc) if not start: if not end: # Live trading until the end of the day bt_dates = EMPTY_DATES live_dates = pd.date_range( start=now, end=normalize_date_format('23h59')) else: end = normalize_date_format(end) if end < now: # Backtesting since a year before end bt_dates = pd.date_range( start=end - 360 * pd.datetools.day, end=end) live_dates = EMPTY_DATES elif end > now: # Live trading from now to end bt_dates = EMPTY_DATES live_dates = pd.date_range(start=now, end=end) else: start = normalize_date_format(start) if start < now: if not end: # Backtest for a year or until now end = start + 360 * pd.datetools.day if end > now: end = now - pd.datetools.day live_dates = EMPTY_DATES bt_dates = pd.date_range( start=start, end=end) else: end = normalize_date_format(end) if end < now: # Nothing to do, backtest from start to end live_dates = EMPTY_DATES bt_dates = pd.date_range(start=start, end=end) elif end > now: # Hybrid timeline, backtest from start to end, live # trade from now to end bt_dates = pd.date_range( start=start, end=now - pd.datetools.day) live_dates = pd.date_range(start=now, end=end) elif start > now: if not end: # Live trading from start to the end of the day bt_dates = EMPTY_DATES live_dates = pd.date_range( start=start, end=normalize_date_format('23h59')) else: # Live trading from start to end end = normalize_date_format(end) bt_dates = EMPTY_DATES live_dates = pd.date_range(start=start, end=end) return bt_dates + live_dates
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:gcal2jd; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:year; 5, identifier:month; 6, identifier:day; 7, block; 7, 8; 7, 10; 7, 17; 7, 24; 7, 31; 7, 43; 7, 60; 7, 79; 7, 93; 7, 105; 7, 111; 7, 112; 7, 116; 7, 117; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:year; 13, call; 13, 14; 13, 15; 14, identifier:int; 15, argument_list; 15, 16; 16, identifier:year; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:month; 20, call; 20, 21; 20, 22; 21, identifier:int; 22, argument_list; 22, 23; 23, identifier:month; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:day; 27, call; 27, 28; 27, 29; 28, identifier:int; 29, argument_list; 29, 30; 30, identifier:day; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:a; 34, call; 34, 35; 34, 36; 35, identifier:ipart; 36, argument_list; 36, 37; 37, binary_operator:/; 37, 38; 37, 42; 38, parenthesized_expression; 38, 39; 39, binary_operator:-; 39, 40; 39, 41; 40, identifier:month; 41, integer:14; 42, float:12.0; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:jd; 46, call; 46, 47; 46, 48; 47, identifier:ipart; 48, argument_list; 48, 49; 49, binary_operator:/; 49, 50; 49, 59; 50, parenthesized_expression; 50, 51; 51, binary_operator:*; 51, 52; 51, 53; 52, integer:1461; 53, parenthesized_expression; 53, 54; 54, binary_operator:+; 54, 55; 54, 58; 55, binary_operator:+; 55, 56; 55, 57; 56, identifier:year; 57, integer:4800; 58, identifier:a; 59, float:4.0; 60, expression_statement; 60, 61; 61, augmented_assignment:+=; 61, 62; 61, 63; 62, identifier:jd; 63, call; 63, 64; 63, 65; 64, identifier:ipart; 65, argument_list; 65, 66; 66, binary_operator:/; 66, 67; 66, 78; 67, parenthesized_expression; 67, 68; 68, binary_operator:*; 68, 69; 68, 70; 69, integer:367; 70, parenthesized_expression; 70, 71; 71, binary_operator:-; 71, 72; 71, 75; 72, binary_operator:-; 72, 73; 72, 74; 73, identifier:month; 74, integer:2; 75, binary_operator:*; 75, 76; 75, 77; 76, integer:12; 77, identifier:a; 78, float:12.0; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:x; 82, call; 82, 83; 82, 84; 83, identifier:ipart; 84, argument_list; 84, 85; 85, binary_operator:/; 85, 86; 85, 92; 86, parenthesized_expression; 86, 87; 87, binary_operator:+; 87, 88; 87, 91; 88, binary_operator:+; 88, 89; 88, 90; 89, identifier:year; 90, integer:4900; 91, identifier:a; 92, float:100.0; 93, expression_statement; 93, 94; 94, augmented_assignment:-=; 94, 95; 94, 96; 95, identifier:jd; 96, call; 96, 97; 96, 98; 97, identifier:ipart; 98, argument_list; 98, 99; 99, binary_operator:/; 99, 100; 99, 104; 100, parenthesized_expression; 100, 101; 101, binary_operator:*; 101, 102; 101, 103; 102, integer:3; 103, identifier:x; 104, float:4.0; 105, expression_statement; 105, 106; 106, augmented_assignment:+=; 106, 107; 106, 108; 107, identifier:jd; 108, binary_operator:-; 108, 109; 108, 110; 109, identifier:day; 110, float:2432075.5; 111, comment; 112, expression_statement; 112, 113; 113, augmented_assignment:-=; 113, 114; 113, 115; 114, identifier:jd; 115, float:0.5; 116, comment; 117, return_statement; 117, 118; 118, expression_list; 118, 119; 118, 120; 119, identifier:MJD_0; 120, identifier:jd
def gcal2jd(year, month, day): """Gregorian calendar date to Julian date. The input and output are for the proleptic Gregorian calendar, i.e., no consideration of historical usage of the calendar is made. Parameters ---------- year : int Year as an integer. month : int Month as an integer. day : int Day as an integer. Returns ------- jd1, jd2: 2-element tuple of floats When added together, the numbers give the Julian date for the given Gregorian calendar date. The first number is always MJD_0 i.e., 2451545.5. So the second is the MJD. Examples -------- >>> gcal2jd(2000,1,1) (2400000.5, 51544.0) >>> 2400000.5 + 51544.0 + 0.5 2451545.0 >>> year = [-4699, -2114, -1050, -123, -1, 0, 1, 123, 1678.0, 2000, ....: 2012, 2245] >>> month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] >>> day = [1, 12, 23, 14, 25, 16, 27, 8, 9, 10, 11, 31] >>> x = [gcal2jd(y, m, d) for y, m, d in zip(year, month, day)] >>> for i in x: print i (2400000.5, -2395215.0) (2400000.5, -1451021.0) (2400000.5, -1062364.0) (2400000.5, -723762.0) (2400000.5, -679162.0) (2400000.5, -678774.0) (2400000.5, -678368.0) (2400000.5, -633797.0) (2400000.5, -65812.0) (2400000.5, 51827.0) (2400000.5, 56242.0) (2400000.5, 141393.0) Negative months and days are valid. For example, 2000/-2/-4 => 1999/+12-2/-4 => 1999/10/-4 => 1999/9/30-4 => 1999/9/26. >>> gcal2jd(2000, -2, -4) (2400000.5, 51447.0) >>> gcal2jd(1999, 9, 26) (2400000.5, 51447.0) >>> gcal2jd(2000, 2, -1) (2400000.5, 51573.0) >>> gcal2jd(2000, 1, 30) (2400000.5, 51573.0) >>> gcal2jd(2000, 3, -1) (2400000.5, 51602.0) >>> gcal2jd(2000, 2, 28) (2400000.5, 51602.0) Month 0 becomes previous month. >>> gcal2jd(2000, 0, 1) (2400000.5, 51513.0) >>> gcal2jd(1999, 12, 1) (2400000.5, 51513.0) Day number 0 becomes last day of previous month. >>> gcal2jd(2000, 3, 0) (2400000.5, 51603.0) >>> gcal2jd(2000, 2, 29) (2400000.5, 51603.0) If `day` is greater than the number of days in `month`, then it gets carried over to the next month. >>> gcal2jd(2000,2,30) (2400000.5, 51604.0) >>> gcal2jd(2000,3,1) (2400000.5, 51604.0) >>> gcal2jd(2001,2,30) (2400000.5, 51970.0) >>> gcal2jd(2001,3,2) (2400000.5, 51970.0) Notes ----- The returned Julian date is for mid-night of the given date. To find the Julian date for any time of the day, simply add time as a fraction of a day. For example Julian date for mid-day can be obtained by adding 0.5 to either the first part or the second part. The latter is preferable, since it will give the MJD for the date and time. BC dates should be given as -(BC - 1) where BC is the year. For example 1 BC == 0, 2 BC == -1, and so on. Negative numbers can be used for `month` and `day`. For example 2000, -1, 1 is the same as 1999, 11, 1. The Julian dates are proleptic Julian dates, i.e., values are returned without considering if Gregorian dates are valid for the given date. The input values are truncated to integers. """ year = int(year) month = int(month) day = int(day) a = ipart((month - 14) / 12.0) jd = ipart((1461 * (year + 4800 + a)) / 4.0) jd += ipart((367 * (month - 2 - 12 * a)) / 12.0) x = ipart((year + 4900 + a) / 100.0) jd -= ipart((3 * x) / 4.0) jd += day - 2432075.5 # was 32075; add 2400000.5 jd -= 0.5 # 0 hours; above JD is for midday, switch to midnight. return MJD_0, jd
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:_check_restart_params; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:self; 5, identifier:restart_strategy; 6, identifier:min_beta; 7, identifier:s_greedy; 8, identifier:xi_restart; 9, block; 9, 10; 9, 12; 9, 19; 9, 33; 9, 48; 9, 62; 9, 76; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 16; 13, comparison_operator:is; 13, 14; 13, 15; 14, identifier:restart_strategy; 15, None; 16, block; 16, 17; 17, return_statement; 17, 18; 18, True; 19, if_statement; 19, 20; 19, 25; 20, comparison_operator:!=; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:mode; 24, string:'regular'; 25, block; 25, 26; 26, raise_statement; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:ValueError; 29, argument_list; 29, 30; 30, concatenated_string; 30, 31; 30, 32; 31, string:'Restarting strategies can only be used with '; 32, string:'regular mode.'; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:greedy_params_check; 36, parenthesized_expression; 36, 37; 37, boolean_operator:or; 37, 38; 37, 45; 38, boolean_operator:or; 38, 39; 38, 42; 39, comparison_operator:is; 39, 40; 39, 41; 40, identifier:min_beta; 41, None; 42, comparison_operator:is; 42, 43; 42, 44; 43, identifier:s_greedy; 44, None; 45, comparison_operator:<=; 45, 46; 45, 47; 46, identifier:s_greedy; 47, integer:1; 48, if_statement; 48, 49; 48, 54; 49, boolean_operator:and; 49, 50; 49, 53; 50, comparison_operator:==; 50, 51; 50, 52; 51, identifier:restart_strategy; 52, string:'greedy'; 53, identifier:greedy_params_check; 54, block; 54, 55; 55, raise_statement; 55, 56; 56, call; 56, 57; 56, 58; 57, identifier:ValueError; 58, argument_list; 58, 59; 59, concatenated_string; 59, 60; 59, 61; 60, string:'You need a min_beta and an s_greedy > 1 for '; 61, string:'greedy restart.'; 62, if_statement; 62, 63; 62, 70; 63, boolean_operator:or; 63, 64; 63, 67; 64, comparison_operator:is; 64, 65; 64, 66; 65, identifier:xi_restart; 66, None; 67, comparison_operator:>=; 67, 68; 67, 69; 68, identifier:xi_restart; 69, integer:1; 70, block; 70, 71; 71, raise_statement; 71, 72; 72, call; 72, 73; 72, 74; 73, identifier:ValueError; 74, argument_list; 74, 75; 75, string:'You need a xi_restart < 1 for restart.'; 76, return_statement; 76, 77; 77, True
def _check_restart_params(self, restart_strategy, min_beta, s_greedy, xi_restart): r""" Check restarting parameters This method checks that the restarting parameters are set and satisfy the correct assumptions. It also checks that the current mode is regular (as opposed to CD for now). Parameters ---------- restart_strategy: str or None name of the restarting strategy. If None, there is no restarting. Defaults to None. min_beta: float or None the minimum beta when using the greedy restarting strategy. Defaults to None. s_greedy: float or None. parameter for the safeguard comparison in the greedy restarting strategy. It has to be > 1. Defaults to None. xi_restart: float or None. mutlitplicative parameter for the update of beta in the greedy restarting strategy and for the update of r_lazy in the adaptive restarting strategies. It has to be > 1. Defaults to None. Returns ------- bool: True Raises ------ ValueError When a parameter that should be set isn't or doesn't verify the correct assumptions. """ if restart_strategy is None: return True if self.mode != 'regular': raise ValueError('Restarting strategies can only be used with ' 'regular mode.') greedy_params_check = (min_beta is None or s_greedy is None or s_greedy <= 1) if restart_strategy == 'greedy' and greedy_params_check: raise ValueError('You need a min_beta and an s_greedy > 1 for ' 'greedy restart.') if xi_restart is None or xi_restart >= 1: raise ValueError('You need a xi_restart < 1 for restart.') return True
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:method_cache; 3, parameters; 3, 4; 3, 7; 4, default_parameter; 4, 5; 4, 6; 5, identifier:by; 6, string:'value'; 7, default_parameter; 7, 8; 7, 9; 8, identifier:method; 9, string:'run'; 10, block; 10, 11; 10, 13; 10, 252; 11, expression_statement; 11, 12; 12, comment; 13, function_definition; 13, 14; 13, 15; 13, 17; 14, function_name:decorate_; 15, parameters; 15, 16; 16, identifier:func; 17, block; 17, 18; 17, 250; 18, function_definition; 18, 19; 18, 20; 18, 25; 19, function_name:decorate; 20, parameters; 20, 21; 20, 23; 21, list_splat_pattern; 21, 22; 22, identifier:args; 23, dictionary_splat_pattern; 23, 24; 24, identifier:kwargs; 25, block; 25, 26; 25, 32; 25, 33; 25, 42; 25, 68; 25, 88; 25, 96; 25, 176; 25, 242; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:model; 29, subscript; 29, 30; 29, 31; 30, identifier:args; 31, integer:0; 32, comment; 33, assert_statement; 33, 34; 33, 39; 34, call; 34, 35; 34, 36; 35, identifier:hasattr; 36, argument_list; 36, 37; 36, 38; 37, identifier:model; 38, identifier:method; 39, binary_operator:%; 39, 40; 39, 41; 40, string:"Model must have a '%s' method."; 41, identifier:method; 42, if_statement; 42, 43; 42, 48; 42, 49; 42, 54; 43, comparison_operator:==; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:func; 46, identifier:__name__; 47, identifier:method; 48, comment; 49, block; 49, 50; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:method_args; 53, identifier:kwargs; 54, else_clause; 54, 55; 54, 56; 55, comment; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:method_args; 60, conditional_expression:if; 60, 61; 60, 64; 60, 67; 61, subscript; 61, 62; 61, 63; 62, identifier:kwargs; 63, identifier:method; 64, comparison_operator:in; 64, 65; 64, 66; 65, identifier:method; 66, identifier:kwargs; 67, dictionary; 68, if_statement; 68, 69; 68, 77; 68, 78; 69, not_operator; 69, 70; 70, call; 70, 71; 70, 72; 71, identifier:hasattr; 72, argument_list; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:model; 75, identifier:__class__; 76, string:'cached_runs'; 77, comment; 78, block; 78, 79; 78, 87; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 86; 81, attribute; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:model; 84, identifier:__class__; 85, identifier:cached_runs; 86, dictionary; 87, comment; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:cache; 91, attribute; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:model; 94, identifier:__class__; 95, identifier:cached_runs; 96, if_statement; 96, 97; 96, 100; 96, 145; 96, 169; 97, comparison_operator:==; 97, 98; 97, 99; 98, identifier:by; 99, string:'value'; 100, block; 100, 101; 100, 129; 100, 144; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:model_dict; 104, dictionary_comprehension; 104, 105; 104, 108; 104, 122; 104, 123; 105, pair; 105, 106; 105, 107; 106, identifier:key; 107, identifier:value; 108, for_in_clause; 108, 109; 108, 112; 109, pattern_list; 109, 110; 109, 111; 110, identifier:key; 111, identifier:value; 112, call; 112, 113; 112, 114; 113, identifier:list; 114, argument_list; 114, 115; 115, call; 115, 116; 115, 121; 116, attribute; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:model; 119, identifier:__dict__; 120, identifier:items; 121, argument_list; 122, line_continuation:\; 123, if_clause; 123, 124; 124, comparison_operator:!=; 124, 125; 124, 128; 125, subscript; 125, 126; 125, 127; 126, identifier:key; 127, integer:0; 128, string:'_'; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:method_signature; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:SciUnit; 135, identifier:dict_hash; 136, argument_list; 136, 137; 137, dictionary; 137, 138; 137, 141; 138, pair; 138, 139; 138, 140; 139, string:'attrs'; 140, identifier:model_dict; 141, pair; 141, 142; 141, 143; 142, string:'args'; 143, identifier:method_args; 144, comment; 145, elif_clause; 145, 146; 145, 149; 146, comparison_operator:==; 146, 147; 146, 148; 147, identifier:by; 148, string:'instance'; 149, block; 149, 150; 149, 168; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:method_signature; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:SciUnit; 156, identifier:dict_hash; 157, argument_list; 157, 158; 158, dictionary; 158, 159; 158, 165; 159, pair; 159, 160; 159, 161; 160, string:'id'; 161, call; 161, 162; 161, 163; 162, identifier:id; 163, argument_list; 163, 164; 164, identifier:model; 165, pair; 165, 166; 165, 167; 166, string:'args'; 167, identifier:method_args; 168, comment; 169, else_clause; 169, 170; 170, block; 170, 171; 171, raise_statement; 171, 172; 172, call; 172, 173; 172, 174; 173, identifier:ValueError; 174, argument_list; 174, 175; 175, string:"Cache type must be 'value' or 'instance'"; 176, if_statement; 176, 177; 176, 180; 176, 218; 177, comparison_operator:not; 177, 178; 177, 179; 178, identifier:method_signature; 179, identifier:cache; 180, block; 180, 181; 180, 186; 180, 194; 180, 200; 181, expression_statement; 181, 182; 182, call; 182, 183; 182, 184; 183, identifier:print; 184, argument_list; 184, 185; 185, string:"Method with this signature not found in the cache. Running..."; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:f; 189, call; 189, 190; 189, 191; 190, identifier:getattr; 191, argument_list; 191, 192; 191, 193; 192, identifier:model; 193, identifier:method; 194, expression_statement; 194, 195; 195, call; 195, 196; 195, 197; 196, identifier:f; 197, argument_list; 197, 198; 198, dictionary_splat; 198, 199; 199, identifier:method_args; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 205; 202, subscript; 202, 203; 202, 204; 203, identifier:cache; 204, identifier:method_signature; 205, tuple; 205, 206; 205, 211; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:datetime; 209, identifier:now; 210, argument_list; 211, call; 211, 212; 211, 217; 212, attribute; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:model; 215, identifier:__dict__; 216, identifier:copy; 217, argument_list; 218, else_clause; 218, 219; 219, block; 219, 220; 219, 225; 219, 233; 220, expression_statement; 220, 221; 221, call; 221, 222; 221, 223; 222, identifier:print; 223, argument_list; 223, 224; 224, string:"Method with this signature found in the cache. Restoring..."; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 230; 227, pattern_list; 227, 228; 227, 229; 228, identifier:_; 229, identifier:attrs; 230, subscript; 230, 231; 230, 232; 231, identifier:cache; 232, identifier:method_signature; 233, expression_statement; 233, 234; 234, call; 234, 235; 234, 240; 235, attribute; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:model; 238, identifier:__dict__; 239, identifier:update; 240, argument_list; 240, 241; 241, identifier:attrs; 242, return_statement; 242, 243; 243, call; 243, 244; 243, 245; 244, identifier:func; 245, argument_list; 245, 246; 245, 248; 246, list_splat; 246, 247; 247, identifier:args; 248, dictionary_splat; 248, 249; 249, identifier:kwargs; 250, return_statement; 250, 251; 251, identifier:decorate; 252, return_statement; 252, 253; 253, identifier:decorate_
def method_cache(by='value',method='run'): """A decorator used on any model method which calls the model's 'method' method if that latter method has not been called using the current arguments or simply sets model attributes to match the run results if it has.""" def decorate_(func): def decorate(*args, **kwargs): model = args[0] # Assumed to be self. assert hasattr(model,method), "Model must have a '%s' method."%method if func.__name__ == method: # Run itself. method_args = kwargs else: # Any other method. method_args = kwargs[method] if method in kwargs else {} if not hasattr(model.__class__,'cached_runs'): # If there is no run cache. model.__class__.cached_runs = {} # Create the method cache. cache = model.__class__.cached_runs if by == 'value': model_dict = {key:value for key,value in list(model.__dict__.items()) \ if key[0]!='_'} method_signature = SciUnit.dict_hash({'attrs':model_dict,'args':method_args}) # Hash key. elif by == 'instance': method_signature = SciUnit.dict_hash({'id':id(model),'args':method_args}) # Hash key. else: raise ValueError("Cache type must be 'value' or 'instance'") if method_signature not in cache: print("Method with this signature not found in the cache. Running...") f = getattr(model,method) f(**method_args) cache[method_signature] = (datetime.now(),model.__dict__.copy()) else: print("Method with this signature found in the cache. Restoring...") _,attrs = cache[method_signature] model.__dict__.update(attrs) return func(*args, **kwargs) return decorate return decorate_
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:_format_command; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:ctx; 5, identifier:show_nested; 6, default_parameter; 6, 7; 6, 8; 7, identifier:commands; 8, None; 9, block; 9, 10; 9, 12; 9, 13; 9, 24; 9, 25; 9, 35; 9, 45; 9, 46; 9, 56; 9, 57; 9, 67; 9, 78; 9, 85; 9, 86; 9, 96; 9, 105; 9, 112; 9, 113; 9, 123; 9, 132; 9, 139; 9, 140; 9, 144; 9, 152; 9, 161; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, if_statement; 13, 14; 13, 22; 14, call; 14, 15; 14, 16; 15, identifier:getattr; 16, argument_list; 16, 17; 16, 20; 16, 21; 17, attribute; 17, 18; 17, 19; 18, identifier:ctx; 19, identifier:command; 20, string:'hidden'; 21, False; 22, block; 22, 23; 23, return_statement; 24, comment; 25, for_statement; 25, 26; 25, 27; 25, 31; 26, identifier:line; 27, call; 27, 28; 27, 29; 28, identifier:_format_description; 29, argument_list; 29, 30; 30, identifier:ctx; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, yield; 33, 34; 34, identifier:line; 35, expression_statement; 35, 36; 36, yield; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, string:'.. program:: {}'; 40, identifier:format; 41, argument_list; 41, 42; 42, attribute; 42, 43; 42, 44; 43, identifier:ctx; 44, identifier:command_path; 45, comment; 46, for_statement; 46, 47; 46, 48; 46, 52; 47, identifier:line; 48, call; 48, 49; 48, 50; 49, identifier:_format_usage; 50, argument_list; 50, 51; 51, identifier:ctx; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, yield; 54, 55; 55, identifier:line; 56, comment; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:lines; 60, call; 60, 61; 60, 62; 61, identifier:list; 62, argument_list; 62, 63; 63, call; 63, 64; 63, 65; 64, identifier:_format_options; 65, argument_list; 65, 66; 66, identifier:ctx; 67, if_statement; 67, 68; 67, 69; 67, 70; 67, 71; 68, identifier:lines; 69, comment; 70, comment; 71, block; 71, 72; 71, 75; 72, expression_statement; 72, 73; 73, yield; 73, 74; 74, string:'.. rubric:: Options'; 75, expression_statement; 75, 76; 76, yield; 76, 77; 77, string:''; 78, for_statement; 78, 79; 78, 80; 78, 81; 79, identifier:line; 80, identifier:lines; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, yield; 83, 84; 84, identifier:line; 85, comment; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:lines; 89, call; 89, 90; 89, 91; 90, identifier:list; 91, argument_list; 91, 92; 92, call; 92, 93; 92, 94; 93, identifier:_format_arguments; 94, argument_list; 94, 95; 95, identifier:ctx; 96, if_statement; 96, 97; 96, 98; 97, identifier:lines; 98, block; 98, 99; 98, 102; 99, expression_statement; 99, 100; 100, yield; 100, 101; 101, string:'.. rubric:: Arguments'; 102, expression_statement; 102, 103; 103, yield; 103, 104; 104, string:''; 105, for_statement; 105, 106; 105, 107; 105, 108; 106, identifier:line; 107, identifier:lines; 108, block; 108, 109; 109, expression_statement; 109, 110; 110, yield; 110, 111; 111, identifier:line; 112, comment; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:lines; 116, call; 116, 117; 116, 118; 117, identifier:list; 118, argument_list; 118, 119; 119, call; 119, 120; 119, 121; 120, identifier:_format_envvars; 121, argument_list; 121, 122; 122, identifier:ctx; 123, if_statement; 123, 124; 123, 125; 124, identifier:lines; 125, block; 125, 126; 125, 129; 126, expression_statement; 126, 127; 127, yield; 127, 128; 128, string:'.. rubric:: Environment variables'; 129, expression_statement; 129, 130; 130, yield; 130, 131; 131, string:''; 132, for_statement; 132, 133; 132, 134; 132, 135; 133, identifier:line; 134, identifier:lines; 135, block; 135, 136; 136, expression_statement; 136, 137; 137, yield; 137, 138; 138, identifier:line; 139, comment; 140, if_statement; 140, 141; 140, 142; 141, identifier:show_nested; 142, block; 142, 143; 143, return_statement; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:commands; 147, call; 147, 148; 147, 149; 148, identifier:_filter_commands; 149, argument_list; 149, 150; 149, 151; 150, identifier:ctx; 151, identifier:commands; 152, if_statement; 152, 153; 152, 154; 153, identifier:commands; 154, block; 154, 155; 154, 158; 155, expression_statement; 155, 156; 156, yield; 156, 157; 157, string:'.. rubric:: Commands'; 158, expression_statement; 158, 159; 159, yield; 159, 160; 160, string:''; 161, for_statement; 161, 162; 161, 163; 161, 164; 161, 165; 162, identifier:command; 163, identifier:commands; 164, comment; 165, block; 165, 166; 165, 179; 165, 189; 166, if_statement; 166, 167; 166, 172; 167, comparison_operator:>=; 167, 168; 167, 169; 168, identifier:CLICK_VERSION; 169, tuple; 169, 170; 169, 171; 170, integer:7; 171, integer:0; 172, block; 172, 173; 173, if_statement; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:command; 176, identifier:hidden; 177, block; 177, 178; 178, continue_statement; 179, for_statement; 179, 180; 179, 181; 179, 185; 180, identifier:line; 181, call; 181, 182; 181, 183; 182, identifier:_format_subcommand; 183, argument_list; 183, 184; 184, identifier:command; 185, block; 185, 186; 186, expression_statement; 186, 187; 187, yield; 187, 188; 188, identifier:line; 189, expression_statement; 189, 190; 190, yield; 190, 191; 191, string:''
def _format_command(ctx, show_nested, commands=None): """Format the output of `click.Command`.""" # the hidden attribute is part of click 7.x only hence use of getattr if getattr(ctx.command, 'hidden', False): return # description for line in _format_description(ctx): yield line yield '.. program:: {}'.format(ctx.command_path) # usage for line in _format_usage(ctx): yield line # options lines = list(_format_options(ctx)) if lines: # we use rubric to provide some separation without exploding the table # of contents yield '.. rubric:: Options' yield '' for line in lines: yield line # arguments lines = list(_format_arguments(ctx)) if lines: yield '.. rubric:: Arguments' yield '' for line in lines: yield line # environment variables lines = list(_format_envvars(ctx)) if lines: yield '.. rubric:: Environment variables' yield '' for line in lines: yield line # if we're nesting commands, we need to do this slightly differently if show_nested: return commands = _filter_commands(ctx, commands) if commands: yield '.. rubric:: Commands' yield '' for command in commands: # Don't show hidden subcommands if CLICK_VERSION >= (7, 0): if command.hidden: continue for line in _format_subcommand(command): yield line yield ''
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 22; 2, function_name:_formatter; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:x; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:y; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:z; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:s; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:label; 19, None; 20, dictionary_splat_pattern; 20, 21; 21, identifier:kwargs; 22, block; 22, 23; 22, 25; 22, 55; 22, 79; 22, 89; 22, 90; 22, 142; 22, 194; 22, 198; 22, 262; 22, 263; 22, 264; 22, 287; 22, 314; 22, 348; 23, expression_statement; 23, 24; 24, comment; 25, function_definition; 25, 26; 25, 27; 25, 29; 26, function_name:is_date; 27, parameters; 27, 28; 28, identifier:axis; 29, block; 29, 30; 29, 38; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:fmt; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:axis; 36, identifier:get_major_formatter; 37, argument_list; 38, return_statement; 38, 39; 39, parenthesized_expression; 39, 40; 40, boolean_operator:or; 40, 41; 40, 48; 41, call; 41, 42; 41, 43; 42, identifier:isinstance; 43, argument_list; 43, 44; 43, 45; 44, identifier:fmt; 45, attribute; 45, 46; 45, 47; 46, identifier:mdates; 47, identifier:DateFormatter; 48, call; 48, 49; 48, 50; 49, identifier:isinstance; 50, argument_list; 50, 51; 50, 52; 51, identifier:fmt; 52, attribute; 52, 53; 52, 54; 53, identifier:mdates; 54, identifier:AutoDateFormatter; 55, function_definition; 55, 56; 55, 57; 55, 59; 56, function_name:format_date; 57, parameters; 57, 58; 58, identifier:num; 59, block; 59, 60; 60, if_statement; 60, 61; 60, 64; 61, comparison_operator:is; 61, 62; 61, 63; 62, identifier:num; 63, None; 64, block; 64, 65; 65, return_statement; 65, 66; 66, call; 66, 67; 66, 75; 67, attribute; 67, 68; 67, 74; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:mdates; 71, identifier:num2date; 72, argument_list; 72, 73; 73, identifier:num; 74, identifier:strftime; 75, argument_list; 75, 76; 76, attribute; 76, 77; 76, 78; 77, identifier:self; 78, identifier:date_format; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:ax; 82, attribute; 82, 83; 82, 88; 83, attribute; 83, 84; 83, 87; 84, subscript; 84, 85; 84, 86; 85, identifier:kwargs; 86, string:'event'; 87, identifier:artist; 88, identifier:axes; 89, comment; 90, if_statement; 90, 91; 90, 97; 90, 105; 91, call; 91, 92; 91, 93; 92, identifier:is_date; 93, argument_list; 93, 94; 94, attribute; 94, 95; 94, 96; 95, identifier:ax; 96, identifier:xaxis; 97, block; 97, 98; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:x; 101, call; 101, 102; 101, 103; 102, identifier:format_date; 103, argument_list; 103, 104; 104, identifier:x; 105, else_clause; 105, 106; 106, block; 106, 107; 106, 115; 106, 125; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:limits; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:ax; 113, identifier:get_xlim; 114, argument_list; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:x; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:self; 121, identifier:_format_coord; 122, argument_list; 122, 123; 122, 124; 123, identifier:x; 124, identifier:limits; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 130; 127, subscript; 127, 128; 127, 129; 128, identifier:kwargs; 129, string:'xerror'; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:_format_coord; 134, argument_list; 134, 135; 134, 141; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:kwargs; 138, identifier:get; 139, argument_list; 139, 140; 140, string:'xerror'; 141, identifier:limits; 142, if_statement; 142, 143; 142, 149; 142, 157; 143, call; 143, 144; 143, 145; 144, identifier:is_date; 145, argument_list; 145, 146; 146, attribute; 146, 147; 146, 148; 147, identifier:ax; 148, identifier:yaxis; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:y; 153, call; 153, 154; 153, 155; 154, identifier:format_date; 155, argument_list; 155, 156; 156, identifier:y; 157, else_clause; 157, 158; 158, block; 158, 159; 158, 167; 158, 177; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:limits; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:ax; 165, identifier:get_ylim; 166, argument_list; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:y; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:self; 173, identifier:_format_coord; 174, argument_list; 174, 175; 174, 176; 175, identifier:y; 176, identifier:limits; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 182; 179, subscript; 179, 180; 179, 181; 180, identifier:kwargs; 181, string:'yerror'; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:self; 185, identifier:_format_coord; 186, argument_list; 186, 187; 186, 193; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:kwargs; 190, identifier:get; 191, argument_list; 191, 192; 192, string:'yerror'; 193, identifier:limits; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:output; 197, list:[]; 198, for_statement; 198, 199; 198, 202; 198, 215; 199, pattern_list; 199, 200; 199, 201; 200, identifier:key; 201, identifier:val; 202, call; 202, 203; 202, 204; 203, identifier:zip; 204, argument_list; 204, 205; 204, 210; 205, list:['x', 'y', 'z', 's']; 205, 206; 205, 207; 205, 208; 205, 209; 206, string:'x'; 207, string:'y'; 208, string:'z'; 209, string:'s'; 210, list:[x, y, z, s]; 210, 211; 210, 212; 210, 213; 210, 214; 211, identifier:x; 212, identifier:y; 213, identifier:z; 214, identifier:s; 215, block; 215, 216; 216, if_statement; 216, 217; 216, 220; 217, comparison_operator:is; 217, 218; 217, 219; 218, identifier:val; 219, None; 220, block; 220, 221; 221, try_statement; 221, 222; 221, 240; 222, block; 222, 223; 223, expression_statement; 223, 224; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:output; 227, identifier:append; 228, argument_list; 228, 229; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, string:u'{key}: {val:0.3g}'; 232, identifier:format; 233, argument_list; 233, 234; 233, 237; 234, keyword_argument; 234, 235; 234, 236; 235, identifier:key; 236, identifier:key; 237, keyword_argument; 237, 238; 237, 239; 238, identifier:val; 239, identifier:val; 240, except_clause; 240, 241; 240, 242; 240, 243; 240, 244; 241, identifier:ValueError; 242, comment; 243, comment; 244, block; 244, 245; 245, expression_statement; 245, 246; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:output; 249, identifier:append; 250, argument_list; 250, 251; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, string:u'{key}: {val}'; 254, identifier:format; 255, argument_list; 255, 256; 255, 259; 256, keyword_argument; 256, 257; 256, 258; 257, identifier:key; 258, identifier:key; 259, keyword_argument; 259, 260; 259, 261; 260, identifier:val; 261, identifier:val; 262, comment; 263, comment; 264, if_statement; 264, 265; 264, 274; 265, boolean_operator:and; 265, 266; 265, 267; 266, identifier:label; 267, not_operator; 267, 268; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:label; 271, identifier:startswith; 272, argument_list; 272, 273; 273, string:'_'; 274, block; 274, 275; 275, expression_statement; 275, 276; 276, call; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:output; 279, identifier:append; 280, argument_list; 280, 281; 281, call; 281, 282; 281, 285; 282, attribute; 282, 283; 282, 284; 283, string:u'Label: {}'; 284, identifier:format; 285, argument_list; 285, 286; 286, identifier:label; 287, if_statement; 287, 288; 287, 297; 288, comparison_operator:is; 288, 289; 288, 296; 289, call; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:kwargs; 292, identifier:get; 293, argument_list; 293, 294; 293, 295; 294, string:u'point_label'; 295, None; 296, None; 297, block; 297, 298; 298, expression_statement; 298, 299; 299, call; 299, 300; 299, 303; 300, attribute; 300, 301; 300, 302; 301, identifier:output; 302, identifier:append; 303, argument_list; 303, 304; 304, binary_operator:+; 304, 305; 304, 306; 305, string:u'Point: '; 306, call; 306, 307; 306, 310; 307, attribute; 307, 308; 307, 309; 308, string:u', '; 309, identifier:join; 310, argument_list; 310, 311; 311, subscript; 311, 312; 311, 313; 312, identifier:kwargs; 313, string:'point_label'; 314, for_statement; 314, 315; 314, 316; 314, 319; 315, identifier:arg; 316, list:['xerror', 'yerror']; 316, 317; 316, 318; 317, string:'xerror'; 318, string:'yerror'; 319, block; 319, 320; 319, 330; 320, expression_statement; 320, 321; 321, assignment; 321, 322; 321, 323; 322, identifier:val; 323, call; 323, 324; 323, 327; 324, attribute; 324, 325; 324, 326; 325, identifier:kwargs; 326, identifier:get; 327, argument_list; 327, 328; 327, 329; 328, identifier:arg; 329, None; 330, if_statement; 330, 331; 330, 334; 331, comparison_operator:is; 331, 332; 331, 333; 332, identifier:val; 333, None; 334, block; 334, 335; 335, expression_statement; 335, 336; 336, call; 336, 337; 336, 340; 337, attribute; 337, 338; 337, 339; 338, identifier:output; 339, identifier:append; 340, argument_list; 340, 341; 341, call; 341, 342; 341, 345; 342, attribute; 342, 343; 342, 344; 343, string:u'{}: {}'; 344, identifier:format; 345, argument_list; 345, 346; 345, 347; 346, identifier:arg; 347, identifier:val; 348, return_statement; 348, 349; 349, call; 349, 350; 349, 353; 350, attribute; 350, 351; 350, 352; 351, string:u'\n'; 352, identifier:join; 353, argument_list; 353, 354; 354, identifier:output
def _formatter(self, x=None, y=None, z=None, s=None, label=None, **kwargs): """ Default formatter function, if no `formatter` kwarg is specified. Takes information about the pick event as a series of kwargs and returns the string to be displayed. """ def is_date(axis): fmt = axis.get_major_formatter() return (isinstance(fmt, mdates.DateFormatter) or isinstance(fmt, mdates.AutoDateFormatter)) def format_date(num): if num is not None: return mdates.num2date(num).strftime(self.date_format) ax = kwargs['event'].artist.axes # Display x and y with range-specific formatting if is_date(ax.xaxis): x = format_date(x) else: limits = ax.get_xlim() x = self._format_coord(x, limits) kwargs['xerror'] = self._format_coord(kwargs.get('xerror'), limits) if is_date(ax.yaxis): y = format_date(y) else: limits = ax.get_ylim() y = self._format_coord(y, limits) kwargs['yerror'] = self._format_coord(kwargs.get('yerror'), limits) output = [] for key, val in zip(['x', 'y', 'z', 's'], [x, y, z, s]): if val is not None: try: output.append(u'{key}: {val:0.3g}'.format(key=key, val=val)) except ValueError: # X & Y will be strings at this point. # For masked arrays, etc, "z" and s values may be a string output.append(u'{key}: {val}'.format(key=key, val=val)) # label may be None or an empty string (for an un-labeled AxesImage)... # Un-labeled Line2D's will have labels that start with an underscore if label and not label.startswith('_'): output.append(u'Label: {}'.format(label)) if kwargs.get(u'point_label', None) is not None: output.append(u'Point: ' + u', '.join(kwargs['point_label'])) for arg in ['xerror', 'yerror']: val = kwargs.get(arg, None) if val is not None: output.append(u'{}: {}'.format(arg, val)) return u'\n'.join(output)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:datacursor; 3, parameters; 3, 4; 3, 7; 3, 10; 4, default_parameter; 4, 5; 4, 6; 5, identifier:artists; 6, None; 7, default_parameter; 7, 8; 7, 9; 8, identifier:axes; 9, None; 10, dictionary_splat_pattern; 10, 11; 11, identifier:kwargs; 12, block; 12, 13; 12, 15; 12, 45; 12, 46; 12, 86; 12, 100; 12, 101; 12, 102; 12, 121; 13, expression_statement; 13, 14; 14, comment; 15, function_definition; 15, 16; 15, 17; 15, 19; 16, function_name:plotted_artists; 17, parameters; 17, 18; 18, identifier:ax; 19, block; 19, 20; 19, 43; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:artists; 23, parenthesized_expression; 23, 24; 24, binary_operator:+; 24, 25; 24, 40; 25, binary_operator:+; 25, 26; 25, 37; 26, binary_operator:+; 26, 27; 26, 34; 27, binary_operator:+; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:ax; 30, identifier:lines; 31, attribute; 31, 32; 31, 33; 32, identifier:ax; 33, identifier:patches; 34, attribute; 34, 35; 34, 36; 35, identifier:ax; 36, identifier:collections; 37, attribute; 37, 38; 37, 39; 38, identifier:ax; 39, identifier:images; 40, attribute; 40, 41; 40, 42; 41, identifier:ax; 42, identifier:containers; 43, return_statement; 43, 44; 44, identifier:artists; 45, comment; 46, if_statement; 46, 47; 46, 50; 47, comparison_operator:is; 47, 48; 47, 49; 48, identifier:axes; 49, None; 50, block; 50, 51; 50, 61; 50, 73; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:managers; 54, call; 54, 55; 54, 60; 55, attribute; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:pylab_helpers; 58, identifier:Gcf; 59, identifier:get_all_fig_managers; 60, argument_list; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:figs; 64, list_comprehension; 64, 65; 64, 70; 65, attribute; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:manager; 68, identifier:canvas; 69, identifier:figure; 70, for_in_clause; 70, 71; 70, 72; 71, identifier:manager; 72, identifier:managers; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:axes; 76, list_comprehension; 76, 77; 76, 78; 76, 81; 77, identifier:ax; 78, for_in_clause; 78, 79; 78, 80; 79, identifier:fig; 80, identifier:figs; 81, for_in_clause; 81, 82; 81, 83; 82, identifier:ax; 83, attribute; 83, 84; 83, 85; 84, identifier:fig; 85, identifier:axes; 86, if_statement; 86, 87; 86, 94; 87, not_operator; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:cbook; 91, identifier:iterable; 92, argument_list; 92, 93; 93, identifier:axes; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:axes; 98, list:[axes]; 98, 99; 99, identifier:axes; 100, comment; 101, comment; 102, if_statement; 102, 103; 102, 106; 103, comparison_operator:is; 103, 104; 103, 105; 104, identifier:artists; 105, None; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:artists; 110, list_comprehension; 110, 111; 110, 112; 110, 115; 111, identifier:artist; 112, for_in_clause; 112, 113; 112, 114; 113, identifier:ax; 114, identifier:axes; 115, for_in_clause; 115, 116; 115, 117; 116, identifier:artist; 117, call; 117, 118; 117, 119; 118, identifier:plotted_artists; 119, argument_list; 119, 120; 120, identifier:ax; 121, return_statement; 121, 122; 122, call; 122, 123; 122, 124; 123, identifier:DataCursor; 124, argument_list; 124, 125; 124, 126; 125, identifier:artists; 126, dictionary_splat; 126, 127; 127, identifier:kwargs
def datacursor(artists=None, axes=None, **kwargs): """ Create an interactive data cursor for the specified artists or specified axes. The data cursor displays information about a selected artist in a "popup" annotation box. If a specific sequence of artists is given, only the specified artists will be interactively selectable. Otherwise, all manually-plotted artists in *axes* will be used (*axes* defaults to all axes in all figures). Parameters ----------- artists : a matplotlib artist or sequence of artists, optional The artists to make selectable and display information for. If this is not specified, then all manually plotted artists in `axes` will be used. axes : a matplotlib axes of sequence of axes, optional The axes to selected artists from if a sequence of artists is not specified. If `axes` is not specified, then all available axes in all figures will be used. tolerance : number, optional The radius (in points) that the mouse click must be within to select the artist. Default: 5 points. formatter : callable, optional A function that accepts arbitrary kwargs and returns a string that will be displayed with annotate. Often, it is convienent to pass in the format method of a template string, e.g. ``formatter="{label}".format``. Keyword arguments passed in to the `formatter` function: `x`, `y` : floats The x and y data coordinates of the clicked point `event` : a matplotlib ``PickEvent`` The pick event that was fired (note that the selected artist can be accessed through ``event.artist``). `label` : string or None The legend label of the selected artist. `ind` : list of ints or None If the artist has "subitems" (e.g. points in a scatter or line plot), this will be a list of the item(s) that were clicked on. If the artist does not have "subitems", this will be None. Note that this is always a list, even when a single item is selected. Some selected artists may supply additional keyword arguments that are not always present, for example: `z` : number The "z" (usually color or array) value, if present. For an ``AxesImage`` (as created by ``imshow``), this will be the uninterpolated array value at the point clicked. For a ``PathCollection`` (as created by ``scatter``) this will be the "c" value if an array was passed to "c". `i`, `j` : ints The row, column indicies of the selected point for an ``AxesImage`` (as created by ``imshow``) `s` : number The size of the selected item in a ``PathCollection`` if a size array is specified. `c` : number The array value displayed as color for a ``PathCollection`` if a "c" array is specified (identical to "z"). `point_label` : list If `point_labels` is given when the data cursor is initialized and the artist has "subitems", this will be a list of the items of `point_labels` that correspond to the selected artists. Note that this is always a list, even when a single artist is selected. `width`, `height`, `top`, `bottom` : numbers The parameters for ``Rectangle`` artists (e.g. bar plots). point_labels : sequence or dict, optional For artists with "subitems" (e.g. Line2D's), the item(s) of `point_labels` corresponding to the selected "subitems" of the artist will be passed into the formatter function as the "point_label" kwarg. If a single sequence is given, it will be used for all artists with "subitems". Alternatively, a dict of artist:sequence pairs may be given to match an artist to the correct series of point labels. display : {"one-per-axes", "single", "multiple"}, optional Controls whether more than one annotation box will be shown. Default: "one-per-axes" draggable : boolean, optional Controls whether or not the annotation box will be interactively draggable to a new location after being displayed. Defaults to False. hover : boolean, optional If True, the datacursor will "pop up" when the mouse hovers over an artist. Defaults to False. Enabling hover also sets `display="single"` and `draggable=False`. props_override : function, optional If specified, this function customizes the parameters passed into the formatter function and the x, y location that the datacursor "pop up" "points" to. This is often useful to make the annotation "point" to a specific side or corner of an artist, regardless of the position clicked. The function is passed the same kwargs as the `formatter` function and is expected to return a dict with at least the keys "x" and "y" (and probably several others). Expected call signature: `props_dict = props_override(**kwargs)` keybindings : boolean or dict, optional By default, the keys "d" and "t" will be bound to deleting/hiding all annotation boxes and toggling interactivity for datacursors, respectively. If keybindings is False, the ability to hide/toggle datacursors interactively will be disabled. Alternatively, a dict of the form {'hide':'somekey', 'toggle':'somekey'} may specified to customize the keyboard shortcuts. date_format : string, optional The strftime-style formatting string for dates. Used only if the x or y axes have been set to display dates. Defaults to "%x %X". display_button: int, optional The mouse button that will triggers displaying an annotation box. Defaults to 1, for left-clicking. (Common options are 1:left-click, 2:middle-click, 3:right-click) hide_button: int or None, optional The mouse button that triggers hiding the selected annotation box. Defaults to 3, for right-clicking. (Common options are 1:left-click, 2:middle-click, 3:right-click, None:hiding disabled) keep_inside : boolean, optional Whether or not to adjust the x,y offset to keep the text box inside the figure. This option has no effect on draggable datacursors. Defaults to True. Note: Currently disabled on OSX and NbAgg/notebook backends. **kwargs : additional keyword arguments, optional Additional keyword arguments are passed on to annotate. Returns ------- dc : A ``mpldatacursor.DataCursor`` instance """ def plotted_artists(ax): artists = (ax.lines + ax.patches + ax.collections + ax.images + ax.containers) return artists # If no axes are specified, get all axes. if axes is None: managers = pylab_helpers.Gcf.get_all_fig_managers() figs = [manager.canvas.figure for manager in managers] axes = [ax for fig in figs for ax in fig.axes] if not cbook.iterable(axes): axes = [axes] # If no artists are specified, get all manually plotted artists in all of # the specified axes. if artists is None: artists = [artist for ax in axes for artist in plotted_artists(ax)] return DataCursor(artists, **kwargs)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:wait_for_notification; 3, parameters; 3, 4; 3, 5; 3, 9; 3, 10; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:handle; 7, type; 7, 8; 8, identifier:int; 9, identifier:delegate; 10, typed_parameter; 10, 11; 10, 12; 11, identifier:notification_timeout; 12, type; 12, 13; 13, identifier:float; 14, block; 14, 15; 14, 17; 14, 30; 14, 34; 14, 38; 14, 48; 14, 293; 15, expression_statement; 15, 16; 16, comment; 17, if_statement; 17, 18; 17, 24; 18, not_operator; 18, 19; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:is_connected; 23, argument_list; 24, block; 24, 25; 25, raise_statement; 25, 26; 26, call; 26, 27; 26, 28; 27, identifier:BluetoothBackendException; 28, argument_list; 28, 29; 29, string:'Not connected to any device.'; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:attempt; 33, integer:0; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:delay; 37, integer:10; 38, expression_statement; 38, 39; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:_LOGGER; 42, identifier:debug; 43, argument_list; 43, 44; 43, 45; 44, string:"Enter write_ble (%s)"; 45, call; 45, 46; 45, 47; 46, identifier:current_thread; 47, argument_list; 48, while_statement; 48, 49; 48, 54; 49, comparison_operator:<=; 49, 50; 49, 51; 50, identifier:attempt; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:retries; 54, block; 54, 55; 54, 86; 54, 95; 54, 175; 54, 189; 54, 204; 54, 212; 54, 213; 54, 263; 54, 267; 54, 275; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:cmd; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, string:"gatttool --device={} --addr-type={} --char-write-req -a {} -n {} --adapter={} --listen"; 61, identifier:format; 62, argument_list; 62, 63; 62, 66; 62, 69; 62, 75; 62, 83; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:_mac; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:address_type; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:byte_to_handle; 73, argument_list; 73, 74; 74, identifier:handle; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:self; 78, identifier:bytes_to_string; 79, argument_list; 79, 80; 80, attribute; 80, 81; 80, 82; 81, identifier:self; 82, identifier:_DATA_MODE_LISTEN; 83, attribute; 83, 84; 83, 85; 84, identifier:self; 85, identifier:adapter; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:_LOGGER; 90, identifier:debug; 91, argument_list; 91, 92; 91, 93; 91, 94; 92, string:"Running gatttool with a timeout of %d: %s"; 93, identifier:notification_timeout; 94, identifier:cmd; 95, with_statement; 95, 96; 95, 119; 96, with_clause; 96, 97; 97, with_item; 97, 98; 98, as_pattern; 98, 99; 98, 117; 99, call; 99, 100; 99, 101; 100, identifier:Popen; 101, argument_list; 101, 102; 101, 103; 101, 106; 101, 109; 101, 112; 102, identifier:cmd; 103, keyword_argument; 103, 104; 103, 105; 104, identifier:shell; 105, True; 106, keyword_argument; 106, 107; 106, 108; 107, identifier:stdout; 108, identifier:PIPE; 109, keyword_argument; 109, 110; 109, 111; 110, identifier:stderr; 111, identifier:PIPE; 112, keyword_argument; 112, 113; 112, 114; 113, identifier:preexec_fn; 114, attribute; 114, 115; 114, 116; 115, identifier:os; 116, identifier:setsid; 117, as_pattern_target; 117, 118; 118, identifier:process; 119, block; 119, 120; 120, try_statement; 120, 121; 120, 142; 121, block; 121, 122; 121, 135; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:result; 125, subscript; 125, 126; 125, 134; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:process; 129, identifier:communicate; 130, argument_list; 130, 131; 131, keyword_argument; 131, 132; 131, 133; 132, identifier:timeout; 133, identifier:notification_timeout; 134, integer:0; 135, expression_statement; 135, 136; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:_LOGGER; 139, identifier:debug; 140, argument_list; 140, 141; 141, string:"Finished gatttool"; 142, except_clause; 142, 143; 142, 144; 142, 145; 143, identifier:TimeoutExpired; 144, comment; 145, block; 145, 146; 145, 158; 145, 168; 146, expression_statement; 146, 147; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:os; 150, identifier:killpg; 151, argument_list; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:process; 154, identifier:pid; 155, attribute; 155, 156; 155, 157; 156, identifier:signal; 157, identifier:SIGINT; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:result; 161, subscript; 161, 162; 161, 167; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:process; 165, identifier:communicate; 166, argument_list; 167, integer:0; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:_LOGGER; 172, identifier:debug; 173, argument_list; 173, 174; 174, string:"Listening stopped forcefully after timeout."; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:result; 178, call; 178, 179; 178, 187; 179, attribute; 179, 180; 179, 186; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:result; 183, identifier:decode; 184, argument_list; 184, 185; 185, string:"utf-8"; 186, identifier:strip; 187, argument_list; 187, 188; 188, string:' \n\t'; 189, if_statement; 189, 190; 189, 193; 190, comparison_operator:in; 190, 191; 190, 192; 191, string:"Write Request failed"; 192, identifier:result; 193, block; 193, 194; 194, raise_statement; 194, 195; 195, call; 195, 196; 195, 197; 196, identifier:BluetoothBackendException; 197, argument_list; 197, 198; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, string:'Error writing handle to sensor: {}'; 201, identifier:format; 202, argument_list; 202, 203; 203, identifier:result; 204, expression_statement; 204, 205; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:_LOGGER; 208, identifier:debug; 209, argument_list; 209, 210; 209, 211; 210, string:"Got %s from gatttool"; 211, identifier:result; 212, comment; 213, if_statement; 213, 214; 213, 217; 214, comparison_operator:in; 214, 215; 214, 216; 215, string:"successfully"; 216, identifier:result; 217, block; 217, 218; 217, 228; 217, 229; 217, 261; 218, expression_statement; 218, 219; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:_LOGGER; 222, identifier:debug; 223, argument_list; 223, 224; 223, 225; 224, string:"Exit write_ble with result (%s)"; 225, call; 225, 226; 225, 227; 226, identifier:current_thread; 227, argument_list; 228, comment; 229, for_statement; 229, 230; 229, 231; 229, 237; 230, identifier:element; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:self; 234, identifier:extract_notification_payload; 235, argument_list; 235, 236; 236, identifier:result; 237, block; 237, 238; 238, expression_statement; 238, 239; 239, call; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:delegate; 242, identifier:handleNotification; 243, argument_list; 243, 244; 243, 245; 244, identifier:handle; 245, call; 245, 246; 245, 247; 246, identifier:bytes; 247, argument_list; 247, 248; 248, list_comprehension; 248, 249; 248, 254; 249, call; 249, 250; 249, 251; 250, identifier:int; 251, argument_list; 251, 252; 251, 253; 252, identifier:x; 253, integer:16; 254, for_in_clause; 254, 255; 254, 256; 255, identifier:x; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:element; 259, identifier:split; 260, argument_list; 261, return_statement; 261, 262; 262, True; 263, expression_statement; 263, 264; 264, augmented_assignment:+=; 264, 265; 264, 266; 265, identifier:attempt; 266, integer:1; 267, expression_statement; 267, 268; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:_LOGGER; 271, identifier:debug; 272, argument_list; 272, 273; 272, 274; 273, string:"Waiting for %s seconds before retrying"; 274, identifier:delay; 275, if_statement; 275, 276; 275, 281; 276, comparison_operator:<; 276, 277; 276, 278; 277, identifier:attempt; 278, attribute; 278, 279; 278, 280; 279, identifier:self; 280, identifier:retries; 281, block; 281, 282; 281, 289; 282, expression_statement; 282, 283; 283, call; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:time; 286, identifier:sleep; 287, argument_list; 287, 288; 288, identifier:delay; 289, expression_statement; 289, 290; 290, augmented_assignment:*=; 290, 291; 290, 292; 291, identifier:delay; 292, integer:2; 293, raise_statement; 293, 294; 294, call; 294, 295; 294, 296; 295, identifier:BluetoothBackendException; 296, argument_list; 296, 297; 297, call; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, string:"Exit write_ble, no data ({})"; 300, identifier:format; 301, argument_list; 301, 302; 302, call; 302, 303; 302, 304; 303, identifier:current_thread; 304, argument_list
def wait_for_notification(self, handle: int, delegate, notification_timeout: float): """Listen for characteristics changes from a BLE address. @param: mac - MAC address in format XX:XX:XX:XX:XX:XX @param: handle - BLE characteristics handle in format 0xXX a value of 0x0100 is written to register for listening @param: delegate - gatttool receives the --listen argument and the delegate object's handleNotification is called for every returned row @param: notification_timeout """ if not self.is_connected(): raise BluetoothBackendException('Not connected to any device.') attempt = 0 delay = 10 _LOGGER.debug("Enter write_ble (%s)", current_thread()) while attempt <= self.retries: cmd = "gatttool --device={} --addr-type={} --char-write-req -a {} -n {} --adapter={} --listen".format( self._mac, self.address_type, self.byte_to_handle(handle), self.bytes_to_string(self._DATA_MODE_LISTEN), self.adapter) _LOGGER.debug("Running gatttool with a timeout of %d: %s", notification_timeout, cmd) with Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE, preexec_fn=os.setsid) as process: try: result = process.communicate(timeout=notification_timeout)[0] _LOGGER.debug("Finished gatttool") except TimeoutExpired: # send signal to the process group, because listening always hangs os.killpg(process.pid, signal.SIGINT) result = process.communicate()[0] _LOGGER.debug("Listening stopped forcefully after timeout.") result = result.decode("utf-8").strip(' \n\t') if "Write Request failed" in result: raise BluetoothBackendException('Error writing handle to sensor: {}'.format(result)) _LOGGER.debug("Got %s from gatttool", result) # Parse the output to determine success if "successfully" in result: _LOGGER.debug("Exit write_ble with result (%s)", current_thread()) # extract useful data. for element in self.extract_notification_payload(result): delegate.handleNotification(handle, bytes([int(x, 16) for x in element.split()])) return True attempt += 1 _LOGGER.debug("Waiting for %s seconds before retrying", delay) if attempt < self.retries: time.sleep(delay) delay *= 2 raise BluetoothBackendException("Exit write_ble, no data ({})".format(current_thread()))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:simplify_tree; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:tree; 5, default_parameter; 5, 6; 5, 7; 6, identifier:unpack_lists; 7, True; 8, default_parameter; 8, 9; 8, 10; 9, identifier:in_list; 10, False; 11, block; 11, 12; 11, 14; 11, 15; 11, 191; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, if_statement; 15, 16; 15, 28; 15, 135; 15, 157; 16, boolean_operator:and; 16, 17; 16, 22; 17, call; 17, 18; 17, 19; 18, identifier:isinstance; 19, argument_list; 19, 20; 19, 21; 20, identifier:tree; 21, identifier:BaseNode; 22, not_operator; 22, 23; 23, call; 23, 24; 23, 25; 24, identifier:isinstance; 25, argument_list; 25, 26; 25, 27; 26, identifier:tree; 27, identifier:Terminal; 28, block; 28, 29; 28, 46; 28, 70; 28, 131; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:used_fields; 32, list_comprehension; 32, 33; 32, 34; 32, 39; 33, identifier:field; 34, for_in_clause; 34, 35; 34, 36; 35, identifier:field; 36, attribute; 36, 37; 36, 38; 37, identifier:tree; 38, identifier:_fields; 39, if_clause; 39, 40; 40, call; 40, 41; 40, 42; 41, identifier:getattr; 42, argument_list; 42, 43; 42, 44; 42, 45; 43, identifier:tree; 44, identifier:field; 45, False; 46, if_statement; 46, 47; 46, 53; 46, 64; 47, comparison_operator:==; 47, 48; 47, 52; 48, call; 48, 49; 48, 50; 49, identifier:len; 50, argument_list; 50, 51; 51, identifier:used_fields; 52, integer:1; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:result; 57, call; 57, 58; 57, 59; 58, identifier:getattr; 59, argument_list; 59, 60; 59, 61; 60, identifier:tree; 61, subscript; 61, 62; 61, 63; 62, identifier:used_fields; 63, integer:0; 64, else_clause; 64, 65; 65, block; 65, 66; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:result; 69, None; 70, if_statement; 70, 71; 70, 93; 71, parenthesized_expression; 71, 72; 72, boolean_operator:or; 72, 73; 72, 85; 73, boolean_operator:or; 73, 74; 73, 80; 74, comparison_operator:!=; 74, 75; 74, 79; 75, call; 75, 76; 75, 77; 76, identifier:len; 77, argument_list; 77, 78; 78, identifier:used_fields; 79, integer:1; 80, call; 80, 81; 80, 82; 81, identifier:isinstance; 82, argument_list; 82, 83; 82, 84; 83, identifier:tree; 84, identifier:AliasNode; 85, parenthesized_expression; 85, 86; 86, boolean_operator:and; 86, 87; 86, 88; 87, identifier:in_list; 88, call; 88, 89; 88, 90; 89, identifier:isinstance; 90, argument_list; 90, 91; 90, 92; 91, identifier:result; 92, identifier:list; 93, block; 93, 94; 93, 98; 93, 129; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:result; 97, identifier:tree; 98, for_statement; 98, 99; 98, 100; 98, 103; 99, identifier:field; 100, attribute; 100, 101; 100, 102; 101, identifier:tree; 102, identifier:_fields; 103, block; 103, 104; 103, 113; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:old_value; 107, call; 107, 108; 107, 109; 108, identifier:getattr; 109, argument_list; 109, 110; 109, 111; 109, 112; 110, identifier:tree; 111, identifier:field; 112, None; 113, if_statement; 113, 114; 113, 115; 114, identifier:old_value; 115, block; 115, 116; 116, expression_statement; 116, 117; 117, call; 117, 118; 117, 119; 118, identifier:setattr; 119, argument_list; 119, 120; 119, 121; 119, 122; 120, identifier:result; 121, identifier:field; 122, call; 122, 123; 122, 124; 123, identifier:simplify_tree; 124, argument_list; 124, 125; 124, 126; 125, identifier:old_value; 126, keyword_argument; 126, 127; 126, 128; 127, identifier:unpack_lists; 128, identifier:unpack_lists; 129, return_statement; 129, 130; 130, identifier:result; 131, assert_statement; 131, 132; 132, comparison_operator:is; 132, 133; 132, 134; 133, identifier:result; 134, None; 135, elif_clause; 135, 136; 135, 150; 136, boolean_operator:and; 136, 137; 136, 149; 137, boolean_operator:and; 137, 138; 137, 143; 138, call; 138, 139; 138, 140; 139, identifier:isinstance; 140, argument_list; 140, 141; 140, 142; 141, identifier:tree; 142, identifier:list; 143, comparison_operator:==; 143, 144; 143, 148; 144, call; 144, 145; 144, 146; 145, identifier:len; 146, argument_list; 146, 147; 147, identifier:tree; 148, integer:1; 149, identifier:unpack_lists; 150, block; 150, 151; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:result; 154, subscript; 154, 155; 154, 156; 155, identifier:tree; 156, integer:0; 157, else_clause; 157, 158; 158, block; 158, 159; 158, 189; 159, if_statement; 159, 160; 159, 165; 159, 183; 160, call; 160, 161; 160, 162; 161, identifier:isinstance; 162, argument_list; 162, 163; 162, 164; 163, identifier:tree; 164, identifier:list; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:result; 169, list_comprehension; 169, 170; 169, 180; 170, call; 170, 171; 170, 172; 171, identifier:simplify_tree; 172, argument_list; 172, 173; 172, 174; 172, 177; 173, identifier:el; 174, keyword_argument; 174, 175; 174, 176; 175, identifier:unpack_lists; 176, identifier:unpack_lists; 177, keyword_argument; 177, 178; 177, 179; 178, identifier:in_list; 179, True; 180, for_in_clause; 180, 181; 180, 182; 181, identifier:el; 182, identifier:tree; 183, else_clause; 183, 184; 184, block; 184, 185; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:result; 188, identifier:tree; 189, return_statement; 189, 190; 190, identifier:result; 191, return_statement; 191, 192; 192, call; 192, 193; 192, 194; 193, identifier:simplify_tree; 194, argument_list; 194, 195; 194, 196; 195, identifier:result; 196, keyword_argument; 196, 197; 196, 198; 197, identifier:unpack_lists; 198, identifier:unpack_lists
def simplify_tree(tree, unpack_lists=True, in_list=False): """Recursively unpack single-item lists and objects where fields and labels only reference a single child :param tree: the tree to simplify (mutating!) :param unpack_lists: whether single-item lists should be replaced by that item :param in_list: this is used to prevent unpacking a node in a list as AST visit can't handle nested lists """ # TODO: copy (or (de)serialize)? outside this function? if isinstance(tree, BaseNode) and not isinstance(tree, Terminal): used_fields = [field for field in tree._fields if getattr(tree, field, False)] if len(used_fields) == 1: result = getattr(tree, used_fields[0]) else: result = None if ( len(used_fields) != 1 or isinstance(tree, AliasNode) or (in_list and isinstance(result, list)) ): result = tree for field in tree._fields: old_value = getattr(tree, field, None) if old_value: setattr( result, field, simplify_tree(old_value, unpack_lists=unpack_lists), ) return result assert result is not None elif isinstance(tree, list) and len(tree) == 1 and unpack_lists: result = tree[0] else: if isinstance(tree, list): result = [ simplify_tree(el, unpack_lists=unpack_lists, in_list=True) for el in tree ] else: result = tree return result return simplify_tree(result, unpack_lists=unpack_lists)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:run; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, list_splat_pattern; 5, 6; 6, identifier:args; 7, block; 7, 8; 7, 10; 7, 21; 7, 32; 7, 80; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:params; 13, call; 13, 14; 13, 19; 14, attribute; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:parser; 18, identifier:parse_args; 19, argument_list; 19, 20; 20, identifier:args; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:config_file; 24, call; 24, 25; 24, 30; 25, attribute; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:os; 28, identifier:path; 29, identifier:expanduser; 30, argument_list; 30, 31; 31, string:'~/.sortinghat'; 32, if_statement; 32, 33; 32, 38; 32, 51; 32, 73; 33, comparison_operator:==; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:params; 36, identifier:action; 37, string:'get'; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:code; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:get; 46, argument_list; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:params; 49, identifier:parameter; 50, identifier:config_file; 51, elif_clause; 51, 52; 51, 57; 52, comparison_operator:==; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:params; 55, identifier:action; 56, string:'set'; 57, block; 57, 58; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:code; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:self; 64, identifier:set; 65, argument_list; 65, 66; 65, 69; 65, 72; 66, attribute; 66, 67; 66, 68; 67, identifier:params; 68, identifier:parameter; 69, attribute; 69, 70; 69, 71; 70, identifier:params; 71, identifier:value; 72, identifier:config_file; 73, else_clause; 73, 74; 74, block; 74, 75; 75, raise_statement; 75, 76; 76, call; 76, 77; 76, 78; 77, identifier:RuntimeError; 78, argument_list; 78, 79; 79, string:"Not get or set action given"; 80, return_statement; 80, 81; 81, identifier:code
def run(self, *args): """Get and set configuration parameters. This command gets or sets parameter values from the user configuration file. On Linux systems, configuration will be stored in the file '~/.sortinghat'. """ params = self.parser.parse_args(args) config_file = os.path.expanduser('~/.sortinghat') if params.action == 'get': code = self.get(params.parameter, config_file) elif params.action == 'set': code = self.set(params.parameter, params.value, config_file) else: raise RuntimeError("Not get or set action given") return code
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:export; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:source; 7, None; 8, block; 8, 9; 8, 11; 8, 15; 8, 29; 8, 99; 8, 116; 8, 144; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:uidentities; 14, dictionary; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:uids; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:api; 21, identifier:unique_identities; 22, argument_list; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:db; 26, keyword_argument; 26, 27; 26, 28; 27, identifier:source; 28, identifier:source; 29, for_statement; 29, 30; 29, 31; 29, 32; 30, identifier:uid; 31, identifier:uids; 32, block; 32, 33; 32, 57; 32, 65; 32, 81; 32, 89; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:enrollments; 36, list_comprehension; 36, 37; 36, 42; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:rol; 40, identifier:to_dict; 41, argument_list; 42, for_in_clause; 42, 43; 42, 44; 43, identifier:rol; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:api; 47, identifier:enrollments; 48, argument_list; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:db; 52, keyword_argument; 52, 53; 52, 54; 53, identifier:uuid; 54, attribute; 54, 55; 54, 56; 55, identifier:uid; 56, identifier:uuid; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:u; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:uid; 63, identifier:to_dict; 64, argument_list; 65, expression_statement; 65, 66; 66, call; 66, 67; 66, 72; 67, attribute; 67, 68; 67, 71; 68, subscript; 68, 69; 68, 70; 69, identifier:u; 70, string:'identities'; 71, identifier:sort; 72, argument_list; 72, 73; 73, keyword_argument; 73, 74; 73, 75; 74, identifier:key; 75, lambda; 75, 76; 75, 78; 76, lambda_parameters; 76, 77; 77, identifier:x; 78, subscript; 78, 79; 78, 80; 79, identifier:x; 80, string:'id'; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 88; 83, subscript; 83, 84; 83, 85; 84, identifier:uidentities; 85, attribute; 85, 86; 85, 87; 86, identifier:uid; 87, identifier:uuid; 88, identifier:u; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 98; 91, subscript; 91, 92; 91, 97; 92, subscript; 92, 93; 92, 94; 93, identifier:uidentities; 94, attribute; 94, 95; 94, 96; 95, identifier:uid; 96, identifier:uuid; 97, string:'enrollments'; 98, identifier:enrollments; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:blacklist; 102, list_comprehension; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:mb; 105, identifier:excluded; 106, for_in_clause; 106, 107; 106, 108; 107, identifier:mb; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:api; 111, identifier:blacklist; 112, argument_list; 112, 113; 113, attribute; 113, 114; 113, 115; 114, identifier:self; 115, identifier:db; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:obj; 119, dictionary; 119, 120; 119, 132; 119, 135; 119, 138; 119, 141; 120, pair; 120, 121; 120, 122; 121, string:'time'; 122, call; 122, 123; 122, 124; 123, identifier:str; 124, argument_list; 124, 125; 125, call; 125, 126; 125, 131; 126, attribute; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:datetime; 129, identifier:datetime; 130, identifier:now; 131, argument_list; 132, pair; 132, 133; 132, 134; 133, string:'source'; 134, identifier:source; 135, pair; 135, 136; 135, 137; 136, string:'blacklist'; 137, identifier:blacklist; 138, pair; 138, 139; 138, 140; 139, string:'organizations'; 140, dictionary; 141, pair; 141, 142; 141, 143; 142, string:'uidentities'; 143, identifier:uidentities; 144, return_statement; 144, 145; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:json; 148, identifier:dumps; 149, argument_list; 149, 150; 149, 151; 149, 156; 149, 159; 149, 164; 150, identifier:obj; 151, keyword_argument; 151, 152; 151, 153; 152, identifier:default; 153, attribute; 153, 154; 153, 155; 154, identifier:self; 155, identifier:_json_encoder; 156, keyword_argument; 156, 157; 156, 158; 157, identifier:indent; 158, integer:4; 159, keyword_argument; 159, 160; 159, 161; 160, identifier:separators; 161, tuple; 161, 162; 161, 163; 162, string:','; 163, string:': '; 164, keyword_argument; 164, 165; 164, 166; 165, identifier:sort_keys; 166, True
def export(self, source=None): """Export a set of unique identities. Method to export unique identities from the registry. Identities schema will follow Sorting Hat JSON format. When source parameter is given, only those unique identities which have one or more identities from the given source will be exported. :param source: source of the identities to export :returns: a JSON formatted str """ uidentities = {} uids = api.unique_identities(self.db, source=source) for uid in uids: enrollments = [rol.to_dict() for rol in api.enrollments(self.db, uuid=uid.uuid)] u = uid.to_dict() u['identities'].sort(key=lambda x: x['id']) uidentities[uid.uuid] = u uidentities[uid.uuid]['enrollments'] = enrollments blacklist = [mb.excluded for mb in api.blacklist(self.db)] obj = {'time': str(datetime.datetime.now()), 'source': source, 'blacklist': blacklist, 'organizations': {}, 'uidentities': uidentities} return json.dumps(obj, default=self._json_encoder, indent=4, separators=(',', ': '), sort_keys=True)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:export; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 12; 5, 23; 5, 69; 5, 94; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:organizations; 11, dictionary; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:orgs; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:api; 18, identifier:registry; 19, argument_list; 19, 20; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:db; 23, for_statement; 23, 24; 23, 25; 23, 26; 24, identifier:org; 25, identifier:orgs; 26, block; 26, 27; 26, 47; 26, 61; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:domains; 30, list_comprehension; 30, 31; 30, 42; 31, dictionary; 31, 32; 31, 37; 32, pair; 32, 33; 32, 34; 33, string:'domain'; 34, attribute; 34, 35; 34, 36; 35, identifier:dom; 36, identifier:domain; 37, pair; 37, 38; 37, 39; 38, string:'is_top'; 39, attribute; 39, 40; 39, 41; 40, identifier:dom; 41, identifier:is_top_domain; 42, for_in_clause; 42, 43; 42, 44; 43, identifier:dom; 44, attribute; 44, 45; 44, 46; 45, identifier:org; 46, identifier:domains; 47, expression_statement; 47, 48; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:domains; 51, identifier:sort; 52, argument_list; 52, 53; 53, keyword_argument; 53, 54; 53, 55; 54, identifier:key; 55, lambda; 55, 56; 55, 58; 56, lambda_parameters; 56, 57; 57, identifier:x; 58, subscript; 58, 59; 58, 60; 59, identifier:x; 60, string:'domain'; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 68; 63, subscript; 63, 64; 63, 65; 64, identifier:organizations; 65, attribute; 65, 66; 65, 67; 66, identifier:org; 67, identifier:name; 68, identifier:domains; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:obj; 72, dictionary; 72, 73; 72, 85; 72, 88; 72, 91; 73, pair; 73, 74; 73, 75; 74, string:'time'; 75, call; 75, 76; 75, 77; 76, identifier:str; 77, argument_list; 77, 78; 78, call; 78, 79; 78, 84; 79, attribute; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:datetime; 82, identifier:datetime; 83, identifier:now; 84, argument_list; 85, pair; 85, 86; 85, 87; 86, string:'blacklist'; 87, list:[]; 88, pair; 88, 89; 88, 90; 89, string:'organizations'; 90, identifier:organizations; 91, pair; 91, 92; 91, 93; 92, string:'uidentities'; 93, dictionary; 94, return_statement; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:json; 98, identifier:dumps; 99, argument_list; 99, 100; 99, 101; 99, 106; 99, 109; 99, 114; 100, identifier:obj; 101, keyword_argument; 101, 102; 101, 103; 102, identifier:default; 103, attribute; 103, 104; 103, 105; 104, identifier:self; 105, identifier:_json_encoder; 106, keyword_argument; 106, 107; 106, 108; 107, identifier:indent; 108, integer:4; 109, keyword_argument; 109, 110; 109, 111; 110, identifier:separators; 111, tuple; 111, 112; 111, 113; 112, string:','; 113, string:': '; 114, keyword_argument; 114, 115; 114, 116; 115, identifier:sort_keys; 116, True
def export(self): """Export a set of organizations. Method to export organizations from the registry. Organizations schema will follow Sorting Hat JSON format. :returns: a JSON formatted str """ organizations = {} orgs = api.registry(self.db) for org in orgs: domains = [{'domain': dom.domain, 'is_top': dom.is_top_domain} for dom in org.domains] domains.sort(key=lambda x: x['domain']) organizations[org.name] = domains obj = {'time': str(datetime.datetime.now()), 'blacklist': [], 'organizations': organizations, 'uidentities': {}} return json.dumps(obj, default=self._json_encoder, indent=4, separators=(',', ': '), sort_keys=True)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:match; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:a; 6, identifier:b; 7, block; 7, 8; 7, 10; 7, 23; 7, 36; 7, 55; 7, 64; 7, 73; 7, 92; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 17; 11, not_operator; 11, 12; 12, call; 12, 13; 12, 14; 13, identifier:isinstance; 14, argument_list; 14, 15; 14, 16; 15, identifier:a; 16, identifier:UniqueIdentity; 17, block; 17, 18; 18, raise_statement; 18, 19; 19, call; 19, 20; 19, 21; 20, identifier:ValueError; 21, argument_list; 21, 22; 22, string:"<a> is not an instance of UniqueIdentity"; 23, if_statement; 23, 24; 23, 30; 24, not_operator; 24, 25; 25, call; 25, 26; 25, 27; 26, identifier:isinstance; 27, argument_list; 27, 28; 27, 29; 28, identifier:b; 29, identifier:UniqueIdentity; 30, block; 30, 31; 31, raise_statement; 31, 32; 32, call; 32, 33; 32, 34; 33, identifier:ValueError; 34, argument_list; 34, 35; 35, string:"<b> is not an instance of UniqueIdentity"; 36, if_statement; 36, 37; 36, 52; 37, boolean_operator:and; 37, 38; 37, 45; 38, boolean_operator:and; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:a; 41, identifier:uuid; 42, attribute; 42, 43; 42, 44; 43, identifier:b; 44, identifier:uuid; 45, comparison_operator:==; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:a; 48, identifier:uuid; 49, attribute; 49, 50; 49, 51; 50, identifier:b; 51, identifier:uuid; 52, block; 52, 53; 53, return_statement; 53, 54; 54, True; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:filtered_a; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:filter; 62, argument_list; 62, 63; 63, identifier:a; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:filtered_b; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:filter; 71, argument_list; 71, 72; 72, identifier:b; 73, for_statement; 73, 74; 73, 75; 73, 76; 74, identifier:fa; 75, identifier:filtered_a; 76, block; 76, 77; 77, for_statement; 77, 78; 77, 79; 77, 80; 78, identifier:fb; 79, identifier:filtered_b; 80, block; 80, 81; 81, if_statement; 81, 82; 81, 89; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:self; 85, identifier:match_filtered_identities; 86, argument_list; 86, 87; 86, 88; 87, identifier:fa; 88, identifier:fb; 89, block; 89, 90; 90, return_statement; 90, 91; 91, True; 92, return_statement; 92, 93; 93, False
def match(self, a, b): """Determine if two unique identities are the same. This method compares the email addresses or the names of each identity to check if the given unique identities are the same. When the given unique identities are the same object or share the same UUID, this will also produce a positive match. Identities which their email addresses or names are in the blacklist will be ignored during the matching. :param a: unique identity to match :param b: unique identity to match :returns: True when both unique identities are likely to be the same. Otherwise, returns False. :raises ValueError: when any of the given unique identities is not an instance of UniqueIdentity class """ if not isinstance(a, UniqueIdentity): raise ValueError("<a> is not an instance of UniqueIdentity") if not isinstance(b, UniqueIdentity): raise ValueError("<b> is not an instance of UniqueIdentity") if a.uuid and b.uuid and a.uuid == b.uuid: return True filtered_a = self.filter(a) filtered_b = self.filter(b) for fa in filtered_a: for fb in filtered_b: if self.match_filtered_identities(fa, fb): return True return False
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:merge_enrollments; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:db; 5, identifier:uuid; 6, identifier:organization; 7, block; 7, 8; 7, 10; 7, 11; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, with_statement; 11, 12; 11, 22; 12, with_clause; 12, 13; 13, with_item; 13, 14; 14, as_pattern; 14, 15; 14, 20; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:db; 18, identifier:connect; 19, argument_list; 20, as_pattern_target; 20, 21; 21, identifier:session; 22, block; 22, 23; 22, 31; 22, 42; 22, 50; 22, 61; 22, 89; 22, 111; 22, 125; 22, 218; 22, 219; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:uidentity; 26, call; 26, 27; 26, 28; 27, identifier:find_unique_identity; 28, argument_list; 28, 29; 28, 30; 29, identifier:session; 30, identifier:uuid; 31, if_statement; 31, 32; 31, 34; 32, not_operator; 32, 33; 33, identifier:uidentity; 34, block; 34, 35; 35, raise_statement; 35, 36; 36, call; 36, 37; 36, 38; 37, identifier:NotFoundError; 38, argument_list; 38, 39; 39, keyword_argument; 39, 40; 39, 41; 40, identifier:entity; 41, identifier:uuid; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:org; 45, call; 45, 46; 45, 47; 46, identifier:find_organization; 47, argument_list; 47, 48; 47, 49; 48, identifier:session; 49, identifier:organization; 50, if_statement; 50, 51; 50, 53; 51, not_operator; 51, 52; 52, identifier:org; 53, block; 53, 54; 54, raise_statement; 54, 55; 55, call; 55, 56; 55, 57; 56, identifier:NotFoundError; 57, argument_list; 57, 58; 58, keyword_argument; 58, 59; 58, 60; 59, identifier:entity; 60, identifier:organization; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:disjoint; 64, call; 64, 65; 64, 88; 65, attribute; 65, 66; 65, 87; 66, call; 66, 67; 66, 76; 67, attribute; 67, 68; 67, 74; 67, 75; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:session; 71, identifier:query; 72, argument_list; 72, 73; 73, identifier:Enrollment; 74, line_continuation:\; 75, identifier:filter; 76, argument_list; 76, 77; 76, 82; 77, comparison_operator:==; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:Enrollment; 80, identifier:uidentity; 81, identifier:uidentity; 82, comparison_operator:==; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:Enrollment; 85, identifier:organization; 86, identifier:org; 87, identifier:all; 88, argument_list; 89, if_statement; 89, 90; 89, 92; 90, not_operator; 90, 91; 91, identifier:disjoint; 92, block; 92, 93; 92, 104; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:entity; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, string:'-'; 99, identifier:join; 100, argument_list; 100, 101; 101, tuple; 101, 102; 101, 103; 102, identifier:uuid; 103, identifier:organization; 104, raise_statement; 104, 105; 105, call; 105, 106; 105, 107; 106, identifier:NotFoundError; 107, argument_list; 107, 108; 108, keyword_argument; 108, 109; 108, 110; 109, identifier:entity; 110, identifier:entity; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:dates; 114, list_comprehension; 114, 115; 114, 122; 115, tuple; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:enr; 118, identifier:start; 119, attribute; 119, 120; 119, 121; 120, identifier:enr; 121, identifier:end; 122, for_in_clause; 122, 123; 122, 124; 123, identifier:enr; 124, identifier:disjoint; 125, for_statement; 125, 126; 125, 129; 125, 135; 125, 136; 125, 137; 125, 138; 126, pattern_list; 126, 127; 126, 128; 127, identifier:st; 128, identifier:en; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:utils; 132, identifier:merge_date_ranges; 133, argument_list; 133, 134; 134, identifier:dates; 135, comment; 136, comment; 137, comment; 138, block; 138, 139; 138, 158; 138, 174; 138, 190; 138, 191; 138, 192; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:is_dup; 142, lambda; 142, 143; 142, 147; 143, lambda_parameters; 143, 144; 143, 145; 143, 146; 144, identifier:x; 145, identifier:st; 146, identifier:en; 147, boolean_operator:and; 147, 148; 147, 153; 148, comparison_operator:==; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:x; 151, identifier:start; 152, identifier:st; 153, comparison_operator:==; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:x; 156, identifier:end; 157, identifier:en; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:filtered; 161, list_comprehension; 161, 162; 161, 163; 161, 166; 162, identifier:x; 163, for_in_clause; 163, 164; 163, 165; 164, identifier:x; 165, identifier:disjoint; 166, if_clause; 166, 167; 167, not_operator; 167, 168; 168, call; 168, 169; 168, 170; 169, identifier:is_dup; 170, argument_list; 170, 171; 170, 172; 170, 173; 171, identifier:x; 172, identifier:st; 173, identifier:en; 174, if_statement; 174, 175; 174, 184; 175, comparison_operator:!=; 175, 176; 175, 180; 176, call; 176, 177; 176, 178; 177, identifier:len; 178, argument_list; 178, 179; 179, identifier:filtered; 180, call; 180, 181; 180, 182; 181, identifier:len; 182, argument_list; 182, 183; 183, identifier:disjoint; 184, block; 184, 185; 184, 189; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:disjoint; 188, identifier:filtered; 189, continue_statement; 190, comment; 191, comment; 192, try_statement; 192, 193; 192, 207; 193, block; 193, 194; 194, expression_statement; 194, 195; 195, call; 195, 196; 195, 197; 196, identifier:enroll_db; 197, argument_list; 197, 198; 197, 199; 197, 200; 197, 201; 197, 204; 198, identifier:session; 199, identifier:uidentity; 200, identifier:org; 201, keyword_argument; 201, 202; 201, 203; 202, identifier:from_date; 203, identifier:st; 204, keyword_argument; 204, 205; 204, 206; 205, identifier:to_date; 206, identifier:en; 207, except_clause; 207, 208; 207, 212; 208, as_pattern; 208, 209; 208, 210; 209, identifier:ValueError; 210, as_pattern_target; 210, 211; 211, identifier:e; 212, block; 212, 213; 213, raise_statement; 213, 214; 214, call; 214, 215; 214, 216; 215, identifier:InvalidValueError; 216, argument_list; 216, 217; 217, identifier:e; 218, comment; 219, for_statement; 219, 220; 219, 221; 219, 222; 220, identifier:enr; 221, identifier:disjoint; 222, block; 222, 223; 223, expression_statement; 223, 224; 224, call; 224, 225; 224, 226; 225, identifier:delete_enrollment_db; 226, argument_list; 226, 227; 226, 228; 227, identifier:session; 228, identifier:enr
def merge_enrollments(db, uuid, organization): """Merge overlapping enrollments. This function merges those enrollments, related to the given 'uuid' and 'organization', that have overlapping dates. Default start and end dates (1900-01-01 and 2100-01-01) are considered range limits and will be removed when a set of ranges overlap. For example: * [(1900-01-01, 2010-01-01), (2008-01-01, 2100-01-01)] --> (2008-01-01, 2010-01-01) * [(1900-01-01, 2010-01-01), (2008-01-01, 2010-01-01), (2010-01-02, 2100-01-01)] --> (2008-01-01, 2010-01-01),(2010-01-02, 2100-01-01) * [(1900-01-01, 2010-01-01), (2010-01-02, 2100-01-01)] --> (1900-01-01, 2010-01-01), (2010-01-02, 2100-01-01) It may raise a InvalidValueError when any date is out of bounds. In other words, when any date < 1900-01-01 or date > 2100-01-01. :param db: database manager :param uuid: unique identifier :param organization: name of the organization :raises NotFoundError: when either 'uuid' or 'organization' are not found in the registry. It is also raised when there are not enrollments related to 'uuid' and 'organization' :raises InvalidValueError: when any date is out of bounds """ # Merge enrollments with db.connect() as session: uidentity = find_unique_identity(session, uuid) if not uidentity: raise NotFoundError(entity=uuid) org = find_organization(session, organization) if not org: raise NotFoundError(entity=organization) disjoint = session.query(Enrollment).\ filter(Enrollment.uidentity == uidentity, Enrollment.organization == org).all() if not disjoint: entity = '-'.join((uuid, organization)) raise NotFoundError(entity=entity) dates = [(enr.start, enr.end) for enr in disjoint] for st, en in utils.merge_date_ranges(dates): # We prefer this method to find duplicates # to avoid integrity exceptions when creating # enrollments that are already in the database is_dup = lambda x, st, en: x.start == st and x.end == en filtered = [x for x in disjoint if not is_dup(x, st, en)] if len(filtered) != len(disjoint): disjoint = filtered continue # This means no dups where found so we need to add a # new enrollment try: enroll_db(session, uidentity, org, from_date=st, to_date=en) except ValueError as e: raise InvalidValueError(e) # Remove disjoint enrollments from the registry for enr in disjoint: delete_enrollment_db(session, enr)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:registry; 3, parameters; 3, 4; 3, 5; 4, identifier:db; 5, default_parameter; 5, 6; 5, 7; 6, identifier:term; 7, None; 8, block; 8, 9; 8, 11; 8, 15; 8, 109; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:orgs; 14, list:[]; 15, with_statement; 15, 16; 15, 26; 16, with_clause; 16, 17; 17, with_item; 17, 18; 18, as_pattern; 18, 19; 18, 24; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:db; 22, identifier:connect; 23, argument_list; 24, as_pattern_target; 24, 25; 25, identifier:session; 26, block; 26, 27; 26, 102; 26, 103; 27, if_statement; 27, 28; 27, 29; 27, 79; 28, identifier:term; 29, block; 29, 30; 29, 68; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:orgs; 33, call; 33, 34; 33, 67; 34, attribute; 34, 35; 34, 66; 35, call; 35, 36; 35, 62; 36, attribute; 36, 37; 36, 60; 36, 61; 37, call; 37, 38; 37, 47; 38, attribute; 38, 39; 38, 45; 38, 46; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:session; 42, identifier:query; 43, argument_list; 43, 44; 44, identifier:Organization; 45, line_continuation:\; 46, identifier:filter; 47, argument_list; 47, 48; 48, call; 48, 49; 48, 54; 49, attribute; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:Organization; 52, identifier:name; 53, identifier:like; 54, argument_list; 54, 55; 55, binary_operator:+; 55, 56; 55, 59; 56, binary_operator:+; 56, 57; 56, 58; 57, string:'%'; 58, identifier:term; 59, string:'%'; 60, line_continuation:\; 61, identifier:order_by; 62, argument_list; 62, 63; 63, attribute; 63, 64; 63, 65; 64, identifier:Organization; 65, identifier:name; 66, identifier:all; 67, argument_list; 68, if_statement; 68, 69; 68, 71; 69, not_operator; 69, 70; 70, identifier:orgs; 71, block; 71, 72; 72, raise_statement; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:NotFoundError; 75, argument_list; 75, 76; 76, keyword_argument; 76, 77; 76, 78; 77, identifier:entity; 78, identifier:term; 79, else_clause; 79, 80; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:orgs; 84, call; 84, 85; 84, 101; 85, attribute; 85, 86; 85, 100; 86, call; 86, 87; 86, 96; 87, attribute; 87, 88; 87, 94; 87, 95; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:session; 91, identifier:query; 92, argument_list; 92, 93; 93, identifier:Organization; 94, line_continuation:\; 95, identifier:order_by; 96, argument_list; 96, 97; 97, attribute; 97, 98; 97, 99; 98, identifier:Organization; 99, identifier:name; 100, identifier:all; 101, argument_list; 102, comment; 103, expression_statement; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:session; 107, identifier:expunge_all; 108, argument_list; 109, return_statement; 109, 110; 110, identifier:orgs
def registry(db, term=None): """List the organizations available in the registry. The function will return the list of organizations. If term parameter is set, it will only return the information about the organizations which match that term. When the given term does not match with any organization from the registry a 'NotFounError' exception will be raised. :param db: database manager :param term: term to match with organizations names :returns: a list of organizations sorted by their name :raises NotFoundError: raised when the given term is not found on any organization from the registry """ orgs = [] with db.connect() as session: if term: orgs = session.query(Organization).\ filter(Organization.name.like('%' + term + '%')).\ order_by(Organization.name).all() if not orgs: raise NotFoundError(entity=term) else: orgs = session.query(Organization).\ order_by(Organization.name).all() # Detach objects from the session session.expunge_all() return orgs
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:domains; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:db; 5, default_parameter; 5, 6; 5, 7; 6, identifier:domain; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:top; 10, False; 11, block; 11, 12; 11, 14; 11, 18; 11, 199; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:doms; 17, list:[]; 18, with_statement; 18, 19; 18, 29; 19, with_clause; 19, 20; 20, with_item; 20, 21; 21, as_pattern; 21, 22; 21, 27; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:db; 25, identifier:connect; 26, argument_list; 27, as_pattern_target; 27, 28; 28, identifier:session; 29, block; 29, 30; 29, 192; 29, 193; 30, if_statement; 30, 31; 30, 32; 30, 152; 31, identifier:domain; 32, block; 32, 33; 32, 41; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:dom; 36, call; 36, 37; 36, 38; 37, identifier:find_domain; 38, argument_list; 38, 39; 38, 40; 39, identifier:session; 40, identifier:domain; 41, if_statement; 41, 42; 41, 44; 41, 145; 42, not_operator; 42, 43; 43, identifier:dom; 44, block; 44, 45; 45, if_statement; 45, 46; 45, 48; 45, 56; 46, not_operator; 46, 47; 47, identifier:top; 48, block; 48, 49; 49, raise_statement; 49, 50; 50, call; 50, 51; 50, 52; 51, identifier:NotFoundError; 52, argument_list; 52, 53; 53, keyword_argument; 53, 54; 53, 55; 54, identifier:entity; 55, identifier:domain; 56, else_clause; 56, 57; 56, 58; 56, 59; 56, 60; 57, comment; 58, comment; 59, comment; 60, block; 60, 61; 60, 79; 60, 86; 60, 114; 60, 134; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:add_dot; 64, lambda; 64, 65; 64, 67; 65, lambda_parameters; 65, 66; 66, identifier:d; 67, conditional_expression:if; 67, 68; 67, 71; 67, 78; 68, binary_operator:+; 68, 69; 68, 70; 69, string:'.'; 70, identifier:d; 71, not_operator; 71, 72; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:d; 75, identifier:startswith; 76, argument_list; 76, 77; 77, string:'.'; 78, identifier:d; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:d; 82, call; 82, 83; 82, 84; 83, identifier:add_dot; 84, argument_list; 84, 85; 85, identifier:domain; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:tops; 89, call; 89, 90; 89, 113; 90, attribute; 90, 91; 90, 112; 91, call; 91, 92; 91, 108; 92, attribute; 92, 93; 92, 107; 93, call; 93, 94; 93, 103; 94, attribute; 94, 95; 94, 101; 94, 102; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:session; 98, identifier:query; 99, argument_list; 99, 100; 100, identifier:Domain; 101, line_continuation:\; 102, identifier:filter; 103, argument_list; 103, 104; 104, attribute; 104, 105; 104, 106; 105, identifier:Domain; 106, identifier:is_top_domain; 107, identifier:order_by; 108, argument_list; 108, 109; 109, attribute; 109, 110; 109, 111; 110, identifier:Domain; 111, identifier:domain; 112, identifier:all; 113, argument_list; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:doms; 117, list_comprehension; 117, 118; 117, 119; 117, 122; 118, identifier:t; 119, for_in_clause; 119, 120; 119, 121; 120, identifier:t; 121, identifier:tops; 122, if_clause; 122, 123; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:d; 126, identifier:endswith; 127, argument_list; 127, 128; 128, call; 128, 129; 128, 130; 129, identifier:add_dot; 130, argument_list; 130, 131; 131, attribute; 131, 132; 131, 133; 132, identifier:t; 133, identifier:domain; 134, if_statement; 134, 135; 134, 137; 135, not_operator; 135, 136; 136, identifier:doms; 137, block; 137, 138; 138, raise_statement; 138, 139; 139, call; 139, 140; 139, 141; 140, identifier:NotFoundError; 141, argument_list; 141, 142; 142, keyword_argument; 142, 143; 142, 144; 143, identifier:entity; 144, identifier:domain; 145, else_clause; 145, 146; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:doms; 150, list:[dom]; 150, 151; 151, identifier:dom; 152, else_clause; 152, 153; 153, block; 153, 154; 153, 163; 153, 177; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:query; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:session; 160, identifier:query; 161, argument_list; 161, 162; 162, identifier:Domain; 163, if_statement; 163, 164; 163, 165; 164, identifier:top; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:query; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:query; 172, identifier:filter; 173, argument_list; 173, 174; 174, attribute; 174, 175; 174, 176; 175, identifier:Domain; 176, identifier:is_top_domain; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:doms; 180, call; 180, 181; 180, 191; 181, attribute; 181, 182; 181, 190; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:query; 185, identifier:order_by; 186, argument_list; 186, 187; 187, attribute; 187, 188; 187, 189; 188, identifier:Domain; 189, identifier:domain; 190, identifier:all; 191, argument_list; 192, comment; 193, expression_statement; 193, 194; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:session; 197, identifier:expunge_all; 198, argument_list; 199, return_statement; 199, 200; 200, identifier:doms
def domains(db, domain=None, top=False): """List the domains available in the registry. The function will return the list of domains. Settting the top flag, it will look for those domains that are top domains. If domain parameter is set, it will only return the information about that domain. When both paramaters are set, it will first search for the given domain. If it is not found, it will look for its top domains. In the case of neither the domain exists nor has top domains, a 'NotFoundError' exception will be raised. :param db: database manager :param domain: name of the domain :param top: filter by top domains :returns: a list of domains :raises NotFoundError: raised when the given domain is not found in the registry """ doms = [] with db.connect() as session: if domain: dom = find_domain(session, domain) if not dom: if not top: raise NotFoundError(entity=domain) else: # Adds a dot to the beggining of the domain. # Useful to compare domains like example.com and # myexample.com add_dot = lambda d: '.' + d if not d.startswith('.') else d d = add_dot(domain) tops = session.query(Domain).\ filter(Domain.is_top_domain).order_by(Domain.domain).all() doms = [t for t in tops if d.endswith(add_dot(t.domain))] if not doms: raise NotFoundError(entity=domain) else: doms = [dom] else: query = session.query(Domain) if top: query = query.filter(Domain.is_top_domain) doms = query.order_by(Domain.domain).all() # Detach objects from the session session.expunge_all() return doms
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:countries; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:db; 5, default_parameter; 5, 6; 5, 7; 6, identifier:code; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:term; 10, None; 11, block; 11, 12; 11, 14; 11, 41; 11, 62; 11, 66; 11, 198; 12, expression_statement; 12, 13; 13, comment; 14, function_definition; 14, 15; 14, 16; 14, 18; 15, function_name:_is_code_valid; 16, parameters; 16, 17; 17, identifier:code; 18, block; 18, 19; 19, return_statement; 19, 20; 20, boolean_operator:and; 20, 21; 20, 35; 20, 36; 21, boolean_operator:and; 21, 22; 21, 28; 21, 29; 22, comparison_operator:==; 22, 23; 22, 27; 23, call; 23, 24; 23, 25; 24, identifier:type; 25, argument_list; 25, 26; 26, identifier:code; 27, identifier:str; 28, line_continuation:\; 29, comparison_operator:==; 29, 30; 29, 34; 30, call; 30, 31; 30, 32; 31, identifier:len; 32, argument_list; 32, 33; 33, identifier:code; 34, integer:2; 35, line_continuation:\; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:code; 39, identifier:isalpha; 40, argument_list; 41, if_statement; 41, 42; 41, 51; 42, boolean_operator:and; 42, 43; 42, 46; 43, comparison_operator:is; 43, 44; 43, 45; 44, identifier:code; 45, None; 46, not_operator; 46, 47; 47, call; 47, 48; 47, 49; 48, identifier:_is_code_valid; 49, argument_list; 49, 50; 50, identifier:code; 51, block; 51, 52; 52, raise_statement; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:InvalidValueError; 55, argument_list; 55, 56; 56, binary_operator:%; 56, 57; 56, 58; 57, string:'country code must be a 2 length alpha string - %s given'; 58, call; 58, 59; 58, 60; 59, identifier:str; 60, argument_list; 60, 61; 61, identifier:code; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:cs; 65, list:[]; 66, with_statement; 66, 67; 66, 77; 67, with_clause; 67, 68; 68, with_item; 68, 69; 69, as_pattern; 69, 70; 69, 75; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:db; 73, identifier:connect; 74, argument_list; 75, as_pattern_target; 75, 76; 76, identifier:session; 77, block; 77, 78; 77, 87; 77, 191; 77, 192; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:query; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:session; 84, identifier:query; 85, argument_list; 85, 86; 86, identifier:Country; 87, if_statement; 87, 88; 87, 91; 87, 168; 88, boolean_operator:or; 88, 89; 88, 90; 89, identifier:code; 90, identifier:term; 91, block; 91, 92; 91, 135; 91, 150; 92, if_statement; 92, 93; 92, 94; 92, 112; 93, identifier:code; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:query; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:query; 101, identifier:filter; 102, argument_list; 102, 103; 103, comparison_operator:==; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:Country; 106, identifier:code; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:code; 110, identifier:upper; 111, argument_list; 112, elif_clause; 112, 113; 112, 114; 113, identifier:term; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:query; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:query; 121, identifier:filter; 122, argument_list; 122, 123; 123, call; 123, 124; 123, 129; 124, attribute; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:Country; 127, identifier:name; 128, identifier:like; 129, argument_list; 129, 130; 130, binary_operator:+; 130, 131; 130, 134; 131, binary_operator:+; 131, 132; 131, 133; 132, string:'%'; 133, identifier:term; 134, string:'%'; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:cs; 138, call; 138, 139; 138, 149; 139, attribute; 139, 140; 139, 148; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:query; 143, identifier:order_by; 144, argument_list; 144, 145; 145, attribute; 145, 146; 145, 147; 146, identifier:Country; 147, identifier:code; 148, identifier:all; 149, argument_list; 150, if_statement; 150, 151; 150, 153; 151, not_operator; 151, 152; 152, identifier:cs; 153, block; 153, 154; 153, 161; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:e; 157, conditional_expression:if; 157, 158; 157, 159; 157, 160; 158, identifier:code; 159, identifier:code; 160, identifier:term; 161, raise_statement; 161, 162; 162, call; 162, 163; 162, 164; 163, identifier:NotFoundError; 164, argument_list; 164, 165; 165, keyword_argument; 165, 166; 165, 167; 166, identifier:entity; 167, identifier:e; 168, else_clause; 168, 169; 169, block; 169, 170; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:cs; 173, call; 173, 174; 173, 190; 174, attribute; 174, 175; 174, 189; 175, call; 175, 176; 175, 185; 176, attribute; 176, 177; 176, 183; 176, 184; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:session; 180, identifier:query; 181, argument_list; 181, 182; 182, identifier:Country; 183, line_continuation:\; 184, identifier:order_by; 185, argument_list; 185, 186; 186, attribute; 186, 187; 186, 188; 187, identifier:Country; 188, identifier:code; 189, identifier:all; 190, argument_list; 191, comment; 192, expression_statement; 192, 193; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:session; 196, identifier:expunge_all; 197, argument_list; 198, return_statement; 198, 199; 199, identifier:cs
def countries(db, code=None, term=None): """List the countries available in the registry. The function will return the list of countries. When either 'code' or 'term' parameters are set, it will only return the information about those countries that match them. Take into account that 'code' is a country identifier composed by two letters (i.e ES or US). A 'InvalidValueError' exception will be raised when this identifier is not valid. If this value is valid, 'term' parameter will be ignored. When the given values do not match with any country from the registry a 'NotFounError' exception will be raised. :param db: database manager :param code: country identifier composed by two letters :param term: term to match with countries names :returns: a list of countries sorted by their country id :raises InvalidValueError: raised when 'code' is not a string composed by two letters :raises NotFoundError: raised when the given 'code' or 'term' is not found for any country from the registry """ def _is_code_valid(code): return type(code) == str \ and len(code) == 2 \ and code.isalpha() if code is not None and not _is_code_valid(code): raise InvalidValueError('country code must be a 2 length alpha string - %s given' % str(code)) cs = [] with db.connect() as session: query = session.query(Country) if code or term: if code: query = query.filter(Country.code == code.upper()) elif term: query = query.filter(Country.name.like('%' + term + '%')) cs = query.order_by(Country.code).all() if not cs: e = code if code else term raise NotFoundError(entity=e) else: cs = session.query(Country).\ order_by(Country.code).all() # Detach objects from the session session.expunge_all() return cs
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:enrollments; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:db; 5, default_parameter; 5, 6; 5, 7; 6, identifier:uuid; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:organization; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:from_date; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:to_date; 16, None; 17, block; 17, 18; 17, 20; 17, 28; 17, 36; 17, 55; 17, 74; 17, 92; 17, 96; 17, 243; 18, expression_statement; 18, 19; 19, comment; 20, if_statement; 20, 21; 20, 23; 21, not_operator; 21, 22; 22, identifier:from_date; 23, block; 23, 24; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:from_date; 27, identifier:MIN_PERIOD_DATE; 28, if_statement; 28, 29; 28, 31; 29, not_operator; 29, 30; 30, identifier:to_date; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:to_date; 35, identifier:MAX_PERIOD_DATE; 36, if_statement; 36, 37; 36, 44; 37, boolean_operator:or; 37, 38; 37, 41; 38, comparison_operator:<; 38, 39; 38, 40; 39, identifier:from_date; 40, identifier:MIN_PERIOD_DATE; 41, comparison_operator:>; 41, 42; 41, 43; 42, identifier:from_date; 43, identifier:MAX_PERIOD_DATE; 44, block; 44, 45; 45, raise_statement; 45, 46; 46, call; 46, 47; 46, 48; 47, identifier:InvalidValueError; 48, argument_list; 48, 49; 49, binary_operator:%; 49, 50; 49, 51; 50, string:"'from_date' %s is out of bounds"; 51, call; 51, 52; 51, 53; 52, identifier:str; 53, argument_list; 53, 54; 54, identifier:from_date; 55, if_statement; 55, 56; 55, 63; 56, boolean_operator:or; 56, 57; 56, 60; 57, comparison_operator:<; 57, 58; 57, 59; 58, identifier:to_date; 59, identifier:MIN_PERIOD_DATE; 60, comparison_operator:>; 60, 61; 60, 62; 61, identifier:to_date; 62, identifier:MAX_PERIOD_DATE; 63, block; 63, 64; 64, raise_statement; 64, 65; 65, call; 65, 66; 65, 67; 66, identifier:InvalidValueError; 67, argument_list; 67, 68; 68, binary_operator:%; 68, 69; 68, 70; 69, string:"'to_date' %s is out of bounds"; 70, call; 70, 71; 70, 72; 71, identifier:str; 72, argument_list; 72, 73; 73, identifier:to_date; 74, if_statement; 74, 75; 74, 82; 75, boolean_operator:and; 75, 76; 75, 79; 76, boolean_operator:and; 76, 77; 76, 78; 77, identifier:from_date; 78, identifier:to_date; 79, comparison_operator:>; 79, 80; 79, 81; 80, identifier:from_date; 81, identifier:to_date; 82, block; 82, 83; 83, raise_statement; 83, 84; 84, call; 84, 85; 84, 86; 85, identifier:InvalidValueError; 86, argument_list; 86, 87; 87, binary_operator:%; 87, 88; 87, 89; 88, string:"'from_date' %s cannot be greater than %s"; 89, tuple; 89, 90; 89, 91; 90, identifier:from_date; 91, identifier:to_date; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:enrollments; 95, list:[]; 96, with_statement; 96, 97; 96, 107; 97, with_clause; 97, 98; 98, with_item; 98, 99; 99, as_pattern; 99, 100; 99, 105; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:db; 103, identifier:connect; 104, argument_list; 105, as_pattern_target; 105, 106; 106, identifier:session; 107, block; 107, 108; 107, 139; 107, 140; 107, 175; 107, 176; 107, 211; 107, 212; 107, 236; 107, 237; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:query; 111, call; 111, 112; 111, 128; 112, attribute; 112, 113; 112, 126; 112, 127; 113, call; 113, 114; 113, 123; 114, attribute; 114, 115; 114, 121; 114, 122; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:session; 118, identifier:query; 119, argument_list; 119, 120; 120, identifier:Enrollment; 121, line_continuation:\; 122, identifier:join; 123, argument_list; 123, 124; 123, 125; 124, identifier:UniqueIdentity; 125, identifier:Organization; 126, line_continuation:\; 127, identifier:filter; 128, argument_list; 128, 129; 128, 134; 129, comparison_operator:>=; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:Enrollment; 132, identifier:start; 133, identifier:from_date; 134, comparison_operator:<=; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:Enrollment; 137, identifier:end; 138, identifier:to_date; 139, comment; 140, if_statement; 140, 141; 140, 142; 141, identifier:uuid; 142, block; 142, 143; 142, 151; 142, 162; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:uidentity; 146, call; 146, 147; 146, 148; 147, identifier:find_unique_identity; 148, argument_list; 148, 149; 148, 150; 149, identifier:session; 150, identifier:uuid; 151, if_statement; 151, 152; 151, 154; 152, not_operator; 152, 153; 153, identifier:uidentity; 154, block; 154, 155; 155, raise_statement; 155, 156; 156, call; 156, 157; 156, 158; 157, identifier:NotFoundError; 158, argument_list; 158, 159; 159, keyword_argument; 159, 160; 159, 161; 160, identifier:entity; 161, identifier:uuid; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:query; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:query; 168, identifier:filter; 169, argument_list; 169, 170; 170, comparison_operator:==; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:Enrollment; 173, identifier:uidentity; 174, identifier:uidentity; 175, comment; 176, if_statement; 176, 177; 176, 178; 177, identifier:organization; 178, block; 178, 179; 178, 187; 178, 198; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:org; 182, call; 182, 183; 182, 184; 183, identifier:find_organization; 184, argument_list; 184, 185; 184, 186; 185, identifier:session; 186, identifier:organization; 187, if_statement; 187, 188; 187, 190; 188, not_operator; 188, 189; 189, identifier:org; 190, block; 190, 191; 191, raise_statement; 191, 192; 192, call; 192, 193; 192, 194; 193, identifier:NotFoundError; 194, argument_list; 194, 195; 195, keyword_argument; 195, 196; 195, 197; 196, identifier:entity; 197, identifier:organization; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:query; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:query; 204, identifier:filter; 205, argument_list; 205, 206; 206, comparison_operator:==; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:Enrollment; 209, identifier:organization; 210, identifier:org; 211, comment; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:enrollments; 215, call; 215, 216; 215, 235; 216, attribute; 216, 217; 216, 234; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:query; 220, identifier:order_by; 221, argument_list; 221, 222; 221, 225; 221, 228; 221, 231; 222, attribute; 222, 223; 222, 224; 223, identifier:UniqueIdentity; 224, identifier:uuid; 225, attribute; 225, 226; 225, 227; 226, identifier:Organization; 227, identifier:name; 228, attribute; 228, 229; 228, 230; 229, identifier:Enrollment; 230, identifier:start; 231, attribute; 231, 232; 231, 233; 232, identifier:Enrollment; 233, identifier:end; 234, identifier:all; 235, argument_list; 236, comment; 237, expression_statement; 237, 238; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:session; 241, identifier:expunge_all; 242, argument_list; 243, return_statement; 243, 244; 244, identifier:enrollments
def enrollments(db, uuid=None, organization=None, from_date=None, to_date=None): """List the enrollment information available in the registry. This function will return a list of enrollments. If 'uuid' parameter is set, it will return the enrollments related to that unique identity; if 'organization' parameter is given, it will return the enrollments related to that organization; if both parameters are set, the function will return the list of enrollments of 'uuid' on the 'organization'. Enrollments between a period can also be listed using 'from_date' and 'to_date' parameters. When these are set, the function will return all those enrollments where Enrollment.start >= from_date AND Enrollment.end <= to_date. Defaults values for these dates are 1900-01-01 and 2100-01-01. When either 'uuid' or 'organization' are not in the registry a 'NotFoundError' exception will be raised. :param db: database manager :param uuid: unique identifier :param organization: name of the organization :param from_date: date when the enrollment starts :param to_date: date when the enrollment ends :returns: a list of enrollments sorted by uuid or by organization. :raises NotFoundError: when either 'uuid' or 'organization' are not found in the registry. :raises InvalidValeError: it is raised in two cases, when "from_date" < 1900-01-01 or "to_date" > 2100-01-01; when "from_date > to_date". """ if not from_date: from_date = MIN_PERIOD_DATE if not to_date: to_date = MAX_PERIOD_DATE if from_date < MIN_PERIOD_DATE or from_date > MAX_PERIOD_DATE: raise InvalidValueError("'from_date' %s is out of bounds" % str(from_date)) if to_date < MIN_PERIOD_DATE or to_date > MAX_PERIOD_DATE: raise InvalidValueError("'to_date' %s is out of bounds" % str(to_date)) if from_date and to_date and from_date > to_date: raise InvalidValueError("'from_date' %s cannot be greater than %s" % (from_date, to_date)) enrollments = [] with db.connect() as session: query = session.query(Enrollment).\ join(UniqueIdentity, Organization).\ filter(Enrollment.start >= from_date, Enrollment.end <= to_date) # Filter by uuid if uuid: uidentity = find_unique_identity(session, uuid) if not uidentity: raise NotFoundError(entity=uuid) query = query.filter(Enrollment.uidentity == uidentity) # Filter by organization if organization: org = find_organization(session, organization) if not org: raise NotFoundError(entity=organization) query = query.filter(Enrollment.organization == org) # Get the results enrollments = query.order_by(UniqueIdentity.uuid, Organization.name, Enrollment.start, Enrollment.end).all() # Detach objects from the session session.expunge_all() return enrollments
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:blacklist; 3, parameters; 3, 4; 3, 5; 4, identifier:db; 5, default_parameter; 5, 6; 5, 7; 6, identifier:term; 7, None; 8, block; 8, 9; 8, 11; 8, 15; 8, 109; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:mbs; 14, list:[]; 15, with_statement; 15, 16; 15, 26; 16, with_clause; 16, 17; 17, with_item; 17, 18; 18, as_pattern; 18, 19; 18, 24; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:db; 22, identifier:connect; 23, argument_list; 24, as_pattern_target; 24, 25; 25, identifier:session; 26, block; 26, 27; 26, 102; 26, 103; 27, if_statement; 27, 28; 27, 29; 27, 79; 28, identifier:term; 29, block; 29, 30; 29, 68; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:mbs; 33, call; 33, 34; 33, 67; 34, attribute; 34, 35; 34, 66; 35, call; 35, 36; 35, 62; 36, attribute; 36, 37; 36, 60; 36, 61; 37, call; 37, 38; 37, 47; 38, attribute; 38, 39; 38, 45; 38, 46; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:session; 42, identifier:query; 43, argument_list; 43, 44; 44, identifier:MatchingBlacklist; 45, line_continuation:\; 46, identifier:filter; 47, argument_list; 47, 48; 48, call; 48, 49; 48, 54; 49, attribute; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:MatchingBlacklist; 52, identifier:excluded; 53, identifier:like; 54, argument_list; 54, 55; 55, binary_operator:+; 55, 56; 55, 59; 56, binary_operator:+; 56, 57; 56, 58; 57, string:'%'; 58, identifier:term; 59, string:'%'; 60, line_continuation:\; 61, identifier:order_by; 62, argument_list; 62, 63; 63, attribute; 63, 64; 63, 65; 64, identifier:MatchingBlacklist; 65, identifier:excluded; 66, identifier:all; 67, argument_list; 68, if_statement; 68, 69; 68, 71; 69, not_operator; 69, 70; 70, identifier:mbs; 71, block; 71, 72; 72, raise_statement; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:NotFoundError; 75, argument_list; 75, 76; 76, keyword_argument; 76, 77; 76, 78; 77, identifier:entity; 78, identifier:term; 79, else_clause; 79, 80; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:mbs; 84, call; 84, 85; 84, 101; 85, attribute; 85, 86; 85, 100; 86, call; 86, 87; 86, 96; 87, attribute; 87, 88; 87, 94; 87, 95; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:session; 91, identifier:query; 92, argument_list; 92, 93; 93, identifier:MatchingBlacklist; 94, line_continuation:\; 95, identifier:order_by; 96, argument_list; 96, 97; 97, attribute; 97, 98; 97, 99; 98, identifier:MatchingBlacklist; 99, identifier:excluded; 100, identifier:all; 101, argument_list; 102, comment; 103, expression_statement; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:session; 107, identifier:expunge_all; 108, argument_list; 109, return_statement; 109, 110; 110, identifier:mbs
def blacklist(db, term=None): """List the blacklisted entities available in the registry. The function will return the list of blacklisted entities. If term parameter is set, it will only return the information about the entities which match that term. When the given term does not match with any entry on the blacklist a 'NotFoundError' exception will be raised. :param db: database manager :param term: term to match with blacklisted entries :returns: a list of blacklisted entities sorted by their name :raises NotFoundError: raised when the given term is not found on any blacklisted entry from the registry """ mbs = [] with db.connect() as session: if term: mbs = session.query(MatchingBlacklist).\ filter(MatchingBlacklist.excluded.like('%' + term + '%')).\ order_by(MatchingBlacklist.excluded).all() if not mbs: raise NotFoundError(entity=term) else: mbs = session.query(MatchingBlacklist).\ order_by(MatchingBlacklist.excluded).all() # Detach objects from the session session.expunge_all() return mbs
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:__parse_identities; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:stream; 6, block; 6, 7; 6, 9; 6, 102; 6, 111; 6, 115; 7, expression_statement; 7, 8; 8, comment; 9, function_definition; 9, 10; 9, 11; 9, 15; 10, function_name:__create_sh_identities; 11, parameters; 11, 12; 11, 13; 11, 14; 12, identifier:name; 13, identifier:emails; 14, identifier:yaml_entry; 15, block; 15, 16; 15, 18; 15, 22; 15, 39; 15, 40; 15, 41; 15, 65; 15, 100; 16, expression_statement; 16, 17; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:ids; 21, list:[]; 22, expression_statement; 22, 23; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:ids; 26, identifier:append; 27, argument_list; 27, 28; 28, call; 28, 29; 28, 30; 29, identifier:Identity; 30, argument_list; 30, 31; 30, 34; 31, keyword_argument; 31, 32; 31, 33; 32, identifier:name; 33, identifier:name; 34, keyword_argument; 34, 35; 34, 36; 35, identifier:source; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:source; 39, comment; 40, comment; 41, if_statement; 41, 42; 41, 43; 42, identifier:emails; 43, block; 43, 44; 44, for_statement; 44, 45; 44, 46; 44, 47; 45, identifier:m; 46, identifier:emails; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:ids; 52, identifier:append; 53, argument_list; 53, 54; 54, call; 54, 55; 54, 56; 55, identifier:Identity; 56, argument_list; 56, 57; 56, 60; 57, keyword_argument; 57, 58; 57, 59; 58, identifier:email; 59, identifier:m; 60, keyword_argument; 60, 61; 60, 62; 61, identifier:source; 62, attribute; 62, 63; 62, 64; 63, identifier:self; 64, identifier:source; 65, for_statement; 65, 66; 65, 67; 65, 68; 66, identifier:pb; 67, identifier:PERCEVAL_BACKENDS; 68, block; 68, 69; 68, 75; 69, if_statement; 69, 70; 69, 73; 70, comparison_operator:not; 70, 71; 70, 72; 71, identifier:pb; 72, identifier:yaml_entry; 73, block; 73, 74; 74, continue_statement; 75, for_statement; 75, 76; 75, 77; 75, 80; 76, identifier:username; 77, subscript; 77, 78; 77, 79; 78, identifier:yaml_entry; 79, identifier:pb; 80, block; 80, 81; 80, 93; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:identity; 84, call; 84, 85; 84, 86; 85, identifier:Identity; 86, argument_list; 86, 87; 86, 90; 87, keyword_argument; 87, 88; 87, 89; 88, identifier:username; 89, identifier:username; 90, keyword_argument; 90, 91; 90, 92; 91, identifier:source; 92, identifier:pb; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:ids; 97, identifier:append; 98, argument_list; 98, 99; 99, identifier:identity; 100, return_statement; 100, 101; 101, identifier:ids; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:yaml_file; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:self; 108, identifier:__load_yml; 109, argument_list; 109, 110; 110, identifier:stream; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:yid_counter; 114, integer:0; 115, try_statement; 115, 116; 115, 272; 116, block; 116, 117; 117, for_statement; 117, 118; 117, 119; 117, 120; 118, identifier:yid; 119, identifier:yaml_file; 120, block; 120, 121; 120, 127; 120, 137; 120, 138; 120, 146; 120, 156; 120, 166; 120, 182; 120, 192; 120, 199; 120, 203; 120, 212; 120, 224; 120, 230; 120, 231; 120, 240; 120, 246; 120, 264; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:profile; 124, subscript; 124, 125; 124, 126; 125, identifier:yid; 126, string:'profile'; 127, if_statement; 127, 128; 127, 131; 128, comparison_operator:is; 128, 129; 128, 130; 129, identifier:profile; 130, None; 131, block; 131, 132; 132, raise_statement; 132, 133; 133, call; 133, 134; 133, 135; 134, identifier:AttributeError; 135, argument_list; 135, 136; 136, string:'profile'; 137, comment; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:name; 141, subscript; 141, 142; 141, 145; 142, subscript; 142, 143; 142, 144; 143, identifier:yid; 144, string:'profile'; 145, string:'name'; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:is_bot; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:profile; 152, identifier:get; 153, argument_list; 153, 154; 153, 155; 154, string:'is_bot'; 155, False; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:emails; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:yid; 162, identifier:get; 163, argument_list; 163, 164; 163, 165; 164, string:'email'; 165, None; 166, if_statement; 166, 167; 166, 172; 167, boolean_operator:and; 167, 168; 167, 169; 168, identifier:emails; 169, attribute; 169, 170; 169, 171; 170, identifier:self; 171, identifier:email_validation; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:self; 177, identifier:__validate_email; 178, argument_list; 178, 179; 179, subscript; 179, 180; 179, 181; 180, identifier:emails; 181, integer:0; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:enrollments; 185, call; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:yid; 188, identifier:get; 189, argument_list; 189, 190; 189, 191; 190, string:'enrollments'; 191, None; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:uuid; 195, call; 195, 196; 195, 197; 196, identifier:str; 197, argument_list; 197, 198; 198, identifier:yid_counter; 199, expression_statement; 199, 200; 200, augmented_assignment:+=; 200, 201; 200, 202; 201, identifier:yid_counter; 202, integer:1; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:uid; 206, call; 206, 207; 206, 208; 207, identifier:UniqueIdentity; 208, argument_list; 208, 209; 209, keyword_argument; 209, 210; 209, 211; 210, identifier:uuid; 211, identifier:uuid; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:prf; 215, call; 215, 216; 215, 217; 216, identifier:Profile; 217, argument_list; 217, 218; 217, 221; 218, keyword_argument; 218, 219; 218, 220; 219, identifier:name; 220, identifier:name; 221, keyword_argument; 221, 222; 221, 223; 222, identifier:is_bot; 223, identifier:is_bot; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:uid; 228, identifier:profile; 229, identifier:prf; 230, comment; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:sh_identities; 234, call; 234, 235; 234, 236; 235, identifier:__create_sh_identities; 236, argument_list; 236, 237; 236, 238; 236, 239; 237, identifier:name; 238, identifier:emails; 239, identifier:yid; 240, expression_statement; 240, 241; 241, augmented_assignment:+=; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:uid; 244, identifier:identities; 245, identifier:sh_identities; 246, if_statement; 246, 247; 246, 248; 247, identifier:enrollments; 248, block; 248, 249; 248, 258; 249, expression_statement; 249, 250; 250, assignment; 250, 251; 250, 252; 251, identifier:affiliations; 252, call; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:self; 255, identifier:__parse_affiliations_yml; 256, argument_list; 256, 257; 257, identifier:enrollments; 258, expression_statement; 258, 259; 259, augmented_assignment:+=; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:uid; 262, identifier:enrollments; 263, identifier:affiliations; 264, expression_statement; 264, 265; 265, assignment; 265, 266; 265, 271; 266, subscript; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:self; 269, identifier:_identities; 270, identifier:uuid; 271, identifier:uid; 272, except_clause; 272, 273; 272, 277; 273, as_pattern; 273, 274; 273, 275; 274, identifier:KeyError; 275, as_pattern_target; 275, 276; 276, identifier:e; 277, block; 277, 278; 277, 286; 277, 297; 278, expression_statement; 278, 279; 279, assignment; 279, 280; 279, 281; 280, identifier:error; 281, binary_operator:%; 281, 282; 281, 283; 282, string:"Attribute %s not found"; 283, attribute; 283, 284; 283, 285; 284, identifier:e; 285, identifier:args; 286, expression_statement; 286, 287; 287, assignment; 287, 288; 287, 289; 288, identifier:msg; 289, binary_operator:%; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:self; 292, identifier:GRIMOIRELAB_INVALID_FORMAT; 293, dictionary; 293, 294; 294, pair; 294, 295; 294, 296; 295, string:'error'; 296, identifier:error; 297, raise_statement; 297, 298; 298, call; 298, 299; 298, 300; 299, identifier:InvalidFormatError; 300, argument_list; 300, 301; 301, keyword_argument; 301, 302; 301, 303; 302, identifier:cause; 303, identifier:msg
def __parse_identities(self, stream): """Parse identities using GrimoireLab format. The GrimoireLab identities format is a YAML document following a schema similar to the example below. More information available at https://github.com/bitergia/identities - profile: name: Vivek K. is_bot: false github: - vsekhark email: - vivek@****.com enrollments: - organization: Community start: 1900-01-01 end: 2100-01-01 :parse json: YAML object to parse :raise InvalidFormatError: raised when the format of the YAML is not valid. """ def __create_sh_identities(name, emails, yaml_entry): """Create SH identities based on name, emails and backens data in yaml_entry""" ids = [] ids.append(Identity(name=name, source=self.source)) # FIXME we should encourage our users to add email or usernames # and if not returning at least a WARNING if emails: for m in emails: ids.append(Identity(email=m, source=self.source)) for pb in PERCEVAL_BACKENDS: if pb not in yaml_entry: continue for username in yaml_entry[pb]: identity = Identity(username=username, source=pb) ids.append(identity) return ids yaml_file = self.__load_yml(stream) yid_counter = 0 try: for yid in yaml_file: profile = yid['profile'] if profile is None: raise AttributeError('profile') # we want the KeyError if name is missing name = yid['profile']['name'] is_bot = profile.get('is_bot', False) emails = yid.get('email', None) if emails and self.email_validation: self.__validate_email(emails[0]) enrollments = yid.get('enrollments', None) uuid = str(yid_counter) yid_counter += 1 uid = UniqueIdentity(uuid=uuid) prf = Profile(name=name, is_bot=is_bot) uid.profile = prf # now it is time to add the identities for name, emails and backends sh_identities = __create_sh_identities(name, emails, yid) uid.identities += sh_identities if enrollments: affiliations = self.__parse_affiliations_yml(enrollments) uid.enrollments += affiliations self._identities[uuid] = uid except KeyError as e: error = "Attribute %s not found" % e.args msg = self.GRIMOIRELAB_INVALID_FORMAT % {'error': error} raise InvalidFormatError(cause=msg)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:__parse_organizations; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:stream; 6, block; 6, 7; 6, 9; 6, 14; 6, 23; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 12; 10, not_operator; 10, 11; 11, identifier:stream; 12, block; 12, 13; 13, return_statement; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:yaml_file; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:__load_yml; 21, argument_list; 21, 22; 22, identifier:stream; 23, try_statement; 23, 24; 23, 178; 23, 210; 24, block; 24, 25; 25, for_statement; 25, 26; 25, 27; 25, 28; 26, identifier:element; 27, identifier:yaml_file; 28, block; 28, 29; 28, 40; 28, 66; 28, 75; 28, 170; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:name; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:self; 35, identifier:__encode; 36, argument_list; 36, 37; 37, subscript; 37, 38; 37, 39; 38, identifier:element; 39, string:'organization'; 40, if_statement; 40, 41; 40, 43; 41, not_operator; 41, 42; 42, identifier:name; 43, block; 43, 44; 43, 48; 43, 59; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:error; 47, string:"Empty organization name"; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:msg; 51, binary_operator:%; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:self; 54, identifier:GRIMOIRELAB_INVALID_FORMAT; 55, dictionary; 55, 56; 56, pair; 56, 57; 56, 58; 57, string:'error'; 58, identifier:error; 59, raise_statement; 59, 60; 60, call; 60, 61; 60, 62; 61, identifier:InvalidFormatError; 62, argument_list; 62, 63; 63, keyword_argument; 63, 64; 63, 65; 64, identifier:cause; 65, identifier:msg; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:o; 69, call; 69, 70; 69, 71; 70, identifier:Organization; 71, argument_list; 71, 72; 72, keyword_argument; 72, 73; 72, 74; 73, identifier:name; 74, identifier:name; 75, if_statement; 75, 76; 75, 79; 76, comparison_operator:in; 76, 77; 76, 78; 77, string:'domains'; 78, identifier:element; 79, block; 79, 80; 79, 114; 80, if_statement; 80, 81; 80, 89; 81, not_operator; 81, 82; 82, call; 82, 83; 82, 84; 83, identifier:isinstance; 84, argument_list; 84, 85; 84, 88; 85, subscript; 85, 86; 85, 87; 86, identifier:element; 87, string:'domains'; 88, identifier:list; 89, block; 89, 90; 89, 96; 89, 107; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:error; 93, binary_operator:%; 93, 94; 93, 95; 94, string:"List of elements expected for organization %s"; 95, identifier:name; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:msg; 99, binary_operator:%; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:self; 102, identifier:GRIMOIRELAB_INVALID_FORMAT; 103, dictionary; 103, 104; 104, pair; 104, 105; 104, 106; 105, string:'error'; 106, identifier:error; 107, raise_statement; 107, 108; 108, call; 108, 109; 108, 110; 109, identifier:InvalidFormatError; 110, argument_list; 110, 111; 111, keyword_argument; 111, 112; 111, 113; 112, identifier:cause; 113, identifier:msg; 114, for_statement; 114, 115; 114, 116; 114, 119; 115, identifier:dom; 116, subscript; 116, 117; 116, 118; 117, identifier:element; 118, string:'domains'; 119, block; 119, 120; 120, if_statement; 120, 121; 120, 122; 120, 144; 121, identifier:dom; 122, block; 122, 123; 122, 135; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:d; 126, call; 126, 127; 126, 128; 127, identifier:Domain; 128, argument_list; 128, 129; 128, 132; 129, keyword_argument; 129, 130; 129, 131; 130, identifier:domain; 131, identifier:dom; 132, keyword_argument; 132, 133; 132, 134; 133, identifier:is_top_domain; 134, False; 135, expression_statement; 135, 136; 136, call; 136, 137; 136, 142; 137, attribute; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:o; 140, identifier:domains; 141, identifier:append; 142, argument_list; 142, 143; 143, identifier:d; 144, else_clause; 144, 145; 145, block; 145, 146; 145, 152; 145, 163; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:error; 149, binary_operator:%; 149, 150; 149, 151; 150, string:"Empty domain name for organization %s"; 151, identifier:name; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:msg; 155, binary_operator:%; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:self; 158, identifier:GRIMOIRELAB_INVALID_FORMAT; 159, dictionary; 159, 160; 160, pair; 160, 161; 160, 162; 161, string:'error'; 162, identifier:error; 163, raise_statement; 163, 164; 164, call; 164, 165; 164, 166; 165, identifier:InvalidFormatError; 166, argument_list; 166, 167; 167, keyword_argument; 167, 168; 167, 169; 168, identifier:cause; 169, identifier:msg; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 177; 172, subscript; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:self; 175, identifier:_organizations; 176, identifier:name; 177, identifier:o; 178, except_clause; 178, 179; 178, 183; 179, as_pattern; 179, 180; 179, 181; 180, identifier:KeyError; 181, as_pattern_target; 181, 182; 182, identifier:e; 183, block; 183, 184; 183, 192; 183, 203; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:error; 187, binary_operator:%; 187, 188; 187, 189; 188, string:"Attribute %s not found"; 189, attribute; 189, 190; 189, 191; 190, identifier:e; 191, identifier:args; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:msg; 195, binary_operator:%; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:self; 198, identifier:GRIMOIRELAB_INVALID_FORMAT; 199, dictionary; 199, 200; 200, pair; 200, 201; 200, 202; 201, string:'error'; 202, identifier:error; 203, raise_statement; 203, 204; 204, call; 204, 205; 204, 206; 205, identifier:InvalidFormatError; 206, argument_list; 206, 207; 207, keyword_argument; 207, 208; 207, 209; 208, identifier:cause; 209, identifier:msg; 210, except_clause; 210, 211; 210, 215; 211, as_pattern; 211, 212; 211, 213; 212, identifier:TypeError; 213, as_pattern_target; 213, 214; 214, identifier:e; 215, block; 215, 216; 215, 224; 215, 235; 216, expression_statement; 216, 217; 217, assignment; 217, 218; 217, 219; 218, identifier:error; 219, binary_operator:%; 219, 220; 219, 221; 220, string:"%s"; 221, attribute; 221, 222; 221, 223; 222, identifier:e; 223, identifier:args; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:msg; 227, binary_operator:%; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:self; 230, identifier:GRIMOIRELAB_INVALID_FORMAT; 231, dictionary; 231, 232; 232, pair; 232, 233; 232, 234; 233, string:'error'; 234, identifier:error; 235, raise_statement; 235, 236; 236, call; 236, 237; 236, 238; 237, identifier:InvalidFormatError; 238, argument_list; 238, 239; 239, keyword_argument; 239, 240; 239, 241; 240, identifier:cause; 241, identifier:msg
def __parse_organizations(self, stream): """Parse GrimoireLab organizations. The GrimoireLab organizations format is a YAML element stored under the "organizations" key. The next example shows the structure of the document: - organizations: Bitergia: - bitergia.com - support.bitergia.com - biterg.io LibreSoft: - libresoft.es :param json: YAML object to parse :raises InvalidFormatError: raised when the format of the YAML is not valid. """ if not stream: return yaml_file = self.__load_yml(stream) try: for element in yaml_file: name = self.__encode(element['organization']) if not name: error = "Empty organization name" msg = self.GRIMOIRELAB_INVALID_FORMAT % {'error': error} raise InvalidFormatError(cause=msg) o = Organization(name=name) if 'domains' in element: if not isinstance(element['domains'], list): error = "List of elements expected for organization %s" % name msg = self.GRIMOIRELAB_INVALID_FORMAT % {'error': error} raise InvalidFormatError(cause=msg) for dom in element['domains']: if dom: d = Domain(domain=dom, is_top_domain=False) o.domains.append(d) else: error = "Empty domain name for organization %s" % name msg = self.GRIMOIRELAB_INVALID_FORMAT % {'error': error} raise InvalidFormatError(cause=msg) self._organizations[name] = o except KeyError as e: error = "Attribute %s not found" % e.args msg = self.GRIMOIRELAB_INVALID_FORMAT % {'error': error} raise InvalidFormatError(cause=msg) except TypeError as e: error = "%s" % e.args msg = self.GRIMOIRELAB_INVALID_FORMAT % {'error': error} raise InvalidFormatError(cause=msg)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:__parse; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:stream; 6, block; 6, 7; 6, 9; 6, 20; 6, 29; 6, 36; 6, 43; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 12; 10, not_operator; 10, 11; 11, identifier:stream; 12, block; 12, 13; 13, raise_statement; 13, 14; 14, call; 14, 15; 14, 16; 15, identifier:InvalidFormatError; 16, argument_list; 16, 17; 17, keyword_argument; 17, 18; 17, 19; 18, identifier:cause; 19, string:"stream cannot be empty or None"; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:json; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:__load_json; 27, argument_list; 27, 28; 28, identifier:stream; 29, expression_statement; 29, 30; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:__parse_organizations; 34, argument_list; 34, 35; 35, identifier:json; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:__parse_identities; 41, argument_list; 41, 42; 42, identifier:json; 43, expression_statement; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:__parse_blacklist; 48, argument_list; 48, 49; 49, identifier:json
def __parse(self, stream): """Parse Sorting Hat stream""" if not stream: raise InvalidFormatError(cause="stream cannot be empty or None") json = self.__load_json(stream) self.__parse_organizations(json) self.__parse_identities(json) self.__parse_blacklist(json)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:__parse_blacklist; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:json; 6, block; 6, 7; 6, 9; 7, expression_statement; 7, 8; 8, comment; 9, try_statement; 9, 10; 9, 74; 10, block; 10, 11; 11, for_statement; 11, 12; 11, 13; 11, 16; 12, identifier:entry; 13, subscript; 13, 14; 13, 15; 14, identifier:json; 15, string:'blacklist'; 16, block; 16, 17; 16, 32; 16, 41; 16, 53; 17, if_statement; 17, 18; 17, 20; 18, not_operator; 18, 19; 19, identifier:entry; 20, block; 20, 21; 20, 25; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:msg; 24, string:"invalid json format. Blacklist entries cannot be null or empty"; 25, raise_statement; 25, 26; 26, call; 26, 27; 26, 28; 27, identifier:InvalidFormatError; 28, argument_list; 28, 29; 29, keyword_argument; 29, 30; 29, 31; 30, identifier:cause; 31, identifier:msg; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:excluded; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:__encode; 39, argument_list; 39, 40; 40, identifier:entry; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:bl; 44, call; 44, 45; 44, 50; 45, attribute; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:_blacklist; 49, identifier:get; 50, argument_list; 50, 51; 50, 52; 51, identifier:excluded; 52, None; 53, if_statement; 53, 54; 53, 56; 54, not_operator; 54, 55; 55, identifier:bl; 56, block; 56, 57; 56, 66; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:bl; 60, call; 60, 61; 60, 62; 61, identifier:MatchingBlacklist; 62, argument_list; 62, 63; 63, keyword_argument; 63, 64; 63, 65; 64, identifier:excluded; 65, identifier:excluded; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 73; 68, subscript; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:_blacklist; 72, identifier:excluded; 73, identifier:bl; 74, except_clause; 74, 75; 74, 79; 75, as_pattern; 75, 76; 75, 77; 76, identifier:KeyError; 77, as_pattern_target; 77, 78; 78, identifier:e; 79, block; 79, 80; 79, 88; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:msg; 83, binary_operator:%; 83, 84; 83, 85; 84, string:"invalid json format. Attribute %s not found"; 85, attribute; 85, 86; 85, 87; 86, identifier:e; 87, identifier:args; 88, raise_statement; 88, 89; 89, call; 89, 90; 89, 91; 90, identifier:InvalidFormatError; 91, argument_list; 91, 92; 92, keyword_argument; 92, 93; 92, 94; 93, identifier:cause; 94, identifier:msg
def __parse_blacklist(self, json): """Parse blacklist entries using Sorting Hat format. The Sorting Hat blacklist format is a JSON stream that stores a list of blacklisted entries. Next, there is an example of a valid stream: { "blacklist": [ "John Doe", "John Smith", "[email protected]" ] } :param stream: stream to parse :raises InvalidFormatError: raised when the format of the stream is not valid. """ try: for entry in json['blacklist']: if not entry: msg = "invalid json format. Blacklist entries cannot be null or empty" raise InvalidFormatError(cause=msg) excluded = self.__encode(entry) bl = self._blacklist.get(excluded, None) if not bl: bl = MatchingBlacklist(excluded=excluded) self._blacklist[excluded] = bl except KeyError as e: msg = "invalid json format. Attribute %s not found" % e.args raise InvalidFormatError(cause=msg)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:__parse_organizations; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:json; 6, block; 6, 7; 6, 9; 7, expression_statement; 7, 8; 8, comment; 9, try_statement; 9, 10; 9, 117; 10, block; 10, 11; 11, for_statement; 11, 12; 11, 13; 11, 16; 12, identifier:organization; 13, subscript; 13, 14; 13, 15; 14, identifier:json; 15, string:'organizations'; 16, block; 16, 17; 16, 26; 16, 38; 16, 59; 16, 67; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:name; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:__encode; 24, argument_list; 24, 25; 25, identifier:organization; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:org; 29, call; 29, 30; 29, 35; 30, attribute; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:_organizations; 34, identifier:get; 35, argument_list; 35, 36; 35, 37; 36, identifier:name; 37, None; 38, if_statement; 38, 39; 38, 41; 39, not_operator; 39, 40; 40, identifier:org; 41, block; 41, 42; 41, 51; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:org; 45, call; 45, 46; 45, 47; 46, identifier:Organization; 47, argument_list; 47, 48; 48, keyword_argument; 48, 49; 48, 50; 49, identifier:name; 50, identifier:name; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 58; 53, subscript; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:_organizations; 57, identifier:name; 58, identifier:org; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:domains; 62, subscript; 62, 63; 62, 66; 63, subscript; 63, 64; 63, 65; 64, identifier:json; 65, string:'organizations'; 66, identifier:organization; 67, for_statement; 67, 68; 67, 69; 67, 70; 68, identifier:domain; 69, identifier:domains; 70, block; 70, 71; 70, 92; 70, 108; 71, if_statement; 71, 72; 71, 80; 72, comparison_operator:!=; 72, 73; 72, 79; 73, call; 73, 74; 73, 75; 74, identifier:type; 75, argument_list; 75, 76; 76, subscript; 76, 77; 76, 78; 77, identifier:domain; 78, string:'is_top'; 79, identifier:bool; 80, block; 80, 81; 80, 85; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:msg; 84, string:"invalid json format. 'is_top' must have a bool value"; 85, raise_statement; 85, 86; 86, call; 86, 87; 86, 88; 87, identifier:InvalidFormatError; 88, argument_list; 88, 89; 89, keyword_argument; 89, 90; 89, 91; 90, identifier:cause; 91, identifier:msg; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:dom; 95, call; 95, 96; 95, 97; 96, identifier:Domain; 97, argument_list; 97, 98; 97, 103; 98, keyword_argument; 98, 99; 98, 100; 99, identifier:domain; 100, subscript; 100, 101; 100, 102; 101, identifier:domain; 102, string:'domain'; 103, keyword_argument; 103, 104; 103, 105; 104, identifier:is_top_domain; 105, subscript; 105, 106; 105, 107; 106, identifier:domain; 107, string:'is_top'; 108, expression_statement; 108, 109; 109, call; 109, 110; 109, 115; 110, attribute; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:org; 113, identifier:domains; 114, identifier:append; 115, argument_list; 115, 116; 116, identifier:dom; 117, except_clause; 117, 118; 117, 122; 118, as_pattern; 118, 119; 118, 120; 119, identifier:KeyError; 120, as_pattern_target; 120, 121; 121, identifier:e; 122, block; 122, 123; 122, 131; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:msg; 126, binary_operator:%; 126, 127; 126, 128; 127, string:"invalid json format. Attribute %s not found"; 128, attribute; 128, 129; 128, 130; 129, identifier:e; 130, identifier:args; 131, raise_statement; 131, 132; 132, call; 132, 133; 132, 134; 133, identifier:InvalidFormatError; 134, argument_list; 134, 135; 135, keyword_argument; 135, 136; 135, 137; 136, identifier:cause; 137, identifier:msg
def __parse_organizations(self, json): """Parse organizations using Sorting Hat format. The Sorting Hat organizations format is a JSON stream which its keys are the name of the organizations. Each organization object has a list of domains. For instance: { "organizations": { "Bitergia": [ { "domain": "api.bitergia.com", "is_top": false }, { "domain": "bitergia.com", "is_top": true } ], "Example": [] }, "time": "2015-01-20 20:10:56.133378" } :param json: stream to parse :raises InvalidFormatError: raised when the format of the stream is not valid. """ try: for organization in json['organizations']: name = self.__encode(organization) org = self._organizations.get(name, None) if not org: org = Organization(name=name) self._organizations[name] = org domains = json['organizations'][organization] for domain in domains: if type(domain['is_top']) != bool: msg = "invalid json format. 'is_top' must have a bool value" raise InvalidFormatError(cause=msg) dom = Domain(domain=domain['domain'], is_top_domain=domain['is_top']) org.domains.append(dom) except KeyError as e: msg = "invalid json format. Attribute %s not found" % e.args raise InvalidFormatError(cause=msg)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:import_blacklist; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:parser; 6, block; 6, 7; 6, 9; 6, 15; 6, 22; 6, 26; 6, 96; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:blacklist; 12, attribute; 12, 13; 12, 14; 13, identifier:parser; 14, identifier:blacklist; 15, expression_statement; 15, 16; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:log; 20, argument_list; 20, 21; 21, string:"Loading blacklist..."; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:n; 25, integer:0; 26, for_statement; 26, 27; 26, 28; 26, 29; 27, identifier:entry; 28, identifier:blacklist; 29, block; 29, 30; 30, try_statement; 30, 31; 30, 60; 30, 74; 31, block; 31, 32; 31, 44; 31, 56; 32, expression_statement; 32, 33; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:api; 36, identifier:add_to_matching_blacklist; 37, argument_list; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:db; 41, attribute; 41, 42; 41, 43; 42, identifier:entry; 43, identifier:excluded; 44, expression_statement; 44, 45; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:display; 49, argument_list; 49, 50; 49, 51; 50, string:'load_blacklist.tmpl'; 51, keyword_argument; 51, 52; 51, 53; 52, identifier:entry; 53, attribute; 53, 54; 53, 55; 54, identifier:entry; 55, identifier:excluded; 56, expression_statement; 56, 57; 57, augmented_assignment:+=; 57, 58; 57, 59; 58, identifier:n; 59, integer:1; 60, except_clause; 60, 61; 60, 65; 61, as_pattern; 61, 62; 61, 63; 62, identifier:ValueError; 63, as_pattern_target; 63, 64; 64, identifier:e; 65, block; 65, 66; 66, raise_statement; 66, 67; 67, call; 67, 68; 67, 69; 68, identifier:RuntimeError; 69, argument_list; 69, 70; 70, call; 70, 71; 70, 72; 71, identifier:str; 72, argument_list; 72, 73; 73, identifier:e; 74, except_clause; 74, 75; 74, 79; 75, as_pattern; 75, 76; 75, 77; 76, identifier:AlreadyExistsError; 77, as_pattern_target; 77, 78; 78, identifier:e; 79, block; 79, 80; 79, 89; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:msg; 83, binary_operator:%; 83, 84; 83, 85; 84, string:"%s. Not added."; 85, call; 85, 86; 85, 87; 86, identifier:str; 87, argument_list; 87, 88; 88, identifier:e; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:self; 93, identifier:warning; 94, argument_list; 94, 95; 95, identifier:msg; 96, expression_statement; 96, 97; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:log; 101, argument_list; 101, 102; 102, binary_operator:%; 102, 103; 102, 104; 103, string:"%d/%d blacklist entries loaded"; 104, tuple; 104, 105; 104, 106; 105, identifier:n; 106, call; 106, 107; 106, 108; 107, identifier:len; 108, argument_list; 108, 109; 109, identifier:blacklist
def import_blacklist(self, parser): """Import blacklist. New entries parsed by 'parser' will be added to the blacklist. :param parser: sorting hat parser """ blacklist = parser.blacklist self.log("Loading blacklist...") n = 0 for entry in blacklist: try: api.add_to_matching_blacklist(self.db, entry.excluded) self.display('load_blacklist.tmpl', entry=entry.excluded) n += 1 except ValueError as e: raise RuntimeError(str(e)) except AlreadyExistsError as e: msg = "%s. Not added." % str(e) self.warning(msg) self.log("%d/%d blacklist entries loaded" % (n, len(blacklist)))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:import_organizations; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:parser; 6, default_parameter; 6, 7; 6, 8; 7, identifier:overwrite; 8, False; 9, block; 9, 10; 9, 12; 9, 18; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:orgs; 15, attribute; 15, 16; 15, 17; 16, identifier:parser; 17, identifier:organizations; 18, for_statement; 18, 19; 18, 20; 18, 21; 19, identifier:org; 20, identifier:orgs; 21, block; 21, 22; 21, 57; 22, try_statement; 22, 23; 22, 36; 22, 50; 23, block; 23, 24; 24, expression_statement; 24, 25; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:api; 28, identifier:add_organization; 29, argument_list; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:db; 33, attribute; 33, 34; 33, 35; 34, identifier:org; 35, identifier:name; 36, except_clause; 36, 37; 36, 41; 37, as_pattern; 37, 38; 37, 39; 38, identifier:ValueError; 39, as_pattern_target; 39, 40; 40, identifier:e; 41, block; 41, 42; 42, raise_statement; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:RuntimeError; 45, argument_list; 45, 46; 46, call; 46, 47; 46, 48; 47, identifier:str; 48, argument_list; 48, 49; 49, identifier:e; 50, except_clause; 50, 51; 50, 55; 51, as_pattern; 51, 52; 51, 53; 52, identifier:AlreadyExistsError; 53, as_pattern_target; 53, 54; 54, identifier:e; 55, block; 55, 56; 56, pass_statement; 57, for_statement; 57, 58; 57, 59; 57, 62; 58, identifier:dom; 59, attribute; 59, 60; 59, 61; 60, identifier:org; 61, identifier:domains; 62, block; 62, 63; 63, try_statement; 63, 64; 63, 105; 63, 121; 64, block; 64, 65; 64, 88; 65, expression_statement; 65, 66; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:api; 69, identifier:add_domain; 70, argument_list; 70, 71; 70, 74; 70, 77; 70, 80; 70, 85; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:db; 74, attribute; 74, 75; 74, 76; 75, identifier:org; 76, identifier:name; 77, attribute; 77, 78; 77, 79; 78, identifier:dom; 79, identifier:domain; 80, keyword_argument; 80, 81; 80, 82; 81, identifier:is_top_domain; 82, attribute; 82, 83; 82, 84; 83, identifier:dom; 84, identifier:is_top_domain; 85, keyword_argument; 85, 86; 85, 87; 86, identifier:overwrite; 87, identifier:overwrite; 88, expression_statement; 88, 89; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:self; 92, identifier:display; 93, argument_list; 93, 94; 93, 95; 93, 100; 94, string:'load_domains.tmpl'; 95, keyword_argument; 95, 96; 95, 97; 96, identifier:domain; 97, attribute; 97, 98; 97, 99; 98, identifier:dom; 99, identifier:domain; 100, keyword_argument; 100, 101; 100, 102; 101, identifier:organization; 102, attribute; 102, 103; 102, 104; 103, identifier:org; 104, identifier:name; 105, except_clause; 105, 106; 105, 112; 106, as_pattern; 106, 107; 106, 110; 107, tuple; 107, 108; 107, 109; 108, identifier:ValueError; 109, identifier:NotFoundError; 110, as_pattern_target; 110, 111; 111, identifier:e; 112, block; 112, 113; 113, raise_statement; 113, 114; 114, call; 114, 115; 114, 116; 115, identifier:RuntimeError; 116, argument_list; 116, 117; 117, call; 117, 118; 117, 119; 118, identifier:str; 119, argument_list; 119, 120; 120, identifier:e; 121, except_clause; 121, 122; 121, 126; 122, as_pattern; 122, 123; 122, 124; 123, identifier:AlreadyExistsError; 124, as_pattern_target; 124, 125; 125, identifier:e; 126, block; 126, 127; 126, 136; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:msg; 130, binary_operator:%; 130, 131; 130, 132; 131, string:"%s. Not updated."; 132, call; 132, 133; 132, 134; 133, identifier:str; 134, argument_list; 134, 135; 135, identifier:e; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:self; 140, identifier:warning; 141, argument_list; 141, 142; 142, identifier:msg
def import_organizations(self, parser, overwrite=False): """Import organizations. New domains and organizations parsed by 'parser' will be added to the registry. Remember that a domain can only be assigned to one organization. If one of the given domains is already on the registry, the new relationship will NOT be created unless 'overwrite' were set to 'True'. :param parser: sorting hat parser :param overwrite: force to reassign domains """ orgs = parser.organizations for org in orgs: try: api.add_organization(self.db, org.name) except ValueError as e: raise RuntimeError(str(e)) except AlreadyExistsError as e: pass for dom in org.domains: try: api.add_domain(self.db, org.name, dom.domain, is_top_domain=dom.is_top_domain, overwrite=overwrite) self.display('load_domains.tmpl', domain=dom.domain, organization=org.name) except (ValueError, NotFoundError) as e: raise RuntimeError(str(e)) except AlreadyExistsError as e: msg = "%s. Not updated." % str(e) self.warning(msg)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 21; 2, function_name:import_identities; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 4, identifier:self; 5, identifier:parser; 6, default_parameter; 6, 7; 6, 8; 7, identifier:matching; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:match_new; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:no_strict_matching; 14, False; 15, default_parameter; 15, 16; 15, 17; 16, identifier:reset; 17, False; 18, default_parameter; 18, 19; 18, 20; 19, identifier:verbose; 20, False; 21, block; 21, 22; 21, 24; 21, 28; 21, 80; 21, 86; 21, 119; 22, expression_statement; 22, 23; 23, comment; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:matcher; 27, None; 28, if_statement; 28, 29; 28, 30; 29, identifier:matching; 30, block; 30, 31; 30, 36; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:strict; 34, not_operator; 34, 35; 35, identifier:no_strict_matching; 36, try_statement; 36, 37; 36, 60; 37, block; 37, 38; 37, 49; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:blacklist; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:api; 44, identifier:blacklist; 45, argument_list; 45, 46; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:db; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:matcher; 52, call; 52, 53; 52, 54; 53, identifier:create_identity_matcher; 54, argument_list; 54, 55; 54, 56; 54, 57; 55, identifier:matching; 56, identifier:blacklist; 57, keyword_argument; 57, 58; 57, 59; 58, identifier:strict; 59, identifier:strict; 60, except_clause; 60, 61; 60, 65; 61, as_pattern; 61, 62; 61, 63; 62, identifier:MatcherNotSupportedError; 63, as_pattern_target; 63, 64; 64, identifier:e; 65, block; 65, 66; 65, 76; 66, expression_statement; 66, 67; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:error; 71, argument_list; 71, 72; 72, call; 72, 73; 72, 74; 73, identifier:str; 74, argument_list; 74, 75; 75, identifier:e; 76, return_statement; 76, 77; 77, attribute; 77, 78; 77, 79; 78, identifier:e; 79, identifier:code; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:uidentities; 83, attribute; 83, 84; 83, 85; 84, identifier:parser; 85, identifier:identities; 86, try_statement; 86, 87; 86, 99; 87, block; 87, 88; 88, expression_statement; 88, 89; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:self; 92, identifier:__load_unique_identities; 93, argument_list; 93, 94; 93, 95; 93, 96; 93, 97; 93, 98; 94, identifier:uidentities; 95, identifier:matcher; 96, identifier:match_new; 97, identifier:reset; 98, identifier:verbose; 99, except_clause; 99, 100; 99, 104; 100, as_pattern; 100, 101; 100, 102; 101, identifier:LoadError; 102, as_pattern_target; 102, 103; 103, identifier:e; 104, block; 104, 105; 104, 115; 105, expression_statement; 105, 106; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:self; 109, identifier:error; 110, argument_list; 110, 111; 111, call; 111, 112; 111, 113; 112, identifier:str; 113, argument_list; 113, 114; 114, identifier:e; 115, return_statement; 115, 116; 116, attribute; 116, 117; 116, 118; 117, identifier:e; 118, identifier:code; 119, return_statement; 119, 120; 120, identifier:CMD_SUCCESS
def import_identities(self, parser, matching=None, match_new=False, no_strict_matching=False, reset=False, verbose=False): """Import identities information on the registry. New unique identities, organizations and enrollment data parsed by 'parser' will be added to the registry. Optionally, this method can look for possible identities that match with the new one to insert using 'matching' method. If a match is found, that means both identities are likely the same. Therefore, both identities would be merged into one. The 'match_new' parameter can be set to match and merge only new loaded identities. Rigorous validation of mathching values (i.e, well formed email addresses) will be disabled when <no_strict_matching> is set to to `True`. When `reset` is set, relationships and enrollments will be removed before loading any data. :param parser: sorting hat parser :param matching: type of matching used to merge existing identities :param match_new: match and merge only the new loaded identities :param no_strict_matching: disable strict matching (i.e, well-formed email addresses) :param reset: remove relationships and enrollments before loading data :param verbose: run in verbose mode when matching is set """ matcher = None if matching: strict = not no_strict_matching try: blacklist = api.blacklist(self.db) matcher = create_identity_matcher(matching, blacklist, strict=strict) except MatcherNotSupportedError as e: self.error(str(e)) return e.code uidentities = parser.identities try: self.__load_unique_identities(uidentities, matcher, match_new, reset, verbose) except LoadError as e: self.error(str(e)) return e.code return CMD_SUCCESS
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:__create_profile_from_identities; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:identities; 6, identifier:uuid; 7, identifier:verbose; 8, block; 8, 9; 8, 11; 8, 14; 8, 18; 8, 22; 8, 26; 8, 30; 8, 34; 8, 117; 8, 118; 8, 119; 8, 158; 8, 168; 8, 180; 9, expression_statement; 9, 10; 10, comment; 11, import_statement; 11, 12; 12, dotted_name; 12, 13; 13, identifier:re; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:EMAIL_ADDRESS_REGEX; 17, string:r"^(?P<email>[^\s@]+@[^\s@.]+\.[^\s@]+)$"; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:NAME_REGEX; 21, string:r"^\w+\s\w+"; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:name; 25, None; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:email; 29, None; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:username; 33, None; 34, for_statement; 34, 35; 34, 36; 34, 37; 35, identifier:identity; 36, identifier:identities; 37, block; 37, 38; 37, 67; 37, 96; 38, if_statement; 38, 39; 38, 45; 39, boolean_operator:and; 39, 40; 39, 42; 40, not_operator; 40, 41; 41, identifier:name; 42, attribute; 42, 43; 42, 44; 43, identifier:identity; 44, identifier:name; 45, block; 45, 46; 45, 58; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:m; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:re; 52, identifier:match; 53, argument_list; 53, 54; 53, 55; 54, identifier:NAME_REGEX; 55, attribute; 55, 56; 55, 57; 56, identifier:identity; 57, identifier:name; 58, if_statement; 58, 59; 58, 60; 59, identifier:m; 60, block; 60, 61; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:name; 64, attribute; 64, 65; 64, 66; 65, identifier:identity; 66, identifier:name; 67, if_statement; 67, 68; 67, 74; 68, boolean_operator:and; 68, 69; 68, 71; 69, not_operator; 69, 70; 70, identifier:email; 71, attribute; 71, 72; 71, 73; 72, identifier:identity; 73, identifier:email; 74, block; 74, 75; 74, 87; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:m; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:re; 81, identifier:match; 82, argument_list; 82, 83; 82, 84; 83, identifier:EMAIL_ADDRESS_REGEX; 84, attribute; 84, 85; 84, 86; 85, identifier:identity; 86, identifier:email; 87, if_statement; 87, 88; 87, 89; 88, identifier:m; 89, block; 89, 90; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:email; 93, attribute; 93, 94; 93, 95; 94, identifier:identity; 95, identifier:email; 96, if_statement; 96, 97; 96, 99; 97, not_operator; 97, 98; 98, identifier:username; 99, block; 99, 100; 100, if_statement; 100, 101; 100, 110; 101, boolean_operator:and; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:identity; 104, identifier:username; 105, comparison_operator:!=; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:identity; 108, identifier:username; 109, string:'None'; 110, block; 110, 111; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:username; 114, attribute; 114, 115; 114, 116; 115, identifier:identity; 116, identifier:username; 117, comment; 118, comment; 119, if_statement; 119, 120; 119, 122; 120, not_operator; 120, 121; 121, identifier:name; 122, block; 122, 123; 123, if_statement; 123, 124; 123, 125; 123, 137; 123, 152; 124, identifier:email; 125, block; 125, 126; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:name; 129, subscript; 129, 130; 129, 136; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:email; 133, identifier:split; 134, argument_list; 134, 135; 135, string:'@'; 136, integer:0; 137, elif_clause; 137, 138; 137, 139; 137, 140; 138, identifier:username; 139, comment; 140, block; 140, 141; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:name; 144, subscript; 144, 145; 144, 151; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:username; 148, identifier:split; 149, argument_list; 149, 150; 150, string:'@'; 151, integer:0; 152, else_clause; 152, 153; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:name; 157, None; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:kw; 161, dictionary; 161, 162; 161, 165; 162, pair; 162, 163; 162, 164; 163, string:'name'; 164, identifier:name; 165, pair; 165, 166; 165, 167; 166, string:'email'; 167, identifier:email; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:api; 172, identifier:edit_profile; 173, argument_list; 173, 174; 173, 177; 173, 178; 174, attribute; 174, 175; 174, 176; 175, identifier:self; 176, identifier:db; 177, identifier:uuid; 178, dictionary_splat; 178, 179; 179, identifier:kw; 180, expression_statement; 180, 181; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:self; 184, identifier:log; 185, argument_list; 185, 186; 185, 189; 186, binary_operator:%; 186, 187; 186, 188; 187, string:"-- profile %s updated"; 188, identifier:uuid; 189, identifier:verbose
def __create_profile_from_identities(self, identities, uuid, verbose): """Create a profile using the data from the identities""" import re EMAIL_ADDRESS_REGEX = r"^(?P<email>[^\s@]+@[^\s@.]+\.[^\s@]+)$" NAME_REGEX = r"^\w+\s\w+" name = None email = None username = None for identity in identities: if not name and identity.name: m = re.match(NAME_REGEX, identity.name) if m: name = identity.name if not email and identity.email: m = re.match(EMAIL_ADDRESS_REGEX, identity.email) if m: email = identity.email if not username: if identity.username and identity.username != 'None': username = identity.username # We need a name for each profile, so if no one was defined, # use email or username to complete it. if not name: if email: name = email.split('@')[0] elif username: # filter email addresses on username fields name = username.split('@')[0] else: name = None kw = {'name': name, 'email': email} api.edit_profile(self.db, uuid, **kw) self.log("-- profile %s updated" % uuid, verbose)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:__parse_identities; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:aliases; 6, identifier:email_to_employer; 7, block; 7, 8; 7, 10; 7, 11; 7, 18; 7, 25; 7, 26; 7, 185; 7, 186; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, expression_statement; 11, 12; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:__parse_aliases_stream; 16, argument_list; 16, 17; 17, identifier:aliases; 18, expression_statement; 18, 19; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:__parse_email_to_employer_stream; 23, argument_list; 23, 24; 24, identifier:email_to_employer; 25, comment; 26, for_statement; 26, 27; 26, 30; 26, 37; 27, pattern_list; 27, 28; 27, 29; 28, identifier:alias; 29, identifier:email; 30, call; 30, 31; 30, 36; 31, attribute; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:__raw_aliases; 35, identifier:items; 36, argument_list; 37, block; 37, 38; 37, 50; 37, 128; 37, 143; 37, 176; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:uid; 41, call; 41, 42; 41, 47; 42, attribute; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:_identities; 46, identifier:get; 47, argument_list; 47, 48; 47, 49; 48, identifier:email; 49, None; 50, if_statement; 50, 51; 50, 53; 51, not_operator; 51, 52; 52, identifier:uid; 53, block; 53, 54; 53, 63; 53, 78; 53, 111; 53, 120; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:uid; 57, call; 57, 58; 57, 59; 58, identifier:UniqueIdentity; 59, argument_list; 59, 60; 60, keyword_argument; 60, 61; 60, 62; 61, identifier:uuid; 62, identifier:email; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:e; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:re; 69, identifier:match; 70, argument_list; 70, 71; 70, 74; 70, 75; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:EMAIL_ADDRESS_REGEX; 74, identifier:email; 75, attribute; 75, 76; 75, 77; 76, identifier:re; 77, identifier:UNICODE; 78, if_statement; 78, 79; 78, 80; 78, 95; 79, identifier:e; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:identity; 84, call; 84, 85; 84, 86; 85, identifier:Identity; 86, argument_list; 86, 87; 86, 90; 87, keyword_argument; 87, 88; 87, 89; 88, identifier:email; 89, identifier:email; 90, keyword_argument; 90, 91; 90, 92; 91, identifier:source; 92, attribute; 92, 93; 92, 94; 93, identifier:self; 94, identifier:source; 95, else_clause; 95, 96; 96, block; 96, 97; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:identity; 100, call; 100, 101; 100, 102; 101, identifier:Identity; 102, argument_list; 102, 103; 102, 106; 103, keyword_argument; 103, 104; 103, 105; 104, identifier:username; 105, identifier:email; 106, keyword_argument; 106, 107; 106, 108; 107, identifier:source; 108, attribute; 108, 109; 108, 110; 109, identifier:self; 110, identifier:source; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 118; 113, attribute; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:uid; 116, identifier:identities; 117, identifier:append; 118, argument_list; 118, 119; 119, identifier:identity; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 127; 122, subscript; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:_identities; 126, identifier:email; 127, identifier:uid; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:e; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:re; 134, identifier:match; 135, argument_list; 135, 136; 135, 139; 135, 140; 136, attribute; 136, 137; 136, 138; 137, identifier:self; 138, identifier:EMAIL_ADDRESS_REGEX; 139, identifier:alias; 140, attribute; 140, 141; 140, 142; 141, identifier:re; 142, identifier:UNICODE; 143, if_statement; 143, 144; 143, 145; 143, 160; 144, identifier:e; 145, block; 145, 146; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:identity; 149, call; 149, 150; 149, 151; 150, identifier:Identity; 151, argument_list; 151, 152; 151, 155; 152, keyword_argument; 152, 153; 152, 154; 153, identifier:email; 154, identifier:alias; 155, keyword_argument; 155, 156; 155, 157; 156, identifier:source; 157, attribute; 157, 158; 157, 159; 158, identifier:self; 159, identifier:source; 160, else_clause; 160, 161; 161, block; 161, 162; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:identity; 165, call; 165, 166; 165, 167; 166, identifier:Identity; 167, argument_list; 167, 168; 167, 171; 168, keyword_argument; 168, 169; 168, 170; 169, identifier:username; 170, identifier:alias; 171, keyword_argument; 171, 172; 171, 173; 172, identifier:source; 173, attribute; 173, 174; 173, 175; 174, identifier:self; 175, identifier:source; 176, expression_statement; 176, 177; 177, call; 177, 178; 177, 183; 178, attribute; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:uid; 181, identifier:identities; 182, identifier:append; 183, argument_list; 183, 184; 184, identifier:identity; 185, comment; 186, for_statement; 186, 187; 186, 188; 186, 191; 186, 192; 187, identifier:email; 188, attribute; 188, 189; 188, 190; 189, identifier:self; 190, identifier:__raw_identities; 191, comment; 192, block; 192, 193; 192, 273; 192, 274; 192, 282; 192, 296; 192, 300; 193, if_statement; 193, 194; 193, 199; 193, 208; 193, 231; 194, comparison_operator:in; 194, 195; 194, 196; 195, identifier:email; 196, attribute; 196, 197; 196, 198; 197, identifier:self; 198, identifier:_identities; 199, block; 199, 200; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 203; 202, identifier:uid; 203, subscript; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:self; 206, identifier:_identities; 207, identifier:email; 208, elif_clause; 208, 209; 208, 214; 209, comparison_operator:in; 209, 210; 209, 211; 210, identifier:email; 211, attribute; 211, 212; 211, 213; 212, identifier:self; 213, identifier:__raw_aliases; 214, block; 214, 215; 214, 223; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:canonical; 218, subscript; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:self; 221, identifier:__raw_aliases; 222, identifier:email; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 226; 225, identifier:uid; 226, subscript; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:self; 229, identifier:_identities; 230, identifier:canonical; 231, else_clause; 231, 232; 232, block; 232, 233; 232, 242; 232, 256; 232, 265; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 236; 235, identifier:uid; 236, call; 236, 237; 236, 238; 237, identifier:UniqueIdentity; 238, argument_list; 238, 239; 239, keyword_argument; 239, 240; 239, 241; 240, identifier:uuid; 241, identifier:email; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 245; 244, identifier:identity; 245, call; 245, 246; 245, 247; 246, identifier:Identity; 247, argument_list; 247, 248; 247, 251; 248, keyword_argument; 248, 249; 248, 250; 249, identifier:email; 250, identifier:email; 251, keyword_argument; 251, 252; 251, 253; 252, identifier:source; 253, attribute; 253, 254; 253, 255; 254, identifier:self; 255, identifier:source; 256, expression_statement; 256, 257; 257, call; 257, 258; 257, 263; 258, attribute; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:uid; 261, identifier:identities; 262, identifier:append; 263, argument_list; 263, 264; 264, identifier:identity; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 272; 267, subscript; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:self; 270, identifier:_identities; 271, identifier:email; 272, identifier:uid; 273, comment; 274, expression_statement; 274, 275; 275, assignment; 275, 276; 275, 277; 276, identifier:enrs; 277, subscript; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:self; 280, identifier:__raw_identities; 281, identifier:email; 282, expression_statement; 282, 283; 283, call; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:enrs; 286, identifier:sort; 287, argument_list; 287, 288; 288, keyword_argument; 288, 289; 288, 290; 289, identifier:key; 290, lambda; 290, 291; 290, 293; 291, lambda_parameters; 291, 292; 292, identifier:r; 293, subscript; 293, 294; 293, 295; 294, identifier:r; 295, integer:1; 296, expression_statement; 296, 297; 297, assignment; 297, 298; 297, 299; 298, identifier:start_date; 299, identifier:MIN_PERIOD_DATE; 300, for_statement; 300, 301; 300, 302; 300, 303; 301, identifier:rol; 302, identifier:enrs; 303, block; 303, 304; 303, 310; 303, 322; 303, 343; 303, 349; 303, 364; 303, 373; 304, expression_statement; 304, 305; 305, assignment; 305, 306; 305, 307; 306, identifier:name; 307, subscript; 307, 308; 307, 309; 308, identifier:rol; 309, integer:0; 310, expression_statement; 310, 311; 311, assignment; 311, 312; 311, 313; 312, identifier:org; 313, call; 313, 314; 313, 319; 314, attribute; 314, 315; 314, 318; 315, attribute; 315, 316; 315, 317; 316, identifier:self; 317, identifier:_organizations; 318, identifier:get; 319, argument_list; 319, 320; 319, 321; 320, identifier:name; 321, None; 322, if_statement; 322, 323; 322, 325; 323, not_operator; 323, 324; 324, identifier:org; 325, block; 325, 326; 325, 335; 326, expression_statement; 326, 327; 327, assignment; 327, 328; 327, 329; 328, identifier:org; 329, call; 329, 330; 329, 331; 330, identifier:Organization; 331, argument_list; 331, 332; 332, keyword_argument; 332, 333; 332, 334; 333, identifier:name; 334, identifier:name; 335, expression_statement; 335, 336; 336, assignment; 336, 337; 336, 342; 337, subscript; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, identifier:self; 340, identifier:_organizations; 341, identifier:name; 342, identifier:org; 343, expression_statement; 343, 344; 344, assignment; 344, 345; 344, 346; 345, identifier:end_date; 346, subscript; 346, 347; 346, 348; 347, identifier:rol; 348, integer:1; 349, expression_statement; 349, 350; 350, assignment; 350, 351; 350, 352; 351, identifier:enrollment; 352, call; 352, 353; 352, 354; 353, identifier:Enrollment; 354, argument_list; 354, 355; 354, 358; 354, 361; 355, keyword_argument; 355, 356; 355, 357; 356, identifier:start; 357, identifier:start_date; 358, keyword_argument; 358, 359; 358, 360; 359, identifier:end; 360, identifier:end_date; 361, keyword_argument; 361, 362; 361, 363; 362, identifier:organization; 363, identifier:org; 364, expression_statement; 364, 365; 365, call; 365, 366; 365, 371; 366, attribute; 366, 367; 366, 370; 367, attribute; 367, 368; 367, 369; 368, identifier:uid; 369, identifier:enrollments; 370, identifier:append; 371, argument_list; 371, 372; 372, identifier:enrollment; 373, if_statement; 373, 374; 373, 377; 374, comparison_operator:!=; 374, 375; 374, 376; 375, identifier:end_date; 376, identifier:MAX_PERIOD_DATE; 377, block; 377, 378; 378, expression_statement; 378, 379; 379, assignment; 379, 380; 379, 381; 380, identifier:start_date; 381, identifier:end_date
def __parse_identities(self, aliases, email_to_employer): """Parse Gitdm identities""" # Parse streams self.__parse_aliases_stream(aliases) self.__parse_email_to_employer_stream(email_to_employer) # Create unique identities from aliases list for alias, email in self.__raw_aliases.items(): uid = self._identities.get(email, None) if not uid: uid = UniqueIdentity(uuid=email) e = re.match(self.EMAIL_ADDRESS_REGEX, email, re.UNICODE) if e: identity = Identity(email=email, source=self.source) else: identity = Identity(username=email, source=self.source) uid.identities.append(identity) self._identities[email] = uid e = re.match(self.EMAIL_ADDRESS_REGEX, alias, re.UNICODE) if e: identity = Identity(email=alias, source=self.source) else: identity = Identity(username=alias, source=self.source) uid.identities.append(identity) # Create unique identities from enrollments list for email in self.__raw_identities: # Do we have it from aliases? if email in self._identities: uid = self._identities[email] elif email in self.__raw_aliases: canonical = self.__raw_aliases[email] uid = self._identities[canonical] else: uid = UniqueIdentity(uuid=email) identity = Identity(email=email, source=self.source) uid.identities.append(identity) self._identities[email] = uid # Assign enrollments enrs = self.__raw_identities[email] enrs.sort(key=lambda r: r[1]) start_date = MIN_PERIOD_DATE for rol in enrs: name = rol[0] org = self._organizations.get(name, None) if not org: org = Organization(name=name) self._organizations[name] = org end_date = rol[1] enrollment = Enrollment(start=start_date, end=end_date, organization=org) uid.enrollments.append(enrollment) if end_date != MAX_PERIOD_DATE: start_date = end_date
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:initialize; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:name; 6, default_parameter; 6, 7; 6, 8; 7, identifier:reuse; 8, False; 9, block; 9, 10; 9, 12; 9, 20; 9, 28; 9, 36; 9, 44; 9, 60; 9, 162; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:user; 15, subscript; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:_kwargs; 19, string:'user'; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:password; 23, subscript; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:_kwargs; 27, string:'password'; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:host; 31, subscript; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:_kwargs; 35, string:'host'; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:port; 39, subscript; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:_kwargs; 43, string:'port'; 44, if_statement; 44, 45; 44, 48; 45, comparison_operator:in; 45, 46; 45, 47; 46, string:'-'; 47, identifier:name; 48, block; 48, 49; 48, 58; 49, expression_statement; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:error; 54, argument_list; 54, 55; 55, binary_operator:%; 55, 56; 55, 57; 56, string:"dabase name '%s' cannot contain '-' characters"; 57, identifier:name; 58, return_statement; 58, 59; 59, identifier:CODE_VALUE_ERROR; 60, try_statement; 60, 61; 60, 93; 60, 115; 60, 133; 61, block; 61, 62; 61, 73; 61, 74; 61, 85; 61, 86; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:Database; 66, identifier:create; 67, argument_list; 67, 68; 67, 69; 67, 70; 67, 71; 67, 72; 68, identifier:user; 69, identifier:password; 70, identifier:name; 71, identifier:host; 72, identifier:port; 73, comment; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:db; 77, call; 77, 78; 77, 79; 78, identifier:Database; 79, argument_list; 79, 80; 79, 81; 79, 82; 79, 83; 79, 84; 80, identifier:user; 81, identifier:password; 82, identifier:name; 83, identifier:host; 84, identifier:port; 85, comment; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:__load_countries; 91, argument_list; 91, 92; 92, identifier:db; 93, except_clause; 93, 94; 93, 98; 94, as_pattern; 94, 95; 94, 96; 95, identifier:DatabaseExists; 96, as_pattern_target; 96, 97; 97, identifier:e; 98, block; 98, 99; 99, if_statement; 99, 100; 99, 102; 100, not_operator; 100, 101; 101, identifier:reuse; 102, block; 102, 103; 102, 113; 103, expression_statement; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:self; 107, identifier:error; 108, argument_list; 108, 109; 109, call; 109, 110; 109, 111; 110, identifier:str; 111, argument_list; 111, 112; 112, identifier:e; 113, return_statement; 113, 114; 114, identifier:CODE_DATABASE_EXISTS; 115, except_clause; 115, 116; 115, 120; 116, as_pattern; 116, 117; 116, 118; 117, identifier:DatabaseError; 118, as_pattern_target; 118, 119; 119, identifier:e; 120, block; 120, 121; 120, 131; 121, expression_statement; 121, 122; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:error; 126, argument_list; 126, 127; 127, call; 127, 128; 127, 129; 128, identifier:str; 129, argument_list; 129, 130; 130, identifier:e; 131, return_statement; 131, 132; 132, identifier:CODE_DATABASE_ERROR; 133, except_clause; 133, 134; 133, 138; 134, as_pattern; 134, 135; 134, 136; 135, identifier:LoadError; 136, as_pattern_target; 136, 137; 137, identifier:e; 138, block; 138, 139; 138, 150; 138, 160; 139, expression_statement; 139, 140; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:Database; 143, identifier:drop; 144, argument_list; 144, 145; 144, 146; 144, 147; 144, 148; 144, 149; 145, identifier:user; 146, identifier:password; 147, identifier:name; 148, identifier:host; 149, identifier:port; 150, expression_statement; 150, 151; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:self; 154, identifier:error; 155, argument_list; 155, 156; 156, call; 156, 157; 156, 158; 157, identifier:str; 158, argument_list; 158, 159; 159, identifier:e; 160, return_statement; 160, 161; 161, identifier:CODE_LOAD_ERROR; 162, return_statement; 162, 163; 163, identifier:CMD_SUCCESS
def initialize(self, name, reuse=False): """Create an empty Sorting Hat registry. This method creates a new database including the schema of Sorting Hat. Any attempt to create a new registry over an existing instance will produce an error, except if reuse=True. In that case, the database will be reused, assuming the database schema is correct (it won't be created in this case). :param name: name of the database :param reuse: reuse database if it already exists """ user = self._kwargs['user'] password = self._kwargs['password'] host = self._kwargs['host'] port = self._kwargs['port'] if '-' in name: self.error("dabase name '%s' cannot contain '-' characters" % name) return CODE_VALUE_ERROR try: Database.create(user, password, name, host, port) # Try to access and create schema db = Database(user, password, name, host, port) # Load countries list self.__load_countries(db) except DatabaseExists as e: if not reuse: self.error(str(e)) return CODE_DATABASE_EXISTS except DatabaseError as e: self.error(str(e)) return CODE_DATABASE_ERROR except LoadError as e: Database.drop(user, password, name, host, port) self.error(str(e)) return CODE_LOAD_ERROR return CMD_SUCCESS
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:merge_date_ranges; 3, parameters; 3, 4; 4, identifier:dates; 5, block; 5, 6; 5, 8; 5, 13; 5, 27; 5, 36; 5, 155; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 11; 9, not_operator; 9, 10; 10, identifier:dates; 11, block; 11, 12; 12, return_statement; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:sorted_dates; 16, call; 16, 17; 16, 18; 17, identifier:sorted; 18, argument_list; 18, 19; 19, list_comprehension; 19, 20; 19, 24; 20, call; 20, 21; 20, 22; 21, identifier:sorted; 22, argument_list; 22, 23; 23, identifier:t; 24, for_in_clause; 24, 25; 24, 26; 25, identifier:t; 26, identifier:dates; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:saved; 30, call; 30, 31; 30, 32; 31, identifier:list; 32, argument_list; 32, 33; 33, subscript; 33, 34; 33, 35; 34, identifier:sorted_dates; 35, integer:0; 36, for_statement; 36, 37; 36, 40; 36, 41; 37, pattern_list; 37, 38; 37, 39; 38, identifier:st; 39, identifier:en; 40, identifier:sorted_dates; 41, block; 41, 42; 41, 61; 41, 80; 42, if_statement; 42, 43; 42, 50; 43, boolean_operator:or; 43, 44; 43, 47; 44, comparison_operator:<; 44, 45; 44, 46; 45, identifier:st; 46, identifier:MIN_PERIOD_DATE; 47, comparison_operator:>; 47, 48; 47, 49; 48, identifier:st; 49, identifier:MAX_PERIOD_DATE; 50, block; 50, 51; 51, raise_statement; 51, 52; 52, call; 52, 53; 52, 54; 53, identifier:ValueError; 54, argument_list; 54, 55; 55, binary_operator:%; 55, 56; 55, 57; 56, string:"start date %s is out of bounds"; 57, call; 57, 58; 57, 59; 58, identifier:str; 59, argument_list; 59, 60; 60, identifier:st; 61, if_statement; 61, 62; 61, 69; 62, boolean_operator:or; 62, 63; 62, 66; 63, comparison_operator:<; 63, 64; 63, 65; 64, identifier:en; 65, identifier:MIN_PERIOD_DATE; 66, comparison_operator:>; 66, 67; 66, 68; 67, identifier:en; 68, identifier:MAX_PERIOD_DATE; 69, block; 69, 70; 70, raise_statement; 70, 71; 71, call; 71, 72; 71, 73; 72, identifier:ValueError; 73, argument_list; 73, 74; 74, binary_operator:%; 74, 75; 74, 76; 75, string:"end date %s is out of bounds"; 76, call; 76, 77; 76, 78; 77, identifier:str; 78, argument_list; 78, 79; 79, identifier:en; 80, if_statement; 80, 81; 80, 86; 80, 135; 81, comparison_operator:<=; 81, 82; 81, 83; 82, identifier:st; 83, subscript; 83, 84; 83, 85; 84, identifier:saved; 85, integer:1; 86, block; 86, 87; 86, 100; 87, if_statement; 87, 88; 87, 93; 88, comparison_operator:==; 88, 89; 88, 92; 89, subscript; 89, 90; 89, 91; 90, identifier:saved; 91, integer:0; 92, identifier:MIN_PERIOD_DATE; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 99; 96, subscript; 96, 97; 96, 98; 97, identifier:saved; 98, integer:0; 99, identifier:st; 100, if_statement; 100, 101; 100, 108; 100, 121; 101, comparison_operator:in; 101, 102; 101, 103; 102, identifier:MAX_PERIOD_DATE; 103, tuple; 103, 104; 103, 105; 104, identifier:en; 105, subscript; 105, 106; 105, 107; 106, identifier:saved; 107, integer:1; 108, block; 108, 109; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 114; 111, subscript; 111, 112; 111, 113; 112, identifier:saved; 113, integer:1; 114, call; 114, 115; 114, 116; 115, identifier:min; 116, argument_list; 116, 117; 116, 120; 117, subscript; 117, 118; 117, 119; 118, identifier:saved; 119, integer:1; 120, identifier:en; 121, else_clause; 121, 122; 122, block; 122, 123; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 128; 125, subscript; 125, 126; 125, 127; 126, identifier:saved; 127, integer:1; 128, call; 128, 129; 128, 130; 129, identifier:max; 130, argument_list; 130, 131; 130, 134; 131, subscript; 131, 132; 131, 133; 132, identifier:saved; 133, integer:1; 134, identifier:en; 135, else_clause; 135, 136; 136, block; 136, 137; 136, 143; 136, 149; 137, expression_statement; 137, 138; 138, yield; 138, 139; 139, call; 139, 140; 139, 141; 140, identifier:tuple; 141, argument_list; 141, 142; 142, identifier:saved; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 148; 145, subscript; 145, 146; 145, 147; 146, identifier:saved; 147, integer:0; 148, identifier:st; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 154; 151, subscript; 151, 152; 151, 153; 152, identifier:saved; 153, integer:1; 154, identifier:en; 155, expression_statement; 155, 156; 156, yield; 156, 157; 157, call; 157, 158; 157, 159; 158, identifier:tuple; 159, argument_list; 159, 160; 160, identifier:saved
def merge_date_ranges(dates): """Merge date ranges. Generator that merges ovelaped data ranges. Default init and end dates (1900-01-01 and 2100-01-01) are considered range limits and will be removed when a set of ranges overlap. For example: * [(1900-01-01, 2010-01-01), (2008-01-01, 2100-01-01)] --> (2008-01-01, 2010-01-01) * [(1900-01-01, 2010-01-01), (2008-01-01, 2010-01-01), (2010-01-02, 2100-01-01)] --> (2008-01-01, 2010-01-01),(2010-01-02, 2100-01-01) * [(1900-01-01, 2010-01-01), (2010-01-02, 2100-01-01)] --> (1900-01-01, 2010-01-01), (2010-01-02, 2100-01-01) The condition MIN_PERIOD_DATE <= dt <= MAX_PERIOD_DATE must be true for each date. Otherwise, the generator will raise a ValueError exception. This code is based on samplebias' answer to StackOverflow's question "Merging a list of time-range tuples that have overlapping time-ranges" (http://stackoverflow.com/questions/5679638). :param dates: sequence of date ranges where each range is a (st_date, en_date) tuple :raises ValueError: when a value of the data range is out of bounds """ if not dates: return sorted_dates = sorted([sorted(t) for t in dates]) saved = list(sorted_dates[0]) for st, en in sorted_dates: if st < MIN_PERIOD_DATE or st > MAX_PERIOD_DATE: raise ValueError("start date %s is out of bounds" % str(st)) if en < MIN_PERIOD_DATE or en > MAX_PERIOD_DATE: raise ValueError("end date %s is out of bounds" % str(en)) if st <= saved[1]: if saved[0] == MIN_PERIOD_DATE: saved[0] = st if MAX_PERIOD_DATE in (en, saved[1]): saved[1] = min(saved[1], en) else: saved[1] = max(saved[1], en) else: yield tuple(saved) saved[0] = st saved[1] = en yield tuple(saved)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:uuid; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:source; 5, default_parameter; 5, 6; 5, 7; 6, identifier:email; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:name; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:username; 13, None; 14, block; 14, 15; 14, 17; 14, 27; 14, 37; 14, 51; 14, 83; 14, 100; 14, 108; 15, expression_statement; 15, 16; 16, comment; 17, if_statement; 17, 18; 17, 21; 18, comparison_operator:is; 18, 19; 18, 20; 19, identifier:source; 20, None; 21, block; 21, 22; 22, raise_statement; 22, 23; 23, call; 23, 24; 23, 25; 24, identifier:ValueError; 25, argument_list; 25, 26; 26, string:"source cannot be None"; 27, if_statement; 27, 28; 27, 31; 28, comparison_operator:==; 28, 29; 28, 30; 29, identifier:source; 30, string:''; 31, block; 31, 32; 32, raise_statement; 32, 33; 33, call; 33, 34; 33, 35; 34, identifier:ValueError; 35, argument_list; 35, 36; 36, string:"source cannot be an empty string"; 37, if_statement; 37, 38; 37, 45; 38, not_operator; 38, 39; 39, parenthesized_expression; 39, 40; 40, boolean_operator:or; 40, 41; 40, 44; 41, boolean_operator:or; 41, 42; 41, 43; 42, identifier:email; 43, identifier:name; 44, identifier:username; 45, block; 45, 46; 46, raise_statement; 46, 47; 47, call; 47, 48; 47, 49; 48, identifier:ValueError; 49, argument_list; 49, 50; 50, string:"identity data cannot be None or empty"; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:s; 54, call; 54, 55; 54, 82; 55, attribute; 55, 56; 55, 81; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, string:':'; 59, identifier:join; 60, argument_list; 60, 61; 61, tuple; 61, 62; 61, 66; 61, 70; 61, 77; 62, call; 62, 63; 62, 64; 63, identifier:to_unicode; 64, argument_list; 64, 65; 65, identifier:source; 66, call; 66, 67; 66, 68; 67, identifier:to_unicode; 68, argument_list; 68, 69; 69, identifier:email; 70, call; 70, 71; 70, 72; 71, identifier:to_unicode; 72, argument_list; 72, 73; 72, 74; 73, identifier:name; 74, keyword_argument; 74, 75; 74, 76; 75, identifier:unaccent; 76, True; 77, call; 77, 78; 77, 79; 78, identifier:to_unicode; 79, argument_list; 79, 80; 80, identifier:username; 81, identifier:lower; 82, argument_list; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:sha1; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:hashlib; 89, identifier:sha1; 90, argument_list; 90, 91; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:s; 94, identifier:encode; 95, argument_list; 95, 96; 95, 97; 96, string:'UTF-8'; 97, keyword_argument; 97, 98; 97, 99; 98, identifier:errors; 99, string:"surrogateescape"; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:uuid_; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:sha1; 106, identifier:hexdigest; 107, argument_list; 108, return_statement; 108, 109; 109, identifier:uuid_
def uuid(source, email=None, name=None, username=None): """Get the UUID related to the identity data. Based on the input data, the function will return the UUID associated to an identity. On this version, the UUID will be the SHA1 of "source:email:name:username" string. This string is case insensitive, which means same values for the input parameters in upper or lower case will produce the same UUID. The value of 'name' will converted to its unaccent form which means same values with accent or unnacent chars (i.e 'ö and o') will generate the same UUID. For instance, these combinations will produce the same UUID: ('scm', '[email protected]', 'John Smith', 'jsmith'), ('scm', 'jsmith@example,com', 'Jöhn Smith', 'jsmith'), ('scm', '[email protected]', 'John Smith', 'JSMITH'), ('scm', '[email protected]', 'john Smith', 'jsmith') :param source: data source :param email: email of the identity :param name: full name of the identity :param username: user name used by the identity :returns: a universal unique identifier for Sorting Hat :raises ValueError: when source is None or empty; each one of the parameters is None; parameters are empty. """ if source is None: raise ValueError("source cannot be None") if source == '': raise ValueError("source cannot be an empty string") if not (email or name or username): raise ValueError("identity data cannot be None or empty") s = ':'.join((to_unicode(source), to_unicode(email), to_unicode(name, unaccent=True), to_unicode(username))).lower() sha1 = hashlib.sha1(s.encode('UTF-8', errors="surrogateescape")) uuid_ = sha1.hexdigest() return uuid_
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 2, function_name:call; 3, parameters; 3, 4; 3, 5; 3, 9; 3, 11; 3, 16; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:method_name; 7, type; 7, 8; 8, identifier:str; 9, list_splat_pattern; 9, 10; 10, identifier:args; 11, typed_default_parameter; 11, 12; 11, 13; 11, 15; 12, identifier:rpc_timeout; 13, type; 13, 14; 14, identifier:float; 15, None; 16, dictionary_splat_pattern; 16, 17; 17, identifier:kwargs; 18, block; 18, 19; 18, 21; 18, 34; 18, 42; 18, 55; 18, 56; 18, 67; 18, 75; 18, 171; 18, 182; 19, expression_statement; 19, 20; 20, comment; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:request; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:utils; 27, identifier:rpc_request; 28, argument_list; 28, 29; 28, 30; 28, 32; 29, identifier:method_name; 30, list_splat; 30, 31; 31, identifier:args; 32, dictionary_splat; 32, 33; 33, identifier:kwargs; 34, expression_statement; 34, 35; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:_log; 38, identifier:debug; 39, argument_list; 39, 40; 39, 41; 40, string:"Sending request: %s"; 41, identifier:request; 42, expression_statement; 42, 43; 43, call; 43, 44; 43, 49; 44, attribute; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:_socket; 48, identifier:send_multipart; 49, argument_list; 49, 50; 50, list:[to_msgpack(request)]; 50, 51; 51, call; 51, 52; 51, 53; 52, identifier:to_msgpack; 53, argument_list; 53, 54; 54, identifier:request; 55, comment; 56, if_statement; 56, 57; 56, 60; 57, comparison_operator:is; 57, 58; 57, 59; 58, identifier:rpc_timeout; 59, None; 60, block; 60, 61; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:rpc_timeout; 64, attribute; 64, 65; 64, 66; 65, identifier:self; 66, identifier:rpc_timeout; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:start_time; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:time; 73, identifier:time; 74, argument_list; 75, while_statement; 75, 76; 75, 77; 75, 78; 75, 79; 75, 80; 76, True; 77, comment; 78, comment; 79, comment; 80, block; 80, 81; 80, 105; 80, 122; 80, 133; 80, 140; 80, 148; 80, 149; 80, 150; 80, 151; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:timeout; 84, conditional_expression:if; 84, 85; 84, 101; 84, 104; 85, call; 85, 86; 85, 87; 86, identifier:max; 87, argument_list; 87, 88; 87, 100; 88, binary_operator:*; 88, 89; 88, 99; 89, parenthesized_expression; 89, 90; 90, binary_operator:-; 90, 91; 90, 94; 91, binary_operator:+; 91, 92; 91, 93; 92, identifier:start_time; 93, identifier:rpc_timeout; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:time; 97, identifier:time; 98, argument_list; 99, integer:1000; 100, integer:0; 101, comparison_operator:is; 101, 102; 101, 103; 102, identifier:rpc_timeout; 103, None; 104, None; 105, if_statement; 105, 106; 105, 116; 106, comparison_operator:==; 106, 107; 106, 115; 107, call; 107, 108; 107, 113; 108, attribute; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:_socket; 112, identifier:poll; 113, argument_list; 113, 114; 114, identifier:timeout; 115, integer:0; 116, block; 116, 117; 117, raise_statement; 117, 118; 118, call; 118, 119; 118, 120; 119, identifier:TimeoutError; 120, argument_list; 120, 121; 121, string:f"Timeout on client {self.endpoint}, method name {method_name}, class info: {self}"; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 126; 124, pattern_list; 124, 125; 125, identifier:raw_reply; 126, call; 126, 127; 126, 132; 127, attribute; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:_socket; 131, identifier:recv_multipart; 132, argument_list; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:reply; 136, call; 136, 137; 136, 138; 137, identifier:from_msgpack; 138, argument_list; 138, 139; 139, identifier:raw_reply; 140, expression_statement; 140, 141; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:_log; 144, identifier:debug; 145, argument_list; 145, 146; 145, 147; 146, string:"Received reply: %s"; 147, identifier:reply; 148, comment; 149, comment; 150, comment; 151, if_statement; 151, 152; 151, 159; 151, 161; 152, comparison_operator:==; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:reply; 155, identifier:id; 156, attribute; 156, 157; 156, 158; 157, identifier:request; 158, identifier:id; 159, block; 159, 160; 160, break_statement; 161, else_clause; 161, 162; 162, block; 162, 163; 163, expression_statement; 163, 164; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:_log; 167, identifier:debug; 168, argument_list; 168, 169; 168, 170; 169, string:'Discarding reply: %s'; 170, identifier:reply; 171, for_statement; 171, 172; 171, 173; 171, 176; 172, identifier:warning; 173, attribute; 173, 174; 173, 175; 174, identifier:reply; 175, identifier:warnings; 176, block; 176, 177; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 180; 179, identifier:warn; 180, argument_list; 180, 181; 181, string:f"{warning.kind}: {warning.body}"; 182, if_statement; 182, 183; 182, 188; 182, 198; 183, call; 183, 184; 183, 185; 184, identifier:isinstance; 185, argument_list; 185, 186; 185, 187; 186, identifier:reply; 187, identifier:RPCError; 188, block; 188, 189; 189, raise_statement; 189, 190; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:utils; 193, identifier:RPCError; 194, argument_list; 194, 195; 195, attribute; 195, 196; 195, 197; 196, identifier:reply; 197, identifier:error; 198, else_clause; 198, 199; 199, block; 199, 200; 200, return_statement; 200, 201; 201, attribute; 201, 202; 201, 203; 202, identifier:reply; 203, identifier:result
def call(self, method_name: str, *args, rpc_timeout: float = None, **kwargs): """ Send JSON RPC request to a backend socket and receive reply Note that this uses the default event loop to run in a blocking manner. If you would rather run in an async fashion or provide your own event loop then use .async_call instead :param method_name: Method name :param args: Args that will be passed to the remote function :param float rpc_timeout: Timeout in seconds for Server response, set to None to disable the timeout :param kwargs: Keyword args that will be passed to the remote function """ request = utils.rpc_request(method_name, *args, **kwargs) _log.debug("Sending request: %s", request) self._socket.send_multipart([to_msgpack(request)]) # if an rpc_timeout override is not specified, use the one set in the Client attributes if rpc_timeout is None: rpc_timeout = self.rpc_timeout start_time = time.time() while True: # Need to keep track of timeout manually in case this loop runs more than once. We subtract off already # elapsed time from the timeout. The call to max is to make sure we don't send a negative value # which would throw an error. timeout = max((start_time + rpc_timeout - time.time()) * 1000, 0) if rpc_timeout is not None else None if self._socket.poll(timeout) == 0: raise TimeoutError(f"Timeout on client {self.endpoint}, method name {method_name}, class info: {self}") raw_reply, = self._socket.recv_multipart() reply = from_msgpack(raw_reply) _log.debug("Received reply: %s", reply) # there's a possibility that the socket will have some leftover replies from a previous # request on it if that .call() was cancelled or timed out. Therefore, we need to discard replies that # don't match the request just like in the call_async case. if reply.id == request.id: break else: _log.debug('Discarding reply: %s', reply) for warning in reply.warnings: warn(f"{warning.kind}: {warning.body}") if isinstance(reply, RPCError): raise utils.RPCError(reply.error) else: return reply.result
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:init_process_dut; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:contextlist; 5, identifier:conf; 6, identifier:index; 7, identifier:args; 8, block; 8, 9; 8, 11; 8, 314; 8, 323; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 19; 11, 100; 12, boolean_operator:and; 12, 13; 12, 16; 13, comparison_operator:in; 13, 14; 13, 15; 14, string:"subtype"; 15, identifier:conf; 16, subscript; 16, 17; 16, 18; 17, identifier:conf; 18, string:"subtype"; 19, block; 19, 20; 19, 52; 19, 53; 19, 57; 19, 68; 19, 77; 19, 94; 20, if_statement; 20, 21; 20, 26; 21, comparison_operator:!=; 21, 22; 21, 25; 22, subscript; 22, 23; 22, 24; 23, identifier:conf; 24, string:"subtype"; 25, string:"console"; 26, block; 26, 27; 26, 31; 26, 47; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:msg; 30, string:"Unrecognized process subtype: {}"; 31, expression_statement; 31, 32; 32, call; 32, 33; 32, 38; 33, attribute; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:contextlist; 36, identifier:logger; 37, identifier:error; 38, argument_list; 38, 39; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:msg; 42, identifier:format; 43, argument_list; 43, 44; 44, subscript; 44, 45; 44, 46; 45, identifier:conf; 46, string:"subtype"; 47, raise_statement; 47, 48; 48, call; 48, 49; 48, 50; 49, identifier:ResourceInitError; 50, argument_list; 50, 51; 51, string:"Unrecognized process subtype: {}"; 52, comment; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:config; 56, None; 57, if_statement; 57, 58; 57, 61; 58, comparison_operator:in; 58, 59; 58, 60; 59, string:"application"; 60, identifier:conf; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:config; 65, subscript; 65, 66; 65, 67; 66, identifier:conf; 67, string:"application"; 68, expression_statement; 68, 69; 69, call; 69, 70; 69, 75; 70, attribute; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:contextlist; 73, identifier:logger; 74, identifier:debug; 75, argument_list; 75, 76; 76, string:"Starting a remote console"; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:dut; 80, call; 80, 81; 80, 82; 81, identifier:DutConsole; 82, argument_list; 82, 83; 82, 88; 82, 91; 83, keyword_argument; 83, 84; 83, 85; 84, identifier:name; 85, binary_operator:%; 85, 86; 85, 87; 86, string:"D%d"; 87, identifier:index; 88, keyword_argument; 88, 89; 88, 90; 89, identifier:conf; 90, identifier:config; 91, keyword_argument; 91, 92; 91, 93; 92, identifier:params; 93, identifier:args; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:dut; 98, identifier:index; 99, identifier:index; 100, else_clause; 100, 101; 101, block; 101, 102; 101, 110; 101, 116; 101, 126; 101, 136; 101, 147; 101, 164; 101, 170; 101, 176; 101, 203; 101, 227; 101, 255; 101, 279; 101, 290; 101, 302; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:binary; 105, subscript; 105, 106; 105, 109; 106, subscript; 106, 107; 106, 108; 107, identifier:conf; 108, string:"application"; 109, string:'bin'; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:app_config; 113, subscript; 113, 114; 113, 115; 114, identifier:conf; 115, string:"application"; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:init_cli_cmds; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:app_config; 122, identifier:get; 123, argument_list; 123, 124; 123, 125; 124, string:"init_cli_cmds"; 125, None; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:post_cli_cmds; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:app_config; 132, identifier:get; 133, argument_list; 133, 134; 133, 135; 134, string:"post_cli_cmds"; 135, None; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 143; 138, attribute; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:contextlist; 141, identifier:logger; 142, identifier:debug; 143, argument_list; 143, 144; 144, binary_operator:%; 144, 145; 144, 146; 145, string:"Starting process '%s'"; 146, identifier:binary; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:dut; 150, call; 150, 151; 150, 152; 151, identifier:DutProcess; 152, argument_list; 152, 153; 152, 158; 152, 161; 153, keyword_argument; 153, 154; 153, 155; 154, identifier:name; 155, binary_operator:%; 155, 156; 155, 157; 156, string:"D%d"; 157, identifier:index; 158, keyword_argument; 158, 159; 158, 160; 159, identifier:config; 160, identifier:conf; 161, keyword_argument; 161, 162; 161, 163; 162, identifier:params; 163, identifier:args; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:dut; 168, identifier:index; 169, identifier:index; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:dut; 174, identifier:command; 175, identifier:binary; 176, if_statement; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:args; 179, identifier:valgrind; 180, block; 180, 181; 181, expression_statement; 181, 182; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:dut; 185, identifier:use_valgrind; 186, argument_list; 186, 187; 186, 190; 186, 194; 186, 197; 186, 200; 187, attribute; 187, 188; 187, 189; 188, identifier:args; 189, identifier:valgrind_tool; 190, not_operator; 190, 191; 191, attribute; 191, 192; 191, 193; 192, identifier:args; 193, identifier:valgrind_text; 194, attribute; 194, 195; 194, 196; 195, identifier:args; 196, identifier:valgrind_console; 197, attribute; 197, 198; 197, 199; 198, identifier:args; 199, identifier:valgrind_track_origins; 200, attribute; 200, 201; 200, 202; 201, identifier:args; 202, identifier:valgrind_extra_params; 203, if_statement; 203, 204; 203, 209; 204, comparison_operator:==; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:args; 207, identifier:gdb; 208, identifier:index; 209, block; 209, 210; 209, 216; 210, expression_statement; 210, 211; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:dut; 214, identifier:use_gdb; 215, argument_list; 216, expression_statement; 216, 217; 217, call; 217, 218; 217, 223; 218, attribute; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:contextlist; 221, identifier:logger; 222, identifier:info; 223, argument_list; 223, 224; 224, binary_operator:%; 224, 225; 224, 226; 225, string:"GDB is activated for node %i"; 226, identifier:index; 227, if_statement; 227, 228; 227, 233; 228, comparison_operator:==; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:args; 231, identifier:gdbs; 232, identifier:index; 233, block; 233, 234; 233, 244; 234, expression_statement; 234, 235; 235, call; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:dut; 238, identifier:use_gdbs; 239, argument_list; 239, 240; 239, 241; 240, True; 241, attribute; 241, 242; 241, 243; 242, identifier:args; 243, identifier:gdbs_port; 244, expression_statement; 244, 245; 245, call; 245, 246; 245, 251; 246, attribute; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:contextlist; 249, identifier:logger; 250, identifier:info; 251, argument_list; 251, 252; 252, binary_operator:%; 252, 253; 252, 254; 253, string:"GDBserver is activated for node %i"; 254, identifier:index; 255, if_statement; 255, 256; 255, 261; 256, comparison_operator:==; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:args; 259, identifier:vgdb; 260, identifier:index; 261, block; 261, 262; 261, 268; 262, expression_statement; 262, 263; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:dut; 266, identifier:use_vgdb; 267, argument_list; 268, expression_statement; 268, 269; 269, call; 269, 270; 269, 275; 270, attribute; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:contextlist; 273, identifier:logger; 274, identifier:info; 275, argument_list; 275, 276; 276, binary_operator:%; 276, 277; 276, 278; 277, string:"VGDB is activated for node %i"; 278, identifier:index; 279, if_statement; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:args; 282, identifier:nobuf; 283, block; 283, 284; 284, expression_statement; 284, 285; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:dut; 288, identifier:no_std_buf; 289, argument_list; 290, if_statement; 290, 291; 290, 294; 291, comparison_operator:is; 291, 292; 291, 293; 292, identifier:init_cli_cmds; 293, None; 294, block; 294, 295; 295, expression_statement; 295, 296; 296, call; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:dut; 299, identifier:set_init_cli_cmds; 300, argument_list; 300, 301; 301, identifier:init_cli_cmds; 302, if_statement; 302, 303; 302, 306; 303, comparison_operator:is; 303, 304; 303, 305; 304, identifier:post_cli_cmds; 305, None; 306, block; 306, 307; 307, expression_statement; 307, 308; 308, call; 308, 309; 308, 312; 309, attribute; 309, 310; 309, 311; 310, identifier:dut; 311, identifier:set_post_cli_cmds; 312, argument_list; 312, 313; 313, identifier:post_cli_cmds; 314, expression_statement; 314, 315; 315, call; 315, 316; 315, 321; 316, attribute; 316, 317; 316, 320; 317, attribute; 317, 318; 317, 319; 318, identifier:contextlist; 319, identifier:duts; 320, identifier:append; 321, argument_list; 321, 322; 322, identifier:dut; 323, expression_statement; 323, 324; 324, call; 324, 325; 324, 330; 325, attribute; 325, 326; 325, 329; 326, attribute; 326, 327; 326, 328; 327, identifier:contextlist; 328, identifier:dutinformations; 329, identifier:append; 330, argument_list; 330, 331; 331, call; 331, 332; 331, 335; 332, attribute; 332, 333; 332, 334; 333, identifier:dut; 334, identifier:get_info; 335, argument_list
def init_process_dut(contextlist, conf, index, args): """ Initialize process type Dut as DutProcess or DutConsole. """ if "subtype" in conf and conf["subtype"]: if conf["subtype"] != "console": msg = "Unrecognized process subtype: {}" contextlist.logger.error(msg.format(conf["subtype"])) raise ResourceInitError("Unrecognized process subtype: {}") # This is a specialized 'console' process config = None if "application" in conf: config = conf["application"] contextlist.logger.debug("Starting a remote console") dut = DutConsole(name="D%d" % index, conf=config, params=args) dut.index = index else: binary = conf["application"]['bin'] app_config = conf["application"] init_cli_cmds = app_config.get("init_cli_cmds", None) post_cli_cmds = app_config.get("post_cli_cmds", None) contextlist.logger.debug("Starting process '%s'" % binary) dut = DutProcess(name="D%d" % index, config=conf, params=args) dut.index = index dut.command = binary if args.valgrind: dut.use_valgrind(args.valgrind_tool, not args.valgrind_text, args.valgrind_console, args.valgrind_track_origins, args.valgrind_extra_params) if args.gdb == index: dut.use_gdb() contextlist.logger.info("GDB is activated for node %i" % index) if args.gdbs == index: dut.use_gdbs(True, args.gdbs_port) contextlist.logger.info("GDBserver is activated for node %i" % index) if args.vgdb == index: dut.use_vgdb() contextlist.logger.info("VGDB is activated for node %i" % index) if args.nobuf: dut.no_std_buf() if init_cli_cmds is not None: dut.set_init_cli_cmds(init_cli_cmds) if post_cli_cmds is not None: dut.set_post_cli_cmds(post_cli_cmds) contextlist.duts.append(dut) contextlist.dutinformations.append(dut.get_info())
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:allocate; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:dut_configuration_list; 6, default_parameter; 6, 7; 6, 8; 7, identifier:args; 8, None; 9, block; 9, 10; 9, 12; 9, 20; 9, 21; 9, 22; 9, 35; 9, 85; 9, 86; 9, 123; 9, 129; 9, 133; 9, 180; 9, 188; 9, 196; 9, 204; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:dut_config_list; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:dut_configuration_list; 18, identifier:get_dut_configuration; 19, argument_list; 20, comment; 21, comment; 22, if_statement; 22, 23; 22, 29; 23, not_operator; 23, 24; 24, call; 24, 25; 24, 26; 25, identifier:isinstance; 26, argument_list; 26, 27; 26, 28; 27, identifier:dut_config_list; 28, identifier:list; 29, block; 29, 30; 30, raise_statement; 30, 31; 31, call; 31, 32; 31, 33; 32, identifier:AllocationError; 33, argument_list; 33, 34; 34, string:"Invalid dut configuration format!"; 35, if_statement; 35, 36; 35, 54; 36, call; 36, 37; 36, 38; 37, identifier:next; 38, argument_list; 38, 39; 38, 53; 39, generator_expression; 39, 40; 39, 41; 39, 44; 40, identifier:item; 41, for_in_clause; 41, 42; 41, 43; 42, identifier:item; 43, identifier:dut_config_list; 44, if_clause; 44, 45; 45, comparison_operator:==; 45, 46; 45, 52; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:item; 49, identifier:get; 50, argument_list; 50, 51; 51, string:"type"; 52, string:"hardware"; 53, False; 54, block; 54, 55; 54, 67; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:_available_devices; 60, call; 60, 61; 60, 66; 61, attribute; 61, 62; 61, 65; 62, call; 62, 63; 62, 64; 63, identifier:DutDetection; 64, argument_list; 65, identifier:get_available_devices; 66, argument_list; 67, if_statement; 67, 68; 67, 79; 68, comparison_operator:<; 68, 69; 68, 75; 69, call; 69, 70; 69, 71; 70, identifier:len; 71, argument_list; 71, 72; 72, attribute; 72, 73; 72, 74; 73, identifier:self; 74, identifier:_available_devices; 75, call; 75, 76; 75, 77; 76, identifier:len; 77, argument_list; 77, 78; 78, identifier:dut_config_list; 79, block; 79, 80; 80, raise_statement; 80, 81; 81, call; 81, 82; 81, 83; 82, identifier:AllocationError; 83, argument_list; 83, 84; 84, string:"Required amount of devices not available."; 85, comment; 86, try_statement; 86, 87; 86, 117; 87, block; 87, 88; 88, for_statement; 88, 89; 88, 90; 88, 91; 89, identifier:dut_config; 90, identifier:dut_config_list; 91, block; 91, 92; 91, 110; 92, if_statement; 92, 93; 92, 104; 93, not_operator; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:can_allocate; 98, argument_list; 98, 99; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:dut_config; 102, identifier:get_requirements; 103, argument_list; 104, block; 104, 105; 105, raise_statement; 105, 106; 106, call; 106, 107; 106, 108; 107, identifier:AllocationError; 108, argument_list; 108, 109; 109, string:"Resource type is not supported"; 110, expression_statement; 110, 111; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:self; 114, identifier:_allocate; 115, argument_list; 115, 116; 116, identifier:dut_config; 117, except_clause; 117, 118; 117, 119; 117, 120; 117, 121; 118, identifier:AllocationError; 119, comment; 120, comment; 121, block; 121, 122; 122, raise_statement; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:alloc_list; 126, call; 126, 127; 126, 128; 127, identifier:AllocationContextList; 128, argument_list; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:res_id; 132, None; 133, for_statement; 133, 134; 133, 135; 133, 136; 134, identifier:conf; 135, identifier:dut_config_list; 136, block; 136, 137; 136, 161; 136, 173; 137, if_statement; 137, 138; 137, 146; 138, comparison_operator:==; 138, 139; 138, 145; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:conf; 142, identifier:get; 143, argument_list; 143, 144; 144, string:"type"; 145, string:"mbed"; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:res_id; 150, call; 150, 151; 150, 159; 151, attribute; 151, 152; 151, 158; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:conf; 155, identifier:get; 156, argument_list; 156, 157; 157, string:"allocated"; 158, identifier:get; 159, argument_list; 159, 160; 160, string:"target_id"; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:context; 164, call; 164, 165; 164, 166; 165, identifier:AllocationContext; 166, argument_list; 166, 167; 166, 170; 167, keyword_argument; 167, 168; 167, 169; 168, identifier:resource_id; 169, identifier:res_id; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:alloc_data; 172, identifier:conf; 173, expression_statement; 173, 174; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:alloc_list; 177, identifier:append; 178, argument_list; 178, 179; 179, identifier:context; 180, expression_statement; 180, 181; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:alloc_list; 184, identifier:set_dut_init_function; 185, argument_list; 185, 186; 185, 187; 186, string:"serial"; 187, identifier:init_generic_serial_dut; 188, expression_statement; 188, 189; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:alloc_list; 192, identifier:set_dut_init_function; 193, argument_list; 193, 194; 193, 195; 194, string:"process"; 195, identifier:init_process_dut; 196, expression_statement; 196, 197; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:alloc_list; 200, identifier:set_dut_init_function; 201, argument_list; 201, 202; 201, 203; 202, string:"mbed"; 203, identifier:init_mbed_dut; 204, return_statement; 204, 205; 205, identifier:alloc_list
def allocate(self, dut_configuration_list, args=None): """ Allocates resources from available local devices. :param dut_configuration_list: List of ResourceRequirements objects :param args: Not used :return: AllocationContextList with allocated resources """ dut_config_list = dut_configuration_list.get_dut_configuration() # if we need one or more local hardware duts let's search attached # devices using DutDetection if not isinstance(dut_config_list, list): raise AllocationError("Invalid dut configuration format!") if next((item for item in dut_config_list if item.get("type") == "hardware"), False): self._available_devices = DutDetection().get_available_devices() if len(self._available_devices) < len(dut_config_list): raise AllocationError("Required amount of devices not available.") # Enumerate all required DUT's try: for dut_config in dut_config_list: if not self.can_allocate(dut_config.get_requirements()): raise AllocationError("Resource type is not supported") self._allocate(dut_config) except AllocationError: # Locally allocated don't need to be released any way for # now, so just re-raise the error raise alloc_list = AllocationContextList() res_id = None for conf in dut_config_list: if conf.get("type") == "mbed": res_id = conf.get("allocated").get("target_id") context = AllocationContext(resource_id=res_id, alloc_data=conf) alloc_list.append(context) alloc_list.set_dut_init_function("serial", init_generic_serial_dut) alloc_list.set_dut_init_function("process", init_process_dut) alloc_list.set_dut_init_function("mbed", init_mbed_dut) return alloc_list
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 1, 7; 2, function_name:_allocate; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:dut_configuration; 6, comment; 7, block; 7, 8; 7, 10; 7, 25; 7, 276; 7, 277; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 16; 11, comparison_operator:==; 11, 12; 11, 15; 12, subscript; 12, 13; 12, 14; 13, identifier:dut_configuration; 14, string:"type"; 15, string:"hardware"; 16, block; 16, 17; 17, expression_statement; 17, 18; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:dut_configuration; 21, identifier:set; 22, argument_list; 22, 23; 22, 24; 23, string:"type"; 24, string:"mbed"; 25, if_statement; 25, 26; 25, 31; 25, 219; 26, comparison_operator:==; 26, 27; 26, 30; 27, subscript; 27, 28; 27, 29; 28, identifier:dut_configuration; 29, string:"type"; 30, string:"mbed"; 31, block; 31, 32; 31, 43; 31, 51; 31, 62; 31, 73; 31, 86; 31, 101; 31, 102; 31, 213; 31, 214; 32, if_statement; 32, 33; 32, 37; 33, not_operator; 33, 34; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:_available_devices; 37, block; 37, 38; 38, raise_statement; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:AllocationError; 41, argument_list; 41, 42; 42, string:"No available devices to allocate from"; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:dut_reqs; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:dut_configuration; 49, identifier:get_requirements; 50, argument_list; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:platforms; 54, conditional_expression:if; 54, 55; 54, 56; 54, 59; 55, None; 56, comparison_operator:not; 56, 57; 56, 58; 57, string:'allowed_platforms'; 58, identifier:dut_reqs; 59, subscript; 59, 60; 59, 61; 60, identifier:dut_reqs; 61, string:'allowed_platforms'; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:platform_name; 65, conditional_expression:if; 65, 66; 65, 67; 65, 70; 66, None; 67, comparison_operator:not; 67, 68; 67, 69; 68, string:'platform_name'; 69, identifier:dut_reqs; 70, subscript; 70, 71; 70, 72; 71, identifier:dut_reqs; 72, string:"platform_name"; 73, if_statement; 73, 74; 73, 79; 74, boolean_operator:and; 74, 75; 74, 78; 75, comparison_operator:is; 75, 76; 75, 77; 76, identifier:platform_name; 77, None; 78, identifier:platforms; 79, block; 79, 80; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:platform_name; 83, subscript; 83, 84; 83, 85; 84, identifier:platforms; 85, integer:0; 86, if_statement; 86, 87; 86, 90; 87, boolean_operator:and; 87, 88; 87, 89; 88, identifier:platform_name; 89, identifier:platforms; 90, block; 90, 91; 91, if_statement; 91, 92; 91, 95; 92, comparison_operator:not; 92, 93; 92, 94; 93, identifier:platform_name; 94, identifier:platforms; 95, block; 95, 96; 96, raise_statement; 96, 97; 97, call; 97, 98; 97, 99; 98, identifier:AllocationError; 99, argument_list; 99, 100; 100, string:"Platform name not in allowed platforms."; 101, comment; 102, for_statement; 102, 103; 102, 104; 102, 107; 103, identifier:dev; 104, attribute; 104, 105; 104, 106; 105, identifier:self; 106, identifier:_available_devices; 107, block; 107, 108; 107, 136; 107, 158; 108, if_statement; 108, 109; 108, 116; 109, boolean_operator:and; 109, 110; 109, 111; 110, identifier:platform_name; 111, comparison_operator:!=; 111, 112; 111, 115; 112, subscript; 112, 113; 112, 114; 113, identifier:dev; 114, string:"platform_name"; 115, identifier:platform_name; 116, block; 116, 117; 116, 135; 117, expression_statement; 117, 118; 118, call; 118, 119; 118, 124; 119, attribute; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:self; 122, identifier:logger; 123, identifier:debug; 124, argument_list; 124, 125; 124, 128; 124, 131; 124, 132; 125, concatenated_string; 125, 126; 125, 127; 126, string:"Skipping device %s because of mismatching platform. "; 127, string:"Required %s but device was %s"; 128, subscript; 128, 129; 128, 130; 129, identifier:dev; 130, string:'target_id'; 131, identifier:platform_name; 132, subscript; 132, 133; 132, 134; 133, identifier:dev; 134, string:'platform_name'; 135, continue_statement; 136, if_statement; 136, 137; 136, 142; 137, comparison_operator:==; 137, 138; 137, 141; 138, subscript; 138, 139; 138, 140; 139, identifier:dev; 140, string:'state'; 141, string:'allocated'; 142, block; 142, 143; 142, 157; 143, expression_statement; 143, 144; 144, call; 144, 145; 144, 150; 145, attribute; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:self; 148, identifier:logger; 149, identifier:debug; 150, argument_list; 150, 151; 150, 154; 151, concatenated_string; 151, 152; 151, 153; 152, string:"Skipping device %s because it was "; 153, string:"already allocated"; 154, subscript; 154, 155; 154, 156; 155, identifier:dev; 156, string:'target_id'; 157, continue_statement; 158, if_statement; 158, 159; 158, 167; 158, 194; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:DutDetection; 162, identifier:is_port_usable; 163, argument_list; 163, 164; 164, subscript; 164, 165; 164, 166; 165, identifier:dev; 166, string:'serial_port'; 167, block; 167, 168; 167, 174; 167, 180; 167, 192; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 173; 170, subscript; 170, 171; 170, 172; 171, identifier:dev; 172, string:'state'; 173, string:"allocated"; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 179; 176, subscript; 176, 177; 176, 178; 177, identifier:dut_reqs; 178, string:'allocated'; 179, identifier:dev; 180, expression_statement; 180, 181; 181, call; 181, 182; 181, 187; 182, attribute; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:self; 185, identifier:logger; 186, identifier:info; 187, argument_list; 187, 188; 187, 189; 188, string:"Allocated device %s"; 189, subscript; 189, 190; 189, 191; 190, identifier:dev; 191, string:'target_id'; 192, return_statement; 192, 193; 193, True; 194, else_clause; 194, 195; 195, block; 195, 196; 196, expression_statement; 196, 197; 197, call; 197, 198; 197, 203; 198, attribute; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:self; 201, identifier:logger; 202, identifier:info; 203, argument_list; 203, 204; 203, 207; 203, 210; 204, concatenated_string; 204, 205; 204, 206; 205, string:"Could not open serial port (%s) of "; 206, string:"allocated device %s"; 207, subscript; 207, 208; 207, 209; 208, identifier:dev; 209, string:'serial_port'; 210, subscript; 210, 211; 210, 212; 211, identifier:dev; 212, string:'target_id'; 213, comment; 214, raise_statement; 214, 215; 215, call; 215, 216; 215, 217; 216, identifier:AllocationError; 217, argument_list; 217, 218; 218, string:"No suitable local device available"; 219, elif_clause; 219, 220; 219, 225; 220, comparison_operator:==; 220, 221; 220, 224; 221, subscript; 221, 222; 221, 223; 222, identifier:dut_configuration; 223, string:"type"; 224, string:"serial"; 225, block; 225, 226; 225, 234; 225, 253; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 229; 228, identifier:dut_reqs; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:dut_configuration; 232, identifier:get_requirements; 233, argument_list; 234, if_statement; 234, 235; 234, 242; 235, not_operator; 235, 236; 236, call; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:dut_reqs; 239, identifier:get; 240, argument_list; 240, 241; 241, string:"serial_port"; 242, block; 242, 243; 243, raise_statement; 243, 244; 244, call; 244, 245; 244, 246; 245, identifier:AllocationError; 246, argument_list; 246, 247; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, string:"Serial port not defined for requirement {}"; 250, identifier:format; 251, argument_list; 251, 252; 252, identifier:dut_reqs; 253, if_statement; 253, 254; 253, 263; 254, not_operator; 254, 255; 255, call; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:DutDetection; 258, identifier:is_port_usable; 259, argument_list; 259, 260; 260, subscript; 260, 261; 260, 262; 261, identifier:dut_reqs; 262, string:'serial_port'; 263, block; 263, 264; 264, raise_statement; 264, 265; 265, call; 265, 266; 265, 267; 266, identifier:AllocationError; 267, argument_list; 267, 268; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, string:"Serial port {} not usable"; 271, identifier:format; 272, argument_list; 272, 273; 273, subscript; 273, 274; 273, 275; 274, identifier:dut_reqs; 275, string:'serial_port'; 276, comment; 277, return_statement; 277, 278; 278, True
def _allocate(self, dut_configuration): # pylint: disable=too-many-branches """ Internal allocation function. Allocates a single resource based on dut_configuration. :param dut_configuration: ResourceRequirements object which describes a required resource :return: True :raises: AllocationError if suitable resource was not found or if the platform was not allowed to be used. """ if dut_configuration["type"] == "hardware": dut_configuration.set("type", "mbed") if dut_configuration["type"] == "mbed": if not self._available_devices: raise AllocationError("No available devices to allocate from") dut_reqs = dut_configuration.get_requirements() platforms = None if 'allowed_platforms' not in dut_reqs else dut_reqs[ 'allowed_platforms'] platform_name = None if 'platform_name' not in dut_reqs else dut_reqs[ "platform_name"] if platform_name is None and platforms: platform_name = platforms[0] if platform_name and platforms: if platform_name not in platforms: raise AllocationError("Platform name not in allowed platforms.") # Enumerate through all available devices for dev in self._available_devices: if platform_name and dev["platform_name"] != platform_name: self.logger.debug("Skipping device %s because of mismatching platform. " "Required %s but device was %s", dev['target_id'], platform_name, dev['platform_name']) continue if dev['state'] == 'allocated': self.logger.debug("Skipping device %s because it was " "already allocated", dev['target_id']) continue if DutDetection.is_port_usable(dev['serial_port']): dev['state'] = "allocated" dut_reqs['allocated'] = dev self.logger.info("Allocated device %s", dev['target_id']) return True else: self.logger.info("Could not open serial port (%s) of " "allocated device %s", dev['serial_port'], dev['target_id']) # Didn't find a matching device to allocate so allocation failed raise AllocationError("No suitable local device available") elif dut_configuration["type"] == "serial": dut_reqs = dut_configuration.get_requirements() if not dut_reqs.get("serial_port"): raise AllocationError("Serial port not defined for requirement {}".format(dut_reqs)) if not DutDetection.is_port_usable(dut_reqs['serial_port']): raise AllocationError("Serial port {} not usable".format(dut_reqs['serial_port'])) # Successful allocation, return True return True
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:__generate; 3, parameters; 3, 4; 4, identifier:results; 5, block; 5, 6; 5, 8; 5, 21; 5, 22; 5, 26; 5, 30; 5, 34; 5, 38; 5, 102; 5, 318; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 14; 10, pattern_list; 10, 11; 10, 12; 10, 13; 11, identifier:doc; 12, identifier:tag; 13, identifier:text; 14, call; 14, 15; 14, 20; 15, attribute; 15, 16; 15, 19; 16, call; 16, 17; 16, 18; 17, identifier:Doc; 18, argument_list; 19, identifier:tagtext; 20, argument_list; 21, comment; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:count; 25, integer:0; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:fails; 29, integer:0; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:errors; 33, integer:0; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:skips; 37, integer:0; 38, for_statement; 38, 39; 38, 40; 38, 41; 38, 42; 39, identifier:result; 40, identifier:results; 41, comment; 42, block; 42, 43; 42, 61; 42, 65; 43, if_statement; 43, 44; 43, 51; 44, comparison_operator:is; 44, 45; 44, 50; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:result; 48, identifier:passed; 49, argument_list; 50, False; 51, block; 51, 52; 52, if_statement; 52, 53; 52, 58; 52, 59; 53, comparison_operator:>; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:result; 56, identifier:retries_left; 57, integer:0; 58, comment; 59, block; 59, 60; 60, continue_statement; 61, expression_statement; 61, 62; 62, augmented_assignment:+=; 62, 63; 62, 64; 63, identifier:count; 64, integer:1; 65, if_statement; 65, 66; 65, 71; 65, 72; 65, 74; 65, 85; 65, 96; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:result; 69, identifier:passed; 70, argument_list; 71, comment; 72, block; 72, 73; 73, continue_statement; 74, elif_clause; 74, 75; 74, 80; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:result; 78, identifier:skipped; 79, argument_list; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, augmented_assignment:+=; 82, 83; 82, 84; 83, identifier:skips; 84, integer:1; 85, elif_clause; 85, 86; 85, 91; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:result; 89, identifier:was_inconclusive; 90, argument_list; 91, block; 91, 92; 92, expression_statement; 92, 93; 93, augmented_assignment:+=; 93, 94; 93, 95; 94, identifier:errors; 95, integer:1; 96, else_clause; 96, 97; 97, block; 97, 98; 98, expression_statement; 98, 99; 99, augmented_assignment:+=; 99, 100; 99, 101; 100, identifier:fails; 101, integer:1; 102, with_statement; 102, 103; 102, 133; 103, with_clause; 103, 104; 104, with_item; 104, 105; 105, call; 105, 106; 105, 107; 106, identifier:tag; 107, argument_list; 107, 108; 107, 109; 107, 115; 107, 121; 107, 127; 108, string:'testsuite'; 109, keyword_argument; 109, 110; 109, 111; 110, identifier:tests; 111, call; 111, 112; 111, 113; 112, identifier:str; 113, argument_list; 113, 114; 114, identifier:count; 115, keyword_argument; 115, 116; 115, 117; 116, identifier:failures; 117, call; 117, 118; 117, 119; 118, identifier:str; 119, argument_list; 119, 120; 120, identifier:fails; 121, keyword_argument; 121, 122; 121, 123; 122, identifier:errors; 123, call; 123, 124; 123, 125; 124, identifier:str; 125, argument_list; 125, 126; 126, identifier:errors; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:skipped; 129, call; 129, 130; 129, 131; 130, identifier:str; 131, argument_list; 131, 132; 132, identifier:skips; 133, block; 133, 134; 134, for_statement; 134, 135; 134, 136; 134, 137; 135, identifier:result; 136, identifier:results; 137, block; 137, 138; 137, 154; 137, 162; 137, 170; 137, 181; 137, 189; 138, if_statement; 138, 139; 138, 152; 139, boolean_operator:and; 139, 140; 139, 147; 140, comparison_operator:is; 140, 141; 140, 146; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:result; 144, identifier:passed; 145, argument_list; 146, False; 147, comparison_operator:>; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:result; 150, identifier:retries_left; 151, integer:0; 152, block; 152, 153; 153, continue_statement; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:class_name; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:result; 160, identifier:get_tc_name; 161, argument_list; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:models; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:result; 168, identifier:get_dut_models; 169, argument_list; 170, if_statement; 170, 171; 170, 172; 171, identifier:models; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:class_name; 176, binary_operator:+; 176, 177; 176, 180; 177, binary_operator:+; 177, 178; 177, 179; 178, identifier:class_name; 179, string:"."; 180, identifier:models; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:name; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:result; 187, identifier:get_toolchain; 188, argument_list; 189, with_statement; 189, 190; 189, 212; 190, with_clause; 190, 191; 191, with_item; 191, 192; 192, call; 192, 193; 192, 194; 193, identifier:tag; 194, argument_list; 194, 195; 194, 196; 194, 199; 194, 202; 195, string:'testcase'; 196, keyword_argument; 196, 197; 196, 198; 197, identifier:classname; 198, identifier:class_name; 199, keyword_argument; 199, 200; 199, 201; 200, identifier:name; 201, identifier:name; 202, keyword_argument; 202, 203; 202, 204; 203, identifier:time; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:result; 207, identifier:get_duration; 208, argument_list; 208, 209; 209, keyword_argument; 209, 210; 209, 211; 210, identifier:seconds; 211, True; 212, block; 212, 213; 212, 233; 213, if_statement; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:result; 216, identifier:stdout; 217, block; 217, 218; 218, with_statement; 218, 219; 218, 225; 219, with_clause; 219, 220; 220, with_item; 220, 221; 221, call; 221, 222; 221, 223; 222, identifier:tag; 223, argument_list; 223, 224; 224, string:'system-out'; 225, block; 225, 226; 226, expression_statement; 226, 227; 227, call; 227, 228; 227, 229; 228, identifier:text; 229, argument_list; 229, 230; 230, attribute; 230, 231; 230, 232; 231, identifier:result; 232, identifier:stdout; 233, if_statement; 233, 234; 233, 239; 233, 241; 233, 263; 233, 293; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:result; 237, identifier:passed; 238, argument_list; 239, block; 239, 240; 240, continue_statement; 241, elif_clause; 241, 242; 241, 247; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:result; 245, identifier:skipped; 246, argument_list; 247, block; 247, 248; 248, with_statement; 248, 249; 248, 255; 249, with_clause; 249, 250; 250, with_item; 250, 251; 251, call; 251, 252; 251, 253; 252, identifier:tag; 253, argument_list; 253, 254; 254, string:'skipped'; 255, block; 255, 256; 256, expression_statement; 256, 257; 257, call; 257, 258; 257, 259; 258, identifier:text; 259, argument_list; 259, 260; 260, attribute; 260, 261; 260, 262; 261, identifier:result; 262, identifier:skip_reason; 263, elif_clause; 263, 264; 263, 269; 264, call; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:result; 267, identifier:was_inconclusive; 268, argument_list; 269, block; 269, 270; 270, with_statement; 270, 271; 270, 285; 271, with_clause; 271, 272; 272, with_item; 272, 273; 273, call; 273, 274; 273, 275; 274, identifier:tag; 275, argument_list; 275, 276; 275, 277; 276, string:'error'; 277, keyword_argument; 277, 278; 277, 279; 278, identifier:message; 279, call; 279, 280; 279, 281; 280, identifier:hex_escape_str; 281, argument_list; 281, 282; 282, attribute; 282, 283; 282, 284; 283, identifier:result; 284, identifier:fail_reason; 285, block; 285, 286; 286, expression_statement; 286, 287; 287, call; 287, 288; 287, 289; 288, identifier:text; 289, argument_list; 289, 290; 290, attribute; 290, 291; 290, 292; 291, identifier:result; 292, identifier:stderr; 293, else_clause; 293, 294; 294, block; 294, 295; 295, with_statement; 295, 296; 295, 310; 296, with_clause; 296, 297; 297, with_item; 297, 298; 298, call; 298, 299; 298, 300; 299, identifier:tag; 300, argument_list; 300, 301; 300, 302; 301, string:'failure'; 302, keyword_argument; 302, 303; 302, 304; 303, identifier:message; 304, call; 304, 305; 304, 306; 305, identifier:hex_escape_str; 306, argument_list; 306, 307; 307, attribute; 307, 308; 307, 309; 308, identifier:result; 309, identifier:fail_reason; 310, block; 310, 311; 311, expression_statement; 311, 312; 312, call; 312, 313; 312, 314; 313, identifier:text; 314, argument_list; 314, 315; 315, attribute; 315, 316; 315, 317; 316, identifier:result; 317, identifier:stderr; 318, return_statement; 318, 319; 319, call; 319, 320; 319, 321; 320, identifier:indent; 321, argument_list; 321, 322; 321, 327; 322, call; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:doc; 325, identifier:getvalue; 326, argument_list; 327, keyword_argument; 327, 328; 327, 329; 328, identifier:indentation; 329, binary_operator:*; 329, 330; 329, 331; 330, string:' '; 331, integer:4
def __generate(results): """ Static method which generates the Junit xml string from results :param results: Results as ResultList object. :return: Junit xml format string. """ doc, tag, text = Doc().tagtext() # Counters for testsuite tag info count = 0 fails = 0 errors = 0 skips = 0 for result in results: # Loop through all results and count the ones that were not later retried. if result.passed() is False: if result.retries_left > 0: # This will appear in the list again, move on continue count += 1 if result.passed(): # Passed, no need to increment anything else continue elif result.skipped(): skips += 1 elif result.was_inconclusive(): errors += 1 else: fails += 1 with tag('testsuite', tests=str(count), failures=str(fails), errors=str(errors), skipped=str(skips)): for result in results: if result.passed() is False and result.retries_left > 0: continue class_name = result.get_tc_name() models = result.get_dut_models() if models: class_name = class_name + "." + models name = result.get_toolchain() with tag('testcase', classname=class_name, name=name, time=result.get_duration(seconds=True)): if result.stdout: with tag('system-out'): text(result.stdout) if result.passed(): continue elif result.skipped(): with tag('skipped'): text(result.skip_reason) elif result.was_inconclusive(): with tag('error', message=hex_escape_str(result.fail_reason)): text(result.stderr) else: with tag('failure', message=hex_escape_str(result.fail_reason)): text(result.stderr) return indent( doc.getvalue(), indentation=' '*4 )
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 25; 2, function_name:init_base_logging; 3, parameters; 3, 4; 3, 7; 3, 10; 3, 13; 3, 16; 3, 19; 3, 22; 4, default_parameter; 4, 5; 4, 6; 5, identifier:directory; 6, string:"./log"; 7, default_parameter; 7, 8; 7, 9; 8, identifier:verbose; 9, integer:0; 10, default_parameter; 10, 11; 10, 12; 11, identifier:silent; 12, False; 13, default_parameter; 13, 14; 13, 15; 14, identifier:color; 15, False; 16, default_parameter; 16, 17; 16, 18; 17, identifier:no_file; 18, False; 19, default_parameter; 19, 20; 19, 21; 20, identifier:truncate; 21, True; 22, default_parameter; 22, 23; 22, 24; 23, identifier:config_location; 24, None; 25, block; 25, 26; 25, 28; 25, 30; 25, 32; 25, 34; 25, 36; 25, 38; 25, 40; 25, 89; 25, 117; 25, 118; 25, 127; 25, 133; 25, 142; 25, 150; 25, 178; 25, 317; 25, 321; 25, 325; 25, 381; 25, 431; 25, 438; 25, 442; 25, 454; 26, expression_statement; 26, 27; 27, comment; 28, global_statement; 28, 29; 29, identifier:LOGPATHDIR; 30, global_statement; 30, 31; 31, identifier:STANDALONE_LOGGING; 32, global_statement; 32, 33; 33, identifier:TRUNCATE_LOG; 34, global_statement; 34, 35; 35, identifier:COLOR_ON; 36, global_statement; 36, 37; 37, identifier:SILENT_ON; 38, global_statement; 38, 39; 39, identifier:VERBOSE_LEVEL; 40, if_statement; 40, 41; 40, 42; 41, identifier:config_location; 42, block; 42, 43; 43, try_statement; 43, 44; 43, 50; 43, 67; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 48; 47, identifier:_read_config; 48, argument_list; 48, 49; 49, identifier:config_location; 50, except_clause; 50, 51; 50, 55; 51, as_pattern; 51, 52; 51, 53; 52, identifier:IOError; 53, as_pattern_target; 53, 54; 54, identifier:error; 55, block; 55, 56; 56, raise_statement; 56, 57; 57, call; 57, 58; 57, 59; 58, identifier:IOError; 59, argument_list; 59, 60; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, string:"Unable to read from configuration file {}: {}"; 63, identifier:format; 64, argument_list; 64, 65; 64, 66; 65, identifier:config_location; 66, identifier:error; 67, except_clause; 67, 68; 67, 74; 68, as_pattern; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:jsonschema; 71, identifier:SchemaError; 72, as_pattern_target; 72, 73; 73, identifier:error; 74, block; 74, 75; 75, raise_statement; 75, 76; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:jsonschema; 79, identifier:SchemaError; 80, argument_list; 80, 81; 81, call; 81, 82; 81, 87; 82, attribute; 82, 83; 82, 86; 83, concatenated_string; 83, 84; 83, 85; 84, string:"Logging configuration schema "; 85, string:"file malformed: {}"; 86, identifier:format; 87, argument_list; 87, 88; 88, identifier:error; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:LOGPATHDIR; 92, call; 92, 93; 92, 98; 93, attribute; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:os; 96, identifier:path; 97, identifier:join; 98, argument_list; 98, 99; 98, 100; 99, identifier:directory; 100, call; 100, 101; 100, 115; 101, attribute; 101, 102; 101, 114; 102, call; 102, 103; 102, 112; 103, attribute; 103, 104; 103, 111; 104, call; 104, 105; 104, 110; 105, attribute; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:datetime; 108, identifier:datetime; 109, identifier:now; 110, argument_list; 111, identifier:strftime; 112, argument_list; 112, 113; 113, string:"%Y-%m-%d_%H%M%S.%f"; 114, identifier:rstrip; 115, argument_list; 115, 116; 116, string:"0"; 117, comment; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:icetealogger; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:logging; 124, identifier:getLogger; 125, argument_list; 125, 126; 126, string:"icetea"; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:icetealogger; 131, identifier:propagate; 132, False; 133, expression_statement; 133, 134; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:icetealogger; 137, identifier:setLevel; 138, argument_list; 138, 139; 139, attribute; 139, 140; 139, 141; 140, identifier:logging; 141, identifier:DEBUG; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:stream_handler; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:logging; 148, identifier:StreamHandler; 149, argument_list; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:formatter; 153, call; 153, 154; 153, 155; 154, identifier:BenchFormatter; 155, argument_list; 155, 156; 155, 167; 156, call; 156, 157; 156, 165; 157, attribute; 157, 158; 157, 164; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:LOGGING_CONFIG; 161, identifier:get; 162, argument_list; 162, 163; 163, string:"IceteaManager"; 164, identifier:get; 165, argument_list; 165, 166; 166, string:"format"; 167, call; 167, 168; 167, 176; 168, attribute; 168, 169; 168, 175; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:LOGGING_CONFIG; 172, identifier:get; 173, argument_list; 173, 174; 174, string:"IceteaManager"; 175, identifier:get; 176, argument_list; 176, 177; 177, string:"dateformat"; 178, if_statement; 178, 179; 178, 181; 178, 189; 178, 202; 179, not_operator; 179, 180; 180, identifier:color; 181, block; 181, 182; 182, expression_statement; 182, 183; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:stream_handler; 186, identifier:setFormatter; 187, argument_list; 187, 188; 188, identifier:formatter; 189, elif_clause; 189, 190; 189, 194; 190, boolean_operator:and; 190, 191; 190, 192; 191, identifier:color; 192, not_operator; 192, 193; 193, identifier:COLORS; 194, block; 194, 195; 195, raise_statement; 195, 196; 196, call; 196, 197; 196, 198; 197, identifier:ImportError; 198, argument_list; 198, 199; 199, concatenated_string; 199, 200; 199, 201; 200, string:"Missing coloredlogs module. Please install with "; 201, string:"pip to use colors in logging."; 202, else_clause; 202, 203; 203, block; 203, 204; 203, 280; 203, 284; 204, class_definition; 204, 205; 204, 206; 204, 210; 205, identifier:ColoredBenchFormatter; 206, argument_list; 206, 207; 207, attribute; 207, 208; 207, 209; 208, identifier:coloredlogs; 209, identifier:ColoredFormatter; 210, block; 210, 211; 210, 213; 210, 221; 211, expression_statement; 211, 212; 212, comment; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 216; 215, identifier:converter; 216, attribute; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:datetime; 219, identifier:datetime; 220, identifier:fromtimestamp; 221, function_definition; 221, 222; 221, 223; 221, 229; 222, function_name:formatTime; 223, parameters; 223, 224; 223, 225; 223, 226; 224, identifier:self; 225, identifier:record; 226, default_parameter; 226, 227; 226, 228; 227, identifier:datefmt; 228, None; 229, block; 229, 230; 229, 246; 229, 269; 229, 278; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:date_and_time; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:self; 236, identifier:converter; 237, argument_list; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:record; 240, identifier:created; 241, keyword_argument; 241, 242; 241, 243; 242, identifier:tz; 243, attribute; 243, 244; 243, 245; 244, identifier:pytz; 245, identifier:utc; 246, if_statement; 246, 247; 246, 250; 247, comparison_operator:in; 247, 248; 247, 249; 248, string:"%F"; 249, identifier:datefmt; 250, block; 250, 251; 250, 259; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 254; 253, identifier:msec; 254, binary_operator:%; 254, 255; 254, 256; 255, string:"%03d"; 256, attribute; 256, 257; 256, 258; 257, identifier:record; 258, identifier:msecs; 259, expression_statement; 259, 260; 260, assignment; 260, 261; 260, 262; 261, identifier:datefmt; 262, call; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:datefmt; 265, identifier:replace; 266, argument_list; 266, 267; 266, 268; 267, string:"%F"; 268, identifier:msec; 269, expression_statement; 269, 270; 270, assignment; 270, 271; 270, 272; 271, identifier:str_time; 272, call; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:date_and_time; 275, identifier:strftime; 276, argument_list; 276, 277; 277, identifier:datefmt; 278, return_statement; 278, 279; 279, identifier:str_time; 280, expression_statement; 280, 281; 281, assignment; 281, 282; 281, 283; 282, identifier:COLOR_ON; 283, identifier:color; 284, expression_statement; 284, 285; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:stream_handler; 288, identifier:setFormatter; 289, argument_list; 289, 290; 290, call; 290, 291; 290, 292; 291, identifier:ColoredBenchFormatter; 292, argument_list; 292, 293; 292, 304; 292, 315; 292, 316; 293, call; 293, 294; 293, 302; 294, attribute; 294, 295; 294, 301; 295, call; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:LOGGING_CONFIG; 298, identifier:get; 299, argument_list; 299, 300; 300, string:"IceteaManager"; 301, identifier:get; 302, argument_list; 302, 303; 303, string:"format"; 304, call; 304, 305; 304, 313; 305, attribute; 305, 306; 305, 312; 306, call; 306, 307; 306, 310; 307, attribute; 307, 308; 307, 309; 308, identifier:LOGGING_CONFIG; 309, identifier:get; 310, argument_list; 310, 311; 311, string:"IceteaManager"; 312, identifier:get; 313, argument_list; 313, 314; 314, string:"dateformat"; 315, identifier:LEVEL_FORMATS; 316, identifier:FIELD_STYLES; 317, expression_statement; 317, 318; 318, assignment; 318, 319; 318, 320; 319, identifier:SILENT_ON; 320, identifier:silent; 321, expression_statement; 321, 322; 322, assignment; 322, 323; 322, 324; 323, identifier:VERBOSE_LEVEL; 324, identifier:verbose; 325, if_statement; 325, 326; 325, 328; 326, not_operator; 326, 327; 327, identifier:no_file; 328, block; 328, 329; 328, 348; 328, 368; 329, try_statement; 329, 330; 329, 338; 330, block; 330, 331; 331, expression_statement; 331, 332; 332, call; 332, 333; 332, 336; 333, attribute; 333, 334; 333, 335; 334, identifier:os; 335, identifier:makedirs; 336, argument_list; 336, 337; 337, identifier:LOGPATHDIR; 338, except_clause; 338, 339; 338, 340; 339, identifier:OSError; 340, block; 340, 341; 341, raise_statement; 341, 342; 342, call; 342, 343; 342, 344; 343, identifier:OSError; 344, argument_list; 344, 345; 345, binary_operator:%; 345, 346; 345, 347; 346, string:"Log path %s already exists."; 347, identifier:LOGPATHDIR; 348, expression_statement; 348, 349; 349, assignment; 349, 350; 349, 351; 350, identifier:filename; 351, call; 351, 352; 351, 365; 352, attribute; 352, 353; 352, 364; 353, call; 353, 354; 353, 362; 354, attribute; 354, 355; 354, 361; 355, call; 355, 356; 355, 359; 356, attribute; 356, 357; 356, 358; 357, identifier:LOGGING_CONFIG; 358, identifier:get; 359, argument_list; 359, 360; 360, string:"IceteaManager"; 361, identifier:get; 362, argument_list; 362, 363; 363, string:"file"; 364, identifier:get; 365, argument_list; 365, 366; 365, 367; 366, string:"name"; 367, string:"icetea.log"; 368, expression_statement; 368, 369; 369, assignment; 369, 370; 369, 371; 370, identifier:icetealogger; 371, call; 371, 372; 371, 373; 372, identifier:_add_filehandler; 373, argument_list; 373, 374; 373, 375; 373, 379; 373, 380; 374, identifier:icetealogger; 375, call; 375, 376; 375, 377; 376, identifier:get_base_logfilename; 377, argument_list; 377, 378; 378, identifier:filename; 379, identifier:formatter; 380, string:"IceteaManager"; 381, if_statement; 381, 382; 381, 386; 381, 396; 381, 408; 382, boolean_operator:and; 382, 383; 382, 384; 383, identifier:verbose; 384, not_operator; 384, 385; 385, identifier:silent; 386, block; 386, 387; 387, expression_statement; 387, 388; 388, call; 388, 389; 388, 392; 389, attribute; 389, 390; 389, 391; 390, identifier:stream_handler; 391, identifier:setLevel; 392, argument_list; 392, 393; 393, attribute; 393, 394; 393, 395; 394, identifier:logging; 395, identifier:DEBUG; 396, elif_clause; 396, 397; 396, 398; 397, identifier:silent; 398, block; 398, 399; 399, expression_statement; 399, 400; 400, call; 400, 401; 400, 404; 401, attribute; 401, 402; 401, 403; 402, identifier:stream_handler; 403, identifier:setLevel; 404, argument_list; 404, 405; 405, attribute; 405, 406; 405, 407; 406, identifier:logging; 407, identifier:WARN; 408, else_clause; 408, 409; 409, block; 409, 410; 410, expression_statement; 410, 411; 411, call; 411, 412; 411, 415; 412, attribute; 412, 413; 412, 414; 413, identifier:stream_handler; 414, identifier:setLevel; 415, argument_list; 415, 416; 416, call; 416, 417; 416, 418; 417, identifier:getattr; 418, argument_list; 418, 419; 418, 420; 419, identifier:logging; 420, call; 420, 421; 420, 429; 421, attribute; 421, 422; 421, 428; 422, call; 422, 423; 422, 426; 423, attribute; 423, 424; 423, 425; 424, identifier:LOGGING_CONFIG; 425, identifier:get; 426, argument_list; 426, 427; 427, string:"IceteaManager"; 428, identifier:get; 429, argument_list; 429, 430; 430, string:"level"; 431, expression_statement; 431, 432; 432, call; 432, 433; 432, 436; 433, attribute; 433, 434; 433, 435; 434, identifier:icetealogger; 435, identifier:addHandler; 436, argument_list; 436, 437; 437, identifier:stream_handler; 438, expression_statement; 438, 439; 439, assignment; 439, 440; 439, 441; 440, identifier:TRUNCATE_LOG; 441, identifier:truncate; 442, if_statement; 442, 443; 442, 444; 443, identifier:TRUNCATE_LOG; 444, block; 444, 445; 445, expression_statement; 445, 446; 446, call; 446, 447; 446, 450; 447, attribute; 447, 448; 447, 449; 448, identifier:icetealogger; 449, identifier:addFilter; 450, argument_list; 450, 451; 451, call; 451, 452; 451, 453; 452, identifier:ContextFilter; 453, argument_list; 454, expression_statement; 454, 455; 455, assignment; 455, 456; 455, 457; 456, identifier:STANDALONE_LOGGING; 457, False
def init_base_logging(directory="./log", verbose=0, silent=False, color=False, no_file=False, truncate=True, config_location=None): """ Initialize the Icetea logging by creating a directory to store logs for this run and initialize the console logger for Icetea itself. :param directory: Directory where to store the resulting logs :param verbose: Log level as integer :param silent: Log level warning :param no_file: Log to file :param color: Log coloring :param truncate: Log truncating :param config_location: Location of config file. :raises IOError if unable to read configuration file. :raises OSError if log path already exists. :raises ImportError if colored logging was requested but coloredlogs module is not installed. """ global LOGPATHDIR global STANDALONE_LOGGING global TRUNCATE_LOG global COLOR_ON global SILENT_ON global VERBOSE_LEVEL if config_location: try: _read_config(config_location) except IOError as error: raise IOError("Unable to read from configuration file {}: {}".format(config_location, error)) except jsonschema.SchemaError as error: raise jsonschema.SchemaError("Logging configuration schema " "file malformed: {}".format(error)) LOGPATHDIR = os.path.join(directory, datetime.datetime.now().strftime( "%Y-%m-%d_%H%M%S.%f").rstrip("0")) # Initialize the simple console logger for IceteaManager icetealogger = logging.getLogger("icetea") icetealogger.propagate = False icetealogger.setLevel(logging.DEBUG) stream_handler = logging.StreamHandler() formatter = BenchFormatter(LOGGING_CONFIG.get("IceteaManager").get("format"), LOGGING_CONFIG.get("IceteaManager").get("dateformat")) if not color: stream_handler.setFormatter(formatter) elif color and not COLORS: raise ImportError("Missing coloredlogs module. Please install with " "pip to use colors in logging.") else: class ColoredBenchFormatter(coloredlogs.ColoredFormatter): """ This is defined as an internal class here because coloredlogs is and optional dependency. """ converter = datetime.datetime.fromtimestamp def formatTime(self, record, datefmt=None): date_and_time = self.converter(record.created, tz=pytz.utc) if "%F" in datefmt: msec = "%03d" % record.msecs datefmt = datefmt.replace("%F", msec) str_time = date_and_time.strftime(datefmt) return str_time COLOR_ON = color stream_handler.setFormatter(ColoredBenchFormatter( LOGGING_CONFIG.get("IceteaManager").get("format"), LOGGING_CONFIG.get("IceteaManager").get("dateformat"), LEVEL_FORMATS, FIELD_STYLES)) SILENT_ON = silent VERBOSE_LEVEL = verbose if not no_file: try: os.makedirs(LOGPATHDIR) except OSError: raise OSError("Log path %s already exists." % LOGPATHDIR) filename = LOGGING_CONFIG.get("IceteaManager").get("file").get("name", "icetea.log") icetealogger = _add_filehandler(icetealogger, get_base_logfilename(filename), formatter, "IceteaManager") if verbose and not silent: stream_handler.setLevel(logging.DEBUG) elif silent: stream_handler.setLevel(logging.WARN) else: stream_handler.setLevel(getattr(logging, LOGGING_CONFIG.get("IceteaManager").get("level"))) icetealogger.addHandler(stream_handler) TRUNCATE_LOG = truncate if TRUNCATE_LOG: icetealogger.addFilter(ContextFilter()) STANDALONE_LOGGING = False
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:assertJsonContains; 3, parameters; 3, 4; 3, 7; 3, 10; 4, default_parameter; 4, 5; 4, 6; 5, identifier:jsonStr; 6, None; 7, default_parameter; 7, 8; 7, 9; 8, identifier:key; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:message; 12, None; 13, block; 13, 14; 13, 16; 14, expression_statement; 14, 15; 15, comment; 16, if_statement; 16, 17; 16, 20; 16, 89; 17, comparison_operator:is; 17, 18; 17, 19; 18, identifier:jsonStr; 19, None; 20, block; 20, 21; 21, try_statement; 21, 22; 21, 63; 22, block; 22, 23; 22, 32; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:data; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:json; 29, identifier:loads; 30, argument_list; 30, 31; 31, identifier:jsonStr; 32, if_statement; 32, 33; 32, 36; 33, comparison_operator:not; 33, 34; 33, 35; 34, identifier:key; 35, identifier:data; 36, block; 36, 37; 37, raise_statement; 37, 38; 38, call; 38, 39; 38, 40; 39, identifier:TestStepFail; 40, argument_list; 40, 41; 41, conditional_expression:if; 41, 42; 41, 46; 41, 49; 42, call; 42, 43; 42, 44; 43, identifier:format_message; 44, argument_list; 44, 45; 45, identifier:message; 46, comparison_operator:is; 46, 47; 46, 48; 47, identifier:message; 48, None; 49, binary_operator:%; 49, 50; 49, 54; 50, concatenated_string; 50, 51; 50, 52; 50, 53; 51, string:"Assert: "; 52, string:"Key : %s is not "; 53, string:"in : %s"; 54, tuple; 54, 55; 54, 59; 55, call; 55, 56; 55, 57; 56, identifier:str; 57, argument_list; 57, 58; 58, identifier:key; 59, call; 59, 60; 59, 61; 60, identifier:str; 61, argument_list; 61, 62; 62, identifier:jsonStr; 63, except_clause; 63, 64; 63, 70; 64, as_pattern; 64, 65; 64, 68; 65, tuple; 65, 66; 65, 67; 66, identifier:TypeError; 67, identifier:ValueError; 68, as_pattern_target; 68, 69; 69, identifier:e; 70, block; 70, 71; 71, raise_statement; 71, 72; 72, call; 72, 73; 72, 74; 73, identifier:TestStepFail; 74, argument_list; 74, 75; 75, conditional_expression:if; 75, 76; 75, 80; 75, 83; 76, call; 76, 77; 76, 78; 77, identifier:format_message; 78, argument_list; 78, 79; 79, identifier:message; 80, comparison_operator:is; 80, 81; 80, 82; 81, identifier:message; 82, None; 83, binary_operator:+; 83, 84; 83, 85; 84, string:"Unable to parse json "; 85, call; 85, 86; 85, 87; 86, identifier:str; 87, argument_list; 87, 88; 88, identifier:e; 89, else_clause; 89, 90; 90, block; 90, 91; 91, raise_statement; 91, 92; 92, call; 92, 93; 92, 94; 93, identifier:TestStepFail; 94, argument_list; 94, 95; 95, conditional_expression:if; 95, 96; 95, 100; 95, 103; 96, call; 96, 97; 96, 98; 97, identifier:format_message; 98, argument_list; 98, 99; 99, identifier:message; 100, comparison_operator:is; 100, 101; 100, 102; 101, identifier:message; 102, None; 103, string:"Json string is empty"
def assertJsonContains(jsonStr=None, key=None, message=None): """ Assert that jsonStr contains key. :param jsonStr: Json as string :param key: Key to look for :param message: Failure message :raises: TestStepFail if key is not in jsonStr or if loading jsonStr to a dictionary fails or if jsonStr is None. """ if jsonStr is not None: try: data = json.loads(jsonStr) if key not in data: raise TestStepFail( format_message(message) if message is not None else "Assert: " "Key : %s is not " "in : %s" % (str(key), str(jsonStr))) except (TypeError, ValueError) as e: raise TestStepFail( format_message(message) if message is not None else "Unable to parse json "+str(e)) else: raise TestStepFail( format_message(message) if message is not None else "Json string is empty")
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_read_fd; 3, parameters; 3, 4; 4, identifier:file_descr; 5, block; 5, 6; 5, 8; 5, 60; 5, 179; 6, expression_statement; 6, 7; 7, comment; 8, try_statement; 8, 9; 8, 22; 9, block; 9, 10; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:line; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:os; 16, identifier:read; 17, argument_list; 17, 18; 17, 19; 18, identifier:file_descr; 19, binary_operator:*; 19, 20; 19, 21; 20, integer:1024; 21, integer:1024; 22, except_clause; 22, 23; 22, 24; 23, identifier:OSError; 24, block; 24, 25; 24, 34; 24, 58; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:stream_desc; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:NonBlockingStreamReader; 31, identifier:_get_sd; 32, argument_list; 32, 33; 33, identifier:file_descr; 34, if_statement; 34, 35; 34, 38; 35, comparison_operator:is; 35, 36; 35, 37; 36, identifier:stream_desc; 37, None; 38, block; 38, 39; 38, 45; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:stream_desc; 43, identifier:has_error; 44, True; 45, if_statement; 45, 46; 45, 51; 46, comparison_operator:is; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:stream_desc; 49, identifier:callback; 50, None; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:stream_desc; 56, identifier:callback; 57, argument_list; 58, return_statement; 58, 59; 59, integer:0; 60, if_statement; 60, 61; 60, 62; 61, identifier:line; 62, block; 62, 63; 62, 72; 62, 80; 62, 105; 62, 111; 62, 112; 62, 125; 62, 163; 62, 164; 62, 165; 62, 174; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:stream_desc; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:NonBlockingStreamReader; 69, identifier:_get_sd; 70, argument_list; 70, 71; 71, identifier:file_descr; 72, if_statement; 72, 73; 72, 76; 73, comparison_operator:is; 73, 74; 73, 75; 74, identifier:stream_desc; 75, None; 76, block; 76, 77; 76, 79; 77, return_statement; 77, 78; 78, integer:0; 79, comment; 80, if_statement; 80, 81; 80, 82; 81, identifier:IS_PYTHON3; 82, block; 82, 83; 83, try_statement; 83, 84; 83, 85; 83, 95; 84, comment; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:line; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:line; 92, identifier:decode; 93, argument_list; 93, 94; 94, string:"ascii"; 95, except_clause; 95, 96; 95, 97; 96, identifier:UnicodeDecodeError; 97, block; 97, 98; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:line; 101, call; 101, 102; 101, 103; 102, identifier:repr; 103, argument_list; 103, 104; 104, identifier:line; 105, expression_statement; 105, 106; 106, augmented_assignment:+=; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:stream_desc; 109, identifier:buf; 110, identifier:line; 111, comment; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:split; 115, call; 115, 116; 115, 121; 116, attribute; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:stream_desc; 119, identifier:buf; 120, identifier:split; 121, argument_list; 121, 122; 122, attribute; 122, 123; 122, 124; 123, identifier:os; 124, identifier:linesep; 125, for_statement; 125, 126; 125, 127; 125, 133; 126, identifier:line; 127, subscript; 127, 128; 127, 129; 128, identifier:split; 129, slice; 129, 130; 129, 131; 130, colon; 131, unary_operator:-; 131, 132; 132, integer:1; 133, block; 133, 134; 133, 150; 134, expression_statement; 134, 135; 135, call; 135, 136; 135, 141; 136, attribute; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:stream_desc; 139, identifier:read_queue; 140, identifier:appendleft; 141, argument_list; 141, 142; 142, call; 142, 143; 142, 144; 143, identifier:strip_escape; 144, argument_list; 144, 145; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:line; 148, identifier:strip; 149, argument_list; 150, if_statement; 150, 151; 150, 156; 151, comparison_operator:is; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:stream_desc; 154, identifier:callback; 155, None; 156, block; 156, 157; 157, expression_statement; 157, 158; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:stream_desc; 161, identifier:callback; 162, argument_list; 163, comment; 164, comment; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:stream_desc; 169, identifier:buf; 170, subscript; 170, 171; 170, 172; 171, identifier:split; 172, unary_operator:-; 172, 173; 173, integer:1; 174, return_statement; 174, 175; 175, call; 175, 176; 175, 177; 176, identifier:len; 177, argument_list; 177, 178; 178, identifier:line; 179, return_statement; 179, 180; 180, integer:0
def _read_fd(file_descr): """ Read incoming data from file handle. Then find the matching StreamDescriptor by file_descr value. :param file_descr: file object :return: Return number of bytes read """ try: line = os.read(file_descr, 1024 * 1024) except OSError: stream_desc = NonBlockingStreamReader._get_sd(file_descr) if stream_desc is not None: stream_desc.has_error = True if stream_desc.callback is not None: stream_desc.callback() return 0 if line: stream_desc = NonBlockingStreamReader._get_sd(file_descr) if stream_desc is None: return 0 # Process closing if IS_PYTHON3: try: # @TODO: further develop for not ascii/unicode binary content line = line.decode("ascii") except UnicodeDecodeError: line = repr(line) stream_desc.buf += line # Break lines split = stream_desc.buf.split(os.linesep) for line in split[:-1]: stream_desc.read_queue.appendleft(strip_escape(line.strip())) if stream_desc.callback is not None: stream_desc.callback() # Store the remainded, its either '' if last char was '\n' # or remaining buffer before line end stream_desc.buf = split[-1] return len(line) return 0
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:run; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:args; 7, None; 8, block; 8, 9; 8, 11; 8, 12; 8, 13; 8, 19; 8, 30; 8, 40; 8, 64; 8, 65; 8, 71; 8, 104; 8, 121; 8, 122; 8, 135; 8, 136; 8, 186; 8, 235; 8, 270; 8, 281; 8, 333; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:retcodesummary; 16, attribute; 16, 17; 16, 18; 17, identifier:ExitCodes; 18, identifier:EXIT_SUCCESS; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:args; 24, conditional_expression:if; 24, 25; 24, 26; 24, 27; 25, identifier:args; 26, identifier:args; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:args; 30, if_statement; 30, 31; 30, 37; 31, not_operator; 31, 32; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:self; 35, identifier:check_args; 36, argument_list; 37, block; 37, 38; 38, return_statement; 38, 39; 39, identifier:retcodesummary; 40, if_statement; 40, 41; 40, 46; 41, attribute; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:self; 44, identifier:args; 45, identifier:clean; 46, block; 46, 47; 47, if_statement; 47, 48; 47, 61; 48, boolean_operator:and; 48, 49; 48, 55; 49, not_operator; 49, 50; 50, attribute; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:args; 54, identifier:tc; 55, not_operator; 55, 56; 56, attribute; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:args; 60, identifier:suite; 61, block; 61, 62; 62, return_statement; 62, 63; 63, identifier:retcodesummary; 64, comment; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:version; 68, call; 68, 69; 68, 70; 69, identifier:get_fw_version; 70, argument_list; 71, if_statement; 71, 72; 71, 79; 71, 87; 72, boolean_operator:and; 72, 73; 72, 78; 73, attribute; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:self; 76, identifier:args; 77, identifier:version; 78, identifier:version; 79, block; 79, 80; 79, 85; 80, expression_statement; 80, 81; 81, call; 81, 82; 81, 83; 82, identifier:print; 83, argument_list; 83, 84; 84, identifier:version; 85, return_statement; 85, 86; 86, identifier:retcodesummary; 87, elif_clause; 87, 88; 87, 96; 88, boolean_operator:and; 88, 89; 88, 94; 89, attribute; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:self; 92, identifier:args; 93, identifier:version; 94, not_operator; 94, 95; 95, identifier:version; 96, block; 96, 97; 96, 102; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 100; 99, identifier:print; 100, argument_list; 100, 101; 101, string:"Unable to get version. Have you installed Icetea correctly?"; 102, return_statement; 102, 103; 103, identifier:retcodesummary; 104, expression_statement; 104, 105; 105, call; 105, 106; 105, 111; 106, attribute; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:self; 109, identifier:logger; 110, identifier:info; 111, argument_list; 111, 112; 112, conditional_expression:if; 112, 113; 112, 119; 112, 120; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, string:"Using Icetea version {}"; 116, identifier:format; 117, argument_list; 117, 118; 118, identifier:version; 119, identifier:version; 120, string:"Unable to get Icetea version. Is Icetea installed?"; 121, comment; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:cloud; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:self; 128, identifier:_init_cloud; 129, argument_list; 129, 130; 130, attribute; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:args; 134, identifier:cloud; 135, comment; 136, if_statement; 136, 137; 136, 142; 137, attribute; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:self; 140, identifier:args; 141, identifier:listsuites; 142, block; 142, 143; 142, 157; 142, 184; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:table; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:self; 149, identifier:list_suites; 150, argument_list; 150, 151; 150, 156; 151, attribute; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:self; 154, identifier:args; 155, identifier:suitedir; 156, identifier:cloud; 157, if_statement; 157, 158; 157, 161; 157, 177; 158, comparison_operator:is; 158, 159; 158, 160; 159, identifier:table; 160, None; 161, block; 161, 162; 161, 171; 162, expression_statement; 162, 163; 163, call; 163, 164; 163, 169; 164, attribute; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:self; 167, identifier:logger; 168, identifier:error; 169, argument_list; 169, 170; 170, string:"No suites found!"; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:retcodesummary; 174, attribute; 174, 175; 174, 176; 175, identifier:ExitCodes; 176, identifier:EXIT_FAIL; 177, else_clause; 177, 178; 178, block; 178, 179; 179, expression_statement; 179, 180; 180, call; 180, 181; 180, 182; 181, identifier:print; 182, argument_list; 182, 183; 183, identifier:table; 184, return_statement; 184, 185; 185, identifier:retcodesummary; 186, try_statement; 186, 187; 186, 207; 187, block; 187, 188; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:testsuite; 191, call; 191, 192; 191, 193; 192, identifier:TestSuite; 193, argument_list; 193, 194; 193, 199; 193, 202; 194, keyword_argument; 194, 195; 194, 196; 195, identifier:logger; 196, attribute; 196, 197; 196, 198; 197, identifier:self; 198, identifier:logger; 199, keyword_argument; 199, 200; 199, 201; 200, identifier:cloud_module; 201, identifier:cloud; 202, keyword_argument; 202, 203; 202, 204; 203, identifier:args; 204, attribute; 204, 205; 204, 206; 205, identifier:self; 206, identifier:args; 207, except_clause; 207, 208; 207, 212; 208, as_pattern; 208, 209; 208, 210; 209, identifier:SuiteException; 210, as_pattern_target; 210, 211; 211, identifier:error; 212, block; 212, 213; 212, 227; 212, 233; 213, expression_statement; 213, 214; 214, call; 214, 215; 214, 220; 215, attribute; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:self; 218, identifier:logger; 219, identifier:error; 220, argument_list; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, string:"Something went wrong in suite creation! {}"; 224, identifier:format; 225, argument_list; 225, 226; 226, identifier:error; 227, expression_statement; 227, 228; 228, assignment; 228, 229; 228, 230; 229, identifier:retcodesummary; 230, attribute; 230, 231; 230, 232; 231, identifier:ExitCodes; 232, identifier:EXIT_INCONC; 233, return_statement; 233, 234; 234, identifier:retcodesummary; 235, if_statement; 235, 236; 235, 241; 236, attribute; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:self; 239, identifier:args; 240, identifier:list; 241, block; 241, 242; 241, 255; 241, 263; 241, 268; 242, if_statement; 242, 243; 242, 248; 243, attribute; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:self; 246, identifier:args; 247, identifier:cloud; 248, block; 248, 249; 249, expression_statement; 249, 250; 250, call; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:testsuite; 253, identifier:update_testcases; 254, argument_list; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 258; 257, identifier:testcases; 258, call; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:testsuite; 261, identifier:list_testcases; 262, argument_list; 263, expression_statement; 263, 264; 264, call; 264, 265; 264, 266; 265, identifier:print; 266, argument_list; 266, 267; 267, identifier:testcases; 268, return_statement; 268, 269; 269, identifier:retcodesummary; 270, expression_statement; 270, 271; 271, assignment; 271, 272; 271, 273; 272, identifier:results; 273, call; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:self; 276, identifier:runtestsuite; 277, argument_list; 277, 278; 278, keyword_argument; 278, 279; 278, 280; 279, identifier:testsuite; 280, identifier:testsuite; 281, if_statement; 281, 282; 281, 284; 281, 291; 281, 312; 282, not_operator; 282, 283; 283, identifier:results; 284, block; 284, 285; 285, expression_statement; 285, 286; 286, assignment; 286, 287; 286, 288; 287, identifier:retcodesummary; 288, attribute; 288, 289; 288, 290; 289, identifier:ExitCodes; 290, identifier:EXIT_SUCCESS; 291, elif_clause; 291, 292; 291, 305; 292, boolean_operator:and; 292, 293; 292, 298; 293, call; 293, 294; 293, 297; 294, attribute; 294, 295; 294, 296; 295, identifier:results; 296, identifier:failure_count; 297, argument_list; 298, comparison_operator:is; 298, 299; 298, 304; 299, attribute; 299, 300; 299, 303; 300, attribute; 300, 301; 300, 302; 301, identifier:self; 302, identifier:args; 303, identifier:failure_return_value; 304, True; 305, block; 305, 306; 306, expression_statement; 306, 307; 307, assignment; 307, 308; 307, 309; 308, identifier:retcodesummary; 309, attribute; 309, 310; 309, 311; 310, identifier:ExitCodes; 311, identifier:EXIT_FAIL; 312, elif_clause; 312, 313; 312, 326; 313, boolean_operator:and; 313, 314; 313, 319; 314, call; 314, 315; 314, 318; 315, attribute; 315, 316; 315, 317; 316, identifier:results; 317, identifier:inconclusive_count; 318, argument_list; 319, comparison_operator:is; 319, 320; 319, 325; 320, attribute; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:self; 323, identifier:args; 324, identifier:failure_return_value; 325, True; 326, block; 326, 327; 327, expression_statement; 327, 328; 328, assignment; 328, 329; 328, 330; 329, identifier:retcodesummary; 330, attribute; 330, 331; 330, 332; 331, identifier:ExitCodes; 332, identifier:EXIT_INCONC; 333, return_statement; 333, 334; 334, identifier:retcodesummary
def run(self, args=None): """ Runs the set of tests within the given path. """ # Disable "Too many branches" and "Too many return statemets" warnings # pylint: disable=R0912,R0911 retcodesummary = ExitCodes.EXIT_SUCCESS self.args = args if args else self.args if not self.check_args(): return retcodesummary if self.args.clean: if not self.args.tc and not self.args.suite: return retcodesummary # If called with --version print version and exit version = get_fw_version() if self.args.version and version: print(version) return retcodesummary elif self.args.version and not version: print("Unable to get version. Have you installed Icetea correctly?") return retcodesummary self.logger.info("Using Icetea version {}".format(version) if version else "Unable to get Icetea version. Is Icetea installed?") # If cloud set, import cloud, get parameters from environment, initialize cloud cloud = self._init_cloud(self.args.cloud) # Check if called with listsuites. If so, print out suites either from cloud or from local if self.args.listsuites: table = self.list_suites(self.args.suitedir, cloud) if table is None: self.logger.error("No suites found!") retcodesummary = ExitCodes.EXIT_FAIL else: print(table) return retcodesummary try: testsuite = TestSuite(logger=self.logger, cloud_module=cloud, args=self.args) except SuiteException as error: self.logger.error("Something went wrong in suite creation! {}".format(error)) retcodesummary = ExitCodes.EXIT_INCONC return retcodesummary if self.args.list: if self.args.cloud: testsuite.update_testcases() testcases = testsuite.list_testcases() print(testcases) return retcodesummary results = self.runtestsuite(testsuite=testsuite) if not results: retcodesummary = ExitCodes.EXIT_SUCCESS elif results.failure_count() and self.args.failure_return_value is True: retcodesummary = ExitCodes.EXIT_FAIL elif results.inconclusive_count() and self.args.failure_return_value is True: retcodesummary = ExitCodes.EXIT_INCONC return retcodesummary
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:generate_object_graphs_by_class; 3, parameters; 3, 4; 4, identifier:classlist; 5, block; 5, 6; 5, 8; 5, 20; 5, 24; 5, 37; 6, expression_statement; 6, 7; 7, comment; 8, try_statement; 8, 9; 8, 16; 9, block; 9, 10; 9, 13; 10, import_statement; 10, 11; 11, dotted_name; 11, 12; 12, identifier:objgraph; 13, import_statement; 13, 14; 14, dotted_name; 14, 15; 15, identifier:gc; 16, except_clause; 16, 17; 16, 18; 17, identifier:ImportError; 18, block; 18, 19; 19, return_statement; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:graphcount; 23, integer:0; 24, if_statement; 24, 25; 24, 31; 25, not_operator; 25, 26; 26, call; 26, 27; 26, 28; 27, identifier:isinstance; 28, argument_list; 28, 29; 28, 30; 29, identifier:classlist; 30, identifier:list; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:classlist; 35, list:[classlist]; 35, 36; 36, identifier:classlist; 37, for_statement; 37, 38; 37, 39; 37, 40; 38, identifier:class_item; 39, identifier:classlist; 40, block; 40, 41; 41, for_statement; 41, 42; 41, 43; 41, 48; 42, identifier:obj; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:gc; 46, identifier:get_objects; 47, argument_list; 48, block; 48, 49; 49, if_statement; 49, 50; 49, 55; 50, call; 50, 51; 50, 52; 51, identifier:isinstance; 52, argument_list; 52, 53; 52, 54; 53, identifier:obj; 54, identifier:class_item; 55, block; 55, 56; 55, 60; 55, 80; 56, expression_statement; 56, 57; 57, augmented_assignment:+=; 57, 58; 57, 59; 58, identifier:graphcount; 59, integer:1; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:objgraph; 64, identifier:show_refs; 65, argument_list; 65, 66; 65, 68; 66, list:[obj]; 66, 67; 67, identifier:obj; 68, keyword_argument; 68, 69; 68, 70; 69, identifier:filename; 70, binary_operator:%; 70, 71; 70, 72; 71, string:'%d_%s_%d_refs.png'; 72, tuple; 72, 73; 72, 74; 72, 79; 73, identifier:ogcounter; 74, attribute; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:obj; 77, identifier:__class__; 78, identifier:__name__; 79, identifier:graphcount; 80, expression_statement; 80, 81; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:objgraph; 84, identifier:show_backrefs; 85, argument_list; 85, 86; 85, 88; 86, list:[obj]; 86, 87; 87, identifier:obj; 88, keyword_argument; 88, 89; 88, 90; 89, identifier:filename; 90, binary_operator:%; 90, 91; 90, 92; 91, string:'%d_%s_%d_backrefs.png'; 92, tuple; 92, 93; 92, 94; 92, 99; 93, identifier:ogcounter; 94, attribute; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:obj; 97, identifier:__class__; 98, identifier:__name__; 99, identifier:graphcount
def generate_object_graphs_by_class(classlist): """ Generate reference and backreference graphs for objects of type class for each class given in classlist. Useful for debugging reference leaks in framework etc. Usage example to generate graphs for class "someclass": >>> import someclass >>> someclassobject = someclass() >>> generate_object_graphs_by_class(someclass) Needs "objgraph" module installed. """ try: import objgraph import gc except ImportError: return graphcount = 0 if not isinstance(classlist, list): classlist = [classlist] for class_item in classlist: for obj in gc.get_objects(): if isinstance(obj, class_item): graphcount += 1 objgraph.show_refs([obj], filename='%d_%s_%d_refs.png' % ( ogcounter, obj.__class__.__name__, graphcount)) objgraph.show_backrefs([obj], filename='%d_%s_%d_backrefs.png' % ( ogcounter, obj.__class__.__name__, graphcount))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_wait_for_exec_ready; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 115; 5, 169; 5, 170; 5, 194; 5, 200; 6, expression_statement; 6, 7; 7, comment; 8, while_statement; 8, 9; 8, 24; 9, boolean_operator:and; 9, 10; 9, 19; 10, not_operator; 10, 11; 11, call; 11, 12; 11, 17; 12, attribute; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:response_received; 16, identifier:wait; 17, argument_list; 17, 18; 18, integer:1; 19, comparison_operator:!=; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:query_timeout; 23, integer:0; 24, block; 24, 25; 24, 89; 24, 109; 25, if_statement; 25, 26; 25, 41; 26, boolean_operator:and; 26, 27; 26, 32; 27, comparison_operator:!=; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:query_timeout; 31, integer:0; 32, comparison_operator:<; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:self; 35, identifier:query_timeout; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:get_time; 40, argument_list; 41, block; 41, 42; 41, 61; 41, 72; 41, 78; 42, if_statement; 42, 43; 42, 46; 42, 55; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:prev; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:cmd; 50, attribute; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:prev; 54, identifier:cmd; 55, else_clause; 55, 56; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:cmd; 60, string:"???"; 61, expression_statement; 61, 62; 62, call; 62, 63; 62, 68; 63, attribute; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:self; 66, identifier:logger; 67, identifier:error; 68, argument_list; 68, 69; 69, binary_operator:+; 69, 70; 69, 71; 70, string:"CMD timeout: "; 71, identifier:cmd; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:self; 76, identifier:query_timeout; 77, integer:0; 78, raise_statement; 78, 79; 79, call; 79, 80; 79, 81; 80, identifier:TestStepTimeout; 81, argument_list; 81, 82; 82, binary_operator:+; 82, 83; 82, 88; 83, binary_operator:+; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:self; 86, identifier:name; 87, string:" CMD timeout: "; 88, identifier:cmd; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 96; 91, attribute; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:self; 94, identifier:logger; 95, identifier:debug; 96, argument_list; 96, 97; 96, 100; 97, concatenated_string; 97, 98; 97, 99; 98, string:"Waiting for response... "; 99, string:"timeout=%d"; 100, binary_operator:-; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:self; 103, identifier:query_timeout; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:self; 107, identifier:get_time; 108, argument_list; 109, expression_statement; 109, 110; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:self; 113, identifier:_dut_is_alive; 114, argument_list; 115, if_statement; 115, 116; 115, 122; 116, comparison_operator:==; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:response_coming_in; 120, unary_operator:-; 120, 121; 121, integer:1; 122, block; 122, 123; 122, 148; 122, 149; 122, 158; 123, if_statement; 123, 124; 123, 129; 123, 130; 124, comparison_operator:is; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:self; 127, identifier:query_async_response; 128, None; 129, comment; 130, block; 130, 131; 130, 142; 131, expression_statement; 131, 132; 132, call; 132, 133; 132, 138; 133, attribute; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:self; 136, identifier:query_async_response; 137, identifier:set_response; 138, argument_list; 138, 139; 139, call; 139, 140; 139, 141; 140, identifier:CliResponse; 141, argument_list; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:self; 146, identifier:query_async_response; 147, None; 148, comment; 149, expression_statement; 149, 150; 150, call; 150, 151; 150, 156; 151, attribute; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:self; 154, identifier:logger; 155, identifier:error; 156, argument_list; 156, 157; 157, string:"No response received, DUT died"; 158, raise_statement; 158, 159; 159, call; 159, 160; 159, 161; 160, identifier:TestStepError; 161, argument_list; 161, 162; 162, binary_operator:+; 162, 163; 162, 168; 163, binary_operator:+; 163, 164; 163, 165; 164, string:"No response received, DUT "; 165, attribute; 165, 166; 165, 167; 166, identifier:self; 167, identifier:name; 168, string:" died"; 169, comment; 170, if_statement; 170, 171; 170, 176; 171, comparison_operator:is; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:self; 174, identifier:query_async_response; 175, None; 176, block; 176, 177; 176, 188; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 184; 179, attribute; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:self; 182, identifier:query_async_response; 183, identifier:set_response; 184, argument_list; 184, 185; 185, attribute; 185, 186; 185, 187; 186, identifier:self; 187, identifier:response_coming_in; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:self; 192, identifier:query_async_response; 193, None; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:self; 198, identifier:query_timeout; 199, integer:0; 200, return_statement; 200, 201; 201, attribute; 201, 202; 201, 203; 202, identifier:self; 203, identifier:response_coming_in
def _wait_for_exec_ready(self): """ Wait for response. :return: CliResponse object coming in :raises: TestStepTimeout, TestStepError """ while not self.response_received.wait(1) and self.query_timeout != 0: if self.query_timeout != 0 and self.query_timeout < self.get_time(): if self.prev: cmd = self.prev.cmd else: cmd = "???" self.logger.error("CMD timeout: "+ cmd) self.query_timeout = 0 raise TestStepTimeout(self.name + " CMD timeout: " + cmd) self.logger.debug("Waiting for response... " "timeout=%d", self.query_timeout - self.get_time()) self._dut_is_alive() if self.response_coming_in == -1: if self.query_async_response is not None: # fullfill the async response with a dummy response and clean the state self.query_async_response.set_response(CliResponse()) self.query_async_response = None # raise and log the error self.logger.error("No response received, DUT died") raise TestStepError("No response received, DUT "+self.name+" died") # if an async response is pending, fullfill it with the result if self.query_async_response is not None: self.query_async_response.set_response(self.response_coming_in) self.query_async_response = None self.query_timeout = 0 return self.response_coming_in
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:execute_command; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:req; 6, dictionary_splat_pattern; 6, 7; 7, identifier:kwargs; 8, block; 8, 9; 8, 11; 8, 93; 8, 94; 8, 155; 8, 156; 8, 164; 8, 183; 8, 189; 8, 200; 8, 225; 8, 240; 8, 247; 8, 277; 8, 325; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 17; 11, 18; 12, call; 12, 13; 12, 14; 13, identifier:isinstance; 14, argument_list; 14, 15; 14, 16; 15, identifier:req; 16, identifier:string_types; 17, comment; 18, block; 18, 19; 18, 23; 18, 24; 18, 28; 18, 32; 18, 70; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:timeout; 22, integer:50; 23, comment; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:wait; 27, True; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:asynchronous; 31, False; 32, for_statement; 32, 33; 32, 34; 32, 35; 33, identifier:key; 34, identifier:kwargs; 35, block; 35, 36; 36, if_statement; 36, 37; 36, 40; 36, 47; 36, 59; 37, comparison_operator:==; 37, 38; 37, 39; 38, identifier:key; 39, string:'wait'; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:wait; 44, subscript; 44, 45; 44, 46; 45, identifier:kwargs; 46, identifier:key; 47, elif_clause; 47, 48; 47, 51; 48, comparison_operator:==; 48, 49; 48, 50; 49, identifier:key; 50, string:'timeout'; 51, block; 51, 52; 51, 58; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:timeout; 55, subscript; 55, 56; 55, 57; 56, identifier:kwargs; 57, identifier:key; 58, comment; 59, elif_clause; 59, 60; 59, 63; 60, comparison_operator:==; 60, 61; 60, 62; 61, identifier:key; 62, string:'asynchronous'; 63, block; 63, 64; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:asynchronous; 67, subscript; 67, 68; 67, 69; 68, identifier:kwargs; 69, identifier:key; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:req; 73, call; 73, 74; 73, 75; 74, identifier:CliRequest; 75, argument_list; 75, 76; 75, 77; 75, 84; 75, 87; 75, 90; 76, identifier:req; 77, keyword_argument; 77, 78; 77, 79; 78, identifier:timestamp; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:self; 82, identifier:get_time; 83, argument_list; 84, keyword_argument; 84, 85; 84, 86; 85, identifier:wait; 86, identifier:wait; 87, keyword_argument; 87, 88; 87, 89; 88, identifier:timeout; 89, identifier:timeout; 90, keyword_argument; 90, 91; 90, 92; 91, identifier:asynchronous; 92, identifier:asynchronous; 93, comment; 94, if_statement; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:req; 97, identifier:wait; 98, block; 98, 99; 98, 107; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:response; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:self; 105, identifier:_wait_for_exec_ready; 106, argument_list; 107, if_statement; 107, 108; 107, 117; 108, boolean_operator:and; 108, 109; 108, 112; 109, comparison_operator:is; 109, 110; 109, 111; 110, identifier:response; 111, None; 112, comparison_operator:is; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:self; 115, identifier:query_async_expected; 116, None; 117, block; 117, 118; 117, 149; 118, if_statement; 118, 119; 118, 126; 119, comparison_operator:!=; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:response; 122, identifier:retcode; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:query_async_expected; 126, block; 126, 127; 126, 144; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 134; 129, attribute; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:self; 132, identifier:logger; 133, identifier:error; 134, argument_list; 134, 135; 134, 138; 134, 141; 135, concatenated_string; 135, 136; 135, 137; 136, string:"Asynch call returned unexpected result, "; 137, string:"expected %d was %d"; 138, attribute; 138, 139; 138, 140; 139, identifier:self; 140, identifier:query_async_expected; 141, attribute; 141, 142; 141, 143; 142, identifier:response; 143, identifier:retcode; 144, raise_statement; 144, 145; 145, call; 145, 146; 145, 147; 146, identifier:TestStepFail; 147, argument_list; 147, 148; 148, string:"Asynch call returned unexpected result"; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:self; 153, identifier:query_async_expected; 154, None; 155, comment; 156, expression_statement; 156, 157; 157, call; 157, 158; 157, 163; 158, attribute; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:self; 161, identifier:response_received; 162, identifier:clear; 163, argument_list; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:self; 168, identifier:query_timeout; 169, conditional_expression:if; 169, 170; 169, 179; 169, 182; 170, binary_operator:+; 170, 171; 170, 176; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:self; 174, identifier:get_time; 175, argument_list; 176, attribute; 176, 177; 176, 178; 177, identifier:req; 178, identifier:timeout; 179, attribute; 179, 180; 179, 181; 180, identifier:req; 181, identifier:wait; 182, integer:0; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:self; 187, identifier:query; 188, identifier:req; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:msg; 192, conditional_expression:if; 192, 193; 192, 196; 192, 199; 193, concatenated_string; 193, 194; 193, 195; 194, string:"Async CMD {}, "; 195, string:"timeout={}, time={}"; 196, attribute; 196, 197; 196, 198; 197, identifier:req; 198, identifier:asynchronous; 199, string:"CMD {}, timeout={}, time={}"; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 203; 202, identifier:msg; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:msg; 206, identifier:format; 207, argument_list; 207, 208; 207, 211; 207, 217; 208, attribute; 208, 209; 208, 210; 209, identifier:req; 210, identifier:cmd; 211, call; 211, 212; 211, 213; 212, identifier:int; 213, argument_list; 213, 214; 214, attribute; 214, 215; 214, 216; 215, identifier:self; 216, identifier:query_timeout; 217, call; 217, 218; 217, 219; 218, identifier:int; 219, argument_list; 219, 220; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:self; 223, identifier:get_time; 224, argument_list; 225, expression_statement; 225, 226; 226, call; 226, 227; 226, 232; 227, attribute; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:self; 230, identifier:logger; 231, identifier:debug; 232, argument_list; 232, 233; 232, 234; 233, identifier:msg; 234, keyword_argument; 234, 235; 234, 236; 235, identifier:extra; 236, dictionary; 236, 237; 237, pair; 237, 238; 237, 239; 238, string:'type'; 239, string:'<->'; 240, expression_statement; 240, 241; 241, call; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:Dut; 244, identifier:process_dut; 245, argument_list; 245, 246; 246, identifier:self; 247, if_statement; 247, 248; 247, 253; 248, comparison_operator:is; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:req; 251, identifier:asynchronous; 252, True; 253, block; 253, 254; 253, 262; 253, 269; 253, 275; 254, expression_statement; 254, 255; 255, assignment; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:self; 258, identifier:query_async_expected; 259, attribute; 259, 260; 259, 261; 260, identifier:req; 261, identifier:expected_retcode; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 265; 264, identifier:async_response; 265, call; 265, 266; 265, 267; 266, identifier:CliAsyncResponse; 267, argument_list; 267, 268; 268, identifier:self; 269, expression_statement; 269, 270; 270, assignment; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:self; 273, identifier:query_async_response; 274, identifier:async_response; 275, return_statement; 275, 276; 276, identifier:async_response; 277, if_statement; 277, 278; 277, 283; 278, comparison_operator:is; 278, 279; 278, 282; 279, attribute; 279, 280; 279, 281; 280, identifier:req; 281, identifier:wait; 282, False; 283, block; 283, 284; 283, 292; 283, 293; 283, 294; 283, 295; 283, 296; 283, 297; 283, 315; 283, 321; 284, expression_statement; 284, 285; 285, assignment; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:self; 288, identifier:query_async_expected; 289, attribute; 289, 290; 289, 291; 290, identifier:req; 291, identifier:expected_retcode; 292, comment; 293, comment; 294, comment; 295, comment; 296, comment; 297, if_statement; 297, 298; 297, 303; 298, comparison_operator:is; 298, 299; 298, 302; 299, attribute; 299, 300; 299, 301; 300, identifier:self; 301, identifier:query_async_response; 302, None; 303, block; 303, 304; 304, expression_statement; 304, 305; 305, call; 305, 306; 305, 311; 306, attribute; 306, 307; 306, 310; 307, attribute; 307, 308; 307, 309; 308, identifier:self; 309, identifier:query_async_response; 310, identifier:set_response; 311, argument_list; 311, 312; 312, call; 312, 313; 312, 314; 313, identifier:CliResponse; 314, argument_list; 315, expression_statement; 315, 316; 316, assignment; 316, 317; 316, 320; 317, attribute; 317, 318; 317, 319; 318, identifier:self; 319, identifier:query_async_response; 320, None; 321, return_statement; 321, 322; 322, call; 322, 323; 322, 324; 323, identifier:CliResponse; 324, argument_list; 325, return_statement; 325, 326; 326, call; 326, 327; 326, 330; 327, attribute; 327, 328; 327, 329; 328, identifier:self; 329, identifier:_wait_for_exec_ready; 330, argument_list
def execute_command(self, req, **kwargs): """ Execute command and return CliResponse :param req: String, command to be executed in DUT, or CliRequest, command class which contains all configurations like timeout. :param kwargs: Configurations (wait, timeout) which will be used when string mode is in use. :return: CliResponse, which contains all received data from Dut and parsed retcode. """ if isinstance(req, string_types): # backward compatible timeout = 50 # Use same default timeout as bench.py wait = True asynchronous = False for key in kwargs: if key == 'wait': wait = kwargs[key] elif key == 'timeout': timeout = kwargs[key] # [ms] elif key == 'asynchronous': asynchronous = kwargs[key] req = CliRequest(req, timestamp=self.get_time(), wait=wait, timeout=timeout, asynchronous=asynchronous) # wait for previous command ready if req.wait: response = self._wait_for_exec_ready() if response is not None and self.query_async_expected is not None: if response.retcode != self.query_async_expected: self.logger.error("Asynch call returned unexpected result, " "expected %d was %d", self.query_async_expected, response.retcode) raise TestStepFail("Asynch call returned unexpected result") self.query_async_expected = None # Tell Query to worker thread self.response_received.clear() self.query_timeout = self.get_time() + req.timeout if req.wait else 0 self.query = req msg = "Async CMD {}, " \ "timeout={}, time={}" if req.asynchronous else "CMD {}, timeout={}, time={}" msg = msg.format(req.cmd, int(self.query_timeout), int(self.get_time())) self.logger.debug(msg, extra={'type': '<->'}) Dut.process_dut(self) if req.asynchronous is True: self.query_async_expected = req.expected_retcode async_response = CliAsyncResponse(self) self.query_async_response = async_response return async_response if req.wait is False: self.query_async_expected = req.expected_retcode # if an async response was waiting, just discard the result # since the new command has already been sent... # This is not ideal but when a command has its flags "Wait == False" # the result of the previous command is already discarded in previous # stages if self.query_async_response is not None: self.query_async_response.set_response(CliResponse()) self.query_async_response = None return CliResponse() return self._wait_for_exec_ready()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 4; 1, 5; 2, function_name:run; 3, parameters; 4, comment; 5, block; 5, 6; 5, 8; 5, 23; 5, 345; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, call; 9, 10; 9, 15; 10, attribute; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:Dut; 13, identifier:_logger; 14, identifier:debug; 15, argument_list; 15, 16; 15, 17; 16, string:"Start DUT communication"; 17, keyword_argument; 17, 18; 17, 19; 18, identifier:extra; 19, dictionary; 19, 20; 20, pair; 20, 21; 20, 22; 21, string:'type'; 22, string:'<->'; 23, while_statement; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:Dut; 26, identifier:_run; 27, block; 27, 28; 27, 36; 28, expression_statement; 28, 29; 29, call; 29, 30; 29, 35; 30, attribute; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:Dut; 33, identifier:_sem; 34, identifier:acquire; 35, argument_list; 36, try_statement; 36, 37; 36, 341; 37, block; 37, 38; 37, 48; 37, 49; 37, 139; 37, 140; 37, 242; 37, 271; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:dut; 41, call; 41, 42; 41, 47; 42, attribute; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:Dut; 45, identifier:_signalled_duts; 46, identifier:pop; 47, argument_list; 48, comment; 49, if_statement; 49, 50; 49, 55; 50, comparison_operator:is; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:dut; 53, identifier:waiting_for_response; 54, None; 55, block; 55, 56; 55, 62; 55, 63; 55, 73; 55, 82; 55, 109; 55, 115; 55, 130; 55, 138; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:item; 59, attribute; 59, 60; 59, 61; 60, identifier:dut; 61, identifier:waiting_for_response; 62, comment; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:dut; 67, identifier:response_coming_in; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:dut; 71, identifier:_read_response; 72, argument_list; 73, if_statement; 73, 74; 73, 79; 73, 80; 74, comparison_operator:is; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:dut; 77, identifier:response_coming_in; 78, None; 79, comment; 80, block; 80, 81; 81, continue_statement; 82, if_statement; 82, 83; 82, 90; 83, call; 83, 84; 83, 85; 84, identifier:isinstance; 85, argument_list; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:dut; 88, identifier:response_coming_in; 89, identifier:CliResponse; 90, block; 90, 91; 91, expression_statement; 91, 92; 92, call; 92, 93; 92, 98; 93, attribute; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:dut; 96, identifier:response_coming_in; 97, identifier:set_response_time; 98, argument_list; 98, 99; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:item; 102, identifier:get_timedelta; 103, argument_list; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:dut; 107, identifier:get_time; 108, argument_list; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:dut; 113, identifier:waiting_for_response; 114, None; 115, expression_statement; 115, 116; 116, call; 116, 117; 116, 122; 117, attribute; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:dut; 120, identifier:logger; 121, identifier:debug; 122, argument_list; 122, 123; 122, 124; 123, string:"Got response"; 124, keyword_argument; 124, 125; 124, 126; 125, identifier:extra; 126, dictionary; 126, 127; 127, pair; 127, 128; 127, 129; 128, string:'type'; 129, string:'<->'; 130, expression_statement; 130, 131; 131, call; 131, 132; 131, 137; 132, attribute; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:dut; 135, identifier:response_received; 136, identifier:set; 137, argument_list; 138, continue_statement; 139, comment; 140, if_statement; 140, 141; 140, 146; 141, comparison_operator:is; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:dut; 144, identifier:query; 145, None; 146, block; 146, 147; 146, 153; 146, 159; 146, 176; 146, 206; 146, 212; 146, 213; 146, 241; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:item; 150, attribute; 150, 151; 150, 152; 151, identifier:dut; 152, identifier:query; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:dut; 157, identifier:query; 158, None; 159, expression_statement; 159, 160; 160, call; 160, 161; 160, 166; 161, attribute; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:dut; 164, identifier:logger; 165, identifier:info; 166, argument_list; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:item; 169, identifier:cmd; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:extra; 172, dictionary; 172, 173; 173, pair; 173, 174; 173, 175; 174, string:'type'; 175, string:'-->'; 176, try_statement; 176, 177; 176, 187; 177, block; 177, 178; 178, expression_statement; 178, 179; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:dut; 182, identifier:writeline; 183, argument_list; 183, 184; 184, attribute; 184, 185; 184, 186; 185, identifier:item; 186, identifier:cmd; 187, except_clause; 187, 188; 187, 189; 188, identifier:RuntimeError; 189, block; 189, 190; 189, 197; 189, 205; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:dut; 194, identifier:response_coming_in; 195, unary_operator:-; 195, 196; 196, integer:1; 197, expression_statement; 197, 198; 198, call; 198, 199; 198, 204; 199, attribute; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:dut; 202, identifier:response_received; 203, identifier:set; 204, argument_list; 205, continue_statement; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:dut; 210, identifier:prev; 211, identifier:item; 212, comment; 213, if_statement; 213, 214; 213, 217; 213, 218; 213, 225; 214, attribute; 214, 215; 214, 216; 215, identifier:item; 216, identifier:wait; 217, comment; 218, block; 218, 219; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:dut; 223, identifier:waiting_for_response; 224, identifier:item; 225, else_clause; 225, 226; 226, block; 226, 227; 226, 233; 227, expression_statement; 227, 228; 228, assignment; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:dut; 231, identifier:query_timeout; 232, integer:0; 233, expression_statement; 233, 234; 234, call; 234, 235; 234, 240; 235, attribute; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:dut; 238, identifier:response_received; 239, identifier:set; 240, argument_list; 241, continue_statement; 242, try_statement; 242, 243; 242, 252; 243, block; 243, 244; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 247; 246, identifier:line; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:dut; 250, identifier:readline; 251, argument_list; 252, except_clause; 252, 253; 252, 254; 253, identifier:RuntimeError; 254, block; 254, 255; 254, 262; 254, 270; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:dut; 259, identifier:response_coming_in; 260, unary_operator:-; 260, 261; 261, integer:1; 262, expression_statement; 262, 263; 263, call; 263, 264; 263, 269; 264, attribute; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:dut; 267, identifier:response_received; 268, identifier:set; 269, argument_list; 270, continue_statement; 271, if_statement; 271, 272; 271, 273; 272, identifier:line; 273, block; 273, 274; 273, 288; 273, 297; 273, 306; 273, 326; 274, if_statement; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:dut; 277, identifier:store_traces; 278, block; 278, 279; 279, expression_statement; 279, 280; 280, call; 280, 281; 280, 286; 281, attribute; 281, 282; 281, 285; 282, attribute; 282, 283; 282, 284; 283, identifier:dut; 284, identifier:traces; 285, identifier:append; 286, argument_list; 286, 287; 287, identifier:line; 288, expression_statement; 288, 289; 289, call; 289, 290; 289, 291; 290, identifier:EventObject; 291, argument_list; 291, 292; 291, 295; 291, 296; 292, attribute; 292, 293; 292, 294; 293, identifier:EventTypes; 294, identifier:DUT_LINE_RECEIVED; 295, identifier:dut; 296, identifier:line; 297, expression_statement; 297, 298; 298, assignment; 298, 299; 298, 300; 299, identifier:retcode; 300, call; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:dut; 303, identifier:check_retcode; 304, argument_list; 304, 305; 305, identifier:line; 306, if_statement; 306, 307; 306, 310; 307, comparison_operator:is; 307, 308; 307, 309; 308, identifier:retcode; 309, None; 310, block; 310, 311; 311, expression_statement; 311, 312; 312, call; 312, 313; 312, 318; 313, attribute; 313, 314; 313, 317; 314, attribute; 314, 315; 314, 316; 315, identifier:dut; 316, identifier:logger; 317, identifier:warning; 318, argument_list; 318, 319; 318, 320; 319, string:"unrequested retcode"; 320, keyword_argument; 320, 321; 320, 322; 321, identifier:extra; 322, dictionary; 322, 323; 323, pair; 323, 324; 323, 325; 324, string:'type'; 325, string:'!<-'; 326, expression_statement; 326, 327; 327, call; 327, 328; 327, 333; 328, attribute; 328, 329; 328, 332; 329, attribute; 329, 330; 329, 331; 330, identifier:dut; 331, identifier:logger; 332, identifier:debug; 333, argument_list; 333, 334; 333, 335; 334, identifier:line; 335, keyword_argument; 335, 336; 335, 337; 336, identifier:extra; 337, dictionary; 337, 338; 338, pair; 338, 339; 338, 340; 339, string:'type'; 340, string:'<<<'; 341, except_clause; 341, 342; 341, 343; 342, identifier:IndexError; 343, block; 343, 344; 344, pass_statement; 345, expression_statement; 345, 346; 346, call; 346, 347; 346, 352; 347, attribute; 347, 348; 347, 351; 348, attribute; 348, 349; 348, 350; 349, identifier:Dut; 350, identifier:_logger; 351, identifier:debug; 352, argument_list; 352, 353; 352, 354; 353, string:"End DUT communication"; 354, keyword_argument; 354, 355; 354, 356; 355, identifier:extra; 356, dictionary; 356, 357; 357, pair; 357, 358; 357, 359; 358, string:'type'; 359, string:'<->'
def run(): # pylint: disable=too-many-branches """ Main thread runner for all Duts. :return: Nothing """ Dut._logger.debug("Start DUT communication", extra={'type': '<->'}) while Dut._run: Dut._sem.acquire() try: dut = Dut._signalled_duts.pop() # Check for pending requests if dut.waiting_for_response is not None: item = dut.waiting_for_response # pylint: disable=protected-access dut.response_coming_in = dut._read_response() if dut.response_coming_in is None: # Continue to next node continue if isinstance(dut.response_coming_in, CliResponse): dut.response_coming_in.set_response_time(item.get_timedelta(dut.get_time())) dut.waiting_for_response = None dut.logger.debug("Got response", extra={'type': '<->'}) dut.response_received.set() continue # Check for new Request if dut.query is not None: item = dut.query dut.query = None dut.logger.info(item.cmd, extra={'type': '-->'}) try: dut.writeline(item.cmd) except RuntimeError: dut.response_coming_in = -1 dut.response_received.set() continue dut.prev = item # Save previous command for logging purposes if item.wait: # Only caller will care if this was asynchronous. dut.waiting_for_response = item else: dut.query_timeout = 0 dut.response_received.set() continue try: line = dut.readline() except RuntimeError: dut.response_coming_in = -1 dut.response_received.set() continue if line: if dut.store_traces: dut.traces.append(line) EventObject(EventTypes.DUT_LINE_RECEIVED, dut, line) retcode = dut.check_retcode(line) if retcode is not None: dut.logger.warning("unrequested retcode", extra={'type': '!<-'}) dut.logger.debug(line, extra={'type': '<<<'}) except IndexError: pass Dut._logger.debug("End DUT communication", extra={'type': '<->'})