sequence
stringlengths 546
16.2k
| code
stringlengths 108
19.3k
|
---|---|
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:databasesKEGG; 3, parameters; 3, 4; 3, 5; 4, identifier:organism; 5, identifier:ens_ids; 6, block; 6, 7; 6, 9; 6, 22; 6, 31; 6, 35; 6, 173; 6, 184; 6, 195; 6, 212; 6, 221; 6, 283; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:all_genes; 12, call; 12, 13; 12, 21; 13, attribute; 13, 14; 13, 20; 14, call; 14, 15; 14, 16; 15, identifier:urlopen; 16, argument_list; 16, 17; 17, binary_operator:+; 17, 18; 17, 19; 18, string:"http://rest.kegg.jp/list/"; 19, identifier:organism; 20, identifier:read; 21, argument_list; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:all_genes; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:all_genes; 28, identifier:split; 29, argument_list; 29, 30; 30, string:"\n"; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:dbs; 34, list:[]; 35, while_statement; 35, 36; 35, 42; 36, comparison_operator:==; 36, 37; 36, 41; 37, call; 37, 38; 37, 39; 38, identifier:len; 39, argument_list; 39, 40; 40, identifier:dbs; 41, integer:0; 42, block; 42, 43; 43, for_statement; 43, 44; 43, 45; 43, 46; 44, identifier:g; 45, identifier:all_genes; 46, block; 46, 47; 47, if_statement; 47, 48; 47, 54; 47, 170; 48, comparison_operator:==; 48, 49; 48, 53; 49, call; 49, 50; 49, 51; 50, identifier:len; 51, argument_list; 51, 52; 52, identifier:dbs; 53, integer:0; 54, block; 54, 55; 54, 66; 54, 79; 54, 88; 54, 100; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:kid; 58, subscript; 58, 59; 58, 65; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:g; 62, identifier:split; 63, argument_list; 63, 64; 64, string:"\t"; 65, integer:0; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:gene; 69, call; 69, 70; 69, 78; 70, attribute; 70, 71; 70, 77; 71, call; 71, 72; 71, 73; 72, identifier:urlopen; 73, argument_list; 73, 74; 74, binary_operator:+; 74, 75; 74, 76; 75, string:"http://rest.kegg.jp/get/"; 76, identifier:kid; 77, identifier:read; 78, argument_list; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:DBLINKS; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:gene; 85, identifier:split; 86, argument_list; 86, 87; 87, string:"\n"; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:DBLINKS; 91, list_comprehension; 91, 92; 91, 93; 91, 96; 92, identifier:s; 93, for_in_clause; 93, 94; 93, 95; 94, identifier:s; 95, identifier:DBLINKS; 96, if_clause; 96, 97; 97, comparison_operator:in; 97, 98; 97, 99; 98, string:":"; 99, identifier:s; 100, for_statement; 100, 101; 100, 102; 100, 103; 101, identifier:d; 102, identifier:DBLINKS; 103, block; 103, 104; 103, 113; 103, 124; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:test; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:d; 110, identifier:split; 111, argument_list; 111, 112; 112, string:" "; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:test; 116, subscript; 116, 117; 116, 118; 117, identifier:test; 118, binary_operator:-; 118, 119; 118, 123; 119, call; 119, 120; 119, 121; 120, identifier:len; 121, argument_list; 121, 122; 122, identifier:test; 123, integer:1; 124, if_statement; 124, 125; 124, 128; 125, comparison_operator:in; 125, 126; 125, 127; 126, identifier:test; 127, identifier:ens_ids; 128, block; 128, 129; 128, 141; 128, 152; 128, 163; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:DBLINK; 132, list_comprehension; 132, 133; 132, 134; 132, 137; 133, identifier:s; 134, for_in_clause; 134, 135; 134, 136; 135, identifier:s; 136, identifier:DBLINKS; 137, if_clause; 137, 138; 138, comparison_operator:in; 138, 139; 138, 140; 139, identifier:test; 140, identifier:s; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:DBLINK; 144, call; 144, 145; 144, 150; 145, attribute; 145, 146; 145, 149; 146, subscript; 146, 147; 146, 148; 147, identifier:DBLINK; 148, integer:0; 149, identifier:split; 150, argument_list; 150, 151; 151, string:":"; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:DBLINK; 155, subscript; 155, 156; 155, 157; 156, identifier:DBLINK; 157, binary_operator:-; 157, 158; 157, 162; 158, call; 158, 159; 158, 160; 159, identifier:len; 160, argument_list; 160, 161; 161, identifier:DBLINK; 162, integer:2; 163, expression_statement; 163, 164; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:dbs; 167, identifier:append; 168, argument_list; 168, 169; 169, identifier:DBLINK; 170, else_clause; 170, 171; 171, block; 171, 172; 172, break_statement; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:ens_db; 176, call; 176, 177; 176, 182; 177, attribute; 177, 178; 177, 181; 178, subscript; 178, 179; 178, 180; 179, identifier:dbs; 180, integer:0; 181, identifier:split; 182, argument_list; 182, 183; 183, string:" "; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:ens_db; 187, subscript; 187, 188; 187, 189; 188, identifier:ens_db; 189, binary_operator:-; 189, 190; 189, 194; 190, call; 190, 191; 190, 192; 191, identifier:len; 192, argument_list; 192, 193; 193, identifier:ens_db; 194, integer:1; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:test_db; 198, call; 198, 199; 198, 211; 199, attribute; 199, 200; 199, 210; 200, call; 200, 201; 200, 202; 201, identifier:urlopen; 202, argument_list; 202, 203; 203, binary_operator:+; 203, 204; 203, 209; 204, binary_operator:+; 204, 205; 204, 208; 205, binary_operator:+; 205, 206; 205, 207; 206, string:"http://rest.genome.jp/link/"; 207, identifier:ens_db; 208, string:"/"; 209, identifier:organism; 210, identifier:read; 211, argument_list; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:test_db; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:test_db; 218, identifier:split; 219, argument_list; 219, 220; 220, string:"\n"; 221, if_statement; 221, 222; 221, 228; 221, 262; 222, comparison_operator:==; 222, 223; 222, 227; 223, call; 223, 224; 223, 225; 224, identifier:len; 225, argument_list; 225, 226; 226, identifier:test_db; 227, integer:1; 228, block; 228, 229; 228, 240; 228, 245; 228, 250; 228, 258; 229, expression_statement; 229, 230; 230, call; 230, 231; 230, 232; 231, identifier:print; 232, argument_list; 232, 233; 233, binary_operator:+; 233, 234; 233, 239; 234, binary_operator:+; 234, 235; 234, 238; 235, binary_operator:+; 235, 236; 235, 237; 236, string:"For "; 237, identifier:organism; 238, string:" the following db was found: "; 239, identifier:ens_db; 240, expression_statement; 240, 241; 241, call; 241, 242; 241, 243; 242, identifier:print; 243, argument_list; 243, 244; 244, string:"This database does not seem to be valid KEGG-linked database identifier"; 245, expression_statement; 245, 246; 246, call; 246, 247; 246, 248; 247, identifier:print; 248, argument_list; 248, 249; 249, string:"For \n'hsa' use 'ensembl-hsa'\n'mmu' use 'ensembl-mmu'\n'cel' use 'EnsemblGenomes-Gn'\n'dme' use 'FlyBase'"; 250, expression_statement; 250, 251; 251, call; 251, 252; 251, 257; 252, attribute; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:sys; 255, identifier:stdout; 256, identifier:flush; 257, argument_list; 258, expression_statement; 258, 259; 259, assignment; 259, 260; 259, 261; 260, identifier:ens_db; 261, None; 262, else_clause; 262, 263; 263, block; 263, 264; 263, 275; 264, expression_statement; 264, 265; 265, call; 265, 266; 265, 267; 266, identifier:print; 267, argument_list; 267, 268; 268, binary_operator:+; 268, 269; 268, 274; 269, binary_operator:+; 269, 270; 269, 273; 270, binary_operator:+; 270, 271; 270, 272; 271, string:"For "; 272, identifier:organism; 273, string:" the following db was found: "; 274, identifier:ens_db; 275, expression_statement; 275, 276; 276, call; 276, 277; 276, 282; 277, attribute; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:sys; 280, identifier:stdout; 281, identifier:flush; 282, argument_list; 283, return_statement; 283, 284; 284, identifier:ens_db | def databasesKEGG(organism,ens_ids):
"""
Finds KEGG database identifiers for a respective organism given example ensembl ids.
:param organism: an organism as listed in organismsKEGG()
:param ens_ids: a list of ensenbl ids of the respective organism
:returns: nothing if no database was found, or a string if a database was found
"""
all_genes=urlopen("http://rest.kegg.jp/list/"+organism).read()
all_genes=all_genes.split("\n")
dbs=[]
while len(dbs) == 0:
for g in all_genes:
if len(dbs) == 0:
kid = g.split("\t")[0]
gene=urlopen("http://rest.kegg.jp/get/"+kid).read()
DBLINKS=gene.split("\n")
DBLINKS=[ s for s in DBLINKS if ":" in s ]
for d in DBLINKS:
test=d.split(" ")
test=test[len(test)-1]
if test in ens_ids:
DBLINK=[ s for s in DBLINKS if test in s ]
DBLINK=DBLINK[0].split(":")
DBLINK=DBLINK[len(DBLINK)-2]
dbs.append(DBLINK)
else:
break
ens_db=dbs[0].split(" ")
ens_db=ens_db[len(ens_db)-1]
test_db=urlopen("http://rest.genome.jp/link/"+ens_db+"/"+organism).read()
test_db=test_db.split("\n")
if len(test_db) == 1:
print("For "+organism+" the following db was found: "+ens_db)
print("This database does not seem to be valid KEGG-linked database identifier")
print("For \n'hsa' use 'ensembl-hsa'\n'mmu' use 'ensembl-mmu'\n'cel' use 'EnsemblGenomes-Gn'\n'dme' use 'FlyBase'")
sys.stdout.flush()
ens_db = None
else:
print("For "+organism+" the following db was found: "+ens_db)
sys.stdout.flush()
return ens_db |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:process; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:form; 6, identifier:post; 7, block; 7, 8; 7, 10; 7, 23; 7, 32; 7, 38; 7, 448; 7, 449; 7, 450; 7, 472; 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:form; 16, identifier:FormMeta; 17, block; 17, 18; 18, raise_statement; 18, 19; 19, call; 19, 20; 19, 21; 20, identifier:TypeError; 21, argument_list; 21, 22; 22, string:'Given form is not a valid WTForm.'; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:re_field_name; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:re; 29, identifier:compile; 30, argument_list; 30, 31; 31, string:r'\%([a-zA-Z0-9_]*)\%'; 32, class_definition; 32, 33; 32, 34; 32, 36; 33, identifier:F; 34, argument_list; 34, 35; 35, identifier:form; 36, block; 36, 37; 37, pass_statement; 38, for_statement; 38, 39; 38, 42; 38, 47; 39, pattern_list; 39, 40; 39, 41; 40, identifier:field; 41, identifier:data; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:post; 45, identifier:iteritems; 46, argument_list; 47, block; 47, 48; 47, 173; 47, 174; 47, 175; 47, 176; 47, 180; 47, 390; 47, 391; 47, 401; 47, 411; 47, 421; 47, 431; 48, if_statement; 48, 49; 48, 54; 48, 55; 48, 57; 49, comparison_operator:in; 49, 50; 49, 51; 50, identifier:field; 51, call; 51, 52; 51, 53; 52, identifier:F; 53, argument_list; 54, comment; 55, block; 55, 56; 56, continue_statement; 57, else_clause; 57, 58; 58, block; 58, 59; 59, if_statement; 59, 60; 59, 65; 59, 66; 59, 67; 59, 77; 59, 167; 60, comparison_operator:in; 60, 61; 60, 62; 61, identifier:field; 62, attribute; 62, 63; 62, 64; 63, identifier:self; 64, identifier:_dyn_fields; 65, comment; 66, comment; 67, block; 67, 68; 67, 72; 67, 73; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:field_cname; 71, identifier:field; 72, comment; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:current_set_number; 76, None; 77, elif_clause; 77, 78; 77, 121; 77, 122; 77, 123; 77, 124; 77, 125; 77, 126; 78, parenthesized_expression; 78, 79; 79, boolean_operator:and; 79, 80; 79, 93; 80, call; 80, 81; 80, 92; 81, attribute; 81, 82; 81, 91; 82, subscript; 82, 83; 82, 89; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:field; 86, identifier:split; 87, argument_list; 87, 88; 88, string:'_'; 89, unary_operator:-; 89, 90; 90, integer:1; 91, identifier:isdigit; 92, argument_list; 93, comparison_operator:in; 93, 94; 93, 114; 94, subscript; 94, 95; 94, 96; 95, identifier:field; 96, slice; 96, 97; 96, 98; 97, colon; 98, binary_operator:-; 98, 99; 98, 113; 99, unary_operator:-; 99, 100; 100, parenthesized_expression; 100, 101; 101, call; 101, 102; 101, 103; 102, identifier:len; 103, argument_list; 103, 104; 104, subscript; 104, 105; 104, 111; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:field; 108, identifier:split; 109, argument_list; 109, 110; 110, string:'_'; 111, unary_operator:-; 111, 112; 112, integer:1; 113, integer:1; 114, call; 114, 115; 114, 120; 115, attribute; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:self; 118, identifier:_dyn_fields; 119, identifier:keys; 120, argument_list; 121, comment; 122, comment; 123, comment; 124, comment; 125, comment; 126, block; 126, 127; 126, 150; 126, 151; 126, 152; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:field_cname; 130, subscript; 130, 131; 130, 132; 131, identifier:field; 132, slice; 132, 133; 132, 134; 133, colon; 134, binary_operator:-; 134, 135; 134, 149; 135, unary_operator:-; 135, 136; 136, parenthesized_expression; 136, 137; 137, call; 137, 138; 137, 139; 138, identifier:len; 139, argument_list; 139, 140; 140, subscript; 140, 141; 140, 147; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:field; 144, identifier:split; 145, argument_list; 145, 146; 146, string:'_'; 147, unary_operator:-; 147, 148; 148, integer:1; 149, integer:1; 150, comment; 151, comment; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:current_set_number; 155, call; 155, 156; 155, 157; 156, identifier:str; 157, argument_list; 157, 158; 158, subscript; 158, 159; 158, 165; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:field; 162, identifier:split; 163, argument_list; 163, 164; 164, string:'_'; 165, unary_operator:-; 165, 166; 166, integer:1; 167, else_clause; 167, 168; 167, 169; 167, 170; 167, 171; 168, comment; 169, comment; 170, comment; 171, block; 171, 172; 172, continue_statement; 173, comment; 174, comment; 175, comment; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:validators; 179, list:[]; 180, if_statement; 180, 181; 180, 188; 181, comparison_operator:in; 181, 182; 181, 183; 182, string:'validators'; 183, subscript; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:self; 186, identifier:_dyn_fields; 187, identifier:field_cname; 188, block; 188, 189; 189, for_statement; 189, 190; 189, 191; 189, 198; 190, identifier:validator; 191, subscript; 191, 192; 191, 197; 192, subscript; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:self; 195, identifier:_dyn_fields; 196, identifier:field_cname; 197, string:'validators'; 198, block; 198, 199; 198, 203; 198, 207; 198, 288; 198, 375; 198, 376; 198, 377; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:args; 202, list:[]; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:kwargs; 206, dictionary; 207, if_statement; 207, 208; 207, 220; 208, comparison_operator:in; 208, 209; 208, 210; 209, string:'args'; 210, subscript; 210, 211; 210, 216; 210, 217; 211, subscript; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:self; 214, identifier:_dyn_fields; 215, identifier:field_cname; 216, line_continuation:\; 217, attribute; 217, 218; 217, 219; 218, identifier:validator; 219, identifier:__name__; 220, block; 220, 221; 221, if_statement; 221, 222; 221, 224; 221, 240; 222, not_operator; 222, 223; 223, identifier:current_set_number; 224, block; 224, 225; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:args; 228, subscript; 228, 229; 228, 239; 229, subscript; 229, 230; 229, 235; 229, 236; 230, subscript; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:self; 233, identifier:_dyn_fields; 234, identifier:field_cname; 235, line_continuation:\; 236, attribute; 236, 237; 236, 238; 237, identifier:validator; 238, identifier:__name__; 239, string:'args'; 240, else_clause; 240, 241; 240, 242; 240, 243; 240, 244; 241, comment; 242, comment; 243, comment; 244, block; 244, 245; 245, for_statement; 245, 246; 245, 247; 245, 259; 246, identifier:arg; 247, subscript; 247, 248; 247, 258; 248, subscript; 248, 249; 248, 254; 248, 255; 249, subscript; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:self; 252, identifier:_dyn_fields; 253, identifier:field_cname; 254, line_continuation:\; 255, attribute; 255, 256; 255, 257; 256, identifier:validator; 257, identifier:__name__; 258, string:'args'; 259, block; 259, 260; 259, 281; 260, try_statement; 260, 261; 260, 276; 261, block; 261, 262; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 265; 264, identifier:arg; 265, call; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, identifier:re_field_name; 268, identifier:sub; 269, argument_list; 269, 270; 269, 275; 270, binary_operator:+; 270, 271; 270, 274; 271, binary_operator:+; 271, 272; 271, 273; 272, string:r'\1'; 273, string:'_'; 274, identifier:current_set_number; 275, identifier:arg; 276, except_clause; 276, 277; 276, 278; 276, 279; 277, comment; 278, comment; 279, block; 279, 280; 280, pass_statement; 281, expression_statement; 281, 282; 282, call; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, identifier:args; 285, identifier:append; 286, argument_list; 286, 287; 287, identifier:arg; 288, if_statement; 288, 289; 288, 301; 289, comparison_operator:in; 289, 290; 289, 291; 290, string:'kwargs'; 291, subscript; 291, 292; 291, 297; 291, 298; 292, subscript; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:self; 295, identifier:_dyn_fields; 296, identifier:field_cname; 297, line_continuation:\; 298, attribute; 298, 299; 298, 300; 299, identifier:validator; 300, identifier:__name__; 301, block; 301, 302; 302, if_statement; 302, 303; 302, 305; 302, 321; 303, not_operator; 303, 304; 304, identifier:current_set_number; 305, block; 305, 306; 306, expression_statement; 306, 307; 307, assignment; 307, 308; 307, 309; 308, identifier:kwargs; 309, subscript; 309, 310; 309, 320; 310, subscript; 310, 311; 310, 316; 310, 317; 311, subscript; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:self; 314, identifier:_dyn_fields; 315, identifier:field_cname; 316, line_continuation:\; 317, attribute; 317, 318; 317, 319; 318, identifier:validator; 319, identifier:__name__; 320, string:'kwargs'; 321, else_clause; 321, 322; 321, 323; 321, 324; 321, 325; 322, comment; 323, comment; 324, comment; 325, block; 325, 326; 326, for_statement; 326, 327; 326, 330; 326, 347; 327, pattern_list; 327, 328; 327, 329; 328, identifier:key; 329, identifier:arg; 330, call; 330, 331; 330, 334; 331, attribute; 331, 332; 331, 333; 332, identifier:self; 333, identifier:iteritems; 334, argument_list; 334, 335; 335, subscript; 335, 336; 335, 346; 336, subscript; 336, 337; 336, 342; 336, 343; 337, subscript; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, identifier:self; 340, identifier:_dyn_fields; 341, identifier:field_cname; 342, line_continuation:\; 343, attribute; 343, 344; 343, 345; 344, identifier:validator; 345, identifier:__name__; 346, string:'kwargs'; 347, block; 347, 348; 347, 369; 348, try_statement; 348, 349; 348, 364; 349, block; 349, 350; 350, expression_statement; 350, 351; 351, assignment; 351, 352; 351, 353; 352, identifier:arg; 353, call; 353, 354; 353, 357; 354, attribute; 354, 355; 354, 356; 355, identifier:re_field_name; 356, identifier:sub; 357, argument_list; 357, 358; 357, 363; 358, binary_operator:+; 358, 359; 358, 362; 359, binary_operator:+; 359, 360; 359, 361; 360, string:r'\1'; 361, string:'_'; 362, identifier:current_set_number; 363, identifier:arg; 364, except_clause; 364, 365; 364, 366; 364, 367; 365, comment; 366, comment; 367, block; 367, 368; 368, pass_statement; 369, expression_statement; 369, 370; 370, assignment; 370, 371; 370, 374; 371, subscript; 371, 372; 371, 373; 372, identifier:kwargs; 373, identifier:key; 374, identifier:arg; 375, comment; 376, comment; 377, expression_statement; 377, 378; 378, call; 378, 379; 378, 382; 379, attribute; 379, 380; 379, 381; 380, identifier:validators; 381, identifier:append; 382, argument_list; 382, 383; 383, call; 383, 384; 383, 385; 384, identifier:validator; 385, argument_list; 385, 386; 385, 388; 386, list_splat; 386, 387; 387, identifier:args; 388, dictionary_splat; 388, 389; 389, identifier:kwargs; 390, comment; 391, expression_statement; 391, 392; 392, assignment; 392, 393; 392, 394; 393, identifier:field_type; 394, subscript; 394, 395; 394, 400; 395, subscript; 395, 396; 395, 399; 396, attribute; 396, 397; 396, 398; 397, identifier:self; 398, identifier:_dyn_fields; 399, identifier:field_cname; 400, string:'type'; 401, expression_statement; 401, 402; 402, assignment; 402, 403; 402, 404; 403, identifier:field_label; 404, subscript; 404, 405; 404, 410; 405, subscript; 405, 406; 405, 409; 406, attribute; 406, 407; 406, 408; 407, identifier:self; 408, identifier:_dyn_fields; 409, identifier:field_cname; 410, string:'label'; 411, expression_statement; 411, 412; 412, assignment; 412, 413; 412, 414; 413, identifier:field_args; 414, subscript; 414, 415; 414, 420; 415, subscript; 415, 416; 415, 419; 416, attribute; 416, 417; 416, 418; 417, identifier:self; 418, identifier:_dyn_fields; 419, identifier:field_cname; 420, string:'args'; 421, expression_statement; 421, 422; 422, assignment; 422, 423; 422, 424; 423, identifier:field_kwargs; 424, subscript; 424, 425; 424, 430; 425, subscript; 425, 426; 425, 429; 426, attribute; 426, 427; 426, 428; 427, identifier:self; 428, identifier:_dyn_fields; 429, identifier:field_cname; 430, string:'kwargs'; 431, expression_statement; 431, 432; 432, call; 432, 433; 432, 434; 433, identifier:setattr; 434, argument_list; 434, 435; 434, 436; 434, 437; 435, identifier:F; 436, identifier:field; 437, call; 437, 438; 437, 439; 438, identifier:field_type; 439, argument_list; 439, 440; 439, 441; 439, 444; 439, 446; 440, identifier:field_label; 441, keyword_argument; 441, 442; 441, 443; 442, identifier:validators; 443, identifier:validators; 444, list_splat; 444, 445; 445, identifier:field_args; 446, dictionary_splat; 446, 447; 447, identifier:field_kwargs; 448, comment; 449, comment; 450, if_statement; 450, 451; 450, 454; 450, 455; 450, 456; 450, 463; 451, attribute; 451, 452; 451, 453; 452, identifier:self; 453, identifier:flask_wtf; 454, comment; 455, comment; 456, block; 456, 457; 457, expression_statement; 457, 458; 458, assignment; 458, 459; 458, 460; 459, identifier:form; 460, call; 460, 461; 460, 462; 461, identifier:F; 462, argument_list; 463, else_clause; 463, 464; 464, block; 464, 465; 465, expression_statement; 465, 466; 466, assignment; 466, 467; 466, 468; 467, identifier:form; 468, call; 468, 469; 468, 470; 469, identifier:F; 470, argument_list; 470, 471; 471, identifier:post; 472, return_statement; 472, 473; 473, identifier:form | def process(self, form, post):
""" Process the given WTForm Form object.
Itterate over the POST values and check each field
against the configuration that was made.
For each field that is valid, check all the validator
parameters for possible %field% replacement, then bind
these parameters to their validator.
Finally, add the field together with their validators
to the form.
:param form:
A valid WTForm Form object
:param post:
A MultiDict with the POST variables
"""
if not isinstance(form, FormMeta):
raise TypeError('Given form is not a valid WTForm.')
re_field_name = re.compile(r'\%([a-zA-Z0-9_]*)\%')
class F(form):
pass
for field, data in post.iteritems():
if field in F():
# Skip it if the POST field is one of the standard form fields.
continue
else:
if field in self._dyn_fields:
# If we can find the field name directly, it means the field
# is not a set so just set the canonical name and go on.
field_cname = field
# Since we are not in a set, (re)set the current set.
current_set_number = None
elif (field.split('_')[-1].isdigit()
and field[:-(len(field.split('_')[-1]))-1] in self._dyn_fields.keys()):
# If the field can be split on underscore characters,
# the last part contains only digits and the
# everything *but* the last part is found in the
# field configuration, we are good to go.
# (Cowardly refusing to use regex here).
field_cname = field[:-(len(field.split('_')[-1]))-1]
# Since we apparently are in a set, remember the
# the set number we are at.
current_set_number = str(field.split('_')[-1])
else:
# The field did not match to a canonical name
# from the fields dictionary or the name
# was malformed, throw it out.
continue
# Since the field seems to be a valid one, let us
# prepare the validator arguments and, if we are in a set
# replace the %field_name% convention where we find it.
validators = []
if 'validators' in self._dyn_fields[field_cname]:
for validator in self._dyn_fields[field_cname]['validators']:
args = []
kwargs = {}
if 'args' in self._dyn_fields[field_cname]\
[validator.__name__]:
if not current_set_number:
args = self._dyn_fields[field_cname]\
[validator.__name__]['args']
else:
# If we are currently in a set, append the set number
# to all the words that are decorated with %'s within
# the arguments.
for arg in self._dyn_fields[field_cname]\
[validator.__name__]['args']:
try:
arg = re_field_name.sub(r'\1'+'_'+current_set_number,
arg)
except:
# The argument does not seem to be regex-able
# Probably not a string, thus we can skip it.
pass
args.append(arg)
if 'kwargs' in self._dyn_fields[field_cname]\
[validator.__name__]:
if not current_set_number:
kwargs = self._dyn_fields[field_cname]\
[validator.__name__]['kwargs']
else:
# If we are currently in a set, append the set number
# to all the words that are decorated with %'s within
# the arguments.
for key, arg in self.iteritems(self._dyn_fields[field_cname]\
[validator.__name__]['kwargs']):
try:
arg = re_field_name.sub(r'\1'+'_'+current_set_number,
arg)
except:
# The argument does not seem to be regex-able
# Probably not a string, thus we can skip it.
pass
kwargs[key] = arg
# Finally, bind arguments to the validator
# and add it to the list
validators.append(validator(*args, **kwargs))
# The field is setup, it is time to add it to the form.
field_type = self._dyn_fields[field_cname]['type']
field_label = self._dyn_fields[field_cname]['label']
field_args = self._dyn_fields[field_cname]['args']
field_kwargs = self._dyn_fields[field_cname]['kwargs']
setattr(F, field, field_type(field_label,
validators=validators,
*field_args,
**field_kwargs))
# Create an instance of the form with the newly
# created fields and give it back to the caller.
if self.flask_wtf:
# Flask WTF overrides the form initialization
# and already injects the POST variables.
form = F()
else:
form = F(post)
return form |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:distinct_values; 3, parameters; 3, 4; 3, 5; 4, identifier:t_old; 5, identifier:t_new; 6, block; 6, 7; 6, 9; 6, 13; 6, 21; 6, 125; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:res; 12, list:[]; 13, expression_statement; 13, 14; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:res; 17, identifier:append; 18, argument_list; 18, 19; 19, list:[' -- NOT IN check -- ']; 19, 20; 20, string:' -- NOT IN check -- '; 21, for_statement; 21, 22; 21, 23; 21, 26; 22, identifier:new_col; 23, attribute; 23, 24; 23, 25; 24, identifier:t_new; 25, identifier:header; 26, block; 26, 27; 26, 37; 26, 38; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:dist_new; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:t_new; 33, identifier:get_distinct_values_from_cols; 34, argument_list; 34, 35; 35, list:[new_col]; 35, 36; 36, identifier:new_col; 37, comment; 38, for_statement; 38, 39; 38, 40; 38, 43; 39, identifier:old_col; 40, attribute; 40, 41; 40, 42; 41, identifier:t_old; 42, identifier:header; 43, block; 43, 44; 44, if_statement; 44, 45; 44, 48; 45, comparison_operator:==; 45, 46; 45, 47; 46, identifier:old_col; 47, identifier:new_col; 48, block; 48, 49; 48, 59; 48, 60; 48, 61; 48, 77; 48, 93; 48, 109; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:dist_old; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:t_old; 55, identifier:get_distinct_values_from_cols; 56, argument_list; 56, 57; 57, list:[old_col]; 57, 58; 58, identifier:old_col; 59, comment; 60, comment; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:not_in_new; 64, list_comprehension; 64, 65; 64, 66; 64, 71; 65, identifier:x; 66, for_in_clause; 66, 67; 66, 68; 67, identifier:x; 68, subscript; 68, 69; 68, 70; 69, identifier:dist_old; 70, integer:0; 71, if_clause; 71, 72; 72, comparison_operator:not; 72, 73; 72, 74; 73, identifier:x; 74, subscript; 74, 75; 74, 76; 75, identifier:dist_new; 76, integer:0; 77, if_statement; 77, 78; 77, 81; 77, 82; 78, comparison_operator:!=; 78, 79; 78, 80; 79, identifier:not_in_new; 80, list:[]; 81, comment; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:res; 87, identifier:append; 88, argument_list; 88, 89; 89, list:['Not in New', old_col, not_in_new]; 89, 90; 89, 91; 89, 92; 90, string:'Not in New'; 91, identifier:old_col; 92, identifier:not_in_new; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:not_in_old; 96, list_comprehension; 96, 97; 96, 98; 96, 103; 97, identifier:x; 98, for_in_clause; 98, 99; 98, 100; 99, identifier:x; 100, subscript; 100, 101; 100, 102; 101, identifier:dist_new; 102, integer:0; 103, if_clause; 103, 104; 104, comparison_operator:not; 104, 105; 104, 106; 105, identifier:x; 106, subscript; 106, 107; 106, 108; 107, identifier:dist_old; 108, integer:0; 109, if_statement; 109, 110; 109, 113; 109, 114; 110, comparison_operator:!=; 110, 111; 110, 112; 111, identifier:not_in_old; 112, list:[]; 113, comment; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:res; 119, identifier:append; 120, argument_list; 120, 121; 121, list:['Not in Old', new_col, not_in_old]; 121, 122; 121, 123; 121, 124; 122, string:'Not in Old'; 123, identifier:new_col; 124, identifier:not_in_old; 125, return_statement; 125, 126; 126, call; 126, 127; 126, 128; 127, identifier:sorted; 128, argument_list; 128, 129; 129, identifier:res | def distinct_values(t_old, t_new):
"""
for all columns, check which values are not in
the other table
"""
res = []
res.append([' -- NOT IN check -- '])
for new_col in t_new.header:
dist_new = t_new.get_distinct_values_from_cols([new_col])
#print('NEW Distinct values for ' + new_col + ' = ' + str(dist_new))
for old_col in t_old.header:
if old_col == new_col:
dist_old = t_old.get_distinct_values_from_cols([old_col])
#print('OLD Distinct values for ' + old_col + ' = ' + str(dist_old))
# Now compare the old and new values to see what is different
not_in_new = [x for x in dist_old[0] if x not in dist_new[0]]
if not_in_new != []:
#print(old_col + ' not_in_new = ' , not_in_new)
res.append(['Not in New', old_col, not_in_new])
not_in_old = [x for x in dist_new[0] if x not in dist_old[0]]
if not_in_old != []:
#print(new_col + ' not_in_old = ' , not_in_old)
res.append(['Not in Old', new_col, not_in_old])
return sorted(res) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:most_similar_catchments; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 4, identifier:self; 5, identifier:subject_catchment; 6, identifier:similarity_dist_function; 7, default_parameter; 7, 8; 7, 9; 8, identifier:records_limit; 9, integer:500; 10, default_parameter; 10, 11; 10, 12; 11, identifier:include_subject_catchment; 12, string:'auto'; 13, block; 13, 14; 13, 16; 13, 36; 13, 113; 13, 114; 13, 122; 13, 123; 13, 190; 13, 191; 13, 205; 13, 206; 13, 218; 13, 219; 13, 223; 13, 227; 13, 250; 14, expression_statement; 14, 15; 15, comment; 16, if_statement; 16, 17; 16, 23; 17, comparison_operator:not; 17, 18; 17, 19; 18, identifier:include_subject_catchment; 19, list:['auto', 'force', 'exclude']; 19, 20; 19, 21; 19, 22; 20, string:'auto'; 21, string:'force'; 22, string:'exclude'; 23, block; 23, 24; 24, raise_statement; 24, 25; 25, call; 25, 26; 25, 27; 26, identifier:ValueError; 27, argument_list; 27, 28; 28, binary_operator:+; 28, 29; 28, 35; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, string:"Parameter `include_subject_catchment={}` invalid."; 32, identifier:format; 33, argument_list; 33, 34; 34, identifier:include_subject_catchment; 35, string:"Must be one of `auto`, `force` or `exclude`."; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:query; 39, parenthesized_expression; 39, 40; 40, call; 40, 41; 40, 102; 41, attribute; 41, 42; 41, 101; 42, call; 42, 43; 42, 99; 43, attribute; 43, 44; 43, 98; 44, call; 44, 45; 44, 69; 45, attribute; 45, 46; 45, 68; 46, call; 46, 47; 46, 64; 47, attribute; 47, 48; 47, 63; 48, call; 48, 49; 48, 59; 49, attribute; 49, 50; 49, 58; 50, call; 50, 51; 50, 56; 51, attribute; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:self; 54, identifier:db_session; 55, identifier:query; 56, argument_list; 56, 57; 57, identifier:Catchment; 58, identifier:join; 59, argument_list; 59, 60; 60, attribute; 60, 61; 60, 62; 61, identifier:Catchment; 62, identifier:descriptors; 63, identifier:join; 64, argument_list; 64, 65; 65, attribute; 65, 66; 65, 67; 66, identifier:Catchment; 67, identifier:amax_records; 68, identifier:filter; 69, argument_list; 69, 70; 69, 77; 69, 80; 69, 93; 70, comparison_operator:!=; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:Catchment; 73, identifier:id; 74, attribute; 74, 75; 74, 76; 75, identifier:subject_catchment; 76, identifier:id; 77, attribute; 77, 78; 77, 79; 78, identifier:Catchment; 79, identifier:is_suitable_for_pooling; 80, call; 80, 81; 80, 82; 81, identifier:or_; 82, argument_list; 82, 83; 82, 88; 83, comparison_operator:<; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:Descriptors; 86, identifier:urbext2000; 87, float:0.03; 88, comparison_operator:==; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:Descriptors; 91, identifier:urbext2000; 92, None; 93, comparison_operator:==; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:AmaxRecord; 96, identifier:flag; 97, integer:0; 98, identifier:group_by; 99, argument_list; 99, 100; 100, identifier:Catchment; 101, identifier:having; 102, argument_list; 102, 103; 103, comparison_operator:>=; 103, 104; 103, 112; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:func; 107, identifier:count; 108, argument_list; 108, 109; 109, attribute; 109, 110; 109, 111; 110, identifier:AmaxRecord; 111, identifier:catchment_id; 112, integer:10; 113, comment; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:catchments; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:query; 120, identifier:all; 121, argument_list; 122, comment; 123, if_statement; 123, 124; 123, 127; 123, 146; 124, comparison_operator:==; 124, 125; 124, 126; 125, identifier:include_subject_catchment; 126, string:'force'; 127, block; 127, 128; 128, if_statement; 128, 129; 128, 137; 128, 138; 129, comparison_operator:>=; 129, 130; 129, 136; 130, call; 130, 131; 130, 132; 131, identifier:len; 132, argument_list; 132, 133; 133, attribute; 133, 134; 133, 135; 134, identifier:subject_catchment; 135, identifier:amax_records; 136, integer:10; 137, comment; 138, block; 138, 139; 139, expression_statement; 139, 140; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:catchments; 143, identifier:append; 144, argument_list; 144, 145; 145, identifier:subject_catchment; 146, elif_clause; 146, 147; 146, 150; 147, comparison_operator:==; 147, 148; 147, 149; 148, identifier:include_subject_catchment; 149, string:'auto'; 150, block; 150, 151; 151, if_statement; 151, 152; 151, 182; 152, boolean_operator:and; 152, 153; 152, 165; 152, 166; 153, boolean_operator:and; 153, 154; 153, 162; 154, comparison_operator:>=; 154, 155; 154, 161; 155, call; 155, 156; 155, 157; 156, identifier:len; 157, argument_list; 157, 158; 158, attribute; 158, 159; 158, 160; 159, identifier:subject_catchment; 160, identifier:amax_records; 161, integer:10; 162, attribute; 162, 163; 162, 164; 163, identifier:subject_catchment; 164, identifier:is_suitable_for_pooling; 165, line_continuation:\; 166, parenthesized_expression; 166, 167; 167, boolean_operator:or; 167, 168; 167, 175; 168, comparison_operator:<; 168, 169; 168, 174; 169, attribute; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:subject_catchment; 172, identifier:descriptors; 173, identifier:urbext2000; 174, float:0.03; 175, comparison_operator:is; 175, 176; 175, 181; 176, attribute; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:subject_catchment; 179, identifier:descriptors; 180, identifier:urbext2000; 181, None; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:catchments; 187, identifier:append; 188, argument_list; 188, 189; 189, identifier:subject_catchment; 190, comment; 191, for_statement; 191, 192; 191, 193; 191, 194; 192, identifier:catchment; 193, identifier:catchments; 194, block; 194, 195; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:catchment; 199, identifier:similarity_dist; 200, call; 200, 201; 200, 202; 201, identifier:similarity_dist_function; 202, argument_list; 202, 203; 202, 204; 203, identifier:subject_catchment; 204, identifier:catchment; 205, comment; 206, expression_statement; 206, 207; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:catchments; 210, identifier:sort; 211, argument_list; 211, 212; 212, keyword_argument; 212, 213; 212, 214; 213, identifier:key; 214, call; 214, 215; 214, 216; 215, identifier:attrgetter; 216, argument_list; 216, 217; 217, string:'similarity_dist'; 218, comment; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 222; 221, identifier:amax_records_count; 222, integer:0; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 226; 225, identifier:catchments_limited; 226, list:[]; 227, for_statement; 227, 228; 227, 229; 227, 230; 228, identifier:catchment; 229, identifier:catchments; 230, block; 230, 231; 230, 238; 230, 244; 231, expression_statement; 231, 232; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:catchments_limited; 235, identifier:append; 236, argument_list; 236, 237; 237, identifier:catchment; 238, expression_statement; 238, 239; 239, augmented_assignment:+=; 239, 240; 239, 241; 240, identifier:amax_records_count; 241, attribute; 241, 242; 241, 243; 242, identifier:catchment; 243, identifier:record_length; 244, if_statement; 244, 245; 244, 248; 245, comparison_operator:>=; 245, 246; 245, 247; 246, identifier:amax_records_count; 247, identifier:records_limit; 248, block; 248, 249; 249, break_statement; 250, return_statement; 250, 251; 251, identifier:catchments_limited | def most_similar_catchments(self, subject_catchment, similarity_dist_function, records_limit=500,
include_subject_catchment='auto'):
"""
Return a list of catchments sorted by hydrological similarity defined by `similarity_distance_function`
:param subject_catchment: subject catchment to find similar catchments for
:type subject_catchment: :class:`floodestimation.entities.Catchment`
:param similarity_dist_function: a method returning a similarity distance measure with 2 arguments, both
:class:`floodestimation.entities.Catchment` objects
:param include_subject_catchment: - `auto`: include subject catchment if suitable for pooling and if urbext < 0.03
- `force`: always include subject catchment having at least 10 years of data
- `exclude`: do not include the subject catchment
:type include_subject_catchment: str
:return: list of catchments sorted by similarity
:type: list of :class:`floodestimation.entities.Catchment`
"""
if include_subject_catchment not in ['auto', 'force', 'exclude']:
raise ValueError("Parameter `include_subject_catchment={}` invalid.".format(include_subject_catchment) +
"Must be one of `auto`, `force` or `exclude`.")
query = (self.db_session.query(Catchment).
join(Catchment.descriptors).
join(Catchment.amax_records).
filter(Catchment.id != subject_catchment.id,
Catchment.is_suitable_for_pooling,
or_(Descriptors.urbext2000 < 0.03, Descriptors.urbext2000 == None),
AmaxRecord.flag == 0).
group_by(Catchment).
having(func.count(AmaxRecord.catchment_id) >= 10)) # At least 10 AMAX records
catchments = query.all()
# Add subject catchment if required (may not exist in database, so add after querying db
if include_subject_catchment == 'force':
if len(subject_catchment.amax_records) >= 10: # Never include short-record catchments
catchments.append(subject_catchment)
elif include_subject_catchment == 'auto':
if len(subject_catchment.amax_records) >= 10 and subject_catchment.is_suitable_for_pooling and \
(subject_catchment.descriptors.urbext2000 < 0.03 or subject_catchment.descriptors.urbext2000 is None):
catchments.append(subject_catchment)
# Store the similarity distance as an additional attribute for each catchment
for catchment in catchments:
catchment.similarity_dist = similarity_dist_function(subject_catchment, catchment)
# Then simply sort by this attribute
catchments.sort(key=attrgetter('similarity_dist'))
# Limit catchments until total amax_records counts is at least `records_limit`, default 500
amax_records_count = 0
catchments_limited = []
for catchment in catchments:
catchments_limited.append(catchment)
amax_records_count += catchment.record_length
if amax_records_count >= records_limit:
break
return catchments_limited |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:readSAM; 3, parameters; 3, 4; 3, 5; 4, identifier:SAMfile; 5, default_parameter; 5, 6; 5, 7; 6, identifier:header; 7, False; 8, block; 8, 9; 8, 11; 8, 59; 8, 77; 8, 99; 8, 113; 8, 119; 8, 129; 8, 141; 8, 151; 8, 207; 8, 224; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 15; 12, comparison_operator:==; 12, 13; 12, 14; 13, identifier:header; 14, True; 15, block; 15, 16; 15, 24; 15, 28; 15, 53; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:f; 19, call; 19, 20; 19, 21; 20, identifier:open; 21, argument_list; 21, 22; 21, 23; 22, identifier:SAMfile; 23, string:"r+"; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:head; 27, list:[]; 28, for_statement; 28, 29; 28, 30; 28, 35; 29, identifier:line; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:f; 33, identifier:readlines; 34, argument_list; 35, block; 35, 36; 36, if_statement; 36, 37; 36, 42; 36, 50; 37, comparison_operator:==; 37, 38; 37, 41; 38, subscript; 38, 39; 38, 40; 39, identifier:line; 40, integer:0; 41, string:"@"; 42, block; 42, 43; 43, expression_statement; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:head; 47, identifier:append; 48, argument_list; 48, 49; 49, identifier:line; 50, else_clause; 50, 51; 51, block; 51, 52; 52, continue_statement; 53, expression_statement; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:f; 57, identifier:close; 58, argument_list; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:sam; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:pd; 65, identifier:read_table; 66, argument_list; 66, 67; 66, 68; 66, 71; 66, 74; 67, identifier:SAMfile; 68, keyword_argument; 68, 69; 68, 70; 69, identifier:sep; 70, string:"this_gives_one_column"; 71, keyword_argument; 71, 72; 71, 73; 72, identifier:comment; 73, string:"@"; 74, keyword_argument; 74, 75; 74, 76; 75, identifier:header; 76, None; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:sam; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:pd; 83, identifier:DataFrame; 84, argument_list; 84, 85; 85, call; 85, 86; 85, 98; 86, attribute; 86, 87; 86, 97; 87, call; 87, 88; 87, 95; 88, attribute; 88, 89; 88, 94; 89, attribute; 89, 90; 89, 93; 90, subscript; 90, 91; 90, 92; 91, identifier:sam; 92, integer:0; 93, identifier:str; 94, identifier:split; 95, argument_list; 95, 96; 96, string:"\t"; 97, identifier:tolist; 98, argument_list; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:acols; 102, list:[0,1,2,3,4,5,6,7,8,9]; 102, 103; 102, 104; 102, 105; 102, 106; 102, 107; 102, 108; 102, 109; 102, 110; 102, 111; 102, 112; 103, integer:0; 104, integer:1; 105, integer:2; 106, integer:3; 107, integer:4; 108, integer:5; 109, integer:6; 110, integer:7; 111, integer:8; 112, integer:9; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:sam_; 116, subscript; 116, 117; 116, 118; 117, identifier:sam; 118, identifier:acols; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:samcols; 122, call; 122, 123; 122, 128; 123, attribute; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:sam; 126, identifier:columns; 127, identifier:tolist; 128, argument_list; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:bcols; 132, list_comprehension; 132, 133; 132, 134; 132, 137; 133, identifier:s; 134, for_in_clause; 134, 135; 134, 136; 135, identifier:s; 136, identifier:samcols; 137, if_clause; 137, 138; 138, comparison_operator:not; 138, 139; 138, 140; 139, identifier:s; 140, identifier:acols; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 146; 143, subscript; 143, 144; 143, 145; 144, identifier:sam_; 145, integer:10; 146, subscript; 146, 147; 146, 148; 147, identifier:sam; 148, subscript; 148, 149; 148, 150; 149, identifier:bcols; 150, integer:0; 151, if_statement; 151, 152; 151, 158; 152, comparison_operator:>; 152, 153; 152, 157; 153, call; 153, 154; 153, 155; 154, identifier:len; 155, argument_list; 155, 156; 156, identifier:bcols; 157, integer:1; 158, block; 158, 159; 159, for_statement; 159, 160; 159, 161; 159, 166; 160, identifier:c; 161, subscript; 161, 162; 161, 163; 162, identifier:bcols; 163, slice; 163, 164; 163, 165; 164, integer:1; 165, colon; 166, block; 166, 167; 166, 180; 166, 193; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 172; 169, subscript; 169, 170; 169, 171; 170, identifier:sam_; 171, integer:10; 172, call; 172, 173; 172, 178; 173, attribute; 173, 174; 173, 177; 174, subscript; 174, 175; 174, 176; 175, identifier:sam_; 176, integer:10; 177, identifier:astype; 178, argument_list; 178, 179; 179, identifier:str; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 185; 182, subscript; 182, 183; 182, 184; 183, identifier:sam; 184, identifier:c; 185, call; 185, 186; 185, 191; 186, attribute; 186, 187; 186, 190; 187, subscript; 187, 188; 187, 189; 188, identifier:sam; 189, identifier:c; 190, identifier:astype; 191, argument_list; 191, 192; 192, identifier:str; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 198; 195, subscript; 195, 196; 195, 197; 196, identifier:sam_; 197, integer:10; 198, binary_operator:+; 198, 199; 198, 204; 199, binary_operator:+; 199, 200; 199, 203; 200, subscript; 200, 201; 200, 202; 201, identifier:sam_; 202, integer:10; 203, string:"\t"; 204, subscript; 204, 205; 204, 206; 205, identifier:sam; 206, identifier:c; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:sam_; 211, identifier:columns; 212, list:['QNAME','FLAG','RNAME','POS','MAPQ','CIGAR','RNEXT','PNEXT','TLEN','SEQ','QUAL']; 212, 213; 212, 214; 212, 215; 212, 216; 212, 217; 212, 218; 212, 219; 212, 220; 212, 221; 212, 222; 212, 223; 213, string:'QNAME'; 214, string:'FLAG'; 215, string:'RNAME'; 216, string:'POS'; 217, string:'MAPQ'; 218, string:'CIGAR'; 219, string:'RNEXT'; 220, string:'PNEXT'; 221, string:'TLEN'; 222, string:'SEQ'; 223, string:'QUAL'; 224, if_statement; 224, 225; 224, 228; 224, 233; 225, comparison_operator:==; 225, 226; 225, 227; 226, identifier:header; 227, True; 228, block; 228, 229; 229, return_statement; 229, 230; 230, expression_list; 230, 231; 230, 232; 231, identifier:sam_; 232, identifier:head; 233, else_clause; 233, 234; 234, block; 234, 235; 235, return_statement; 235, 236; 236, identifier:sam_ | def readSAM(SAMfile,header=False):
"""
Reads and parses a sam file.
:param SAMfile: /path/to/file.sam
:param header: logical, if True, reads the header information
:returns: a pandas dataframe with the respective SAM columns: 'QNAME','FLAG','RNAME','POS','MAPQ','CIGAR','RNEXT','PNEXT','TLEN','SEQ','QUAL' and a list of the headers if header=True
"""
if header==True:
f=open(SAMfile,"r+")
head=[]
for line in f.readlines():
if line[0]=="@":
head.append(line)
else:
continue
f.close()
sam=pd.read_table(SAMfile,sep="this_gives_one_column",comment="@",header=None)
sam=pd.DataFrame(sam[0].str.split("\t").tolist())
acols=[0,1,2,3,4,5,6,7,8,9]
sam_=sam[acols]
samcols=sam.columns.tolist()
bcols=[ s for s in samcols if s not in acols ]
sam_[10]=sam[bcols[0]]
if len(bcols) > 1:
for c in bcols[1:]:
sam_[10]=sam_[10].astype(str)
sam[c]=sam[c].astype(str)
sam_[10]=sam_[10]+"\t"+sam[c]
sam_.columns=['QNAME','FLAG','RNAME','POS','MAPQ','CIGAR','RNEXT','PNEXT','TLEN','SEQ','QUAL']
if header==True:
return sam_, head
else:
return sam_ |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:SAMflags; 3, parameters; 3, 4; 4, identifier:x; 5, block; 5, 6; 5, 8; 5, 12; 5, 27; 5, 34; 5, 49; 5, 56; 5, 71; 5, 78; 5, 93; 5, 100; 5, 115; 5, 122; 5, 137; 5, 144; 5, 159; 5, 166; 5, 181; 5, 188; 5, 203; 5, 210; 5, 225; 5, 232; 5, 247; 5, 254; 5, 269; 5, 276; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:flags; 11, list:[]; 12, if_statement; 12, 13; 12, 16; 12, 21; 13, binary_operator:&; 13, 14; 13, 15; 14, identifier:x; 15, integer:1; 16, block; 16, 17; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:l; 20, string:"1: Read paired"; 21, else_clause; 21, 22; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:l; 26, string:"0: Read unpaired"; 27, expression_statement; 27, 28; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:flags; 31, identifier:append; 32, argument_list; 32, 33; 33, identifier:l; 34, if_statement; 34, 35; 34, 38; 34, 43; 35, binary_operator:&; 35, 36; 35, 37; 36, identifier:x; 37, integer:2; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:l; 42, string:"1: Read mapped in proper pair"; 43, else_clause; 43, 44; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:l; 48, string:"0: Read not mapped in proper pair"; 49, expression_statement; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:flags; 53, identifier:append; 54, argument_list; 54, 55; 55, identifier:l; 56, if_statement; 56, 57; 56, 60; 56, 65; 57, binary_operator:&; 57, 58; 57, 59; 58, identifier:x; 59, integer:4; 60, block; 60, 61; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:l; 64, string:"1: Read unmapped"; 65, else_clause; 65, 66; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:l; 70, string:"0: Read mapped"; 71, expression_statement; 71, 72; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:flags; 75, identifier:append; 76, argument_list; 76, 77; 77, identifier:l; 78, if_statement; 78, 79; 78, 82; 78, 87; 79, binary_operator:&; 79, 80; 79, 81; 80, identifier:x; 81, integer:8; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:l; 86, string:"1: Mate unmapped"; 87, else_clause; 87, 88; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:l; 92, string:"0: Mate mapped"; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:flags; 97, identifier:append; 98, argument_list; 98, 99; 99, identifier:l; 100, if_statement; 100, 101; 100, 104; 100, 109; 101, binary_operator:&; 101, 102; 101, 103; 102, identifier:x; 103, integer:16; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:l; 108, string:"1: Read reverse strand"; 109, else_clause; 109, 110; 110, block; 110, 111; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:l; 114, string:"0: Read direct strand"; 115, expression_statement; 115, 116; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:flags; 119, identifier:append; 120, argument_list; 120, 121; 121, identifier:l; 122, if_statement; 122, 123; 122, 126; 122, 131; 123, binary_operator:&; 123, 124; 123, 125; 124, identifier:x; 125, integer:32; 126, block; 126, 127; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:l; 130, string:"1: Mate reverse strand"; 131, else_clause; 131, 132; 132, block; 132, 133; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:l; 136, string:"0: Mate direct strand"; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:flags; 141, identifier:append; 142, argument_list; 142, 143; 143, identifier:l; 144, if_statement; 144, 145; 144, 148; 144, 153; 145, binary_operator:&; 145, 146; 145, 147; 146, identifier:x; 147, integer:64; 148, block; 148, 149; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:l; 152, string:"1: First in pair"; 153, else_clause; 153, 154; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:l; 158, string:"0: Second in pair"; 159, expression_statement; 159, 160; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:flags; 163, identifier:append; 164, argument_list; 164, 165; 165, identifier:l; 166, if_statement; 166, 167; 166, 170; 166, 175; 167, binary_operator:&; 167, 168; 167, 169; 168, identifier:x; 169, integer:128; 170, block; 170, 171; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:l; 174, string:"1: Second in pair"; 175, else_clause; 175, 176; 176, block; 176, 177; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:l; 180, string:"0: First in pair"; 181, expression_statement; 181, 182; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:flags; 185, identifier:append; 186, argument_list; 186, 187; 187, identifier:l; 188, if_statement; 188, 189; 188, 192; 188, 197; 189, binary_operator:&; 189, 190; 189, 191; 190, identifier:x; 191, integer:256; 192, block; 192, 193; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:l; 196, string:"1: Not primary alignment"; 197, else_clause; 197, 198; 198, block; 198, 199; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:l; 202, string:"0: Primary alignment"; 203, expression_statement; 203, 204; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:flags; 207, identifier:append; 208, argument_list; 208, 209; 209, identifier:l; 210, if_statement; 210, 211; 210, 214; 210, 219; 211, binary_operator:&; 211, 212; 211, 213; 212, identifier:x; 213, integer:512; 214, block; 214, 215; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:l; 218, string:"1: Read fails platform/vendor quality checks"; 219, else_clause; 219, 220; 220, block; 220, 221; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:l; 224, string:"0: Read passes platform/vendor quality checks"; 225, expression_statement; 225, 226; 226, call; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:flags; 229, identifier:append; 230, argument_list; 230, 231; 231, identifier:l; 232, if_statement; 232, 233; 232, 236; 232, 241; 233, binary_operator:&; 233, 234; 233, 235; 234, identifier:x; 235, integer:1024; 236, block; 236, 237; 237, expression_statement; 237, 238; 238, assignment; 238, 239; 238, 240; 239, identifier:l; 240, string:"1: Read is PCR or optical duplicate"; 241, else_clause; 241, 242; 242, block; 242, 243; 243, expression_statement; 243, 244; 244, assignment; 244, 245; 244, 246; 245, identifier:l; 246, string:"0: Read is not PCR or optical duplicate"; 247, expression_statement; 247, 248; 248, call; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:flags; 251, identifier:append; 252, argument_list; 252, 253; 253, identifier:l; 254, if_statement; 254, 255; 254, 258; 254, 263; 255, binary_operator:&; 255, 256; 255, 257; 256, identifier:x; 257, integer:2048; 258, block; 258, 259; 259, expression_statement; 259, 260; 260, assignment; 260, 261; 260, 262; 261, identifier:l; 262, string:"1: Supplementary alignment"; 263, else_clause; 263, 264; 264, block; 264, 265; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 268; 267, identifier:l; 268, string:"0: Not supplementary alignment"; 269, expression_statement; 269, 270; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:flags; 273, identifier:append; 274, argument_list; 274, 275; 275, identifier:l; 276, return_statement; 276, 277; 277, identifier:flags | def SAMflags(x):
"""
Explains a SAM flag.
:param x: flag
:returns: complete SAM flag explanaition
"""
flags=[]
if x & 1:
l="1: Read paired"
else:
l="0: Read unpaired"
flags.append(l)
if x & 2 :
l="1: Read mapped in proper pair"
else:
l="0: Read not mapped in proper pair"
flags.append(l)
if x & 4 :
l="1: Read unmapped"
else:
l="0: Read mapped"
flags.append(l)
if x & 8 :
l="1: Mate unmapped"
else:
l="0: Mate mapped"
flags.append(l)
if x & 16 :
l="1: Read reverse strand"
else:
l="0: Read direct strand"
flags.append(l)
if x & 32 :
l="1: Mate reverse strand"
else:
l="0: Mate direct strand"
flags.append(l)
if x & 64 :
l="1: First in pair"
else:
l="0: Second in pair"
flags.append(l)
if x & 128 :
l="1: Second in pair"
else:
l="0: First in pair"
flags.append(l)
if x & 256 :
l="1: Not primary alignment"
else:
l="0: Primary alignment"
flags.append(l)
if x & 512 :
l="1: Read fails platform/vendor quality checks"
else:
l="0: Read passes platform/vendor quality checks"
flags.append(l)
if x & 1024 :
l="1: Read is PCR or optical duplicate"
else:
l="0: Read is not PCR or optical duplicate"
flags.append(l)
if x & 2048 :
l="1: Supplementary alignment"
else:
l="0: Not supplementary alignment"
flags.append(l)
return flags |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 74; 2, function_name:BLASTquery; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 11; 3, 14; 3, 17; 3, 18; 3, 21; 3, 24; 3, 25; 3, 28; 3, 31; 3, 32; 3, 35; 3, 38; 3, 39; 3, 42; 3, 43; 3, 46; 3, 49; 3, 50; 3, 53; 3, 56; 3, 57; 3, 60; 3, 63; 3, 64; 3, 67; 3, 70; 3, 71; 4, identifier:query; 5, identifier:database; 6, identifier:program; 7, default_parameter; 7, 8; 7, 9; 8, identifier:filter; 9, None; 10, line_continuation:\; 11, default_parameter; 11, 12; 11, 13; 12, identifier:format_type; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:expect; 16, None; 17, line_continuation:\; 18, default_parameter; 18, 19; 18, 20; 19, identifier:nucl_reward; 20, None; 21, default_parameter; 21, 22; 21, 23; 22, identifier:nucl_penalty; 23, None; 24, line_continuation:\; 25, default_parameter; 25, 26; 25, 27; 26, identifier:gapcosts; 27, None; 28, default_parameter; 28, 29; 28, 30; 29, identifier:matrix; 30, None; 31, line_continuation:\; 32, default_parameter; 32, 33; 32, 34; 33, identifier:hitlist_size; 34, None; 35, default_parameter; 35, 36; 35, 37; 36, identifier:descriptions; 37, None; 38, line_continuation:\; 39, default_parameter; 39, 40; 39, 41; 40, identifier:alignments; 41, None; 42, line_continuation:\; 43, default_parameter; 43, 44; 43, 45; 44, identifier:ncbi_gi; 45, None; 46, default_parameter; 46, 47; 46, 48; 47, identifier:threshold; 48, None; 49, line_continuation:\; 50, default_parameter; 50, 51; 50, 52; 51, identifier:word_size; 52, None; 53, default_parameter; 53, 54; 53, 55; 54, identifier:composition_based_statistics; 55, None; 56, line_continuation:\; 57, default_parameter; 57, 58; 57, 59; 58, identifier:organism; 59, None; 60, default_parameter; 60, 61; 60, 62; 61, identifier:others; 62, None; 63, line_continuation:\; 64, default_parameter; 64, 65; 64, 66; 65, identifier:num_threads; 66, None; 67, default_parameter; 67, 68; 67, 69; 68, identifier:baseURL; 69, string:"http://blast.ncbi.nlm.nih.gov"; 70, line_continuation:\; 71, default_parameter; 71, 72; 71, 73; 72, identifier:verbose; 73, False; 74, block; 74, 75; 74, 77; 74, 118; 74, 124; 74, 149; 74, 213; 74, 224; 74, 230; 74, 246; 74, 257; 74, 268; 74, 280; 74, 313; 75, expression_statement; 75, 76; 76, comment; 77, if_statement; 77, 78; 77, 79; 77, 112; 78, identifier:organism; 79, block; 79, 80; 79, 108; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:organism; 83, call; 83, 84; 83, 105; 84, attribute; 84, 85; 84, 104; 85, call; 85, 86; 85, 101; 86, attribute; 86, 87; 86, 100; 87, call; 87, 88; 87, 97; 88, attribute; 88, 89; 88, 96; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:organism; 92, identifier:replace; 93, argument_list; 93, 94; 93, 95; 94, string:" "; 95, string:"%20"; 96, identifier:replace; 97, argument_list; 97, 98; 97, 99; 98, string:"("; 99, string:"%28"; 100, identifier:replace; 101, argument_list; 101, 102; 101, 103; 102, string:")"; 103, string:"%29"; 104, identifier:replace; 105, argument_list; 105, 106; 105, 107; 106, string:":"; 107, string:"%3A"; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:EQ_MENU; 111, identifier:organism; 112, else_clause; 112, 113; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:EQ_MENU; 117, None; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:URL; 121, binary_operator:+; 121, 122; 121, 123; 122, identifier:baseURL; 123, string:"/Blast.cgi?"; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:URL; 127, binary_operator:+; 127, 128; 127, 145; 128, binary_operator:+; 128, 129; 128, 144; 129, binary_operator:+; 129, 130; 129, 140; 130, binary_operator:+; 130, 131; 130, 139; 131, binary_operator:+; 131, 132; 131, 135; 132, binary_operator:+; 132, 133; 132, 134; 133, identifier:URL; 134, string:"QUERY="; 135, call; 135, 136; 135, 137; 136, identifier:str; 137, argument_list; 137, 138; 138, identifier:query; 139, string:"&DATABASE="; 140, call; 140, 141; 140, 142; 141, identifier:str; 142, argument_list; 142, 143; 143, identifier:database; 144, string:"&PROGRAM="; 145, call; 145, 146; 145, 147; 146, identifier:str; 147, argument_list; 147, 148; 148, identifier:program; 149, for_statement; 149, 150; 149, 153; 149, 194; 150, pattern_list; 150, 151; 150, 152; 151, identifier:o; 152, identifier:varname; 153, call; 153, 154; 153, 155; 154, identifier:zip; 155, argument_list; 155, 156; 155, 176; 155, 177; 156, list:[filter, format_type, expect, nucl_reward, nucl_penalty,\
gapcosts, matrix, hitlist_size, descriptions, alignments,\
ncbi_gi, threshold, word_size, composition_based_statistics,\
EQ_MENU, num_threads]; 156, 157; 156, 158; 156, 159; 156, 160; 156, 161; 156, 162; 156, 163; 156, 164; 156, 165; 156, 166; 156, 167; 156, 168; 156, 169; 156, 170; 156, 171; 156, 172; 156, 173; 156, 174; 156, 175; 157, identifier:filter; 158, identifier:format_type; 159, identifier:expect; 160, identifier:nucl_reward; 161, identifier:nucl_penalty; 162, line_continuation:\; 163, identifier:gapcosts; 164, identifier:matrix; 165, identifier:hitlist_size; 166, identifier:descriptions; 167, identifier:alignments; 168, line_continuation:\; 169, identifier:ncbi_gi; 170, identifier:threshold; 171, identifier:word_size; 172, identifier:composition_based_statistics; 173, line_continuation:\; 174, identifier:EQ_MENU; 175, identifier:num_threads; 176, line_continuation:\; 177, list:['FILTER' , 'FORMAT_TYPE', 'EXPECT', 'NUCL_REWARD', 'NUCL_PENALTY',\
'GAPCOSTS', 'MATRIX', 'HITLIST_SIZE', 'DESCRIPTIONS', 'ALIGNMENTS',\
'NCBI_GI', 'THRESHOLD', 'WORD_SIZE', 'COMPOSITION_BASED_STATISTICS',\
'EQ_MENU', 'NUM_THREADS']; 177, 178; 177, 179; 177, 180; 177, 181; 177, 182; 177, 183; 177, 184; 177, 185; 177, 186; 177, 187; 177, 188; 177, 189; 177, 190; 177, 191; 177, 192; 177, 193; 178, string:'FILTER'; 179, string:'FORMAT_TYPE'; 180, string:'EXPECT'; 181, string:'NUCL_REWARD'; 182, string:'NUCL_PENALTY'; 183, string:'GAPCOSTS'; 184, string:'MATRIX'; 185, string:'HITLIST_SIZE'; 186, string:'DESCRIPTIONS'; 187, string:'ALIGNMENTS'; 188, string:'NCBI_GI'; 189, string:'THRESHOLD'; 190, string:'WORD_SIZE'; 191, string:'COMPOSITION_BASED_STATISTICS'; 192, string:'EQ_MENU'; 193, string:'NUM_THREADS'; 194, block; 194, 195; 195, if_statement; 195, 196; 195, 197; 196, identifier:o; 197, block; 197, 198; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:URL; 201, binary_operator:+; 201, 202; 201, 209; 202, binary_operator:+; 202, 203; 202, 208; 203, binary_operator:+; 203, 204; 203, 207; 204, binary_operator:+; 204, 205; 204, 206; 205, identifier:URL; 206, string:"&"; 207, identifier:varname; 208, string:"="; 209, call; 209, 210; 209, 211; 210, identifier:str; 211, argument_list; 211, 212; 212, identifier:o; 213, if_statement; 213, 214; 213, 215; 214, identifier:others; 215, block; 215, 216; 216, expression_statement; 216, 217; 217, assignment; 217, 218; 217, 219; 218, identifier:URL; 219, binary_operator:+; 219, 220; 219, 223; 220, binary_operator:+; 220, 221; 220, 222; 221, identifier:URL; 222, string:"&"; 223, identifier:others; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:URL; 227, binary_operator:+; 227, 228; 227, 229; 228, identifier:URL; 229, string:"&CMD=Put"; 230, if_statement; 230, 231; 230, 232; 231, identifier:verbose; 232, block; 232, 233; 232, 238; 233, expression_statement; 233, 234; 234, call; 234, 235; 234, 236; 235, identifier:print; 236, argument_list; 236, 237; 237, identifier:URL; 238, expression_statement; 238, 239; 239, call; 239, 240; 239, 245; 240, attribute; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:sys; 243, identifier:stdout; 244, identifier:flush; 245, argument_list; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:response; 249, call; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:requests; 252, identifier:get; 253, argument_list; 253, 254; 254, keyword_argument; 254, 255; 254, 256; 255, identifier:url; 256, identifier:URL; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 260; 259, identifier:r; 260, call; 260, 261; 260, 266; 261, attribute; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:response; 264, identifier:content; 265, identifier:split; 266, argument_list; 266, 267; 267, string:"\n"; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 271; 270, identifier:RID; 271, list_comprehension; 271, 272; 271, 273; 271, 276; 272, identifier:s; 273, for_in_clause; 273, 274; 273, 275; 274, identifier:s; 275, identifier:r; 276, if_clause; 276, 277; 277, comparison_operator:in; 277, 278; 277, 279; 278, string:"RID = "; 279, identifier:s; 280, if_statement; 280, 281; 280, 287; 280, 302; 281, comparison_operator:>; 281, 282; 281, 286; 282, call; 282, 283; 282, 284; 283, identifier:len; 284, argument_list; 284, 285; 285, identifier:RID; 286, integer:0; 287, block; 287, 288; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 291; 290, identifier:RID; 291, subscript; 291, 292; 291, 300; 292, call; 292, 293; 292, 298; 293, attribute; 293, 294; 293, 297; 294, subscript; 294, 295; 294, 296; 295, identifier:RID; 296, integer:0; 297, identifier:split; 298, argument_list; 298, 299; 299, string:" "; 300, unary_operator:-; 300, 301; 301, integer:1; 302, else_clause; 302, 303; 303, block; 303, 304; 303, 309; 304, expression_statement; 304, 305; 305, call; 305, 306; 305, 307; 306, identifier:print; 307, argument_list; 307, 308; 308, string:"Could not return an RID for this query."; 309, expression_statement; 309, 310; 310, assignment; 310, 311; 310, 312; 311, identifier:RID; 312, None; 313, return_statement; 313, 314; 314, identifier:RID | def BLASTquery(query,database,program,filter=None,\
format_type=None, expect=None,\
nucl_reward=None, nucl_penalty=None,\
gapcosts=None, matrix=None,\
hitlist_size=None, descriptions=None,\
alignments=None,\
ncbi_gi=None, threshold=None,\
word_size=None, composition_based_statistics=None,\
organism=None, others=None,\
num_threads=None, baseURL="http://blast.ncbi.nlm.nih.gov",\
verbose=False):
"""
Performs a blast query online.
As in https://ncbi.github.io/blast-cloud/
:param query: Search query. Allowed values: Accession, GI, or FASTA.
:param database: BLAST database. Allowed values: nt, nr, refseq_rna, refseq_protein, swissprot, pdbaa, pdbnt
:param program: BLAST program. Allowed values: blastn, megablast, blastp, blastx, tblastn, tblastx
:param filter: Low complexity filtering. Allowed values: F to disable. T or L to enable. Prepend "m" for mask at lookup (e.g., mL)
:param format_type: Report type. Allowed values: HTML, Text, XML, XML2, JSON2, or Tabular. HTML is the default.
:param expect: Expect value. Allowed values: Number greater than zero.
:param nucl_reward: Reward for matching bases (BLASTN and megaBLAST). Allowed values: Integer greater than zero.
:param nucl_penalty: Cost for mismatched bases (BLASTN and megaBLAST). Allowed values: Integer less than zero.
:param gapcosts: Gap existence and extension costs. Allowed values: Pair of positive integers separated by a space such as "11 1".
:param matrix: Scoring matrix name. Allowed values: One of BLOSUM45, BLOSUM50, BLOSUM62, BLOSUM80, BLOSUM90, PAM250, PAM30 or PAM70. Default: BLOSUM62 for all applicable programs.
:param hitlist_size: Number of databases sequences to keep. Allowed values: Integer greater than zero.
:param descriptions: Number of descriptions to print (applies to HTML and Text). Allowed values: Integer greater than zero.
:param alignments: Number of alignments to print (applies to HTML and Text). Allowed values: Integer greater than zero.
:param ncbi_gi: Show NCBI GIs in report. Allowed values: T or F.
:param threshold: Neighboring score for initial words. Allowed values: Positive integer (BLASTP default is 11). Does not apply to BLASTN or MegaBLAST).
:param word_size: Size of word for initial matches. Allowed values: Positive integer.
:param composition_based_statistics: Composition based statistics algorithm to use. Allowed values: One of 0, 1, 2, or 3. See comp_based_stats command line option in the BLAST+ user manual for details.
:param organism: an organism as in https://blast.ncbi.nlm.nih.gov/Blast.cgi?PROGRAM=blastn&PAGE_TYPE=BlastSearch&LINK_LOC=blasthome
:param others: here you can add other parameters as seen in a blast bookmarked page. Define you query in https://blast.ncbi.nlm.nih.gov/Blast.cgi?PROGRAM=blastn&PAGE_TYPE=BlastSearch&LINK_LOC=blasthome
Once your query is defined click on "Bookmark" on right upper side of the page. You can copy fragments of the URL
which define the query. Eg. For organism "Homo sapiens (taxid:9606)" you will see the string "EQ_MENU=Homo%20sapiens%20%28taxid%3A9606%29" - this is
the string you can use here in others.
:param num_threads: Number of virtual CPUs to use. Allowed values: Integer greater than zero (default is 1). Supported only on the cloud.
:param verbose: print more
:returns: BLAST search request identifier
"""
if organism:
organism=organism.replace(" ", "%20").replace("(", "%28").replace(")", "%29").replace(":", "%3A")
EQ_MENU=organism
else:
EQ_MENU=None
URL=baseURL+"/Blast.cgi?"
URL=URL+"QUERY="+str(query)+"&DATABASE="+str(database)+"&PROGRAM="+str(program)
for o,varname in zip([filter, format_type, expect, nucl_reward, nucl_penalty,\
gapcosts, matrix, hitlist_size, descriptions, alignments,\
ncbi_gi, threshold, word_size, composition_based_statistics,\
EQ_MENU, num_threads],\
['FILTER' , 'FORMAT_TYPE', 'EXPECT', 'NUCL_REWARD', 'NUCL_PENALTY',\
'GAPCOSTS', 'MATRIX', 'HITLIST_SIZE', 'DESCRIPTIONS', 'ALIGNMENTS',\
'NCBI_GI', 'THRESHOLD', 'WORD_SIZE', 'COMPOSITION_BASED_STATISTICS',\
'EQ_MENU', 'NUM_THREADS']):
if o:
URL=URL+"&"+ varname +"="+str(o)
if others:
URL=URL+"&"+others
URL=URL+"&CMD=Put"
if verbose:
print(URL)
sys.stdout.flush()
response=requests.get(url = URL)
r=response.content.split("\n")
RID=[ s for s in r if "RID = " in s ]
if len(RID) > 0:
RID=RID[0].split(" ")[-1]
else:
print("Could not return an RID for this query.")
RID=None
return RID |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:run_star; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:job; 5, identifier:fastqs; 6, identifier:univ_options; 7, identifier:star_options; 8, block; 8, 9; 8, 11; 8, 19; 8, 27; 8, 46; 8, 58; 8, 59; 8, 71; 8, 102; 8, 103; 8, 115; 8, 116; 8, 130; 8, 146; 8, 168; 8, 169; 8, 170; 8, 231; 8, 243; 8, 298; 8, 304; 8, 331; 8, 344; 8, 357; 8, 370; 9, expression_statement; 9, 10; 10, comment; 11, assert_statement; 11, 12; 12, comparison_operator:in; 12, 13; 12, 16; 13, subscript; 13, 14; 13, 15; 14, identifier:star_options; 15, string:'type'; 16, tuple; 16, 17; 16, 18; 17, string:'star'; 18, string:'starlong'; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:work_dir; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:os; 25, identifier:getcwd; 26, argument_list; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:input_files; 30, dictionary; 30, 31; 30, 36; 30, 41; 31, pair; 31, 32; 31, 33; 32, string:'rna_cutadapt_1.fastq'; 33, subscript; 33, 34; 33, 35; 34, identifier:fastqs; 35, integer:0; 36, pair; 36, 37; 36, 38; 37, string:'rna_cutadapt_2.fastq'; 38, subscript; 38, 39; 38, 40; 39, identifier:fastqs; 40, integer:1; 41, pair; 41, 42; 41, 43; 42, string:'star_index.tar.gz'; 43, subscript; 43, 44; 43, 45; 44, identifier:star_options; 45, string:'index'; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:input_files; 49, call; 49, 50; 49, 51; 50, identifier:get_files_from_filestore; 51, argument_list; 51, 52; 51, 53; 51, 54; 51, 55; 52, identifier:job; 53, identifier:input_files; 54, identifier:work_dir; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:docker; 57, False; 58, comment; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:gz; 62, conditional_expression:if; 62, 63; 62, 64; 62, 70; 63, string:'.gz'; 64, call; 64, 65; 64, 66; 65, identifier:is_gzipfile; 66, argument_list; 66, 67; 67, subscript; 67, 68; 67, 69; 68, identifier:input_files; 69, string:'rna_cutadapt_1.fastq'; 70, string:''; 71, if_statement; 71, 72; 71, 73; 72, identifier:gz; 73, block; 73, 74; 74, for_statement; 74, 75; 74, 76; 74, 79; 75, identifier:read_file; 76, expression_list; 76, 77; 76, 78; 77, string:'rna_cutadapt_1.fastq'; 78, string:'rna_cutadapt_2.fastq'; 79, block; 79, 80; 79, 90; 80, expression_statement; 80, 81; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:os; 84, identifier:symlink; 85, argument_list; 85, 86; 85, 87; 86, identifier:read_file; 87, binary_operator:+; 87, 88; 87, 89; 88, identifier:read_file; 89, identifier:gz; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 97; 92, subscript; 92, 93; 92, 94; 93, identifier:input_files; 94, binary_operator:+; 94, 95; 94, 96; 95, identifier:read_file; 96, identifier:gz; 97, binary_operator:+; 97, 98; 97, 101; 98, subscript; 98, 99; 98, 100; 99, identifier:input_files; 100, identifier:read_file; 101, identifier:gz; 102, comment; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 108; 105, subscript; 105, 106; 105, 107; 106, identifier:input_files; 107, string:'star_index'; 108, call; 108, 109; 108, 110; 109, identifier:untargz; 110, argument_list; 110, 111; 110, 114; 111, subscript; 111, 112; 111, 113; 112, identifier:input_files; 113, string:'star_index.tar.gz'; 114, identifier:work_dir; 115, comment; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:star_fusion_idx; 119, call; 119, 120; 119, 125; 120, attribute; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:os; 123, identifier:path; 124, identifier:join; 125, argument_list; 125, 126; 125, 129; 126, subscript; 126, 127; 126, 128; 127, identifier:input_files; 128, string:'star_index'; 129, string:'ref_genome.fa.star.idx'; 130, if_statement; 130, 131; 130, 139; 131, call; 131, 132; 131, 137; 132, attribute; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:os; 135, identifier:path; 136, identifier:exists; 137, argument_list; 137, 138; 138, identifier:star_fusion_idx; 139, block; 139, 140; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 145; 142, subscript; 142, 143; 142, 144; 143, identifier:input_files; 144, string:'star_index'; 145, identifier:star_fusion_idx; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:input_files; 149, dictionary_comprehension; 149, 150; 149, 159; 150, pair; 150, 151; 150, 152; 151, identifier:key; 152, call; 152, 153; 152, 154; 153, identifier:docker_path; 154, argument_list; 154, 155; 154, 156; 155, identifier:path; 156, keyword_argument; 156, 157; 156, 158; 157, identifier:work_dir; 158, identifier:work_dir; 159, for_in_clause; 159, 160; 159, 163; 160, pattern_list; 160, 161; 160, 162; 161, identifier:key; 162, identifier:path; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:input_files; 166, identifier:items; 167, argument_list; 168, comment; 169, comment; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:parameters; 173, list:['--runThreadN', str(star_options['n']),
'--genomeDir', input_files['star_index'],
'--twopassMode', 'Basic',
'--outReadsUnmapped', 'None',
'--chimSegmentMin', '12',
'--chimJunctionOverhangMin', '12',
'--alignSJDBoverhangMin', '10',
'--alignMatesGapMax', '200000',
'--alignIntronMax', '200000',
'--chimSegmentReadGapMax', 'parameter', '3',
'--alignSJstitchMismatchNmax', '5', '-1', '5', '5',
'--outFileNamePrefix', 'rna',
'--readFilesIn',
input_files['rna_cutadapt_1.fastq' + gz],
input_files['rna_cutadapt_2.fastq' + gz],
'--outSAMattributes', 'NH', 'HI', 'AS', 'NM', 'MD',
'--outSAMtype', 'BAM', 'Unsorted',
'--quantMode', 'TranscriptomeSAM']; 173, 174; 173, 175; 173, 181; 173, 182; 173, 185; 173, 186; 173, 187; 173, 188; 173, 189; 173, 190; 173, 191; 173, 192; 173, 193; 173, 194; 173, 195; 173, 196; 173, 197; 173, 198; 173, 199; 173, 200; 173, 201; 173, 202; 173, 203; 173, 204; 173, 205; 173, 206; 173, 207; 173, 208; 173, 209; 173, 210; 173, 215; 173, 220; 173, 221; 173, 222; 173, 223; 173, 224; 173, 225; 173, 226; 173, 227; 173, 228; 173, 229; 173, 230; 174, string:'--runThreadN'; 175, call; 175, 176; 175, 177; 176, identifier:str; 177, argument_list; 177, 178; 178, subscript; 178, 179; 178, 180; 179, identifier:star_options; 180, string:'n'; 181, string:'--genomeDir'; 182, subscript; 182, 183; 182, 184; 183, identifier:input_files; 184, string:'star_index'; 185, string:'--twopassMode'; 186, string:'Basic'; 187, string:'--outReadsUnmapped'; 188, string:'None'; 189, string:'--chimSegmentMin'; 190, string:'12'; 191, string:'--chimJunctionOverhangMin'; 192, string:'12'; 193, string:'--alignSJDBoverhangMin'; 194, string:'10'; 195, string:'--alignMatesGapMax'; 196, string:'200000'; 197, string:'--alignIntronMax'; 198, string:'200000'; 199, string:'--chimSegmentReadGapMax'; 200, string:'parameter'; 201, string:'3'; 202, string:'--alignSJstitchMismatchNmax'; 203, string:'5'; 204, string:'-1'; 205, string:'5'; 206, string:'5'; 207, string:'--outFileNamePrefix'; 208, string:'rna'; 209, string:'--readFilesIn'; 210, subscript; 210, 211; 210, 212; 211, identifier:input_files; 212, binary_operator:+; 212, 213; 212, 214; 213, string:'rna_cutadapt_1.fastq'; 214, identifier:gz; 215, subscript; 215, 216; 215, 217; 216, identifier:input_files; 217, binary_operator:+; 217, 218; 217, 219; 218, string:'rna_cutadapt_2.fastq'; 219, identifier:gz; 220, string:'--outSAMattributes'; 221, string:'NH'; 222, string:'HI'; 223, string:'AS'; 224, string:'NM'; 225, string:'MD'; 226, string:'--outSAMtype'; 227, string:'BAM'; 228, string:'Unsorted'; 229, string:'--quantMode'; 230, string:'TranscriptomeSAM'; 231, if_statement; 231, 232; 231, 233; 232, identifier:gz; 233, block; 233, 234; 234, expression_statement; 234, 235; 235, call; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:parameters; 238, identifier:extend; 239, argument_list; 239, 240; 240, list:['--readFilesCommand', 'zcat']; 240, 241; 240, 242; 241, string:'--readFilesCommand'; 242, string:'zcat'; 243, if_statement; 243, 244; 243, 249; 243, 273; 244, comparison_operator:==; 244, 245; 244, 248; 245, subscript; 245, 246; 245, 247; 246, identifier:star_options; 247, string:'type'; 248, string:'star'; 249, block; 249, 250; 250, expression_statement; 250, 251; 251, call; 251, 252; 251, 253; 252, identifier:docker_call; 253, argument_list; 253, 254; 253, 257; 253, 260; 253, 263; 253, 268; 254, keyword_argument; 254, 255; 254, 256; 255, identifier:tool; 256, string:'star'; 257, keyword_argument; 257, 258; 257, 259; 258, identifier:tool_parameters; 259, identifier:parameters; 260, keyword_argument; 260, 261; 260, 262; 261, identifier:work_dir; 262, identifier:work_dir; 263, keyword_argument; 263, 264; 263, 265; 264, identifier:dockerhub; 265, subscript; 265, 266; 265, 267; 266, identifier:univ_options; 267, string:'dockerhub'; 268, keyword_argument; 268, 269; 268, 270; 269, identifier:tool_version; 270, subscript; 270, 271; 270, 272; 271, identifier:star_options; 272, string:'version'; 273, else_clause; 273, 274; 274, block; 274, 275; 275, expression_statement; 275, 276; 276, call; 276, 277; 276, 278; 277, identifier:docker_call; 278, argument_list; 278, 279; 278, 282; 278, 285; 278, 288; 278, 293; 279, keyword_argument; 279, 280; 279, 281; 280, identifier:tool; 281, string:'starlong'; 282, keyword_argument; 282, 283; 282, 284; 283, identifier:tool_parameters; 284, identifier:parameters; 285, keyword_argument; 285, 286; 285, 287; 286, identifier:work_dir; 287, identifier:work_dir; 288, keyword_argument; 288, 289; 288, 290; 289, identifier:dockerhub; 290, subscript; 290, 291; 290, 292; 291, identifier:univ_options; 292, string:'dockerhub'; 293, keyword_argument; 293, 294; 293, 295; 294, identifier:tool_version; 295, subscript; 295, 296; 295, 297; 296, identifier:star_options; 297, string:'version'; 298, expression_statement; 298, 299; 299, assignment; 299, 300; 299, 301; 300, identifier:output_files; 301, call; 301, 302; 301, 303; 302, identifier:defaultdict; 303, argument_list; 304, for_statement; 304, 305; 304, 306; 304, 310; 305, identifier:output_file; 306, list:['rnaAligned.toTranscriptome.out.bam',
'rnaAligned.out.bam',
'rnaChimeric.out.junction']; 306, 307; 306, 308; 306, 309; 307, string:'rnaAligned.toTranscriptome.out.bam'; 308, string:'rnaAligned.out.bam'; 309, string:'rnaChimeric.out.junction'; 310, block; 310, 311; 311, expression_statement; 311, 312; 312, assignment; 312, 313; 312, 316; 313, subscript; 313, 314; 313, 315; 314, identifier:output_files; 315, identifier:output_file; 316, call; 316, 317; 316, 322; 317, attribute; 317, 318; 317, 321; 318, attribute; 318, 319; 318, 320; 319, identifier:job; 320, identifier:fileStore; 321, identifier:writeGlobalFile; 322, argument_list; 322, 323; 323, call; 323, 324; 323, 327; 324, attribute; 324, 325; 324, 326; 325, string:'/'; 326, identifier:join; 327, argument_list; 327, 328; 328, list:[work_dir, output_file]; 328, 329; 328, 330; 329, identifier:work_dir; 330, identifier:output_file; 331, expression_statement; 331, 332; 332, call; 332, 333; 332, 334; 333, identifier:export_results; 334, argument_list; 334, 335; 334, 336; 334, 339; 334, 340; 334, 341; 335, identifier:job; 336, subscript; 336, 337; 336, 338; 337, identifier:output_files; 338, string:'rnaAligned.toTranscriptome.out.bam'; 339, string:'rna_transcriptome.bam'; 340, identifier:univ_options; 341, keyword_argument; 341, 342; 341, 343; 342, identifier:subfolder; 343, string:'alignments'; 344, expression_statement; 344, 345; 345, call; 345, 346; 345, 347; 346, identifier:export_results; 347, argument_list; 347, 348; 347, 349; 347, 352; 347, 353; 347, 354; 348, identifier:job; 349, subscript; 349, 350; 349, 351; 350, identifier:output_files; 351, string:'rnaChimeric.out.junction'; 352, string:'rna_chimeric.junction'; 353, identifier:univ_options; 354, keyword_argument; 354, 355; 354, 356; 355, identifier:subfolder; 356, string:'mutations/fusions'; 357, expression_statement; 357, 358; 358, call; 358, 359; 358, 364; 359, attribute; 359, 360; 359, 363; 360, attribute; 360, 361; 360, 362; 361, identifier:job; 362, identifier:fileStore; 363, identifier:logToMaster; 364, argument_list; 364, 365; 365, binary_operator:%; 365, 366; 365, 367; 366, string:'Ran STAR on %s successfully'; 367, subscript; 367, 368; 367, 369; 368, identifier:univ_options; 369, string:'patient'; 370, return_statement; 370, 371; 371, identifier:output_files | def run_star(job, fastqs, univ_options, star_options):
"""
Align a pair of fastqs with STAR.
:param list fastqs: The input fastqs for alignment
:param dict univ_options: Dict of universal options used by almost all tools
:param dict star_options: Options specific to star
:return: Dict containing output genome bam, genome bai, and transcriptome bam
output_files:
|- 'rnaAligned.toTranscriptome.out.bam': fsID
+- 'rnaAligned.out.bam': fsID
+- 'rnaChimeric.out.junction': fsID
:rtype: dict
"""
assert star_options['type'] in ('star', 'starlong')
work_dir = os.getcwd()
input_files = {
'rna_cutadapt_1.fastq': fastqs[0],
'rna_cutadapt_2.fastq': fastqs[1],
'star_index.tar.gz': star_options['index']}
input_files = get_files_from_filestore(job, input_files, work_dir,
docker=False)
# Handle gzipped file
gz = '.gz' if is_gzipfile(input_files['rna_cutadapt_1.fastq']) else ''
if gz:
for read_file in 'rna_cutadapt_1.fastq', 'rna_cutadapt_2.fastq':
os.symlink(read_file, read_file + gz)
input_files[read_file + gz] = input_files[read_file] + gz
# Untar the index
input_files['star_index'] = untargz(input_files['star_index.tar.gz'], work_dir)
# Check to see if user is using a STAR-Fusion index
star_fusion_idx = os.path.join(input_files['star_index'], 'ref_genome.fa.star.idx')
if os.path.exists(star_fusion_idx):
input_files['star_index'] = star_fusion_idx
input_files = {key: docker_path(path, work_dir=work_dir) for key, path in input_files.items()}
# Using recommended STAR-Fusion parameters:
# https://github.com/STAR-Fusion/STAR-Fusion/wiki
parameters = ['--runThreadN', str(star_options['n']),
'--genomeDir', input_files['star_index'],
'--twopassMode', 'Basic',
'--outReadsUnmapped', 'None',
'--chimSegmentMin', '12',
'--chimJunctionOverhangMin', '12',
'--alignSJDBoverhangMin', '10',
'--alignMatesGapMax', '200000',
'--alignIntronMax', '200000',
'--chimSegmentReadGapMax', 'parameter', '3',
'--alignSJstitchMismatchNmax', '5', '-1', '5', '5',
'--outFileNamePrefix', 'rna',
'--readFilesIn',
input_files['rna_cutadapt_1.fastq' + gz],
input_files['rna_cutadapt_2.fastq' + gz],
'--outSAMattributes', 'NH', 'HI', 'AS', 'NM', 'MD',
'--outSAMtype', 'BAM', 'Unsorted',
'--quantMode', 'TranscriptomeSAM']
if gz:
parameters.extend(['--readFilesCommand', 'zcat'])
if star_options['type'] == 'star':
docker_call(tool='star', tool_parameters=parameters, work_dir=work_dir,
dockerhub=univ_options['dockerhub'], tool_version=star_options['version'])
else:
docker_call(tool='starlong', tool_parameters=parameters, work_dir=work_dir,
dockerhub=univ_options['dockerhub'], tool_version=star_options['version'])
output_files = defaultdict()
for output_file in ['rnaAligned.toTranscriptome.out.bam',
'rnaAligned.out.bam',
'rnaChimeric.out.junction']:
output_files[output_file] = job.fileStore.writeGlobalFile('/'.join([work_dir, output_file]))
export_results(job, output_files['rnaAligned.toTranscriptome.out.bam'], 'rna_transcriptome.bam',
univ_options, subfolder='alignments')
export_results(job, output_files['rnaChimeric.out.junction'], 'rna_chimeric.junction',
univ_options, subfolder='mutations/fusions')
job.fileStore.logToMaster('Ran STAR on %s successfully' % univ_options['patient'])
return output_files |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:sort_and_index_star; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:job; 5, identifier:star_bams; 6, identifier:univ_options; 7, identifier:star_options; 8, block; 8, 9; 8, 11; 8, 21; 8, 49; 8, 84; 8, 91; 8, 98; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 18; 13, subscript; 13, 14; 13, 17; 14, subscript; 14, 15; 14, 16; 15, identifier:star_options; 16, string:'samtools'; 17, string:'n'; 18, subscript; 18, 19; 18, 20; 19, identifier:star_options; 20, string:'n'; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:sort; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:job; 27, identifier:wrapJobFn; 28, argument_list; 28, 29; 28, 30; 28, 33; 28, 34; 28, 35; 28, 40; 29, identifier:sort_bamfile; 30, subscript; 30, 31; 30, 32; 31, identifier:star_bams; 32, string:'rnaAligned.out.bam'; 33, string:'rna'; 34, identifier:univ_options; 35, keyword_argument; 35, 36; 35, 37; 36, identifier:samtools_options; 37, subscript; 37, 38; 37, 39; 38, identifier:star_options; 39, string:'samtools'; 40, keyword_argument; 40, 41; 40, 42; 41, identifier:disk; 42, call; 42, 43; 42, 44; 43, identifier:PromisedRequirement; 44, argument_list; 44, 45; 44, 46; 45, identifier:sort_disk; 46, subscript; 46, 47; 46, 48; 47, identifier:star_bams; 48, string:'rnaAligned.out.bam'; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:index; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:job; 55, identifier:wrapJobFn; 56, argument_list; 56, 57; 56, 58; 56, 63; 56, 64; 56, 65; 56, 70; 56, 73; 57, identifier:index_bamfile; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:sort; 61, identifier:rv; 62, argument_list; 63, string:'rna'; 64, identifier:univ_options; 65, keyword_argument; 65, 66; 65, 67; 66, identifier:samtools_options; 67, subscript; 67, 68; 67, 69; 68, identifier:star_options; 69, string:'samtools'; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:sample_info; 72, string:'genome_sorted'; 73, keyword_argument; 73, 74; 73, 75; 74, identifier:disk; 75, call; 75, 76; 75, 77; 76, identifier:PromisedRequirement; 77, argument_list; 77, 78; 77, 79; 78, identifier:index_disk; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:sort; 82, identifier:rv; 83, argument_list; 84, expression_statement; 84, 85; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:job; 88, identifier:addChild; 89, argument_list; 89, 90; 90, identifier:sort; 91, expression_statement; 91, 92; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:sort; 95, identifier:addChild; 96, argument_list; 96, 97; 97, identifier:index; 98, return_statement; 98, 99; 99, dictionary; 99, 100; 99, 107; 99, 112; 100, pair; 100, 101; 100, 102; 101, string:'rna_genome'; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:index; 105, identifier:rv; 106, argument_list; 107, pair; 107, 108; 107, 109; 108, string:'rna_transcriptome.bam'; 109, subscript; 109, 110; 109, 111; 110, identifier:star_bams; 111, string:'rnaAligned.toTranscriptome.out.bam'; 112, pair; 112, 113; 112, 114; 113, string:'rnaChimeric.out.junction'; 114, subscript; 114, 115; 114, 116; 115, identifier:star_bams; 116, string:'rnaChimeric.out.junction' | def sort_and_index_star(job, star_bams, univ_options, star_options):
"""
A wrapper for sorting and indexing the genomic star bam generated by run_star. It is required
since run_star returns a dict of 2 bams
:param dict star_bams: The bams from run_star
:param dict univ_options: Dict of universal options used by almost all tools
:param dict star_options: Options specific to star
:return: Dict containing input bam and the generated index (.bam.bai)
output_files:
|- 'rna_transcriptome.bam': fsID
+- 'rna_genome':
|- 'rna_sorted.bam': fsID
+- 'rna_sorted.bam.bai': fsID
+- 'rnaChimeric.out.junction': fsID
:rtype: dict
"""
star_options['samtools']['n'] = star_options['n']
sort = job.wrapJobFn(sort_bamfile, star_bams['rnaAligned.out.bam'], 'rna', univ_options,
samtools_options=star_options['samtools'],
disk=PromisedRequirement(sort_disk, star_bams['rnaAligned.out.bam']))
index = job.wrapJobFn(index_bamfile, sort.rv(), 'rna', univ_options,
samtools_options=star_options['samtools'], sample_info='genome_sorted',
disk=PromisedRequirement(index_disk, sort.rv()))
job.addChild(sort)
sort.addChild(index)
return {'rna_genome': index.rv(),
'rna_transcriptome.bam': star_bams['rnaAligned.toTranscriptome.out.bam'],
'rnaChimeric.out.junction': star_bams['rnaChimeric.out.junction']} |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:evaluate; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 10; 5, 11; 5, 12; 5, 13; 5, 17; 5, 21; 5, 74; 5, 75; 5, 92; 5, 93; 5, 97; 5, 204; 5, 217; 5, 225; 5, 226; 5, 243; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, comment; 10, comment; 11, comment; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:ops; 16, list:[]; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:rpn; 20, list:[]; 21, for_statement; 21, 22; 21, 23; 21, 26; 22, identifier:token; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:expr; 26, block; 26, 27; 27, if_statement; 27, 28; 27, 33; 27, 65; 28, call; 28, 29; 28, 30; 29, identifier:isinstance; 30, argument_list; 30, 31; 30, 32; 31, identifier:token; 32, identifier:int; 33, block; 33, 34; 33, 58; 34, while_statement; 34, 35; 34, 46; 35, boolean_operator:and; 35, 36; 35, 40; 36, call; 36, 37; 36, 38; 37, identifier:len; 38, argument_list; 38, 39; 39, identifier:ops; 40, comparison_operator:<=; 40, 41; 40, 42; 41, identifier:token; 42, subscript; 42, 43; 42, 44; 43, identifier:ops; 44, unary_operator:-; 44, 45; 45, integer:1; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:rpn; 51, identifier:append; 52, argument_list; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:ops; 56, identifier:pop; 57, argument_list; 58, expression_statement; 58, 59; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:ops; 62, identifier:append; 63, argument_list; 63, 64; 64, identifier:token; 65, else_clause; 65, 66; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:rpn; 71, identifier:append; 72, argument_list; 72, 73; 73, identifier:token; 74, comment; 75, while_statement; 75, 76; 75, 80; 76, call; 76, 77; 76, 78; 77, identifier:len; 78, argument_list; 78, 79; 79, identifier:ops; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:rpn; 85, identifier:append; 86, argument_list; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:ops; 90, identifier:pop; 91, argument_list; 92, comment; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:stack; 96, list:[]; 97, for_statement; 97, 98; 97, 99; 97, 100; 98, identifier:token; 99, identifier:rpn; 100, block; 100, 101; 101, if_statement; 101, 102; 101, 107; 101, 108; 101, 195; 102, call; 102, 103; 102, 104; 103, identifier:isinstance; 104, argument_list; 104, 105; 104, 106; 105, identifier:token; 106, identifier:int; 107, comment; 108, block; 108, 109; 108, 130; 108, 131; 108, 144; 108, 145; 108, 185; 109, if_statement; 109, 110; 109, 115; 110, comparison_operator:==; 110, 111; 110, 112; 111, identifier:token; 112, attribute; 112, 113; 112, 114; 113, identifier:OPERATOR; 114, identifier:NOT; 115, block; 115, 116; 115, 129; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 122; 118, subscript; 118, 119; 118, 120; 119, identifier:stack; 120, unary_operator:-; 120, 121; 121, integer:1; 122, call; 122, 123; 122, 124; 123, identifier:IsNot; 124, argument_list; 124, 125; 125, subscript; 125, 126; 125, 127; 126, identifier:stack; 127, unary_operator:-; 127, 128; 128, integer:1; 129, continue_statement; 130, comment; 131, if_statement; 131, 132; 131, 138; 132, comparison_operator:<; 132, 133; 132, 137; 133, call; 133, 134; 133, 135; 134, identifier:len; 135, argument_list; 135, 136; 136, identifier:stack; 137, integer:2; 138, block; 138, 139; 139, raise_statement; 139, 140; 140, call; 140, 141; 140, 142; 141, identifier:RuntimeError; 142, argument_list; 142, 143; 143, string:'Unable to build a valid expression. Not enough operands available.'; 144, comment; 145, if_statement; 145, 146; 145, 151; 145, 167; 146, comparison_operator:==; 146, 147; 146, 148; 147, identifier:token; 148, attribute; 148, 149; 148, 150; 149, identifier:OPERATOR; 150, identifier:OR; 151, block; 151, 152; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:matcher; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:hc; 158, identifier:any_of; 159, argument_list; 159, 160; 160, list_splat; 160, 161; 161, subscript; 161, 162; 161, 163; 162, identifier:stack; 163, slice; 163, 164; 163, 166; 164, unary_operator:-; 164, 165; 165, integer:2; 166, colon; 167, else_clause; 167, 168; 167, 169; 168, comment; 169, block; 169, 170; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:matcher; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:hc; 176, identifier:all_of; 177, argument_list; 177, 178; 178, list_splat; 178, 179; 179, subscript; 179, 180; 179, 181; 180, identifier:stack; 181, slice; 181, 182; 181, 184; 182, unary_operator:-; 182, 183; 183, integer:2; 184, colon; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 193; 187, subscript; 187, 188; 187, 189; 188, identifier:stack; 189, slice; 189, 190; 189, 192; 190, unary_operator:-; 190, 191; 191, integer:2; 192, colon; 193, list:[matcher]; 193, 194; 194, identifier:matcher; 195, else_clause; 195, 196; 196, block; 196, 197; 197, expression_statement; 197, 198; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:stack; 201, identifier:append; 202, argument_list; 202, 203; 203, identifier:token; 204, if_statement; 204, 205; 204, 211; 205, comparison_operator:!=; 205, 206; 205, 210; 206, call; 206, 207; 206, 208; 207, identifier:len; 208, argument_list; 208, 209; 209, identifier:stack; 210, integer:1; 211, block; 211, 212; 212, raise_statement; 212, 213; 213, call; 213, 214; 213, 215; 214, identifier:RuntimeError; 215, argument_list; 215, 216; 216, string:'Unable to build a valid expression. The RPN stack should have just one item.'; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:matcher; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:stack; 223, identifier:pop; 224, argument_list; 225, comment; 226, if_statement; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:self; 229, identifier:description; 230, block; 230, 231; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:matcher; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:hc; 237, identifier:described_as; 238, argument_list; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:self; 241, identifier:description; 242, identifier:matcher; 243, return_statement; 243, 244; 244, identifier:matcher | def evaluate(self):
""" Converts the current expression into a single matcher, applying
coordination operators to operands according to their binding rules
"""
# Apply Shunting Yard algorithm to convert the infix expression
# into Reverse Polish Notation. Since we have a very limited
# set of operators and binding rules, the implementation becomes
# really simple. The expression is formed of hamcrest matcher instances
# and operators identifiers (ints).
ops = []
rpn = []
for token in self.expr:
if isinstance(token, int):
while len(ops) and token <= ops[-1]:
rpn.append(ops.pop())
ops.append(token)
else:
rpn.append(token)
# Append the remaining operators
while len(ops):
rpn.append(ops.pop())
# Walk the RPN expression to create AllOf/AnyOf matchers
stack = []
for token in rpn:
if isinstance(token, int):
# Handle the NOT case in a special way since it's unary
if token == OPERATOR.NOT:
stack[-1] = IsNot(stack[-1])
continue
# Our operators always need two operands
if len(stack) < 2:
raise RuntimeError('Unable to build a valid expression. Not enough operands available.')
# Check what kind of matcher we need to create
if token == OPERATOR.OR:
matcher = hc.any_of(*stack[-2:])
else: # AND, BUT
matcher = hc.all_of(*stack[-2:])
stack[-2:] = [matcher]
else:
stack.append(token)
if len(stack) != 1:
raise RuntimeError('Unable to build a valid expression. The RPN stack should have just one item.')
matcher = stack.pop()
# If a description has been given include it in the matcher
if self.description:
matcher = hc.described_as(self.description, matcher)
return matcher |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_ensure_patient_group_is_ok; 3, parameters; 3, 4; 3, 5; 4, identifier:patient_object; 5, default_parameter; 5, 6; 5, 7; 6, identifier:patient_name; 7, None; 8, block; 8, 9; 8, 11; 8, 18; 8, 31; 8, 32; 8, 39; 8, 73; 9, expression_statement; 9, 10; 10, comment; 11, import_from_statement; 11, 12; 11, 16; 12, dotted_name; 12, 13; 12, 14; 12, 15; 13, identifier:protect; 14, identifier:addons; 15, identifier:common; 16, dotted_name; 16, 17; 17, identifier:TCGAToGTEx; 18, assert_statement; 18, 19; 18, 26; 19, call; 19, 20; 19, 21; 20, identifier:isinstance; 21, argument_list; 21, 22; 21, 23; 22, identifier:patient_object; 23, tuple; 23, 24; 23, 25; 24, identifier:set; 25, identifier:dict; 26, binary_operator:%; 26, 27; 26, 28; 27, string:'%s,%s'; 28, tuple; 28, 29; 28, 30; 29, identifier:patient_object; 30, identifier:patient_name; 31, comment; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:test_set; 35, call; 35, 36; 35, 37; 36, identifier:set; 37, argument_list; 37, 38; 38, identifier:patient_object; 39, if_statement; 39, 40; 39, 43; 39, 54; 40, comparison_operator:not; 40, 41; 40, 42; 41, string:'tumor_type'; 42, identifier:patient_object; 43, block; 43, 44; 44, raise_statement; 44, 45; 45, call; 45, 46; 45, 47; 46, identifier:ParameterError; 47, argument_list; 47, 48; 48, binary_operator:+; 48, 49; 48, 53; 49, parenthesized_expression; 49, 50; 50, binary_operator:%; 50, 51; 50, 52; 51, string:'The patient entry for sample %s '; 52, identifier:patient_name; 53, string:'does not contain a Tumor type.'; 54, elif_clause; 54, 55; 54, 60; 55, comparison_operator:not; 55, 56; 55, 59; 56, subscript; 56, 57; 56, 58; 57, identifier:patient_object; 58, string:'tumor_type'; 59, identifier:TCGAToGTEx; 60, block; 60, 61; 61, raise_statement; 61, 62; 62, call; 62, 63; 62, 64; 63, identifier:ParameterError; 64, argument_list; 64, 65; 65, binary_operator:+; 65, 66; 65, 70; 66, parenthesized_expression; 66, 67; 67, binary_operator:%; 67, 68; 67, 69; 68, string:'The patient entry for sample %s '; 69, identifier:patient_name; 70, concatenated_string; 70, 71; 70, 72; 71, string:'does contains an invalid Tumor type. Please use one of the '; 72, string:'valid TCGA tumor types.'; 73, if_statement; 73, 74; 73, 83; 73, 84; 73, 86; 74, call; 74, 75; 74, 81; 75, attribute; 75, 76; 75, 80; 76, set; 76, 77; 76, 78; 76, 79; 77, string:'tumor_dna_fastq_1'; 78, string:'normal_dna_fastq_1'; 79, string:'tumor_rna_fastq_1'; 80, identifier:issubset; 81, argument_list; 81, 82; 82, identifier:test_set; 83, comment; 84, block; 84, 85; 85, pass_statement; 86, else_clause; 86, 87; 86, 88; 87, comment; 88, block; 88, 89; 88, 106; 88, 107; 88, 162; 88, 163; 88, 204; 88, 205; 88, 206; 89, if_statement; 89, 90; 89, 93; 90, comparison_operator:not; 90, 91; 90, 92; 91, string:'hla_haplotype_files'; 92, identifier:test_set; 93, block; 93, 94; 94, raise_statement; 94, 95; 95, call; 95, 96; 95, 97; 96, identifier:ParameterError; 97, argument_list; 97, 98; 98, binary_operator:+; 98, 99; 98, 103; 99, parenthesized_expression; 99, 100; 100, binary_operator:%; 100, 101; 100, 102; 101, string:'The patient entry for sample %s '; 102, identifier:patient_name; 103, concatenated_string; 103, 104; 103, 105; 104, string:'does not contain a hla_haplotype_files entry.\nCannot haplotype '; 105, string:'patient if all the input sequence files are not fastqs.'; 106, comment; 107, if_statement; 107, 108; 107, 148; 108, parenthesized_expression; 108, 109; 109, boolean_operator:and; 109, 110; 109, 140; 110, parenthesized_expression; 110, 111; 111, boolean_operator:or; 111, 112; 111, 126; 112, comparison_operator:==; 112, 113; 112, 124; 113, set_comprehension; 113, 114; 113, 121; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:re; 117, identifier:search; 118, argument_list; 118, 119; 118, 120; 119, string:'tumor_dna_((bam)|(fastq_1)).*'; 120, identifier:x; 121, for_in_clause; 121, 122; 121, 123; 122, identifier:x; 123, identifier:test_set; 124, set; 124, 125; 125, None; 126, comparison_operator:==; 126, 127; 126, 138; 127, set_comprehension; 127, 128; 127, 135; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:re; 131, identifier:search; 132, argument_list; 132, 133; 132, 134; 133, string:'normal_dna_((bam)|(fastq_1)).*'; 134, identifier:x; 135, for_in_clause; 135, 136; 135, 137; 136, identifier:x; 137, identifier:test_set; 138, set; 138, 139; 139, None; 140, parenthesized_expression; 140, 141; 141, boolean_operator:and; 141, 142; 141, 145; 142, comparison_operator:not; 142, 143; 142, 144; 143, string:'mutation_vcf'; 144, identifier:test_set; 145, comparison_operator:not; 145, 146; 145, 147; 146, string:'fusion_bedpe'; 147, identifier:test_set; 148, block; 148, 149; 149, raise_statement; 149, 150; 150, call; 150, 151; 150, 152; 151, identifier:ParameterError; 152, argument_list; 152, 153; 153, binary_operator:+; 153, 154; 153, 158; 154, parenthesized_expression; 154, 155; 155, binary_operator:%; 155, 156; 155, 157; 156, string:'The patient entry for sample %s '; 157, identifier:patient_name; 158, concatenated_string; 158, 159; 158, 160; 158, 161; 159, string:'does not contain a mutation_vcf or fusion_bedpe entry. If both '; 160, string:'tumor and normal DNA sequences (fastqs or bam) are not provided, '; 161, string:'a pre-computed vcf and/or bedpe must be provided.'; 162, comment; 163, if_statement; 163, 164; 163, 178; 164, comparison_operator:==; 164, 165; 164, 176; 165, set_comprehension; 165, 166; 165, 173; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:re; 169, identifier:search; 170, argument_list; 170, 171; 170, 172; 171, string:'tumor_rna_((bam)|(fastq_1)).*'; 172, identifier:x; 173, for_in_clause; 173, 174; 173, 175; 174, identifier:x; 175, identifier:test_set; 176, set; 176, 177; 177, None; 178, block; 178, 179; 179, if_statement; 179, 180; 179, 187; 179, 188; 179, 190; 180, boolean_operator:and; 180, 181; 180, 184; 181, comparison_operator:not; 181, 182; 181, 183; 182, string:'mutation_vcf'; 183, identifier:test_set; 184, comparison_operator:in; 184, 185; 184, 186; 185, string:'fusion_bedpe'; 186, identifier:test_set; 187, comment; 188, block; 188, 189; 189, pass_statement; 190, else_clause; 190, 191; 191, block; 191, 192; 192, raise_statement; 192, 193; 193, call; 193, 194; 193, 195; 194, identifier:ParameterError; 195, argument_list; 195, 196; 196, binary_operator:+; 196, 197; 196, 201; 197, parenthesized_expression; 197, 198; 198, binary_operator:%; 198, 199; 198, 200; 199, string:'The patient entry for sample %s '; 200, identifier:patient_name; 201, concatenated_string; 201, 202; 201, 203; 202, string:'does not contain a tumor rna sequence data entry. We require '; 203, string:'either tumor_rna_fastq_1 or tumor_rna_bam.'; 204, comment; 205, comment; 206, if_statement; 206, 207; 206, 214; 207, boolean_operator:and; 207, 208; 207, 211; 208, comparison_operator:in; 208, 209; 208, 210; 209, string:'tumor_rna_bam'; 210, identifier:test_set; 211, comparison_operator:not; 211, 212; 211, 213; 212, string:'tumor_rna_transcriptome_bam'; 213, identifier:test_set; 214, block; 214, 215; 215, if_statement; 215, 216; 215, 219; 216, comparison_operator:not; 216, 217; 216, 218; 217, string:'expression_files'; 218, identifier:test_set; 219, block; 219, 220; 220, raise_statement; 220, 221; 221, call; 221, 222; 221, 223; 222, identifier:ParameterError; 223, argument_list; 223, 224; 224, parenthesized_expression; 224, 225; 225, binary_operator:+; 225, 226; 225, 229; 226, binary_operator:%; 226, 227; 226, 228; 227, string:'The patient entry for sample %s '; 228, identifier:patient_name; 229, concatenated_string; 229, 230; 229, 231; 229, 232; 229, 233; 229, 234; 230, string:'was provided a tumor rna bam with sequences mapped to the '; 231, string:'genome but was not provided a matching rna bam for the '; 232, string:'transcriptome or a tar containing expression values. '; 233, string:'We require either a matching transcriptome bam to estimate'; 234, string:'expression, or the precomputed expression values.' | def _ensure_patient_group_is_ok(patient_object, patient_name=None):
"""
Ensure that the provided entries for the patient groups is formatted properly.
:param set|dict patient_object: The values passed to the samples patient group
:param str patient_name: Optional name for the set
:raises ParameterError: If required entry doesnt exist
"""
from protect.addons.common import TCGAToGTEx
assert isinstance(patient_object, (set, dict)), '%s,%s' % (patient_object, patient_name)
# set(dict) = set of keys of the dict
test_set = set(patient_object)
if 'tumor_type' not in patient_object:
raise ParameterError(('The patient entry for sample %s ' % patient_name) +
'does not contain a Tumor type.')
elif patient_object['tumor_type'] not in TCGAToGTEx:
raise ParameterError(('The patient entry for sample %s ' % patient_name) +
'does contains an invalid Tumor type. Please use one of the '
'valid TCGA tumor types.')
if {'tumor_dna_fastq_1', 'normal_dna_fastq_1', 'tumor_rna_fastq_1'}.issubset(test_set):
# Best case scenario, we get all fastqs
pass
else:
# We have less than 3 fastqs so we have to have a haplotype.
if 'hla_haplotype_files' not in test_set:
raise ParameterError(('The patient entry for sample %s ' % patient_name) +
'does not contain a hla_haplotype_files entry.\nCannot haplotype '
'patient if all the input sequence files are not fastqs.')
# Either we have a fastq and/or bam for the tumor and normal, or we need to be given a vcf
if (({re.search('tumor_dna_((bam)|(fastq_1)).*', x) for x in test_set} == {None} or
{re.search('normal_dna_((bam)|(fastq_1)).*', x) for x in test_set} == {None}) and
('mutation_vcf' not in test_set and 'fusion_bedpe' not in test_set)):
raise ParameterError(('The patient entry for sample %s ' % patient_name) +
'does not contain a mutation_vcf or fusion_bedpe entry. If both '
'tumor and normal DNA sequences (fastqs or bam) are not provided, '
'a pre-computed vcf and/or bedpe must be provided.')
# We have to be given a tumor rna fastq or bam unless we are processing ONLY fusions
if {re.search('tumor_rna_((bam)|(fastq_1)).*', x) for x in test_set} == {None}:
if 'mutation_vcf' not in test_set and 'fusion_bedpe' in test_set:
# The only case where it is ok to not have the genome mapped rna.
pass
else:
raise ParameterError(('The patient entry for sample %s ' % patient_name) +
'does not contain a tumor rna sequence data entry. We require '
'either tumor_rna_fastq_1 or tumor_rna_bam.')
# If we are given an RNA bam then it needs to have a corresponding transcriptome bam unless
# we have also been provided expression values.
if 'tumor_rna_bam' in test_set and 'tumor_rna_transcriptome_bam' not in test_set:
if 'expression_files' not in test_set:
raise ParameterError(('The patient entry for sample %s ' % patient_name +
'was provided a tumor rna bam with sequences mapped to the '
'genome but was not provided a matching rna bam for the '
'transcriptome or a tar containing expression values. '
'We require either a matching transcriptome bam to estimate'
'expression, or the precomputed expression values.')) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_add_default_entries; 3, parameters; 3, 4; 3, 5; 4, identifier:input_dict; 5, identifier:defaults_dict; 6, block; 6, 7; 6, 9; 6, 99; 7, expression_statement; 7, 8; 8, comment; 9, for_statement; 9, 10; 9, 13; 9, 18; 10, pattern_list; 10, 11; 10, 12; 11, identifier:key; 12, identifier:value; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:defaults_dict; 16, identifier:iteritems; 17, argument_list; 18, block; 18, 19; 18, 30; 19, if_statement; 19, 20; 19, 23; 20, comparison_operator:==; 20, 21; 20, 22; 21, identifier:key; 22, string:'patients'; 23, block; 23, 24; 23, 29; 24, expression_statement; 24, 25; 25, call; 25, 26; 25, 27; 26, identifier:print; 27, argument_list; 27, 28; 28, string:'Cannot default `patients`.'; 29, continue_statement; 30, if_statement; 30, 31; 30, 36; 30, 78; 31, call; 31, 32; 31, 33; 32, identifier:isinstance; 33, argument_list; 33, 34; 33, 35; 34, identifier:value; 35, identifier:dict; 36, block; 36, 37; 37, if_statement; 37, 38; 37, 47; 37, 48; 37, 49; 37, 56; 38, boolean_operator:or; 38, 39; 38, 42; 39, comparison_operator:not; 39, 40; 39, 41; 40, identifier:key; 41, identifier:input_dict; 42, comparison_operator:is; 42, 43; 42, 46; 43, subscript; 43, 44; 43, 45; 44, identifier:input_dict; 45, identifier:key; 46, None; 47, comment; 48, comment; 49, block; 49, 50; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 55; 52, subscript; 52, 53; 52, 54; 53, identifier:input_dict; 54, identifier:key; 55, identifier:value; 56, else_clause; 56, 57; 57, block; 57, 58; 57, 72; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:r; 61, call; 61, 62; 61, 63; 62, identifier:_add_default_entries; 63, argument_list; 63, 64; 63, 71; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:input_dict; 67, identifier:get; 68, argument_list; 68, 69; 68, 70; 69, identifier:key; 70, dictionary; 71, identifier:value; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 77; 74, subscript; 74, 75; 74, 76; 75, identifier:input_dict; 76, identifier:key; 77, identifier:r; 78, else_clause; 78, 79; 78, 80; 79, comment; 80, block; 80, 81; 81, if_statement; 81, 82; 81, 91; 81, 92; 82, boolean_operator:or; 82, 83; 82, 86; 83, comparison_operator:not; 83, 84; 83, 85; 84, identifier:key; 85, identifier:input_dict; 86, comparison_operator:is; 86, 87; 86, 90; 87, subscript; 87, 88; 87, 89; 88, identifier:input_dict; 89, identifier:key; 90, None; 91, comment; 92, block; 92, 93; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 98; 95, subscript; 95, 96; 95, 97; 96, identifier:input_dict; 97, identifier:key; 98, identifier:value; 99, return_statement; 99, 100; 100, identifier:input_dict | def _add_default_entries(input_dict, defaults_dict):
"""
Add the entries in defaults dict into input_dict if they don't exist in input_dict
This is based on the accepted answer at
http://stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth
:param dict input_dict: The dict to be updated
:param dict defaults_dict: Dict containing the defaults for entries in input_dict
:return: updated dict
:rtype: dict
"""
for key, value in defaults_dict.iteritems():
if key == 'patients':
print('Cannot default `patients`.')
continue
if isinstance(value, dict):
if key not in input_dict or input_dict[key] is None:
# User didn't specify anython for the tool, but the entry was still in there so we
# just copy over the whole defaults dict
input_dict[key] = value
else:
r = _add_default_entries(input_dict.get(key, {}), value)
input_dict[key] = r
else:
# Only write if not in input_dict
if key not in input_dict or input_dict[key] is None:
# Either the user didn't have the entry, or had it without a value
input_dict[key] = value
return input_dict |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 4; 2, function_name:main; 3, parameters; 4, block; 4, 5; 4, 7; 4, 26; 4, 37; 4, 58; 4, 80; 4, 107; 4, 108; 4, 109; 4, 110; 4, 111; 4, 121; 4, 256; 5, expression_statement; 5, 6; 6, comment; 7, expression_statement; 7, 8; 8, assignment; 8, 9; 8, 10; 9, identifier:parser; 10, call; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:argparse; 13, identifier:ArgumentParser; 14, argument_list; 14, 15; 14, 18; 14, 21; 15, keyword_argument; 15, 16; 15, 17; 16, identifier:prog; 17, string:'ProTECT'; 18, keyword_argument; 18, 19; 18, 20; 19, identifier:description; 20, string:'Prediction of T-Cell Epitopes for Cancer Therapy'; 21, keyword_argument; 21, 22; 21, 23; 22, identifier:epilog; 23, concatenated_string; 23, 24; 23, 25; 24, string:'Contact Arjun Rao ([email protected]) if you encounter '; 25, string:'any problems while running ProTECT'; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:inputs; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:parser; 32, identifier:add_mutually_exclusive_group; 33, argument_list; 33, 34; 34, keyword_argument; 34, 35; 34, 36; 35, identifier:required; 36, True; 37, expression_statement; 37, 38; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:inputs; 41, identifier:add_argument; 42, argument_list; 42, 43; 42, 44; 42, 47; 42, 52; 42, 55; 43, string:'--config_file'; 44, keyword_argument; 44, 45; 44, 46; 45, identifier:dest; 46, string:'config_file'; 47, keyword_argument; 47, 48; 47, 49; 48, identifier:help; 49, concatenated_string; 49, 50; 49, 51; 50, string:'Config file to be used in the '; 51, string:'run.'; 52, keyword_argument; 52, 53; 52, 54; 53, identifier:type; 54, identifier:str; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:default; 57, None; 58, expression_statement; 58, 59; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:inputs; 62, identifier:add_argument; 63, argument_list; 63, 64; 63, 65; 63, 68; 63, 74; 63, 77; 64, string:'--generate_config'; 65, keyword_argument; 65, 66; 65, 67; 66, identifier:dest; 67, string:'generate_config'; 68, keyword_argument; 68, 69; 68, 70; 69, identifier:help; 70, concatenated_string; 70, 71; 70, 72; 70, 73; 71, string:'Generate a config file '; 72, string:'in the current directory that is pre-filled with references and flags for '; 73, string:'an hg19 run.'; 74, keyword_argument; 74, 75; 74, 76; 75, identifier:action; 76, string:'store_true'; 77, keyword_argument; 77, 78; 77, 79; 78, identifier:default; 79, False; 80, expression_statement; 80, 81; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:parser; 84, identifier:add_argument; 85, argument_list; 85, 86; 85, 87; 85, 90; 85, 98; 85, 101; 85, 104; 86, string:'--max-cores-per-job'; 87, keyword_argument; 87, 88; 87, 89; 88, identifier:dest; 89, string:'max_cores'; 90, keyword_argument; 90, 91; 90, 92; 91, identifier:help; 92, concatenated_string; 92, 93; 92, 94; 92, 95; 92, 96; 92, 97; 93, string:'Maximum cores to use per '; 94, string:'job. Aligners and Haplotypers ask for cores dependent on the machine that '; 95, string:'the launchpad gets assigned to -- In a heterogeneous cluster, this can '; 96, string:'lead to problems. This value should be set to the number of cpus on the '; 97, string:'smallest node in a cluster.'; 98, keyword_argument; 98, 99; 98, 100; 99, identifier:type; 100, identifier:int; 101, keyword_argument; 101, 102; 101, 103; 102, identifier:required; 103, False; 104, keyword_argument; 104, 105; 104, 106; 105, identifier:default; 106, None; 107, comment; 108, comment; 109, comment; 110, comment; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 116; 113, pattern_list; 113, 114; 113, 115; 114, identifier:params; 115, identifier:others; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:parser; 119, identifier:parse_known_args; 120, argument_list; 121, if_statement; 121, 122; 121, 125; 121, 130; 122, attribute; 122, 123; 122, 124; 123, identifier:params; 124, identifier:generate_config; 125, block; 125, 126; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 129; 128, identifier:generate_config_file; 129, argument_list; 130, else_clause; 130, 131; 131, block; 131, 132; 131, 141; 131, 149; 131, 164; 131, 231; 131, 246; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 139; 134, attribute; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:Job; 137, identifier:Runner; 138, identifier:addToilOptions; 139, argument_list; 139, 140; 140, identifier:parser; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:params; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:parser; 147, identifier:parse_args; 148, argument_list; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:params; 153, identifier:config_file; 154, call; 154, 155; 154, 160; 155, attribute; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:os; 158, identifier:path; 159, identifier:abspath; 160, argument_list; 160, 161; 161, attribute; 161, 162; 161, 163; 162, identifier:params; 163, identifier:config_file; 164, if_statement; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:params; 167, identifier:maxCores; 168, block; 168, 169; 169, if_statement; 169, 170; 169, 174; 169, 186; 170, not_operator; 170, 171; 171, attribute; 171, 172; 171, 173; 172, identifier:params; 173, identifier:max_cores; 174, block; 174, 175; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:params; 179, identifier:max_cores; 180, call; 180, 181; 180, 182; 181, identifier:int; 182, argument_list; 182, 183; 183, attribute; 183, 184; 183, 185; 184, identifier:params; 185, identifier:maxCores; 186, else_clause; 186, 187; 187, block; 187, 188; 188, if_statement; 188, 189; 188, 199; 189, comparison_operator:>; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:params; 192, identifier:max_cores; 193, call; 193, 194; 193, 195; 194, identifier:int; 195, argument_list; 195, 196; 196, attribute; 196, 197; 196, 198; 197, identifier:params; 198, identifier:maxCores; 199, block; 199, 200; 199, 220; 200, expression_statement; 200, 201; 201, call; 201, 202; 201, 203; 202, identifier:print; 203, argument_list; 203, 204; 203, 215; 204, binary_operator:%; 204, 205; 204, 208; 205, concatenated_string; 205, 206; 205, 207; 206, string:"The value provided to max-cores-per-job (%s) was greater than that "; 207, string:"provided to maxCores (%s). Setting max-cores-per-job = maxCores."; 208, tuple; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:params; 211, identifier:max_cores; 212, attribute; 212, 213; 212, 214; 213, identifier:params; 214, identifier:maxCores; 215, keyword_argument; 215, 216; 215, 217; 216, identifier:file; 217, attribute; 217, 218; 217, 219; 218, identifier:sys; 219, identifier:stderr; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:params; 224, identifier:max_cores; 225, call; 225, 226; 225, 227; 226, identifier:int; 227, argument_list; 227, 228; 228, attribute; 228, 229; 228, 230; 229, identifier:params; 230, identifier:maxCores; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:start; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:Job; 237, identifier:wrapJobFn; 238, argument_list; 238, 239; 238, 240; 238, 243; 239, identifier:parse_config_file; 240, attribute; 240, 241; 240, 242; 241, identifier:params; 242, identifier:config_file; 243, attribute; 243, 244; 243, 245; 244, identifier:params; 245, identifier:max_cores; 246, expression_statement; 246, 247; 247, call; 247, 248; 247, 253; 248, attribute; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:Job; 251, identifier:Runner; 252, identifier:startToil; 253, argument_list; 253, 254; 253, 255; 254, identifier:start; 255, identifier:params; 256, return_statement; 256, 257; 257, None | def main():
"""
This is the main function for ProTECT.
"""
parser = argparse.ArgumentParser(prog='ProTECT',
description='Prediction of T-Cell Epitopes for Cancer Therapy',
epilog='Contact Arjun Rao ([email protected]) if you encounter '
'any problems while running ProTECT')
inputs = parser.add_mutually_exclusive_group(required=True)
inputs.add_argument('--config_file', dest='config_file', help='Config file to be used in the '
'run.', type=str, default=None)
inputs.add_argument('--generate_config', dest='generate_config', help='Generate a config file '
'in the current directory that is pre-filled with references and flags for '
'an hg19 run.', action='store_true', default=False)
parser.add_argument('--max-cores-per-job', dest='max_cores', help='Maximum cores to use per '
'job. Aligners and Haplotypers ask for cores dependent on the machine that '
'the launchpad gets assigned to -- In a heterogeneous cluster, this can '
'lead to problems. This value should be set to the number of cpus on the '
'smallest node in a cluster.',
type=int, required=False, default=None)
# We parse the args once to see if the user has asked for a config file to be generated. In
# this case, we don't need a jobstore. To handle the case where Toil arguments are passed to
# ProTECT, we parse known args, and if the used specified config_file instead of generate_config
# we re-parse the arguments with the added Toil parser.
params, others = parser.parse_known_args()
if params.generate_config:
generate_config_file()
else:
Job.Runner.addToilOptions(parser)
params = parser.parse_args()
params.config_file = os.path.abspath(params.config_file)
if params.maxCores:
if not params.max_cores:
params.max_cores = int(params.maxCores)
else:
if params.max_cores > int(params.maxCores):
print("The value provided to max-cores-per-job (%s) was greater than that "
"provided to maxCores (%s). Setting max-cores-per-job = maxCores." %
(params.max_cores, params.maxCores), file=sys.stderr)
params.max_cores = int(params.maxCores)
start = Job.wrapJobFn(parse_config_file, params.config_file, params.max_cores)
Job.Runner.startToil(start, params)
return None |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:align_dna; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:job; 5, identifier:fastqs; 6, identifier:sample_type; 7, identifier:univ_options; 8, identifier:bwa_options; 9, block; 9, 10; 9, 12; 9, 13; 9, 14; 9, 15; 9, 48; 9, 83; 9, 84; 9, 119; 9, 154; 9, 189; 9, 227; 9, 234; 9, 241; 9, 248; 9, 255; 9, 262; 9, 269; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, comment; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:bwa; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:job; 21, identifier:wrapJobFn; 22, argument_list; 22, 23; 22, 24; 22, 25; 22, 26; 22, 27; 22, 28; 22, 38; 22, 43; 23, identifier:run_bwa; 24, identifier:fastqs; 25, identifier:sample_type; 26, identifier:univ_options; 27, identifier:bwa_options; 28, keyword_argument; 28, 29; 28, 30; 29, identifier:disk; 30, call; 30, 31; 30, 32; 31, identifier:PromisedRequirement; 32, argument_list; 32, 33; 32, 34; 32, 35; 33, identifier:bwa_disk; 34, identifier:fastqs; 35, subscript; 35, 36; 35, 37; 36, identifier:bwa_options; 37, string:'index'; 38, keyword_argument; 38, 39; 38, 40; 39, identifier:memory; 40, subscript; 40, 41; 40, 42; 41, identifier:univ_options; 42, string:'java_Xmx'; 43, keyword_argument; 43, 44; 43, 45; 44, identifier:cores; 45, subscript; 45, 46; 45, 47; 46, identifier:bwa_options; 47, string:'n'; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:sam2bam; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:job; 54, identifier:wrapJobFn; 55, argument_list; 55, 56; 55, 57; 55, 62; 55, 63; 55, 64; 55, 67; 55, 78; 56, identifier:bam_conversion; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:bwa; 60, identifier:rv; 61, argument_list; 62, identifier:sample_type; 63, identifier:univ_options; 64, subscript; 64, 65; 64, 66; 65, identifier:bwa_options; 66, string:'samtools'; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:disk; 69, call; 69, 70; 69, 71; 70, identifier:PromisedRequirement; 71, argument_list; 71, 72; 71, 73; 72, identifier:sam2bam_disk; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:bwa; 76, identifier:rv; 77, argument_list; 78, keyword_argument; 78, 79; 78, 80; 79, identifier:memory; 80, subscript; 80, 81; 80, 82; 81, identifier:univ_options; 82, string:'java_Xmx'; 83, comment; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:reheader; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:job; 90, identifier:wrapJobFn; 91, argument_list; 91, 92; 91, 93; 91, 98; 91, 99; 91, 100; 91, 103; 91, 114; 92, identifier:fix_bam_header; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:sam2bam; 96, identifier:rv; 97, argument_list; 98, identifier:sample_type; 99, identifier:univ_options; 100, subscript; 100, 101; 100, 102; 101, identifier:bwa_options; 102, string:'samtools'; 103, keyword_argument; 103, 104; 103, 105; 104, identifier:disk; 105, call; 105, 106; 105, 107; 106, identifier:PromisedRequirement; 107, argument_list; 107, 108; 107, 109; 108, identifier:sam2bam_disk; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:bwa; 112, identifier:rv; 113, argument_list; 114, keyword_argument; 114, 115; 114, 116; 115, identifier:memory; 116, subscript; 116, 117; 116, 118; 117, identifier:univ_options; 118, string:'java_Xmx'; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:regroup; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:job; 125, identifier:wrapJobFn; 126, argument_list; 126, 127; 126, 128; 126, 133; 126, 134; 126, 135; 126, 138; 126, 149; 127, identifier:add_readgroups; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:reheader; 131, identifier:rv; 132, argument_list; 133, identifier:sample_type; 134, identifier:univ_options; 135, subscript; 135, 136; 135, 137; 136, identifier:bwa_options; 137, string:'picard'; 138, keyword_argument; 138, 139; 138, 140; 139, identifier:disk; 140, call; 140, 141; 140, 142; 141, identifier:PromisedRequirement; 142, argument_list; 142, 143; 142, 144; 143, identifier:regroup_disk; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:reheader; 147, identifier:rv; 148, argument_list; 149, keyword_argument; 149, 150; 149, 151; 150, identifier:memory; 151, subscript; 151, 152; 151, 153; 152, identifier:univ_options; 153, string:'java_Xmx'; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:mkdup; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:job; 160, identifier:wrapJobFn; 161, argument_list; 161, 162; 161, 163; 161, 168; 161, 169; 161, 170; 161, 173; 161, 184; 162, identifier:mark_duplicates; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:regroup; 166, identifier:rv; 167, argument_list; 168, identifier:sample_type; 169, identifier:univ_options; 170, subscript; 170, 171; 170, 172; 171, identifier:bwa_options; 172, string:'picard'; 173, keyword_argument; 173, 174; 173, 175; 174, identifier:disk; 175, call; 175, 176; 175, 177; 176, identifier:PromisedRequirement; 177, argument_list; 177, 178; 177, 179; 178, identifier:mkdup_disk; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:regroup; 182, identifier:rv; 183, argument_list; 184, keyword_argument; 184, 185; 184, 186; 185, identifier:memory; 186, subscript; 186, 187; 186, 188; 187, identifier:univ_options; 188, string:'java_Xmx'; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:index; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:job; 195, identifier:wrapJobFn; 196, argument_list; 196, 197; 196, 198; 196, 203; 196, 204; 196, 205; 196, 208; 196, 211; 196, 222; 197, identifier:index_bamfile; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:mkdup; 201, identifier:rv; 202, argument_list; 203, identifier:sample_type; 204, identifier:univ_options; 205, subscript; 205, 206; 205, 207; 206, identifier:bwa_options; 207, string:'samtools'; 208, keyword_argument; 208, 209; 208, 210; 209, identifier:sample_info; 210, string:'fix_pg_sorted'; 211, keyword_argument; 211, 212; 211, 213; 212, identifier:disk; 213, call; 213, 214; 213, 215; 214, identifier:PromisedRequirement; 215, argument_list; 215, 216; 215, 217; 216, identifier:index_disk; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:mkdup; 220, identifier:rv; 221, argument_list; 222, keyword_argument; 222, 223; 222, 224; 223, identifier:memory; 224, subscript; 224, 225; 224, 226; 225, identifier:univ_options; 226, string:'java_Xmx'; 227, expression_statement; 227, 228; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:job; 231, identifier:addChild; 232, argument_list; 232, 233; 233, identifier:bwa; 234, expression_statement; 234, 235; 235, call; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:bwa; 238, identifier:addChild; 239, argument_list; 239, 240; 240, identifier:sam2bam; 241, expression_statement; 241, 242; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:sam2bam; 245, identifier:addChild; 246, argument_list; 246, 247; 247, identifier:reheader; 248, expression_statement; 248, 249; 249, call; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:reheader; 252, identifier:addChild; 253, argument_list; 253, 254; 254, identifier:regroup; 255, expression_statement; 255, 256; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:regroup; 259, identifier:addChild; 260, argument_list; 260, 261; 261, identifier:mkdup; 262, expression_statement; 262, 263; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:mkdup; 266, identifier:addChild; 267, argument_list; 267, 268; 268, identifier:index; 269, return_statement; 269, 270; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:index; 273, identifier:rv; 274, argument_list | def align_dna(job, fastqs, sample_type, univ_options, bwa_options):
"""
A wrapper for the entire dna alignment subgraph.
:param list fastqs: The input fastqs for alignment
:param str sample_type: Description of the sample to inject into the filename
:param dict univ_options: Dict of universal options used by almost all tools
:param dict bwa_options: Options specific to bwa
:return: Dict containing output bam and bai
output_files:
|- '<sample_type>_fix_pg_sorted.bam': fsID
+- '<sample_type>_fix_pg_sorted.bam.bai': fsID
:rtype: dict
"""
# The mkdup and regroup steps use picard that allots heap space using the Xmx key in the
# univ_options dictionary. This should reflect in the job allotment. Since We want all these
# jobs to occur on the same node, we ened to give them all the same memory requirements.
bwa = job.wrapJobFn(run_bwa, fastqs, sample_type, univ_options, bwa_options,
disk=PromisedRequirement(bwa_disk, fastqs, bwa_options['index']),
memory=univ_options['java_Xmx'],
cores=bwa_options['n'])
sam2bam = job.wrapJobFn(bam_conversion, bwa.rv(), sample_type, univ_options,
bwa_options['samtools'],
disk=PromisedRequirement(sam2bam_disk, bwa.rv()),
memory=univ_options['java_Xmx'])
# reheader takes the same disk as sam2bam so we can serialize this on the same worker.
reheader = job.wrapJobFn(fix_bam_header, sam2bam.rv(), sample_type, univ_options,
bwa_options['samtools'],
disk=PromisedRequirement(sam2bam_disk, bwa.rv()),
memory=univ_options['java_Xmx'])
regroup = job.wrapJobFn(add_readgroups, reheader.rv(), sample_type, univ_options,
bwa_options['picard'],
disk=PromisedRequirement(regroup_disk, reheader.rv()),
memory=univ_options['java_Xmx'])
mkdup = job.wrapJobFn(mark_duplicates, regroup.rv(), sample_type, univ_options,
bwa_options['picard'],
disk=PromisedRequirement(mkdup_disk, regroup.rv()),
memory=univ_options['java_Xmx'])
index = job.wrapJobFn(index_bamfile, mkdup.rv(), sample_type, univ_options,
bwa_options['samtools'], sample_info='fix_pg_sorted',
disk=PromisedRequirement(index_disk, mkdup.rv()),
memory=univ_options['java_Xmx'])
job.addChild(bwa)
bwa.addChild(sam2bam)
sam2bam.addChild(reheader)
reheader.addChild(regroup)
regroup.addChild(mkdup)
mkdup.addChild(index)
return index.rv() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:create; 3, parameters; 3, 4; 3, 5; 4, identifier:cls; 5, identifier:entry; 6, block; 6, 7; 6, 9; 6, 10; 6, 85; 6, 86; 6, 87; 6, 94; 6, 120; 6, 121; 6, 122; 6, 137; 6, 138; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, try_statement; 10, 11; 10, 12; 10, 13; 10, 14; 10, 22; 10, 50; 11, comment; 12, comment; 13, comment; 14, block; 14, 15; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:module; 18, call; 18, 19; 18, 20; 19, identifier:import_module; 20, argument_list; 20, 21; 21, identifier:entry; 22, except_clause; 22, 23; 22, 24; 22, 25; 22, 26; 23, identifier:ImportError; 24, comment; 25, comment; 26, block; 26, 27; 26, 31; 26, 43; 26, 44; 26, 45; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:module; 30, None; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 37; 33, pattern_list; 33, 34; 33, 35; 33, 36; 34, identifier:mod_path; 35, identifier:_; 36, identifier:cls_name; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:entry; 40, identifier:rpartition; 41, argument_list; 41, 42; 42, string:'.'; 43, comment; 44, comment; 45, if_statement; 45, 46; 45, 48; 46, not_operator; 46, 47; 47, identifier:mod_path; 48, block; 48, 49; 49, raise_statement; 50, else_clause; 50, 51; 51, block; 51, 52; 52, try_statement; 52, 53; 52, 54; 52, 61; 52, 71; 53, comment; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:entry; 58, attribute; 58, 59; 58, 60; 59, identifier:module; 60, identifier:default_bot; 61, except_clause; 61, 62; 61, 63; 61, 64; 62, identifier:AttributeError; 63, comment; 64, block; 64, 65; 65, return_statement; 65, 66; 66, call; 66, 67; 66, 68; 67, identifier:cls; 68, argument_list; 68, 69; 68, 70; 69, string:f'{entry}.Bot'; 70, identifier:module; 71, else_clause; 71, 72; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 79; 75, pattern_list; 75, 76; 75, 77; 75, 78; 76, identifier:mod_path; 77, identifier:_; 78, identifier:cls_name; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:entry; 82, identifier:rpartition; 83, argument_list; 83, 84; 84, string:'.'; 85, comment; 86, comment; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:mod; 90, call; 90, 91; 90, 92; 91, identifier:import_module; 92, argument_list; 92, 93; 93, identifier:mod_path; 94, try_statement; 94, 95; 94, 104; 95, block; 95, 96; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:bot_cls; 99, call; 99, 100; 99, 101; 100, identifier:getattr; 101, argument_list; 101, 102; 101, 103; 102, identifier:mod; 103, identifier:cls_name; 104, except_clause; 104, 105; 104, 106; 105, identifier:AttributeError; 106, block; 106, 107; 106, 119; 107, if_statement; 107, 108; 107, 111; 107, 112; 107, 113; 108, comparison_operator:is; 108, 109; 108, 110; 109, identifier:module; 110, None; 111, comment; 112, comment; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, call; 115, 116; 115, 117; 116, identifier:import_module; 117, argument_list; 117, 118; 118, identifier:entry; 119, raise_statement; 120, comment; 121, comment; 122, if_statement; 122, 123; 122, 129; 123, not_operator; 123, 124; 124, call; 124, 125; 124, 126; 125, identifier:issubclass; 126, argument_list; 126, 127; 126, 128; 127, identifier:bot_cls; 128, identifier:Bot; 129, block; 129, 130; 130, raise_statement; 130, 131; 131, call; 131, 132; 131, 133; 132, identifier:ImproperlyConfigured; 133, argument_list; 133, 134; 134, binary_operator:%; 134, 135; 134, 136; 135, string:"'%s' isn't a subclass of Bot."; 136, identifier:entry; 137, comment; 138, return_statement; 138, 139; 139, call; 139, 140; 139, 141; 140, identifier:cls; 141, argument_list; 141, 142; 141, 143; 141, 144; 142, identifier:entry; 143, identifier:mod; 144, attribute; 144, 145; 144, 146; 145, identifier:bot_cls; 146, identifier:label | def create(cls, entry):
"""
Factory that creates an bot config from an entry in INSTALLED_APPS.
"""
# trading_bots.example.bot.ExampleBot
try:
# If import_module succeeds, entry is a path to a bot module,
# which may specify a bot class with a default_bot attribute.
# Otherwise, entry is a path to a bot class or an error.
module = import_module(entry)
except ImportError:
# Track that importing as a bot module failed. If importing as a
# bot class fails too, we'll trigger the ImportError again.
module = None
mod_path, _, cls_name = entry.rpartition('.')
# Raise the original exception when entry cannot be a path to an
# bot config class.
if not mod_path:
raise
else:
try:
# If this works, the bot module specifies a bot class.
entry = module.default_bot
except AttributeError:
# Otherwise, it simply uses the default bot registry class.
return cls(f'{entry}.Bot', module)
else:
mod_path, _, cls_name = entry.rpartition('.')
# If we're reaching this point, we must attempt to load the bot
# class located at <mod_path>.<cls_name>
mod = import_module(mod_path)
try:
bot_cls = getattr(mod, cls_name)
except AttributeError:
if module is None:
# If importing as an bot module failed, that error probably
# contains the most informative traceback. Trigger it again.
import_module(entry)
raise
# Check for obvious errors. (This check prevents duck typing, but
# it could be removed if it became a problem in practice.)
if not issubclass(bot_cls, Bot):
raise ImproperlyConfigured(
"'%s' isn't a subclass of Bot." % entry)
# Entry is a path to an bot config class.
return cls(entry, mod, bot_cls.label) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:populate; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:installed_bots; 7, None; 8, block; 8, 9; 8, 11; 8, 17; 8, 18; 8, 19; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:self; 14, identifier:ready; 15, block; 15, 16; 16, return_statement; 17, comment; 18, comment; 19, with_statement; 19, 20; 19, 25; 20, with_clause; 20, 21; 21, with_item; 21, 22; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:_lock; 25, block; 25, 26; 25, 32; 25, 33; 25, 34; 25, 46; 25, 52; 25, 53; 25, 130; 25, 131; 25, 149; 25, 167; 25, 184; 25, 190; 25, 191; 25, 207; 25, 213; 26, if_statement; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:ready; 30, block; 30, 31; 31, return_statement; 32, comment; 33, comment; 34, if_statement; 34, 35; 34, 38; 34, 39; 34, 40; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:loading; 38, comment; 39, comment; 40, block; 40, 41; 41, raise_statement; 41, 42; 42, call; 42, 43; 42, 44; 43, identifier:RuntimeError; 44, argument_list; 44, 45; 45, string:"populate() isn't re-entrant"; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:loading; 51, True; 52, comment; 53, for_statement; 53, 54; 53, 55; 53, 58; 54, identifier:entry; 55, boolean_operator:or; 55, 56; 55, 57; 56, identifier:installed_bots; 57, dictionary; 58, block; 58, 59; 58, 85; 58, 94; 58, 114; 58, 124; 59, if_statement; 59, 60; 59, 65; 60, call; 60, 61; 60, 62; 61, identifier:isinstance; 62, argument_list; 62, 63; 62, 64; 63, identifier:entry; 64, identifier:Bot; 65, block; 65, 66; 65, 70; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:cls; 69, identifier:entry; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:entry; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, string:'.'; 76, identifier:join; 77, argument_list; 77, 78; 78, list:[cls.__module__, cls.__name__]; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:cls; 81, identifier:__module__; 82, attribute; 82, 83; 82, 84; 83, identifier:cls; 84, identifier:__name__; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:bot_reg; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:BotRegistry; 91, identifier:create; 92, argument_list; 92, 93; 93, identifier:entry; 94, if_statement; 94, 95; 94, 102; 95, comparison_operator:in; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:bot_reg; 98, identifier:label; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:bots; 102, block; 102, 103; 103, raise_statement; 103, 104; 104, call; 104, 105; 104, 106; 105, identifier:ImproperlyConfigured; 106, argument_list; 106, 107; 107, binary_operator:%; 107, 108; 107, 111; 108, concatenated_string; 108, 109; 108, 110; 109, string:"Bot labels aren't unique, "; 110, string:"duplicates: %s"; 111, attribute; 111, 112; 111, 113; 112, identifier:bot_reg; 113, identifier:label; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 123; 116, subscript; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:bots; 120, attribute; 120, 121; 120, 122; 121, identifier:bot_reg; 122, identifier:label; 123, identifier:bot_reg; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:bot_reg; 128, identifier:bots; 129, identifier:self; 130, comment; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:counts; 134, call; 134, 135; 134, 136; 135, identifier:Counter; 136, generator_expression; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:bot_reg; 139, identifier:name; 140, for_in_clause; 140, 141; 140, 142; 141, identifier:bot_reg; 142, call; 142, 143; 142, 148; 143, attribute; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:self; 146, identifier:bots; 147, identifier:values; 148, argument_list; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:duplicates; 152, list_comprehension; 152, 153; 152, 154; 152, 163; 153, identifier:name; 154, for_in_clause; 154, 155; 154, 158; 155, pattern_list; 155, 156; 155, 157; 156, identifier:name; 157, identifier:count; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:counts; 161, identifier:most_common; 162, argument_list; 163, if_clause; 163, 164; 164, comparison_operator:>; 164, 165; 164, 166; 165, identifier:count; 166, integer:1; 167, if_statement; 167, 168; 167, 169; 168, identifier:duplicates; 169, block; 169, 170; 170, raise_statement; 170, 171; 171, call; 171, 172; 171, 173; 172, identifier:ImproperlyConfigured; 173, argument_list; 173, 174; 174, binary_operator:%; 174, 175; 174, 178; 175, concatenated_string; 175, 176; 175, 177; 176, string:"Bot names aren't unique, "; 177, string:"duplicates: %s"; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, string:", "; 181, identifier:join; 182, argument_list; 182, 183; 183, identifier:duplicates; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:self; 188, identifier:bots_ready; 189, True; 190, comment; 191, for_statement; 191, 192; 191, 193; 191, 200; 192, identifier:bot; 193, call; 193, 194; 193, 199; 194, attribute; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:self; 197, identifier:bots; 198, identifier:values; 199, argument_list; 200, block; 200, 201; 201, expression_statement; 201, 202; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:bot; 205, identifier:import_configs; 206, argument_list; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:self; 211, identifier:configs_ready; 212, True; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:self; 217, identifier:ready; 218, True | def populate(self, installed_bots=None):
"""
Load bots.
Import each bot module.
It is thread-safe and idempotent, but not re-entrant.
"""
if self.ready:
return
# populate() might be called by two threads in parallel on servers
# that create threads before initializing the WSGI callable.
with self._lock:
if self.ready:
return
# An RLock prevents other threads from entering this section. The
# compare and set operation below is atomic.
if self.loading:
# Prevent re-entrant calls to avoid running AppConfig.ready()
# methods twice.
raise RuntimeError("populate() isn't re-entrant")
self.loading = True
# Phase 1: Initialize bots
for entry in installed_bots or {}:
if isinstance(entry, Bot):
cls = entry
entry = '.'.join([cls.__module__, cls.__name__])
bot_reg = BotRegistry.create(entry)
if bot_reg.label in self.bots:
raise ImproperlyConfigured(
"Bot labels aren't unique, "
"duplicates: %s" % bot_reg.label)
self.bots[bot_reg.label] = bot_reg
bot_reg.bots = self
# Check for duplicate bot names.
counts = Counter(
bot_reg.name for bot_reg in self.bots.values())
duplicates = [
name for name, count in counts.most_common() if count > 1]
if duplicates:
raise ImproperlyConfigured(
"Bot names aren't unique, "
"duplicates: %s" % ", ".join(duplicates))
self.bots_ready = True
# Phase 2: import config files
for bot in self.bots.values():
bot.import_configs()
self.configs_ready = True
self.ready = True |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 19; 2, function_name:get_initkwargs; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 17; 4, identifier:cls; 5, identifier:form_list; 6, default_parameter; 6, 7; 6, 8; 7, identifier:initial_dict; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:instance_dict; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:condition_dict; 14, None; 15, list_splat_pattern; 15, 16; 16, identifier:args; 17, dictionary_splat_pattern; 17, 18; 18, identifier:kwargs; 19, block; 19, 20; 19, 22; 19, 44; 19, 50; 19, 58; 19, 59; 19, 104; 19, 105; 19, 161; 19, 162; 19, 168; 20, expression_statement; 20, 21; 21, comment; 22, expression_statement; 22, 23; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:kwargs; 26, identifier:update; 27, argument_list; 27, 28; 28, dictionary; 28, 29; 28, 34; 28, 39; 29, pair; 29, 30; 29, 31; 30, string:'initial_dict'; 31, boolean_operator:or; 31, 32; 31, 33; 32, identifier:initial_dict; 33, dictionary; 34, pair; 34, 35; 34, 36; 35, string:'instance_dict'; 36, boolean_operator:or; 36, 37; 36, 38; 37, identifier:instance_dict; 38, dictionary; 39, pair; 39, 40; 39, 41; 40, string:'condition_dict'; 41, boolean_operator:or; 41, 42; 41, 43; 42, identifier:condition_dict; 43, dictionary; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:init_form_list; 47, call; 47, 48; 47, 49; 48, identifier:SortedDict; 49, argument_list; 50, assert_statement; 50, 51; 50, 57; 51, comparison_operator:>; 51, 52; 51, 56; 52, call; 52, 53; 52, 54; 53, identifier:len; 54, argument_list; 54, 55; 55, identifier:form_list; 56, integer:0; 57, string:'at least one form is needed'; 58, comment; 59, for_statement; 59, 60; 59, 63; 59, 67; 60, pattern_list; 60, 61; 60, 62; 61, identifier:i; 62, identifier:form; 63, call; 63, 64; 63, 65; 64, identifier:enumerate; 65, argument_list; 65, 66; 66, identifier:form_list; 67, block; 67, 68; 68, if_statement; 68, 69; 68, 76; 68, 77; 68, 78; 68, 92; 69, call; 69, 70; 69, 71; 70, identifier:isinstance; 71, argument_list; 71, 72; 71, 73; 72, identifier:form; 73, tuple; 73, 74; 73, 75; 74, identifier:list; 75, identifier:tuple; 76, comment; 77, comment; 78, block; 78, 79; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 89; 81, subscript; 81, 82; 81, 83; 82, identifier:init_form_list; 83, call; 83, 84; 83, 85; 84, identifier:unicode; 85, argument_list; 85, 86; 86, subscript; 86, 87; 86, 88; 87, identifier:form; 88, integer:0; 89, subscript; 89, 90; 89, 91; 90, identifier:form; 91, integer:1; 92, else_clause; 92, 93; 92, 94; 93, comment; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 103; 97, subscript; 97, 98; 97, 99; 98, identifier:init_form_list; 99, call; 99, 100; 99, 101; 100, identifier:unicode; 101, argument_list; 101, 102; 102, identifier:i; 103, identifier:form; 104, comment; 105, for_statement; 105, 106; 105, 107; 105, 112; 106, identifier:form; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:init_form_list; 110, identifier:itervalues; 111, argument_list; 112, block; 112, 113; 112, 130; 112, 131; 112, 132; 113, if_statement; 113, 114; 113, 121; 113, 122; 113, 123; 114, call; 114, 115; 114, 116; 115, identifier:issubclass; 116, argument_list; 116, 117; 116, 118; 117, identifier:form; 118, attribute; 118, 119; 118, 120; 119, identifier:formsets; 120, identifier:BaseFormSet; 121, comment; 122, comment; 123, block; 123, 124; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:form; 127, attribute; 127, 128; 127, 129; 128, identifier:form; 129, identifier:form; 130, comment; 131, comment; 132, for_statement; 132, 133; 132, 134; 132, 141; 133, identifier:field; 134, call; 134, 135; 134, 140; 135, attribute; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:form; 138, identifier:base_fields; 139, identifier:itervalues; 140, argument_list; 141, block; 141, 142; 142, if_statement; 142, 143; 142, 158; 143, parenthesized_expression; 143, 144; 144, boolean_operator:and; 144, 145; 144, 152; 145, call; 145, 146; 145, 147; 146, identifier:isinstance; 147, argument_list; 147, 148; 147, 149; 148, identifier:field; 149, attribute; 149, 150; 149, 151; 150, identifier:forms; 151, identifier:FileField; 152, not_operator; 152, 153; 153, call; 153, 154; 153, 155; 154, identifier:hasattr; 155, argument_list; 155, 156; 155, 157; 156, identifier:cls; 157, string:'file_storage'; 158, block; 158, 159; 159, raise_statement; 159, 160; 160, identifier:NoFileStorageConfigured; 161, comment; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 167; 164, subscript; 164, 165; 164, 166; 165, identifier:kwargs; 166, string:'form_list'; 167, identifier:init_form_list; 168, return_statement; 168, 169; 169, identifier:kwargs | def get_initkwargs(cls, form_list, initial_dict=None,
instance_dict=None, condition_dict=None, *args, **kwargs):
"""
Creates a dict with all needed parameters for the form wizard instances.
* `form_list` - is a list of forms. The list entries can be single form
classes or tuples of (`step_name`, `form_class`). If you pass a list
of forms, the formwizard will convert the class list to
(`zero_based_counter`, `form_class`). This is needed to access the
form for a specific step.
* `initial_dict` - contains a dictionary of initial data dictionaries.
The key should be equal to the `step_name` in the `form_list` (or
the str of the zero based counter - if no step_names added in the
`form_list`)
* `instance_dict` - contains a dictionary of instance objects. This list
is only used when `ModelForm`s are used. The key should be equal to
the `step_name` in the `form_list`. Same rules as for `initial_dict`
apply.
* `condition_dict` - contains a dictionary of boolean values or
callables. If the value of for a specific `step_name` is callable it
will be called with the formwizard instance as the only argument.
If the return value is true, the step's form will be used.
"""
kwargs.update({
'initial_dict': initial_dict or {},
'instance_dict': instance_dict or {},
'condition_dict': condition_dict or {},
})
init_form_list = SortedDict()
assert len(form_list) > 0, 'at least one form is needed'
# walk through the passed form list
for i, form in enumerate(form_list):
if isinstance(form, (list, tuple)):
# if the element is a tuple, add the tuple to the new created
# sorted dictionary.
init_form_list[unicode(form[0])] = form[1]
else:
# if not, add the form with a zero based counter as unicode
init_form_list[unicode(i)] = form
# walk through the ne created list of forms
for form in init_form_list.itervalues():
if issubclass(form, formsets.BaseFormSet):
# if the element is based on BaseFormSet (FormSet/ModelFormSet)
# we need to override the form variable.
form = form.form
# check if any form contains a FileField, if yes, we need a
# file_storage added to the formwizard (by subclassing).
for field in form.base_fields.itervalues():
if (isinstance(field, forms.FileField) and
not hasattr(cls, 'file_storage')):
raise NoFileStorageConfigured
# build the kwargs for the formwizard instances
kwargs['form_list'] = init_form_list
return kwargs |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:post; 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, 13; 9, 14; 9, 15; 9, 29; 9, 91; 9, 92; 9, 108; 9, 121; 9, 129; 9, 156; 9, 157; 9, 179; 9, 180; 9, 260; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, comment; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:wizard_prev_step; 18, call; 18, 19; 18, 26; 19, attribute; 19, 20; 19, 25; 20, attribute; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:request; 24, identifier:POST; 25, identifier:get; 26, argument_list; 26, 27; 26, 28; 27, string:'wizard_prev_step'; 28, None; 29, if_statement; 29, 30; 29, 39; 30, boolean_operator:and; 30, 31; 30, 32; 31, identifier:wizard_prev_step; 32, comparison_operator:in; 32, 33; 32, 34; 33, identifier:wizard_prev_step; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:get_form_list; 38, argument_list; 39, block; 39, 40; 39, 48; 39, 84; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 47; 42, attribute; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:storage; 46, identifier:current_step; 47, identifier:wizard_prev_step; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:form; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:self; 54, identifier:get_form; 55, argument_list; 55, 56; 55, 70; 56, keyword_argument; 56, 57; 56, 58; 57, identifier:data; 58, call; 58, 59; 58, 64; 59, attribute; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:self; 62, identifier:storage; 63, identifier:get_step_data; 64, argument_list; 64, 65; 65, attribute; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:steps; 69, identifier:current; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:files; 72, call; 72, 73; 72, 78; 73, attribute; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:self; 76, identifier:storage; 77, identifier:get_step_files; 78, argument_list; 78, 79; 79, attribute; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:self; 82, identifier:steps; 83, identifier:current; 84, return_statement; 84, 85; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:self; 88, identifier:render; 89, argument_list; 89, 90; 90, identifier:form; 91, comment; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:management_form; 95, call; 95, 96; 95, 97; 96, identifier:ManagementForm; 97, argument_list; 97, 98; 97, 103; 98, attribute; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:request; 102, identifier:POST; 103, keyword_argument; 103, 104; 103, 105; 104, identifier:prefix; 105, attribute; 105, 106; 105, 107; 106, identifier:self; 107, identifier:prefix; 108, if_statement; 108, 109; 108, 115; 109, not_operator; 109, 110; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:management_form; 113, identifier:is_valid; 114, argument_list; 115, block; 115, 116; 116, raise_statement; 116, 117; 117, call; 117, 118; 117, 119; 118, identifier:ValidationError; 119, argument_list; 119, 120; 120, string:'ManagementForm data is missing or has been tampered.'; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:form_current_step; 124, subscript; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:management_form; 127, identifier:cleaned_data; 128, string:'current_step'; 129, if_statement; 129, 130; 129, 146; 129, 147; 130, parenthesized_expression; 130, 131; 131, boolean_operator:and; 131, 132; 131, 139; 132, comparison_operator:!=; 132, 133; 132, 134; 133, identifier:form_current_step; 134, attribute; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:steps; 138, identifier:current; 139, comparison_operator:is; 139, 140; 139, 145; 140, attribute; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:self; 143, identifier:storage; 144, identifier:current_step; 145, None; 146, comment; 147, block; 147, 148; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 155; 150, attribute; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:self; 153, identifier:storage; 154, identifier:current_step; 155, identifier:form_current_step; 156, comment; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:form; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:self; 163, identifier:get_form; 164, argument_list; 164, 165; 164, 172; 165, keyword_argument; 165, 166; 165, 167; 166, identifier:data; 167, attribute; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:self; 170, identifier:request; 171, identifier:POST; 172, keyword_argument; 172, 173; 172, 174; 173, identifier:files; 174, attribute; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:self; 177, identifier:request; 178, identifier:FILES; 179, comment; 180, if_statement; 180, 181; 180, 186; 180, 187; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:form; 184, identifier:is_valid; 185, argument_list; 186, comment; 187, block; 187, 188; 187, 207; 187, 226; 187, 227; 188, expression_statement; 188, 189; 189, call; 189, 190; 189, 195; 190, attribute; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:self; 193, identifier:storage; 194, identifier:set_step_data; 195, argument_list; 195, 196; 195, 201; 196, attribute; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:self; 199, identifier:steps; 200, identifier:current; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:self; 204, identifier:process_step; 205, argument_list; 205, 206; 206, identifier:form; 207, expression_statement; 207, 208; 208, call; 208, 209; 208, 214; 209, attribute; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:self; 212, identifier:storage; 213, identifier:set_step_files; 214, argument_list; 214, 215; 214, 220; 215, attribute; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:self; 218, identifier:steps; 219, identifier:current; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:self; 223, identifier:process_step_files; 224, argument_list; 224, 225; 225, identifier:form; 226, comment; 227, if_statement; 227, 228; 227, 239; 227, 240; 227, 250; 228, comparison_operator:==; 228, 229; 228, 234; 229, attribute; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:self; 232, identifier:steps; 233, identifier:current; 234, attribute; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:self; 237, identifier:steps; 238, identifier:last; 239, comment; 240, block; 240, 241; 241, return_statement; 241, 242; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:self; 245, identifier:render_done; 246, argument_list; 246, 247; 246, 248; 247, identifier:form; 248, dictionary_splat; 248, 249; 249, identifier:kwargs; 250, else_clause; 250, 251; 250, 252; 251, comment; 252, block; 252, 253; 253, return_statement; 253, 254; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:self; 257, identifier:render_next_step; 258, argument_list; 258, 259; 259, identifier:form; 260, return_statement; 260, 261; 261, call; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:self; 264, identifier:render; 265, argument_list; 265, 266; 266, identifier:form | def post(self, *args, **kwargs):
"""
This method handles POST requests.
The wizard will render either the current step (if form validation
wasn't successful), the next step (if the current step was stored
successful) or the done view (if no more steps are available)
"""
# Look for a wizard_prev_step element in the posted data which
# contains a valid step name. If one was found, render the requested
# form. (This makes stepping back a lot easier).
wizard_prev_step = self.request.POST.get('wizard_prev_step', None)
if wizard_prev_step and wizard_prev_step in self.get_form_list():
self.storage.current_step = wizard_prev_step
form = self.get_form(
data=self.storage.get_step_data(self.steps.current),
files=self.storage.get_step_files(self.steps.current))
return self.render(form)
# Check if form was refreshed
management_form = ManagementForm(self.request.POST, prefix=self.prefix)
if not management_form.is_valid():
raise ValidationError(
'ManagementForm data is missing or has been tampered.')
form_current_step = management_form.cleaned_data['current_step']
if (form_current_step != self.steps.current and
self.storage.current_step is not None):
# form refreshed, change current step
self.storage.current_step = form_current_step
# get the form for the current step
form = self.get_form(data=self.request.POST, files=self.request.FILES)
# and try to validate
if form.is_valid():
# if the form is valid, store the cleaned data and files.
self.storage.set_step_data(self.steps.current, self.process_step(form))
self.storage.set_step_files(self.steps.current, self.process_step_files(form))
# check if the current step is the last step
if self.steps.current == self.steps.last:
# no more steps, render done view
return self.render_done(form, **kwargs)
else:
# proceed to the next step
return self.render_next_step(form)
return self.render(form) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:parse_config_file; 3, parameters; 3, 4; 3, 5; 4, identifier:job; 5, identifier:config_file; 6, block; 6, 7; 6, 9; 6, 18; 6, 29; 6, 47; 6, 48; 6, 54; 6, 60; 6, 66; 6, 67; 6, 246; 6, 247; 6, 262; 6, 263; 6, 279; 6, 299; 6, 300; 6, 320; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, call; 10, 11; 10, 16; 11, attribute; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:job; 14, identifier:fileStore; 15, identifier:logToMaster; 16, argument_list; 16, 17; 17, string:'Parsing config file'; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:config_file; 21, call; 21, 22; 21, 27; 22, attribute; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:os; 25, identifier:path; 26, identifier:abspath; 27, argument_list; 27, 28; 28, identifier:config_file; 29, if_statement; 29, 30; 29, 39; 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:os; 35, identifier:path; 36, identifier:exists; 37, argument_list; 37, 38; 38, identifier:config_file; 39, block; 39, 40; 40, raise_statement; 40, 41; 41, call; 41, 42; 41, 43; 42, identifier:ParameterError; 43, argument_list; 43, 44; 44, binary_operator:+; 44, 45; 44, 46; 45, string:'The config file was not found at specified location. Please verify '; 46, string:'and retry.'; 47, comment; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:sample_set; 51, call; 51, 52; 51, 53; 52, identifier:defaultdict; 53, argument_list; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:univ_options; 57, call; 57, 58; 57, 59; 58, identifier:defaultdict; 59, argument_list; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:tool_options; 63, call; 63, 64; 63, 65; 64, identifier:defaultdict; 65, argument_list; 66, comment; 67, with_statement; 67, 68; 67, 78; 68, with_clause; 68, 69; 69, with_item; 69, 70; 70, as_pattern; 70, 71; 70, 76; 71, call; 71, 72; 71, 73; 72, identifier:open; 73, argument_list; 73, 74; 73, 75; 74, identifier:config_file; 75, string:'r'; 76, as_pattern_target; 76, 77; 77, identifier:conf; 78, block; 78, 79; 78, 116; 78, 117; 78, 118; 79, for_statement; 79, 80; 79, 81; 79, 82; 80, identifier:line; 81, identifier:conf; 82, block; 82, 83; 82, 91; 82, 107; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:line; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:line; 89, identifier:strip; 90, argument_list; 91, if_statement; 91, 92; 91, 105; 92, boolean_operator:or; 92, 93; 92, 99; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:line; 96, identifier:startswith; 97, argument_list; 97, 98; 98, string:'##'; 99, comparison_operator:==; 99, 100; 99, 104; 100, call; 100, 101; 100, 102; 101, identifier:len; 102, argument_list; 102, 103; 103, identifier:line; 104, integer:0; 105, block; 105, 106; 106, continue_statement; 107, if_statement; 107, 108; 107, 114; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:line; 111, identifier:startswith; 112, argument_list; 112, 113; 113, string:'BEGIN'; 114, block; 114, 115; 115, break_statement; 116, comment; 117, comment; 118, for_statement; 118, 119; 118, 122; 118, 127; 119, pattern_list; 119, 120; 119, 121; 120, identifier:groupname; 121, identifier:group_params; 122, call; 122, 123; 122, 124; 123, identifier:tool_specific_param_generator; 124, argument_list; 124, 125; 124, 126; 125, identifier:job; 126, identifier:conf; 127, block; 127, 128; 128, if_statement; 128, 129; 128, 132; 128, 155; 128, 237; 128, 238; 129, comparison_operator:==; 129, 130; 129, 131; 130, identifier:groupname; 131, string:'patient'; 132, block; 132, 133; 132, 147; 133, if_statement; 133, 134; 133, 141; 134, comparison_operator:not; 134, 135; 134, 136; 135, string:'patient_id'; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:group_params; 139, identifier:keys; 140, argument_list; 141, block; 141, 142; 142, raise_statement; 142, 143; 143, call; 143, 144; 143, 145; 144, identifier:ParameterError; 145, argument_list; 145, 146; 146, string:'A patient group is missing the patient_id flag.'; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 154; 149, subscript; 149, 150; 149, 151; 150, identifier:sample_set; 151, subscript; 151, 152; 151, 153; 152, identifier:group_params; 153, string:'patient_id'; 154, identifier:group_params; 155, elif_clause; 155, 156; 155, 159; 156, comparison_operator:==; 156, 157; 156, 158; 157, identifier:groupname; 158, string:'Universal_Options'; 159, block; 159, 160; 159, 164; 159, 171; 159, 187; 159, 209; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:univ_options; 163, identifier:group_params; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:required_options; 167, set; 167, 168; 167, 169; 167, 170; 168, string:'java_Xmx'; 169, string:'output_folder'; 170, string:'storage_location'; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:missing_opts; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:required_options; 177, identifier:difference; 178, argument_list; 178, 179; 179, call; 179, 180; 179, 181; 180, identifier:set; 181, argument_list; 181, 182; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:univ_options; 185, identifier:keys; 186, argument_list; 187, if_statement; 187, 188; 187, 194; 188, comparison_operator:>; 188, 189; 188, 193; 189, call; 189, 190; 189, 191; 190, identifier:len; 191, argument_list; 191, 192; 192, identifier:missing_opts; 193, integer:0; 194, block; 194, 195; 195, raise_statement; 195, 196; 196, call; 196, 197; 196, 198; 197, identifier:ParameterError; 198, argument_list; 198, 199; 199, binary_operator:+; 199, 200; 199, 203; 200, concatenated_string; 200, 201; 200, 202; 201, string:' The following options have no arguments in the config '; 202, string:'file :\n'; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, string:'\n'; 206, identifier:join; 207, argument_list; 207, 208; 208, identifier:missing_opts; 209, if_statement; 209, 210; 209, 213; 210, subscript; 210, 211; 210, 212; 211, identifier:univ_options; 212, string:'sse_key_is_master'; 213, block; 213, 214; 213, 224; 214, assert_statement; 214, 215; 215, comparison_operator:in; 215, 216; 215, 219; 216, subscript; 216, 217; 216, 218; 217, identifier:univ_options; 218, string:'sse_key_is_master'; 219, tuple; 219, 220; 219, 221; 219, 222; 219, 223; 220, string:'True'; 221, string:'true'; 222, string:'False'; 223, string:'false'; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 229; 225, 230; 226, subscript; 226, 227; 226, 228; 227, identifier:univ_options; 228, string:'sse_key_is_master'; 229, line_continuation:\; 230, comparison_operator:in; 230, 231; 230, 234; 231, subscript; 231, 232; 231, 233; 232, identifier:univ_options; 233, string:'sse_key_is_master'; 234, tuple; 234, 235; 234, 236; 235, string:'True'; 236, string:'true'; 237, comment; 238, else_clause; 238, 239; 239, block; 239, 240; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 245; 242, subscript; 242, 243; 242, 244; 243, identifier:tool_options; 244, identifier:groupname; 245, identifier:group_params; 246, comment; 247, expression_statement; 247, 248; 248, assignment; 248, 249; 248, 250; 249, identifier:required_tools; 250, set; 250, 251; 250, 252; 250, 253; 250, 254; 250, 255; 250, 256; 250, 257; 250, 258; 250, 259; 250, 260; 250, 261; 251, string:'cutadapt'; 252, string:'bwa'; 253, string:'star'; 254, string:'phlat'; 255, string:'transgene'; 256, string:'mut_callers'; 257, string:'rsem'; 258, string:'mhci'; 259, string:'mhcii'; 260, string:'snpeff'; 261, string:'rank_boost'; 262, comment; 263, expression_statement; 263, 264; 264, assignment; 264, 265; 264, 266; 265, identifier:missing_tools; 266, call; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:required_tools; 269, identifier:difference; 270, argument_list; 270, 271; 271, call; 271, 272; 271, 273; 272, identifier:set; 273, argument_list; 273, 274; 274, call; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:tool_options; 277, identifier:keys; 278, argument_list; 279, if_statement; 279, 280; 279, 286; 280, comparison_operator:>; 280, 281; 280, 285; 281, call; 281, 282; 281, 283; 282, identifier:len; 283, argument_list; 283, 284; 284, identifier:missing_tools; 285, integer:0; 286, block; 286, 287; 287, raise_statement; 287, 288; 288, call; 288, 289; 288, 290; 289, identifier:ParameterError; 290, argument_list; 290, 291; 291, binary_operator:+; 291, 292; 291, 293; 292, string:' The following tools have no arguments in the config file : \n'; 293, call; 293, 294; 293, 297; 294, attribute; 294, 295; 294, 296; 295, string:'\n'; 296, identifier:join; 297, argument_list; 297, 298; 298, identifier:missing_tools; 299, comment; 300, for_statement; 300, 301; 300, 302; 300, 307; 301, identifier:patient_id; 302, call; 302, 303; 302, 306; 303, attribute; 303, 304; 303, 305; 304, identifier:sample_set; 305, identifier:keys; 306, argument_list; 307, block; 307, 308; 308, expression_statement; 308, 309; 309, call; 309, 310; 309, 313; 310, attribute; 310, 311; 310, 312; 311, identifier:job; 312, identifier:addFollowOnJobFn; 313, argument_list; 313, 314; 313, 315; 313, 318; 313, 319; 314, identifier:pipeline_launchpad; 315, subscript; 315, 316; 315, 317; 316, identifier:sample_set; 317, identifier:patient_id; 318, identifier:univ_options; 319, identifier:tool_options; 320, return_statement; 320, 321; 321, None | def parse_config_file(job, config_file):
"""
This module will parse the config file withing params and set up the variables that will be
passed to the various tools in the pipeline.
ARGUMENTS
config_file: string containing path to a config file. An example config
file is available at
https://s3-us-west-2.amazonaws.com/pimmuno-references
/input_parameters.list
RETURN VALUES
None
"""
job.fileStore.logToMaster('Parsing config file')
config_file = os.path.abspath(config_file)
if not os.path.exists(config_file):
raise ParameterError('The config file was not found at specified location. Please verify ' +
'and retry.')
# Initialize variables to hold the sample sets, the universal options, and the per-tool options
sample_set = defaultdict()
univ_options = defaultdict()
tool_options = defaultdict()
# Read through the notes and the
with open(config_file, 'r') as conf:
for line in conf:
line = line.strip()
if line.startswith('##') or len(line) == 0:
continue
if line.startswith('BEGIN'):
break
# The generator function tool_specific_param_generator will yield one group name at a time
# along with it's parameters.
for groupname, group_params in tool_specific_param_generator(job, conf):
if groupname == 'patient':
if 'patient_id' not in group_params.keys():
raise ParameterError('A patient group is missing the patient_id flag.')
sample_set[group_params['patient_id']] = group_params
elif groupname == 'Universal_Options':
univ_options = group_params
required_options = {'java_Xmx', 'output_folder', 'storage_location'}
missing_opts = required_options.difference(set(univ_options.keys()))
if len(missing_opts) > 0:
raise ParameterError(' The following options have no arguments in the config '
'file :\n' + '\n'.join(missing_opts))
if univ_options['sse_key_is_master']:
assert univ_options['sse_key_is_master'] in ('True', 'true', 'False', 'false')
univ_options['sse_key_is_master'] = \
univ_options['sse_key_is_master'] in ('True', 'true')
# If it isn't any of the above, it's a tool group
else:
tool_options[groupname] = group_params
# Ensure that all tools have been provided options.
required_tools = {'cutadapt', 'bwa', 'star', 'phlat', 'transgene', 'mut_callers', 'rsem',
'mhci', 'mhcii', 'snpeff', 'rank_boost'}
# 'fusion', 'indels'}
missing_tools = required_tools.difference(set(tool_options.keys()))
if len(missing_tools) > 0:
raise ParameterError(' The following tools have no arguments in the config file : \n' +
'\n'.join(missing_tools))
# Start a job for each sample in the sample set
for patient_id in sample_set.keys():
job.addFollowOnJobFn(pipeline_launchpad, sample_set[patient_id], univ_options, tool_options)
return None |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:run_star; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:job; 5, identifier:fastqs; 6, identifier:univ_options; 7, identifier:star_options; 8, block; 8, 9; 8, 11; 8, 19; 8, 32; 8, 42; 8, 61; 8, 73; 8, 110; 8, 155; 8, 161; 8, 187; 8, 198; 8, 209; 8, 226; 8, 233; 8, 243; 9, expression_statement; 9, 10; 10, comment; 11, assert_statement; 11, 12; 12, comparison_operator:in; 12, 13; 12, 16; 13, subscript; 13, 14; 13, 15; 14, identifier:star_options; 15, string:'type'; 16, tuple; 16, 17; 16, 18; 17, string:'star'; 18, string:'starlong'; 19, expression_statement; 19, 20; 20, call; 20, 21; 20, 26; 21, attribute; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:job; 24, identifier:fileStore; 25, identifier:logToMaster; 26, argument_list; 26, 27; 27, binary_operator:%; 27, 28; 27, 29; 28, string:'Running STAR on %s'; 29, subscript; 29, 30; 29, 31; 30, identifier:univ_options; 31, string:'patient'; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:work_dir; 35, call; 35, 36; 35, 41; 36, attribute; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:job; 39, identifier:fileStore; 40, identifier:getLocalTempDir; 41, argument_list; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:input_files; 45, dictionary; 45, 46; 45, 51; 45, 56; 46, pair; 46, 47; 46, 48; 47, string:'rna_cutadapt_1.fastq'; 48, subscript; 48, 49; 48, 50; 49, identifier:fastqs; 50, string:'rna_cutadapt_1.fastq'; 51, pair; 51, 52; 51, 53; 52, string:'rna_cutadapt_2.fastq'; 53, subscript; 53, 54; 53, 55; 54, identifier:fastqs; 55, string:'rna_cutadapt_2.fastq'; 56, pair; 56, 57; 56, 58; 57, string:'star_index.tar.gz'; 58, subscript; 58, 59; 58, 60; 59, identifier:star_options; 60, string:'index_tar'; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:input_files; 64, call; 64, 65; 64, 66; 65, identifier:get_files_from_filestore; 66, argument_list; 66, 67; 66, 68; 66, 69; 66, 70; 67, identifier:job; 68, identifier:input_files; 69, identifier:work_dir; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:docker; 72, True; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:parameters; 76, list:['--runThreadN', str(star_options['n']),
'--genomeDir', input_files['star_index'],
'--outFileNamePrefix', 'rna',
'--readFilesIn',
input_files['rna_cutadapt_1.fastq'],
input_files['rna_cutadapt_2.fastq'],
'--outSAMattributes', 'NH', 'HI', 'AS', 'NM', 'MD',
'--outSAMtype', 'BAM', 'SortedByCoordinate',
'--quantMode', 'TranscriptomeSAM',
'--outSAMunmapped', 'Within']; 76, 77; 76, 78; 76, 84; 76, 85; 76, 88; 76, 89; 76, 90; 76, 91; 76, 94; 76, 97; 76, 98; 76, 99; 76, 100; 76, 101; 76, 102; 76, 103; 76, 104; 76, 105; 76, 106; 76, 107; 76, 108; 76, 109; 77, string:'--runThreadN'; 78, call; 78, 79; 78, 80; 79, identifier:str; 80, argument_list; 80, 81; 81, subscript; 81, 82; 81, 83; 82, identifier:star_options; 83, string:'n'; 84, string:'--genomeDir'; 85, subscript; 85, 86; 85, 87; 86, identifier:input_files; 87, string:'star_index'; 88, string:'--outFileNamePrefix'; 89, string:'rna'; 90, string:'--readFilesIn'; 91, subscript; 91, 92; 91, 93; 92, identifier:input_files; 93, string:'rna_cutadapt_1.fastq'; 94, subscript; 94, 95; 94, 96; 95, identifier:input_files; 96, string:'rna_cutadapt_2.fastq'; 97, string:'--outSAMattributes'; 98, string:'NH'; 99, string:'HI'; 100, string:'AS'; 101, string:'NM'; 102, string:'MD'; 103, string:'--outSAMtype'; 104, string:'BAM'; 105, string:'SortedByCoordinate'; 106, string:'--quantMode'; 107, string:'TranscriptomeSAM'; 108, string:'--outSAMunmapped'; 109, string:'Within'; 110, if_statement; 110, 111; 110, 116; 110, 135; 111, comparison_operator:==; 111, 112; 111, 115; 112, subscript; 112, 113; 112, 114; 113, identifier:star_options; 114, string:'type'; 115, string:'star'; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, call; 118, 119; 118, 120; 119, identifier:docker_call; 120, argument_list; 120, 121; 120, 124; 120, 127; 120, 130; 121, keyword_argument; 121, 122; 121, 123; 122, identifier:tool; 123, string:'star'; 124, keyword_argument; 124, 125; 124, 126; 125, identifier:tool_parameters; 126, identifier:parameters; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:work_dir; 129, identifier:work_dir; 130, keyword_argument; 130, 131; 130, 132; 131, identifier:dockerhub; 132, subscript; 132, 133; 132, 134; 133, identifier:univ_options; 134, string:'dockerhub'; 135, else_clause; 135, 136; 136, block; 136, 137; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 140; 139, identifier:docker_call; 140, argument_list; 140, 141; 140, 144; 140, 147; 140, 150; 141, keyword_argument; 141, 142; 141, 143; 142, identifier:tool; 143, string:'starlong'; 144, keyword_argument; 144, 145; 144, 146; 145, identifier:tool_parameters; 146, identifier:parameters; 147, keyword_argument; 147, 148; 147, 149; 148, identifier:work_dir; 149, identifier:work_dir; 150, keyword_argument; 150, 151; 150, 152; 151, identifier:dockerhub; 152, subscript; 152, 153; 152, 154; 153, identifier:univ_options; 154, string:'dockerhub'; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:output_files; 158, call; 158, 159; 158, 160; 159, identifier:defaultdict; 160, argument_list; 161, for_statement; 161, 162; 161, 163; 161, 166; 162, identifier:bam_file; 163, list:['rnaAligned.toTranscriptome.out.bam',
'rnaAligned.sortedByCoord.out.bam']; 163, 164; 163, 165; 164, string:'rnaAligned.toTranscriptome.out.bam'; 165, string:'rnaAligned.sortedByCoord.out.bam'; 166, block; 166, 167; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 172; 169, subscript; 169, 170; 169, 171; 170, identifier:output_files; 171, identifier:bam_file; 172, call; 172, 173; 172, 178; 173, attribute; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:job; 176, identifier:fileStore; 177, identifier:writeGlobalFile; 178, argument_list; 178, 179; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, string:'/'; 182, identifier:join; 183, argument_list; 183, 184; 184, list:[
work_dir, bam_file]; 184, 185; 184, 186; 185, identifier:work_dir; 186, identifier:bam_file; 187, expression_statement; 187, 188; 188, call; 188, 189; 188, 194; 189, attribute; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:job; 192, identifier:fileStore; 193, identifier:deleteGlobalFile; 194, argument_list; 194, 195; 195, subscript; 195, 196; 195, 197; 196, identifier:fastqs; 197, string:'rna_cutadapt_1.fastq'; 198, expression_statement; 198, 199; 199, call; 199, 200; 199, 205; 200, attribute; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:job; 203, identifier:fileStore; 204, identifier:deleteGlobalFile; 205, argument_list; 205, 206; 206, subscript; 206, 207; 206, 208; 207, identifier:fastqs; 208, string:'rna_cutadapt_2.fastq'; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:index_star; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:job; 215, identifier:wrapJobFn; 216, argument_list; 216, 217; 216, 218; 216, 221; 216, 222; 216, 223; 217, identifier:index_bamfile; 218, subscript; 218, 219; 218, 220; 219, identifier:output_files; 220, string:'rnaAligned.sortedByCoord.out.bam'; 221, string:'rna'; 222, identifier:univ_options; 223, keyword_argument; 223, 224; 223, 225; 224, identifier:disk; 225, string:'120G'; 226, expression_statement; 226, 227; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:job; 230, identifier:addChild; 231, argument_list; 231, 232; 232, identifier:index_star; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 238; 235, subscript; 235, 236; 235, 237; 236, identifier:output_files; 237, string:'rnaAligned.sortedByCoord.out.bam'; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:index_star; 241, identifier:rv; 242, argument_list; 243, return_statement; 243, 244; 244, identifier:output_files | def run_star(job, fastqs, univ_options, star_options):
"""
This module uses STAR to align the RNA fastqs to the reference
ARGUMENTS
1. fastqs: REFER RETURN VALUE of run_cutadapt()
2. univ_options: Dict of universal arguments used by almost all tools
univ_options
+- 'dockerhub': <dockerhub to use>
3. star_options: Dict of parameters specific to STAR
star_options
|- 'index_tar': <JSid for the STAR index tarball>
+- 'n': <number of threads to allocate>
RETURN VALUES
1. output_files: Dict of aligned bams
output_files
|- 'rnaAligned.toTranscriptome.out.bam': <JSid>
+- 'rnaAligned.sortedByCoord.out.bam': Dict of genome bam + bai
|- 'rna_fix_pg_sorted.bam': <JSid>
+- 'rna_fix_pg_sorted.bam.bai': <JSid>
This module corresponds to node 9 on the tree
"""
assert star_options['type'] in ('star', 'starlong')
job.fileStore.logToMaster('Running STAR on %s' %univ_options['patient'])
work_dir = job.fileStore.getLocalTempDir()
input_files = {
'rna_cutadapt_1.fastq': fastqs['rna_cutadapt_1.fastq'],
'rna_cutadapt_2.fastq': fastqs['rna_cutadapt_2.fastq'],
'star_index.tar.gz': star_options['index_tar']}
input_files = get_files_from_filestore(job, input_files, work_dir,
docker=True)
parameters = ['--runThreadN', str(star_options['n']),
'--genomeDir', input_files['star_index'],
'--outFileNamePrefix', 'rna',
'--readFilesIn',
input_files['rna_cutadapt_1.fastq'],
input_files['rna_cutadapt_2.fastq'],
'--outSAMattributes', 'NH', 'HI', 'AS', 'NM', 'MD',
'--outSAMtype', 'BAM', 'SortedByCoordinate',
'--quantMode', 'TranscriptomeSAM',
'--outSAMunmapped', 'Within']
if star_options['type'] == 'star':
docker_call(tool='star', tool_parameters=parameters, work_dir=work_dir,
dockerhub=univ_options['dockerhub'])
else:
docker_call(tool='starlong', tool_parameters=parameters, work_dir=work_dir,
dockerhub=univ_options['dockerhub'])
output_files = defaultdict()
for bam_file in ['rnaAligned.toTranscriptome.out.bam',
'rnaAligned.sortedByCoord.out.bam']:
output_files[bam_file] = job.fileStore.writeGlobalFile('/'.join([
work_dir, bam_file]))
job.fileStore.deleteGlobalFile(fastqs['rna_cutadapt_1.fastq'])
job.fileStore.deleteGlobalFile(fastqs['rna_cutadapt_2.fastq'])
index_star = job.wrapJobFn(index_bamfile,
output_files['rnaAligned.sortedByCoord.out.bam'],
'rna', univ_options, disk='120G')
job.addChild(index_star)
output_files['rnaAligned.sortedByCoord.out.bam'] = index_star.rv()
return output_files |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:run_bwa; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:job; 5, identifier:fastqs; 6, identifier:sample_type; 7, identifier:univ_options; 8, identifier:bwa_options; 9, block; 9, 10; 9, 12; 9, 27; 9, 37; 9, 46; 9, 73; 9, 85; 9, 116; 9, 158; 9, 159; 9, 172; 9, 187; 9, 194; 9, 195; 9, 196; 9, 197; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, call; 13, 14; 13, 19; 14, attribute; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:job; 17, identifier:fileStore; 18, identifier:logToMaster; 19, argument_list; 19, 20; 20, binary_operator:%; 20, 21; 20, 22; 21, string:'Running bwa on %s:%s'; 22, tuple; 22, 23; 22, 26; 23, subscript; 23, 24; 23, 25; 24, identifier:univ_options; 25, string:'patient'; 26, identifier:sample_type; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:work_dir; 30, call; 30, 31; 30, 36; 31, attribute; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:job; 34, identifier:fileStore; 35, identifier:getLocalTempDir; 36, argument_list; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:fq_extn; 40, conditional_expression:if; 40, 41; 40, 42; 40, 45; 41, string:'.gz'; 42, subscript; 42, 43; 42, 44; 43, identifier:fastqs; 44, string:'gzipped'; 45, string:''; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:input_files; 49, dictionary; 49, 50; 49, 59; 49, 68; 50, pair; 50, 51; 50, 54; 51, binary_operator:+; 51, 52; 51, 53; 52, string:'dna_1.fastq'; 53, identifier:fq_extn; 54, subscript; 54, 55; 54, 58; 55, subscript; 55, 56; 55, 57; 56, identifier:fastqs; 57, identifier:sample_type; 58, integer:0; 59, pair; 59, 60; 59, 63; 60, binary_operator:+; 60, 61; 60, 62; 61, string:'dna_2.fastq'; 62, identifier:fq_extn; 63, subscript; 63, 64; 63, 67; 64, subscript; 64, 65; 64, 66; 65, identifier:fastqs; 66, identifier:sample_type; 67, integer:1; 68, pair; 68, 69; 68, 70; 69, string:'bwa_index.tar.gz'; 70, subscript; 70, 71; 70, 72; 71, identifier:bwa_options; 72, string:'index_tar'; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:input_files; 76, call; 76, 77; 76, 78; 77, identifier:get_files_from_filestore; 78, argument_list; 78, 79; 78, 80; 78, 81; 78, 82; 79, identifier:job; 80, identifier:input_files; 81, identifier:work_dir; 82, keyword_argument; 82, 83; 82, 84; 83, identifier:docker; 84, True; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:parameters; 88, list:['mem',
'-t', str(bwa_options['n']),
'-v', '1', # Don't print INFO messages to the stderr
'/'.join([input_files['bwa_index'], 'hg19.fa']),
input_files['dna_1.fastq'],
input_files['dna_2.fastq']]; 88, 89; 88, 90; 88, 91; 88, 97; 88, 98; 88, 99; 88, 100; 88, 110; 88, 113; 89, string:'mem'; 90, string:'-t'; 91, call; 91, 92; 91, 93; 92, identifier:str; 93, argument_list; 93, 94; 94, subscript; 94, 95; 94, 96; 95, identifier:bwa_options; 96, string:'n'; 97, string:'-v'; 98, string:'1'; 99, comment; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, string:'/'; 103, identifier:join; 104, argument_list; 104, 105; 105, list:[input_files['bwa_index'], 'hg19.fa']; 105, 106; 105, 109; 106, subscript; 106, 107; 106, 108; 107, identifier:input_files; 108, string:'bwa_index'; 109, string:'hg19.fa'; 110, subscript; 110, 111; 110, 112; 111, identifier:input_files; 112, string:'dna_1.fastq'; 113, subscript; 113, 114; 113, 115; 114, identifier:input_files; 115, string:'dna_2.fastq'; 116, with_statement; 116, 117; 116, 136; 117, with_clause; 117, 118; 118, with_item; 118, 119; 119, as_pattern; 119, 120; 119, 134; 120, call; 120, 121; 120, 122; 121, identifier:open; 122, argument_list; 122, 123; 122, 133; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, string:''; 126, identifier:join; 127, argument_list; 127, 128; 128, list:[work_dir, '/', sample_type, '_aligned.sam']; 128, 129; 128, 130; 128, 131; 128, 132; 129, identifier:work_dir; 130, string:'/'; 131, identifier:sample_type; 132, string:'_aligned.sam'; 133, string:'w'; 134, as_pattern_target; 134, 135; 135, identifier:samfile; 136, block; 136, 137; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 140; 139, identifier:docker_call; 140, argument_list; 140, 141; 140, 144; 140, 147; 140, 150; 140, 155; 141, keyword_argument; 141, 142; 141, 143; 142, identifier:tool; 143, string:'bwa'; 144, keyword_argument; 144, 145; 144, 146; 145, identifier:tool_parameters; 146, identifier:parameters; 147, keyword_argument; 147, 148; 147, 149; 148, identifier:work_dir; 149, identifier:work_dir; 150, keyword_argument; 150, 151; 150, 152; 151, identifier:dockerhub; 152, subscript; 152, 153; 152, 154; 153, identifier:univ_options; 154, string:'dockerhub'; 155, keyword_argument; 155, 156; 155, 157; 156, identifier:outfile; 157, identifier:samfile; 158, comment; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:output_file; 162, call; 162, 163; 162, 168; 163, attribute; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:job; 166, identifier:fileStore; 167, identifier:writeGlobalFile; 168, argument_list; 168, 169; 169, attribute; 169, 170; 169, 171; 170, identifier:samfile; 171, identifier:name; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:samfile_processing; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:job; 178, identifier:wrapJobFn; 179, argument_list; 179, 180; 179, 181; 179, 182; 179, 183; 179, 184; 180, identifier:bam_conversion; 181, identifier:output_file; 182, identifier:sample_type; 183, identifier:univ_options; 184, keyword_argument; 184, 185; 184, 186; 185, identifier:disk; 186, string:'60G'; 187, expression_statement; 187, 188; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:job; 191, identifier:addChild; 192, argument_list; 192, 193; 193, identifier:samfile_processing; 194, comment; 195, comment; 196, comment; 197, return_statement; 197, 198; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:samfile_processing; 201, identifier:rv; 202, argument_list | def run_bwa(job, fastqs, sample_type, univ_options, bwa_options):
"""
This module aligns the SAMPLE_TYPE dna fastqs to the reference
ARGUMENTS -- <ST> depicts the sample type. Substitute with 'tumor'/'normal'
1. fastqs: Dict of list of input WGS/WXS fastqs
fastqs
+- '<ST>_dna': [<JSid for 1.fastq> , <JSid for 2.fastq>]
2. sample_type: string of 'tumor_dna' or 'normal_dna'
3. univ_options: Dict of universal arguments used by almost all tools
univ_options
+- 'dockerhub': <dockerhub to use>
4. bwa_options: Dict of parameters specific to bwa
bwa_options
|- 'index_tar': <JSid for the bwa index tarball>
+- 'n': <number of threads to allocate>
RETURN VALUES
1. output_files: Dict of aligned bam + reference (nested return)
output_files
|- '<ST>_fix_pg_sorted.bam': <JSid>
+- '<ST>_fix_pg_sorted.bam.bai': <JSid>
This module corresponds to nodes 3 and 4 on the tree
"""
job.fileStore.logToMaster('Running bwa on %s:%s' % (univ_options['patient'], sample_type))
work_dir = job.fileStore.getLocalTempDir()
fq_extn = '.gz' if fastqs['gzipped'] else ''
input_files = {
'dna_1.fastq' + fq_extn: fastqs[sample_type][0],
'dna_2.fastq' + fq_extn: fastqs[sample_type][1],
'bwa_index.tar.gz': bwa_options['index_tar']}
input_files = get_files_from_filestore(job, input_files, work_dir, docker=True)
parameters = ['mem',
'-t', str(bwa_options['n']),
'-v', '1', # Don't print INFO messages to the stderr
'/'.join([input_files['bwa_index'], 'hg19.fa']),
input_files['dna_1.fastq'],
input_files['dna_2.fastq']]
with open(''.join([work_dir, '/', sample_type, '_aligned.sam']), 'w') as samfile:
docker_call(tool='bwa', tool_parameters=parameters, work_dir=work_dir,
dockerhub=univ_options['dockerhub'], outfile=samfile)
# samfile.name retains the path info
output_file = job.fileStore.writeGlobalFile(samfile.name)
samfile_processing = job.wrapJobFn(bam_conversion, output_file, sample_type, univ_options,
disk='60G')
job.addChild(samfile_processing)
# Return values get passed up the chain to here. The return value will be a dict with
# SAMPLE_TYPE_fix_pg_sorted.bam: jobStoreID
# SAMPLE_TYPE_fix_pg_sorted.bam.bai: jobStoreID
return samfile_processing.rv() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:merge_radia; 3, parameters; 3, 4; 3, 5; 4, identifier:job; 5, identifier:perchrom_rvs; 6, block; 6, 7; 6, 9; 6, 18; 6, 28; 6, 29; 6, 30; 6, 53; 6, 65; 6, 91; 6, 240; 6, 241; 6, 278; 6, 284; 6, 314; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, call; 10, 11; 10, 16; 11, attribute; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:job; 14, identifier:fileStore; 15, identifier:logToMaster; 16, argument_list; 16, 17; 17, string:'Running merge_radia'; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:work_dir; 21, call; 21, 22; 21, 27; 22, attribute; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:job; 25, identifier:fileStore; 26, identifier:getLocalTempDir; 27, argument_list; 28, comment; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:input_files; 33, dictionary_comprehension; 33, 34; 33, 37; 33, 44; 34, pair; 34, 35; 34, 36; 35, identifier:filename; 36, identifier:jsid; 37, for_in_clause; 37, 38; 37, 39; 38, identifier:perchrom_files; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:perchrom_rvs; 42, identifier:values; 43, argument_list; 44, for_in_clause; 44, 45; 44, 48; 45, pattern_list; 45, 46; 45, 47; 46, identifier:filename; 47, identifier:jsid; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:perchrom_files; 51, identifier:items; 52, argument_list; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:input_files; 56, call; 56, 57; 56, 58; 57, identifier:get_files_from_filestore; 58, argument_list; 58, 59; 58, 60; 58, 61; 58, 62; 59, identifier:job; 60, identifier:input_files; 61, identifier:work_dir; 62, keyword_argument; 62, 63; 62, 64; 63, identifier:docker; 64, False; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:chromosomes; 68, list_comprehension; 68, 69; 68, 80; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, string:''; 72, identifier:join; 73, argument_list; 73, 74; 74, list:['chr', str(x)]; 74, 75; 74, 76; 75, string:'chr'; 76, call; 76, 77; 76, 78; 77, identifier:str; 78, argument_list; 78, 79; 79, identifier:x; 80, for_in_clause; 80, 81; 80, 82; 81, identifier:x; 82, binary_operator:+; 82, 83; 82, 88; 83, call; 83, 84; 83, 85; 84, identifier:range; 85, argument_list; 85, 86; 85, 87; 86, integer:1; 87, integer:23; 88, list:['X', 'Y']; 88, 89; 88, 90; 89, string:'X'; 90, string:'Y'; 91, with_statement; 91, 92; 91, 126; 92, with_clause; 92, 93; 92, 109; 92, 110; 93, with_item; 93, 94; 94, as_pattern; 94, 95; 94, 107; 95, call; 95, 96; 95, 97; 96, identifier:open; 97, argument_list; 97, 98; 97, 106; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, string:'/'; 101, identifier:join; 102, argument_list; 102, 103; 103, list:[work_dir, 'radia_calls.vcf']; 103, 104; 103, 105; 104, identifier:work_dir; 105, string:'radia_calls.vcf'; 106, string:'w'; 107, as_pattern_target; 107, 108; 108, identifier:radfile; 109, line_continuation:\; 110, with_item; 110, 111; 111, as_pattern; 111, 112; 111, 124; 112, call; 112, 113; 112, 114; 113, identifier:open; 114, argument_list; 114, 115; 114, 123; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, string:'/'; 118, identifier:join; 119, argument_list; 119, 120; 120, list:[work_dir, 'radia_filter_passing_calls.vcf']; 120, 121; 120, 122; 121, identifier:work_dir; 122, string:'radia_filter_passing_calls.vcf'; 123, string:'w'; 124, as_pattern_target; 124, 125; 125, identifier:radpassfile; 126, block; 126, 127; 127, for_statement; 127, 128; 127, 129; 127, 130; 128, identifier:chrom; 129, identifier:chromosomes; 130, block; 130, 131; 131, with_statement; 131, 132; 131, 152; 132, with_clause; 132, 133; 133, with_item; 133, 134; 134, as_pattern; 134, 135; 134, 150; 135, call; 135, 136; 135, 137; 136, identifier:open; 137, argument_list; 137, 138; 137, 149; 138, subscript; 138, 139; 138, 140; 139, identifier:input_files; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, string:''; 143, identifier:join; 144, argument_list; 144, 145; 145, list:['radia_filtered_', chrom, '.vcf']; 145, 146; 145, 147; 145, 148; 146, string:'radia_filtered_'; 147, identifier:chrom; 148, string:'.vcf'; 149, string:'r'; 150, as_pattern_target; 150, 151; 151, identifier:filtradfile; 152, block; 152, 153; 153, for_statement; 153, 154; 153, 155; 153, 156; 154, identifier:line; 155, identifier:filtradfile; 156, block; 156, 157; 156, 165; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:line; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:line; 163, identifier:strip; 164, argument_list; 165, if_statement; 165, 166; 165, 172; 165, 195; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:line; 169, identifier:startswith; 170, argument_list; 170, 171; 171, string:'#'; 172, block; 172, 173; 172, 194; 173, if_statement; 173, 174; 173, 177; 174, comparison_operator:==; 174, 175; 174, 176; 175, identifier:chrom; 176, string:'chr1'; 177, block; 177, 178; 177, 186; 178, expression_statement; 178, 179; 179, call; 179, 180; 179, 181; 180, identifier:print; 181, argument_list; 181, 182; 181, 183; 182, identifier:line; 183, keyword_argument; 183, 184; 183, 185; 184, identifier:file; 185, identifier:radfile; 186, expression_statement; 186, 187; 187, call; 187, 188; 187, 189; 188, identifier:print; 189, argument_list; 189, 190; 189, 191; 190, identifier:line; 191, keyword_argument; 191, 192; 191, 193; 192, identifier:file; 193, identifier:radpassfile; 194, continue_statement; 195, else_clause; 195, 196; 196, block; 196, 197; 196, 205; 196, 214; 197, expression_statement; 197, 198; 198, call; 198, 199; 198, 200; 199, identifier:print; 200, argument_list; 200, 201; 200, 202; 201, identifier:line; 202, keyword_argument; 202, 203; 202, 204; 203, identifier:file; 204, identifier:radfile; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:line; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:line; 211, identifier:split; 212, argument_list; 212, 213; 213, string:'\t'; 214, if_statement; 214, 215; 214, 226; 215, boolean_operator:and; 215, 216; 215, 221; 216, comparison_operator:==; 216, 217; 216, 220; 217, subscript; 217, 218; 217, 219; 218, identifier:line; 219, integer:6; 220, string:'PASS'; 221, comparison_operator:not; 221, 222; 221, 223; 222, string:'MT=GERM'; 223, subscript; 223, 224; 223, 225; 224, identifier:line; 225, integer:7; 226, block; 226, 227; 227, expression_statement; 227, 228; 228, call; 228, 229; 228, 230; 229, identifier:print; 230, argument_list; 230, 231; 230, 237; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, string:'\t'; 234, identifier:join; 235, argument_list; 235, 236; 236, identifier:line; 237, keyword_argument; 237, 238; 237, 239; 238, identifier:file; 239, identifier:radpassfile; 240, comment; 241, with_statement; 241, 242; 241, 271; 242, with_clause; 242, 243; 242, 254; 242, 255; 243, with_item; 243, 244; 244, as_pattern; 244, 245; 244, 252; 245, call; 245, 246; 245, 247; 246, identifier:open; 247, argument_list; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:radpassfile; 250, identifier:name; 251, string:'r'; 252, as_pattern_target; 252, 253; 253, identifier:radpassfile; 254, line_continuation:\; 255, with_item; 255, 256; 256, as_pattern; 256, 257; 256, 269; 257, call; 257, 258; 257, 259; 258, identifier:open; 259, argument_list; 259, 260; 259, 268; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, string:'/'; 263, identifier:join; 264, argument_list; 264, 265; 265, list:[work_dir, 'radia_parsed_filter_passing_calls.vcf']; 265, 266; 265, 267; 266, identifier:work_dir; 267, string:'radia_parsed_filter_passing_calls.vcf'; 268, string:'w'; 269, as_pattern_target; 269, 270; 270, identifier:parsedradfile; 271, block; 271, 272; 272, expression_statement; 272, 273; 273, call; 273, 274; 273, 275; 274, identifier:parse_radia_multi_alt; 275, argument_list; 275, 276; 275, 277; 276, identifier:radpassfile; 277, identifier:parsedradfile; 278, expression_statement; 278, 279; 279, assignment; 279, 280; 279, 281; 280, identifier:output_files; 281, call; 281, 282; 281, 283; 282, identifier:defaultdict; 283, argument_list; 284, for_statement; 284, 285; 284, 286; 284, 293; 285, identifier:radia_file; 286, list:[radfile.name, parsedradfile.name]; 286, 287; 286, 290; 287, attribute; 287, 288; 287, 289; 288, identifier:radfile; 289, identifier:name; 290, attribute; 290, 291; 290, 292; 291, identifier:parsedradfile; 292, identifier:name; 293, block; 293, 294; 294, expression_statement; 294, 295; 295, assignment; 295, 296; 295, 306; 296, subscript; 296, 297; 296, 298; 297, identifier:output_files; 298, call; 298, 299; 298, 304; 299, attribute; 299, 300; 299, 303; 300, attribute; 300, 301; 300, 302; 301, identifier:os; 302, identifier:path; 303, identifier:basename; 304, argument_list; 304, 305; 305, identifier:radia_file; 306, call; 306, 307; 306, 312; 307, attribute; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:job; 310, identifier:fileStore; 311, identifier:writeGlobalFile; 312, argument_list; 312, 313; 313, identifier:radia_file; 314, return_statement; 314, 315; 315, identifier:output_files | def merge_radia(job, perchrom_rvs):
"""
This module will merge the per-chromosome radia files created by spawn_radia into a genome vcf.
It will make 2 vcfs, one for PASSing non-germline calls, and one for all calls.
ARGUMENTS
1. perchrom_rvs: REFER RETURN VALUE of spawn_radia()
RETURN VALUES
1. output_files: Dict of outputs
output_files
|- radia_calls.vcf: <JSid>
+- radia_parsed_filter_passing_calls.vcf: <JSid>
This module corresponds to node 11 on the tree
"""
job.fileStore.logToMaster('Running merge_radia')
work_dir = job.fileStore.getLocalTempDir()
# We need to squash the input dict of dicts to a single dict such that it can be passed to
# get_files_from_filestore
input_files = {filename: jsid for perchrom_files in perchrom_rvs.values()
for filename, jsid in perchrom_files.items()}
input_files = get_files_from_filestore(job, input_files, work_dir,
docker=False)
chromosomes = [''.join(['chr', str(x)]) for x in range(1, 23) + ['X', 'Y']]
with open('/'.join([work_dir, 'radia_calls.vcf']), 'w') as radfile, \
open('/'.join([work_dir, 'radia_filter_passing_calls.vcf']), 'w') as radpassfile:
for chrom in chromosomes:
with open(input_files[''.join(['radia_filtered_', chrom, '.vcf'])], 'r') as filtradfile:
for line in filtradfile:
line = line.strip()
if line.startswith('#'):
if chrom == 'chr1':
print(line, file=radfile)
print(line, file=radpassfile)
continue
else:
print(line, file=radfile)
line = line.split('\t')
if line[6] == 'PASS' and 'MT=GERM' not in line[7]:
print('\t'.join(line), file=radpassfile)
# parse the PASS radia vcf for multiple alt alleles
with open(radpassfile.name, 'r') as radpassfile, \
open('/'.join([work_dir, 'radia_parsed_filter_passing_calls.vcf']),
'w') as parsedradfile:
parse_radia_multi_alt(radpassfile, parsedradfile)
output_files = defaultdict()
for radia_file in [radfile.name, parsedradfile.name]:
output_files[os.path.basename(radia_file)] = job.fileStore.writeGlobalFile(radia_file)
return output_files |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:merge_mutect; 3, parameters; 3, 4; 3, 5; 4, identifier:job; 5, identifier:perchrom_rvs; 6, block; 6, 7; 6, 9; 6, 18; 6, 28; 6, 29; 6, 30; 6, 53; 6, 65; 6, 91; 6, 336; 6, 349; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, call; 10, 11; 10, 16; 11, attribute; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:job; 14, identifier:fileStore; 15, identifier:logToMaster; 16, argument_list; 16, 17; 17, string:'Running merge_mutect'; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:work_dir; 21, call; 21, 22; 21, 27; 22, attribute; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:job; 25, identifier:fileStore; 26, identifier:getLocalTempDir; 27, argument_list; 28, comment; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:input_files; 33, dictionary_comprehension; 33, 34; 33, 37; 33, 44; 34, pair; 34, 35; 34, 36; 35, identifier:filename; 36, identifier:jsid; 37, for_in_clause; 37, 38; 37, 39; 38, identifier:perchrom_files; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:perchrom_rvs; 42, identifier:values; 43, argument_list; 44, for_in_clause; 44, 45; 44, 48; 45, pattern_list; 45, 46; 45, 47; 46, identifier:filename; 47, identifier:jsid; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:perchrom_files; 51, identifier:items; 52, argument_list; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:input_files; 56, call; 56, 57; 56, 58; 57, identifier:get_files_from_filestore; 58, argument_list; 58, 59; 58, 60; 58, 61; 58, 62; 59, identifier:job; 60, identifier:input_files; 61, identifier:work_dir; 62, keyword_argument; 62, 63; 62, 64; 63, identifier:docker; 64, False; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:chromosomes; 68, list_comprehension; 68, 69; 68, 80; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, string:''; 72, identifier:join; 73, argument_list; 73, 74; 74, list:['chr', str(x)]; 74, 75; 74, 76; 75, string:'chr'; 76, call; 76, 77; 76, 78; 77, identifier:str; 78, argument_list; 78, 79; 79, identifier:x; 80, for_in_clause; 80, 81; 80, 82; 81, identifier:x; 82, binary_operator:+; 82, 83; 82, 88; 83, call; 83, 84; 83, 85; 84, identifier:range; 85, argument_list; 85, 86; 85, 87; 86, integer:1; 87, integer:23; 88, list:['X', 'Y']; 88, 89; 88, 90; 89, string:'X'; 90, string:'Y'; 91, with_statement; 91, 92; 91, 143; 92, with_clause; 92, 93; 92, 109; 92, 110; 92, 126; 92, 127; 93, with_item; 93, 94; 94, as_pattern; 94, 95; 94, 107; 95, call; 95, 96; 95, 97; 96, identifier:open; 97, argument_list; 97, 98; 97, 106; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, string:'/'; 101, identifier:join; 102, argument_list; 102, 103; 103, list:[work_dir, 'mutect_calls.vcf']; 103, 104; 103, 105; 104, identifier:work_dir; 105, string:'mutect_calls.vcf'; 106, string:'w'; 107, as_pattern_target; 107, 108; 108, identifier:mutvcf; 109, line_continuation:\; 110, with_item; 110, 111; 111, as_pattern; 111, 112; 111, 124; 112, call; 112, 113; 112, 114; 113, identifier:open; 114, argument_list; 114, 115; 114, 123; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, string:'/'; 118, identifier:join; 119, argument_list; 119, 120; 120, list:[work_dir, 'mutect_calls.out']; 120, 121; 120, 122; 121, identifier:work_dir; 122, string:'mutect_calls.out'; 123, string:'w'; 124, as_pattern_target; 124, 125; 125, identifier:mutout; 126, line_continuation:\; 127, with_item; 127, 128; 128, as_pattern; 128, 129; 128, 141; 129, call; 129, 130; 129, 131; 130, identifier:open; 131, argument_list; 131, 132; 131, 140; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, string:'/'; 135, identifier:join; 136, argument_list; 136, 137; 137, list:[work_dir, 'mutect_passing_calls.vcf']; 137, 138; 137, 139; 138, identifier:work_dir; 139, string:'mutect_passing_calls.vcf'; 140, string:'w'; 141, as_pattern_target; 141, 142; 142, identifier:mutpassvcf; 143, block; 143, 144; 143, 148; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:out_header_not_printed; 147, True; 148, for_statement; 148, 149; 148, 150; 148, 151; 149, identifier:chrom; 150, identifier:chromosomes; 151, block; 151, 152; 151, 255; 152, with_statement; 152, 153; 152, 173; 153, with_clause; 153, 154; 154, with_item; 154, 155; 155, as_pattern; 155, 156; 155, 171; 156, call; 156, 157; 156, 158; 157, identifier:open; 158, argument_list; 158, 159; 158, 170; 159, subscript; 159, 160; 159, 161; 160, identifier:input_files; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, string:''; 164, identifier:join; 165, argument_list; 165, 166; 166, list:['mutect_', chrom, '.vcf']; 166, 167; 166, 168; 166, 169; 167, string:'mutect_'; 168, identifier:chrom; 169, string:'.vcf'; 170, string:'r'; 171, as_pattern_target; 171, 172; 172, identifier:mutfile; 173, block; 173, 174; 174, for_statement; 174, 175; 174, 176; 174, 177; 175, identifier:line; 176, identifier:mutfile; 177, block; 177, 178; 177, 186; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:line; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:line; 184, identifier:strip; 185, argument_list; 186, if_statement; 186, 187; 186, 193; 186, 216; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:line; 190, identifier:startswith; 191, argument_list; 191, 192; 192, string:'#'; 193, block; 193, 194; 193, 215; 194, if_statement; 194, 195; 194, 198; 195, comparison_operator:==; 195, 196; 195, 197; 196, identifier:chrom; 197, string:'chr1'; 198, block; 198, 199; 198, 207; 199, expression_statement; 199, 200; 200, call; 200, 201; 200, 202; 201, identifier:print; 202, argument_list; 202, 203; 202, 204; 203, identifier:line; 204, keyword_argument; 204, 205; 204, 206; 205, identifier:file; 206, identifier:mutvcf; 207, expression_statement; 207, 208; 208, call; 208, 209; 208, 210; 209, identifier:print; 210, argument_list; 210, 211; 210, 212; 211, identifier:line; 212, keyword_argument; 212, 213; 212, 214; 213, identifier:file; 214, identifier:mutpassvcf; 215, continue_statement; 216, else_clause; 216, 217; 217, block; 217, 218; 217, 226; 217, 235; 218, expression_statement; 218, 219; 219, call; 219, 220; 219, 221; 220, identifier:print; 221, argument_list; 221, 222; 221, 223; 222, identifier:line; 223, keyword_argument; 223, 224; 223, 225; 224, identifier:file; 225, identifier:mutvcf; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 229; 228, identifier:line; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:line; 232, identifier:split; 233, argument_list; 233, 234; 234, string:'\t'; 235, if_statement; 235, 236; 235, 241; 236, comparison_operator:!=; 236, 237; 236, 240; 237, subscript; 237, 238; 237, 239; 238, identifier:line; 239, integer:6; 240, string:'REJECT'; 241, block; 241, 242; 242, expression_statement; 242, 243; 243, call; 243, 244; 243, 245; 244, identifier:print; 245, argument_list; 245, 246; 245, 252; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, string:'\t'; 249, identifier:join; 250, argument_list; 250, 251; 251, identifier:line; 252, keyword_argument; 252, 253; 252, 254; 253, identifier:file; 254, identifier:mutpassvcf; 255, with_statement; 255, 256; 255, 276; 256, with_clause; 256, 257; 257, with_item; 257, 258; 258, as_pattern; 258, 259; 258, 274; 259, call; 259, 260; 259, 261; 260, identifier:open; 261, argument_list; 261, 262; 261, 273; 262, subscript; 262, 263; 262, 264; 263, identifier:input_files; 264, call; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, string:''; 267, identifier:join; 268, argument_list; 268, 269; 269, list:['mutect_', chrom,
'.out']; 269, 270; 269, 271; 269, 272; 270, string:'mutect_'; 271, identifier:chrom; 272, string:'.out'; 273, string:'r'; 274, as_pattern_target; 274, 275; 275, identifier:mutfile; 276, block; 276, 277; 277, for_statement; 277, 278; 277, 279; 277, 280; 278, identifier:line; 279, identifier:mutfile; 280, block; 280, 281; 280, 289; 281, expression_statement; 281, 282; 282, assignment; 282, 283; 282, 284; 283, identifier:line; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:line; 287, identifier:strip; 288, argument_list; 289, if_statement; 289, 290; 289, 296; 289, 311; 289, 326; 290, call; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:line; 293, identifier:startswith; 294, argument_list; 294, 295; 295, string:'#'; 296, block; 296, 297; 296, 310; 297, if_statement; 297, 298; 297, 301; 298, comparison_operator:==; 298, 299; 298, 300; 299, identifier:chrom; 300, string:'chr1'; 301, block; 301, 302; 302, expression_statement; 302, 303; 303, call; 303, 304; 303, 305; 304, identifier:print; 305, argument_list; 305, 306; 305, 307; 306, identifier:line; 307, keyword_argument; 307, 308; 307, 309; 308, identifier:file; 309, identifier:mutout; 310, continue_statement; 311, elif_clause; 311, 312; 311, 313; 312, identifier:out_header_not_printed; 313, block; 313, 314; 313, 322; 314, expression_statement; 314, 315; 315, call; 315, 316; 315, 317; 316, identifier:print; 317, argument_list; 317, 318; 317, 319; 318, identifier:line; 319, keyword_argument; 319, 320; 319, 321; 320, identifier:file; 321, identifier:mutout; 322, expression_statement; 322, 323; 323, assignment; 323, 324; 323, 325; 324, identifier:out_header_not_printed; 325, False; 326, else_clause; 326, 327; 327, block; 327, 328; 328, expression_statement; 328, 329; 329, call; 329, 330; 329, 331; 330, identifier:print; 331, argument_list; 331, 332; 331, 333; 332, identifier:line; 333, keyword_argument; 333, 334; 333, 335; 334, identifier:file; 335, identifier:mutout; 336, expression_statement; 336, 337; 337, assignment; 337, 338; 337, 339; 338, identifier:output_file; 339, call; 339, 340; 339, 345; 340, attribute; 340, 341; 340, 344; 341, attribute; 341, 342; 341, 343; 342, identifier:job; 343, identifier:fileStore; 344, identifier:writeGlobalFile; 345, argument_list; 345, 346; 346, attribute; 346, 347; 346, 348; 347, identifier:mutpassvcf; 348, identifier:name; 349, return_statement; 349, 350; 350, identifier:output_file | def merge_mutect(job, perchrom_rvs):
"""
This module will merge the per-chromosome mutect files created by spawn_mutect into a genome
vcf. It will make 2 vcfs, one for PASSing non-germline calls, and one for all calls.
ARGUMENTS
1. perchrom_rvs: REFER RETURN VALUE of spawn_mutect()
RETURN VALUES
1. output_files: <JSid for mutect_passing_calls.vcf>
This module corresponds to node 11 on the tree
"""
job.fileStore.logToMaster('Running merge_mutect')
work_dir = job.fileStore.getLocalTempDir()
# We need to squash the input dict of dicts to a single dict such that it can be passed to
# get_files_from_filestore
input_files = {filename: jsid for perchrom_files in perchrom_rvs.values()
for filename, jsid in perchrom_files.items()}
input_files = get_files_from_filestore(job, input_files, work_dir, docker=False)
chromosomes = [''.join(['chr', str(x)]) for x in range(1, 23) + ['X', 'Y']]
with open('/'.join([work_dir, 'mutect_calls.vcf']), 'w') as mutvcf, \
open('/'.join([work_dir, 'mutect_calls.out']), 'w') as mutout, \
open('/'.join([work_dir, 'mutect_passing_calls.vcf']), 'w') as mutpassvcf:
out_header_not_printed = True
for chrom in chromosomes:
with open(input_files[''.join(['mutect_', chrom, '.vcf'])], 'r') as mutfile:
for line in mutfile:
line = line.strip()
if line.startswith('#'):
if chrom == 'chr1':
print(line, file=mutvcf)
print(line, file=mutpassvcf)
continue
else:
print(line, file=mutvcf)
line = line.split('\t')
if line[6] != 'REJECT':
print('\t'.join(line), file=mutpassvcf)
with open(input_files[''.join(['mutect_', chrom,
'.out'])], 'r') as mutfile:
for line in mutfile:
line = line.strip()
if line.startswith('#'):
if chrom == 'chr1':
print(line, file=mutout)
continue
elif out_header_not_printed:
print(line, file=mutout)
out_header_not_printed = False
else:
print(line, file=mutout)
output_file = job.fileStore.writeGlobalFile(mutpassvcf.name)
return output_file |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:run_mutation_aggregator; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 4, identifier:job; 5, identifier:fusion_output; 6, identifier:radia_output; 7, identifier:mutect_output; 8, identifier:indel_output; 9, identifier:univ_options; 10, block; 10, 11; 10, 13; 10, 26; 10, 36; 10, 54; 10, 66; 10, 67; 10, 74; 10, 81; 10, 82; 10, 88; 10, 96; 10, 216; 10, 217; 10, 218; 10, 226; 10, 234; 10, 247; 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:job; 18, identifier:fileStore; 19, identifier:logToMaster; 20, argument_list; 20, 21; 21, binary_operator:%; 21, 22; 21, 23; 22, string:'Aggregating mutations for %s'; 23, subscript; 23, 24; 23, 25; 24, identifier:univ_options; 25, string:'patient'; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:work_dir; 29, call; 29, 30; 29, 35; 30, attribute; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:job; 33, identifier:fileStore; 34, identifier:getLocalTempDir; 35, argument_list; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:input_files; 39, dictionary; 39, 40; 39, 43; 39, 48; 39, 51; 40, pair; 40, 41; 40, 42; 41, string:'mutect.vcf'; 42, identifier:mutect_output; 43, pair; 43, 44; 43, 45; 44, string:'radia.vcf'; 45, subscript; 45, 46; 45, 47; 46, identifier:radia_output; 47, string:'radia_parsed_filter_passing_calls.vcf'; 48, pair; 48, 49; 48, 50; 49, string:'indel.vcf'; 50, identifier:indel_output; 51, pair; 51, 52; 51, 53; 52, string:'fusion.vcf'; 53, identifier:fusion_output; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:input_files; 57, call; 57, 58; 57, 59; 58, identifier:get_files_from_filestore; 59, argument_list; 59, 60; 59, 61; 59, 62; 59, 63; 60, identifier:job; 61, identifier:input_files; 62, identifier:work_dir; 63, keyword_argument; 63, 64; 63, 65; 64, identifier:docker; 65, False; 66, comment; 67, expression_statement; 67, 68; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:input_files; 71, identifier:pop; 72, argument_list; 72, 73; 73, string:'indel.vcf'; 74, expression_statement; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:input_files; 78, identifier:pop; 79, argument_list; 79, 80; 80, string:'fusion.vcf'; 81, comment; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:vcf_file; 85, call; 85, 86; 85, 87; 86, identifier:defaultdict; 87, argument_list; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:mutcallers; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:input_files; 94, identifier:keys; 95, argument_list; 96, with_statement; 96, 97; 96, 118; 97, with_clause; 97, 98; 98, with_item; 98, 99; 99, as_pattern; 99, 100; 99, 116; 100, call; 100, 101; 100, 102; 101, identifier:open; 102, argument_list; 102, 103; 102, 115; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, string:''; 106, identifier:join; 107, argument_list; 107, 108; 108, list:[work_dir, '/', univ_options['patient'], '_merged_mutations.vcf']; 108, 109; 108, 110; 108, 111; 108, 114; 109, identifier:work_dir; 110, string:'/'; 111, subscript; 111, 112; 111, 113; 112, identifier:univ_options; 113, string:'patient'; 114, string:'_merged_mutations.vcf'; 115, string:'w'; 116, as_pattern_target; 116, 117; 117, identifier:merged_mut_file; 118, block; 118, 119; 119, for_statement; 119, 120; 119, 121; 119, 122; 120, identifier:mut_caller; 121, identifier:mutcallers; 122, block; 122, 123; 122, 132; 122, 140; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:caller; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:mut_caller; 129, identifier:rstrip; 130, argument_list; 130, 131; 131, string:'.vcf'; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 137; 134, subscript; 134, 135; 134, 136; 135, identifier:vcf_file; 136, identifier:caller; 137, call; 137, 138; 137, 139; 138, identifier:defaultdict; 139, argument_list; 140, with_statement; 140, 141; 140, 153; 141, with_clause; 141, 142; 142, with_item; 142, 143; 143, as_pattern; 143, 144; 143, 151; 144, call; 144, 145; 144, 146; 145, identifier:open; 146, argument_list; 146, 147; 146, 150; 147, subscript; 147, 148; 147, 149; 148, identifier:input_files; 149, identifier:mut_caller; 150, string:'r'; 151, as_pattern_target; 151, 152; 152, identifier:mutfile; 153, block; 153, 154; 154, for_statement; 154, 155; 154, 156; 154, 157; 155, identifier:line; 156, identifier:mutfile; 157, block; 157, 158; 157, 184; 157, 196; 158, if_statement; 158, 159; 158, 165; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:line; 162, identifier:startswith; 163, argument_list; 163, 164; 164, string:'#'; 165, block; 165, 166; 165, 183; 166, if_statement; 166, 167; 166, 170; 167, comparison_operator:==; 167, 168; 167, 169; 168, identifier:caller; 169, string:'radia'; 170, block; 170, 171; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 174; 173, identifier:print; 174, argument_list; 174, 175; 174, 180; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:line; 178, identifier:strip; 179, argument_list; 180, keyword_argument; 180, 181; 180, 182; 181, identifier:file; 182, identifier:merged_mut_file; 183, continue_statement; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:line; 187, call; 187, 188; 187, 195; 188, attribute; 188, 189; 188, 194; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:line; 192, identifier:strip; 193, argument_list; 194, identifier:split; 195, argument_list; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 215; 198, subscript; 198, 199; 198, 202; 199, subscript; 199, 200; 199, 201; 200, identifier:vcf_file; 201, identifier:caller; 202, tuple; 202, 203; 202, 206; 202, 209; 202, 212; 203, subscript; 203, 204; 203, 205; 204, identifier:line; 205, integer:0; 206, subscript; 206, 207; 206, 208; 207, identifier:line; 208, integer:1; 209, subscript; 209, 210; 209, 211; 210, identifier:line; 211, integer:3; 212, subscript; 212, 213; 212, 214; 213, identifier:line; 214, integer:4; 215, identifier:line; 216, comment; 217, comment; 218, expression_statement; 218, 219; 219, call; 219, 220; 219, 221; 220, identifier:merge_vcfs; 221, argument_list; 221, 222; 221, 223; 222, identifier:vcf_file; 223, attribute; 223, 224; 223, 225; 224, identifier:merged_mut_file; 225, identifier:name; 226, expression_statement; 226, 227; 227, call; 227, 228; 227, 229; 228, identifier:export_results; 229, argument_list; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:merged_mut_file; 232, identifier:name; 233, identifier:univ_options; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 237; 236, identifier:output_file; 237, call; 237, 238; 237, 243; 238, attribute; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:job; 241, identifier:fileStore; 242, identifier:writeGlobalFile; 243, argument_list; 243, 244; 244, attribute; 244, 245; 244, 246; 245, identifier:merged_mut_file; 246, identifier:name; 247, return_statement; 247, 248; 248, identifier:output_file | def run_mutation_aggregator(job, fusion_output, radia_output, mutect_output, indel_output,
univ_options):
"""
This module will aggregate all the mutations called in the previous steps and will then call
snpeff on the results.
ARGUMENTS
1. fusion_output: <JSid for vcf generated by the fusion caller>
2. radia_output: <JSid for vcf generated by radia>
3. mutect_output: <JSid for vcf generated by mutect>
4. indel_output: <JSid for vcf generated by the indel caller>
RETURN VALUES
1. output_file: <JSid for merged vcf>
This module corresponds to node 15 on the tree
"""
job.fileStore.logToMaster('Aggregating mutations for %s' % univ_options['patient'])
work_dir = job.fileStore.getLocalTempDir()
input_files = {
'mutect.vcf': mutect_output,
'radia.vcf': radia_output['radia_parsed_filter_passing_calls.vcf'],
'indel.vcf': indel_output,
'fusion.vcf': fusion_output}
input_files = get_files_from_filestore(job, input_files, work_dir, docker=False)
# Modify these once INDELs and Fusions are implemented
input_files.pop('indel.vcf')
input_files.pop('fusion.vcf')
# read files into memory
vcf_file = defaultdict()
mutcallers = input_files.keys()
with open(''.join([work_dir, '/', univ_options['patient'], '_merged_mutations.vcf']),
'w') as merged_mut_file:
for mut_caller in mutcallers:
caller = mut_caller.rstrip('.vcf')
vcf_file[caller] = defaultdict()
with open(input_files[mut_caller], 'r') as mutfile:
for line in mutfile:
if line.startswith('#'):
if caller == 'radia':
print(line.strip(), file=merged_mut_file)
continue
line = line.strip().split()
vcf_file[caller][(line[0], line[1], line[3], line[4])] = line
# This method can be changed in the future to incorporate more callers and
# fancier integration methods
merge_vcfs(vcf_file, merged_mut_file.name)
export_results(merged_mut_file.name, univ_options)
output_file = job.fileStore.writeGlobalFile(merged_mut_file.name)
return output_file |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:merge_phlat_calls; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:job; 5, identifier:tumor_phlat; 6, identifier:normal_phlat; 7, identifier:rna_phlat; 8, block; 8, 9; 8, 11; 8, 20; 8, 30; 8, 43; 8, 52; 8, 134; 8, 135; 8, 289; 8, 295; 8, 322; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, call; 12, 13; 12, 18; 13, attribute; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:job; 16, identifier:fileStore; 17, identifier:logToMaster; 18, argument_list; 18, 19; 19, string:'Merging Phlat calls'; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:work_dir; 23, call; 23, 24; 23, 29; 24, attribute; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:job; 27, identifier:fileStore; 28, identifier:getLocalTempDir; 29, argument_list; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:input_files; 33, dictionary; 33, 34; 33, 37; 33, 40; 34, pair; 34, 35; 34, 36; 35, string:'tumor_dna'; 36, identifier:tumor_phlat; 37, pair; 37, 38; 37, 39; 38, string:'normal_dna'; 39, identifier:normal_phlat; 40, pair; 40, 41; 40, 42; 41, string:'tumor_rna'; 42, identifier:rna_phlat; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:input_files; 46, call; 46, 47; 46, 48; 47, identifier:get_files_from_filestore; 48, argument_list; 48, 49; 48, 50; 48, 51; 49, identifier:job; 50, identifier:input_files; 51, identifier:work_dir; 52, with_statement; 52, 53; 52, 89; 52, 90; 53, with_clause; 53, 54; 53, 65; 53, 66; 53, 77; 53, 78; 54, with_item; 54, 55; 55, as_pattern; 55, 56; 55, 63; 56, call; 56, 57; 56, 58; 57, identifier:open; 58, argument_list; 58, 59; 58, 62; 59, subscript; 59, 60; 59, 61; 60, identifier:input_files; 61, string:'tumor_dna'; 62, string:'r'; 63, as_pattern_target; 63, 64; 64, identifier:td_file; 65, line_continuation:\; 66, with_item; 66, 67; 67, as_pattern; 67, 68; 67, 75; 68, call; 68, 69; 68, 70; 69, identifier:open; 70, argument_list; 70, 71; 70, 74; 71, subscript; 71, 72; 71, 73; 72, identifier:input_files; 73, string:'normal_dna'; 74, string:'r'; 75, as_pattern_target; 75, 76; 76, identifier:nd_file; 77, line_continuation:\; 78, with_item; 78, 79; 79, as_pattern; 79, 80; 79, 87; 80, call; 80, 81; 80, 82; 81, identifier:open; 82, argument_list; 82, 83; 82, 86; 83, subscript; 83, 84; 83, 85; 84, identifier:input_files; 85, string:'tumor_rna'; 86, string:'r'; 87, as_pattern_target; 87, 88; 88, identifier:tr_file; 89, comment; 90, block; 90, 91; 90, 119; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:mhc_alleles; 94, dictionary; 94, 95; 94, 98; 94, 101; 94, 104; 94, 107; 94, 110; 94, 113; 94, 116; 95, pair; 95, 96; 95, 97; 96, string:'HLA_A'; 97, list:[]; 98, pair; 98, 99; 98, 100; 99, string:'HLA_B'; 100, list:[]; 101, pair; 101, 102; 101, 103; 102, string:'HLA_C'; 103, list:[]; 104, pair; 104, 105; 104, 106; 105, string:'HLA_DPA'; 106, list:[]; 107, pair; 107, 108; 107, 109; 108, string:'HLA_DQA'; 109, list:[]; 110, pair; 110, 111; 110, 112; 111, string:'HLA_DPB'; 112, list:[]; 113, pair; 113, 114; 113, 115; 114, string:'HLA_DQB'; 115, list:[]; 116, pair; 116, 117; 116, 118; 117, string:'HLA_DRB'; 118, list:[]; 119, for_statement; 119, 120; 119, 121; 119, 125; 120, identifier:phlatfile; 121, expression_list; 121, 122; 121, 123; 121, 124; 122, identifier:td_file; 123, identifier:nd_file; 124, identifier:tr_file; 125, block; 125, 126; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:mhc_alleles; 129, call; 129, 130; 129, 131; 130, identifier:parse_phlat_file; 131, argument_list; 131, 132; 131, 133; 132, identifier:phlatfile; 133, identifier:mhc_alleles; 134, comment; 135, with_statement; 135, 136; 135, 172; 136, with_clause; 136, 137; 136, 154; 136, 155; 137, with_item; 137, 138; 138, as_pattern; 138, 139; 138, 152; 139, call; 139, 140; 139, 141; 140, identifier:open; 141, argument_list; 141, 142; 141, 151; 142, call; 142, 143; 142, 148; 143, attribute; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:os; 146, identifier:path; 147, identifier:join; 148, argument_list; 148, 149; 148, 150; 149, identifier:work_dir; 150, string:'mhci_alleles.list'; 151, string:'w'; 152, as_pattern_target; 152, 153; 153, identifier:mhci_file; 154, line_continuation:\; 155, with_item; 155, 156; 156, as_pattern; 156, 157; 156, 170; 157, call; 157, 158; 157, 159; 158, identifier:open; 159, argument_list; 159, 160; 159, 169; 160, call; 160, 161; 160, 166; 161, attribute; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:os; 164, identifier:path; 165, identifier:join; 166, argument_list; 166, 167; 166, 168; 167, identifier:work_dir; 168, string:'mhcii_alleles.list'; 169, string:'w'; 170, as_pattern_target; 170, 171; 171, identifier:mhcii_file; 172, block; 172, 173; 172, 213; 172, 222; 172, 246; 172, 255; 172, 264; 173, for_statement; 173, 174; 173, 175; 173, 179; 174, identifier:mhci_group; 175, list:['HLA_A', 'HLA_B', 'HLA_C']; 175, 176; 175, 177; 175, 178; 176, string:'HLA_A'; 177, string:'HLA_B'; 178, string:'HLA_C'; 179, block; 179, 180; 179, 189; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:mpa; 183, call; 183, 184; 183, 185; 184, identifier:most_probable_alleles; 185, argument_list; 185, 186; 186, subscript; 186, 187; 186, 188; 187, identifier:mhc_alleles; 188, identifier:mhci_group; 189, expression_statement; 189, 190; 190, call; 190, 191; 190, 192; 191, identifier:print; 192, argument_list; 192, 193; 192, 210; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, string:'\n'; 196, identifier:join; 197, argument_list; 197, 198; 198, list_comprehension; 198, 199; 198, 207; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, string:''; 202, identifier:join; 203, argument_list; 203, 204; 204, list:['HLA-', x]; 204, 205; 204, 206; 205, string:'HLA-'; 206, identifier:x; 207, for_in_clause; 207, 208; 207, 209; 208, identifier:x; 209, identifier:mpa; 210, keyword_argument; 210, 211; 210, 212; 211, identifier:file; 212, identifier:mhci_file; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 216; 215, identifier:drb_mpa; 216, call; 216, 217; 216, 218; 217, identifier:most_probable_alleles; 218, argument_list; 218, 219; 219, subscript; 219, 220; 219, 221; 220, identifier:mhc_alleles; 221, string:'HLA_DRB'; 222, expression_statement; 222, 223; 223, call; 223, 224; 223, 225; 224, identifier:print; 225, argument_list; 225, 226; 225, 243; 226, call; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, string:'\n'; 229, identifier:join; 230, argument_list; 230, 231; 231, list_comprehension; 231, 232; 231, 240; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, string:''; 235, identifier:join; 236, argument_list; 236, 237; 237, list:['HLA-', x]; 237, 238; 237, 239; 238, string:'HLA-'; 239, identifier:x; 240, for_in_clause; 240, 241; 240, 242; 241, identifier:x; 242, identifier:drb_mpa; 243, keyword_argument; 243, 244; 243, 245; 244, identifier:file; 245, identifier:mhcii_file; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:dqa_mpa; 249, call; 249, 250; 249, 251; 250, identifier:most_probable_alleles; 251, argument_list; 251, 252; 252, subscript; 252, 253; 252, 254; 253, identifier:mhc_alleles; 254, string:'HLA_DQA'; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 258; 257, identifier:dqb_mpa; 258, call; 258, 259; 258, 260; 259, identifier:most_probable_alleles; 260, argument_list; 260, 261; 261, subscript; 261, 262; 261, 263; 262, identifier:mhc_alleles; 263, string:'HLA_DQB'; 264, for_statement; 264, 265; 264, 266; 264, 267; 265, identifier:dqa_allele; 266, identifier:dqa_mpa; 267, block; 267, 268; 268, for_statement; 268, 269; 268, 270; 268, 271; 269, identifier:dqb_allele; 270, identifier:dqb_mpa; 271, block; 271, 272; 272, expression_statement; 272, 273; 273, call; 273, 274; 273, 275; 274, identifier:print; 275, argument_list; 275, 276; 275, 286; 276, call; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, string:''; 279, identifier:join; 280, argument_list; 280, 281; 281, list:['HLA-', dqa_allele, '/', dqb_allele]; 281, 282; 281, 283; 281, 284; 281, 285; 282, string:'HLA-'; 283, identifier:dqa_allele; 284, string:'/'; 285, identifier:dqb_allele; 286, keyword_argument; 286, 287; 286, 288; 287, identifier:file; 288, identifier:mhcii_file; 289, expression_statement; 289, 290; 290, assignment; 290, 291; 290, 292; 291, identifier:output_files; 292, call; 292, 293; 292, 294; 293, identifier:defaultdict; 294, argument_list; 295, for_statement; 295, 296; 295, 297; 295, 300; 296, identifier:allele_file; 297, list:['mhci_alleles.list', 'mhcii_alleles.list']; 297, 298; 297, 299; 298, string:'mhci_alleles.list'; 299, string:'mhcii_alleles.list'; 300, block; 300, 301; 301, expression_statement; 301, 302; 302, assignment; 302, 303; 302, 306; 303, subscript; 303, 304; 303, 305; 304, identifier:output_files; 305, identifier:allele_file; 306, call; 306, 307; 306, 312; 307, attribute; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:job; 310, identifier:fileStore; 311, identifier:writeGlobalFile; 312, argument_list; 312, 313; 313, call; 313, 314; 313, 319; 314, attribute; 314, 315; 314, 318; 315, attribute; 315, 316; 315, 317; 316, identifier:os; 317, identifier:path; 318, identifier:join; 319, argument_list; 319, 320; 319, 321; 320, identifier:work_dir; 321, identifier:allele_file; 322, return_statement; 322, 323; 323, identifier:output_files | def merge_phlat_calls(job, tumor_phlat, normal_phlat, rna_phlat):
"""
This module will merge the results form running PHLAT on the 3 input fastq
pairs.
ARGUMENTS
1. tumor_phlat: <JSid for tumor DNA called alleles>
2. normal_phlat: <JSid for normal DNA called alleles>
3. rna_phlat: <JSid for tumor RNA called alleles>
RETURN VALUES
1. output_files: Dict of JSids for consensus MHCI and MHCII alleles
output_files
|- 'mhci_alleles.list': <JSid>
+- 'mhcii_alleles.list': <JSid>
This module corresponds to node 14 on the tree
"""
job.fileStore.logToMaster('Merging Phlat calls')
work_dir = job.fileStore.getLocalTempDir()
input_files = {
'tumor_dna': tumor_phlat,
'normal_dna': normal_phlat,
'tumor_rna': rna_phlat}
input_files = get_files_from_filestore(job, input_files, work_dir)
with open(input_files['tumor_dna'], 'r') as td_file, \
open(input_files['normal_dna'], 'r') as nd_file, \
open(input_files['tumor_rna'], 'r') as tr_file:
# TODO: Could this be a defautdict?
mhc_alleles = {'HLA_A': [], 'HLA_B': [], 'HLA_C': [], 'HLA_DPA': [], 'HLA_DQA': [],
'HLA_DPB': [], 'HLA_DQB': [], 'HLA_DRB': []}
for phlatfile in td_file, nd_file, tr_file:
mhc_alleles = parse_phlat_file(phlatfile, mhc_alleles)
# Get most probable alleles for each allele group and print to output
with open(os.path.join(work_dir, 'mhci_alleles.list'), 'w') as mhci_file, \
open(os.path.join(work_dir, 'mhcii_alleles.list'), 'w') as mhcii_file:
for mhci_group in ['HLA_A', 'HLA_B', 'HLA_C']:
mpa = most_probable_alleles(mhc_alleles[mhci_group])
print('\n'.join([''.join(['HLA-', x]) for x in mpa]), file=mhci_file)
drb_mpa = most_probable_alleles(mhc_alleles['HLA_DRB'])
print('\n'.join([''.join(['HLA-', x]) for x in drb_mpa]), file=mhcii_file)
dqa_mpa = most_probable_alleles(mhc_alleles['HLA_DQA'])
dqb_mpa = most_probable_alleles(mhc_alleles['HLA_DQB'])
for dqa_allele in dqa_mpa:
for dqb_allele in dqb_mpa:
print(''.join(['HLA-', dqa_allele, '/', dqb_allele]), file=mhcii_file)
output_files = defaultdict()
for allele_file in ['mhci_alleles.list', 'mhcii_alleles.list']:
output_files[allele_file] = job.fileStore.writeGlobalFile(os.path.join(work_dir,
allele_file))
return output_files |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 19; 2, function_name:docker_call; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 3, 16; 4, identifier:tool; 5, identifier:tool_parameters; 6, identifier:work_dir; 7, default_parameter; 7, 8; 7, 9; 8, identifier:java_opts; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:outfile; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:dockerhub; 15, string:'aarjunrao'; 16, default_parameter; 16, 17; 16, 18; 17, identifier:interactive; 18, False; 19, block; 19, 20; 19, 22; 19, 23; 19, 24; 19, 51; 19, 52; 19, 65; 19, 66; 19, 98; 19, 99; 19, 105; 19, 114; 19, 147; 19, 215; 19, 216; 19, 254; 19, 267; 20, expression_statement; 20, 21; 21, comment; 22, comment; 23, comment; 24, if_statement; 24, 25; 24, 26; 25, identifier:outfile; 26, block; 26, 27; 26, 34; 26, 45; 27, assert_statement; 27, 28; 27, 33; 28, call; 28, 29; 28, 30; 29, identifier:isinstance; 30, argument_list; 30, 31; 30, 32; 31, identifier:outfile; 32, identifier:file; 33, string:'outfile was not passsed a file'; 34, assert_statement; 34, 35; 34, 44; 35, comparison_operator:in; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:outfile; 38, identifier:mode; 39, list:['w', 'a', 'wb', 'ab']; 39, 40; 39, 41; 39, 42; 39, 43; 40, string:'w'; 41, string:'a'; 42, string:'wb'; 43, string:'ab'; 44, string:'outfile not writeable'; 45, assert_statement; 45, 46; 45, 50; 46, not_operator; 46, 47; 47, attribute; 47, 48; 47, 49; 48, identifier:outfile; 49, identifier:closed; 50, string:'outfile is closed'; 51, comment; 52, if_statement; 52, 53; 52, 54; 52, 59; 53, identifier:interactive; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:interactive; 58, string:'-i'; 59, else_clause; 59, 60; 60, block; 60, 61; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:interactive; 64, string:''; 65, comment; 66, if_statement; 66, 67; 66, 70; 66, 82; 66, 83; 67, comparison_operator:in; 67, 68; 67, 69; 68, string:':'; 69, identifier:tool; 70, block; 70, 71; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:docker_tool; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, string:'/'; 77, identifier:join; 78, argument_list; 78, 79; 79, list:[dockerhub, tool]; 79, 80; 79, 81; 80, identifier:dockerhub; 81, identifier:tool; 82, comment; 83, else_clause; 83, 84; 84, block; 84, 85; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:docker_tool; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, string:''; 91, identifier:join; 92, argument_list; 92, 93; 93, list:[dockerhub, '/', tool, ':latest']; 93, 94; 93, 95; 93, 96; 93, 97; 94, identifier:dockerhub; 95, string:'/'; 96, identifier:tool; 97, string:':latest'; 98, comment; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:call; 102, list:['docker', 'images']; 102, 103; 102, 104; 103, string:'docker'; 104, string:'images'; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:dimg_rv; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:subprocess; 111, identifier:check_output; 112, argument_list; 112, 113; 113, identifier:call; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:existing_images; 117, list_comprehension; 117, 118; 117, 133; 117, 140; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, string:':'; 121, identifier:join; 122, argument_list; 122, 123; 123, subscript; 123, 124; 123, 129; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:x; 127, identifier:split; 128, argument_list; 129, slice; 129, 130; 129, 131; 129, 132; 130, integer:0; 131, colon; 132, integer:2; 133, for_in_clause; 133, 134; 133, 135; 134, identifier:x; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:dimg_rv; 138, identifier:splitlines; 139, argument_list; 140, if_clause; 140, 141; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:x; 144, identifier:startswith; 145, argument_list; 145, 146; 146, identifier:dockerhub; 147, if_statement; 147, 148; 147, 151; 148, comparison_operator:not; 148, 149; 148, 150; 149, identifier:docker_tool; 150, identifier:existing_images; 151, block; 151, 152; 152, try_statement; 152, 153; 152, 177; 152, 205; 153, block; 153, 154; 153, 170; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:call; 157, call; 157, 158; 157, 169; 158, attribute; 158, 159; 158, 168; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, string:' '; 162, identifier:join; 163, argument_list; 163, 164; 164, list:['docker', 'pull', docker_tool]; 164, 165; 164, 166; 164, 167; 165, string:'docker'; 166, string:'pull'; 167, identifier:docker_tool; 168, identifier:split; 169, argument_list; 170, expression_statement; 170, 171; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:subprocess; 174, identifier:check_call; 175, argument_list; 175, 176; 176, identifier:call; 177, except_clause; 177, 178; 177, 184; 178, as_pattern; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:subprocess; 181, identifier:CalledProcessError; 182, as_pattern_target; 182, 183; 183, identifier:err; 184, block; 184, 185; 185, raise_statement; 185, 186; 186, call; 186, 187; 186, 188; 187, identifier:RuntimeError; 188, argument_list; 188, 189; 189, binary_operator:+; 189, 190; 189, 197; 190, binary_operator:+; 190, 191; 190, 192; 191, string:'docker command returned a non-zero exit status '; 192, binary_operator:%; 192, 193; 192, 194; 193, string:'(%s)'; 194, attribute; 194, 195; 194, 196; 195, identifier:err; 196, identifier:returncode; 197, binary_operator:%; 197, 198; 197, 199; 198, string:'for command \"%s\"'; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, string:' '; 202, identifier:join; 203, argument_list; 203, 204; 204, identifier:call; 205, except_clause; 205, 206; 205, 207; 206, identifier:OSError; 207, block; 207, 208; 208, raise_statement; 208, 209; 209, call; 209, 210; 209, 211; 210, identifier:RuntimeError; 211, argument_list; 211, 212; 212, binary_operator:+; 212, 213; 212, 214; 213, string:'docker not found on system. Install on all'; 214, string:' nodes.'; 215, comment; 216, if_statement; 216, 217; 216, 218; 216, 239; 217, identifier:java_opts; 218, block; 218, 219; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 222; 221, identifier:base_docker_call; 222, binary_operator:+; 222, 223; 222, 238; 223, binary_operator:+; 223, 224; 223, 232; 224, binary_operator:+; 224, 225; 224, 231; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, string:' docker run -e JAVA_OPTS=-Xmx{} '; 228, identifier:format; 229, argument_list; 229, 230; 230, identifier:java_opts; 231, string:'--rm=true '; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, string:'-v {}:/data --log-driver=none '; 235, identifier:format; 236, argument_list; 236, 237; 237, identifier:work_dir; 238, identifier:interactive; 239, else_clause; 239, 240; 240, block; 240, 241; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 244; 243, identifier:base_docker_call; 244, binary_operator:+; 244, 245; 244, 253; 245, binary_operator:+; 245, 246; 245, 252; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, string:' docker run --rm=true -v {}:/data '; 249, identifier:format; 250, argument_list; 250, 251; 251, identifier:work_dir; 252, string:'--log-driver=none '; 253, identifier:interactive; 254, expression_statement; 254, 255; 255, assignment; 255, 256; 255, 257; 256, identifier:call; 257, binary_operator:+; 257, 258; 257, 266; 258, binary_operator:+; 258, 259; 258, 264; 259, call; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:base_docker_call; 262, identifier:split; 263, argument_list; 264, list:[docker_tool]; 264, 265; 265, identifier:docker_tool; 266, identifier:tool_parameters; 267, try_statement; 267, 268; 267, 279; 267, 305; 268, block; 268, 269; 269, expression_statement; 269, 270; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:subprocess; 273, identifier:check_call; 274, argument_list; 274, 275; 274, 276; 275, identifier:call; 276, keyword_argument; 276, 277; 276, 278; 277, identifier:stdout; 278, identifier:outfile; 279, except_clause; 279, 280; 279, 286; 280, as_pattern; 280, 281; 280, 284; 281, attribute; 281, 282; 281, 283; 282, identifier:subprocess; 283, identifier:CalledProcessError; 284, as_pattern_target; 284, 285; 285, identifier:err; 286, block; 286, 287; 287, raise_statement; 287, 288; 288, call; 288, 289; 288, 290; 289, identifier:RuntimeError; 290, argument_list; 290, 291; 291, binary_operator:+; 291, 292; 291, 297; 292, binary_operator:%; 292, 293; 292, 294; 293, string:'docker command returned a non-zero exit status (%s)'; 294, attribute; 294, 295; 294, 296; 295, identifier:err; 296, identifier:returncode; 297, binary_operator:%; 297, 298; 297, 299; 298, string:'for command \"%s\"'; 299, call; 299, 300; 299, 303; 300, attribute; 300, 301; 300, 302; 301, string:' '; 302, identifier:join; 303, argument_list; 303, 304; 304, identifier:call; 305, except_clause; 305, 306; 305, 307; 306, identifier:OSError; 307, block; 307, 308; 308, raise_statement; 308, 309; 309, call; 309, 310; 309, 311; 310, identifier:RuntimeError; 311, argument_list; 311, 312; 312, string:'docker not found on system. Install on all nodes.' | def docker_call(tool, tool_parameters, work_dir, java_opts=None, outfile=None,
dockerhub='aarjunrao', interactive=False):
"""
Makes subprocess call of a command to a docker container. work_dir MUST BE AN ABSOLUTE PATH or
the call will fail. outfile is an open file descriptor to a writeable file.
"""
# If an outifle has been provided, then ensure that it is of type file, it is writeable, and
# that it is open.
if outfile:
assert isinstance(outfile, file), 'outfile was not passsed a file'
assert outfile.mode in ['w', 'a', 'wb', 'ab'], 'outfile not writeable'
assert not outfile.closed, 'outfile is closed'
# If the call is interactive, set intereactive to -i
if interactive:
interactive = '-i'
else:
interactive = ''
# If a tag is passed along with the image, use it.
if ':' in tool:
docker_tool = '/'.join([dockerhub, tool])
# Else use 'latest'
else:
docker_tool = ''.join([dockerhub, '/', tool, ':latest'])
# Get the docker image on the worker if needed
call = ['docker', 'images']
dimg_rv = subprocess.check_output(call)
existing_images = [':'.join(x.split()[0:2]) for x in dimg_rv.splitlines()
if x.startswith(dockerhub)]
if docker_tool not in existing_images:
try:
call = ' '.join(['docker', 'pull', docker_tool]).split()
subprocess.check_call(call)
except subprocess.CalledProcessError as err:
raise RuntimeError('docker command returned a non-zero exit status ' +
'(%s)' % err.returncode + 'for command \"%s\"' % ' '.join(call),)
except OSError:
raise RuntimeError('docker not found on system. Install on all' +
' nodes.')
# If java options have been provided, it needs to be in the docker call
if java_opts:
base_docker_call = ' docker run -e JAVA_OPTS=-Xmx{} '.format(java_opts) + '--rm=true ' + \
'-v {}:/data --log-driver=none '.format(work_dir) + interactive
else:
base_docker_call = ' docker run --rm=true -v {}:/data '.format(work_dir) + \
'--log-driver=none ' + interactive
call = base_docker_call.split() + [docker_tool] + tool_parameters
try:
subprocess.check_call(call, stdout=outfile)
except subprocess.CalledProcessError as err:
raise RuntimeError('docker command returned a non-zero exit status (%s)' % err.returncode +
'for command \"%s\"' % ' '.join(call),)
except OSError:
raise RuntimeError('docker not found on system. Install on all nodes.') |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:get_file_from_gdc; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:job; 5, identifier:gdc_url; 6, identifier:gdc_download_token; 7, default_parameter; 7, 8; 7, 9; 8, identifier:write_to_jobstore; 9, True; 10, block; 10, 11; 10, 13; 10, 23; 10, 30; 10, 39; 10, 52; 10, 53; 10, 61; 10, 68; 10, 97; 10, 106; 10, 135; 10, 136; 10, 224; 10, 242; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:work_dir; 16, call; 16, 17; 16, 22; 17, attribute; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:job; 20, identifier:fileStore; 21, identifier:getLocalTempDir; 22, argument_list; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:parsed_url; 26, call; 26, 27; 26, 28; 27, identifier:urlparse; 28, argument_list; 28, 29; 29, identifier:gdc_url; 30, assert_statement; 30, 31; 30, 36; 31, comparison_operator:==; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:parsed_url; 34, identifier:scheme; 35, string:'gdc'; 36, binary_operator:%; 36, 37; 36, 38; 37, string:'Unexpected url scheme: %s'; 38, identifier:gdc_url; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:file_dir; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, string:'/'; 45, identifier:join; 46, argument_list; 46, 47; 47, list:[work_dir, parsed_url.netloc]; 47, 48; 47, 49; 48, identifier:work_dir; 49, attribute; 49, 50; 49, 51; 50, identifier:parsed_url; 51, identifier:netloc; 52, comment; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:currwd; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:os; 59, identifier:getcwd; 60, argument_list; 61, expression_statement; 61, 62; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:os; 65, identifier:chdir; 66, argument_list; 66, 67; 67, identifier:work_dir; 68, try_statement; 68, 69; 68, 88; 69, block; 69, 70; 69, 81; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:download_call; 73, list:['gdc-client', 'download', '-t', gdc_download_token, parsed_url.netloc]; 73, 74; 73, 75; 73, 76; 73, 77; 73, 78; 74, string:'gdc-client'; 75, string:'download'; 76, string:'-t'; 77, identifier:gdc_download_token; 78, attribute; 78, 79; 78, 80; 79, identifier:parsed_url; 80, identifier:netloc; 81, expression_statement; 81, 82; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:subprocess; 85, identifier:check_call; 86, argument_list; 86, 87; 87, identifier:download_call; 88, finally_clause; 88, 89; 89, block; 89, 90; 90, expression_statement; 90, 91; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:os; 94, identifier:chdir; 95, argument_list; 95, 96; 96, identifier:currwd; 97, assert_statement; 97, 98; 98, call; 98, 99; 98, 104; 99, attribute; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:os; 102, identifier:path; 103, identifier:exists; 104, argument_list; 104, 105; 105, identifier:file_dir; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:output_files; 109, list_comprehension; 109, 110; 109, 119; 109, 127; 110, call; 110, 111; 110, 116; 111, attribute; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:os; 114, identifier:path; 115, identifier:join; 116, argument_list; 116, 117; 116, 118; 117, identifier:file_dir; 118, identifier:x; 119, for_in_clause; 119, 120; 119, 121; 120, identifier:x; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:os; 124, identifier:listdir; 125, argument_list; 125, 126; 126, identifier:file_dir; 127, if_clause; 127, 128; 128, not_operator; 128, 129; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:x; 132, identifier:endswith; 133, argument_list; 133, 134; 134, string:'logs'; 135, comment; 136, if_statement; 136, 137; 136, 143; 136, 153; 137, comparison_operator:==; 137, 138; 137, 142; 138, call; 138, 139; 138, 140; 139, identifier:len; 140, argument_list; 140, 141; 141, identifier:output_files; 142, integer:1; 143, block; 143, 144; 144, assert_statement; 144, 145; 145, call; 145, 146; 145, 151; 146, attribute; 146, 147; 146, 150; 147, subscript; 147, 148; 147, 149; 148, identifier:output_files; 149, integer:0; 150, identifier:endswith; 151, argument_list; 151, 152; 152, string:'vcf'; 153, else_clause; 153, 154; 154, block; 154, 155; 154, 181; 154, 182; 154, 199; 155, if_statement; 155, 156; 155, 175; 156, not_operator; 156, 157; 157, comparison_operator:>=; 157, 158; 157, 172; 158, set_comprehension; 158, 159; 158, 169; 159, subscript; 159, 160; 159, 168; 160, call; 160, 161; 160, 166; 161, attribute; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:os; 164, identifier:path; 165, identifier:splitext; 166, argument_list; 166, 167; 167, identifier:x; 168, integer:1; 169, for_in_clause; 169, 170; 169, 171; 170, identifier:x; 171, identifier:output_files; 172, set; 172, 173; 172, 174; 173, string:'.bam'; 174, string:'.bai'; 175, block; 175, 176; 176, raise_statement; 176, 177; 177, call; 177, 178; 177, 179; 178, identifier:ParameterError; 179, argument_list; 179, 180; 180, string:'Can currently only handle pre-indexed GDC bams.'; 181, comment; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:output_files; 185, list_comprehension; 185, 186; 185, 187; 185, 190; 186, identifier:x; 187, for_in_clause; 187, 188; 187, 189; 188, identifier:x; 189, identifier:output_files; 190, if_clause; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:x; 194, identifier:endswith; 195, argument_list; 195, 196; 196, tuple; 196, 197; 196, 198; 197, string:'bam'; 198, string:'bai'; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:output_files; 202, call; 202, 203; 202, 204; 203, identifier:sorted; 204, argument_list; 204, 205; 204, 206; 204, 221; 205, identifier:output_files; 206, keyword_argument; 206, 207; 206, 208; 207, identifier:key; 208, lambda; 208, 209; 208, 211; 209, lambda_parameters; 209, 210; 210, identifier:x; 211, subscript; 211, 212; 211, 220; 212, call; 212, 213; 212, 218; 213, attribute; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:os; 216, identifier:path; 217, identifier:splitext; 218, argument_list; 218, 219; 219, identifier:x; 220, integer:1; 221, keyword_argument; 221, 222; 221, 223; 222, identifier:reverse; 223, True; 224, if_statement; 224, 225; 224, 226; 225, identifier:write_to_jobstore; 226, block; 226, 227; 227, expression_statement; 227, 228; 228, assignment; 228, 229; 228, 230; 229, identifier:output_files; 230, list_comprehension; 230, 231; 230, 239; 231, call; 231, 232; 231, 237; 232, attribute; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:job; 235, identifier:fileStore; 236, identifier:writeGlobalFile; 237, argument_list; 237, 238; 238, identifier:f; 239, for_in_clause; 239, 240; 239, 241; 240, identifier:f; 241, identifier:output_files; 242, return_statement; 242, 243; 243, identifier:output_files | def get_file_from_gdc(job, gdc_url, gdc_download_token, write_to_jobstore=True):
"""
Download a supplied "URL" that points to a file in the NCBI GDC database. The path to the gdc
download token must be provided. The file is downloaded and written to the jobstore if
requested.
:param str gdc_url: URL for the file (in the form of gdc://<UUID>)
:param str gdc_download_token: Path to the gdc download token
:param bool write_to_jobstore: Should the file be written to the job store?
:return: Path to the downloaded file or fsID (if write_to_jobstore was True)
:rtype: list(str|toil.fileStore.FileID)
"""
work_dir = job.fileStore.getLocalTempDir()
parsed_url = urlparse(gdc_url)
assert parsed_url.scheme == 'gdc', 'Unexpected url scheme: %s' % gdc_url
file_dir = '/'.join([work_dir, parsed_url.netloc])
# This is common to encrypted and unencrypted downloads
currwd = os.getcwd()
os.chdir(work_dir)
try:
download_call = ['gdc-client', 'download', '-t', gdc_download_token, parsed_url.netloc]
subprocess.check_call(download_call)
finally:
os.chdir(currwd)
assert os.path.exists(file_dir)
output_files = [os.path.join(file_dir, x) for x in os.listdir(file_dir)
if not x.endswith('logs')]
# NOTE: We only handle vcf and bam+bai
if len(output_files) == 1:
assert output_files[0].endswith('vcf')
else:
if not {os.path.splitext(x)[1] for x in output_files} >= {'.bam', '.bai'}:
raise ParameterError('Can currently only handle pre-indexed GDC bams.')
# Always [bam, bai]
output_files = [x for x in output_files if x.endswith(('bam', 'bai'))]
output_files = sorted(output_files, key=lambda x: os.path.splitext(x)[1], reverse=True)
if write_to_jobstore:
output_files = [job.fileStore.writeGlobalFile(f) for f in output_files]
return output_files |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:make_key_hippie; 3, parameters; 3, 4; 3, 5; 4, identifier:obj; 5, default_parameter; 5, 6; 5, 7; 6, identifier:typed; 7, True; 8, block; 8, 9; 8, 11; 8, 21; 8, 36; 8, 37; 8, 38; 8, 52; 8, 73; 8, 108; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:ftype; 14, conditional_expression:if; 14, 15; 14, 16; 14, 17; 15, identifier:type; 16, identifier:typed; 17, lambda; 17, 18; 17, 20; 18, lambda_parameters; 18, 19; 19, identifier:o; 20, None; 21, if_statement; 21, 22; 21, 26; 21, 27; 21, 28; 22, call; 22, 23; 22, 24; 23, identifier:is_hashable; 24, argument_list; 24, 25; 25, identifier:obj; 26, comment; 27, comment; 28, block; 28, 29; 29, return_statement; 29, 30; 30, expression_list; 30, 31; 30, 32; 31, identifier:obj; 32, call; 32, 33; 32, 34; 33, identifier:ftype; 34, argument_list; 34, 35; 35, identifier:obj; 36, comment; 37, comment; 38, if_statement; 38, 39; 38, 44; 39, call; 39, 40; 39, 41; 40, identifier:isinstance; 41, argument_list; 41, 42; 41, 43; 42, identifier:obj; 43, identifier:set; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:obj; 48, call; 48, 49; 48, 50; 49, identifier:sorted; 50, argument_list; 50, 51; 51, identifier:obj; 52, if_statement; 52, 53; 52, 60; 53, call; 53, 54; 53, 55; 54, identifier:isinstance; 55, argument_list; 55, 56; 55, 57; 56, identifier:obj; 57, tuple; 57, 58; 57, 59; 58, identifier:list; 59, identifier:tuple; 60, block; 60, 61; 61, return_statement; 61, 62; 62, call; 62, 63; 62, 64; 63, identifier:tuple; 64, generator_expression; 64, 65; 64, 70; 65, call; 65, 66; 65, 67; 66, identifier:make_key_hippie; 67, argument_list; 67, 68; 67, 69; 68, identifier:e; 69, identifier:typed; 70, for_in_clause; 70, 71; 70, 72; 71, identifier:e; 72, identifier:obj; 73, if_statement; 73, 74; 73, 79; 74, call; 74, 75; 74, 76; 75, identifier:isinstance; 76, argument_list; 76, 77; 76, 78; 77, identifier:obj; 78, identifier:dict; 79, block; 79, 80; 80, return_statement; 80, 81; 81, call; 81, 82; 81, 83; 82, identifier:tuple; 83, argument_list; 83, 84; 84, call; 84, 85; 84, 86; 85, identifier:sorted; 86, argument_list; 86, 87; 87, generator_expression; 87, 88; 87, 99; 88, tuple; 88, 89; 88, 94; 89, call; 89, 90; 89, 91; 90, identifier:make_key_hippie; 91, argument_list; 91, 92; 91, 93; 92, identifier:k; 93, identifier:typed; 94, call; 94, 95; 94, 96; 95, identifier:make_key_hippie; 96, argument_list; 96, 97; 96, 98; 97, identifier:v; 98, identifier:typed; 99, for_in_clause; 99, 100; 99, 103; 100, pattern_list; 100, 101; 100, 102; 101, identifier:k; 102, identifier:v; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:obj; 106, identifier:items; 107, argument_list; 108, raise_statement; 108, 109; 109, call; 109, 110; 109, 111; 110, identifier:ValueError; 111, argument_list; 111, 112; 112, binary_operator:%; 112, 113; 112, 114; 113, string:"%r can not be hashed. Try providing a custom key function."; 114, identifier:obj | def make_key_hippie(obj, typed=True):
"""Return hashable structure from non-hashable structure using hippie means
dict and set are sorted and their content subjected to same hippie means.
Note that the key identifies the current content of the structure.
"""
ftype = type if typed else lambda o: None
if is_hashable(obj):
## DO NOT RETURN hash(obj), as hash collision would generate bad
## cache collisions.
return obj, ftype(obj)
## should we try to convert to frozen{set,dict} to get the C
## hashing function speed ? But the convertion has a cost also.
if isinstance(obj, set):
obj = sorted(obj)
if isinstance(obj, (list, tuple)):
return tuple(make_key_hippie(e, typed) for e in obj)
if isinstance(obj, dict):
return tuple(sorted(((make_key_hippie(k, typed),
make_key_hippie(v, typed))
for k, v in obj.items())))
raise ValueError(
"%r can not be hashed. Try providing a custom key function."
% obj) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:run_radia; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 4, identifier:job; 5, identifier:rna_bam; 6, identifier:tumor_bam; 7, identifier:normal_bam; 8, identifier:univ_options; 9, identifier:radia_options; 10, block; 10, 11; 10, 13; 10, 50; 10, 84; 10, 85; 10, 108; 10, 114; 10, 201; 10, 214; 11, expression_statement; 11, 12; 12, comment; 13, if_statement; 13, 14; 13, 21; 13, 28; 13, 43; 14, comparison_operator:in; 14, 15; 14, 16; 15, string:'rna_genome'; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:rna_bam; 19, identifier:keys; 20, argument_list; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:rna_bam; 25, subscript; 25, 26; 25, 27; 26, identifier:rna_bam; 27, string:'rna_genome'; 28, elif_clause; 28, 29; 28, 41; 29, comparison_operator:==; 29, 30; 29, 38; 30, call; 30, 31; 30, 32; 31, identifier:set; 32, argument_list; 32, 33; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:rna_bam; 36, identifier:keys; 37, argument_list; 38, set; 38, 39; 38, 40; 39, string:'rna_genome_sorted.bam'; 40, string:'rna_genome_sorted.bam.bai'; 41, block; 41, 42; 42, pass_statement; 43, else_clause; 43, 44; 44, block; 44, 45; 45, raise_statement; 45, 46; 46, call; 46, 47; 46, 48; 47, identifier:RuntimeError; 48, argument_list; 48, 49; 49, string:'An improperly formatted dict was passed to rna_bam.'; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:bams; 53, dictionary; 53, 54; 53, 59; 53, 64; 53, 69; 53, 74; 53, 79; 54, pair; 54, 55; 54, 56; 55, string:'tumor_rna'; 56, subscript; 56, 57; 56, 58; 57, identifier:rna_bam; 58, string:'rna_genome_sorted.bam'; 59, pair; 59, 60; 59, 61; 60, string:'tumor_rnai'; 61, subscript; 61, 62; 61, 63; 62, identifier:rna_bam; 63, string:'rna_genome_sorted.bam.bai'; 64, pair; 64, 65; 64, 66; 65, string:'tumor_dna'; 66, subscript; 66, 67; 66, 68; 67, identifier:tumor_bam; 68, string:'tumor_dna_fix_pg_sorted.bam'; 69, pair; 69, 70; 69, 71; 70, string:'tumor_dnai'; 71, subscript; 71, 72; 71, 73; 72, identifier:tumor_bam; 73, string:'tumor_dna_fix_pg_sorted.bam.bai'; 74, pair; 74, 75; 74, 76; 75, string:'normal_dna'; 76, subscript; 76, 77; 76, 78; 77, identifier:normal_bam; 78, string:'normal_dna_fix_pg_sorted.bam'; 79, pair; 79, 80; 79, 81; 80, string:'normal_dnai'; 81, subscript; 81, 82; 81, 83; 82, identifier:normal_bam; 83, string:'normal_dna_fix_pg_sorted.bam.bai'; 84, comment; 85, if_statement; 85, 86; 85, 89; 85, 96; 86, subscript; 86, 87; 86, 88; 87, identifier:radia_options; 88, string:'chromosomes'; 89, block; 89, 90; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:chromosomes; 93, subscript; 93, 94; 93, 95; 94, identifier:radia_options; 95, string:'chromosomes'; 96, else_clause; 96, 97; 97, block; 97, 98; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:chromosomes; 101, call; 101, 102; 101, 103; 102, identifier:sample_chromosomes; 103, argument_list; 103, 104; 103, 105; 104, identifier:job; 105, subscript; 105, 106; 105, 107; 106, identifier:radia_options; 107, string:'genome_fai'; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:perchrom_radia; 111, call; 111, 112; 111, 113; 112, identifier:defaultdict; 113, argument_list; 114, for_statement; 114, 115; 114, 116; 114, 117; 115, identifier:chrom; 116, identifier:chromosomes; 117, block; 117, 118; 117, 152; 117, 191; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:radia; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:job; 124, identifier:addChildJobFn; 125, argument_list; 125, 126; 125, 127; 125, 128; 125, 129; 125, 130; 125, 131; 125, 134; 126, identifier:run_radia_perchrom; 127, identifier:bams; 128, identifier:univ_options; 129, identifier:radia_options; 130, identifier:chrom; 131, keyword_argument; 131, 132; 131, 133; 132, identifier:memory; 133, string:'6G'; 134, keyword_argument; 134, 135; 134, 136; 135, identifier:disk; 136, call; 136, 137; 136, 138; 137, identifier:PromisedRequirement; 138, argument_list; 138, 139; 138, 140; 138, 143; 138, 146; 138, 149; 139, identifier:radia_disk; 140, subscript; 140, 141; 140, 142; 141, identifier:tumor_bam; 142, string:'tumor_dna_fix_pg_sorted.bam'; 143, subscript; 143, 144; 143, 145; 144, identifier:normal_bam; 145, string:'normal_dna_fix_pg_sorted.bam'; 146, subscript; 146, 147; 146, 148; 147, identifier:rna_bam; 148, string:'rna_genome_sorted.bam'; 149, subscript; 149, 150; 149, 151; 150, identifier:radia_options; 151, string:'genome_fasta'; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:filter_radia; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:radia; 158, identifier:addChildJobFn; 159, argument_list; 159, 160; 159, 161; 159, 162; 159, 167; 159, 168; 159, 169; 159, 170; 159, 173; 160, identifier:run_filter_radia; 161, identifier:bams; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:radia; 165, identifier:rv; 166, argument_list; 167, identifier:univ_options; 168, identifier:radia_options; 169, identifier:chrom; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:memory; 172, string:'6G'; 173, keyword_argument; 173, 174; 173, 175; 174, identifier:disk; 175, call; 175, 176; 175, 177; 176, identifier:PromisedRequirement; 177, argument_list; 177, 178; 177, 179; 177, 182; 177, 185; 177, 188; 178, identifier:radia_disk; 179, subscript; 179, 180; 179, 181; 180, identifier:tumor_bam; 181, string:'tumor_dna_fix_pg_sorted.bam'; 182, subscript; 182, 183; 182, 184; 183, identifier:normal_bam; 184, string:'normal_dna_fix_pg_sorted.bam'; 185, subscript; 185, 186; 185, 187; 186, identifier:rna_bam; 187, string:'rna_genome_sorted.bam'; 188, subscript; 188, 189; 188, 190; 189, identifier:radia_options; 190, string:'genome_fasta'; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 196; 193, subscript; 193, 194; 193, 195; 194, identifier:perchrom_radia; 195, identifier:chrom; 196, call; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:filter_radia; 199, identifier:rv; 200, argument_list; 201, expression_statement; 201, 202; 202, call; 202, 203; 202, 208; 203, attribute; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:job; 206, identifier:fileStore; 207, identifier:logToMaster; 208, argument_list; 208, 209; 209, binary_operator:%; 209, 210; 209, 211; 210, string:'Ran spawn_radia on %s successfully'; 211, subscript; 211, 212; 211, 213; 212, identifier:univ_options; 213, string:'patient'; 214, return_statement; 214, 215; 215, identifier:perchrom_radia | def run_radia(job, rna_bam, tumor_bam, normal_bam, univ_options, radia_options):
"""
Spawn a RADIA job for each chromosome on the input bam trios.
:param dict rna_bam: Dict of bam and bai for tumor DNA-Seq. It can be one of two formats
rna_bam: # Just the genomic bam and bai
|- 'rna_genome_sorted.bam': fsID
+- 'rna_genome_sorted.bam.bai': fsID
OR
rna_bam: # The output from run_star
|- 'rna_transcriptome.bam': fsID
|- 'rna_genome': # Only this part will be used
|- 'rna_genome_sorted.bam': fsID
+- 'rna_genome_sorted.bam.bai': fsID
:param dict tumor_bam: Dict of bam and bai for tumor DNA-Seq
:param dict normal_bam: Dict of bam and bai for normal DNA-Seq
:param dict univ_options: Dict of universal options used by almost all tools
:param dict radia_options: Options specific to RADIA
:return: Dict of results from running RADIA on every chromosome
perchrom_radia:
|- 'chr1': fsID
|- 'chr2' fsID
|
|-...
|
+- 'chrM': fsID
:rtype: dict
"""
if 'rna_genome' in rna_bam.keys():
rna_bam = rna_bam['rna_genome']
elif set(rna_bam.keys()) == {'rna_genome_sorted.bam', 'rna_genome_sorted.bam.bai'}:
pass
else:
raise RuntimeError('An improperly formatted dict was passed to rna_bam.')
bams = {'tumor_rna': rna_bam['rna_genome_sorted.bam'],
'tumor_rnai': rna_bam['rna_genome_sorted.bam.bai'],
'tumor_dna': tumor_bam['tumor_dna_fix_pg_sorted.bam'],
'tumor_dnai': tumor_bam['tumor_dna_fix_pg_sorted.bam.bai'],
'normal_dna': normal_bam['normal_dna_fix_pg_sorted.bam'],
'normal_dnai': normal_bam['normal_dna_fix_pg_sorted.bam.bai']}
# Get a list of chromosomes to process
if radia_options['chromosomes']:
chromosomes = radia_options['chromosomes']
else:
chromosomes = sample_chromosomes(job, radia_options['genome_fai'])
perchrom_radia = defaultdict()
for chrom in chromosomes:
radia = job.addChildJobFn(run_radia_perchrom, bams, univ_options, radia_options, chrom,
memory='6G',
disk=PromisedRequirement(
radia_disk, tumor_bam['tumor_dna_fix_pg_sorted.bam'],
normal_bam['normal_dna_fix_pg_sorted.bam'],
rna_bam['rna_genome_sorted.bam'],
radia_options['genome_fasta']))
filter_radia = radia.addChildJobFn(run_filter_radia, bams, radia.rv(), univ_options,
radia_options, chrom, memory='6G',
disk=PromisedRequirement(
radia_disk, tumor_bam['tumor_dna_fix_pg_sorted.bam'],
normal_bam['normal_dna_fix_pg_sorted.bam'],
rna_bam['rna_genome_sorted.bam'],
radia_options['genome_fasta']))
perchrom_radia[chrom] = filter_radia.rv()
job.fileStore.logToMaster('Ran spawn_radia on %s successfully' % univ_options['patient'])
return perchrom_radia |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:sort_bamfile; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:job; 5, identifier:bamfile; 6, identifier:sample_type; 7, identifier:univ_options; 8, identifier:samtools_options; 9, block; 9, 10; 9, 12; 9, 20; 9, 31; 9, 42; 9, 49; 9, 61; 9, 85; 9, 108; 9, 117; 9, 132; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:work_dir; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:os; 18, identifier:getcwd; 19, argument_list; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:in_bamfile; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, string:''; 26, identifier:join; 27, argument_list; 27, 28; 28, list:[sample_type, '.bam']; 28, 29; 28, 30; 29, identifier:sample_type; 30, string:'.bam'; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:out_bamfile; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, string:'_'; 37, identifier:join; 38, argument_list; 38, 39; 39, list:[sample_type, 'sorted.bam']; 39, 40; 39, 41; 40, identifier:sample_type; 41, string:'sorted.bam'; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:input_files; 45, dictionary; 45, 46; 46, pair; 46, 47; 46, 48; 47, identifier:in_bamfile; 48, identifier:bamfile; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:input_files; 52, call; 52, 53; 52, 54; 53, identifier:get_files_from_filestore; 54, argument_list; 54, 55; 54, 56; 54, 57; 54, 58; 55, identifier:job; 56, identifier:input_files; 57, identifier:work_dir; 58, keyword_argument; 58, 59; 58, 60; 59, identifier:docker; 60, True; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:parameters; 64, list:['sort',
'-o', docker_path(out_bamfile),
'-O', 'bam',
'-T', 'temp_sorted',
'-@', str(samtools_options['n']),
input_files[in_bamfile]]; 64, 65; 64, 66; 64, 67; 64, 71; 64, 72; 64, 73; 64, 74; 64, 75; 64, 76; 64, 82; 65, string:'sort'; 66, string:'-o'; 67, call; 67, 68; 67, 69; 68, identifier:docker_path; 69, argument_list; 69, 70; 70, identifier:out_bamfile; 71, string:'-O'; 72, string:'bam'; 73, string:'-T'; 74, string:'temp_sorted'; 75, string:'-@'; 76, call; 76, 77; 76, 78; 77, identifier:str; 78, argument_list; 78, 79; 79, subscript; 79, 80; 79, 81; 80, identifier:samtools_options; 81, string:'n'; 82, subscript; 82, 83; 82, 84; 83, identifier:input_files; 84, identifier:in_bamfile; 85, expression_statement; 85, 86; 86, call; 86, 87; 86, 88; 87, identifier:docker_call; 88, argument_list; 88, 89; 88, 92; 88, 95; 88, 98; 88, 103; 89, keyword_argument; 89, 90; 89, 91; 90, identifier:tool; 91, string:'samtools'; 92, keyword_argument; 92, 93; 92, 94; 93, identifier:tool_parameters; 94, identifier:parameters; 95, keyword_argument; 95, 96; 95, 97; 96, identifier:work_dir; 97, identifier:work_dir; 98, keyword_argument; 98, 99; 98, 100; 99, identifier:dockerhub; 100, subscript; 100, 101; 100, 102; 101, identifier:univ_options; 102, string:'dockerhub'; 103, keyword_argument; 103, 104; 103, 105; 104, identifier:tool_version; 105, subscript; 105, 106; 105, 107; 106, identifier:samtools_options; 107, string:'version'; 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:job; 113, identifier:fileStore; 114, identifier:deleteGlobalFile; 115, argument_list; 115, 116; 116, identifier:bamfile; 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:job; 122, identifier:fileStore; 123, identifier:logToMaster; 124, argument_list; 124, 125; 125, binary_operator:%; 125, 126; 125, 127; 126, string:'Ran samtools-sort on %s:%s successfully'; 127, tuple; 127, 128; 127, 131; 128, subscript; 128, 129; 128, 130; 129, identifier:univ_options; 130, string:'patient'; 131, identifier:sample_type; 132, return_statement; 132, 133; 133, call; 133, 134; 133, 139; 134, attribute; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:job; 137, identifier:fileStore; 138, identifier:writeGlobalFile; 139, argument_list; 139, 140; 140, identifier:out_bamfile | def sort_bamfile(job, bamfile, sample_type, univ_options, samtools_options):
"""
Sort `bamfile` using samtools
:param toil.fileStore.FileID bamfile: fsID for the bam file
:param str sample_type: Description of the sample to inject into the filename
:param dict univ_options: Dict of universal options used by almost all tools
:param dict samtools_options: Options specific to samtools
:return: fsID for the sorted bamfile
:rtype: toil.fileStore.FileID
"""
work_dir = os.getcwd()
in_bamfile = ''.join([sample_type, '.bam'])
out_bamfile = '_'.join([sample_type, 'sorted.bam'])
input_files = {
in_bamfile: bamfile}
input_files = get_files_from_filestore(job, input_files, work_dir, docker=True)
parameters = ['sort',
'-o', docker_path(out_bamfile),
'-O', 'bam',
'-T', 'temp_sorted',
'-@', str(samtools_options['n']),
input_files[in_bamfile]]
docker_call(tool='samtools', tool_parameters=parameters, work_dir=work_dir,
dockerhub=univ_options['dockerhub'], tool_version=samtools_options['version'])
job.fileStore.deleteGlobalFile(bamfile)
job.fileStore.logToMaster('Ran samtools-sort on %s:%s successfully'
% (univ_options['patient'], sample_type))
return job.fileStore.writeGlobalFile(out_bamfile) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:recursive_sort; 3, parameters; 3, 4; 4, identifier:data_structure; 5, block; 5, 6; 5, 8; 5, 9; 5, 185; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, if_statement; 9, 10; 9, 16; 10, not_operator; 10, 11; 11, call; 11, 12; 11, 13; 12, identifier:isinstance; 13, argument_list; 13, 14; 13, 15; 14, identifier:data_structure; 15, identifier:_primitive_types; 16, block; 16, 17; 16, 25; 16, 33; 16, 82; 16, 87; 16, 88; 16, 101; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:is_meta; 20, call; 20, 21; 20, 22; 21, identifier:isinstance; 22, argument_list; 22, 23; 22, 24; 23, identifier:data_structure; 24, identifier:Meta; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:was_dict; 28, call; 28, 29; 28, 30; 29, identifier:isinstance; 30, argument_list; 30, 31; 30, 32; 31, identifier:data_structure; 32, identifier:WasDict; 33, if_statement; 33, 34; 33, 39; 34, not_operator; 34, 35; 35, parenthesized_expression; 35, 36; 36, boolean_operator:or; 36, 37; 36, 38; 37, identifier:is_meta; 38, identifier:was_dict; 39, block; 39, 40; 39, 48; 39, 68; 39, 69; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:was_dict; 43, call; 43, 44; 43, 45; 44, identifier:isinstance; 45, argument_list; 45, 46; 45, 47; 46, identifier:data_structure; 47, identifier:dict; 48, if_statement; 48, 49; 48, 51; 48, 52; 49, not_operator; 49, 50; 50, identifier:was_dict; 51, comment; 52, block; 52, 53; 53, try_statement; 53, 54; 53, 65; 54, block; 54, 55; 54, 61; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:data_structure; 58, attribute; 58, 59; 58, 60; 59, identifier:data_structure; 60, identifier:__dict__; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:was_dict; 64, True; 65, except_clause; 65, 66; 66, block; 66, 67; 67, pass_statement; 68, comment; 69, try_statement; 69, 70; 69, 79; 70, block; 70, 71; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:data_structure; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:data_structure; 77, identifier:items; 78, argument_list; 79, except_clause; 79, 80; 80, block; 80, 81; 81, pass_statement; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:tlen; 85, unary_operator:-; 85, 86; 86, integer:1; 87, comment; 88, try_statement; 88, 89; 88, 97; 89, block; 89, 90; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:tlen; 93, call; 93, 94; 93, 95; 94, identifier:len; 95, argument_list; 95, 96; 96, identifier:data_structure; 97, except_clause; 97, 98; 97, 99; 98, comment; 99, block; 99, 100; 100, pass_statement; 101, if_statement; 101, 102; 101, 106; 101, 107; 101, 108; 102, comparison_operator:!=; 102, 103; 102, 104; 103, identifier:tlen; 104, unary_operator:-; 104, 105; 105, integer:1; 106, comment; 107, comment; 108, block; 108, 109; 109, try_statement; 109, 110; 109, 181; 110, block; 110, 111; 111, if_statement; 111, 112; 111, 113; 111, 141; 111, 161; 112, identifier:was_dict; 113, block; 113, 114; 114, return_statement; 114, 115; 115, call; 115, 116; 115, 117; 116, identifier:tuple; 117, argument_list; 117, 118; 118, call; 118, 119; 118, 120; 119, identifier:sorted; 120, argument_list; 120, 121; 120, 138; 121, list_comprehension; 121, 122; 121, 135; 122, tuple; 122, 123; 122, 129; 123, call; 123, 124; 123, 125; 124, identifier:recursive_sort; 125, argument_list; 125, 126; 126, subscript; 126, 127; 126, 128; 127, identifier:x; 128, integer:0; 129, call; 129, 130; 129, 131; 130, identifier:recursive_sort; 131, argument_list; 131, 132; 132, subscript; 132, 133; 132, 134; 133, identifier:x; 134, integer:1; 135, for_in_clause; 135, 136; 135, 137; 136, identifier:x; 137, identifier:data_structure; 138, keyword_argument; 138, 139; 138, 140; 139, identifier:key; 140, identifier:TraversalBasedReprCompare; 141, elif_clause; 141, 142; 141, 143; 142, identifier:is_meta; 143, block; 143, 144; 144, return_statement; 144, 145; 145, binary_operator:+; 145, 146; 145, 153; 146, subscript; 146, 147; 146, 148; 147, identifier:data_structure; 148, slice; 148, 149; 148, 150; 148, 151; 149, integer:0; 150, colon; 151, unary_operator:-; 151, 152; 152, integer:1; 153, list:[
recursive_sort(
data_structure[-1]
)
]; 153, 154; 154, call; 154, 155; 154, 156; 155, identifier:recursive_sort; 156, argument_list; 156, 157; 157, subscript; 157, 158; 157, 159; 158, identifier:data_structure; 159, unary_operator:-; 159, 160; 160, integer:1; 161, else_clause; 161, 162; 162, block; 162, 163; 163, return_statement; 163, 164; 164, call; 164, 165; 164, 166; 165, identifier:tuple; 166, argument_list; 166, 167; 167, call; 167, 168; 167, 169; 168, identifier:sorted; 169, argument_list; 169, 170; 169, 178; 170, list_comprehension; 170, 171; 170, 175; 171, call; 171, 172; 171, 173; 172, identifier:recursive_sort; 173, argument_list; 173, 174; 174, identifier:x; 175, for_in_clause; 175, 176; 175, 177; 176, identifier:x; 177, identifier:data_structure; 178, keyword_argument; 178, 179; 178, 180; 179, identifier:key; 180, identifier:TraversalBasedReprCompare; 181, except_clause; 181, 182; 181, 183; 182, comment; 183, block; 183, 184; 184, pass_statement; 185, return_statement; 185, 186; 186, identifier:data_structure | def recursive_sort(data_structure):
"""Sort a recursive data_structure.
:param data_structure: The structure to convert.
data_structure must be already sortable or you must use freeze() or dump().
The function will work with many kinds of input. Dictionaries will be
converted to lists of tuples.
>>> _py2_to_py3(vformat(recursive_sort(dump(
... [3, 1, {'c' : 'c', 'a' : 'b', 'b' : 'a'}]
... ))))
(["<class 'dict'>",
(('a',
'b'),
('b',
'a'),
('c',
'c'))],
1,
3)
>>> recursive_sort([3, 1, {'c' : 'c', 'a' : 'b', 'b' : 'a'}])
((('a', 'b'), ('b', 'a'), ('c', 'c')), 1, 3)
>>> recursive_sort(_TestClass())
(('a', 'huhu'),)
"""
# We don't sory primitve types
if not isinstance(data_structure, _primitive_types):
is_meta = isinstance(data_structure, Meta)
was_dict = isinstance(data_structure, WasDict)
if not (is_meta or was_dict):
was_dict = isinstance(data_structure, dict)
if not was_dict:
# Dictize if possible (support objects)
try:
data_structure = data_structure.__dict__
was_dict = True
except:
pass
# Itemize if possible
try:
data_structure = data_structure.items()
except:
pass
tlen = -1
# If item has a length we sort it
try:
tlen = len(data_structure)
except: # pragma: no cover
pass
if tlen != -1:
# Well there are classes out in the wild that answer to len
# but have no indexer.
try:
if was_dict:
return tuple(sorted(
[
(
recursive_sort(x[0]),
recursive_sort(x[1]),
)
for x in data_structure
],
key=TraversalBasedReprCompare
))
elif is_meta:
return data_structure[0:-1] + [
recursive_sort(
data_structure[-1]
)
]
else:
return tuple(sorted(
[recursive_sort(
x,
) for x in data_structure],
key=TraversalBasedReprCompare,
))
except: # pragma: no cover
pass
return data_structure |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:tree_diff; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:a; 5, identifier:b; 6, default_parameter; 6, 7; 6, 8; 7, identifier:n; 8, integer:5; 9, default_parameter; 9, 10; 9, 11; 10, identifier:sort; 11, False; 12, block; 12, 13; 12, 15; 12, 22; 12, 29; 12, 89; 13, expression_statement; 13, 14; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:a; 18, call; 18, 19; 18, 20; 19, identifier:dump; 20, argument_list; 20, 21; 21, identifier:a; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:b; 25, call; 25, 26; 25, 27; 26, identifier:dump; 27, argument_list; 27, 28; 28, identifier:b; 29, if_statement; 29, 30; 29, 32; 29, 57; 30, not_operator; 30, 31; 31, identifier:sort; 32, block; 32, 33; 32, 45; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:a; 36, call; 36, 37; 36, 43; 37, attribute; 37, 38; 37, 42; 38, call; 38, 39; 38, 40; 39, identifier:vformat; 40, argument_list; 40, 41; 41, identifier:a; 42, identifier:split; 43, argument_list; 43, 44; 44, string:"\n"; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:b; 48, call; 48, 49; 48, 55; 49, attribute; 49, 50; 49, 54; 50, call; 50, 51; 50, 52; 51, identifier:vformat; 52, argument_list; 52, 53; 53, identifier:b; 54, identifier:split; 55, argument_list; 55, 56; 56, string:"\n"; 57, else_clause; 57, 58; 58, block; 58, 59; 58, 74; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:a; 62, call; 62, 63; 62, 72; 63, attribute; 63, 64; 63, 71; 64, call; 64, 65; 64, 66; 65, identifier:vformat; 66, argument_list; 66, 67; 67, call; 67, 68; 67, 69; 68, identifier:recursive_sort; 69, argument_list; 69, 70; 70, identifier:a; 71, identifier:split; 72, argument_list; 72, 73; 73, string:"\n"; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:b; 77, call; 77, 78; 77, 87; 78, attribute; 78, 79; 78, 86; 79, call; 79, 80; 79, 81; 80, identifier:vformat; 81, argument_list; 81, 82; 82, call; 82, 83; 82, 84; 83, identifier:recursive_sort; 84, argument_list; 84, 85; 85, identifier:b; 86, identifier:split; 87, argument_list; 87, 88; 88, string:"\n"; 89, return_statement; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, string:"\n"; 93, identifier:join; 94, argument_list; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:difflib; 98, identifier:unified_diff; 99, argument_list; 99, 100; 99, 101; 99, 102; 99, 105; 100, identifier:a; 101, identifier:b; 102, keyword_argument; 102, 103; 102, 104; 103, identifier:n; 104, identifier:n; 105, keyword_argument; 105, 106; 105, 107; 106, identifier:lineterm; 107, string:"" | def tree_diff(a, b, n=5, sort=False):
"""Dump any data-structure or object, traverse
it depth-first in-order and apply a unified diff.
Depth-first in-order is just like structure would be printed.
:param a: data_structure a
:param b: data_structure b
:param n: lines of context
:type n: int
:param sort: sort the data-structure
ATTENTION: Sorting means changing the data-structure. The test-result may
differ. But in case of dictionaries the results become comparable because
the sorting negates the hash-algorithms "de-sorting".
>>> a = recursive_sort(freeze([
... 'a',
... [3, 4],
... {'a': [3, {'w' : set([4, '3', frozenset([3,5,2])])}]},
... []
... ]))
>>> b = recursive_sort(freeze([
... 'a',
... [7, 3],
... {'a': [3, {'w' : set([4, '3', frozenset([2,5,3])])}]},
... []
... ]))
>>> transparent_repr("\\n".join(tree_diff(a, b).split("\\n")[2:]))
@@ -7,6 +7,6 @@
'w'),),
3),
'a'),),
'a',
(3,
- 4))
+ 7))
>>> a = [
... 'a',
... [3, 4],
... {'a': [3, {'w' : set([4, '3', frozenset([3,5,2])])}]},
... []
... ]
>>> b = [
... 'a',
... [7, 3],
... {'a': [3, {'w' : set([4, '3', frozenset([2,5,3])])}]},
... []
... ]
>>> transparent_repr("\\n".join(
... tree_diff(a, b, sort=True
... ).split("\\n")[2:]))
@@ -11,6 +11,6 @@
'3',
4)]),)],
3)),)],
'a',
(3,
- 4))
+ 7))
"""
a = dump(a)
b = dump(b)
if not sort:
a = vformat(a).split("\n")
b = vformat(b).split("\n")
else:
a = vformat(recursive_sort(a)).split("\n")
b = vformat(recursive_sort(b)).split("\n")
return "\n".join(difflib.unified_diff(a, b, n=n, lineterm="")) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:merge_perchrom_mutations; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:job; 5, identifier:chrom; 6, identifier:mutations; 7, identifier:univ_options; 8, block; 8, 9; 8, 11; 8, 19; 8, 26; 8, 33; 8, 40; 8, 47; 8, 54; 8, 61; 8, 71; 8, 81; 8, 109; 8, 110; 8, 111; 8, 112; 8, 113; 8, 114; 8, 124; 8, 131; 8, 315; 8, 418; 8, 431; 8, 444; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:work_dir; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:os; 17, identifier:getcwd; 18, argument_list; 19, import_from_statement; 19, 20; 19, 24; 20, dotted_name; 20, 21; 20, 22; 20, 23; 21, identifier:protect; 22, identifier:mutation_calling; 23, identifier:muse; 24, dotted_name; 24, 25; 25, identifier:process_muse_vcf; 26, import_from_statement; 26, 27; 26, 31; 27, dotted_name; 27, 28; 27, 29; 27, 30; 28, identifier:protect; 29, identifier:mutation_calling; 30, identifier:mutect; 31, dotted_name; 31, 32; 32, identifier:process_mutect_vcf; 33, import_from_statement; 33, 34; 33, 38; 34, dotted_name; 34, 35; 34, 36; 34, 37; 35, identifier:protect; 36, identifier:mutation_calling; 37, identifier:radia; 38, dotted_name; 38, 39; 39, identifier:process_radia_vcf; 40, import_from_statement; 40, 41; 40, 45; 41, dotted_name; 41, 42; 41, 43; 41, 44; 42, identifier:protect; 43, identifier:mutation_calling; 44, identifier:somaticsniper; 45, dotted_name; 45, 46; 46, identifier:process_somaticsniper_vcf; 47, import_from_statement; 47, 48; 47, 52; 48, dotted_name; 48, 49; 48, 50; 48, 51; 49, identifier:protect; 50, identifier:mutation_calling; 51, identifier:strelka; 52, dotted_name; 52, 53; 53, identifier:process_strelka_vcf; 54, expression_statement; 54, 55; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:mutations; 58, identifier:pop; 59, argument_list; 59, 60; 60, string:'indels'; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 66; 63, subscript; 63, 64; 63, 65; 64, identifier:mutations; 65, string:'strelka_indels'; 66, subscript; 66, 67; 66, 70; 67, subscript; 67, 68; 67, 69; 68, identifier:mutations; 69, string:'strelka'; 70, string:'indels'; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 76; 73, subscript; 73, 74; 73, 75; 74, identifier:mutations; 75, string:'strelka_snvs'; 76, subscript; 76, 77; 76, 80; 77, subscript; 77, 78; 77, 79; 78, identifier:mutations; 79, string:'strelka'; 80, string:'snvs'; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:vcf_processor; 84, dictionary; 84, 85; 84, 103; 85, pair; 85, 86; 85, 87; 86, string:'snvs'; 87, dictionary; 87, 88; 87, 91; 87, 94; 87, 97; 87, 100; 88, pair; 88, 89; 88, 90; 89, string:'mutect'; 90, identifier:process_mutect_vcf; 91, pair; 91, 92; 91, 93; 92, string:'muse'; 93, identifier:process_muse_vcf; 94, pair; 94, 95; 94, 96; 95, string:'radia'; 96, identifier:process_radia_vcf; 97, pair; 97, 98; 97, 99; 98, string:'somaticsniper'; 99, identifier:process_somaticsniper_vcf; 100, pair; 100, 101; 100, 102; 101, string:'strelka_snvs'; 102, identifier:process_strelka_vcf; 103, pair; 103, 104; 103, 105; 104, string:'indels'; 105, dictionary; 105, 106; 106, pair; 106, 107; 106, 108; 107, string:'strelka_indels'; 108, identifier:process_strelka_vcf; 109, comment; 110, comment; 111, comment; 112, comment; 113, comment; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:majority; 117, dictionary; 117, 118; 117, 121; 118, pair; 118, 119; 118, 120; 119, string:'snvs'; 120, integer:2; 121, pair; 121, 122; 121, 123; 122, string:'indels'; 123, integer:1; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:accepted_hits; 127, call; 127, 128; 127, 129; 128, identifier:defaultdict; 129, argument_list; 129, 130; 130, identifier:dict; 131, for_statement; 131, 132; 131, 133; 131, 138; 131, 139; 132, identifier:mut_type; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:vcf_processor; 136, identifier:keys; 137, argument_list; 138, comment; 139, block; 139, 140; 139, 166; 139, 167; 139, 177; 139, 186; 139, 187; 139, 206; 139, 226; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:perchrom_mutations; 143, dictionary_comprehension; 143, 144; 143, 161; 144, pair; 144, 145; 144, 146; 145, identifier:caller; 146, call; 146, 147; 146, 152; 147, subscript; 147, 148; 147, 151; 148, subscript; 148, 149; 148, 150; 149, identifier:vcf_processor; 150, identifier:mut_type; 151, identifier:caller; 152, argument_list; 152, 153; 152, 154; 152, 159; 152, 160; 153, identifier:job; 154, subscript; 154, 155; 154, 158; 155, subscript; 155, 156; 155, 157; 156, identifier:mutations; 157, identifier:caller; 158, identifier:chrom; 159, identifier:work_dir; 160, identifier:univ_options; 161, for_in_clause; 161, 162; 161, 163; 162, identifier:caller; 163, subscript; 163, 164; 163, 165; 164, identifier:vcf_processor; 165, identifier:mut_type; 166, comment; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 172; 169, subscript; 169, 170; 169, 171; 170, identifier:perchrom_mutations; 171, string:'strelka'; 172, subscript; 172, 173; 172, 174; 173, identifier:perchrom_mutations; 174, binary_operator:+; 174, 175; 174, 176; 175, string:'strelka_'; 176, identifier:mut_type; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:perchrom_mutations; 181, identifier:pop; 182, argument_list; 182, 183; 183, binary_operator:+; 183, 184; 183, 185; 184, string:'strelka_'; 185, identifier:mut_type; 186, comment; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:vcf_lists; 190, dictionary_comprehension; 190, 191; 190, 197; 191, pair; 191, 192; 191, 193; 192, identifier:caller; 193, call; 193, 194; 193, 195; 194, identifier:read_vcf; 195, argument_list; 195, 196; 196, identifier:vcf_file; 197, for_in_clause; 197, 198; 197, 201; 198, pattern_list; 198, 199; 198, 200; 199, identifier:caller; 200, identifier:vcf_file; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:perchrom_mutations; 204, identifier:items; 205, argument_list; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:all_positions; 209, call; 209, 210; 209, 211; 210, identifier:list; 211, argument_list; 211, 212; 212, call; 212, 213; 212, 214; 213, identifier:set; 214, argument_list; 214, 215; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:itertools; 218, identifier:chain; 219, argument_list; 219, 220; 220, list_splat; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:vcf_lists; 224, identifier:values; 225, argument_list; 226, for_statement; 226, 227; 226, 228; 226, 232; 227, identifier:position; 228, call; 228, 229; 228, 230; 229, identifier:sorted; 230, argument_list; 230, 231; 231, identifier:all_positions; 232, block; 232, 233; 232, 251; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 236; 235, identifier:hits; 236, dictionary_comprehension; 236, 237; 236, 244; 237, pair; 237, 238; 237, 239; 238, identifier:caller; 239, comparison_operator:in; 239, 240; 239, 241; 240, identifier:position; 241, subscript; 241, 242; 241, 243; 242, identifier:vcf_lists; 243, identifier:caller; 244, for_in_clause; 244, 245; 244, 246; 245, identifier:caller; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:perchrom_mutations; 249, identifier:keys; 250, argument_list; 251, if_statement; 251, 252; 251, 264; 252, comparison_operator:>=; 252, 253; 252, 261; 253, call; 253, 254; 253, 255; 254, identifier:sum; 255, argument_list; 255, 256; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:hits; 259, identifier:values; 260, argument_list; 261, subscript; 261, 262; 261, 263; 262, identifier:majority; 263, identifier:mut_type; 264, block; 264, 265; 264, 286; 264, 296; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 268; 267, identifier:callers; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, string:','; 271, identifier:join; 272, argument_list; 272, 273; 273, list_comprehension; 273, 274; 273, 275; 273, 284; 274, identifier:caller; 275, for_in_clause; 275, 276; 275, 279; 276, pattern_list; 276, 277; 276, 278; 277, identifier:caller; 278, identifier:hit; 279, call; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:hits; 282, identifier:items; 283, argument_list; 284, if_clause; 284, 285; 285, identifier:hit; 286, assert_statement; 286, 287; 287, comparison_operator:not; 287, 288; 287, 291; 288, subscript; 288, 289; 288, 290; 289, identifier:position; 290, integer:1; 291, subscript; 291, 292; 291, 293; 292, identifier:accepted_hits; 293, subscript; 293, 294; 293, 295; 294, identifier:position; 295, integer:0; 296, expression_statement; 296, 297; 297, assignment; 297, 298; 297, 307; 298, subscript; 298, 299; 298, 304; 299, subscript; 299, 300; 299, 301; 300, identifier:accepted_hits; 301, subscript; 301, 302; 301, 303; 302, identifier:position; 303, integer:0; 304, subscript; 304, 305; 304, 306; 305, identifier:position; 306, integer:1; 307, tuple; 307, 308; 307, 311; 307, 314; 308, subscript; 308, 309; 308, 310; 309, identifier:position; 310, integer:2; 311, subscript; 311, 312; 311, 313; 312, identifier:position; 313, integer:3; 314, identifier:callers; 315, with_statement; 315, 316; 315, 335; 316, with_clause; 316, 317; 317, with_item; 317, 318; 318, as_pattern; 318, 319; 318, 333; 319, call; 319, 320; 319, 321; 320, identifier:open; 321, argument_list; 321, 322; 321, 332; 322, call; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, string:''; 325, identifier:join; 326, argument_list; 326, 327; 327, list:[work_dir, '/', chrom, '.vcf']; 327, 328; 327, 329; 327, 330; 327, 331; 328, identifier:work_dir; 329, string:'/'; 330, identifier:chrom; 331, string:'.vcf'; 332, string:'w'; 333, as_pattern_target; 333, 334; 334, identifier:outfile; 335, block; 335, 336; 335, 344; 335, 352; 335, 360; 336, expression_statement; 336, 337; 337, call; 337, 338; 337, 339; 338, identifier:print; 339, argument_list; 339, 340; 339, 341; 340, string:'##fileformat=VCFv4.0'; 341, keyword_argument; 341, 342; 341, 343; 342, identifier:file; 343, identifier:outfile; 344, expression_statement; 344, 345; 345, call; 345, 346; 345, 347; 346, identifier:print; 347, argument_list; 347, 348; 347, 349; 348, string:'##INFO=<ID=callers,Number=.,Type=String,Description=List of supporting callers.'; 349, keyword_argument; 349, 350; 349, 351; 350, identifier:file; 351, identifier:outfile; 352, expression_statement; 352, 353; 353, call; 353, 354; 353, 355; 354, identifier:print; 355, argument_list; 355, 356; 355, 357; 356, string:'#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO'; 357, keyword_argument; 357, 358; 357, 359; 358, identifier:file; 359, identifier:outfile; 360, for_statement; 360, 361; 360, 362; 360, 370; 361, identifier:chrom; 362, call; 362, 363; 362, 364; 363, identifier:chrom_sorted; 364, argument_list; 364, 365; 365, call; 365, 366; 365, 369; 366, attribute; 366, 367; 366, 368; 367, identifier:accepted_hits; 368, identifier:keys; 369, argument_list; 370, block; 370, 371; 371, for_statement; 371, 372; 371, 373; 371, 379; 372, identifier:position; 373, call; 373, 374; 373, 375; 374, identifier:sorted; 375, argument_list; 375, 376; 376, subscript; 376, 377; 376, 378; 377, identifier:accepted_hits; 378, identifier:chrom; 379, block; 379, 380; 380, expression_statement; 380, 381; 381, call; 381, 382; 381, 383; 382, identifier:print; 383, argument_list; 383, 384; 383, 385; 383, 386; 383, 387; 383, 394; 383, 401; 383, 402; 383, 403; 383, 412; 383, 415; 384, identifier:chrom; 385, identifier:position; 386, string:'.'; 387, subscript; 387, 388; 387, 393; 388, subscript; 388, 389; 388, 392; 389, subscript; 389, 390; 389, 391; 390, identifier:accepted_hits; 391, identifier:chrom; 392, identifier:position; 393, integer:0; 394, subscript; 394, 395; 394, 400; 395, subscript; 395, 396; 395, 399; 396, subscript; 396, 397; 396, 398; 397, identifier:accepted_hits; 398, identifier:chrom; 399, identifier:position; 400, integer:1; 401, string:'.'; 402, string:'PASS'; 403, binary_operator:+; 403, 404; 403, 405; 404, string:'callers='; 405, subscript; 405, 406; 405, 411; 406, subscript; 406, 407; 406, 410; 407, subscript; 407, 408; 407, 409; 408, identifier:accepted_hits; 409, identifier:chrom; 410, identifier:position; 411, integer:2; 412, keyword_argument; 412, 413; 412, 414; 413, identifier:sep; 414, string:'\t'; 415, keyword_argument; 415, 416; 415, 417; 416, identifier:file; 417, identifier:outfile; 418, expression_statement; 418, 419; 419, assignment; 419, 420; 419, 421; 420, identifier:fsid; 421, call; 421, 422; 421, 427; 422, attribute; 422, 423; 422, 426; 423, attribute; 423, 424; 423, 425; 424, identifier:job; 425, identifier:fileStore; 426, identifier:writeGlobalFile; 427, argument_list; 427, 428; 428, attribute; 428, 429; 428, 430; 429, identifier:outfile; 430, identifier:name; 431, expression_statement; 431, 432; 432, call; 432, 433; 432, 434; 433, identifier:export_results; 434, argument_list; 434, 435; 434, 436; 434, 437; 434, 440; 434, 441; 435, identifier:job; 436, identifier:fsid; 437, attribute; 437, 438; 437, 439; 438, identifier:outfile; 439, identifier:name; 440, identifier:univ_options; 441, keyword_argument; 441, 442; 441, 443; 442, identifier:subfolder; 443, string:'mutations/merged'; 444, return_statement; 444, 445; 445, identifier:fsid | def merge_perchrom_mutations(job, chrom, mutations, univ_options):
"""
Merge the mutation calls for a single chromosome.
:param str chrom: Chromosome to process
:param dict mutations: dict of dicts of the various mutation caller names as keys, and a dict of
per chromosome job store ids for vcfs as value
:param dict univ_options: Dict of universal options used by almost all tools
:returns fsID for vcf contaning merged calls for the given chromosome
:rtype: toil.fileStore.FileID
"""
work_dir = os.getcwd()
from protect.mutation_calling.muse import process_muse_vcf
from protect.mutation_calling.mutect import process_mutect_vcf
from protect.mutation_calling.radia import process_radia_vcf
from protect.mutation_calling.somaticsniper import process_somaticsniper_vcf
from protect.mutation_calling.strelka import process_strelka_vcf
mutations.pop('indels')
mutations['strelka_indels'] = mutations['strelka']['indels']
mutations['strelka_snvs'] = mutations['strelka']['snvs']
vcf_processor = {'snvs': {'mutect': process_mutect_vcf,
'muse': process_muse_vcf,
'radia': process_radia_vcf,
'somaticsniper': process_somaticsniper_vcf,
'strelka_snvs': process_strelka_vcf
},
'indels': {'strelka_indels': process_strelka_vcf
}
}
# 'fusions': lambda x: None,
# 'indels': lambda x: None}
# For now, let's just say 2 out of n need to call it.
# num_preds = len(mutations)
# majority = int((num_preds + 0.5) / 2)
majority = {'snvs': 2,
'indels': 1}
accepted_hits = defaultdict(dict)
for mut_type in vcf_processor.keys():
# Get input files
perchrom_mutations = {caller: vcf_processor[mut_type][caller](job, mutations[caller][chrom],
work_dir, univ_options)
for caller in vcf_processor[mut_type]}
# Process the strelka key
perchrom_mutations['strelka'] = perchrom_mutations['strelka_' + mut_type]
perchrom_mutations.pop('strelka_' + mut_type)
# Read in each file to a dict
vcf_lists = {caller: read_vcf(vcf_file) for caller, vcf_file in perchrom_mutations.items()}
all_positions = list(set(itertools.chain(*vcf_lists.values())))
for position in sorted(all_positions):
hits = {caller: position in vcf_lists[caller] for caller in perchrom_mutations.keys()}
if sum(hits.values()) >= majority[mut_type]:
callers = ','.join([caller for caller, hit in hits.items() if hit])
assert position[1] not in accepted_hits[position[0]]
accepted_hits[position[0]][position[1]] = (position[2], position[3], callers)
with open(''.join([work_dir, '/', chrom, '.vcf']), 'w') as outfile:
print('##fileformat=VCFv4.0', file=outfile)
print('##INFO=<ID=callers,Number=.,Type=String,Description=List of supporting callers.',
file=outfile)
print('#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO', file=outfile)
for chrom in chrom_sorted(accepted_hits.keys()):
for position in sorted(accepted_hits[chrom]):
print(chrom, position, '.', accepted_hits[chrom][position][0],
accepted_hits[chrom][position][1], '.', 'PASS',
'callers=' + accepted_hits[chrom][position][2], sep='\t', file=outfile)
fsid = job.fileStore.writeGlobalFile(outfile.name)
export_results(job, fsid, outfile.name, univ_options, subfolder='mutations/merged')
return fsid |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:check; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:domain; 5, identifier:prefix; 6, identifier:code; 7, default_parameter; 7, 8; 7, 9; 8, identifier:strategies; 9, string:'*'; 10, block; 10, 11; 10, 13; 10, 32; 10, 51; 10, 70; 10, 89; 11, expression_statement; 11, 12; 12, comment; 13, if_statement; 13, 14; 13, 21; 14, boolean_operator:or; 14, 15; 14, 18; 15, comparison_operator:==; 15, 16; 15, 17; 16, identifier:strategies; 17, string:'*'; 18, comparison_operator:in; 18, 19; 18, 20; 19, string:'dns_txt'; 20, identifier:strategies; 21, block; 21, 22; 22, if_statement; 22, 23; 22, 29; 23, call; 23, 24; 23, 25; 24, identifier:check_dns_txt; 25, argument_list; 25, 26; 25, 27; 25, 28; 26, identifier:domain; 27, identifier:prefix; 28, identifier:code; 29, block; 29, 30; 30, return_statement; 30, 31; 31, True; 32, if_statement; 32, 33; 32, 40; 33, boolean_operator:or; 33, 34; 33, 37; 34, comparison_operator:==; 34, 35; 34, 36; 35, identifier:strategies; 36, string:'*'; 37, comparison_operator:in; 37, 38; 37, 39; 38, string:'dns_cname'; 39, identifier:strategies; 40, block; 40, 41; 41, if_statement; 41, 42; 41, 48; 42, call; 42, 43; 42, 44; 43, identifier:check_dns_cname; 44, argument_list; 44, 45; 44, 46; 44, 47; 45, identifier:domain; 46, identifier:prefix; 47, identifier:code; 48, block; 48, 49; 49, return_statement; 49, 50; 50, True; 51, if_statement; 51, 52; 51, 59; 52, boolean_operator:or; 52, 53; 52, 56; 53, comparison_operator:==; 53, 54; 53, 55; 54, identifier:strategies; 55, string:'*'; 56, comparison_operator:in; 56, 57; 56, 58; 57, string:'meta_tag'; 58, identifier:strategies; 59, block; 59, 60; 60, if_statement; 60, 61; 60, 67; 61, call; 61, 62; 61, 63; 62, identifier:check_meta_tag; 63, argument_list; 63, 64; 63, 65; 63, 66; 64, identifier:domain; 65, identifier:prefix; 66, identifier:code; 67, block; 67, 68; 68, return_statement; 68, 69; 69, True; 70, if_statement; 70, 71; 70, 78; 71, boolean_operator:or; 71, 72; 71, 75; 72, comparison_operator:==; 72, 73; 72, 74; 73, identifier:strategies; 74, string:'*'; 75, comparison_operator:in; 75, 76; 75, 77; 76, string:'html_file'; 77, identifier:strategies; 78, block; 78, 79; 79, if_statement; 79, 80; 79, 86; 80, call; 80, 81; 80, 82; 81, identifier:check_html_file; 82, argument_list; 82, 83; 82, 84; 82, 85; 83, identifier:domain; 84, identifier:prefix; 85, identifier:code; 86, block; 86, 87; 87, return_statement; 87, 88; 88, True; 89, return_statement; 89, 90; 90, False | def check(domain, prefix, code, strategies='*'):
"""
Check the ownership of a domain by going thru a serie of strategies.
If at least one strategy succeed, the domain is considered verified,
and this methods returns true.
The prefix is a fixed DNS safe string like "yourservice-domain-verification"
and the code is a random value associated to this domain. It is advised to
prefix the code by a fixed value that is unique to your service like
"yourservice2k3dWdk9dwz".
By default all available strategies are tested. You can limit the check
to a limited set of strategies by passing a comma separated list of
strategy names like "nds_txt,dns_cname". See the "strategies" module
for a full list of avaialble strategies.
"""
if strategies == '*' or 'dns_txt' in strategies:
if check_dns_txt(domain, prefix, code):
return True
if strategies == '*' or 'dns_cname' in strategies:
if check_dns_cname(domain, prefix, code):
return True
if strategies == '*' or 'meta_tag' in strategies:
if check_meta_tag(domain, prefix, code):
return True
if strategies == '*' or 'html_file' in strategies:
if check_html_file(domain, prefix, code):
return True
return False |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:register_cache_buster; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:app; 6, default_parameter; 6, 7; 6, 8; 7, identifier:config; 8, None; 9, block; 9, 10; 9, 12; 9, 30; 9, 34; 9, 35; 9, 39; 9, 40; 9, 41; 9, 50; 9, 51; 9, 169; 9, 178; 9, 191; 9, 204; 9, 233; 9, 265; 9, 266; 9, 274; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 24; 13, not_operator; 13, 14; 14, parenthesized_expression; 14, 15; 15, boolean_operator:or; 15, 16; 15, 19; 16, comparison_operator:is; 16, 17; 16, 18; 17, identifier:config; 18, None; 19, call; 19, 20; 19, 21; 20, identifier:isinstance; 21, argument_list; 21, 22; 21, 23; 22, identifier:config; 23, identifier:dict; 24, block; 24, 25; 25, raise_statement; 25, 26; 26, call; 26, 27; 26, 28; 27, identifier:ValueError; 28, argument_list; 28, 29; 29, string:"`config` must be an instance of dict or None"; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:bust_map; 33, dictionary; 34, comment; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:unbust_map; 38, dictionary; 39, comment; 40, comment; 41, expression_statement; 41, 42; 42, call; 42, 43; 42, 48; 43, attribute; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:app; 46, identifier:logger; 47, identifier:debug; 48, argument_list; 48, 49; 49, string:'Starting computing hashes for static assets'; 50, comment; 51, for_statement; 51, 52; 51, 56; 51, 64; 52, pattern_list; 52, 53; 52, 54; 52, 55; 53, identifier:dirpath; 54, identifier:dirnames; 55, identifier:filenames; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:os; 59, identifier:walk; 60, argument_list; 60, 61; 61, attribute; 61, 62; 61, 63; 62, identifier:app; 63, identifier:static_folder; 64, block; 64, 65; 65, for_statement; 65, 66; 65, 67; 65, 68; 65, 69; 66, identifier:filename; 67, identifier:filenames; 68, comment; 69, block; 69, 70; 69, 82; 69, 92; 69, 101; 69, 136; 69, 137; 69, 151; 69, 152; 69, 156; 69, 157; 69, 163; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:rooted_filename; 73, call; 73, 74; 73, 79; 74, attribute; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:os; 77, identifier:path; 78, identifier:join; 79, argument_list; 79, 80; 79, 81; 80, identifier:dirpath; 81, identifier:filename; 82, if_statement; 82, 83; 82, 90; 83, not_operator; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:self; 87, identifier:__is_file_to_be_busted; 88, argument_list; 88, 89; 89, identifier:rooted_filename; 90, block; 90, 91; 91, continue_statement; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 99; 94, attribute; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:app; 97, identifier:logger; 98, identifier:debug; 99, argument_list; 99, 100; 100, string:f'Computing hashes for {rooted_filename}'; 101, with_statement; 101, 102; 101, 112; 102, with_clause; 102, 103; 103, with_item; 103, 104; 104, as_pattern; 104, 105; 104, 110; 105, call; 105, 106; 105, 107; 106, identifier:open; 107, argument_list; 107, 108; 107, 109; 108, identifier:rooted_filename; 109, string:'rb'; 110, as_pattern_target; 110, 111; 111, identifier:f; 112, block; 112, 113; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:version; 116, subscript; 116, 117; 116, 131; 117, call; 117, 118; 117, 130; 118, attribute; 118, 119; 118, 129; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:hashlib; 122, identifier:md5; 123, argument_list; 123, 124; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:f; 127, identifier:read; 128, argument_list; 129, identifier:hexdigest; 130, argument_list; 131, slice; 131, 132; 131, 133; 132, colon; 133, attribute; 133, 134; 133, 135; 134, identifier:self; 135, identifier:hash_size; 136, comment; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:unbusted; 140, call; 140, 141; 140, 146; 141, attribute; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:os; 144, identifier:path; 145, identifier:relpath; 146, argument_list; 146, 147; 146, 148; 147, identifier:rooted_filename; 148, attribute; 148, 149; 148, 150; 149, identifier:app; 150, identifier:static_folder; 151, comment; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:busted; 155, string:f"{unbusted}?q={version}"; 156, comment; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 162; 159, subscript; 159, 160; 159, 161; 160, identifier:bust_map; 161, identifier:unbusted; 162, identifier:busted; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 168; 165, subscript; 165, 166; 165, 167; 166, identifier:unbust_map; 167, identifier:busted; 168, identifier:unbusted; 169, expression_statement; 169, 170; 170, call; 170, 171; 170, 176; 171, attribute; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:app; 174, identifier:logger; 175, identifier:debug; 176, argument_list; 176, 177; 177, string:'Finished Starting computing hashes for static assets'; 178, function_definition; 178, 179; 178, 180; 178, 182; 179, function_name:bust_filename; 180, parameters; 180, 181; 181, identifier:file; 182, block; 182, 183; 183, return_statement; 183, 184; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:bust_map; 187, identifier:get; 188, argument_list; 188, 189; 188, 190; 189, identifier:file; 190, identifier:file; 191, function_definition; 191, 192; 191, 193; 191, 195; 192, function_name:unbust_filename; 193, parameters; 193, 194; 194, identifier:file; 195, block; 195, 196; 196, return_statement; 196, 197; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:unbust_map; 200, identifier:get; 201, argument_list; 201, 202; 201, 203; 202, identifier:file; 203, identifier:file; 204, decorated_definition; 204, 205; 204, 209; 205, decorator; 205, 206; 206, attribute; 206, 207; 206, 208; 207, identifier:app; 208, identifier:url_defaults; 209, function_definition; 209, 210; 209, 211; 209, 214; 210, function_name:reverse_to_cache_busted_url; 211, parameters; 211, 212; 211, 213; 212, identifier:endpoint; 213, identifier:values; 214, block; 214, 215; 214, 217; 215, expression_statement; 215, 216; 216, comment; 217, if_statement; 217, 218; 217, 221; 218, comparison_operator:==; 218, 219; 218, 220; 219, identifier:endpoint; 220, string:'static'; 221, block; 221, 222; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 227; 224, subscript; 224, 225; 224, 226; 225, identifier:values; 226, string:'filename'; 227, call; 227, 228; 227, 229; 228, identifier:bust_filename; 229, argument_list; 229, 230; 230, subscript; 230, 231; 230, 232; 231, identifier:values; 232, string:'filename'; 233, function_definition; 233, 234; 233, 235; 233, 240; 234, function_name:debusting_static_view; 235, parameters; 235, 236; 235, 238; 236, list_splat_pattern; 236, 237; 237, identifier:args; 238, dictionary_splat_pattern; 238, 239; 239, identifier:kwargs; 240, block; 240, 241; 240, 243; 240, 257; 241, expression_statement; 241, 242; 242, comment; 243, expression_statement; 243, 244; 244, assignment; 244, 245; 244, 248; 245, subscript; 245, 246; 245, 247; 246, identifier:kwargs; 247, string:'filename'; 248, call; 248, 249; 248, 250; 249, identifier:unbust_filename; 250, argument_list; 250, 251; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:kwargs; 254, identifier:get; 255, argument_list; 255, 256; 256, string:'filename'; 257, return_statement; 257, 258; 258, call; 258, 259; 258, 260; 259, identifier:original_static_view; 260, argument_list; 260, 261; 260, 263; 261, list_splat; 261, 262; 262, identifier:args; 263, dictionary_splat; 263, 264; 264, identifier:kwargs; 265, comment; 266, expression_statement; 266, 267; 267, assignment; 267, 268; 267, 269; 268, identifier:original_static_view; 269, subscript; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:app; 272, identifier:view_functions; 273, string:'static'; 274, expression_statement; 274, 275; 275, assignment; 275, 276; 275, 281; 276, subscript; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:app; 279, identifier:view_functions; 280, string:'static'; 281, identifier:debusting_static_view | def register_cache_buster(self, app, config=None):
"""
Register `app` in cache buster so that `url_for` adds a unique prefix
to URLs generated for the `'static'` endpoint. Also make the app able
to serve cache-busted static files.
This allows setting long cache expiration values on static resources
because whenever the resource changes, so does its URL.
"""
if not (config is None or isinstance(config, dict)):
raise ValueError("`config` must be an instance of dict or None")
bust_map = {} # map from an unbusted filename to a busted one
unbust_map = {} # map from a busted filename to an unbusted one
# http://flask.pocoo.org/docs/0.12/api/#flask.Flask.static_folder
app.logger.debug('Starting computing hashes for static assets')
# compute (un)bust tables.
for dirpath, dirnames, filenames in os.walk(app.static_folder):
for filename in filenames:
# compute version component
rooted_filename = os.path.join(dirpath, filename)
if not self.__is_file_to_be_busted(rooted_filename):
continue
app.logger.debug(f'Computing hashes for {rooted_filename}')
with open(rooted_filename, 'rb') as f:
version = hashlib.md5(
f.read()
).hexdigest()[:self.hash_size]
# add version
unbusted = os.path.relpath(rooted_filename, app.static_folder)
# busted = os.path.join(version, unbusted)
busted = f"{unbusted}?q={version}"
# save computation to map
bust_map[unbusted] = busted
unbust_map[busted] = unbusted
app.logger.debug('Finished Starting computing hashes for static assets')
def bust_filename(file):
return bust_map.get(file, file)
def unbust_filename(file):
return unbust_map.get(file, file)
@app.url_defaults
def reverse_to_cache_busted_url(endpoint, values):
"""
Make `url_for` produce busted filenames when using the 'static'
endpoint.
"""
if endpoint == 'static':
values['filename'] = bust_filename(values['filename'])
def debusting_static_view(*args, **kwargs):
"""
Serve a request for a static file having a busted name.
"""
kwargs['filename'] = unbust_filename(kwargs.get('filename'))
return original_static_view(*args, **kwargs)
# Replace the default static file view with our debusting view.
original_static_view = app.view_functions['static']
app.view_functions['static'] = debusting_static_view |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:parse_inventory; 3, parameters; 3, 4; 4, default_parameter; 4, 5; 4, 6; 5, identifier:inventory_output; 6, None; 7, block; 7, 8; 7, 10; 7, 29; 7, 36; 7, 37; 7, 41; 7, 45; 7, 103; 7, 116; 7, 135; 7, 148; 7, 167; 7, 180; 7, 194; 7, 207; 7, 221; 7, 234; 7, 252; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:udi; 13, dictionary; 13, 14; 13, 17; 13, 20; 13, 23; 13, 26; 14, pair; 14, 15; 14, 16; 15, string:"name"; 16, string:""; 17, pair; 17, 18; 17, 19; 18, string:"description"; 19, string:""; 20, pair; 20, 21; 20, 22; 21, string:"pid"; 22, string:""; 23, pair; 23, 24; 23, 25; 24, string:"vid"; 25, string:""; 26, pair; 26, 27; 26, 28; 27, string:"sn"; 28, string:""; 29, if_statement; 29, 30; 29, 33; 30, comparison_operator:is; 30, 31; 30, 32; 31, identifier:inventory_output; 32, None; 33, block; 33, 34; 34, return_statement; 34, 35; 35, identifier:udi; 36, comment; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:capture_next; 40, False; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:chassis_udi_text; 44, None; 45, for_statement; 45, 46; 45, 47; 45, 53; 46, identifier:line; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:inventory_output; 50, identifier:split; 51, argument_list; 51, 52; 52, string:'\n'; 53, block; 53, 54; 53, 62; 53, 91; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:lc_line; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:line; 60, identifier:lower; 61, argument_list; 62, if_statement; 62, 63; 62, 81; 63, boolean_operator:and; 63, 64; 63, 80; 64, boolean_operator:and; 64, 65; 64, 77; 65, parenthesized_expression; 65, 66; 66, boolean_operator:or; 66, 67; 66, 74; 67, boolean_operator:or; 67, 68; 67, 71; 68, comparison_operator:in; 68, 69; 68, 70; 69, string:'chassis'; 70, identifier:lc_line; 71, comparison_operator:in; 71, 72; 71, 73; 72, string:'switch system'; 73, identifier:lc_line; 74, comparison_operator:in; 74, 75; 74, 76; 75, string:'rack'; 76, identifier:lc_line; 77, comparison_operator:in; 77, 78; 77, 79; 78, string:'name'; 79, identifier:lc_line; 80, string:'descr'; 81, block; 81, 82; 81, 86; 81, 90; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:capture_next; 85, True; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:chassis_udi_text; 89, identifier:line; 90, continue_statement; 91, if_statement; 91, 92; 91, 93; 92, identifier:capture_next; 93, block; 93, 94; 93, 102; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:inventory_output; 97, binary_operator:+; 97, 98; 97, 101; 98, binary_operator:+; 98, 99; 98, 100; 99, identifier:chassis_udi_text; 100, string:"\n"; 101, identifier:line; 102, break_statement; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:match; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:re; 109, identifier:search; 110, argument_list; 110, 111; 110, 112; 110, 113; 111, string:r"(?i)NAME: (?P<name>.*?),? (?i)DESCR"; 112, identifier:inventory_output; 113, attribute; 113, 114; 113, 115; 114, identifier:re; 115, identifier:MULTILINE; 116, if_statement; 116, 117; 116, 118; 117, identifier:match; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 124; 121, subscript; 121, 122; 121, 123; 122, identifier:udi; 123, string:'name'; 124, call; 124, 125; 124, 133; 125, attribute; 125, 126; 125, 132; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:match; 129, identifier:group; 130, argument_list; 130, 131; 131, string:'name'; 132, identifier:strip; 133, argument_list; 133, 134; 134, string:'" ,'; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:match; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:re; 141, identifier:search; 142, argument_list; 142, 143; 142, 144; 142, 145; 143, string:r"(?i)DESCR: (?P<description>.*)"; 144, identifier:inventory_output; 145, attribute; 145, 146; 145, 147; 146, identifier:re; 147, identifier:MULTILINE; 148, if_statement; 148, 149; 148, 150; 149, identifier:match; 150, block; 150, 151; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 156; 153, subscript; 153, 154; 153, 155; 154, identifier:udi; 155, string:'description'; 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:match; 161, identifier:group; 162, argument_list; 162, 163; 163, string:'description'; 164, identifier:strip; 165, argument_list; 165, 166; 166, string:'" '; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:match; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:re; 173, identifier:search; 174, argument_list; 174, 175; 174, 176; 174, 177; 175, string:r"(?i)PID: (?P<pid>.*?),? "; 176, identifier:inventory_output; 177, attribute; 177, 178; 177, 179; 178, identifier:re; 179, identifier:MULTILINE; 180, if_statement; 180, 181; 180, 182; 181, identifier:match; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 188; 185, subscript; 185, 186; 185, 187; 186, identifier:udi; 187, string:'pid'; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:match; 191, identifier:group; 192, argument_list; 192, 193; 193, string:'pid'; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:match; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:re; 200, identifier:search; 201, argument_list; 201, 202; 201, 203; 201, 204; 202, string:r"(?i)VID: (?P<vid>.*?),? "; 203, identifier:inventory_output; 204, attribute; 204, 205; 204, 206; 205, identifier:re; 206, identifier:MULTILINE; 207, if_statement; 207, 208; 207, 209; 208, identifier:match; 209, block; 209, 210; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 215; 212, subscript; 212, 213; 212, 214; 213, identifier:udi; 214, string:'vid'; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:match; 218, identifier:group; 219, argument_list; 219, 220; 220, string:'vid'; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:match; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:re; 227, identifier:search; 228, argument_list; 228, 229; 228, 230; 228, 231; 229, string:r"(?i)SN: (?P<sn>.*)"; 230, identifier:inventory_output; 231, attribute; 231, 232; 231, 233; 232, identifier:re; 233, identifier:MULTILINE; 234, if_statement; 234, 235; 234, 236; 235, identifier:match; 236, block; 236, 237; 237, expression_statement; 237, 238; 238, assignment; 238, 239; 238, 242; 239, subscript; 239, 240; 239, 241; 240, identifier:udi; 241, string:'sn'; 242, call; 242, 243; 242, 251; 243, attribute; 243, 244; 243, 250; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:match; 247, identifier:group; 248, argument_list; 248, 249; 249, string:'sn'; 250, identifier:strip; 251, argument_list; 252, return_statement; 252, 253; 253, identifier:udi | def parse_inventory(inventory_output=None):
"""Parse the inventory text and return udi dict."""
udi = {
"name": "",
"description": "",
"pid": "",
"vid": "",
"sn": ""
}
if inventory_output is None:
return udi
# find the record with chassis text in name or descr
capture_next = False
chassis_udi_text = None
for line in inventory_output.split('\n'):
lc_line = line.lower()
if ('chassis' in lc_line or 'switch system' in lc_line or 'rack' in lc_line) and 'name' in lc_line and 'descr':
capture_next = True
chassis_udi_text = line
continue
if capture_next:
inventory_output = chassis_udi_text + "\n" + line
break
match = re.search(r"(?i)NAME: (?P<name>.*?),? (?i)DESCR", inventory_output, re.MULTILINE)
if match:
udi['name'] = match.group('name').strip('" ,')
match = re.search(r"(?i)DESCR: (?P<description>.*)", inventory_output, re.MULTILINE)
if match:
udi['description'] = match.group('description').strip('" ')
match = re.search(r"(?i)PID: (?P<pid>.*?),? ", inventory_output, re.MULTILINE)
if match:
udi['pid'] = match.group('pid')
match = re.search(r"(?i)VID: (?P<vid>.*?),? ", inventory_output, re.MULTILINE)
if match:
udi['vid'] = match.group('vid')
match = re.search(r"(?i)SN: (?P<sn>.*)", inventory_output, re.MULTILINE)
if match:
udi['sn'] = match.group('sn').strip()
return udi |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:yaml_file_to_dict; 3, parameters; 3, 4; 3, 5; 4, identifier:script_name; 5, default_parameter; 5, 6; 5, 7; 6, identifier:path; 7, None; 8, block; 8, 9; 8, 11; 8, 50; 8, 108; 8, 124; 8, 138; 8, 159; 8, 166; 8, 195; 8, 218; 8, 246; 9, expression_statement; 9, 10; 10, comment; 11, function_definition; 11, 12; 11, 13; 11, 15; 12, function_name:load_yaml; 13, parameters; 13, 14; 14, identifier:file_path; 15, block; 15, 16; 15, 18; 15, 48; 16, expression_statement; 16, 17; 17, comment; 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, 24; 23, identifier:open; 24, argument_list; 24, 25; 24, 26; 25, identifier:file_path; 26, string:'r'; 27, as_pattern_target; 27, 28; 28, identifier:yamlfile; 29, block; 29, 30; 30, try_statement; 30, 31; 30, 41; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:dictionary; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:yaml; 38, identifier:load; 39, argument_list; 39, 40; 40, identifier:yamlfile; 41, except_clause; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:yaml; 44, identifier:YAMLError; 45, block; 45, 46; 46, return_statement; 46, 47; 47, dictionary; 48, return_statement; 48, 49; 49, identifier:dictionary; 50, function_definition; 50, 51; 50, 52; 50, 55; 51, function_name:merge; 52, parameters; 52, 53; 52, 54; 53, identifier:user; 54, identifier:default; 55, block; 55, 56; 55, 58; 55, 106; 56, expression_statement; 56, 57; 57, comment; 58, if_statement; 58, 59; 58, 70; 59, boolean_operator:and; 59, 60; 59, 65; 60, call; 60, 61; 60, 62; 61, identifier:isinstance; 62, argument_list; 62, 63; 62, 64; 63, identifier:user; 64, identifier:dict; 65, call; 65, 66; 65, 67; 66, identifier:isinstance; 67, argument_list; 67, 68; 67, 69; 68, identifier:default; 69, identifier:dict; 70, block; 70, 71; 71, for_statement; 71, 72; 71, 75; 71, 80; 72, pattern_list; 72, 73; 72, 74; 73, identifier:k; 74, identifier:v; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:default; 78, identifier:iteritems; 79, argument_list; 80, block; 80, 81; 81, if_statement; 81, 82; 81, 85; 81, 92; 82, comparison_operator:not; 82, 83; 82, 84; 83, identifier:k; 84, identifier:user; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 91; 88, subscript; 88, 89; 88, 90; 89, identifier:user; 90, identifier:k; 91, identifier:v; 92, else_clause; 92, 93; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 99; 96, subscript; 96, 97; 96, 98; 97, identifier:user; 98, identifier:k; 99, call; 99, 100; 99, 101; 100, identifier:merge; 101, argument_list; 101, 102; 101, 105; 102, subscript; 102, 103; 102, 104; 103, identifier:user; 104, identifier:k; 105, identifier:v; 106, return_statement; 106, 107; 107, identifier:user; 108, if_statement; 108, 109; 108, 112; 109, comparison_operator:is; 109, 110; 109, 111; 110, identifier:path; 111, None; 112, block; 112, 113; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:path; 116, call; 116, 117; 116, 122; 117, attribute; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:os; 120, identifier:path; 121, identifier:abspath; 122, argument_list; 122, 123; 123, string:'.'; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:config_file_path; 127, call; 127, 128; 127, 133; 128, attribute; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:os; 131, identifier:path; 132, identifier:join; 133, argument_list; 133, 134; 133, 135; 134, identifier:path; 135, binary_operator:+; 135, 136; 135, 137; 136, identifier:script_name; 137, string:'.yaml'; 138, if_statement; 138, 139; 138, 148; 139, not_operator; 139, 140; 140, call; 140, 141; 140, 146; 141, attribute; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:os; 144, identifier:path; 145, identifier:exists; 146, argument_list; 146, 147; 147, identifier:config_file_path; 148, block; 148, 149; 149, raise_statement; 149, 150; 150, call; 150, 151; 150, 152; 151, identifier:RuntimeError; 152, argument_list; 152, 153; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, string:'Config file does not exist: {}'; 156, identifier:format; 157, argument_list; 157, 158; 158, identifier:config_file_path; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:default_dict; 162, call; 162, 163; 162, 164; 163, identifier:load_yaml; 164, argument_list; 164, 165; 165, identifier:config_file_path; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:user_config_file_path; 169, call; 169, 170; 169, 175; 170, attribute; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:os; 173, identifier:path; 174, identifier:join; 175, argument_list; 175, 176; 175, 184; 175, 185; 176, call; 176, 177; 176, 182; 177, attribute; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:os; 180, identifier:path; 181, identifier:expanduser; 182, argument_list; 182, 183; 183, string:'~'; 184, string:'.condoor'; 185, binary_operator:+; 185, 186; 185, 194; 186, call; 186, 187; 186, 192; 187, attribute; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:os; 190, identifier:path; 191, identifier:basename; 192, argument_list; 192, 193; 193, identifier:script_name; 194, string:'.yaml'; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:user_config_file_path; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:os; 201, identifier:getenv; 202, argument_list; 202, 203; 202, 217; 203, binary_operator:+; 203, 204; 203, 205; 204, string:'CONDOOR_'; 205, call; 205, 206; 205, 216; 206, attribute; 206, 207; 206, 215; 207, call; 207, 208; 207, 213; 208, attribute; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:os; 211, identifier:path; 212, identifier:basename; 213, argument_list; 213, 214; 214, identifier:script_name; 215, identifier:upper; 216, argument_list; 217, identifier:user_config_file_path; 218, if_statement; 218, 219; 218, 227; 219, call; 219, 220; 219, 225; 220, attribute; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:os; 223, identifier:path; 224, identifier:exists; 225, argument_list; 225, 226; 226, identifier:user_config_file_path; 227, block; 227, 228; 227, 235; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:user_dict; 231, call; 231, 232; 231, 233; 232, identifier:load_yaml; 233, argument_list; 233, 234; 234, identifier:user_config_file_path; 235, if_statement; 235, 236; 235, 237; 236, identifier:user_dict; 237, block; 237, 238; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 241; 240, identifier:default_dict; 241, call; 241, 242; 241, 243; 242, identifier:merge; 243, argument_list; 243, 244; 243, 245; 244, identifier:user_dict; 245, identifier:default_dict; 246, return_statement; 246, 247; 247, identifier:default_dict | def yaml_file_to_dict(script_name, path=None):
"""Read yaml file and return the dict.
It assumes the module file exists with the defaults.
If the CONDOOR_{SCRIPT_NAME} env is set then the user file from the env is loaded and merged with the default
There can be user file located in ~/.condoor directory with the {script_name}.yaml filename. If exists
it is merget with default config.
"""
def load_yaml(file_path):
"""Load YAML file from full file path and return dict."""
with open(file_path, 'r') as yamlfile:
try:
dictionary = yaml.load(yamlfile)
except yaml.YAMLError:
return {}
return dictionary
def merge(user, default):
"""Merge two dicts."""
if isinstance(user, dict) and isinstance(default, dict):
for k, v in default.iteritems():
if k not in user:
user[k] = v
else:
user[k] = merge(user[k], v)
return user
if path is None:
path = os.path.abspath('.')
config_file_path = os.path.join(path, script_name + '.yaml')
if not os.path.exists(config_file_path):
raise RuntimeError('Config file does not exist: {}'.format(config_file_path))
default_dict = load_yaml(config_file_path)
user_config_file_path = os.path.join(os.path.expanduser('~'), '.condoor', os.path.basename(script_name) + '.yaml')
user_config_file_path = os.getenv('CONDOOR_' + os.path.basename(script_name).upper(), user_config_file_path)
if os.path.exists(user_config_file_path):
user_dict = load_yaml(user_config_file_path)
if user_dict:
default_dict = merge(user_dict, default_dict)
return default_dict |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:make_hop_info_from_url; 3, parameters; 3, 4; 3, 5; 4, identifier:url; 5, default_parameter; 5, 6; 5, 7; 6, identifier:verify_reachability; 7, None; 8, block; 8, 9; 8, 11; 8, 18; 8, 34; 8, 35; 8, 51; 8, 52; 8, 74; 8, 95; 8, 104; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:parsed; 14, call; 14, 15; 14, 16; 15, identifier:urlparse; 16, argument_list; 16, 17; 17, identifier:url; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:username; 21, conditional_expression:if; 21, 22; 21, 23; 21, 28; 22, None; 23, comparison_operator:is; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:parsed; 26, identifier:username; 27, None; 28, call; 28, 29; 28, 30; 29, identifier:unquote; 30, argument_list; 30, 31; 31, attribute; 31, 32; 31, 33; 32, identifier:parsed; 33, identifier:username; 34, comment; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:password; 38, conditional_expression:if; 38, 39; 38, 40; 38, 45; 39, None; 40, comparison_operator:is; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:parsed; 43, identifier:password; 44, None; 45, call; 45, 46; 45, 47; 46, identifier:unquote; 47, argument_list; 47, 48; 48, attribute; 48, 49; 48, 50; 49, identifier:parsed; 50, identifier:password; 51, comment; 52, try_statement; 52, 53; 52, 67; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:enable_password; 57, subscript; 57, 58; 57, 66; 58, subscript; 58, 59; 58, 65; 59, call; 59, 60; 59, 61; 60, identifier:parse_qs; 61, argument_list; 61, 62; 62, attribute; 62, 63; 62, 64; 63, identifier:parsed; 64, identifier:query; 65, string:"enable_password"; 66, integer:0; 67, except_clause; 67, 68; 67, 69; 68, identifier:KeyError; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:enable_password; 73, None; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:hop_info; 77, call; 77, 78; 77, 79; 78, identifier:HopInfo; 79, argument_list; 79, 80; 79, 83; 79, 86; 79, 87; 79, 88; 79, 91; 79, 92; 80, attribute; 80, 81; 80, 82; 81, identifier:parsed; 82, identifier:scheme; 83, attribute; 83, 84; 83, 85; 84, identifier:parsed; 85, identifier:hostname; 86, identifier:username; 87, identifier:password; 88, attribute; 88, 89; 88, 90; 89, identifier:parsed; 90, identifier:port; 91, identifier:enable_password; 92, keyword_argument; 92, 93; 92, 94; 93, identifier:verify_reachability; 94, identifier:verify_reachability; 95, if_statement; 95, 96; 95, 101; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:hop_info; 99, identifier:is_valid; 100, argument_list; 101, block; 101, 102; 102, return_statement; 102, 103; 103, identifier:hop_info; 104, raise_statement; 104, 105; 105, identifier:InvalidHopInfoError | def make_hop_info_from_url(url, verify_reachability=None):
"""Build HopInfo object from url.
It allows only telnet and ssh as a valid protocols.
Args:
url (str): The url string describing the node. i.e.
telnet://[email protected]. The protocol, username and address
portion of url is mandatory. Port and password is optional.
If port is missing the standard protocol -> port mapping is done.
The password is optional i.e. for TS access directly to console
ports.
The path part is treated as additional password required for some
systems, i.e. enable password for IOS devices.:
telnet://<username>:<password>@<host>:<port>/<enable_password>
<enable_password> is optional
verify_reachability: This is optional callable returning boolean
if node is reachable. It can be used to verify reachability
of the node before making a connection. It can speedup the
connection process when node not reachable especially with
telnet having long timeout.
Returns:
HopInfo object or None if url is invalid or protocol not supported
"""
parsed = urlparse(url)
username = None if parsed.username is None else unquote(parsed.username) # It's None if not exists
password = None if parsed.password is None else unquote(parsed.password) # It's None if not exists
try:
enable_password = parse_qs(parsed.query)["enable_password"][0]
except KeyError:
enable_password = None
hop_info = HopInfo(
parsed.scheme,
parsed.hostname,
username,
password,
parsed.port,
enable_password,
verify_reachability=verify_reachability
)
if hop_info.is_valid():
return hop_info
raise InvalidHopInfoError |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:sendmail; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 11; 4, identifier:self; 5, identifier:sender; 6, identifier:recipients; 7, identifier:message; 8, default_parameter; 8, 9; 8, 10; 9, identifier:mail_options; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:rcpt_options; 13, None; 14, block; 14, 15; 14, 17; 14, 18; 14, 30; 14, 31; 14, 40; 14, 49; 14, 50; 14, 57; 14, 84; 14, 93; 14, 97; 14, 125; 14, 142; 14, 150; 14, 151; 15, expression_statement; 15, 16; 16, comment; 17, comment; 18, if_statement; 18, 19; 18, 24; 19, call; 19, 20; 19, 21; 20, identifier:isinstance; 21, argument_list; 21, 22; 21, 23; 22, identifier:recipients; 23, identifier:str; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:recipients; 28, list:[recipients]; 28, 29; 29, identifier:recipients; 30, comment; 31, if_statement; 31, 32; 31, 35; 32, comparison_operator:is; 32, 33; 32, 34; 33, identifier:mail_options; 34, None; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:mail_options; 39, list:[]; 40, if_statement; 40, 41; 40, 44; 41, comparison_operator:is; 41, 42; 41, 43; 42, identifier:rcpt_options; 43, None; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:rcpt_options; 48, list:[]; 49, comment; 50, expression_statement; 50, 51; 51, await; 51, 52; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:self; 55, identifier:ehlo_or_helo_if_needed; 56, argument_list; 57, if_statement; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:supports_esmtp; 61, block; 61, 62; 62, if_statement; 62, 63; 62, 68; 63, comparison_operator:in; 63, 64; 63, 65; 64, string:"size"; 65, attribute; 65, 66; 65, 67; 66, identifier:self; 67, identifier:esmtp_extensions; 68, block; 68, 69; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:mail_options; 73, identifier:append; 74, argument_list; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, string:"size={}"; 78, identifier:format; 79, argument_list; 79, 80; 80, call; 80, 81; 80, 82; 81, identifier:len; 82, argument_list; 82, 83; 83, identifier:message; 84, expression_statement; 84, 85; 85, await; 85, 86; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:mail; 90, argument_list; 90, 91; 90, 92; 91, identifier:sender; 92, identifier:mail_options; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:errors; 96, list:[]; 97, for_statement; 97, 98; 97, 99; 97, 100; 98, identifier:recipient; 99, identifier:recipients; 100, block; 100, 101; 101, try_statement; 101, 102; 101, 112; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, await; 104, 105; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:self; 108, identifier:rcpt; 109, argument_list; 109, 110; 109, 111; 110, identifier:recipient; 111, identifier:rcpt_options; 112, except_clause; 112, 113; 112, 117; 113, as_pattern; 113, 114; 113, 115; 114, identifier:SMTPCommandFailedError; 115, as_pattern_target; 115, 116; 116, identifier:e; 117, block; 117, 118; 118, expression_statement; 118, 119; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:errors; 122, identifier:append; 123, argument_list; 123, 124; 124, identifier:e; 125, if_statement; 125, 126; 125, 135; 125, 136; 126, comparison_operator:==; 126, 127; 126, 131; 127, call; 127, 128; 127, 129; 128, identifier:len; 129, argument_list; 129, 130; 130, identifier:recipients; 131, call; 131, 132; 131, 133; 132, identifier:len; 133, argument_list; 133, 134; 134, identifier:errors; 135, comment; 136, block; 136, 137; 137, raise_statement; 137, 138; 138, call; 138, 139; 138, 140; 139, identifier:SMTPNoRecipientError; 140, argument_list; 140, 141; 141, identifier:errors; 142, expression_statement; 142, 143; 143, await; 143, 144; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:self; 147, identifier:data; 148, argument_list; 148, 149; 149, identifier:message; 150, comment; 151, return_statement; 151, 152; 152, identifier:errors | async def sendmail(
self, sender, recipients, message, mail_options=None, rcpt_options=None
):
"""
Performs an entire e-mail transaction.
Example:
>>> try:
>>> with SMTP() as client:
>>> try:
>>> r = client.sendmail(sender, recipients, message)
>>> except SMTPException:
>>> print("Error while sending message.")
>>> else:
>>> print("Result: {}.".format(r))
>>> except ConnectionError as e:
>>> print(e)
Result: {}.
Args:
sender (str): E-mail address of the sender.
recipients (list of str or str): E-mail(s) address(es) of the
recipient(s).
message (str or bytes): Message body.
mail_options (list of str): ESMTP options (such as *8BITMIME*) to
send along the *MAIL* command.
rcpt_options (list of str): ESMTP options (such as *DSN*) to
send along all the *RCPT* commands.
Raises:
ConnectionResetError: If the connection with the server is
unexpectedely lost.
SMTPCommandFailedError: If the server refuses our EHLO/HELO
greeting.
SMTPCommandFailedError: If the server refuses our MAIL command.
SMTPCommandFailedError: If the server refuses our DATA command.
SMTPNoRecipientError: If the server refuses all given
recipients.
Returns:
dict: A dict containing an entry for each recipient that was
refused. Each entry is associated with a (code, message)
2-tuple containing the error code and message, as returned by
the server.
When everythign runs smoothly, the returning dict is empty.
.. note:: The connection remains open after. It's your responsibility
to close it. A good practice is to use the asynchronous context
manager instead. See :meth:`SMTP.__aenter__` for further details.
"""
# Make sure `recipients` is a list:
if isinstance(recipients, str):
recipients = [recipients]
# Set some defaults values:
if mail_options is None:
mail_options = []
if rcpt_options is None:
rcpt_options = []
# EHLO or HELO is required:
await self.ehlo_or_helo_if_needed()
if self.supports_esmtp:
if "size" in self.esmtp_extensions:
mail_options.append("size={}".format(len(message)))
await self.mail(sender, mail_options)
errors = []
for recipient in recipients:
try:
await self.rcpt(recipient, rcpt_options)
except SMTPCommandFailedError as e:
errors.append(e)
if len(recipients) == len(errors):
# The server refused all our recipients:
raise SMTPNoRecipientError(errors)
await self.data(message)
# If we got here then somebody got our mail:
return errors |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:run_fsm; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 4, identifier:self; 5, identifier:name; 6, identifier:command; 7, identifier:events; 8, identifier:transitions; 9, identifier:timeout; 10, default_parameter; 10, 11; 10, 12; 11, identifier:max_transitions; 12, integer:20; 13, block; 13, 14; 13, 16; 14, expression_statement; 14, 15; 15, comment; 16, return_statement; 16, 17; 17, call; 17, 18; 17, 25; 18, attribute; 18, 19; 18, 24; 19, attribute; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:_chain; 23, identifier:target_device; 24, identifier:run_fsm; 25, argument_list; 25, 26; 25, 27; 25, 28; 25, 29; 25, 30; 25, 31; 26, identifier:name; 27, identifier:command; 28, identifier:events; 29, identifier:transitions; 30, identifier:timeout; 31, identifier:max_transitions | def run_fsm(self, name, command, events, transitions, timeout, max_transitions=20):
"""Instantiate and run the Finite State Machine for the current device connection.
Here is the example of usage::
test_dir = "rw_test"
dir = "disk0:" + test_dir
REMOVE_DIR = re.compile(re.escape("Remove directory filename [{}]?".format(test_dir)))
DELETE_CONFIRM = re.compile(re.escape("Delete {}/{}[confirm]".format(filesystem, test_dir)))
REMOVE_ERROR = re.compile(re.escape("%Error Removing dir {} (Directory doesnot exist)".format(test_dir)))
command = "rmdir {}".format(dir)
events = [device.prompt, REMOVE_DIR, DELETE_CONFIRM, REMOVE_ERROR, pexpect.TIMEOUT]
transitions = [
(REMOVE_DIR, [0], 1, send_newline, 5),
(DELETE_CONFIRM, [1], 2, send_newline, 5),
# if dir does not exist initially it's ok
(REMOVE_ERROR, [0], 2, None, 0),
(device.prompt, [2], -1, None, 0),
(pexpect.TIMEOUT, [0, 1, 2], -1, error, 0)
]
if not conn.run_fsm("DELETE_DIR", command, events, transitions, timeout=5):
return False
This FSM tries to remove directory from disk0:
Args:
name (str): Name of the state machine used for logging purposes. Can't be *None*
command (str): The command sent to the device before FSM starts
events (list): List of expected strings or pexpect.TIMEOUT exception expected from the device.
transitions (list): List of tuples in defining the state machine transitions.
timeout (int): Default timeout between states in seconds.
max_transitions (int): Default maximum number of transitions allowed for FSM.
The transition tuple format is as follows::
(event, [list_of_states], next_state, action, timeout)
Where:
- **event** (str): string from the `events` list which is expected to be received from device.
- **list_of_states** (list): List of FSM states that triggers the action in case of event occurrence.
- **next_state** (int): Next state for FSM transition.
- **action** (func): function to be executed if the current FSM state belongs to `list_of_states`
and the `event` occurred. The action can be also *None* then FSM transits to the next state
without any action. Action can be also the exception, which is raised and FSM stops.
The example action::
def send_newline(ctx):
ctx.ctrl.sendline()
return True
def error(ctx):
ctx.message = "Filesystem error"
return False
def readonly(ctx):
ctx.message = "Filesystem is readonly"
return False
The ctx object description refer to :class:`condoor.fsm.FSM`.
If the action returns True then the FSM continues processing. If the action returns False then FSM stops
and the error message passed back to the ctx object is posted to the log.
The FSM state is the integer number. The FSM starts with initial ``state=0`` and finishes if the ``next_state``
is set to -1.
If action returns False then FSM returns False. FSM returns True if reaches the -1 state.
"""
return self._chain.target_device.run_fsm(name, command, events, transitions, timeout, max_transitions) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:_get_view_details; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:urlpatterns; 6, default_parameter; 6, 7; 6, 8; 7, identifier:parent; 8, string:''; 9, block; 9, 10; 9, 12; 10, expression_statement; 10, 11; 11, comment; 12, for_statement; 12, 13; 12, 14; 12, 15; 13, identifier:pattern; 14, identifier:urlpatterns; 15, block; 15, 16; 16, if_statement; 16, 17; 16, 24; 16, 225; 17, call; 17, 18; 17, 19; 18, identifier:isinstance; 19, argument_list; 19, 20; 19, 21; 20, identifier:pattern; 21, tuple; 21, 22; 21, 23; 22, identifier:URLPattern; 23, identifier:RegexURLPattern; 24, block; 24, 25; 25, try_statement; 25, 26; 25, 221; 26, block; 26, 27; 26, 34; 26, 42; 26, 46; 26, 50; 26, 54; 26, 82; 26, 83; 26, 94; 26, 113; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:d; 30, call; 30, 31; 30, 32; 31, identifier:describe_pattern; 32, argument_list; 32, 33; 33, identifier:pattern; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:docstr; 37, attribute; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:pattern; 40, identifier:callback; 41, identifier:__doc__; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:method; 45, None; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:expected_json_response; 49, string:''; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:expected_url; 53, string:''; 54, if_statement; 54, 55; 54, 56; 54, 57; 55, identifier:docstr; 56, comment; 57, block; 57, 58; 57, 73; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:u; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:re; 64, identifier:findall; 65, argument_list; 65, 66; 65, 67; 65, 68; 66, string:r'URL: (.*)'; 67, identifier:docstr; 68, keyword_argument; 68, 69; 68, 70; 69, identifier:flags; 70, attribute; 70, 71; 70, 72; 71, identifier:re; 72, identifier:DOTALL; 73, if_statement; 73, 74; 73, 75; 74, identifier:u; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:expected_url; 79, subscript; 79, 80; 79, 81; 80, identifier:u; 81, integer:0; 82, comment; 83, if_statement; 83, 84; 83, 92; 84, comparison_operator:not; 84, 85; 84, 86; 85, string:'view_class'; 86, call; 86, 87; 86, 88; 87, identifier:dir; 88, argument_list; 88, 89; 89, attribute; 89, 90; 89, 91; 90, identifier:pattern; 91, identifier:callback; 92, block; 92, 93; 93, continue_statement; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:possible_methods; 97, list_comprehension; 97, 98; 97, 99; 97, 109; 98, identifier:m; 99, for_in_clause; 99, 100; 99, 101; 100, identifier:m; 101, call; 101, 102; 101, 103; 102, identifier:dir; 103, argument_list; 103, 104; 104, attribute; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:pattern; 107, identifier:callback; 108, identifier:view_class; 109, if_clause; 109, 110; 110, comparison_operator:in; 110, 111; 110, 112; 111, identifier:m; 112, identifier:METHODS; 113, for_statement; 113, 114; 113, 115; 113, 116; 114, identifier:method; 115, identifier:possible_methods; 116, block; 116, 117; 116, 131; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:view_method_docstr; 120, attribute; 120, 121; 120, 130; 121, call; 121, 122; 121, 123; 122, identifier:getattr; 123, argument_list; 123, 124; 123, 129; 124, attribute; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:pattern; 127, identifier:callback; 128, identifier:view_class; 129, identifier:method; 130, identifier:__doc__; 131, if_statement; 131, 132; 131, 133; 131, 134; 132, identifier:view_method_docstr; 133, comment; 134, block; 134, 135; 134, 159; 134, 160; 134, 175; 135, if_statement; 135, 136; 135, 142; 136, comparison_operator:in; 136, 137; 136, 138; 137, identifier:method; 138, list:['put', 'patch', 'delete']; 138, 139; 138, 140; 138, 141; 139, string:'put'; 140, string:'patch'; 141, string:'delete'; 142, block; 142, 143; 142, 158; 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:url_details; 149, identifier:append; 150, argument_list; 150, 151; 151, dictionary; 151, 152; 151, 155; 152, pair; 152, 153; 152, 154; 153, string:'url'; 154, identifier:expected_url; 155, pair; 155, 156; 155, 157; 156, string:'method'; 157, identifier:method; 158, continue_statement; 159, comment; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:j; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:re; 166, identifier:findall; 167, argument_list; 167, 168; 167, 169; 167, 170; 168, string:r'```(.*)```'; 169, identifier:view_method_docstr; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:flags; 172, attribute; 172, 173; 172, 174; 173, identifier:re; 174, identifier:DOTALL; 175, if_statement; 175, 176; 175, 179; 176, comparison_operator:is; 176, 177; 176, 178; 177, identifier:j; 178, None; 179, block; 179, 180; 180, for_statement; 180, 181; 180, 182; 180, 183; 181, identifier:match; 182, identifier:j; 183, block; 183, 184; 183, 192; 183, 193; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:expected_json_response; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:match; 190, identifier:strip; 191, argument_list; 192, comment; 193, if_statement; 193, 194; 193, 201; 194, boolean_operator:and; 194, 195; 194, 200; 195, boolean_operator:and; 195, 196; 195, 199; 196, comparison_operator:is; 196, 197; 196, 198; 197, identifier:method; 198, None; 199, identifier:expected_json_response; 200, identifier:expected_url; 201, block; 201, 202; 201, 220; 202, expression_statement; 202, 203; 203, call; 203, 204; 203, 209; 204, attribute; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:self; 207, identifier:url_details; 208, identifier:append; 209, argument_list; 209, 210; 210, dictionary; 210, 211; 210, 214; 210, 217; 211, pair; 211, 212; 211, 213; 212, string:'url'; 213, identifier:expected_url; 214, pair; 214, 215; 214, 216; 215, string:'method'; 216, identifier:method; 217, pair; 217, 218; 217, 219; 218, string:'response'; 219, identifier:expected_json_response; 220, continue_statement; 221, except_clause; 221, 222; 221, 223; 222, identifier:ViewDoesNotExist; 223, block; 223, 224; 224, pass_statement; 225, elif_clause; 225, 226; 225, 233; 226, call; 226, 227; 226, 228; 227, identifier:isinstance; 228, argument_list; 228, 229; 228, 230; 229, identifier:pattern; 230, tuple; 230, 231; 230, 232; 231, identifier:URLResolver; 232, identifier:RegexURLResolver; 233, block; 233, 234; 233, 246; 233, 253; 233, 259; 234, try_statement; 234, 235; 234, 242; 235, block; 235, 236; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 239; 238, identifier:patterns; 239, attribute; 239, 240; 239, 241; 240, identifier:pattern; 241, identifier:url_patterns; 242, except_clause; 242, 243; 242, 244; 243, identifier:ImportError; 244, block; 244, 245; 245, continue_statement; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:d; 249, call; 249, 250; 249, 251; 250, identifier:describe_pattern; 251, argument_list; 251, 252; 252, identifier:pattern; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 256; 255, identifier:current_full_url; 256, binary_operator:+; 256, 257; 256, 258; 257, identifier:parent; 258, identifier:d; 259, expression_statement; 259, 260; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:self; 263, identifier:_get_view_details; 264, argument_list; 264, 265; 264, 266; 265, identifier:patterns; 266, identifier:current_full_url | def _get_view_details(self, urlpatterns, parent=''):
"""Recursive function to extract all url details"""
for pattern in urlpatterns:
if isinstance(pattern, (URLPattern, RegexURLPattern)):
try:
d = describe_pattern(pattern)
docstr = pattern.callback.__doc__
method = None
expected_json_response = ''
expected_url = ''
if docstr:
# Get expected URL
u = re.findall(r'URL: (.*)', docstr, flags=re.DOTALL)
if u:
expected_url = u[0]
# Get all possible methods
if 'view_class' not in dir(pattern.callback):
continue
possible_methods = [m for m in dir(pattern.callback.view_class) if m in METHODS]
for method in possible_methods:
view_method_docstr = getattr(pattern.callback.view_class, method).__doc__
if view_method_docstr:
# Check if method is modifier
if method in ['put', 'patch', 'delete']:
self.url_details.append({
'url': expected_url,
'method': method
})
continue
# Extract request method and JSON response from docstring of request method
j = re.findall(r'```(.*)```', view_method_docstr, flags=re.DOTALL)
if j is not None:
for match in j:
expected_json_response = match.strip()
# Only add the details if all 3 values are filled
if method is not None and expected_json_response and expected_url:
self.url_details.append({
'url': expected_url,
'method': method,
'response': expected_json_response
})
continue
except ViewDoesNotExist:
pass
elif isinstance(pattern, (URLResolver, RegexURLResolver)):
try:
patterns = pattern.url_patterns
except ImportError:
continue
d = describe_pattern(pattern)
current_full_url = parent + d
self._get_view_details(patterns, current_full_url) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:scan_module; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:modpath; 6, identifier:node; 7, block; 7, 8; 7, 10; 7, 24; 7, 93; 7, 190; 7, 238; 7, 269; 7, 275; 7, 281; 7, 321; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:used_origins; 13, call; 13, 14; 13, 19; 14, attribute; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:map; 18, identifier:setdefault; 19, argument_list; 19, 20; 19, 21; 20, identifier:modpath; 21, call; 21, 22; 21, 23; 22, identifier:set; 23, argument_list; 24, function_definition; 24, 25; 24, 26; 24, 29; 25, function_name:get_origins; 26, parameters; 26, 27; 26, 28; 27, identifier:modpath; 28, identifier:name; 29, block; 29, 30; 29, 32; 29, 38; 29, 85; 29, 91; 30, expression_statement; 30, 31; 31, comment; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:origins; 35, call; 35, 36; 35, 37; 36, identifier:set; 37, argument_list; 38, function_definition; 38, 39; 38, 40; 38, 43; 39, function_name:walk_origins; 40, parameters; 40, 41; 40, 42; 41, identifier:modpath; 42, identifier:name; 43, block; 43, 44; 44, for_statement; 44, 45; 44, 46; 44, 55; 45, identifier:origin; 46, call; 46, 47; 46, 52; 47, attribute; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:import_map; 51, identifier:get_origins; 52, argument_list; 52, 53; 52, 54; 53, identifier:modpath; 54, identifier:name; 55, block; 55, 56; 56, if_statement; 56, 57; 56, 60; 57, comparison_operator:not; 57, 58; 57, 59; 58, identifier:origin; 59, identifier:origins; 60, block; 60, 61; 60, 68; 61, expression_statement; 61, 62; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:origins; 65, identifier:add; 66, argument_list; 66, 67; 67, identifier:origin; 68, if_statement; 68, 69; 68, 72; 69, comparison_operator:in; 69, 70; 69, 71; 70, string:'.'; 71, identifier:origin; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 76; 75, identifier:walk_origins; 76, argument_list; 76, 77; 77, list_splat; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:origin; 81, identifier:rsplit; 82, argument_list; 82, 83; 82, 84; 83, string:'.'; 84, integer:1; 85, expression_statement; 85, 86; 86, call; 86, 87; 86, 88; 87, identifier:walk_origins; 88, argument_list; 88, 89; 88, 90; 89, identifier:modpath; 90, identifier:name; 91, return_statement; 91, 92; 92, identifier:origins; 93, function_definition; 93, 94; 93, 95; 93, 97; 94, function_name:get_origins_for_node; 95, parameters; 95, 96; 96, identifier:node; 97, block; 97, 98; 97, 100; 97, 134; 97, 186; 98, expression_statement; 98, 99; 99, comment; 100, if_statement; 100, 101; 100, 116; 101, boolean_operator:and; 101, 102; 101, 108; 102, comparison_operator:==; 102, 103; 102, 107; 103, call; 103, 104; 103, 105; 104, identifier:node_type; 105, argument_list; 105, 106; 106, identifier:node; 107, string:'Name'; 108, comparison_operator:==; 108, 109; 108, 115; 109, call; 109, 110; 109, 111; 110, identifier:node_type; 111, argument_list; 111, 112; 112, attribute; 112, 113; 112, 114; 113, identifier:node; 114, identifier:ctx; 115, string:'Load'; 116, block; 116, 117; 117, return_statement; 117, 118; 118, binary_operator:|; 118, 119; 118, 127; 119, set; 119, 120; 120, binary_operator:+; 120, 121; 120, 124; 121, binary_operator:+; 121, 122; 121, 123; 122, identifier:modpath; 123, string:'.'; 124, attribute; 124, 125; 124, 126; 125, identifier:node; 126, identifier:id; 127, call; 127, 128; 127, 129; 128, identifier:get_origins; 129, argument_list; 129, 130; 129, 131; 130, identifier:modpath; 131, attribute; 131, 132; 131, 133; 132, identifier:node; 133, identifier:id; 134, if_statement; 134, 135; 134, 150; 135, boolean_operator:and; 135, 136; 135, 142; 136, comparison_operator:==; 136, 137; 136, 141; 137, call; 137, 138; 137, 139; 138, identifier:node_type; 139, argument_list; 139, 140; 140, identifier:node; 141, string:'Attribute'; 142, comparison_operator:==; 142, 143; 142, 149; 143, call; 143, 144; 143, 145; 144, identifier:node_type; 145, argument_list; 145, 146; 146, attribute; 146, 147; 146, 148; 147, identifier:node; 148, identifier:ctx; 149, string:'Load'; 150, block; 150, 151; 151, return_statement; 151, 152; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:set; 155, identifier:union; 156, argument_list; 156, 157; 156, 160; 157, call; 157, 158; 157, 159; 158, identifier:set; 159, argument_list; 160, list_splat; 160, 161; 161, list_comprehension; 161, 162; 161, 178; 162, binary_operator:|; 162, 163; 162, 171; 163, set; 163, 164; 164, binary_operator:+; 164, 165; 164, 168; 165, binary_operator:+; 165, 166; 165, 167; 166, identifier:parent; 167, string:'.'; 168, attribute; 168, 169; 168, 170; 169, identifier:node; 170, identifier:attr; 171, call; 171, 172; 171, 173; 172, identifier:get_origins; 173, argument_list; 173, 174; 173, 175; 174, identifier:parent; 175, attribute; 175, 176; 175, 177; 176, identifier:node; 177, identifier:attr; 178, for_in_clause; 178, 179; 178, 180; 179, identifier:parent; 180, call; 180, 181; 180, 182; 181, identifier:get_origins_for_node; 182, argument_list; 182, 183; 183, attribute; 183, 184; 183, 185; 184, identifier:node; 185, identifier:value; 186, return_statement; 186, 187; 187, call; 187, 188; 187, 189; 188, identifier:set; 189, argument_list; 190, function_definition; 190, 191; 190, 192; 190, 194; 191, function_name:get_origins_used_by_node; 192, parameters; 192, 193; 193, identifier:node; 194, block; 194, 195; 194, 197; 194, 210; 194, 234; 195, expression_statement; 195, 196; 196, comment; 197, if_statement; 197, 198; 197, 204; 198, comparison_operator:==; 198, 199; 198, 203; 199, call; 199, 200; 199, 201; 200, identifier:node_type; 201, argument_list; 201, 202; 202, identifier:node; 203, string:'Name'; 204, block; 204, 205; 205, return_statement; 205, 206; 206, call; 206, 207; 206, 208; 207, identifier:get_origins_for_node; 208, argument_list; 208, 209; 209, identifier:node; 210, if_statement; 210, 211; 210, 217; 211, comparison_operator:==; 211, 212; 211, 216; 212, call; 212, 213; 212, 214; 213, identifier:node_type; 214, argument_list; 214, 215; 215, identifier:node; 216, string:'Attribute'; 217, block; 217, 218; 218, return_statement; 218, 219; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:set; 222, identifier:union; 223, argument_list; 223, 224; 223, 230; 224, call; 224, 225; 224, 226; 225, identifier:get_origins_used_by_node; 226, argument_list; 226, 227; 227, attribute; 227, 228; 227, 229; 228, identifier:node; 229, identifier:value; 230, call; 230, 231; 230, 232; 231, identifier:get_origins_for_node; 232, argument_list; 232, 233; 233, identifier:node; 234, return_statement; 234, 235; 235, call; 235, 236; 235, 237; 236, identifier:set; 237, argument_list; 238, function_definition; 238, 239; 238, 240; 238, 242; 239, function_name:scan_loads; 240, parameters; 240, 241; 241, identifier:node; 242, block; 242, 243; 242, 263; 243, if_statement; 243, 244; 243, 252; 244, comparison_operator:in; 244, 245; 244, 249; 245, call; 245, 246; 245, 247; 246, identifier:node_type; 247, argument_list; 247, 248; 248, identifier:node; 249, list:['Name', 'Attribute']; 249, 250; 249, 251; 250, string:'Name'; 251, string:'Attribute'; 252, block; 252, 253; 253, expression_statement; 253, 254; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:used_origins; 257, identifier:update; 258, argument_list; 258, 259; 259, call; 259, 260; 259, 261; 260, identifier:get_origins_used_by_node; 261, argument_list; 261, 262; 262, identifier:node; 263, expression_statement; 263, 264; 264, call; 264, 265; 264, 266; 265, identifier:for_each_child; 266, argument_list; 266, 267; 266, 268; 267, identifier:node; 268, identifier:scan_loads; 269, expression_statement; 269, 270; 270, call; 270, 271; 270, 272; 271, identifier:for_each_child; 272, argument_list; 272, 273; 272, 274; 273, identifier:node; 274, identifier:scan_loads; 275, expression_statement; 275, 276; 276, assignment; 276, 277; 276, 278; 277, identifier:intermediate_origins; 278, call; 278, 279; 278, 280; 279, identifier:set; 280, argument_list; 281, for_statement; 281, 282; 281, 283; 281, 284; 282, identifier:origin; 283, identifier:used_origins; 284, block; 284, 285; 284, 294; 285, expression_statement; 285, 286; 286, assignment; 286, 287; 286, 288; 287, identifier:parts; 288, call; 288, 289; 288, 292; 289, attribute; 289, 290; 289, 291; 290, identifier:origin; 291, identifier:split; 292, argument_list; 292, 293; 293, string:'.'; 294, for_statement; 294, 295; 294, 296; 294, 304; 295, identifier:i; 296, call; 296, 297; 296, 298; 297, identifier:range; 298, argument_list; 298, 299; 298, 300; 299, integer:1; 300, call; 300, 301; 300, 302; 301, identifier:len; 302, argument_list; 302, 303; 303, identifier:parts; 304, block; 304, 305; 305, expression_statement; 305, 306; 306, call; 306, 307; 306, 310; 307, attribute; 307, 308; 307, 309; 308, identifier:intermediate_origins; 309, identifier:add; 310, argument_list; 310, 311; 311, call; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, string:'.'; 314, identifier:join; 315, argument_list; 315, 316; 316, subscript; 316, 317; 316, 318; 317, identifier:parts; 318, slice; 318, 319; 318, 320; 319, colon; 320, identifier:i; 321, expression_statement; 321, 322; 322, call; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:used_origins; 325, identifier:update; 326, argument_list; 326, 327; 327, identifier:intermediate_origins | def scan_module(self, modpath, node):
"""Scans a module, collecting all used origins, assuming that modules
are obtained only by dotted paths and no other kinds of expressions."""
used_origins = self.map.setdefault(modpath, set())
def get_origins(modpath, name):
"""Returns the chain of all origins for a given name in a module."""
origins = set()
def walk_origins(modpath, name):
for origin in self.import_map.get_origins(modpath, name):
if origin not in origins:
origins.add(origin)
if '.' in origin:
walk_origins(*origin.rsplit('.', 1))
walk_origins(modpath, name)
return origins
def get_origins_for_node(node):
"""Returns the set of all possible origins to which the given
dotted-path expression might dereference."""
if node_type(node) == 'Name' and node_type(node.ctx) == 'Load':
return {modpath + '.' + node.id} | get_origins(modpath, node.id)
if node_type(node) == 'Attribute' and node_type(node.ctx) == 'Load':
return set.union(set(), *[
{parent + '.' + node.attr} | get_origins(parent, node.attr)
for parent in get_origins_for_node(node.value)])
return set()
def get_origins_used_by_node(node):
"""Returns the set of all possible origins that could be used
during dereferencing of the given dotted-path expression."""
if node_type(node) == 'Name':
return get_origins_for_node(node)
if node_type(node) == 'Attribute':
return set.union(get_origins_used_by_node(node.value),
get_origins_for_node(node))
return set()
def scan_loads(node):
if node_type(node) in ['Name', 'Attribute']:
used_origins.update(get_origins_used_by_node(node))
for_each_child(node, scan_loads)
for_each_child(node, scan_loads)
intermediate_origins = set()
for origin in used_origins:
parts = origin.split('.')
for i in range(1, len(parts)):
intermediate_origins.add('.'.join(parts[:i]))
used_origins.update(intermediate_origins) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:operations; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:op_types; 7, None; 8, block; 8, 9; 8, 11; 8, 24; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 14; 12, not_operator; 12, 13; 13, identifier:op_types; 14, block; 14, 15; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:op_types; 18, list:['message', 'action', 'sync', 'viewlock', 'savedchapter']; 18, 19; 18, 20; 18, 21; 18, 22; 18, 23; 19, string:'message'; 20, string:'action'; 21, string:'sync'; 22, string:'viewlock'; 23, string:'savedchapter'; 24, while_statement; 24, 25; 24, 36; 25, comparison_operator:<; 25, 26; 25, 33; 26, call; 26, 27; 26, 32; 27, attribute; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:_handle; 31, identifier:tell; 32, argument_list; 33, attribute; 33, 34; 33, 35; 34, identifier:self; 35, identifier:_eof; 36, block; 36, 37; 36, 52; 36, 79; 36, 135; 36, 159; 36, 174; 36, 195; 36, 218; 36, 239; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:current_time; 40, call; 40, 41; 40, 46; 41, attribute; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:mgz; 44, identifier:util; 45, identifier:convert_to_timestamp; 46, argument_list; 46, 47; 47, binary_operator:/; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:_time; 51, integer:1000; 52, try_statement; 52, 53; 52, 69; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:operation; 57, call; 57, 58; 57, 65; 58, attribute; 58, 59; 58, 64; 59, attribute; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:mgz; 62, identifier:body; 63, identifier:operation; 64, identifier:parse_stream; 65, argument_list; 65, 66; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:_handle; 69, except_clause; 69, 70; 69, 73; 70, tuple; 70, 71; 70, 72; 71, identifier:ConstructError; 72, identifier:ValueError; 73, block; 73, 74; 74, raise_statement; 74, 75; 75, call; 75, 76; 75, 77; 76, identifier:MgzError; 77, argument_list; 77, 78; 78, string:'failed to parse body operation'; 79, if_statement; 79, 80; 79, 85; 80, comparison_operator:==; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:operation; 83, identifier:type; 84, string:'action'; 85, block; 85, 86; 86, if_statement; 86, 87; 86, 94; 86, 119; 87, comparison_operator:in; 87, 88; 87, 93; 88, attribute; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:operation; 91, identifier:action; 92, identifier:type; 93, identifier:ACTIONS_WITH_PLAYER_ID; 94, block; 94, 95; 94, 107; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:counter; 98, subscript; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:_actions_by_player; 102, attribute; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:operation; 105, identifier:action; 106, identifier:player_id; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:counter; 111, identifier:update; 112, argument_list; 112, 113; 113, list:[operation.action.type]; 113, 114; 114, attribute; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:operation; 117, identifier:action; 118, identifier:type; 119, else_clause; 119, 120; 120, block; 120, 121; 121, expression_statement; 121, 122; 122, call; 122, 123; 122, 128; 123, attribute; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:self; 126, identifier:_actions_without_player; 127, identifier:update; 128, argument_list; 128, 129; 129, list:[operation.action.type]; 129, 130; 130, attribute; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:operation; 133, identifier:action; 134, identifier:type; 135, if_statement; 135, 136; 135, 151; 136, boolean_operator:and; 136, 137; 136, 142; 137, comparison_operator:==; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:operation; 140, identifier:type; 141, string:'action'; 142, call; 142, 143; 142, 144; 143, identifier:isinstance; 144, argument_list; 144, 145; 144, 150; 145, attribute; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:operation; 148, identifier:action; 149, identifier:type; 150, identifier:int; 151, block; 151, 152; 152, expression_statement; 152, 153; 153, call; 153, 154; 153, 155; 154, identifier:print; 155, argument_list; 155, 156; 156, attribute; 156, 157; 156, 158; 157, identifier:operation; 158, identifier:action; 159, if_statement; 159, 160; 159, 165; 160, comparison_operator:==; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:operation; 163, identifier:type; 164, string:'sync'; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, augmented_assignment:+=; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:self; 170, identifier:_time; 171, attribute; 171, 172; 171, 173; 172, identifier:operation; 173, identifier:time_increment; 174, if_statement; 174, 175; 174, 188; 175, boolean_operator:and; 175, 176; 175, 181; 176, comparison_operator:==; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:operation; 179, identifier:type; 180, string:'action'; 181, comparison_operator:==; 181, 182; 181, 187; 182, attribute; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:operation; 185, identifier:action; 186, identifier:type; 187, string:'postgame'; 188, block; 188, 189; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:self; 193, identifier:_postgame; 194, identifier:operation; 195, if_statement; 195, 196; 195, 201; 196, comparison_operator:==; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:operation; 199, identifier:type; 200, string:'action'; 201, block; 201, 202; 201, 210; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 205; 204, identifier:action; 205, call; 205, 206; 205, 207; 206, identifier:Action; 207, argument_list; 207, 208; 207, 209; 208, identifier:operation; 209, identifier:current_time; 210, expression_statement; 210, 211; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:self; 214, identifier:_parse_action; 215, argument_list; 215, 216; 215, 217; 216, identifier:action; 217, identifier:current_time; 218, if_statement; 218, 219; 218, 224; 218, 225; 219, comparison_operator:==; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:operation; 222, identifier:type; 223, string:'savedchapter'; 224, comment; 225, block; 225, 226; 226, expression_statement; 226, 227; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:self; 230, identifier:_parse_lobby_chat; 231, argument_list; 231, 232; 231, 237; 231, 238; 232, attribute; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:operation; 235, identifier:lobby; 236, identifier:messages; 237, string:'save'; 238, identifier:current_time; 239, if_statement; 239, 240; 239, 245; 239, 259; 239, 288; 240, comparison_operator:==; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:operation; 243, identifier:type; 244, string:'viewlock'; 245, block; 245, 246; 246, if_statement; 246, 247; 246, 252; 247, comparison_operator:in; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:operation; 250, identifier:type; 251, identifier:op_types; 252, block; 252, 253; 253, expression_statement; 253, 254; 254, yield; 254, 255; 255, call; 255, 256; 255, 257; 256, identifier:Viewlock; 257, argument_list; 257, 258; 258, identifier:operation; 259, elif_clause; 259, 260; 259, 273; 260, boolean_operator:and; 260, 261; 260, 266; 261, comparison_operator:==; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:operation; 264, identifier:type; 265, string:'action'; 266, comparison_operator:!=; 266, 267; 266, 272; 267, attribute; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:operation; 270, identifier:action; 271, identifier:type; 272, string:'postgame'; 273, block; 273, 274; 274, if_statement; 274, 275; 274, 280; 275, comparison_operator:in; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:operation; 278, identifier:type; 279, identifier:op_types; 280, block; 280, 281; 281, expression_statement; 281, 282; 282, yield; 282, 283; 283, call; 283, 284; 283, 285; 284, identifier:Action; 285, argument_list; 285, 286; 285, 287; 286, identifier:operation; 287, identifier:current_time; 288, elif_clause; 288, 289; 288, 308; 289, parenthesized_expression; 289, 290; 290, boolean_operator:and; 290, 291; 290, 303; 291, parenthesized_expression; 291, 292; 292, boolean_operator:or; 292, 293; 292, 298; 293, comparison_operator:==; 293, 294; 293, 297; 294, attribute; 294, 295; 294, 296; 295, identifier:operation; 296, identifier:type; 297, string:'message'; 298, comparison_operator:==; 298, 299; 298, 302; 299, attribute; 299, 300; 299, 301; 300, identifier:operation; 301, identifier:type; 302, string:'embedded'; 303, comparison_operator:==; 303, 304; 303, 307; 304, attribute; 304, 305; 304, 306; 305, identifier:operation; 306, identifier:subtype; 307, string:'chat'; 308, block; 308, 309; 308, 332; 308, 339; 309, expression_statement; 309, 310; 310, assignment; 310, 311; 310, 312; 311, identifier:chat; 312, call; 312, 313; 312, 314; 313, identifier:ChatMessage; 314, argument_list; 314, 315; 314, 320; 314, 321; 314, 326; 314, 331; 315, attribute; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:operation; 318, identifier:data; 319, identifier:text; 320, identifier:current_time; 321, call; 321, 322; 321, 325; 322, attribute; 322, 323; 322, 324; 323, identifier:self; 324, identifier:_players; 325, argument_list; 326, subscript; 326, 327; 326, 330; 327, attribute; 327, 328; 327, 329; 328, identifier:self; 329, identifier:_diplomacy; 330, string:'type'; 331, string:'game'; 332, expression_statement; 332, 333; 333, call; 333, 334; 333, 337; 334, attribute; 334, 335; 334, 336; 335, identifier:self; 336, identifier:_parse_chat; 337, argument_list; 337, 338; 338, identifier:chat; 339, if_statement; 339, 340; 339, 345; 340, comparison_operator:in; 340, 341; 340, 344; 341, attribute; 341, 342; 341, 343; 342, identifier:operation; 343, identifier:type; 344, identifier:op_types; 345, block; 345, 346; 346, expression_statement; 346, 347; 347, yield; 347, 348; 348, identifier:chat | def operations(self, op_types=None):
"""Process operation stream."""
if not op_types:
op_types = ['message', 'action', 'sync', 'viewlock', 'savedchapter']
while self._handle.tell() < self._eof:
current_time = mgz.util.convert_to_timestamp(self._time / 1000)
try:
operation = mgz.body.operation.parse_stream(self._handle)
except (ConstructError, ValueError):
raise MgzError('failed to parse body operation')
if operation.type == 'action':
if operation.action.type in ACTIONS_WITH_PLAYER_ID:
counter = self._actions_by_player[operation.action.player_id]
counter.update([operation.action.type])
else:
self._actions_without_player.update([operation.action.type])
if operation.type == 'action' and isinstance(operation.action.type, int):
print(operation.action)
if operation.type == 'sync':
self._time += operation.time_increment
if operation.type == 'action' and operation.action.type == 'postgame':
self._postgame = operation
if operation.type == 'action':
action = Action(operation, current_time)
self._parse_action(action, current_time)
if operation.type == 'savedchapter':
# fix: Don't load messages we already saw in header or prev saved chapters
self._parse_lobby_chat(operation.lobby.messages, 'save', current_time)
if operation.type == 'viewlock':
if operation.type in op_types:
yield Viewlock(operation)
elif operation.type == 'action' and operation.action.type != 'postgame':
if operation.type in op_types:
yield Action(operation, current_time)
elif ((operation.type == 'message' or operation.type == 'embedded')
and operation.subtype == 'chat'):
chat = ChatMessage(operation.data.text, current_time,
self._players(), self._diplomacy['type'], 'game')
self._parse_chat(chat)
if operation.type in op_types:
yield chat |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:run; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 22; 5, 26; 5, 32; 5, 46; 5, 405; 5, 406; 5, 413; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:ctx; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:FSM; 14, identifier:Context; 15, argument_list; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:name; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:device; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:transition_counter; 25, integer:0; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:timeout; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:timeout; 32, expression_statement; 32, 33; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:log; 37, argument_list; 37, 38; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, string:"{} Start"; 41, identifier:format; 42, argument_list; 42, 43; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:name; 46, while_statement; 46, 47; 46, 52; 47, comparison_operator:<; 47, 48; 47, 49; 48, identifier:transition_counter; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:max_transitions; 52, block; 52, 53; 52, 57; 52, 373; 53, expression_statement; 53, 54; 54, augmented_assignment:+=; 54, 55; 54, 56; 55, identifier:transition_counter; 56, integer:1; 57, try_statement; 57, 58; 57, 360; 58, block; 58, 59; 58, 65; 58, 150; 58, 158; 58, 168; 58, 180; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:start_time; 62, call; 62, 63; 62, 64; 63, identifier:time; 64, argument_list; 65, if_statement; 65, 66; 65, 71; 65, 95; 66, comparison_operator:is; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:self; 69, identifier:init_pattern; 70, None; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:ctx; 76, identifier:event; 77, call; 77, 78; 77, 83; 78, attribute; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:self; 81, identifier:ctrl; 82, identifier:expect; 83, argument_list; 83, 84; 83, 87; 83, 92; 84, attribute; 84, 85; 84, 86; 85, identifier:self; 86, identifier:events; 87, keyword_argument; 87, 88; 87, 89; 88, identifier:searchwindowsize; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:searchwindowsize; 92, keyword_argument; 92, 93; 92, 94; 93, identifier:timeout; 94, identifier:timeout; 95, else_clause; 95, 96; 96, block; 96, 97; 96, 114; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:log; 102, argument_list; 102, 103; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, string:"INIT_PATTERN={}"; 106, identifier:format; 107, argument_list; 107, 108; 108, call; 108, 109; 108, 110; 109, identifier:pattern_to_str; 110, argument_list; 110, 111; 111, attribute; 111, 112; 111, 113; 112, identifier:self; 113, identifier:init_pattern; 114, try_statement; 114, 115; 114, 131; 114, 142; 115, block; 115, 116; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:ctx; 120, identifier:event; 121, call; 121, 122; 121, 127; 122, attribute; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:events; 126, identifier:index; 127, argument_list; 127, 128; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:init_pattern; 131, except_clause; 131, 132; 131, 133; 132, identifier:ValueError; 133, block; 133, 134; 133, 141; 134, expression_statement; 134, 135; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:self; 138, identifier:log; 139, argument_list; 139, 140; 140, string:"INIT_PATTERN unknown."; 141, continue_statement; 142, finally_clause; 142, 143; 143, block; 143, 144; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:self; 148, identifier:init_pattern; 149, None; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:finish_time; 153, binary_operator:-; 153, 154; 153, 157; 154, call; 154, 155; 154, 156; 155, identifier:time; 156, argument_list; 157, identifier:start_time; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:key; 161, tuple; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:ctx; 164, identifier:event; 165, attribute; 165, 166; 165, 167; 166, identifier:ctx; 167, identifier:state; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:ctx; 172, identifier:pattern; 173, subscript; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:self; 176, identifier:events; 177, attribute; 177, 178; 177, 179; 178, identifier:ctx; 179, identifier:event; 180, if_statement; 180, 181; 180, 186; 180, 340; 181, comparison_operator:in; 181, 182; 181, 183; 182, identifier:key; 183, attribute; 183, 184; 183, 185; 184, identifier:self; 185, identifier:transition_table; 186, block; 186, 187; 186, 195; 186, 202; 186, 221; 186, 311; 186, 321; 186, 327; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:transition; 190, subscript; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:self; 193, identifier:transition_table; 194, identifier:key; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 201; 197, pattern_list; 197, 198; 197, 199; 197, 200; 198, identifier:next_state; 199, identifier:action_instance; 200, identifier:next_timeout; 201, identifier:transition; 202, expression_statement; 202, 203; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:self; 206, identifier:log; 207, argument_list; 207, 208; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, string:"E={},S={},T={},RT={:.2f}"; 211, identifier:format; 212, argument_list; 212, 213; 212, 216; 212, 219; 212, 220; 213, attribute; 213, 214; 213, 215; 214, identifier:ctx; 215, identifier:event; 216, attribute; 216, 217; 216, 218; 217, identifier:ctx; 218, identifier:state; 219, identifier:timeout; 220, identifier:finish_time; 221, if_statement; 221, 222; 221, 232; 221, 256; 221, 277; 221, 289; 222, boolean_operator:and; 222, 223; 222, 227; 223, call; 223, 224; 223, 225; 224, identifier:callable; 225, argument_list; 225, 226; 226, identifier:action_instance; 227, not_operator; 227, 228; 228, call; 228, 229; 228, 230; 229, identifier:isclass; 230, argument_list; 230, 231; 231, identifier:action_instance; 232, block; 232, 233; 233, if_statement; 233, 234; 233, 239; 234, not_operator; 234, 235; 235, call; 235, 236; 235, 237; 236, identifier:action_instance; 237, argument_list; 237, 238; 238, identifier:ctx; 239, block; 239, 240; 239, 254; 240, expression_statement; 240, 241; 241, call; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:self; 244, identifier:log; 245, argument_list; 245, 246; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, string:"Error: {}"; 249, identifier:format; 250, argument_list; 250, 251; 251, attribute; 251, 252; 251, 253; 252, identifier:ctx; 253, identifier:msg; 254, return_statement; 254, 255; 255, False; 256, elif_clause; 256, 257; 256, 262; 257, call; 257, 258; 257, 259; 258, identifier:isinstance; 259, argument_list; 259, 260; 259, 261; 260, identifier:action_instance; 261, identifier:Exception; 262, block; 262, 263; 262, 275; 263, expression_statement; 263, 264; 264, call; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:self; 267, identifier:log; 268, argument_list; 268, 269; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, string:"A=Exception {}"; 272, identifier:format; 273, argument_list; 273, 274; 274, identifier:action_instance; 275, raise_statement; 275, 276; 276, identifier:action_instance; 277, elif_clause; 277, 278; 277, 281; 278, comparison_operator:is; 278, 279; 278, 280; 279, identifier:action_instance; 280, None; 281, block; 281, 282; 282, expression_statement; 282, 283; 283, call; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:self; 286, identifier:log; 287, argument_list; 287, 288; 288, string:"A=None"; 289, else_clause; 289, 290; 290, block; 290, 291; 290, 306; 291, expression_statement; 291, 292; 292, call; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:self; 295, identifier:log; 296, argument_list; 296, 297; 297, call; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, string:"FSM Action is not callable: {}"; 300, identifier:format; 301, argument_list; 301, 302; 302, call; 302, 303; 302, 304; 303, identifier:str; 304, argument_list; 304, 305; 305, identifier:action_instance; 306, raise_statement; 306, 307; 307, call; 307, 308; 307, 309; 308, identifier:RuntimeWarning; 309, argument_list; 309, 310; 310, string:"FSM Action is not callable"; 311, if_statement; 311, 312; 311, 315; 311, 316; 312, comparison_operator:!=; 312, 313; 312, 314; 313, identifier:next_timeout; 314, integer:0; 315, comment; 316, block; 316, 317; 317, expression_statement; 317, 318; 318, assignment; 318, 319; 318, 320; 319, identifier:timeout; 320, identifier:next_timeout; 321, expression_statement; 321, 322; 322, assignment; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:ctx; 325, identifier:state; 326, identifier:next_state; 327, expression_statement; 327, 328; 328, call; 328, 329; 328, 332; 329, attribute; 329, 330; 329, 331; 330, identifier:self; 331, identifier:log; 332, argument_list; 332, 333; 333, call; 333, 334; 333, 337; 334, attribute; 334, 335; 334, 336; 335, string:"NS={},NT={}"; 336, identifier:format; 337, argument_list; 337, 338; 337, 339; 338, identifier:next_state; 339, identifier:timeout; 340, else_clause; 340, 341; 341, block; 341, 342; 341, 359; 342, expression_statement; 342, 343; 343, call; 343, 344; 343, 347; 344, attribute; 344, 345; 344, 346; 345, identifier:self; 346, identifier:log; 347, argument_list; 347, 348; 348, call; 348, 349; 348, 352; 349, attribute; 349, 350; 349, 351; 350, string:"Unknown transition: EVENT={},STATE={}"; 351, identifier:format; 352, argument_list; 352, 353; 352, 356; 353, attribute; 353, 354; 353, 355; 354, identifier:ctx; 355, identifier:event; 356, attribute; 356, 357; 356, 358; 357, identifier:ctx; 358, identifier:state; 359, continue_statement; 360, except_clause; 360, 361; 360, 362; 361, identifier:EOF; 362, block; 362, 363; 363, raise_statement; 363, 364; 364, call; 364, 365; 364, 366; 365, identifier:ConnectionError; 366, argument_list; 366, 367; 366, 368; 367, string:"Session closed unexpectedly"; 368, attribute; 368, 369; 368, 372; 369, attribute; 369, 370; 369, 371; 370, identifier:self; 371, identifier:ctrl; 372, identifier:hostname; 373, if_statement; 373, 374; 373, 382; 374, boolean_operator:or; 374, 375; 374, 378; 375, attribute; 375, 376; 375, 377; 376, identifier:ctx; 377, identifier:finished; 378, comparison_operator:==; 378, 379; 378, 380; 379, identifier:next_state; 380, unary_operator:-; 380, 381; 381, integer:1; 382, block; 382, 383; 382, 403; 383, expression_statement; 383, 384; 384, call; 384, 385; 384, 388; 385, attribute; 385, 386; 385, 387; 386, identifier:self; 387, identifier:log; 388, argument_list; 388, 389; 389, call; 389, 390; 389, 393; 390, attribute; 390, 391; 390, 392; 391, string:"{} Stop at E={},S={}"; 392, identifier:format; 393, argument_list; 393, 394; 393, 397; 393, 400; 394, attribute; 394, 395; 394, 396; 395, identifier:self; 396, identifier:name; 397, attribute; 397, 398; 397, 399; 398, identifier:ctx; 399, identifier:event; 400, attribute; 400, 401; 400, 402; 401, identifier:ctx; 402, identifier:state; 403, return_statement; 403, 404; 404, True; 405, comment; 406, expression_statement; 406, 407; 407, call; 407, 408; 407, 411; 408, attribute; 408, 409; 408, 410; 409, identifier:self; 410, identifier:log; 411, argument_list; 411, 412; 412, string:"FSM looped. Exiting"; 413, return_statement; 413, 414; 414, False | def run(self):
"""Start the FSM.
Returns:
boolean: True if FSM reaches the last state or false if the exception or error message was raised
"""
ctx = FSM.Context(self.name, self.device)
transition_counter = 0
timeout = self.timeout
self.log("{} Start".format(self.name))
while transition_counter < self.max_transitions:
transition_counter += 1
try:
start_time = time()
if self.init_pattern is None:
ctx.event = self.ctrl.expect(self.events, searchwindowsize=self.searchwindowsize, timeout=timeout)
else:
self.log("INIT_PATTERN={}".format(pattern_to_str(self.init_pattern)))
try:
ctx.event = self.events.index(self.init_pattern)
except ValueError:
self.log("INIT_PATTERN unknown.")
continue
finally:
self.init_pattern = None
finish_time = time() - start_time
key = (ctx.event, ctx.state)
ctx.pattern = self.events[ctx.event]
if key in self.transition_table:
transition = self.transition_table[key]
next_state, action_instance, next_timeout = transition
self.log("E={},S={},T={},RT={:.2f}".format(ctx.event, ctx.state, timeout, finish_time))
if callable(action_instance) and not isclass(action_instance):
if not action_instance(ctx):
self.log("Error: {}".format(ctx.msg))
return False
elif isinstance(action_instance, Exception):
self.log("A=Exception {}".format(action_instance))
raise action_instance
elif action_instance is None:
self.log("A=None")
else:
self.log("FSM Action is not callable: {}".format(str(action_instance)))
raise RuntimeWarning("FSM Action is not callable")
if next_timeout != 0: # no change if set to 0
timeout = next_timeout
ctx.state = next_state
self.log("NS={},NT={}".format(next_state, timeout))
else:
self.log("Unknown transition: EVENT={},STATE={}".format(ctx.event, ctx.state))
continue
except EOF:
raise ConnectionError("Session closed unexpectedly", self.ctrl.hostname)
if ctx.finished or next_state == -1:
self.log("{} Stop at E={},S={}".format(self.name, ctx.event, ctx.state))
return True
# check while else if even exists
self.log("FSM looped. Exiting")
return False |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:build; 3, parameters; 3, 4; 3, 7; 4, default_parameter; 4, 5; 4, 6; 5, identifier:port; 6, integer:8000; 7, default_parameter; 7, 8; 7, 9; 8, identifier:fixtures; 9, None; 10, block; 10, 11; 10, 13; 10, 19; 10, 29; 10, 35; 10, 41; 10, 48; 10, 57; 10, 69; 10, 79; 10, 83; 10, 87; 10, 450; 10, 459; 10, 465; 10, 475; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:extractor; 16, call; 16, 17; 16, 18; 17, identifier:Extractor; 18, argument_list; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:parser; 22, call; 22, 23; 22, 24; 23, identifier:Parser; 24, argument_list; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:extractor; 27, identifier:url_details; 28, identifier:fixtures; 29, expression_statement; 29, 30; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:parser; 33, identifier:parse; 34, argument_list; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:url_details; 38, attribute; 38, 39; 38, 40; 39, identifier:parser; 40, identifier:results; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:_store; 44, call; 44, 45; 44, 46; 45, identifier:get_store; 46, argument_list; 46, 47; 47, identifier:url_details; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:store; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:json; 54, identifier:dumps; 55, argument_list; 55, 56; 56, identifier:_store; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:variables; 60, call; 60, 61; 60, 62; 61, identifier:str; 62, argument_list; 62, 63; 63, call; 63, 64; 63, 65; 64, identifier:Variable; 65, argument_list; 65, 66; 65, 67; 65, 68; 66, string:'let'; 67, string:'store'; 68, identifier:store; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:functions; 72, binary_operator:+; 72, 73; 72, 78; 73, binary_operator:+; 73, 74; 73, 77; 74, binary_operator:+; 74, 75; 74, 76; 75, identifier:DATA_FINDER; 76, identifier:GET_HANDLER; 77, identifier:MODIFY_HANDLER; 78, identifier:POST_HANDLER; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:endpoints; 82, list:[]; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:endpoint_uris; 86, list:[]; 87, for_statement; 87, 88; 87, 89; 87, 92; 88, identifier:u; 89, attribute; 89, 90; 89, 91; 90, identifier:parser; 91, identifier:results; 92, block; 92, 93; 92, 99; 92, 128; 92, 138; 92, 139; 92, 160; 92, 224; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:endpoint; 96, call; 96, 97; 96, 98; 97, identifier:Endpoint; 98, argument_list; 99, if_statement; 99, 100; 99, 111; 99, 122; 100, comparison_operator:in; 100, 101; 100, 108; 101, call; 101, 102; 101, 107; 102, attribute; 102, 103; 102, 106; 103, subscript; 103, 104; 103, 105; 104, identifier:u; 105, string:'method'; 106, identifier:lower; 107, argument_list; 108, list:['get', 'post']; 108, 109; 108, 110; 109, string:'get'; 110, string:'post'; 111, block; 111, 112; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:method; 115, call; 115, 116; 115, 121; 116, attribute; 116, 117; 116, 120; 117, subscript; 117, 118; 117, 119; 118, identifier:u; 119, string:'method'; 120, identifier:lower; 121, argument_list; 122, else_clause; 122, 123; 123, block; 123, 124; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:method; 127, string:'modify'; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:response; 131, call; 131, 132; 131, 133; 132, identifier:str; 133, argument_list; 133, 134; 134, call; 134, 135; 134, 136; 135, identifier:ResponseBody; 136, argument_list; 136, 137; 137, identifier:method; 138, comment; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 146; 141, pattern_list; 141, 142; 141, 145; 142, subscript; 142, 143; 142, 144; 143, identifier:u; 144, string:'url'; 145, identifier:list_url; 146, call; 146, 147; 146, 148; 147, identifier:clean_url; 148, argument_list; 148, 149; 148, 152; 148, 153; 149, subscript; 149, 150; 149, 151; 150, identifier:u; 151, string:'full_url'; 152, identifier:_store; 153, call; 153, 154; 153, 159; 154, attribute; 154, 155; 154, 158; 155, subscript; 155, 156; 155, 157; 156, identifier:u; 157, string:'method'; 158, identifier:lower; 159, argument_list; 160, if_statement; 160, 161; 160, 174; 161, boolean_operator:and; 161, 162; 161, 165; 162, comparison_operator:is; 162, 163; 162, 164; 163, identifier:list_url; 164, None; 165, comparison_operator:==; 165, 166; 165, 173; 166, call; 166, 167; 166, 172; 167, attribute; 167, 168; 167, 171; 168, subscript; 168, 169; 168, 170; 169, identifier:u; 170, string:'method'; 171, identifier:lower; 172, argument_list; 173, string:'get'; 174, block; 174, 175; 174, 181; 174, 190; 174, 208; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:list_endpoint; 178, call; 178, 179; 178, 180; 179, identifier:Endpoint; 180, argument_list; 181, expression_statement; 181, 182; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:list_endpoint; 185, identifier:construct; 186, argument_list; 186, 187; 186, 188; 186, 189; 187, string:'get'; 188, identifier:list_url; 189, identifier:response; 190, if_statement; 190, 191; 190, 197; 191, comparison_operator:not; 191, 192; 191, 196; 192, call; 192, 193; 192, 194; 193, identifier:str; 194, argument_list; 194, 195; 195, identifier:list_endpoint; 196, identifier:endpoints; 197, block; 197, 198; 198, expression_statement; 198, 199; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:endpoints; 202, identifier:append; 203, argument_list; 203, 204; 204, call; 204, 205; 204, 206; 205, identifier:str; 206, argument_list; 206, 207; 207, identifier:list_endpoint; 208, if_statement; 208, 209; 208, 214; 209, comparison_operator:not; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:list_endpoint; 212, identifier:uri; 213, identifier:endpoint_uris; 214, block; 214, 215; 215, expression_statement; 215, 216; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:endpoint_uris; 219, identifier:append; 220, argument_list; 220, 221; 221, attribute; 221, 222; 221, 223; 222, identifier:list_endpoint; 223, identifier:uri; 224, if_statement; 224, 225; 224, 228; 224, 401; 225, comparison_operator:==; 225, 226; 225, 227; 226, identifier:method; 227, string:'modify'; 228, block; 228, 229; 228, 242; 229, expression_statement; 229, 230; 230, assignment; 230, 231; 230, 232; 231, identifier:without_prefix; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:re; 235, identifier:sub; 236, argument_list; 236, 237; 236, 238; 236, 239; 237, string:r'\/(\w+)\_\_'; 238, string:''; 239, subscript; 239, 240; 239, 241; 240, identifier:u; 241, string:'url'; 242, for_statement; 242, 243; 242, 246; 242, 251; 243, pattern_list; 243, 244; 243, 245; 244, identifier:k; 245, identifier:v; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:_store; 249, identifier:items; 250, argument_list; 251, block; 251, 252; 252, if_statement; 252, 253; 252, 256; 253, comparison_operator:in; 253, 254; 253, 255; 254, identifier:without_prefix; 255, identifier:k; 256, block; 256, 257; 256, 267; 256, 276; 256, 280; 256, 295; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 260; 259, identifier:options; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:v; 263, identifier:get; 264, argument_list; 264, 265; 264, 266; 265, string:'options'; 266, string:'{}'; 267, expression_statement; 267, 268; 268, assignment; 268, 269; 268, 270; 269, identifier:options; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:ast; 273, identifier:literal_eval; 274, argument_list; 274, 275; 275, identifier:options; 276, expression_statement; 276, 277; 277, assignment; 277, 278; 277, 279; 278, identifier:modifiers; 279, list:[]; 280, if_statement; 280, 281; 280, 284; 281, comparison_operator:is; 281, 282; 281, 283; 282, identifier:options; 283, None; 284, block; 284, 285; 285, expression_statement; 285, 286; 286, assignment; 286, 287; 286, 288; 287, identifier:modifiers; 288, call; 288, 289; 288, 292; 289, attribute; 289, 290; 289, 291; 290, identifier:options; 291, identifier:get; 292, argument_list; 292, 293; 292, 294; 293, string:'modifiers'; 294, list:[]; 295, if_statement; 295, 296; 295, 297; 296, identifier:modifiers; 297, block; 297, 298; 298, for_statement; 298, 299; 298, 300; 298, 301; 299, identifier:mod; 300, identifier:modifiers; 301, block; 301, 302; 302, if_statement; 302, 303; 302, 312; 303, comparison_operator:==; 303, 304; 303, 311; 304, call; 304, 305; 304, 310; 305, attribute; 305, 306; 305, 309; 306, subscript; 306, 307; 306, 308; 307, identifier:u; 308, string:'method'; 309, identifier:lower; 310, argument_list; 311, identifier:mod; 312, block; 312, 313; 312, 319; 312, 323; 312, 352; 312, 367; 312, 385; 313, expression_statement; 313, 314; 314, assignment; 314, 315; 314, 316; 315, identifier:mod_endpoint; 316, call; 316, 317; 316, 318; 317, identifier:Endpoint; 318, argument_list; 319, expression_statement; 319, 320; 320, assignment; 320, 321; 320, 322; 321, identifier:uri; 322, identifier:without_prefix; 323, if_statement; 323, 324; 323, 338; 324, boolean_operator:and; 324, 325; 324, 333; 325, comparison_operator:is; 325, 326; 325, 332; 326, call; 326, 327; 326, 330; 327, attribute; 327, 328; 327, 329; 328, identifier:v; 329, identifier:get; 330, argument_list; 330, 331; 331, string:'position'; 332, None; 333, comparison_operator:==; 333, 334; 333, 337; 334, subscript; 334, 335; 334, 336; 335, identifier:v; 336, string:'position'; 337, string:'url'; 338, block; 338, 339; 339, expression_statement; 339, 340; 340, assignment; 340, 341; 340, 342; 341, identifier:uri; 342, call; 342, 343; 342, 346; 343, attribute; 343, 344; 343, 345; 344, identifier:re; 345, identifier:sub; 346, argument_list; 346, 347; 346, 348; 346, 349; 347, string:r'\/?\_\_key'; 348, string:'/:id'; 349, subscript; 349, 350; 349, 351; 350, identifier:u; 351, string:'full_url'; 352, expression_statement; 352, 353; 353, call; 353, 354; 353, 357; 354, attribute; 354, 355; 354, 356; 355, identifier:mod_endpoint; 356, identifier:construct; 357, argument_list; 357, 358; 357, 365; 357, 366; 358, call; 358, 359; 358, 364; 359, attribute; 359, 360; 359, 363; 360, subscript; 360, 361; 360, 362; 361, identifier:u; 362, string:'method'; 363, identifier:lower; 364, argument_list; 365, identifier:uri; 366, identifier:response; 367, if_statement; 367, 368; 367, 374; 368, comparison_operator:not; 368, 369; 368, 373; 369, call; 369, 370; 369, 371; 370, identifier:str; 371, argument_list; 371, 372; 372, identifier:mod_endpoint; 373, identifier:endpoints; 374, block; 374, 375; 375, expression_statement; 375, 376; 376, call; 376, 377; 376, 380; 377, attribute; 377, 378; 377, 379; 378, identifier:endpoints; 379, identifier:append; 380, argument_list; 380, 381; 381, call; 381, 382; 381, 383; 382, identifier:str; 383, argument_list; 383, 384; 384, identifier:mod_endpoint; 385, if_statement; 385, 386; 385, 391; 386, comparison_operator:not; 386, 387; 386, 390; 387, attribute; 387, 388; 387, 389; 388, identifier:mod_endpoint; 389, identifier:uri; 390, identifier:endpoint_uris; 391, block; 391, 392; 392, expression_statement; 392, 393; 393, call; 393, 394; 393, 397; 394, attribute; 394, 395; 394, 396; 395, identifier:endpoint_uris; 396, identifier:append; 397, argument_list; 397, 398; 398, attribute; 398, 399; 398, 400; 399, identifier:mod_endpoint; 400, identifier:uri; 401, else_clause; 401, 402; 402, block; 402, 403; 402, 416; 402, 434; 403, expression_statement; 403, 404; 404, call; 404, 405; 404, 408; 405, attribute; 405, 406; 405, 407; 406, identifier:endpoint; 407, identifier:construct; 408, argument_list; 408, 409; 408, 412; 408, 415; 409, subscript; 409, 410; 409, 411; 410, identifier:u; 411, string:'method'; 412, subscript; 412, 413; 412, 414; 413, identifier:u; 414, string:'url'; 415, identifier:response; 416, if_statement; 416, 417; 416, 423; 417, comparison_operator:not; 417, 418; 417, 422; 418, call; 418, 419; 418, 420; 419, identifier:str; 420, argument_list; 420, 421; 421, identifier:endpoint; 422, identifier:endpoints; 423, block; 423, 424; 424, expression_statement; 424, 425; 425, call; 425, 426; 425, 429; 426, attribute; 426, 427; 426, 428; 427, identifier:endpoints; 428, identifier:append; 429, argument_list; 429, 430; 430, call; 430, 431; 430, 432; 431, identifier:str; 432, argument_list; 432, 433; 433, identifier:endpoint; 434, if_statement; 434, 435; 434, 440; 435, comparison_operator:not; 435, 436; 435, 439; 436, attribute; 436, 437; 436, 438; 437, identifier:endpoint; 438, identifier:uri; 439, identifier:endpoint_uris; 440, block; 440, 441; 441, expression_statement; 441, 442; 442, call; 442, 443; 442, 446; 443, attribute; 443, 444; 443, 445; 444, identifier:endpoint_uris; 445, identifier:append; 446, argument_list; 446, 447; 447, attribute; 447, 448; 447, 449; 448, identifier:endpoint; 449, identifier:uri; 450, expression_statement; 450, 451; 451, assignment; 451, 452; 451, 453; 452, identifier:endpoints; 453, call; 453, 454; 453, 457; 454, attribute; 454, 455; 454, 456; 455, string:''; 456, identifier:join; 457, argument_list; 457, 458; 458, identifier:endpoints; 459, expression_statement; 459, 460; 460, assignment; 460, 461; 460, 462; 461, identifier:express; 462, call; 462, 463; 462, 464; 463, identifier:ExpressServer; 464, argument_list; 465, expression_statement; 465, 466; 466, call; 466, 467; 466, 470; 467, attribute; 467, 468; 467, 469; 468, identifier:express; 469, identifier:construct; 470, argument_list; 470, 471; 470, 472; 470, 473; 470, 474; 471, identifier:variables; 472, identifier:functions; 473, identifier:endpoints; 474, identifier:port; 475, return_statement; 475, 476; 476, identifier:express | def build(port=8000, fixtures=None):
"""
Builds a server file.
1. Extract mock response details from all valid docstrings in existing views
2. Parse and generate mock values
3. Create a store of all endpoints and data
4. Construct server file
"""
extractor = Extractor()
parser = Parser(extractor.url_details, fixtures)
parser.parse()
url_details = parser.results
_store = get_store(url_details)
store = json.dumps(_store)
variables = str(Variable('let', 'store', store))
functions = DATA_FINDER + GET_HANDLER + MODIFY_HANDLER + POST_HANDLER
endpoints = []
endpoint_uris = []
for u in parser.results:
endpoint = Endpoint()
if u['method'].lower() in ['get', 'post']:
method = u['method'].lower()
else:
method = 'modify'
response = str(ResponseBody(method))
# Check in store if the base url has individual instances
u['url'], list_url = clean_url(u['full_url'], _store, u['method'].lower())
if list_url is not None and u['method'].lower() == 'get':
list_endpoint = Endpoint()
list_endpoint.construct('get', list_url, response)
if str(list_endpoint) not in endpoints:
endpoints.append(str(list_endpoint))
if list_endpoint.uri not in endpoint_uris:
endpoint_uris.append(list_endpoint.uri)
if method == 'modify':
without_prefix = re.sub(r'\/(\w+)\_\_', '', u['url'])
for k, v in _store.items():
if without_prefix in k:
options = v.get('options', '{}')
options = ast.literal_eval(options)
modifiers = []
if options is not None:
modifiers = options.get('modifiers', [])
if modifiers:
for mod in modifiers:
if u['method'].lower() == mod:
mod_endpoint = Endpoint()
uri = without_prefix
if v.get('position') is not None and v['position'] == 'url':
uri = re.sub(r'\/?\_\_key', '/:id', u['full_url'])
mod_endpoint.construct(u['method'].lower(), uri, response)
if str(mod_endpoint) not in endpoints:
endpoints.append(str(mod_endpoint))
if mod_endpoint.uri not in endpoint_uris:
endpoint_uris.append(mod_endpoint.uri)
else:
endpoint.construct(u['method'], u['url'], response)
if str(endpoint) not in endpoints:
endpoints.append(str(endpoint))
if endpoint.uri not in endpoint_uris:
endpoint_uris.append(endpoint.uri)
endpoints = ''.join(endpoints)
express = ExpressServer()
express.construct(variables, functions, endpoints, port)
return express |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:gpg_decrypt; 3, parameters; 3, 4; 3, 5; 4, identifier:cfg; 5, default_parameter; 5, 6; 5, 7; 6, identifier:gpg_config; 7, None; 8, block; 8, 9; 8, 11; 8, 221; 8, 308; 9, expression_statement; 9, 10; 10, comment; 11, function_definition; 11, 12; 11, 13; 11, 15; 12, function_name:decrypt; 13, parameters; 13, 14; 14, identifier:obj; 15, block; 15, 16; 15, 18; 15, 219; 16, expression_statement; 16, 17; 17, comment; 18, if_statement; 18, 19; 18, 24; 18, 45; 18, 140; 19, call; 19, 20; 19, 21; 20, identifier:isinstance; 21, argument_list; 21, 22; 21, 23; 22, identifier:obj; 23, identifier:list; 24, block; 24, 25; 24, 29; 24, 43; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:res_v; 28, list:[]; 29, for_statement; 29, 30; 29, 31; 29, 32; 30, identifier:item; 31, identifier:obj; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:res_v; 37, identifier:append; 38, argument_list; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:decrypt; 41, argument_list; 41, 42; 42, identifier:item; 43, return_statement; 43, 44; 44, identifier:res_v; 45, elif_clause; 45, 46; 45, 51; 46, call; 46, 47; 46, 48; 47, identifier:isinstance; 48, argument_list; 48, 49; 48, 50; 49, identifier:obj; 50, identifier:dict; 51, block; 51, 52; 52, if_statement; 52, 53; 52, 56; 52, 119; 53, comparison_operator:in; 53, 54; 53, 55; 54, string:'_gpg'; 55, identifier:obj; 56, block; 56, 57; 57, try_statement; 57, 58; 57, 105; 58, block; 58, 59; 58, 70; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:decrypted; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:gpg; 65, identifier:decrypt; 66, argument_list; 66, 67; 67, subscript; 67, 68; 67, 69; 68, identifier:obj; 69, string:'_gpg'; 70, if_statement; 70, 71; 70, 74; 70, 93; 71, attribute; 71, 72; 71, 73; 72, identifier:decrypted; 73, identifier:ok; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:obj; 78, call; 78, 79; 78, 80; 79, identifier:n; 80, argument_list; 80, 81; 81, call; 81, 82; 81, 92; 82, attribute; 82, 83; 82, 91; 83, call; 83, 84; 83, 89; 84, attribute; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:decrypted; 87, identifier:data; 88, identifier:decode; 89, argument_list; 89, 90; 90, string:'utf-8'; 91, identifier:encode; 92, argument_list; 93, else_clause; 93, 94; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:log; 99, identifier:error; 100, argument_list; 100, 101; 100, 102; 101, string:"gpg error unpacking secrets %s"; 102, attribute; 102, 103; 102, 104; 103, identifier:decrypted; 104, identifier:stderr; 105, except_clause; 105, 106; 105, 110; 106, as_pattern; 106, 107; 106, 108; 107, identifier:Exception; 108, as_pattern_target; 108, 109; 109, identifier:err; 110, block; 110, 111; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:log; 115, identifier:error; 116, argument_list; 116, 117; 116, 118; 117, string:"error unpacking secrets %s"; 118, identifier:err; 119, else_clause; 119, 120; 120, block; 120, 121; 121, for_statement; 121, 122; 121, 125; 121, 130; 122, pattern_list; 122, 123; 122, 124; 123, identifier:k; 124, identifier:v; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:obj; 128, identifier:items; 129, argument_list; 130, block; 130, 131; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 136; 133, subscript; 133, 134; 133, 135; 134, identifier:obj; 135, identifier:k; 136, call; 136, 137; 136, 138; 137, identifier:decrypt; 138, argument_list; 138, 139; 139, identifier:v; 140, else_clause; 140, 141; 141, block; 141, 142; 142, try_statement; 142, 143; 142, 209; 143, block; 143, 144; 144, if_statement; 144, 145; 144, 148; 145, comparison_operator:in; 145, 146; 145, 147; 146, string:'BEGIN PGP'; 147, identifier:obj; 148, block; 148, 149; 149, try_statement; 149, 150; 149, 195; 150, block; 150, 151; 150, 160; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:decrypted; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:gpg; 157, identifier:decrypt; 158, argument_list; 158, 159; 159, identifier:obj; 160, if_statement; 160, 161; 160, 164; 160, 183; 161, attribute; 161, 162; 161, 163; 162, identifier:decrypted; 163, identifier:ok; 164, block; 164, 165; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:obj; 168, call; 168, 169; 168, 170; 169, identifier:n; 170, argument_list; 170, 171; 171, call; 171, 172; 171, 182; 172, attribute; 172, 173; 172, 181; 173, call; 173, 174; 173, 179; 174, attribute; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:decrypted; 177, identifier:data; 178, identifier:decode; 179, argument_list; 179, 180; 180, string:'utf-8'; 181, identifier:encode; 182, argument_list; 183, else_clause; 183, 184; 184, block; 184, 185; 185, expression_statement; 185, 186; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:log; 189, identifier:error; 190, argument_list; 190, 191; 190, 192; 191, string:"gpg error unpacking secrets %s"; 192, attribute; 192, 193; 192, 194; 193, identifier:decrypted; 194, identifier:stderr; 195, except_clause; 195, 196; 195, 200; 196, as_pattern; 196, 197; 196, 198; 197, identifier:Exception; 198, as_pattern_target; 198, 199; 199, identifier:err; 200, block; 200, 201; 201, expression_statement; 201, 202; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:log; 205, identifier:error; 206, argument_list; 206, 207; 206, 208; 207, string:"error unpacking secrets %s"; 208, identifier:err; 209, except_clause; 209, 210; 209, 211; 210, identifier:TypeError; 211, block; 211, 212; 212, expression_statement; 212, 213; 213, call; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:log; 216, identifier:debug; 217, argument_list; 217, 218; 218, string:'Pass on decryption. Only decrypt strings'; 219, return_statement; 219, 220; 220, identifier:obj; 221, if_statement; 221, 222; 221, 223; 222, identifier:GPG_IMPORTED; 223, block; 223, 224; 223, 279; 223, 303; 224, if_statement; 224, 225; 224, 227; 225, not_operator; 225, 226; 226, identifier:gpg_config; 227, block; 227, 228; 227, 232; 227, 239; 227, 252; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:gpg_config; 231, dictionary; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 235; 234, identifier:defaults; 235, dictionary; 235, 236; 236, pair; 236, 237; 236, 238; 237, string:'homedir'; 238, string:'~/.gnupg/'; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 242; 241, identifier:env_fields; 242, dictionary; 242, 243; 242, 246; 242, 249; 243, pair; 243, 244; 243, 245; 244, string:'homedir'; 245, string:'FIGGYPY_GPG_HOMEDIR'; 246, pair; 246, 247; 246, 248; 247, string:'binary'; 248, string:'FIGGYPY_GPG_BINARY'; 249, pair; 249, 250; 249, 251; 250, string:'keyring'; 251, string:'FIGGYPY_GPG_KEYRING'; 252, for_statement; 252, 253; 252, 256; 252, 261; 253, pattern_list; 253, 254; 253, 255; 254, identifier:k; 255, identifier:v; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:env_fields; 259, identifier:items; 260, argument_list; 261, block; 261, 262; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 267; 264, subscript; 264, 265; 264, 266; 265, identifier:gpg_config; 266, identifier:k; 267, call; 267, 268; 267, 269; 268, identifier:env_or_default; 269, argument_list; 269, 270; 269, 271; 270, identifier:v; 271, conditional_expression:if; 271, 272; 271, 275; 271, 278; 272, subscript; 272, 273; 272, 274; 273, identifier:defaults; 274, identifier:k; 275, comparison_operator:in; 275, 276; 275, 277; 276, identifier:k; 277, identifier:defaults; 278, None; 279, try_statement; 279, 280; 279, 291; 280, block; 280, 281; 281, expression_statement; 281, 282; 282, assignment; 282, 283; 282, 284; 283, identifier:gpg; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:gnupg; 287, identifier:GPG; 288, argument_list; 288, 289; 289, dictionary_splat; 289, 290; 290, identifier:gpg_config; 291, except_clause; 291, 292; 291, 295; 292, tuple; 292, 293; 292, 294; 293, identifier:OSError; 294, identifier:RuntimeError; 295, block; 295, 296; 296, expression_statement; 296, 297; 297, call; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:log; 300, identifier:exception; 301, argument_list; 301, 302; 302, string:'Failed to configure gpg. Will be unable to decrypt secrets.'; 303, return_statement; 303, 304; 304, call; 304, 305; 304, 306; 305, identifier:decrypt; 306, argument_list; 306, 307; 307, identifier:cfg; 308, return_statement; 308, 309; 309, identifier:cfg | def gpg_decrypt(cfg, gpg_config=None):
"""Decrypt GPG objects in configuration.
Args:
cfg (dict): configuration dictionary
gpg_config (dict): gpg configuration
dict of arguments for gpg including:
homedir, binary, and keyring (require all if any)
example:
gpg_config = {'homedir': '~/.gnupg/',
'binary': 'gpg',
'keyring': 'pubring.kbx'}
Returns:
dict: decrypted configuration dictionary
The aim is to find in the dictionary items which have been encrypted
with gpg, then decrypt them if possible.
We will either detect the encryption based on the PGP block text or a
user can create a key "_gpg" in which to store the data. Either case
will work. In the case of the "_gpg" key all data at this level will
be replaced with the decrypted contents. For example:
{'component': {'key': 'PGP Block ...'}}
will transform to:
{'component': {'key': 'decrypted value'}}
However:
{'component': {'key': {'_gpg': 'PGP Block ...', 'nothing': 'should go here'}}}
will transform to:
{'component': {'key': 'decrypted value'}}
"""
def decrypt(obj):
"""Decrypt the object.
It is an inner function because we must first verify that gpg
is ready. If we did them in the same function we would end up
calling the gpg checks several times, potentially, since we are
calling this recursively.
"""
if isinstance(obj, list):
res_v = []
for item in obj:
res_v.append(decrypt(item))
return res_v
elif isinstance(obj, dict):
if '_gpg' in obj:
try:
decrypted = gpg.decrypt(obj['_gpg'])
if decrypted.ok:
obj = n(decrypted.data.decode('utf-8').encode())
else:
log.error("gpg error unpacking secrets %s", decrypted.stderr)
except Exception as err:
log.error("error unpacking secrets %s", err)
else:
for k, v in obj.items():
obj[k] = decrypt(v)
else:
try:
if 'BEGIN PGP' in obj:
try:
decrypted = gpg.decrypt(obj)
if decrypted.ok:
obj = n(decrypted.data.decode('utf-8').encode())
else:
log.error("gpg error unpacking secrets %s", decrypted.stderr)
except Exception as err:
log.error("error unpacking secrets %s", err)
except TypeError:
log.debug('Pass on decryption. Only decrypt strings')
return obj
if GPG_IMPORTED:
if not gpg_config:
gpg_config = {}
defaults = {'homedir': '~/.gnupg/'}
env_fields = {'homedir': 'FIGGYPY_GPG_HOMEDIR',
'binary': 'FIGGYPY_GPG_BINARY',
'keyring': 'FIGGYPY_GPG_KEYRING'}
for k, v in env_fields.items():
gpg_config[k] = env_or_default(v, defaults[k] if k in defaults else None)
try:
gpg = gnupg.GPG(**gpg_config)
except (OSError, RuntimeError):
log.exception('Failed to configure gpg. Will be unable to decrypt secrets.')
return decrypt(cfg)
return cfg |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_teams; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 21; 5, 25; 5, 194; 5, 202; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 14; 9, subscript; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:_cache; 13, string:'teams'; 14, block; 14, 15; 15, return_statement; 15, 16; 16, subscript; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:_cache; 20, string:'teams'; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:teams; 24, list:[]; 25, for_statement; 25, 26; 25, 29; 25, 39; 26, pattern_list; 26, 27; 26, 28; 27, identifier:j; 28, identifier:player; 29, call; 29, 30; 29, 31; 30, identifier:enumerate; 31, argument_list; 31, 32; 32, attribute; 32, 33; 32, 38; 33, attribute; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:_header; 37, identifier:initial; 38, identifier:players; 39, block; 39, 40; 39, 44; 39, 178; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:added; 43, False; 44, for_statement; 44, 45; 44, 46; 44, 60; 45, identifier:i; 46, call; 46, 47; 46, 48; 47, identifier:range; 48, argument_list; 48, 49; 48, 50; 49, integer:0; 50, call; 50, 51; 50, 52; 51, identifier:len; 52, argument_list; 52, 53; 53, attribute; 53, 54; 53, 59; 54, attribute; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:_header; 58, identifier:initial; 59, identifier:players; 60, block; 60, 61; 61, if_statement; 61, 62; 61, 71; 62, comparison_operator:==; 62, 63; 62, 70; 63, subscript; 63, 64; 63, 69; 64, attribute; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:player; 67, identifier:attributes; 68, identifier:my_diplomacy; 69, identifier:i; 70, string:'ally'; 71, block; 71, 72; 71, 76; 71, 80; 71, 84; 71, 134; 71, 146; 71, 160; 71, 174; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:inner_team; 75, False; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:outer_team; 79, False; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:new_team; 83, True; 84, for_statement; 84, 85; 84, 88; 84, 92; 85, pattern_list; 85, 86; 85, 87; 86, identifier:t; 87, identifier:tl; 88, call; 88, 89; 88, 90; 89, identifier:enumerate; 90, argument_list; 90, 91; 91, identifier:teams; 92, block; 92, 93; 92, 106; 92, 120; 93, if_statement; 93, 94; 93, 101; 94, boolean_operator:or; 94, 95; 94, 98; 95, comparison_operator:in; 95, 96; 95, 97; 96, identifier:j; 97, identifier:tl; 98, comparison_operator:in; 98, 99; 98, 100; 99, identifier:i; 100, identifier:tl; 101, block; 101, 102; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:new_team; 105, False; 106, if_statement; 106, 107; 106, 114; 107, boolean_operator:and; 107, 108; 107, 111; 108, comparison_operator:in; 108, 109; 108, 110; 109, identifier:j; 110, identifier:tl; 111, comparison_operator:not; 111, 112; 111, 113; 112, identifier:i; 113, identifier:tl; 114, block; 114, 115; 114, 119; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:inner_team; 118, identifier:t; 119, break_statement; 120, if_statement; 120, 121; 120, 128; 121, boolean_operator:and; 121, 122; 121, 125; 122, comparison_operator:not; 122, 123; 122, 124; 123, identifier:j; 124, identifier:tl; 125, comparison_operator:in; 125, 126; 125, 127; 126, identifier:i; 127, identifier:tl; 128, block; 128, 129; 128, 133; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:outer_team; 132, identifier:t; 133, break_statement; 134, if_statement; 134, 135; 134, 136; 135, identifier:new_team; 136, block; 136, 137; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:teams; 141, identifier:append; 142, argument_list; 142, 143; 143, list:[i, j]; 143, 144; 143, 145; 144, identifier:i; 145, identifier:j; 146, if_statement; 146, 147; 146, 150; 147, comparison_operator:is; 147, 148; 147, 149; 148, identifier:inner_team; 149, False; 150, block; 150, 151; 151, expression_statement; 151, 152; 152, call; 152, 153; 152, 158; 153, attribute; 153, 154; 153, 157; 154, subscript; 154, 155; 154, 156; 155, identifier:teams; 156, identifier:inner_team; 157, identifier:append; 158, argument_list; 158, 159; 159, identifier:i; 160, if_statement; 160, 161; 160, 164; 161, comparison_operator:is; 161, 162; 161, 163; 162, identifier:outer_team; 163, False; 164, block; 164, 165; 165, expression_statement; 165, 166; 166, call; 166, 167; 166, 172; 167, attribute; 167, 168; 167, 171; 168, subscript; 168, 169; 168, 170; 169, identifier:teams; 170, identifier:outer_team; 171, identifier:append; 172, argument_list; 172, 173; 173, identifier:j; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:added; 177, True; 178, if_statement; 178, 179; 178, 185; 179, boolean_operator:and; 179, 180; 179, 182; 180, not_operator; 180, 181; 181, identifier:added; 182, comparison_operator:!=; 182, 183; 182, 184; 183, identifier:j; 184, integer:0; 185, block; 185, 186; 186, expression_statement; 186, 187; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:teams; 190, identifier:append; 191, argument_list; 191, 192; 192, list:[j]; 192, 193; 193, identifier:j; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 201; 196, subscript; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:self; 199, identifier:_cache; 200, string:'teams'; 201, identifier:teams; 202, return_statement; 202, 203; 203, identifier:teams | def get_teams(self):
"""Get teams."""
if self._cache['teams']:
return self._cache['teams']
teams = []
for j, player in enumerate(self._header.initial.players):
added = False
for i in range(0, len(self._header.initial.players)):
if player.attributes.my_diplomacy[i] == 'ally':
inner_team = False
outer_team = False
new_team = True
for t, tl in enumerate(teams):
if j in tl or i in tl:
new_team = False
if j in tl and i not in tl:
inner_team = t
break
if j not in tl and i in tl:
outer_team = t
break
if new_team:
teams.append([i, j])
if inner_team is not False:
teams[inner_team].append(i)
if outer_team is not False:
teams[outer_team].append(j)
added = True
if not added and j != 0:
teams.append([j])
self._cache['teams'] = teams
return teams |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_settings; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 16; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:postgame; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:self; 14, identifier:get_postgame; 15, argument_list; 16, return_statement; 16, 17; 17, dictionary; 17, 18; 17, 35; 17, 56; 17, 67; 17, 84; 17, 110; 17, 119; 17, 128; 17, 143; 17, 158; 17, 173; 17, 182; 17, 190; 17, 198; 18, pair; 18, 19; 18, 20; 19, string:'type'; 20, tuple; 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:_header; 26, identifier:lobby; 27, identifier:game_type_id; 28, attribute; 28, 29; 28, 34; 29, attribute; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:_header; 33, identifier:lobby; 34, identifier:game_type; 35, pair; 35, 36; 35, 37; 36, string:'difficulty'; 37, tuple; 37, 38; 37, 47; 38, attribute; 38, 39; 38, 46; 39, attribute; 39, 40; 39, 45; 40, attribute; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:self; 43, identifier:_header; 44, identifier:scenario; 45, identifier:game_settings; 46, identifier:difficulty_id; 47, attribute; 47, 48; 47, 55; 48, attribute; 48, 49; 48, 54; 49, attribute; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:_header; 53, identifier:scenario; 54, identifier:game_settings; 55, identifier:difficulty; 56, pair; 56, 57; 56, 58; 57, string:'population_limit'; 58, binary_operator:*; 58, 59; 58, 66; 59, attribute; 59, 60; 59, 65; 60, attribute; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:self; 63, identifier:_header; 64, identifier:lobby; 65, identifier:population_limit; 66, integer:25; 67, pair; 67, 68; 67, 69; 68, string:'map_reveal_choice'; 69, tuple; 69, 70; 69, 77; 70, attribute; 70, 71; 70, 76; 71, attribute; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:self; 74, identifier:_header; 75, identifier:lobby; 76, identifier:reveal_map_id; 77, attribute; 77, 78; 77, 83; 78, attribute; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:self; 81, identifier:_header; 82, identifier:lobby; 83, identifier:reveal_map; 84, pair; 84, 85; 84, 86; 85, string:'speed'; 86, tuple; 86, 87; 86, 94; 87, attribute; 87, 88; 87, 93; 88, attribute; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:_header; 92, identifier:replay; 93, identifier:game_speed_id; 94, call; 94, 95; 94, 102; 95, attribute; 95, 96; 95, 101; 96, attribute; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:mgz; 99, identifier:const; 100, identifier:SPEEDS; 101, identifier:get; 102, argument_list; 102, 103; 103, attribute; 103, 104; 103, 109; 104, attribute; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:self; 107, identifier:_header; 108, identifier:replay; 109, identifier:game_speed_id; 110, pair; 110, 111; 110, 112; 111, string:'cheats'; 112, attribute; 112, 113; 112, 118; 113, attribute; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:self; 116, identifier:_header; 117, identifier:replay; 118, identifier:cheats_enabled; 119, pair; 119, 120; 119, 121; 120, string:'lock_teams'; 121, attribute; 121, 122; 121, 127; 122, attribute; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:_header; 126, identifier:lobby; 127, identifier:lock_teams; 128, pair; 128, 129; 128, 130; 129, string:'starting_resources'; 130, tuple; 130, 131; 130, 137; 131, conditional_expression:if; 131, 132; 131, 135; 131, 136; 132, attribute; 132, 133; 132, 134; 133, identifier:postgame; 134, identifier:resource_level_id; 135, identifier:postgame; 136, None; 137, conditional_expression:if; 137, 138; 137, 141; 137, 142; 138, attribute; 138, 139; 138, 140; 139, identifier:postgame; 140, identifier:resource_level; 141, identifier:postgame; 142, None; 143, pair; 143, 144; 143, 145; 144, string:'starting_age'; 145, tuple; 145, 146; 145, 152; 146, conditional_expression:if; 146, 147; 146, 150; 146, 151; 147, attribute; 147, 148; 147, 149; 148, identifier:postgame; 149, identifier:starting_age_id; 150, identifier:postgame; 151, None; 152, conditional_expression:if; 152, 153; 152, 156; 152, 157; 153, attribute; 153, 154; 153, 155; 154, identifier:postgame; 155, identifier:starting_age; 156, identifier:postgame; 157, None; 158, pair; 158, 159; 158, 160; 159, string:'victory_condition'; 160, tuple; 160, 161; 160, 167; 161, conditional_expression:if; 161, 162; 161, 165; 161, 166; 162, attribute; 162, 163; 162, 164; 163, identifier:postgame; 164, identifier:victory_type_id; 165, identifier:postgame; 166, None; 167, conditional_expression:if; 167, 168; 167, 171; 167, 172; 168, attribute; 168, 169; 168, 170; 169, identifier:postgame; 170, identifier:victory_type; 171, identifier:postgame; 172, None; 173, pair; 173, 174; 173, 175; 174, string:'team_together'; 175, conditional_expression:if; 175, 176; 175, 180; 175, 181; 176, not_operator; 176, 177; 177, attribute; 177, 178; 177, 179; 178, identifier:postgame; 179, identifier:team_together; 180, identifier:postgame; 181, None; 182, pair; 182, 183; 182, 184; 183, string:'all_technologies'; 184, conditional_expression:if; 184, 185; 184, 188; 184, 189; 185, attribute; 185, 186; 185, 187; 186, identifier:postgame; 187, identifier:all_techs; 188, identifier:postgame; 189, None; 190, pair; 190, 191; 190, 192; 191, string:'lock_speed'; 192, conditional_expression:if; 192, 193; 192, 196; 192, 197; 193, attribute; 193, 194; 193, 195; 194, identifier:postgame; 195, identifier:lock_speed; 196, identifier:postgame; 197, None; 198, pair; 198, 199; 198, 200; 199, string:'multiqueue'; 200, None | def get_settings(self):
"""Get settings."""
postgame = self.get_postgame()
return {
'type': (
self._header.lobby.game_type_id,
self._header.lobby.game_type
),
'difficulty': (
self._header.scenario.game_settings.difficulty_id,
self._header.scenario.game_settings.difficulty
),
'population_limit': self._header.lobby.population_limit * 25,
'map_reveal_choice': (
self._header.lobby.reveal_map_id,
self._header.lobby.reveal_map
),
'speed': (
self._header.replay.game_speed_id,
mgz.const.SPEEDS.get(self._header.replay.game_speed_id)
),
'cheats': self._header.replay.cheats_enabled,
'lock_teams': self._header.lobby.lock_teams,
'starting_resources': (
postgame.resource_level_id if postgame else None,
postgame.resource_level if postgame else None,
),
'starting_age': (
postgame.starting_age_id if postgame else None,
postgame.starting_age if postgame else None
),
'victory_condition': (
postgame.victory_type_id if postgame else None,
postgame.victory_type if postgame else None
),
'team_together': not postgame.team_together if postgame else None,
'all_technologies': postgame.all_techs if postgame else None,
'lock_speed': postgame.lock_speed if postgame else None,
'multiqueue': None
} |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_map; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 21; 5, 33; 5, 45; 5, 64; 5, 74; 5, 78; 5, 82; 5, 86; 5, 90; 5, 91; 5, 169; 5, 170; 5, 209; 5, 217; 5, 225; 5, 226; 5, 259; 5, 260; 5, 270; 5, 274; 5, 289; 5, 290; 5, 299; 5, 303; 5, 327; 5, 351; 5, 396; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 14; 9, subscript; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:_cache; 13, string:'map'; 14, block; 14, 15; 15, return_statement; 15, 16; 16, subscript; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:_cache; 20, string:'map'; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:map_id; 24, attribute; 24, 25; 24, 32; 25, attribute; 25, 26; 25, 31; 26, attribute; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:_header; 30, identifier:scenario; 31, identifier:game_settings; 32, identifier:map_id; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:instructions; 36, attribute; 36, 37; 36, 44; 37, attribute; 37, 38; 37, 43; 38, attribute; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:_header; 42, identifier:scenario; 43, identifier:messages; 44, identifier:instructions; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:size; 48, call; 48, 49; 48, 56; 49, attribute; 49, 50; 49, 55; 50, attribute; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:mgz; 53, identifier:const; 54, identifier:MAP_SIZES; 55, identifier:get; 56, argument_list; 56, 57; 57, attribute; 57, 58; 57, 63; 58, attribute; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:_header; 62, identifier:map_info; 63, identifier:size_x; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:dimension; 67, attribute; 67, 68; 67, 73; 68, attribute; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:_header; 72, identifier:map_info; 73, identifier:size_x; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:custom; 77, True; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:name; 81, string:'Unknown'; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:language; 85, None; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:encoding; 89, string:'unknown'; 90, comment; 91, for_statement; 91, 92; 91, 93; 91, 94; 92, identifier:pair; 93, identifier:ENCODING_MARKERS; 94, block; 94, 95; 94, 101; 94, 107; 94, 116; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:marker; 98, subscript; 98, 99; 98, 100; 99, identifier:pair; 100, integer:0; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:test_encoding; 104, subscript; 104, 105; 104, 106; 105, identifier:pair; 106, integer:1; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:e_m; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:marker; 113, identifier:encode; 114, argument_list; 114, 115; 115, identifier:test_encoding; 116, for_statement; 116, 117; 116, 118; 116, 124; 117, identifier:line; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:instructions; 121, identifier:split; 122, argument_list; 122, 123; 123, string:b'\n'; 124, block; 124, 125; 124, 134; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:pos; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:line; 131, identifier:find; 132, argument_list; 132, 133; 133, identifier:e_m; 134, if_statement; 134, 135; 134, 139; 135, comparison_operator:>; 135, 136; 135, 137; 136, identifier:pos; 137, unary_operator:-; 137, 138; 138, integer:1; 139, block; 139, 140; 139, 144; 139, 162; 139, 168; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:encoding; 143, identifier:test_encoding; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:name; 147, call; 147, 148; 147, 160; 148, attribute; 148, 149; 148, 159; 149, subscript; 149, 150; 149, 151; 150, identifier:line; 151, slice; 151, 152; 151, 158; 152, binary_operator:+; 152, 153; 152, 154; 153, identifier:pos; 154, call; 154, 155; 154, 156; 155, identifier:len; 156, argument_list; 156, 157; 157, identifier:e_m; 158, colon; 159, identifier:decode; 160, argument_list; 160, 161; 161, identifier:encoding; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:language; 165, subscript; 165, 166; 165, 167; 166, identifier:pair; 167, integer:2; 168, break_statement; 169, comment; 170, if_statement; 170, 171; 170, 173; 171, not_operator; 171, 172; 172, identifier:language; 173, block; 173, 174; 173, 178; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:language; 177, string:'unknown'; 178, for_statement; 178, 179; 178, 180; 178, 181; 179, identifier:pair; 180, identifier:LANGUAGE_MARKERS; 181, block; 181, 182; 182, if_statement; 182, 183; 182, 201; 183, comparison_operator:>; 183, 184; 183, 199; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:instructions; 187, identifier:find; 188, argument_list; 188, 189; 189, call; 189, 190; 189, 195; 190, attribute; 190, 191; 190, 194; 191, subscript; 191, 192; 191, 193; 192, identifier:pair; 193, integer:0; 194, identifier:encode; 195, argument_list; 195, 196; 196, subscript; 196, 197; 196, 198; 197, identifier:pair; 198, integer:1; 199, unary_operator:-; 199, 200; 200, integer:1; 201, block; 201, 202; 201, 208; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 205; 204, identifier:language; 205, subscript; 205, 206; 205, 207; 206, identifier:pair; 207, integer:2; 208, break_statement; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 216; 211, subscript; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:self; 214, identifier:_cache; 215, string:'encoding'; 216, identifier:encoding; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 224; 219, subscript; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:self; 222, identifier:_cache; 223, string:'language'; 224, identifier:language; 225, comment; 226, if_statement; 226, 227; 226, 230; 227, comparison_operator:!=; 227, 228; 227, 229; 228, identifier:map_id; 229, integer:44; 230, block; 230, 231; 230, 245; 230, 255; 231, if_statement; 231, 232; 231, 239; 232, comparison_operator:not; 232, 233; 232, 234; 233, identifier:map_id; 234, attribute; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:mgz; 237, identifier:const; 238, identifier:MAP_NAMES; 239, block; 239, 240; 240, raise_statement; 240, 241; 241, call; 241, 242; 241, 243; 242, identifier:ValueError; 243, argument_list; 243, 244; 244, string:'unspecified builtin map'; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 248; 247, identifier:name; 248, subscript; 248, 249; 248, 254; 249, attribute; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:mgz; 252, identifier:const; 253, identifier:MAP_NAMES; 254, identifier:map_id; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 258; 257, identifier:custom; 258, False; 259, comment; 260, expression_statement; 260, 261; 261, assignment; 261, 262; 261, 263; 262, identifier:match; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:re; 266, identifier:search; 267, argument_list; 267, 268; 267, 269; 268, string:b'\x00.*? (\-?[0-9]+)\x00.*?\.rms'; 269, identifier:instructions; 270, expression_statement; 270, 271; 271, assignment; 271, 272; 271, 273; 272, identifier:seed; 273, None; 274, if_statement; 274, 275; 274, 276; 275, identifier:match; 276, block; 276, 277; 277, expression_statement; 277, 278; 278, assignment; 278, 279; 278, 280; 279, identifier:seed; 280, call; 280, 281; 280, 282; 281, identifier:int; 282, argument_list; 282, 283; 283, call; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:match; 286, identifier:group; 287, argument_list; 287, 288; 288, integer:1; 289, comment; 290, expression_statement; 290, 291; 291, assignment; 291, 292; 291, 293; 292, identifier:has_modes; 293, call; 293, 294; 293, 297; 294, attribute; 294, 295; 294, 296; 295, identifier:name; 296, identifier:find; 297, argument_list; 297, 298; 298, string:': !'; 299, expression_statement; 299, 300; 300, assignment; 300, 301; 300, 302; 301, identifier:mode_string; 302, string:''; 303, if_statement; 303, 304; 303, 308; 304, comparison_operator:>; 304, 305; 304, 306; 305, identifier:has_modes; 306, unary_operator:-; 306, 307; 307, integer:1; 308, block; 308, 309; 308, 319; 309, expression_statement; 309, 310; 310, assignment; 310, 311; 310, 312; 311, identifier:mode_string; 312, subscript; 312, 313; 312, 314; 313, identifier:name; 314, slice; 314, 315; 314, 318; 315, binary_operator:+; 315, 316; 315, 317; 316, identifier:has_modes; 317, integer:3; 318, colon; 319, expression_statement; 319, 320; 320, assignment; 320, 321; 320, 322; 321, identifier:name; 322, subscript; 322, 323; 322, 324; 323, identifier:name; 324, slice; 324, 325; 324, 326; 325, colon; 326, identifier:has_modes; 327, expression_statement; 327, 328; 328, assignment; 328, 329; 328, 330; 329, identifier:modes; 330, dictionary; 330, 331; 330, 336; 330, 341; 330, 346; 331, pair; 331, 332; 331, 333; 332, string:'direct_placement'; 333, comparison_operator:in; 333, 334; 333, 335; 334, string:'P'; 335, identifier:mode_string; 336, pair; 336, 337; 336, 338; 337, string:'effect_quantity'; 338, comparison_operator:in; 338, 339; 338, 340; 339, string:'C'; 340, identifier:mode_string; 341, pair; 341, 342; 341, 343; 342, string:'guard_state'; 343, comparison_operator:in; 343, 344; 343, 345; 344, string:'G'; 345, identifier:mode_string; 346, pair; 346, 347; 346, 348; 347, string:'fixed_positions'; 348, comparison_operator:in; 348, 349; 348, 350; 349, string:'F'; 350, identifier:mode_string; 351, expression_statement; 351, 352; 352, assignment; 352, 353; 352, 358; 353, subscript; 353, 354; 353, 357; 354, attribute; 354, 355; 354, 356; 355, identifier:self; 356, identifier:_cache; 357, string:'map'; 358, dictionary; 358, 359; 358, 366; 358, 373; 358, 376; 358, 379; 358, 382; 358, 385; 358, 388; 359, pair; 359, 360; 359, 361; 360, string:'id'; 361, conditional_expression:if; 361, 362; 361, 363; 361, 365; 362, identifier:map_id; 363, not_operator; 363, 364; 364, identifier:custom; 365, None; 366, pair; 366, 367; 366, 368; 367, string:'name'; 368, call; 368, 369; 368, 372; 369, attribute; 369, 370; 369, 371; 370, identifier:name; 371, identifier:strip; 372, argument_list; 373, pair; 373, 374; 373, 375; 374, string:'size'; 375, identifier:size; 376, pair; 376, 377; 376, 378; 377, string:'dimension'; 378, identifier:dimension; 379, pair; 379, 380; 379, 381; 380, string:'seed'; 381, identifier:seed; 382, pair; 382, 383; 382, 384; 383, string:'modes'; 384, identifier:modes; 385, pair; 385, 386; 385, 387; 386, string:'custom'; 387, identifier:custom; 388, pair; 388, 389; 388, 390; 389, string:'zr'; 390, call; 390, 391; 390, 394; 391, attribute; 391, 392; 391, 393; 392, identifier:name; 393, identifier:startswith; 394, argument_list; 394, 395; 395, string:'ZR@'; 396, return_statement; 396, 397; 397, subscript; 397, 398; 397, 401; 398, attribute; 398, 399; 398, 400; 399, identifier:self; 400, identifier:_cache; 401, string:'map' | def get_map(self):
"""Get the map metadata."""
if self._cache['map']:
return self._cache['map']
map_id = self._header.scenario.game_settings.map_id
instructions = self._header.scenario.messages.instructions
size = mgz.const.MAP_SIZES.get(self._header.map_info.size_x)
dimension = self._header.map_info.size_x
custom = True
name = 'Unknown'
language = None
encoding = 'unknown'
# detect encoding and language
for pair in ENCODING_MARKERS:
marker = pair[0]
test_encoding = pair[1]
e_m = marker.encode(test_encoding)
for line in instructions.split(b'\n'):
pos = line.find(e_m)
if pos > -1:
encoding = test_encoding
name = line[pos+len(e_m):].decode(encoding)
language = pair[2]
break
# disambiguate certain languages
if not language:
language = 'unknown'
for pair in LANGUAGE_MARKERS:
if instructions.find(pair[0].encode(pair[1])) > -1:
language = pair[2]
break
self._cache['encoding'] = encoding
self._cache['language'] = language
# lookup base game map if applicable
if map_id != 44:
if map_id not in mgz.const.MAP_NAMES:
raise ValueError('unspecified builtin map')
name = mgz.const.MAP_NAMES[map_id]
custom = False
# extract map seed
match = re.search(b'\x00.*? (\-?[0-9]+)\x00.*?\.rms', instructions)
seed = None
if match:
seed = int(match.group(1))
# extract userpatch modes
has_modes = name.find(': !')
mode_string = ''
if has_modes > -1:
mode_string = name[has_modes + 3:]
name = name[:has_modes]
modes = {
'direct_placement': 'P' in mode_string,
'effect_quantity': 'C' in mode_string,
'guard_state': 'G' in mode_string,
'fixed_positions': 'F' in mode_string
}
self._cache['map'] = {
'id': map_id if not custom else None,
'name': name.strip(),
'size': size,
'dimension': dimension,
'seed': seed,
'modes': modes,
'custom': custom,
'zr': name.startswith('ZR@')
}
return self._cache['map'] |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:update; 3, parameters; 3, 4; 3, 5; 3, 7; 3, 12; 4, identifier:self; 5, list_splat_pattern; 5, 6; 6, identifier:sources; 7, typed_default_parameter; 7, 8; 7, 9; 7, 11; 8, identifier:follow_symlinks; 9, type; 9, 10; 10, identifier:bool; 11, False; 12, typed_default_parameter; 12, 13; 12, 14; 12, 16; 13, identifier:maximum_depth; 14, type; 14, 15; 15, identifier:int; 16, integer:20; 17, block; 17, 18; 17, 20; 18, expression_statement; 18, 19; 19, comment; 20, for_statement; 20, 21; 20, 22; 20, 23; 21, identifier:source; 22, identifier:sources; 23, block; 23, 24; 23, 62; 23, 63; 23, 70; 24, if_statement; 24, 25; 24, 32; 25, call; 25, 26; 25, 27; 26, identifier:isinstance; 27, argument_list; 27, 28; 27, 29; 28, identifier:source; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:klass; 32, block; 32, 33; 32, 47; 32, 61; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 46; 35, subscript; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:path_map; 39, attribute; 39, 40; 39, 45; 40, attribute; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:source; 43, identifier:this; 44, identifier:name; 45, identifier:value; 46, identifier:source; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 60; 49, subscript; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:class_cache; 53, attribute; 53, 54; 53, 59; 54, attribute; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:source; 57, identifier:this; 58, identifier:name; 59, identifier:value; 60, identifier:source; 61, continue_statement; 62, comment; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:source; 66, call; 66, 67; 66, 68; 67, identifier:str; 68, argument_list; 68, 69; 69, identifier:source; 70, if_statement; 70, 71; 70, 83; 70, 112; 71, call; 71, 72; 71, 79; 72, attribute; 72, 73; 72, 78; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:source; 76, identifier:lower; 77, argument_list; 78, identifier:endswith; 79, argument_list; 79, 80; 80, tuple; 80, 81; 80, 82; 81, string:'.zip'; 82, string:'.jar'; 83, block; 83, 84; 83, 92; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:zf; 87, call; 87, 88; 87, 89; 88, identifier:ZipFile; 89, argument_list; 89, 90; 89, 91; 90, identifier:source; 91, string:'r'; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 99; 94, attribute; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:path_map; 98, identifier:update; 99, argument_list; 99, 100; 100, call; 100, 101; 100, 102; 101, identifier:zip; 102, argument_list; 102, 103; 102, 108; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:zf; 106, identifier:namelist; 107, argument_list; 108, call; 108, 109; 108, 110; 109, identifier:repeat; 110, argument_list; 110, 111; 111, identifier:zf; 112, elif_clause; 112, 113; 112, 121; 113, call; 113, 114; 113, 119; 114, attribute; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:os; 117, identifier:path; 118, identifier:isdir; 119, argument_list; 119, 120; 120, identifier:source; 121, block; 121, 122; 121, 135; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:walker; 125, call; 125, 126; 125, 127; 126, identifier:_walk; 127, argument_list; 127, 128; 127, 129; 127, 132; 128, identifier:source; 129, keyword_argument; 129, 130; 129, 131; 130, identifier:follow_links; 131, identifier:follow_symlinks; 132, keyword_argument; 132, 133; 132, 134; 133, identifier:maximum_depth; 134, identifier:maximum_depth; 135, for_statement; 135, 136; 135, 140; 135, 141; 136, pattern_list; 136, 137; 136, 138; 136, 139; 137, identifier:root; 138, identifier:dirs; 139, identifier:files; 140, identifier:walker; 141, block; 141, 142; 142, for_statement; 142, 143; 142, 144; 142, 145; 143, identifier:file_; 144, identifier:files; 145, block; 145, 146; 145, 158; 145, 170; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:path_full; 149, call; 149, 150; 149, 155; 150, attribute; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:os; 153, identifier:path; 154, identifier:join; 155, argument_list; 155, 156; 155, 157; 156, identifier:root; 157, identifier:file_; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:path_suffix; 161, call; 161, 162; 161, 167; 162, attribute; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:os; 165, identifier:path; 166, identifier:relpath; 167, argument_list; 167, 168; 167, 169; 168, identifier:path_full; 169, identifier:source; 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:path_map; 176, identifier:path_suffix; 177, identifier:path_full | def update(self, *sources, follow_symlinks: bool=False,
maximum_depth: int=20):
"""Add one or more ClassFile sources to the class loader.
If a given source is a directory path, it is traversed up to the
maximum set depth and all files under it are added to the class loader
lookup table.
If a given source is a .jar or .zip file it will be opened and the
file index added to the class loader lookup table.
If a given source is a ClassFile or a subclass, it's immediately
added to the class loader lookup table and the class cache.
:param sources: One or more ClassFile sources to be added.
:param follow_symlinks: True if symlinks should be followed when
traversing filesystem directories.
[default: False]
:param maximum_depth: The maximum sub-directory depth when traversing
filesystem directories. If set to `None` no limit
will be enforced. [default: 20]
"""
for source in sources:
if isinstance(source, self.klass):
self.path_map[source.this.name.value] = source
self.class_cache[source.this.name.value] = source
continue
# Explicit cast to str to support Path objects.
source = str(source)
if source.lower().endswith(('.zip', '.jar')):
zf = ZipFile(source, 'r')
self.path_map.update(zip(zf.namelist(), repeat(zf)))
elif os.path.isdir(source):
walker = _walk(
source,
follow_links=follow_symlinks,
maximum_depth=maximum_depth
)
for root, dirs, files in walker:
for file_ in files:
path_full = os.path.join(root, file_)
path_suffix = os.path.relpath(path_full, source)
self.path_map[path_suffix] = path_full |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:error_view; 3, parameters; 3, 4; 4, default_parameter; 4, 5; 4, 6; 5, identifier:template_dir; 6, None; 7, block; 7, 8; 7, 10; 7, 18; 7, 24; 7, 216; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 13; 11, not_operator; 11, 12; 12, identifier:template_dir; 13, block; 13, 14; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:template_dir; 17, string:"Pylot/Error"; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:template_page; 21, binary_operator:%; 21, 22; 21, 23; 22, string:"%s/index.html"; 23, identifier:template_dir; 24, class_definition; 24, 25; 24, 26; 24, 28; 25, identifier:Error; 26, argument_list; 26, 27; 27, identifier:Pylot; 28, block; 28, 29; 28, 31; 28, 181; 29, expression_statement; 29, 30; 30, comment; 31, decorated_definition; 31, 32; 31, 34; 32, decorator; 32, 33; 33, identifier:classmethod; 34, function_definition; 34, 35; 34, 36; 34, 41; 35, function_name:register; 36, parameters; 36, 37; 36, 38; 36, 39; 37, identifier:cls; 38, identifier:app; 39, dictionary_splat_pattern; 39, 40; 40, identifier:kwargs; 41, block; 41, 42; 41, 55; 41, 76; 41, 97; 41, 118; 41, 139; 41, 160; 42, expression_statement; 42, 43; 43, call; 43, 44; 43, 51; 44, attribute; 44, 45; 44, 50; 45, call; 45, 46; 45, 47; 46, identifier:super; 47, argument_list; 47, 48; 47, 49; 48, identifier:cls; 49, identifier:cls; 50, identifier:register; 51, argument_list; 51, 52; 51, 53; 52, identifier:app; 53, dictionary_splat; 53, 54; 54, identifier:kwargs; 55, decorated_definition; 55, 56; 55, 63; 56, decorator; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:app; 60, identifier:errorhandler; 61, argument_list; 61, 62; 62, integer:400; 63, function_definition; 63, 64; 63, 65; 63, 67; 64, function_name:error_400; 65, parameters; 65, 66; 66, identifier:error; 67, block; 67, 68; 68, return_statement; 68, 69; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:cls; 72, identifier:index; 73, argument_list; 73, 74; 73, 75; 74, identifier:error; 75, integer:400; 76, decorated_definition; 76, 77; 76, 84; 77, decorator; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:app; 81, identifier:errorhandler; 82, argument_list; 82, 83; 83, integer:401; 84, function_definition; 84, 85; 84, 86; 84, 88; 85, function_name:error_401; 86, parameters; 86, 87; 87, identifier:error; 88, block; 88, 89; 89, return_statement; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:cls; 93, identifier:index; 94, argument_list; 94, 95; 94, 96; 95, identifier:error; 96, integer:401; 97, decorated_definition; 97, 98; 97, 105; 98, decorator; 98, 99; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:app; 102, identifier:errorhandler; 103, argument_list; 103, 104; 104, integer:403; 105, function_definition; 105, 106; 105, 107; 105, 109; 106, function_name:error_403; 107, parameters; 107, 108; 108, identifier:error; 109, block; 109, 110; 110, return_statement; 110, 111; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:cls; 114, identifier:index; 115, argument_list; 115, 116; 115, 117; 116, identifier:error; 117, integer:403; 118, decorated_definition; 118, 119; 118, 126; 119, decorator; 119, 120; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:app; 123, identifier:errorhandler; 124, argument_list; 124, 125; 125, integer:404; 126, function_definition; 126, 127; 126, 128; 126, 130; 127, function_name:error_404; 128, parameters; 128, 129; 129, identifier:error; 130, block; 130, 131; 131, return_statement; 131, 132; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:cls; 135, identifier:index; 136, argument_list; 136, 137; 136, 138; 137, identifier:error; 138, integer:404; 139, decorated_definition; 139, 140; 139, 147; 140, decorator; 140, 141; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:app; 144, identifier:errorhandler; 145, argument_list; 145, 146; 146, integer:500; 147, function_definition; 147, 148; 147, 149; 147, 151; 148, function_name:error_500; 149, parameters; 149, 150; 150, identifier:error; 151, block; 151, 152; 152, return_statement; 152, 153; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:cls; 156, identifier:index; 157, argument_list; 157, 158; 157, 159; 158, identifier:error; 159, integer:500; 160, decorated_definition; 160, 161; 160, 168; 161, decorator; 161, 162; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:app; 165, identifier:errorhandler; 166, argument_list; 166, 167; 167, integer:503; 168, function_definition; 168, 169; 168, 170; 168, 172; 169, function_name:error_503; 170, parameters; 170, 171; 171, identifier:error; 172, block; 172, 173; 173, return_statement; 173, 174; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:cls; 177, identifier:index; 178, argument_list; 178, 179; 178, 180; 179, identifier:error; 180, integer:503; 181, decorated_definition; 181, 182; 181, 184; 182, decorator; 182, 183; 183, identifier:classmethod; 184, function_definition; 184, 185; 184, 186; 184, 190; 185, function_name:index; 186, parameters; 186, 187; 186, 188; 186, 189; 187, identifier:cls; 188, identifier:error; 189, identifier:code; 190, block; 190, 191; 190, 202; 191, expression_statement; 191, 192; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:cls; 195, identifier:meta_; 196, argument_list; 196, 197; 197, keyword_argument; 197, 198; 197, 199; 198, identifier:title; 199, binary_operator:%; 199, 200; 199, 201; 200, string:"Error %s"; 201, identifier:code; 202, return_statement; 202, 203; 203, expression_list; 203, 204; 203, 215; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:cls; 207, identifier:render; 208, argument_list; 208, 209; 208, 212; 209, keyword_argument; 209, 210; 209, 211; 210, identifier:error; 211, identifier:error; 212, keyword_argument; 212, 213; 212, 214; 213, identifier:view_template; 214, identifier:template_page; 215, identifier:code; 216, return_statement; 216, 217; 217, identifier:Error | def error_view(template_dir=None):
"""
Create the Error view
Must be instantiated
import error_view
ErrorView = error_view()
:param template_dir: The directory containing the view pages
:return:
"""
if not template_dir:
template_dir = "Pylot/Error"
template_page = "%s/index.html" % template_dir
class Error(Pylot):
"""
Error Views
"""
@classmethod
def register(cls, app, **kwargs):
super(cls, cls).register(app, **kwargs)
@app.errorhandler(400)
def error_400(error):
return cls.index(error, 400)
@app.errorhandler(401)
def error_401(error):
return cls.index(error, 401)
@app.errorhandler(403)
def error_403(error):
return cls.index(error, 403)
@app.errorhandler(404)
def error_404(error):
return cls.index(error, 404)
@app.errorhandler(500)
def error_500(error):
return cls.index(error, 500)
@app.errorhandler(503)
def error_503(error):
return cls.index(error, 503)
@classmethod
def index(cls, error, code):
cls.meta_(title="Error %s" % code)
return cls.render(error=error, view_template=template_page), code
return Error |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:update_device_list; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:sessions; 6, block; 6, 7; 6, 9; 6, 22; 6, 26; 6, 30; 6, 34; 6, 223; 6, 224; 6, 270; 6, 271; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 13; 10, comparison_operator:is; 10, 11; 10, 12; 11, identifier:sessions; 12, None; 13, block; 13, 14; 13, 21; 14, expression_statement; 14, 15; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:_LOGGER; 18, identifier:error; 19, argument_list; 19, 20; 20, string:'Error updating Emby devices.'; 21, return_statement; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:new_devices; 25, list:[]; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:active_devices; 29, list:[]; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:dev_update; 33, False; 34, for_statement; 34, 35; 34, 36; 34, 37; 35, identifier:device; 36, identifier:sessions; 37, block; 37, 38; 37, 52; 37, 76; 37, 83; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:dev_name; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, string:'{}.{}'; 44, identifier:format; 45, argument_list; 45, 46; 45, 49; 46, subscript; 46, 47; 46, 48; 47, identifier:device; 48, string:'DeviceId'; 49, subscript; 49, 50; 49, 51; 50, identifier:device; 51, string:'Client'; 52, try_statement; 52, 53; 52, 72; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:_LOGGER; 58, identifier:debug; 59, argument_list; 59, 60; 59, 61; 59, 62; 59, 67; 60, string:'Session msg on %s of type: %s, themeflag: %s'; 61, identifier:dev_name; 62, subscript; 62, 63; 62, 66; 63, subscript; 63, 64; 63, 65; 64, identifier:device; 65, string:'NowPlayingItem'; 66, string:'Type'; 67, subscript; 67, 68; 67, 71; 68, subscript; 68, 69; 68, 70; 69, identifier:device; 70, string:'NowPlayingItem'; 71, string:'IsThemeMedia'; 72, except_clause; 72, 73; 72, 74; 73, identifier:KeyError; 74, block; 74, 75; 75, pass_statement; 76, expression_statement; 76, 77; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:active_devices; 80, identifier:append; 81, argument_list; 81, 82; 82, identifier:dev_name; 83, if_statement; 83, 84; 83, 101; 83, 133; 84, boolean_operator:and; 84, 85; 84, 90; 84, 91; 85, comparison_operator:not; 85, 86; 85, 87; 86, identifier:dev_name; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:_devices; 90, line_continuation:\; 91, comparison_operator:!=; 91, 92; 91, 95; 92, subscript; 92, 93; 92, 94; 93, identifier:device; 94, string:'DeviceId'; 95, call; 95, 96; 95, 97; 96, identifier:str; 97, argument_list; 97, 98; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:_api_id; 101, block; 101, 102; 101, 110; 101, 118; 101, 126; 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; 107, 109; 108, string:'New Emby DeviceID: %s. Adding to device list.'; 109, identifier:dev_name; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:new; 113, call; 113, 114; 113, 115; 114, identifier:EmbyDevice; 115, argument_list; 115, 116; 115, 117; 116, identifier:device; 117, identifier:self; 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:self; 123, identifier:_devices; 124, identifier:dev_name; 125, identifier:new; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:new_devices; 130, identifier:append; 131, argument_list; 131, 132; 132, identifier:new; 133, elif_clause; 133, 134; 133, 144; 133, 145; 133, 146; 134, comparison_operator:!=; 134, 135; 134, 138; 135, subscript; 135, 136; 135, 137; 136, identifier:device; 137, string:'DeviceId'; 138, call; 138, 139; 138, 140; 139, identifier:str; 140, argument_list; 140, 141; 141, attribute; 141, 142; 141, 143; 142, identifier:self; 143, identifier:_api_id; 144, comment; 145, comment; 146, block; 146, 147; 146, 163; 146, 177; 146, 188; 146, 199; 146, 213; 147, if_statement; 147, 148; 147, 156; 147, 157; 147, 158; 148, not_operator; 148, 149; 149, attribute; 149, 150; 149, 155; 150, subscript; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:self; 153, identifier:_devices; 154, identifier:dev_name; 155, identifier:is_active; 156, comment; 157, comment; 158, block; 158, 159; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:dev_update; 162, True; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:do_update; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:self; 169, identifier:update_check; 170, argument_list; 170, 171; 170, 176; 171, subscript; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:self; 174, identifier:_devices; 175, identifier:dev_name; 176, identifier:device; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 186; 179, attribute; 179, 180; 179, 185; 180, subscript; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:self; 183, identifier:_devices; 184, identifier:dev_name; 185, identifier:update_data; 186, argument_list; 186, 187; 187, identifier:device; 188, expression_statement; 188, 189; 189, call; 189, 190; 189, 197; 190, attribute; 190, 191; 190, 196; 191, subscript; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:self; 194, identifier:_devices; 195, identifier:dev_name; 196, identifier:set_active; 197, argument_list; 197, 198; 198, True; 199, if_statement; 199, 200; 199, 201; 200, identifier:dev_update; 201, block; 201, 202; 201, 209; 202, expression_statement; 202, 203; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:self; 206, identifier:_do_new_devices_callback; 207, argument_list; 207, 208; 208, integer:0; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:dev_update; 212, False; 213, if_statement; 213, 214; 213, 215; 214, identifier:do_update; 215, block; 215, 216; 216, expression_statement; 216, 217; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:self; 220, identifier:_do_update_callback; 221, argument_list; 221, 222; 222, identifier:dev_name; 223, comment; 224, for_statement; 224, 225; 224, 226; 224, 229; 225, identifier:dev_id; 226, attribute; 226, 227; 226, 228; 227, identifier:self; 228, identifier:_devices; 229, block; 229, 230; 230, if_statement; 230, 231; 230, 234; 230, 235; 231, comparison_operator:not; 231, 232; 231, 233; 232, identifier:dev_id; 233, identifier:active_devices; 234, comment; 235, block; 235, 236; 236, if_statement; 236, 237; 236, 244; 237, attribute; 237, 238; 237, 243; 238, subscript; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:self; 241, identifier:_devices; 242, identifier:dev_id; 243, identifier:is_active; 244, block; 244, 245; 244, 256; 244, 263; 245, expression_statement; 245, 246; 246, call; 246, 247; 246, 254; 247, attribute; 247, 248; 247, 253; 248, subscript; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:self; 251, identifier:_devices; 252, identifier:dev_id; 253, identifier:set_active; 254, argument_list; 254, 255; 255, False; 256, expression_statement; 256, 257; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:self; 260, identifier:_do_update_callback; 261, argument_list; 261, 262; 262, identifier:dev_id; 263, expression_statement; 263, 264; 264, call; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:self; 267, identifier:_do_stale_devices_callback; 268, argument_list; 268, 269; 269, identifier:dev_id; 270, comment; 271, if_statement; 271, 272; 271, 273; 272, identifier:new_devices; 273, block; 273, 274; 274, expression_statement; 274, 275; 275, call; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:self; 278, identifier:_do_new_devices_callback; 279, argument_list; 279, 280; 280, integer:0 | def update_device_list(self, sessions):
""" Update device list. """
if sessions is None:
_LOGGER.error('Error updating Emby devices.')
return
new_devices = []
active_devices = []
dev_update = False
for device in sessions:
dev_name = '{}.{}'.format(device['DeviceId'], device['Client'])
try:
_LOGGER.debug('Session msg on %s of type: %s, themeflag: %s',
dev_name, device['NowPlayingItem']['Type'],
device['NowPlayingItem']['IsThemeMedia'])
except KeyError:
pass
active_devices.append(dev_name)
if dev_name not in self._devices and \
device['DeviceId'] != str(self._api_id):
_LOGGER.debug('New Emby DeviceID: %s. Adding to device list.',
dev_name)
new = EmbyDevice(device, self)
self._devices[dev_name] = new
new_devices.append(new)
elif device['DeviceId'] != str(self._api_id):
# Before we send in new data check for changes to state
# to decide if we need to fire the update callback
if not self._devices[dev_name].is_active:
# Device wasn't active on the last update
# We need to fire a device callback to let subs now
dev_update = True
do_update = self.update_check(
self._devices[dev_name], device)
self._devices[dev_name].update_data(device)
self._devices[dev_name].set_active(True)
if dev_update:
self._do_new_devices_callback(0)
dev_update = False
if do_update:
self._do_update_callback(dev_name)
# Need to check for new inactive devices and flag
for dev_id in self._devices:
if dev_id not in active_devices:
# Device no longer active
if self._devices[dev_id].is_active:
self._devices[dev_id].set_active(False)
self._do_update_callback(dev_id)
self._do_stale_devices_callback(dev_id)
# Call device callback if new devices were found.
if new_devices:
self._do_new_devices_callback(0) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 4; 2, function_name:main; 3, parameters; 4, block; 4, 5; 4, 7; 4, 13; 4, 21; 4, 22; 4, 28; 4, 36; 4, 46; 4, 56; 4, 146; 4, 187; 4, 188; 4, 201; 4, 202; 4, 208; 4, 304; 4, 305; 4, 336; 4, 347; 4, 348; 4, 363; 4, 364; 5, expression_statement; 5, 6; 6, comment; 7, expression_statement; 7, 8; 8, assignment; 8, 9; 8, 10; 9, identifier:parser; 10, call; 10, 11; 10, 12; 11, identifier:get_parser; 12, argument_list; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:args; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:parser; 19, identifier:parse_args; 20, argument_list; 21, comment; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:ARCHIVE; 25, attribute; 25, 26; 25, 27; 26, identifier:args; 27, identifier:archive_path; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:archive; 31, parenthesized_expression; 31, 32; 32, not_operator; 32, 33; 33, attribute; 33, 34; 33, 35; 34, identifier:args; 35, identifier:no_archive; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 43; 38, subscript; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:os; 41, identifier:environ; 42, string:'F2FORMAT_VERSION'; 43, attribute; 43, 44; 43, 45; 44, identifier:args; 45, identifier:python; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 53; 48, subscript; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:os; 51, identifier:environ; 52, string:'F2FORMAT_ENCODING'; 53, attribute; 53, 54; 53, 55; 54, identifier:args; 55, identifier:encoding; 56, function_definition; 56, 57; 56, 58; 56, 60; 57, function_name:find; 58, parameters; 58, 59; 59, identifier:root; 60, block; 60, 61; 60, 63; 60, 69; 60, 78; 60, 143; 61, expression_statement; 61, 62; 62, comment; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:flst; 66, call; 66, 67; 66, 68; 67, identifier:list; 68, argument_list; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:temp; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:os; 75, identifier:listdir; 76, argument_list; 76, 77; 77, identifier:root; 78, for_statement; 78, 79; 78, 80; 78, 81; 79, identifier:file; 80, identifier:temp; 81, block; 81, 82; 81, 94; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:path; 85, call; 85, 86; 85, 91; 86, attribute; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:os; 89, identifier:path; 90, identifier:join; 91, argument_list; 91, 92; 91, 93; 92, identifier:root; 93, identifier:file; 94, if_statement; 94, 95; 94, 103; 94, 114; 94, 131; 95, call; 95, 96; 95, 101; 96, attribute; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:os; 99, identifier:path; 100, identifier:isdir; 101, argument_list; 101, 102; 102, identifier:path; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:flst; 108, identifier:extend; 109, argument_list; 109, 110; 110, call; 110, 111; 110, 112; 111, identifier:find; 112, argument_list; 112, 113; 113, identifier:path; 114, elif_clause; 114, 115; 114, 123; 115, call; 115, 116; 115, 121; 116, attribute; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:os; 119, identifier:path; 120, identifier:isfile; 121, argument_list; 121, 122; 122, identifier:path; 123, block; 123, 124; 124, expression_statement; 124, 125; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:flst; 128, identifier:append; 129, argument_list; 129, 130; 130, identifier:path; 131, elif_clause; 131, 132; 131, 140; 131, 141; 132, call; 132, 133; 132, 138; 133, attribute; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:os; 136, identifier:path; 137, identifier:islink; 138, argument_list; 138, 139; 139, identifier:path; 140, comment; 141, block; 141, 142; 142, continue_statement; 143, expression_statement; 143, 144; 144, yield; 144, 145; 145, identifier:flst; 146, function_definition; 146, 147; 146, 148; 146, 150; 147, function_name:rename; 148, parameters; 148, 149; 149, identifier:path; 150, block; 150, 151; 150, 164; 150, 177; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 156; 153, pattern_list; 153, 154; 153, 155; 154, identifier:stem; 155, identifier:ext; 156, call; 156, 157; 156, 162; 157, attribute; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:os; 160, identifier:path; 161, identifier:splitext; 162, argument_list; 162, 163; 163, identifier:path; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:name; 167, binary_operator:%; 167, 168; 167, 169; 168, string:'%s-%s%s'; 169, tuple; 169, 170; 169, 171; 169, 176; 170, identifier:stem; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:uuid; 174, identifier:uuid4; 175, argument_list; 176, identifier:ext; 177, return_statement; 177, 178; 178, call; 178, 179; 178, 184; 179, attribute; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:os; 182, identifier:path; 183, identifier:join; 184, argument_list; 184, 185; 184, 186; 185, identifier:ARCHIVE; 186, identifier:name; 187, comment; 188, if_statement; 188, 189; 188, 190; 189, identifier:archive; 190, block; 190, 191; 191, expression_statement; 191, 192; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:os; 195, identifier:makedirs; 196, argument_list; 196, 197; 196, 198; 197, identifier:ARCHIVE; 198, keyword_argument; 198, 199; 198, 200; 199, identifier:exist_ok; 200, True; 201, comment; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 205; 204, identifier:filelist; 205, call; 205, 206; 205, 207; 206, identifier:list; 207, argument_list; 208, for_statement; 208, 209; 208, 210; 208, 217; 209, identifier:path; 210, subscript; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:sys; 213, identifier:argv; 214, slice; 214, 215; 214, 216; 215, integer:1; 216, colon; 217, block; 217, 218; 217, 270; 218, if_statement; 218, 219; 218, 227; 219, call; 219, 220; 219, 225; 220, attribute; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:os; 223, identifier:path; 224, identifier:isfile; 225, argument_list; 225, 226; 226, identifier:path; 227, block; 227, 228; 227, 263; 228, if_statement; 228, 229; 228, 230; 229, identifier:archive; 230, block; 230, 231; 230, 238; 230, 255; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:dest; 234, call; 234, 235; 234, 236; 235, identifier:rename; 236, argument_list; 236, 237; 237, identifier:path; 238, expression_statement; 238, 239; 239, call; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:os; 242, identifier:makedirs; 243, argument_list; 243, 244; 243, 252; 244, call; 244, 245; 244, 250; 245, attribute; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:os; 248, identifier:path; 249, identifier:dirname; 250, argument_list; 250, 251; 251, identifier:dest; 252, keyword_argument; 252, 253; 252, 254; 253, identifier:exist_ok; 254, True; 255, expression_statement; 255, 256; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:shutil; 259, identifier:copy; 260, argument_list; 260, 261; 260, 262; 261, identifier:path; 262, identifier:dest; 263, expression_statement; 263, 264; 264, call; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:filelist; 267, identifier:append; 268, argument_list; 268, 269; 269, identifier:path; 270, if_statement; 270, 271; 270, 279; 271, call; 271, 272; 271, 277; 272, attribute; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:os; 275, identifier:path; 276, identifier:isdir; 277, argument_list; 277, 278; 278, identifier:path; 279, block; 279, 280; 279, 294; 280, if_statement; 280, 281; 280, 282; 281, identifier:archive; 282, block; 282, 283; 283, expression_statement; 283, 284; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:shutil; 287, identifier:copytree; 288, argument_list; 288, 289; 288, 290; 289, identifier:path; 290, call; 290, 291; 290, 292; 291, identifier:rename; 292, argument_list; 292, 293; 293, identifier:path; 294, expression_statement; 294, 295; 295, call; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:filelist; 298, identifier:extend; 299, argument_list; 299, 300; 300, call; 300, 301; 300, 302; 301, identifier:find; 302, argument_list; 302, 303; 303, identifier:path; 304, comment; 305, function_definition; 305, 306; 305, 307; 305, 309; 306, function_name:ispy; 307, parameters; 307, 308; 308, identifier:file; 309, block; 309, 310; 310, return_statement; 310, 311; 311, parenthesized_expression; 311, 312; 312, boolean_operator:and; 312, 313; 312, 321; 313, call; 313, 314; 313, 319; 314, attribute; 314, 315; 314, 318; 315, attribute; 315, 316; 315, 317; 316, identifier:os; 317, identifier:path; 318, identifier:isfile; 319, argument_list; 319, 320; 320, identifier:file; 321, parenthesized_expression; 321, 322; 322, comparison_operator:in; 322, 323; 322, 333; 323, subscript; 323, 324; 323, 332; 324, call; 324, 325; 324, 330; 325, attribute; 325, 326; 325, 329; 326, attribute; 326, 327; 326, 328; 327, identifier:os; 328, identifier:path; 329, identifier:splitext; 330, argument_list; 330, 331; 331, identifier:file; 332, integer:1; 333, tuple; 333, 334; 333, 335; 334, string:'.py'; 335, string:'.pyw'; 336, expression_statement; 336, 337; 337, assignment; 337, 338; 337, 339; 338, identifier:filelist; 339, call; 339, 340; 339, 341; 340, identifier:sorted; 341, argument_list; 341, 342; 342, call; 342, 343; 342, 344; 343, identifier:filter; 344, argument_list; 344, 345; 344, 346; 345, identifier:ispy; 346, identifier:filelist; 347, comment; 348, if_statement; 348, 349; 348, 355; 349, comparison_operator:==; 349, 350; 349, 354; 350, call; 350, 351; 350, 352; 351, identifier:len; 352, argument_list; 352, 353; 353, identifier:filelist; 354, integer:0; 355, block; 355, 356; 356, expression_statement; 356, 357; 357, call; 357, 358; 357, 361; 358, attribute; 358, 359; 358, 360; 359, identifier:parser; 360, identifier:error; 361, argument_list; 361, 362; 362, string:'argument PATH: no valid source file found'; 363, comment; 364, if_statement; 364, 365; 364, 372; 364, 382; 365, boolean_operator:or; 365, 366; 365, 369; 366, comparison_operator:is; 366, 367; 366, 368; 367, identifier:mp; 368, None; 369, comparison_operator:<=; 369, 370; 369, 371; 370, identifier:CPU_CNT; 371, integer:1; 372, block; 372, 373; 373, expression_statement; 373, 374; 374, list_comprehension; 374, 375; 374, 379; 375, call; 375, 376; 375, 377; 376, identifier:f2format; 377, argument_list; 377, 378; 378, identifier:filename; 379, for_in_clause; 379, 380; 379, 381; 380, identifier:filename; 381, identifier:filelist; 382, else_clause; 382, 383; 383, block; 383, 384; 384, expression_statement; 384, 385; 385, call; 385, 386; 385, 396; 386, attribute; 386, 387; 386, 395; 387, call; 387, 388; 387, 391; 388, attribute; 388, 389; 388, 390; 389, identifier:mp; 390, identifier:Pool; 391, argument_list; 391, 392; 392, keyword_argument; 392, 393; 392, 394; 393, identifier:processes; 394, identifier:CPU_CNT; 395, identifier:map; 396, argument_list; 396, 397; 396, 398; 397, identifier:f2format; 398, identifier:filelist | def main():
"""Entry point for f2format."""
parser = get_parser()
args = parser.parse_args()
# set up variables
ARCHIVE = args.archive_path
archive = (not args.no_archive)
os.environ['F2FORMAT_VERSION'] = args.python
os.environ['F2FORMAT_ENCODING'] = args.encoding
def find(root):
"""Recursively find all files under root."""
flst = list()
temp = os.listdir(root)
for file in temp:
path = os.path.join(root, file)
if os.path.isdir(path):
flst.extend(find(path))
elif os.path.isfile(path):
flst.append(path)
elif os.path.islink(path): # exclude symbolic links
continue
yield from flst
def rename(path):
stem, ext = os.path.splitext(path)
name = '%s-%s%s' % (stem, uuid.uuid4(), ext)
return os.path.join(ARCHIVE, name)
# make archive directory
if archive:
os.makedirs(ARCHIVE, exist_ok=True)
# fetch file list
filelist = list()
for path in sys.argv[1:]:
if os.path.isfile(path):
if archive:
dest = rename(path)
os.makedirs(os.path.dirname(dest), exist_ok=True)
shutil.copy(path, dest)
filelist.append(path)
if os.path.isdir(path):
if archive:
shutil.copytree(path, rename(path))
filelist.extend(find(path))
# check if file is Python source code
def ispy(file): return (os.path.isfile(file) and (os.path.splitext(file)[1] in ('.py', '.pyw')))
filelist = sorted(filter(ispy, filelist))
# if no file supplied
if len(filelist) == 0:
parser.error('argument PATH: no valid source file found')
# process files
if mp is None or CPU_CNT <= 1:
[f2format(filename) for filename in filelist]
else:
mp.Pool(processes=CPU_CNT).map(f2format, filelist) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:random_useragent; 3, parameters; 3, 4; 4, default_parameter; 4, 5; 4, 6; 5, identifier:filename; 6, True; 7, block; 7, 8; 7, 10; 7, 13; 7, 19; 7, 37; 7, 49; 7, 92; 7, 214; 8, expression_statement; 8, 9; 9, comment; 10, import_statement; 10, 11; 11, dotted_name; 11, 12; 12, identifier:random; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:default_ua; 16, binary_operator:%; 16, 17; 16, 18; 17, string:'urlfetch/%s'; 18, identifier:__version__; 19, if_statement; 19, 20; 19, 25; 19, 31; 20, call; 20, 21; 20, 22; 21, identifier:isinstance; 22, argument_list; 22, 23; 22, 24; 23, identifier:filename; 24, identifier:basestring; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:filenames; 29, list:[filename]; 29, 30; 30, identifier:filename; 31, else_clause; 31, 32; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:filenames; 36, list:[]; 37, if_statement; 37, 38; 37, 41; 38, boolean_operator:and; 38, 39; 38, 40; 39, identifier:filename; 40, identifier:UAFILE; 41, block; 41, 42; 42, expression_statement; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:filenames; 46, identifier:append; 47, argument_list; 47, 48; 48, identifier:UAFILE; 49, for_statement; 49, 50; 49, 51; 49, 52; 49, 88; 50, identifier:filename; 51, identifier:filenames; 52, block; 52, 53; 53, try_statement; 53, 54; 53, 85; 54, block; 54, 55; 54, 64; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:st; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:os; 61, identifier:stat; 62, argument_list; 62, 63; 63, identifier:filename; 64, if_statement; 64, 65; 64, 83; 65, boolean_operator:and; 65, 66; 65, 74; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:stat; 69, identifier:S_ISREG; 70, argument_list; 70, 71; 71, attribute; 71, 72; 71, 73; 72, identifier:st; 73, identifier:st_mode; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:os; 77, identifier:access; 78, argument_list; 78, 79; 78, 80; 79, identifier:filename; 80, attribute; 80, 81; 80, 82; 81, identifier:os; 82, identifier:R_OK; 83, block; 83, 84; 84, break_statement; 85, except_clause; 85, 86; 86, block; 86, 87; 87, pass_statement; 88, else_clause; 88, 89; 89, block; 89, 90; 90, return_statement; 90, 91; 91, identifier:default_ua; 92, with_statement; 92, 93; 92, 103; 93, with_clause; 93, 94; 94, with_item; 94, 95; 95, as_pattern; 95, 96; 95, 101; 96, call; 96, 97; 96, 98; 97, identifier:open; 98, argument_list; 98, 99; 98, 100; 99, identifier:filename; 100, string:'rb'; 101, as_pattern_target; 101, 102; 102, identifier:f; 103, block; 103, 104; 103, 110; 103, 114; 103, 122; 103, 123; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:filesize; 107, attribute; 107, 108; 107, 109; 108, identifier:st; 109, identifier:st_size; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:pos; 113, integer:0; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:r; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:random; 120, identifier:Random; 121, argument_list; 122, comment; 123, for_statement; 123, 124; 123, 125; 123, 129; 124, identifier:i; 125, call; 125, 126; 125, 127; 126, identifier:range; 127, argument_list; 127, 128; 128, integer:3; 129, block; 129, 130; 129, 140; 129, 144; 129, 151; 129, 152; 129, 158; 129, 166; 129, 195; 129, 203; 130, expression_statement; 130, 131; 131, augmented_assignment:+=; 131, 132; 131, 133; 132, identifier:pos; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:r; 136, identifier:randint; 137, argument_list; 137, 138; 137, 139; 138, integer:0; 139, identifier:filesize; 140, expression_statement; 140, 141; 141, augmented_assignment:%=; 141, 142; 141, 143; 142, identifier:pos; 143, identifier:filesize; 144, expression_statement; 144, 145; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:f; 148, identifier:seek; 149, argument_list; 149, 150; 150, identifier:pos; 151, comment; 152, expression_statement; 152, 153; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:f; 156, identifier:readline; 157, argument_list; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:line; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:f; 164, identifier:readline; 165, argument_list; 166, if_statement; 166, 167; 166, 169; 167, not_operator; 167, 168; 168, identifier:line; 169, block; 169, 170; 170, if_statement; 170, 171; 170, 178; 170, 179; 171, comparison_operator:==; 171, 172; 171, 177; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:f; 175, identifier:tell; 176, argument_list; 177, identifier:filesize; 178, comment; 179, block; 179, 180; 179, 187; 180, expression_statement; 180, 181; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:f; 184, identifier:seek; 185, argument_list; 185, 186; 186, integer:0; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:line; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:f; 193, identifier:readline; 194, argument_list; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:line; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:line; 201, identifier:strip; 202, argument_list; 203, if_statement; 203, 204; 203, 211; 204, boolean_operator:and; 204, 205; 204, 206; 205, identifier:line; 206, comparison_operator:!=; 206, 207; 206, 210; 207, subscript; 207, 208; 207, 209; 208, identifier:line; 209, integer:0; 210, string:'#'; 211, block; 211, 212; 212, return_statement; 212, 213; 213, identifier:line; 214, return_statement; 214, 215; 215, identifier:default_ua | def random_useragent(filename=True):
"""Returns a User-Agent string randomly from file.
:arg string filename: (Optional) Path to the file from which a random
useragent is generated. By default it's ``True``, a file shipped
with this module will be used.
:returns: An user-agent string.
"""
import random
default_ua = 'urlfetch/%s' % __version__
if isinstance(filename, basestring):
filenames = [filename]
else:
filenames = []
if filename and UAFILE:
filenames.append(UAFILE)
for filename in filenames:
try:
st = os.stat(filename)
if stat.S_ISREG(st.st_mode) and os.access(filename, os.R_OK):
break
except:
pass
else:
return default_ua
with open(filename, 'rb') as f:
filesize = st.st_size
pos = 0
r = random.Random()
# try getting a valid line for no more than 3 times
for i in range(3):
pos += r.randint(0, filesize)
pos %= filesize
f.seek(pos)
# in case we are in middle of a line
f.readline()
line = f.readline()
if not line:
if f.tell() == filesize:
# end of file
f.seek(0)
line = f.readline()
line = line.strip()
if line and line[0] != '#':
return line
return default_ua |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:encode_multipart; 3, parameters; 3, 4; 3, 5; 4, identifier:data; 5, identifier:files; 6, block; 6, 7; 6, 9; 6, 15; 6, 21; 6, 30; 6, 41; 6, 156; 6, 332; 6, 346; 6, 352; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:body; 12, call; 12, 13; 12, 14; 13, identifier:BytesIO; 14, argument_list; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:boundary; 18, call; 18, 19; 18, 20; 19, identifier:choose_boundary; 20, argument_list; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:part_boundary; 24, call; 24, 25; 24, 26; 25, identifier:b; 26, argument_list; 26, 27; 27, binary_operator:%; 27, 28; 27, 29; 28, string:'--%s\r\n'; 29, identifier:boundary; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:writer; 33, subscript; 33, 34; 33, 40; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:codecs; 37, identifier:lookup; 38, argument_list; 38, 39; 39, string:'utf-8'; 40, integer:3; 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:data; 46, identifier:dict; 47, block; 47, 48; 48, for_statement; 48, 49; 48, 52; 48, 57; 49, pattern_list; 49, 50; 49, 51; 50, identifier:name; 51, identifier:values; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:data; 55, identifier:items; 56, argument_list; 57, block; 57, 58; 57, 75; 58, if_statement; 58, 59; 58, 68; 58, 69; 59, not_operator; 59, 60; 60, call; 60, 61; 60, 62; 61, identifier:isinstance; 62, argument_list; 62, 63; 62, 64; 63, identifier:values; 64, tuple; 64, 65; 64, 66; 64, 67; 65, identifier:list; 66, identifier:tuple; 67, identifier:set; 68, comment; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:values; 73, tuple; 73, 74; 74, identifier:values; 75, for_statement; 75, 76; 75, 77; 75, 78; 76, identifier:value; 77, identifier:values; 78, block; 78, 79; 78, 86; 78, 100; 78, 107; 78, 121; 78, 149; 79, expression_statement; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:body; 83, identifier:write; 84, argument_list; 84, 85; 85, identifier:part_boundary; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 94; 88, attribute; 88, 89; 88, 93; 89, call; 89, 90; 89, 91; 90, identifier:writer; 91, argument_list; 91, 92; 92, identifier:body; 93, identifier:write; 94, argument_list; 94, 95; 95, binary_operator:%; 95, 96; 95, 99; 96, concatenated_string; 96, 97; 96, 98; 97, string:'Content-Disposition: form-data; '; 98, string:'name="%s"\r\n'; 99, identifier:name; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:body; 104, identifier:write; 105, argument_list; 105, 106; 106, string:b'Content-Type: text/plain\r\n\r\n'; 107, if_statement; 107, 108; 107, 113; 108, call; 108, 109; 108, 110; 109, identifier:isinstance; 110, argument_list; 110, 111; 110, 112; 111, identifier:value; 112, identifier:int; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:value; 117, call; 117, 118; 117, 119; 118, identifier:str; 119, argument_list; 119, 120; 120, identifier:value; 121, if_statement; 121, 122; 121, 129; 121, 140; 122, boolean_operator:and; 122, 123; 122, 124; 123, identifier:py3k; 124, call; 124, 125; 124, 126; 125, identifier:isinstance; 126, argument_list; 126, 127; 126, 128; 127, identifier:value; 128, identifier:str; 129, block; 129, 130; 130, expression_statement; 130, 131; 131, call; 131, 132; 131, 138; 132, attribute; 132, 133; 132, 137; 133, call; 133, 134; 133, 135; 134, identifier:writer; 135, argument_list; 135, 136; 136, identifier:body; 137, identifier:write; 138, argument_list; 138, 139; 139, identifier:value; 140, else_clause; 140, 141; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:body; 146, identifier:write; 147, argument_list; 147, 148; 148, identifier:value; 149, expression_statement; 149, 150; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:body; 153, identifier:write; 154, argument_list; 154, 155; 155, string:b'\r\n'; 156, for_statement; 156, 157; 156, 160; 156, 165; 157, pattern_list; 157, 158; 157, 159; 158, identifier:fieldname; 159, identifier:f; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:files; 163, identifier:items; 164, argument_list; 165, block; 165, 166; 165, 206; 165, 241; 165, 248; 165, 297; 165, 325; 166, if_statement; 166, 167; 166, 172; 166, 179; 166, 195; 167, call; 167, 168; 167, 169; 168, identifier:isinstance; 169, argument_list; 169, 170; 169, 171; 170, identifier:f; 171, identifier:tuple; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 178; 175, pattern_list; 175, 176; 175, 177; 176, identifier:filename; 177, identifier:f; 178, identifier:f; 179, elif_clause; 179, 180; 179, 185; 180, call; 180, 181; 180, 182; 181, identifier:hasattr; 182, argument_list; 182, 183; 182, 184; 183, identifier:f; 184, string:'name'; 185, block; 185, 186; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:filename; 189, call; 189, 190; 189, 191; 190, identifier:basename; 191, argument_list; 191, 192; 192, attribute; 192, 193; 192, 194; 193, identifier:f; 194, identifier:name; 195, else_clause; 195, 196; 196, block; 196, 197; 196, 201; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:filename; 200, None; 201, raise_statement; 201, 202; 202, call; 202, 203; 202, 204; 203, identifier:UrlfetchException; 204, argument_list; 204, 205; 205, string:"file must has filename"; 206, if_statement; 206, 207; 206, 212; 206, 221; 206, 232; 207, call; 207, 208; 207, 209; 208, identifier:hasattr; 209, argument_list; 209, 210; 209, 211; 210, identifier:f; 211, string:'read'; 212, block; 212, 213; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 216; 215, identifier:value; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:f; 219, identifier:read; 220, argument_list; 221, elif_clause; 221, 222; 221, 227; 222, call; 222, 223; 222, 224; 223, identifier:isinstance; 224, argument_list; 224, 225; 224, 226; 225, identifier:f; 226, identifier:basestring; 227, block; 227, 228; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:value; 231, identifier:f; 232, else_clause; 232, 233; 233, block; 233, 234; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 237; 236, identifier:value; 237, call; 237, 238; 237, 239; 238, identifier:str; 239, argument_list; 239, 240; 240, identifier:f; 241, expression_statement; 241, 242; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:body; 245, identifier:write; 246, argument_list; 246, 247; 247, identifier:part_boundary; 248, if_statement; 248, 249; 248, 250; 248, 274; 249, identifier:filename; 250, block; 250, 251; 250, 267; 251, expression_statement; 251, 252; 252, call; 252, 253; 252, 259; 253, attribute; 253, 254; 253, 258; 254, call; 254, 255; 254, 256; 255, identifier:writer; 256, argument_list; 256, 257; 257, identifier:body; 258, identifier:write; 259, argument_list; 259, 260; 260, binary_operator:%; 260, 261; 260, 264; 261, concatenated_string; 261, 262; 261, 263; 262, string:'Content-Disposition: form-data; name="%s"; '; 263, string:'filename="%s"\r\n'; 264, tuple; 264, 265; 264, 266; 265, identifier:fieldname; 266, identifier:filename; 267, expression_statement; 267, 268; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:body; 271, identifier:write; 272, argument_list; 272, 273; 273, string:b'Content-Type: application/octet-stream\r\n\r\n'; 274, else_clause; 274, 275; 275, block; 275, 276; 275, 290; 276, expression_statement; 276, 277; 277, call; 277, 278; 277, 284; 278, attribute; 278, 279; 278, 283; 279, call; 279, 280; 279, 281; 280, identifier:writer; 281, argument_list; 281, 282; 282, identifier:body; 283, identifier:write; 284, argument_list; 284, 285; 285, binary_operator:%; 285, 286; 285, 289; 286, concatenated_string; 286, 287; 286, 288; 287, string:'Content-Disposition: form-data; name="%s"'; 288, string:'\r\n'; 289, identifier:name; 290, expression_statement; 290, 291; 291, call; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, identifier:body; 294, identifier:write; 295, argument_list; 295, 296; 296, string:b'Content-Type: text/plain\r\n\r\n'; 297, if_statement; 297, 298; 297, 305; 297, 316; 298, boolean_operator:and; 298, 299; 298, 300; 299, identifier:py3k; 300, call; 300, 301; 300, 302; 301, identifier:isinstance; 302, argument_list; 302, 303; 302, 304; 303, identifier:value; 304, identifier:str; 305, block; 305, 306; 306, expression_statement; 306, 307; 307, call; 307, 308; 307, 314; 308, attribute; 308, 309; 308, 313; 309, call; 309, 310; 309, 311; 310, identifier:writer; 311, argument_list; 311, 312; 312, identifier:body; 313, identifier:write; 314, argument_list; 314, 315; 315, identifier:value; 316, else_clause; 316, 317; 317, block; 317, 318; 318, expression_statement; 318, 319; 319, call; 319, 320; 319, 323; 320, attribute; 320, 321; 320, 322; 321, identifier:body; 322, identifier:write; 323, argument_list; 323, 324; 324, identifier:value; 325, expression_statement; 325, 326; 326, call; 326, 327; 326, 330; 327, attribute; 327, 328; 327, 329; 328, identifier:body; 329, identifier:write; 330, argument_list; 330, 331; 331, string:b'\r\n'; 332, expression_statement; 332, 333; 333, call; 333, 334; 333, 337; 334, attribute; 334, 335; 334, 336; 335, identifier:body; 336, identifier:write; 337, argument_list; 337, 338; 338, call; 338, 339; 338, 340; 339, identifier:b; 340, argument_list; 340, 341; 341, binary_operator:+; 341, 342; 341, 345; 342, binary_operator:+; 342, 343; 342, 344; 343, string:'--'; 344, identifier:boundary; 345, string:'--\r\n'; 346, expression_statement; 346, 347; 347, assignment; 347, 348; 347, 349; 348, identifier:content_type; 349, binary_operator:%; 349, 350; 349, 351; 350, string:'multipart/form-data; boundary=%s'; 351, identifier:boundary; 352, return_statement; 352, 353; 353, expression_list; 353, 354; 353, 355; 354, identifier:content_type; 355, call; 355, 356; 355, 359; 356, attribute; 356, 357; 356, 358; 357, identifier:body; 358, identifier:getvalue; 359, argument_list | def encode_multipart(data, files):
"""Encode multipart.
:arg dict data: Data to be encoded
:arg dict files: Files to be encoded
:returns: Encoded binary string
:raises: :class:`UrlfetchException`
"""
body = BytesIO()
boundary = choose_boundary()
part_boundary = b('--%s\r\n' % boundary)
writer = codecs.lookup('utf-8')[3]
if isinstance(data, dict):
for name, values in data.items():
if not isinstance(values, (list, tuple, set)):
# behave like urllib.urlencode(dict, 1)
values = (values, )
for value in values:
body.write(part_boundary)
writer(body).write('Content-Disposition: form-data; '
'name="%s"\r\n' % name)
body.write(b'Content-Type: text/plain\r\n\r\n')
if isinstance(value, int):
value = str(value)
if py3k and isinstance(value, str):
writer(body).write(value)
else:
body.write(value)
body.write(b'\r\n')
for fieldname, f in files.items():
if isinstance(f, tuple):
filename, f = f
elif hasattr(f, 'name'):
filename = basename(f.name)
else:
filename = None
raise UrlfetchException("file must has filename")
if hasattr(f, 'read'):
value = f.read()
elif isinstance(f, basestring):
value = f
else:
value = str(f)
body.write(part_boundary)
if filename:
writer(body).write('Content-Disposition: form-data; name="%s"; '
'filename="%s"\r\n' % (fieldname, filename))
body.write(b'Content-Type: application/octet-stream\r\n\r\n')
else:
writer(body).write('Content-Disposition: form-data; name="%s"'
'\r\n' % name)
body.write(b'Content-Type: text/plain\r\n\r\n')
if py3k and isinstance(value, str):
writer(body).write(value)
else:
body.write(value)
body.write(b'\r\n')
body.write(b('--' + boundary + '--\r\n'))
content_type = 'multipart/form-data; boundary=%s' % boundary
return content_type, body.getvalue() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:assemble; 3, parameters; 3, 4; 4, identifier:code; 5, block; 5, 6; 5, 8; 5, 12; 5, 13; 5, 14; 5, 15; 5, 16; 5, 173; 5, 177; 5, 178; 5, 182; 5, 213; 5, 214; 5, 215; 5, 219; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:final; 11, list:[]; 12, comment; 13, comment; 14, comment; 15, comment; 16, for_statement; 16, 17; 16, 18; 16, 19; 17, identifier:line; 18, identifier:code; 19, block; 19, 20; 19, 35; 19, 49; 19, 57; 19, 58; 19, 59; 19, 63; 19, 159; 19, 160; 20, if_statement; 20, 21; 20, 26; 21, call; 21, 22; 21, 23; 22, identifier:isinstance; 23, argument_list; 23, 24; 23, 25; 24, identifier:line; 25, identifier:Label; 26, block; 26, 27; 26, 34; 27, expression_statement; 27, 28; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:final; 31, identifier:append; 32, argument_list; 32, 33; 33, identifier:line; 34, continue_statement; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 40; 37, pattern_list; 37, 38; 37, 39; 38, identifier:mnemonic; 39, identifier:operands; 40, expression_list; 40, 41; 40, 44; 41, subscript; 41, 42; 41, 43; 42, identifier:line; 43, integer:0; 44, subscript; 44, 45; 44, 46; 45, identifier:line; 46, slice; 46, 47; 46, 48; 47, integer:1; 48, colon; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:operand_fmts; 52, subscript; 52, 53; 52, 56; 53, subscript; 53, 54; 53, 55; 54, identifier:opcode_table; 55, identifier:mnemonic; 56, string:'operands'; 57, comment; 58, comment; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:final_operands; 62, list:[]; 63, for_statement; 63, 64; 63, 67; 63, 71; 64, pattern_list; 64, 65; 64, 66; 65, identifier:i; 66, identifier:operand; 67, call; 67, 68; 67, 69; 68, identifier:enumerate; 69, argument_list; 69, 70; 70, identifier:operands; 71, block; 71, 72; 72, if_statement; 72, 73; 72, 78; 72, 79; 72, 87; 72, 110; 72, 126; 72, 140; 73, call; 73, 74; 73, 75; 74, identifier:isinstance; 75, argument_list; 75, 76; 75, 77; 76, identifier:operand; 77, identifier:Operand; 78, comment; 79, block; 79, 80; 80, expression_statement; 80, 81; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:final_operands; 84, identifier:append; 85, argument_list; 85, 86; 86, identifier:operand; 87, elif_clause; 87, 88; 87, 93; 87, 94; 88, call; 88, 89; 88, 90; 89, identifier:isinstance; 90, argument_list; 90, 91; 90, 92; 91, identifier:operand; 92, identifier:Constant; 93, comment; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:final_operands; 99, identifier:append; 100, argument_list; 100, 101; 101, call; 101, 102; 101, 103; 102, identifier:Operand; 103, argument_list; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:OperandTypes; 106, identifier:CONSTANT_INDEX; 107, attribute; 107, 108; 107, 109; 108, identifier:operand; 109, identifier:index; 110, elif_clause; 110, 111; 110, 116; 110, 117; 110, 118; 111, call; 111, 112; 111, 113; 112, identifier:isinstance; 113, argument_list; 113, 114; 113, 115; 114, identifier:operand; 115, identifier:dict; 116, comment; 117, comment; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:final_operands; 123, identifier:append; 124, argument_list; 124, 125; 125, identifier:operand; 126, elif_clause; 126, 127; 126, 132; 127, call; 127, 128; 127, 129; 128, identifier:isinstance; 129, argument_list; 129, 130; 129, 131; 130, identifier:operand; 131, identifier:Label; 132, block; 132, 133; 133, expression_statement; 133, 134; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:final_operands; 137, identifier:append; 138, argument_list; 138, 139; 139, identifier:operand; 140, else_clause; 140, 141; 140, 142; 140, 143; 141, comment; 142, comment; 143, block; 143, 144; 144, expression_statement; 144, 145; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:final_operands; 148, identifier:append; 149, argument_list; 149, 150; 150, call; 150, 151; 150, 152; 151, identifier:Operand; 152, argument_list; 152, 153; 152, 158; 153, subscript; 153, 154; 153, 157; 154, subscript; 154, 155; 154, 156; 155, identifier:operand_fmts; 156, identifier:i; 157, integer:1; 158, identifier:operand; 159, comment; 160, expression_statement; 160, 161; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:final; 164, identifier:append; 165, argument_list; 165, 166; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:Instruction; 169, identifier:create; 170, argument_list; 170, 171; 170, 172; 171, identifier:mnemonic; 172, identifier:final_operands; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:label_pcs; 176, dictionary; 177, comment; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:current_pc; 181, integer:0; 182, for_statement; 182, 183; 182, 184; 182, 185; 183, identifier:ins; 184, identifier:final; 185, block; 185, 186; 185, 202; 185, 203; 185, 204; 186, if_statement; 186, 187; 186, 192; 187, call; 187, 188; 187, 189; 188, identifier:isinstance; 189, argument_list; 189, 190; 189, 191; 190, identifier:ins; 191, identifier:Label; 192, block; 192, 193; 192, 201; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 200; 195, subscript; 195, 196; 195, 197; 196, identifier:label_pcs; 197, attribute; 197, 198; 197, 199; 198, identifier:ins; 199, identifier:name; 200, identifier:current_pc; 201, continue_statement; 202, comment; 203, comment; 204, expression_statement; 204, 205; 205, augmented_assignment:+=; 205, 206; 205, 207; 206, identifier:current_pc; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:ins; 210, identifier:size_on_disk; 211, argument_list; 211, 212; 212, identifier:current_pc; 213, comment; 214, comment; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:current_pc; 218, integer:0; 219, for_statement; 219, 220; 219, 221; 219, 222; 220, identifier:ins; 221, identifier:final; 222, block; 222, 223; 222, 231; 222, 308; 222, 317; 223, if_statement; 223, 224; 223, 229; 224, call; 224, 225; 224, 226; 225, identifier:isinstance; 226, argument_list; 226, 227; 226, 228; 227, identifier:ins; 228, identifier:Label; 229, block; 229, 230; 230, continue_statement; 231, for_statement; 231, 232; 231, 235; 231, 241; 232, pattern_list; 232, 233; 232, 234; 233, identifier:i; 234, identifier:operand; 235, call; 235, 236; 235, 237; 236, identifier:enumerate; 237, argument_list; 237, 238; 238, attribute; 238, 239; 238, 240; 239, identifier:ins; 240, identifier:operands; 241, block; 241, 242; 242, if_statement; 242, 243; 242, 248; 242, 249; 242, 283; 243, call; 243, 244; 243, 245; 244, identifier:isinstance; 245, argument_list; 245, 246; 245, 247; 246, identifier:operand; 247, identifier:dict; 248, comment; 249, block; 249, 250; 250, for_statement; 250, 251; 250, 254; 250, 259; 251, pattern_list; 251, 252; 251, 253; 252, identifier:k; 253, identifier:v; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:operand; 257, identifier:items; 258, argument_list; 259, block; 259, 260; 260, if_statement; 260, 261; 260, 266; 261, call; 261, 262; 261, 263; 262, identifier:isinstance; 263, argument_list; 263, 264; 263, 265; 264, identifier:v; 265, identifier:Label; 266, block; 266, 267; 267, expression_statement; 267, 268; 268, assignment; 268, 269; 268, 272; 269, subscript; 269, 270; 269, 271; 270, identifier:operand; 271, identifier:k; 272, call; 272, 273; 272, 274; 273, identifier:Operand; 274, argument_list; 274, 275; 274, 276; 275, integer:40; 276, binary_operator:-; 276, 277; 276, 282; 277, subscript; 277, 278; 277, 279; 278, identifier:label_pcs; 279, attribute; 279, 280; 279, 281; 280, identifier:v; 281, identifier:name; 282, identifier:current_pc; 283, elif_clause; 283, 284; 283, 289; 284, call; 284, 285; 284, 286; 285, identifier:isinstance; 286, argument_list; 286, 287; 286, 288; 287, identifier:operand; 288, identifier:Label; 289, block; 289, 290; 290, expression_statement; 290, 291; 291, assignment; 291, 292; 291, 297; 292, subscript; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:ins; 295, identifier:operands; 296, identifier:i; 297, call; 297, 298; 297, 299; 298, identifier:Operand; 299, argument_list; 299, 300; 299, 301; 300, integer:40; 301, binary_operator:-; 301, 302; 301, 307; 302, subscript; 302, 303; 302, 304; 303, identifier:label_pcs; 304, attribute; 304, 305; 304, 306; 305, identifier:operand; 306, identifier:name; 307, identifier:current_pc; 308, expression_statement; 308, 309; 309, augmented_assignment:+=; 309, 310; 309, 311; 310, identifier:current_pc; 311, call; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:ins; 314, identifier:size_on_disk; 315, argument_list; 315, 316; 316, identifier:current_pc; 317, expression_statement; 317, 318; 318, yield; 318, 319; 319, identifier:ins | def assemble(code):
"""
Assemble the given iterable of mnemonics, operands, and lables.
A convienience over constructing individual Instruction and Operand
objects, the output of this function can be directly piped to
:class:`~jawa.attributes.code.CodeAttribute.assemble()` to produce
executable bytecode.
As a simple example, lets produce an infinite loop:
>>> from jawa.assemble import assemble, Label
>>> print(list(assemble((
... Label('start'),
... ('goto', Label('start'))
... ))))
[Instruction(mnemonic='goto', opcode=167, operands=[
Operand(op_type=40, value=0)], pos=0)]
For a more complex example, see examples/hello_world.py.
"""
final = []
# We need to make three passes, because we cannot know the offset for
# jump labels until after we've figured out the PC for each instructions,
# which is complicated by the variable-width instructions set and
# alignment padding.
for line in code:
if isinstance(line, Label):
final.append(line)
continue
mnemonic, operands = line[0], line[1:]
operand_fmts = opcode_table[mnemonic]['operands']
# We need to coerce each opcodes operands into their
# final `Operand` form.
final_operands = []
for i, operand in enumerate(operands):
if isinstance(operand, Operand):
# Already in Operand form.
final_operands.append(operand)
elif isinstance(operand, Constant):
# Convert constants into CONSTANT_INDEX'es
final_operands.append(Operand(
OperandTypes.CONSTANT_INDEX,
operand.index
))
elif isinstance(operand, dict):
# lookupswitch's operand is a dict as
# a special usability case.
final_operands.append(operand)
elif isinstance(operand, Label):
final_operands.append(operand)
else:
# For anything else, lookup that opcode's operand
# type from its definition.
final_operands.append(Operand(
operand_fmts[i][1],
operand
))
# Build the final, immutable `Instruction`.
final.append(Instruction.create(mnemonic, final_operands))
label_pcs = {}
# The second pass, find the absolute PC for each label.
current_pc = 0
for ins in final:
if isinstance(ins, Label):
label_pcs[ins.name] = current_pc
continue
# size_on_disk must know the current pc because of alignment on
# tableswitch and lookupswitch.
current_pc += ins.size_on_disk(current_pc)
# The third pass, now that we know where each label is we can figure
# out the offset for each jump.
current_pc = 0
for ins in final:
if isinstance(ins, Label):
continue
for i, operand in enumerate(ins.operands):
if isinstance(operand, dict):
# lookupswitch is a special case
for k, v in operand.items():
if isinstance(v, Label):
operand[k] = Operand(40, label_pcs[v.name] - current_pc)
elif isinstance(operand, Label):
ins.operands[i] = Operand(
40,
label_pcs[operand.name] - current_pc
)
current_pc += ins.size_on_disk(current_pc)
yield ins |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_add_new; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:host_object; 6, block; 6, 7; 6, 9; 6, 18; 6, 45; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:i; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:_get_insertion_point; 16, argument_list; 16, 17; 17, identifier:host_object; 18, for_statement; 18, 19; 18, 20; 18, 25; 19, identifier:listed; 20, subscript; 20, 21; 20, 22; 21, identifier:self; 22, slice; 22, 23; 22, 24; 23, identifier:i; 24, colon; 25, block; 25, 26; 25, 36; 26, if_statement; 26, 27; 26, 34; 27, not_operator; 27, 28; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:listed; 31, identifier:is_subdomain; 32, argument_list; 32, 33; 33, identifier:host_object; 34, block; 34, 35; 35, break_statement; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 43; 38, attribute; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:hosts; 42, identifier:pop; 43, argument_list; 43, 44; 44, identifier:i; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 52; 47, attribute; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:hosts; 51, identifier:insert; 52, argument_list; 52, 53; 52, 54; 53, identifier:i; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:host_object; 57, identifier:to_unicode; 58, argument_list | def _add_new(self, host_object):
"""Add a new host to the collection.
Before a new hostname can be added, all its subdomains already
present in the collection must be removed. Since the collection
is sorted, we can limit our search for them to a slice of
the collection starting from insertion point and ending with
the last detected subdomain.
:param host_obj: an object representing value to be added.
It is assumed that, during execution of this method,
the value to be added is not currently listed.
"""
i = self._get_insertion_point(host_object)
for listed in self[i:]:
if not listed.is_subdomain(host_object):
break
self.hosts.pop(i)
self.hosts.insert(i, host_object.to_unicode()) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:write_instruction; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:fout; 5, identifier:start_pos; 6, identifier:ins; 7, block; 7, 8; 7, 10; 7, 22; 7, 30; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 15; 12, pattern_list; 12, 13; 12, 14; 13, identifier:opcode; 14, identifier:operands; 15, expression_list; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:ins; 18, identifier:opcode; 19, attribute; 19, 20; 19, 21; 20, identifier:ins; 21, identifier:operands; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:fmt_operands; 25, subscript; 25, 26; 25, 29; 26, subscript; 26, 27; 26, 28; 27, identifier:opcode_table; 28, identifier:opcode; 29, string:'operands'; 30, if_statement; 30, 31; 30, 34; 30, 35; 30, 94; 30, 138; 30, 241; 30, 326; 31, attribute; 31, 32; 31, 33; 32, identifier:ins; 33, identifier:wide; 34, comment; 35, block; 35, 36; 35, 47; 35, 48; 35, 59; 35, 74; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:fout; 40, identifier:write; 41, argument_list; 41, 42; 42, call; 42, 43; 42, 44; 43, identifier:pack; 44, argument_list; 44, 45; 44, 46; 45, string:'>B'; 46, integer:0xC4; 47, comment; 48, expression_statement; 48, 49; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:fout; 52, identifier:write; 53, argument_list; 53, 54; 54, call; 54, 55; 54, 56; 55, identifier:pack; 56, argument_list; 56, 57; 56, 58; 57, string:'>B'; 58, identifier:opcode; 59, expression_statement; 59, 60; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:fout; 63, identifier:write; 64, argument_list; 64, 65; 65, call; 65, 66; 65, 67; 66, identifier:pack; 67, argument_list; 67, 68; 67, 69; 68, string:'>H'; 69, attribute; 69, 70; 69, 73; 70, subscript; 70, 71; 70, 72; 71, identifier:operands; 72, integer:0; 73, identifier:value; 74, if_statement; 74, 75; 74, 78; 75, comparison_operator:==; 75, 76; 75, 77; 76, identifier:opcode; 77, integer:0x84; 78, block; 78, 79; 79, expression_statement; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:fout; 83, identifier:write; 84, argument_list; 84, 85; 85, call; 85, 86; 85, 87; 86, identifier:pack; 87, argument_list; 87, 88; 87, 89; 88, string:'>h'; 89, attribute; 89, 90; 89, 93; 90, subscript; 90, 91; 90, 92; 91, identifier:operands; 92, integer:1; 93, identifier:value; 94, elif_clause; 94, 95; 94, 96; 94, 97; 95, identifier:fmt_operands; 96, comment; 97, block; 97, 98; 97, 109; 98, expression_statement; 98, 99; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:fout; 102, identifier:write; 103, argument_list; 103, 104; 104, call; 104, 105; 104, 106; 105, identifier:pack; 106, argument_list; 106, 107; 106, 108; 107, string:'>B'; 108, identifier:opcode; 109, for_statement; 109, 110; 109, 115; 109, 119; 110, pattern_list; 110, 111; 110, 112; 111, identifier:i; 112, tuple_pattern; 112, 113; 112, 114; 113, identifier:fmt; 114, identifier:_; 115, call; 115, 116; 115, 117; 116, identifier:enumerate; 117, argument_list; 117, 118; 118, identifier:fmt_operands; 119, block; 119, 120; 120, expression_statement; 120, 121; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:fout; 124, identifier:write; 125, argument_list; 125, 126; 126, call; 126, 127; 126, 132; 127, attribute; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:fmt; 130, identifier:value; 131, identifier:pack; 132, argument_list; 132, 133; 133, attribute; 133, 134; 133, 137; 134, subscript; 134, 135; 134, 136; 135, identifier:operands; 136, identifier:i; 137, identifier:value; 138, elif_clause; 138, 139; 138, 142; 138, 143; 139, comparison_operator:==; 139, 140; 139, 141; 140, identifier:opcode; 141, integer:0xAB; 142, comment; 143, block; 143, 144; 143, 155; 143, 156; 143, 157; 143, 158; 143, 159; 143, 160; 143, 161; 143, 172; 143, 181; 143, 191; 143, 212; 144, expression_statement; 144, 145; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:fout; 148, identifier:write; 149, argument_list; 149, 150; 150, call; 150, 151; 150, 152; 151, identifier:pack; 152, argument_list; 152, 153; 152, 154; 153, string:'>B'; 154, identifier:opcode; 155, comment; 156, comment; 157, comment; 158, comment; 159, comment; 160, comment; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:padding; 164, binary_operator:-; 164, 165; 164, 166; 165, integer:4; 166, binary_operator:%; 166, 167; 166, 171; 167, parenthesized_expression; 167, 168; 168, binary_operator:+; 168, 169; 168, 170; 169, identifier:start_pos; 170, integer:1; 171, integer:4; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:padding; 175, conditional_expression:if; 175, 176; 175, 177; 175, 180; 176, identifier:padding; 177, comparison_operator:!=; 177, 178; 177, 179; 178, identifier:padding; 179, integer:4; 180, integer:0; 181, expression_statement; 181, 182; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:fout; 185, identifier:write; 186, argument_list; 186, 187; 187, call; 187, 188; 187, 189; 188, identifier:pack; 189, argument_list; 189, 190; 190, string:f'{padding}x'; 191, expression_statement; 191, 192; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:fout; 195, identifier:write; 196, argument_list; 196, 197; 197, call; 197, 198; 197, 199; 198, identifier:pack; 199, argument_list; 199, 200; 199, 201; 199, 206; 200, string:'>ii'; 201, attribute; 201, 202; 201, 205; 202, subscript; 202, 203; 202, 204; 203, identifier:operands; 204, integer:1; 205, identifier:value; 206, call; 206, 207; 206, 208; 207, identifier:len; 208, argument_list; 208, 209; 209, subscript; 209, 210; 209, 211; 210, identifier:operands; 211, integer:0; 212, for_statement; 212, 213; 212, 214; 212, 224; 213, identifier:key; 214, call; 214, 215; 214, 216; 215, identifier:sorted; 216, argument_list; 216, 217; 217, call; 217, 218; 217, 223; 218, attribute; 218, 219; 218, 222; 219, subscript; 219, 220; 219, 221; 220, identifier:operands; 221, integer:0; 222, identifier:keys; 223, argument_list; 224, block; 224, 225; 225, expression_statement; 225, 226; 226, call; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:fout; 229, identifier:write; 230, argument_list; 230, 231; 231, call; 231, 232; 231, 233; 232, identifier:pack; 233, argument_list; 233, 234; 233, 235; 233, 236; 234, string:'>ii'; 235, identifier:key; 236, subscript; 236, 237; 236, 240; 237, subscript; 237, 238; 237, 239; 238, identifier:operands; 239, integer:0; 240, identifier:key; 241, elif_clause; 241, 242; 241, 245; 241, 246; 242, comparison_operator:==; 242, 243; 242, 244; 243, identifier:opcode; 244, integer:0xAA; 245, comment; 246, block; 246, 247; 246, 258; 246, 269; 246, 278; 246, 288; 247, expression_statement; 247, 248; 248, call; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:fout; 251, identifier:write; 252, argument_list; 252, 253; 253, call; 253, 254; 253, 255; 254, identifier:pack; 255, argument_list; 255, 256; 255, 257; 256, string:'>B'; 257, identifier:opcode; 258, expression_statement; 258, 259; 259, assignment; 259, 260; 259, 261; 260, identifier:padding; 261, binary_operator:-; 261, 262; 261, 263; 262, integer:4; 263, binary_operator:%; 263, 264; 263, 268; 264, parenthesized_expression; 264, 265; 265, binary_operator:+; 265, 266; 265, 267; 266, identifier:start_pos; 267, integer:1; 268, integer:4; 269, expression_statement; 269, 270; 270, assignment; 270, 271; 270, 272; 271, identifier:padding; 272, conditional_expression:if; 272, 273; 272, 274; 272, 277; 273, identifier:padding; 274, comparison_operator:!=; 274, 275; 274, 276; 275, identifier:padding; 276, integer:4; 277, integer:0; 278, expression_statement; 278, 279; 279, call; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:fout; 282, identifier:write; 283, argument_list; 283, 284; 284, call; 284, 285; 284, 286; 285, identifier:pack; 286, argument_list; 286, 287; 287, string:f'{padding}x'; 288, expression_statement; 288, 289; 289, call; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:fout; 292, identifier:write; 293, argument_list; 293, 294; 294, call; 294, 295; 294, 296; 295, identifier:pack; 296, argument_list; 296, 297; 296, 298; 296, 299; 296, 304; 296, 309; 296, 314; 297, string:f'>iii{len(operands) - 3}i'; 298, comment; 299, attribute; 299, 300; 299, 303; 300, subscript; 300, 301; 300, 302; 301, identifier:operands; 302, integer:0; 303, identifier:value; 304, attribute; 304, 305; 304, 308; 305, subscript; 305, 306; 305, 307; 306, identifier:operands; 307, integer:1; 308, identifier:value; 309, attribute; 309, 310; 309, 313; 310, subscript; 310, 311; 310, 312; 311, identifier:operands; 312, integer:2; 313, identifier:value; 314, list_splat; 314, 315; 315, generator_expression; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:o; 318, identifier:value; 319, for_in_clause; 319, 320; 319, 321; 320, identifier:o; 321, subscript; 321, 322; 321, 323; 322, identifier:operands; 323, slice; 323, 324; 323, 325; 324, integer:3; 325, colon; 326, else_clause; 326, 327; 326, 328; 327, comment; 328, block; 328, 329; 329, expression_statement; 329, 330; 330, call; 330, 331; 330, 334; 331, attribute; 331, 332; 331, 333; 332, identifier:fout; 333, identifier:write; 334, argument_list; 334, 335; 335, call; 335, 336; 335, 337; 336, identifier:pack; 337, argument_list; 337, 338; 337, 339; 338, string:'>B'; 339, identifier:opcode | def write_instruction(fout, start_pos, ins):
"""
Writes a single instruction of `opcode` with `operands` to `fout`.
:param fout: Any file-like object providing ``write()``.
:param start_pos: The current position in the stream.
:param ins: The `Instruction` to write.
"""
opcode, operands = ins.opcode, ins.operands
fmt_operands = opcode_table[opcode]['operands']
if ins.wide:
# The "WIDE" prefix
fout.write(pack('>B', 0xC4))
# The real opcode.
fout.write(pack('>B', opcode))
fout.write(pack('>H', operands[0].value))
if opcode == 0x84:
fout.write(pack('>h', operands[1].value))
elif fmt_operands:
# A normal simple opcode with simple operands.
fout.write(pack('>B', opcode))
for i, (fmt, _) in enumerate(fmt_operands):
fout.write(fmt.value.pack(operands[i].value))
elif opcode == 0xAB:
# Special case for lookupswitch.
fout.write(pack('>B', opcode))
# assemble([
# ('lookupswitch', {
# 2: -3,
# 4: 5
# }, <default>)
# ])
padding = 4 - (start_pos + 1) % 4
padding = padding if padding != 4 else 0
fout.write(pack(f'{padding}x'))
fout.write(pack('>ii', operands[1].value, len(operands[0])))
for key in sorted(operands[0].keys()):
fout.write(pack('>ii', key, operands[0][key]))
elif opcode == 0xAA:
# Special case for table switch.
fout.write(pack('>B', opcode))
padding = 4 - (start_pos + 1) % 4
padding = padding if padding != 4 else 0
fout.write(pack(f'{padding}x'))
fout.write(pack(
f'>iii{len(operands) - 3}i',
# Default branch offset
operands[0].value,
operands[1].value,
operands[2].value,
*(o.value for o in operands[3:])
))
else:
# opcode with no operands.
fout.write(pack('>B', opcode)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:read_instruction; 3, parameters; 3, 4; 3, 5; 4, identifier:fio; 5, identifier:start_pos; 6, block; 6, 7; 6, 9; 6, 18; 6, 24; 6, 31; 6, 37; 6, 43; 6, 49; 6, 53; 6, 54; 6, 411; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:op; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:fio; 15, identifier:read; 16, argument_list; 16, 17; 17, integer:1; 18, if_statement; 18, 19; 18, 21; 19, not_operator; 19, 20; 20, identifier:op; 21, block; 21, 22; 22, return_statement; 22, 23; 23, None; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:op; 27, call; 27, 28; 27, 29; 28, identifier:ord; 29, argument_list; 29, 30; 30, identifier:op; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:ins; 34, subscript; 34, 35; 34, 36; 35, identifier:opcode_table; 36, identifier:op; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:operands; 40, subscript; 40, 41; 40, 42; 41, identifier:ins; 42, string:'operands'; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:name; 46, subscript; 46, 47; 46, 48; 47, identifier:ins; 48, string:'mnemonic'; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:final_operands; 52, list:[]; 53, comment; 54, if_statement; 54, 55; 54, 56; 54, 92; 54, 93; 54, 195; 54, 196; 54, 324; 54, 325; 55, identifier:operands; 56, block; 56, 57; 57, for_statement; 57, 58; 57, 61; 57, 62; 58, pattern_list; 58, 59; 58, 60; 59, identifier:fmt; 60, identifier:type_; 61, identifier:operands; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:final_operands; 67, identifier:append; 68, argument_list; 68, 69; 69, call; 69, 70; 69, 71; 70, identifier:Operand; 71, argument_list; 71, 72; 71, 73; 72, identifier:type_; 73, subscript; 73, 74; 73, 91; 74, call; 74, 75; 74, 80; 75, attribute; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:fmt; 78, identifier:value; 79, identifier:unpack; 80, argument_list; 80, 81; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:fio; 84, identifier:read; 85, argument_list; 85, 86; 86, attribute; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:fmt; 89, identifier:value; 90, identifier:size; 91, integer:0; 92, comment; 93, elif_clause; 93, 94; 93, 97; 93, 98; 94, comparison_operator:==; 94, 95; 94, 96; 95, identifier:op; 96, integer:0xAB; 97, comment; 98, block; 98, 99; 98, 110; 98, 119; 98, 126; 98, 127; 98, 142; 98, 146; 98, 175; 98, 182; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:padding; 102, binary_operator:-; 102, 103; 102, 104; 103, integer:4; 104, binary_operator:%; 104, 105; 104, 109; 105, parenthesized_expression; 105, 106; 106, binary_operator:+; 106, 107; 106, 108; 107, identifier:start_pos; 108, integer:1; 109, integer:4; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:padding; 113, conditional_expression:if; 113, 114; 113, 115; 113, 118; 114, identifier:padding; 115, comparison_operator:!=; 115, 116; 115, 117; 116, identifier:padding; 117, integer:4; 118, integer:0; 119, expression_statement; 119, 120; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:fio; 123, identifier:read; 124, argument_list; 124, 125; 125, identifier:padding; 126, comment; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 132; 129, pattern_list; 129, 130; 129, 131; 130, identifier:default; 131, identifier:npairs; 132, call; 132, 133; 132, 134; 133, identifier:unpack; 134, argument_list; 134, 135; 134, 136; 135, string:'>ii'; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:fio; 139, identifier:read; 140, argument_list; 140, 141; 141, integer:8; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:pairs; 145, dictionary; 146, for_statement; 146, 147; 146, 148; 146, 153; 147, identifier:_; 148, call; 148, 149; 148, 150; 149, identifier:repeat; 150, argument_list; 150, 151; 150, 152; 151, None; 152, identifier:npairs; 153, block; 153, 154; 153, 169; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 159; 156, pattern_list; 156, 157; 156, 158; 157, identifier:match; 158, identifier:offset; 159, call; 159, 160; 159, 161; 160, identifier:unpack; 161, argument_list; 161, 162; 161, 163; 162, string:'>ii'; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:fio; 166, identifier:read; 167, argument_list; 167, 168; 168, integer:8; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 174; 171, subscript; 171, 172; 171, 173; 172, identifier:pairs; 173, identifier:match; 174, identifier:offset; 175, expression_statement; 175, 176; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:final_operands; 179, identifier:append; 180, argument_list; 180, 181; 181, identifier:pairs; 182, expression_statement; 182, 183; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:final_operands; 186, identifier:append; 187, argument_list; 187, 188; 188, call; 188, 189; 188, 190; 189, identifier:Operand; 190, argument_list; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:OperandTypes; 193, identifier:BRANCH; 194, identifier:default; 195, comment; 196, elif_clause; 196, 197; 196, 200; 196, 201; 197, comparison_operator:==; 197, 198; 197, 199; 198, identifier:op; 199, integer:0xAA; 200, comment; 201, block; 201, 202; 201, 213; 201, 222; 201, 229; 201, 245; 201, 258; 201, 271; 201, 284; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 205; 204, identifier:padding; 205, binary_operator:-; 205, 206; 205, 207; 206, integer:4; 207, binary_operator:%; 207, 208; 207, 212; 208, parenthesized_expression; 208, 209; 209, binary_operator:+; 209, 210; 209, 211; 210, identifier:start_pos; 211, integer:1; 212, integer:4; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 216; 215, identifier:padding; 216, conditional_expression:if; 216, 217; 216, 218; 216, 221; 217, identifier:padding; 218, comparison_operator:!=; 218, 219; 218, 220; 219, identifier:padding; 220, integer:4; 221, integer:0; 222, expression_statement; 222, 223; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:fio; 226, identifier:read; 227, argument_list; 227, 228; 228, identifier:padding; 229, expression_statement; 229, 230; 230, assignment; 230, 231; 230, 235; 231, pattern_list; 231, 232; 231, 233; 231, 234; 232, identifier:default; 233, identifier:low; 234, identifier:high; 235, call; 235, 236; 235, 237; 236, identifier:unpack; 237, argument_list; 237, 238; 237, 239; 238, string:'>iii'; 239, call; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:fio; 242, identifier:read; 243, argument_list; 243, 244; 244, integer:12; 245, expression_statement; 245, 246; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:final_operands; 249, identifier:append; 250, argument_list; 250, 251; 251, call; 251, 252; 251, 253; 252, identifier:Operand; 253, argument_list; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:OperandTypes; 256, identifier:BRANCH; 257, identifier:default; 258, expression_statement; 258, 259; 259, call; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:final_operands; 262, identifier:append; 263, argument_list; 263, 264; 264, call; 264, 265; 264, 266; 265, identifier:Operand; 266, argument_list; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:OperandTypes; 269, identifier:LITERAL; 270, identifier:low; 271, expression_statement; 271, 272; 272, call; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:final_operands; 275, identifier:append; 276, argument_list; 276, 277; 277, call; 277, 278; 277, 279; 278, identifier:Operand; 279, argument_list; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:OperandTypes; 282, identifier:LITERAL; 283, identifier:high; 284, for_statement; 284, 285; 284, 286; 284, 295; 285, identifier:_; 286, call; 286, 287; 286, 288; 287, identifier:repeat; 288, argument_list; 288, 289; 288, 290; 289, None; 290, binary_operator:+; 290, 291; 290, 294; 291, binary_operator:-; 291, 292; 291, 293; 292, identifier:high; 293, identifier:low; 294, integer:1; 295, block; 295, 296; 295, 311; 296, expression_statement; 296, 297; 297, assignment; 297, 298; 297, 299; 298, identifier:offset; 299, subscript; 299, 300; 299, 310; 300, call; 300, 301; 300, 302; 301, identifier:unpack; 302, argument_list; 302, 303; 302, 304; 303, string:'>i'; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:fio; 307, identifier:read; 308, argument_list; 308, 309; 309, integer:4; 310, integer:0; 311, expression_statement; 311, 312; 312, call; 312, 313; 312, 316; 313, attribute; 313, 314; 313, 315; 314, identifier:final_operands; 315, identifier:append; 316, argument_list; 316, 317; 317, call; 317, 318; 317, 319; 318, identifier:Operand; 319, argument_list; 319, 320; 319, 323; 320, attribute; 320, 321; 320, 322; 321, identifier:OperandTypes; 322, identifier:BRANCH; 323, identifier:offset; 324, comment; 325, elif_clause; 325, 326; 325, 329; 326, comparison_operator:==; 326, 327; 326, 328; 327, identifier:op; 328, integer:0xC4; 329, block; 329, 330; 329, 345; 329, 351; 329, 357; 329, 381; 329, 382; 330, expression_statement; 330, 331; 331, assignment; 331, 332; 331, 333; 332, identifier:real_op; 333, subscript; 333, 334; 333, 344; 334, call; 334, 335; 334, 336; 335, identifier:unpack; 336, argument_list; 336, 337; 336, 338; 337, string:'>B'; 338, call; 338, 339; 338, 342; 339, attribute; 339, 340; 339, 341; 340, identifier:fio; 341, identifier:read; 342, argument_list; 342, 343; 343, integer:1; 344, integer:0; 345, expression_statement; 345, 346; 346, assignment; 346, 347; 346, 348; 347, identifier:ins; 348, subscript; 348, 349; 348, 350; 349, identifier:opcode_table; 350, identifier:real_op; 351, expression_statement; 351, 352; 352, assignment; 352, 353; 352, 354; 353, identifier:name; 354, subscript; 354, 355; 354, 356; 355, identifier:ins; 356, string:'mnemonic'; 357, expression_statement; 357, 358; 358, call; 358, 359; 358, 362; 359, attribute; 359, 360; 359, 361; 360, identifier:final_operands; 361, identifier:append; 362, argument_list; 362, 363; 363, call; 363, 364; 363, 365; 364, identifier:Operand; 365, argument_list; 365, 366; 365, 369; 366, attribute; 366, 367; 366, 368; 367, identifier:OperandTypes; 368, identifier:LOCAL_INDEX; 369, subscript; 369, 370; 369, 380; 370, call; 370, 371; 370, 372; 371, identifier:unpack; 372, argument_list; 372, 373; 372, 374; 373, string:'>H'; 374, call; 374, 375; 374, 378; 375, attribute; 375, 376; 375, 377; 376, identifier:fio; 377, identifier:read; 378, argument_list; 378, 379; 379, integer:2; 380, integer:0; 381, comment; 382, if_statement; 382, 383; 382, 386; 383, comparison_operator:==; 383, 384; 383, 385; 384, identifier:real_op; 385, integer:0x84; 386, block; 386, 387; 387, expression_statement; 387, 388; 388, call; 388, 389; 388, 392; 389, attribute; 389, 390; 389, 391; 390, identifier:final_operands; 391, identifier:append; 392, argument_list; 392, 393; 393, call; 393, 394; 393, 395; 394, identifier:Operand; 395, argument_list; 395, 396; 395, 399; 396, attribute; 396, 397; 396, 398; 397, identifier:OperandTypes; 398, identifier:LITERAL; 399, subscript; 399, 400; 399, 410; 400, call; 400, 401; 400, 402; 401, identifier:unpack; 402, argument_list; 402, 403; 402, 404; 403, string:'>H'; 404, call; 404, 405; 404, 408; 405, attribute; 405, 406; 405, 407; 406, identifier:fio; 407, identifier:read; 408, argument_list; 408, 409; 409, integer:2; 410, integer:0; 411, return_statement; 411, 412; 412, call; 412, 413; 412, 414; 413, identifier:Instruction; 414, argument_list; 414, 415; 414, 416; 414, 417; 414, 418; 415, identifier:name; 416, identifier:op; 417, identifier:final_operands; 418, identifier:start_pos | def read_instruction(fio, start_pos):
"""
Reads a single instruction from `fio` and returns it, or ``None`` if
the stream is empty.
:param fio: Any file-like object providing ``read()``.
:param start_pos: The current position in the stream.
"""
op = fio.read(1)
if not op:
return None
op = ord(op)
ins = opcode_table[op]
operands = ins['operands']
name = ins['mnemonic']
final_operands = []
# Most opcodes have simple operands.
if operands:
for fmt, type_ in operands:
final_operands.append(
Operand(
type_,
fmt.value.unpack(fio.read(fmt.value.size))[0]
)
)
# Special case for lookupswitch.
elif op == 0xAB:
# Get rid of the alignment padding.
padding = 4 - (start_pos + 1) % 4
padding = padding if padding != 4 else 0
fio.read(padding)
# Default branch address and branch count.
default, npairs = unpack('>ii', fio.read(8))
pairs = {}
for _ in repeat(None, npairs):
match, offset = unpack('>ii', fio.read(8))
pairs[match] = offset
final_operands.append(pairs)
final_operands.append(Operand(OperandTypes.BRANCH, default))
# Special case for tableswitch
elif op == 0xAA:
# Get rid of the alignment padding.
padding = 4 - (start_pos + 1) % 4
padding = padding if padding != 4 else 0
fio.read(padding)
default, low, high = unpack('>iii', fio.read(12))
final_operands.append(Operand(OperandTypes.BRANCH, default))
final_operands.append(Operand(OperandTypes.LITERAL, low))
final_operands.append(Operand(OperandTypes.LITERAL, high))
for _ in repeat(None, high - low + 1):
offset = unpack('>i', fio.read(4))[0]
final_operands.append(Operand(OperandTypes.BRANCH, offset))
# Special case for the wide prefix
elif op == 0xC4:
real_op = unpack('>B', fio.read(1))[0]
ins = opcode_table[real_op]
name = ins['mnemonic']
final_operands.append(Operand(
OperandTypes.LOCAL_INDEX,
unpack('>H', fio.read(2))[0]
))
# Further special case for iinc.
if real_op == 0x84:
final_operands.append(Operand(
OperandTypes.LITERAL,
unpack('>H', fio.read(2))[0]
))
return Instruction(name, op, final_operands, start_pos) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort_by_type; 3, parameters; 3, 4; 4, identifier:file_list; 5, block; 5, 6; 5, 8; 5, 15; 5, 47; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:ret_dict; 11, call; 11, 12; 11, 13; 12, identifier:defaultdict; 13, argument_list; 13, 14; 14, identifier:list; 15, for_statement; 15, 16; 15, 17; 15, 18; 16, identifier:filepath; 17, identifier:file_list; 18, block; 18, 19; 18, 32; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 24; 21, pattern_list; 21, 22; 21, 23; 22, identifier:_; 23, identifier:ext; 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:splitext; 30, argument_list; 30, 31; 31, identifier:filepath; 32, expression_statement; 32, 33; 33, call; 33, 34; 33, 45; 34, attribute; 34, 35; 34, 44; 35, subscript; 35, 36; 35, 37; 36, identifier:ret_dict; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:ext; 40, identifier:replace; 41, argument_list; 41, 42; 41, 43; 42, string:'.'; 43, string:''; 44, identifier:append; 45, argument_list; 45, 46; 46, identifier:filepath; 47, return_statement; 47, 48; 48, identifier:ret_dict | def sort_by_type(file_list):
"""
Sorts a list of files into types.
:param file_list: List of file paths.
:return: {extension: [<list of file paths with that extension>]}
"""
ret_dict = defaultdict(list)
for filepath in file_list:
_, ext = os.path.splitext(filepath)
ret_dict[ext.replace('.', '')].append(filepath)
return ret_dict |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_check_triple; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:triple; 6, block; 6, 7; 6, 9; 6, 16; 6, 42; 6, 46; 6, 55; 6, 64; 6, 85; 6, 97; 6, 116; 6, 148; 6, 157; 6, 171; 6, 172; 6, 173; 6, 174; 6, 175; 6, 176; 6, 188; 6, 195; 6, 206; 6, 218; 6, 230; 6, 242; 6, 243; 6, 254; 6, 261; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 15; 11, pattern_list; 11, 12; 11, 13; 11, 14; 12, identifier:subj; 13, identifier:pred; 14, identifier:obj; 15, identifier:triple; 16, if_statement; 16, 17; 16, 23; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:_should_ignore_predicate; 21, argument_list; 21, 22; 22, identifier:pred; 23, block; 23, 24; 23, 41; 24, expression_statement; 24, 25; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:log; 28, identifier:info; 29, argument_list; 29, 30; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, string:"Ignoring triple with predicate '{}'"; 33, identifier:format; 34, argument_list; 34, 35; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:_field_name_from_uri; 39, argument_list; 39, 40; 40, identifier:pred; 41, return_statement; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:classes; 45, list:[]; 46, expression_statement; 46, 47; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:log; 50, identifier:warning; 51, argument_list; 51, 52; 52, binary_operator:%; 52, 53; 52, 54; 53, string:"Possible member %s found"; 54, identifier:pred; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:pred; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:_expand_qname; 62, argument_list; 62, 63; 63, identifier:pred; 64, if_statement; 64, 65; 64, 75; 65, comparison_operator:not; 65, 66; 65, 72; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:self; 69, identifier:_namespace_from_uri; 70, argument_list; 70, 71; 71, identifier:pred; 72, attribute; 72, 73; 72, 74; 73, identifier:self; 74, identifier:allowed_namespaces; 75, block; 75, 76; 75, 84; 76, expression_statement; 76, 77; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:log; 80, identifier:info; 81, argument_list; 81, 82; 81, 83; 82, string:"Member %s does not use an allowed namespace"; 83, identifier:pred; 84, return_statement; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:instanceof; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:_is_instance; 92, argument_list; 92, 93; 93, tuple; 93, 94; 93, 95; 93, 96; 94, identifier:subj; 95, identifier:pred; 96, identifier:obj; 97, if_statement; 97, 98; 97, 106; 98, comparison_operator:==; 98, 99; 98, 103; 99, call; 99, 100; 99, 101; 100, identifier:type; 101, argument_list; 101, 102; 102, identifier:instanceof; 103, attribute; 103, 104; 103, 105; 104, identifier:rt; 105, identifier:URIRef; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:instanceof; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:self; 113, identifier:_expand_qname; 114, argument_list; 114, 115; 115, identifier:instanceof; 116, if_statement; 116, 117; 116, 132; 117, boolean_operator:and; 117, 118; 117, 125; 117, 126; 118, call; 118, 119; 118, 120; 119, identifier:hasattr; 120, argument_list; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:self; 123, identifier:schema_def; 124, string:"attributes_by_class"; 125, line_continuation:\; 126, not_operator; 126, 127; 127, attribute; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:schema_def; 131, identifier:attributes_by_class; 132, block; 132, 133; 132, 140; 133, expression_statement; 133, 134; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:log; 137, identifier:info; 138, argument_list; 138, 139; 139, string:"Parsed ontology not found. Parsing..."; 140, expression_statement; 140, 141; 141, call; 141, 142; 141, 147; 142, attribute; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:self; 145, identifier:schema_def; 146, identifier:parse_ontology; 147, argument_list; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:class_invalid; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:self; 154, identifier:_validate_class; 155, argument_list; 155, 156; 156, identifier:instanceof; 157, if_statement; 157, 158; 157, 159; 158, identifier:class_invalid; 159, block; 159, 160; 159, 169; 160, expression_statement; 160, 161; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:log; 164, identifier:warning; 165, argument_list; 165, 166; 166, binary_operator:%; 166, 167; 166, 168; 167, string:"Invalid class %s"; 168, identifier:instanceof; 169, return_statement; 169, 170; 170, identifier:class_invalid; 171, comment; 172, comment; 173, comment; 174, comment; 175, comment; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:classes; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:self; 182, identifier:_superclasses_for_subject; 183, argument_list; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:self; 186, identifier:graph; 187, identifier:instanceof; 188, expression_statement; 188, 189; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:classes; 192, identifier:append; 193, argument_list; 193, 194; 194, identifier:instanceof; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:member_invalid; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:self; 201, identifier:_validate_member; 202, argument_list; 202, 203; 202, 204; 202, 205; 203, identifier:pred; 204, identifier:classes; 205, identifier:instanceof; 206, if_statement; 206, 207; 206, 208; 207, identifier:member_invalid; 208, block; 208, 209; 208, 216; 209, expression_statement; 209, 210; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:log; 213, identifier:warning; 214, argument_list; 214, 215; 215, string:"Invalid member of class"; 216, return_statement; 216, 217; 217, identifier:member_invalid; 218, expression_statement; 218, 219; 219, assignment; 219, 220; 219, 221; 220, identifier:dupe_invalid; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:self; 224, identifier:_validate_duplication; 225, argument_list; 225, 226; 225, 229; 226, tuple; 226, 227; 226, 228; 227, identifier:subj; 228, identifier:pred; 229, identifier:instanceof; 230, if_statement; 230, 231; 230, 232; 231, identifier:dupe_invalid; 232, block; 232, 233; 232, 240; 233, expression_statement; 233, 234; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:log; 237, identifier:warning; 238, argument_list; 238, 239; 239, string:"Duplication found"; 240, return_statement; 240, 241; 241, identifier:dupe_invalid; 242, comment; 243, expression_statement; 243, 244; 244, call; 244, 245; 244, 250; 245, attribute; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:self; 248, identifier:checked_attributes; 249, identifier:append; 250, argument_list; 250, 251; 251, tuple; 251, 252; 251, 253; 252, identifier:subj; 253, identifier:pred; 254, expression_statement; 254, 255; 255, call; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:log; 258, identifier:warning; 259, argument_list; 259, 260; 260, string:"successfully validated triple, no errors"; 261, return_statement | def _check_triple(self, triple):
"""compare triple to ontology, return error or None"""
subj, pred, obj = triple
if self._should_ignore_predicate(pred):
log.info("Ignoring triple with predicate '{}'"
.format(self._field_name_from_uri(pred)))
return
classes = []
log.warning("Possible member %s found" % pred)
pred = self._expand_qname(pred)
if self._namespace_from_uri(pred) not in self.allowed_namespaces:
log.info("Member %s does not use an allowed namespace", pred)
return
instanceof = self._is_instance((subj, pred, obj))
if type(instanceof) == rt.URIRef:
instanceof = self._expand_qname(instanceof)
if hasattr(self.schema_def, "attributes_by_class") and \
not self.schema_def.attributes_by_class:
log.info("Parsed ontology not found. Parsing...")
self.schema_def.parse_ontology()
class_invalid = self._validate_class(instanceof)
if class_invalid:
log.warning("Invalid class %s" % instanceof)
return class_invalid
# TODO - the above sometimes fails when a single object has more than
# one rdfa type (eg <span property="schema:creator rnews:creator"
# typeof="schema:Person rnews:Person">
# Graph chooses the type in an arbitrary order, so it's unreliable
# eg: http://semanticweb.com/the-impact-of-rdfa_b35003
classes = self._superclasses_for_subject(self.graph, instanceof)
classes.append(instanceof)
member_invalid = self._validate_member(pred, classes, instanceof)
if member_invalid:
log.warning("Invalid member of class")
return member_invalid
dupe_invalid = self._validate_duplication((subj, pred), instanceof)
if dupe_invalid:
log.warning("Duplication found")
return dupe_invalid
# collect a list of checked attributes
self.checked_attributes.append((subj, pred))
log.warning("successfully validated triple, no errors")
return |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:parse_ontology; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 16; 5, 29; 5, 161; 5, 174; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:start; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:time; 14, identifier:clock; 15, argument_list; 16, expression_statement; 16, 17; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:log; 20, identifier:info; 21, argument_list; 21, 22; 22, binary_operator:%; 22, 23; 22, 24; 23, string:"Parsing ontology file for %s"; 24, attribute; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:__class__; 28, identifier:__name__; 29, for_statement; 29, 30; 29, 34; 29, 39; 30, pattern_list; 30, 31; 30, 32; 30, 33; 31, identifier:subj; 32, identifier:pred; 33, identifier:obj; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:_schema_nodes; 38, argument_list; 39, block; 39, 40; 39, 83; 39, 91; 39, 114; 40, if_statement; 40, 41; 40, 46; 41, comparison_operator:not; 41, 42; 41, 43; 42, identifier:subj; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:attributes_by_class; 46, block; 46, 47; 47, if_statement; 47, 48; 47, 74; 48, boolean_operator:and; 48, 49; 48, 61; 48, 62; 49, comparison_operator:==; 49, 50; 49, 51; 50, identifier:obj; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:rt; 54, identifier:URIRef; 55, argument_list; 55, 56; 56, subscript; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:lexicon; 60, string:'class'; 61, line_continuation:\; 62, comparison_operator:==; 62, 63; 62, 64; 63, identifier:pred; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:rt; 67, identifier:URIRef; 68, argument_list; 68, 69; 69, subscript; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:lexicon; 73, string:'type'; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 82; 77, subscript; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:self; 80, identifier:attributes_by_class; 81, identifier:subj; 82, list:[]; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:leaves; 86, list:[(subj, pred, obj)]; 86, 87; 87, tuple; 87, 88; 87, 89; 87, 90; 88, identifier:subj; 89, identifier:pred; 90, identifier:obj; 91, if_statement; 91, 92; 91, 100; 92, comparison_operator:==; 92, 93; 92, 97; 93, call; 93, 94; 93, 95; 94, identifier:type; 95, argument_list; 95, 96; 96, identifier:obj; 97, attribute; 97, 98; 97, 99; 98, identifier:rt; 99, identifier:BNode; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:leaves; 104, call; 104, 105; 104, 106; 105, identifier:deepest_node; 106, argument_list; 106, 107; 106, 111; 107, tuple; 107, 108; 107, 109; 107, 110; 108, identifier:subj; 109, identifier:pred; 110, identifier:obj; 111, attribute; 111, 112; 111, 113; 112, identifier:self; 113, identifier:graph; 114, for_statement; 114, 115; 114, 119; 114, 120; 115, pattern_list; 115, 116; 115, 117; 115, 118; 116, identifier:s; 117, identifier:p; 118, identifier:o; 119, identifier:leaves; 120, block; 120, 121; 120, 136; 121, if_statement; 121, 122; 121, 127; 122, comparison_operator:not; 122, 123; 122, 124; 123, identifier:o; 124, attribute; 124, 125; 124, 126; 125, identifier:self; 126, identifier:attributes_by_class; 127, block; 127, 128; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 135; 130, subscript; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:attributes_by_class; 134, identifier:o; 135, list:[]; 136, if_statement; 136, 137; 136, 149; 137, comparison_operator:==; 137, 138; 137, 139; 138, identifier:pred; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:rt; 142, identifier:URIRef; 143, argument_list; 143, 144; 144, subscript; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:self; 147, identifier:lexicon; 148, string:'domain'; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, call; 151, 152; 151, 159; 152, attribute; 152, 153; 152, 158; 153, subscript; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:attributes_by_class; 157, identifier:o; 158, identifier:append; 159, argument_list; 159, 160; 160, identifier:subj; 161, if_statement; 161, 162; 161, 166; 162, not_operator; 162, 163; 163, attribute; 163, 164; 163, 165; 164, identifier:self; 165, identifier:attributes_by_class; 166, block; 166, 167; 167, expression_statement; 167, 168; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:log; 171, identifier:info; 172, argument_list; 172, 173; 173, string:"No nodes found in ontology"; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:log; 178, identifier:info; 179, argument_list; 179, 180; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, string:"Ontology parsing complete in {}"; 183, identifier:format; 184, argument_list; 184, 185; 185, binary_operator:*; 185, 186; 185, 194; 186, parenthesized_expression; 186, 187; 187, binary_operator:-; 187, 188; 187, 193; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:time; 191, identifier:clock; 192, argument_list; 193, identifier:start; 194, integer:1000 | def parse_ontology(self):
"""place the ontology graph into a set of custom data structures
for use by the validator"""
start = time.clock()
log.info("Parsing ontology file for %s" % self.__class__.__name__)
for subj, pred, obj in self._schema_nodes():
if subj not in self.attributes_by_class:
if obj == rt.URIRef(self.lexicon['class']) and \
pred == rt.URIRef(self.lexicon['type']):
self.attributes_by_class[subj] = []
leaves = [(subj, pred, obj)]
if type(obj) == rt.BNode:
leaves = deepest_node((subj, pred, obj), self.graph)
for s, p, o in leaves:
if o not in self.attributes_by_class:
self.attributes_by_class[o] = []
if pred == rt.URIRef(self.lexicon['domain']):
self.attributes_by_class[o].append(subj)
if not self.attributes_by_class:
log.info("No nodes found in ontology")
log.info("Ontology parsing complete in {}".format(
(time.clock() - start) * 1000)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:new_frame_event; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 28; 5, 29; 5, 33; 5, 111; 5, 202; 5, 203; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, for_statement; 9, 10; 9, 11; 9, 18; 10, identifier:out_pool; 11, call; 11, 12; 11, 17; 12, attribute; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:outframe_pool; 16, identifier:values; 17, argument_list; 18, block; 18, 19; 19, if_statement; 19, 20; 19, 26; 20, not_operator; 20, 21; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:out_pool; 24, identifier:available; 25, argument_list; 26, block; 26, 27; 27, return_statement; 28, comment; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:frame_nos; 32, dictionary; 33, for_statement; 33, 34; 33, 35; 33, 42; 34, identifier:in_buff; 35, call; 35, 36; 35, 41; 36, attribute; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:input_buffer; 40, identifier:values; 41, argument_list; 42, block; 42, 43; 42, 52; 42, 60; 42, 69; 43, if_statement; 43, 44; 43, 50; 44, not_operator; 44, 45; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:in_buff; 48, identifier:available; 49, argument_list; 50, block; 50, 51; 51, return_statement; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:in_frame; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:in_buff; 58, identifier:peek; 59, argument_list; 60, if_statement; 60, 61; 60, 64; 61, comparison_operator:is; 61, 62; 61, 63; 62, identifier:in_frame; 63, None; 64, block; 64, 65; 65, raise_statement; 65, 66; 66, call; 66, 67; 66, 68; 67, identifier:StopIteration; 68, argument_list; 69, if_statement; 69, 70; 69, 75; 69, 84; 70, comparison_operator:>=; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:in_frame; 73, identifier:frame_no; 74, integer:0; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 81; 78, subscript; 78, 79; 78, 80; 79, identifier:frame_nos; 80, identifier:in_buff; 81, attribute; 81, 82; 81, 83; 82, identifier:in_frame; 83, identifier:frame_no; 84, else_clause; 84, 85; 84, 86; 85, comment; 86, block; 86, 87; 87, while_statement; 87, 88; 87, 104; 88, boolean_operator:and; 88, 89; 88, 96; 89, comparison_operator:>; 89, 90; 89, 95; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:in_buff; 93, identifier:available; 94, argument_list; 95, integer:1; 96, comparison_operator:is; 96, 97; 96, 103; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:in_buff; 100, identifier:peek; 101, argument_list; 101, 102; 102, integer:1; 103, None; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:in_buff; 109, identifier:get; 110, argument_list; 111, if_statement; 111, 112; 111, 118; 112, comparison_operator:>; 112, 113; 112, 117; 113, call; 113, 114; 113, 115; 114, identifier:len; 115, argument_list; 115, 116; 116, identifier:frame_nos; 117, integer:1; 118, block; 118, 119; 118, 130; 118, 131; 118, 181; 118, 182; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:frame_no; 122, call; 122, 123; 122, 124; 123, identifier:max; 124, argument_list; 124, 125; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:frame_nos; 128, identifier:values; 129, argument_list; 130, comment; 131, for_statement; 131, 132; 131, 133; 131, 134; 132, identifier:in_buff; 133, identifier:frame_nos; 134, block; 134, 135; 135, while_statement; 135, 136; 135, 149; 136, boolean_operator:and; 136, 137; 136, 142; 137, comparison_operator:<; 137, 138; 137, 141; 138, subscript; 138, 139; 138, 140; 139, identifier:frame_nos; 140, identifier:in_buff; 141, identifier:frame_no; 142, comparison_operator:>; 142, 143; 142, 148; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:in_buff; 146, identifier:available; 147, argument_list; 148, integer:1; 149, block; 149, 150; 149, 156; 149, 164; 149, 173; 150, expression_statement; 150, 151; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:in_buff; 154, identifier:get; 155, argument_list; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:in_frame; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:in_buff; 162, identifier:peek; 163, argument_list; 164, if_statement; 164, 165; 164, 168; 165, comparison_operator:is; 165, 166; 165, 167; 166, identifier:in_frame; 167, None; 168, block; 168, 169; 169, raise_statement; 169, 170; 170, call; 170, 171; 170, 172; 171, identifier:StopIteration; 172, argument_list; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 178; 175, subscript; 175, 176; 175, 177; 176, identifier:frame_nos; 177, identifier:in_buff; 178, attribute; 178, 179; 178, 180; 179, identifier:in_frame; 180, identifier:frame_no; 181, comment; 182, if_statement; 182, 183; 182, 200; 183, comparison_operator:!=; 183, 184; 183, 192; 184, call; 184, 185; 184, 186; 185, identifier:min; 186, argument_list; 186, 187; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:frame_nos; 190, identifier:values; 191, argument_list; 192, call; 192, 193; 192, 194; 193, identifier:max; 194, argument_list; 194, 195; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:frame_nos; 198, identifier:values; 199, argument_list; 200, block; 200, 201; 201, return_statement; 202, comment; 203, try_statement; 203, 204; 203, 211; 203, 215; 204, block; 204, 205; 205, expression_statement; 205, 206; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:self; 209, identifier:process_frame; 210, argument_list; 211, except_clause; 211, 212; 211, 213; 212, identifier:StopIteration; 213, block; 213, 214; 214, raise_statement; 215, except_clause; 215, 216; 215, 220; 216, as_pattern; 216, 217; 216, 218; 217, identifier:Exception; 218, as_pattern_target; 218, 219; 219, identifier:ex; 220, block; 220, 221; 220, 230; 221, expression_statement; 221, 222; 222, call; 222, 223; 222, 228; 223, attribute; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:self; 226, identifier:logger; 227, identifier:exception; 228, argument_list; 228, 229; 229, identifier:ex; 230, raise_statement; 230, 231; 231, call; 231, 232; 231, 233; 232, identifier:StopIteration; 233, argument_list | def new_frame_event(self):
"""Called by the event loop when a new input or output frame is
available.
Inputs are correlated by comparing their frame numbers. If there
is a complete set of inputs, and all output frame pools are
ready, the :py:meth:`process_frame` method is called.
If an input frame has a negative frame number it is not
correlated with other inputs, it is merely required to exist.
This allows frame objects to be used as control inputs when
processing video sequences. The derived class should use the
input buffer's :py:meth:`~InputBuffer.peek` method to get the
frame without removing it from the buffer. See the
:py:class:`~pyctools.components.colourspace.matrix.Matrix`
component for an example.
"""
# check output frames are available
for out_pool in self.outframe_pool.values():
if not out_pool.available():
return
# check input frames are available, and get current frame numbers
frame_nos = {}
for in_buff in self.input_buffer.values():
if not in_buff.available():
return
in_frame = in_buff.peek()
if in_frame is None:
raise StopIteration()
if in_frame.frame_no >= 0:
frame_nos[in_buff] = in_frame.frame_no
else:
# discard any superseded 'static' input
while in_buff.available() > 1 and in_buff.peek(1) is not None:
in_buff.get()
if len(frame_nos) > 1:
frame_no = max(frame_nos.values())
# discard old frames that can never be used
for in_buff in frame_nos:
while frame_nos[in_buff] < frame_no and in_buff.available() > 1:
in_buff.get()
in_frame = in_buff.peek()
if in_frame is None:
raise StopIteration()
frame_nos[in_buff] = in_frame.frame_no
# check for complete set of matching frame numbers
if min(frame_nos.values()) != max(frame_nos.values()):
return
# now have a full set of correlated inputs to process
try:
self.process_frame()
except StopIteration:
raise
except Exception as ex:
self.logger.exception(ex)
raise StopIteration() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:GaussianFilterCore; 3, parameters; 3, 4; 3, 7; 4, default_parameter; 4, 5; 4, 6; 5, identifier:x_sigma; 6, float:0.0; 7, default_parameter; 7, 8; 7, 9; 8, identifier:y_sigma; 9, float:0.0; 10, block; 10, 11; 10, 13; 10, 140; 10, 148; 10, 156; 10, 163; 10, 170; 10, 195; 10, 231; 10, 237; 10, 243; 10, 249; 10, 260; 10, 264; 10, 276; 10, 288; 10, 298; 11, expression_statement; 11, 12; 12, comment; 13, function_definition; 13, 14; 13, 15; 13, 17; 14, function_name:filter_1D; 15, parameters; 15, 16; 16, identifier:sigma; 17, block; 17, 18; 17, 34; 17, 38; 17, 42; 17, 76; 17, 85; 17, 104; 17, 129; 17, 130; 17, 138; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:alpha; 21, binary_operator:/; 21, 22; 21, 23; 22, float:1.0; 23, parenthesized_expression; 23, 24; 24, binary_operator:*; 24, 25; 24, 26; 25, float:2.0; 26, parenthesized_expression; 26, 27; 27, binary_operator:**; 27, 28; 27, 33; 28, call; 28, 29; 28, 30; 29, identifier:max; 30, argument_list; 30, 31; 30, 32; 31, identifier:sigma; 32, float:0.0001; 33, float:2.0; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:coefs; 37, list:[]; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:coef; 41, float:1.0; 42, while_statement; 42, 43; 42, 46; 43, comparison_operator:>; 43, 44; 43, 45; 44, identifier:coef; 45, float:0.0001; 46, block; 46, 47; 46, 54; 47, expression_statement; 47, 48; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:coefs; 51, identifier:append; 52, argument_list; 52, 53; 53, identifier:coef; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:coef; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:math; 60, identifier:exp; 61, argument_list; 61, 62; 62, unary_operator:-; 62, 63; 63, parenthesized_expression; 63, 64; 64, binary_operator:*; 64, 65; 64, 66; 65, identifier:alpha; 66, parenthesized_expression; 66, 67; 67, call; 67, 68; 67, 69; 68, identifier:float; 69, argument_list; 69, 70; 70, binary_operator:**; 70, 71; 70, 75; 71, call; 71, 72; 71, 73; 72, identifier:len; 73, argument_list; 73, 74; 74, identifier:coefs; 75, integer:2; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:fil_dim; 79, binary_operator:-; 79, 80; 79, 84; 80, call; 80, 81; 80, 82; 81, identifier:len; 82, argument_list; 82, 83; 83, identifier:coefs; 84, integer:1; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:result; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:numpy; 91, identifier:zeros; 92, argument_list; 92, 93; 92, 99; 93, binary_operator:+; 93, 94; 93, 95; 94, integer:1; 95, parenthesized_expression; 95, 96; 96, binary_operator:*; 96, 97; 96, 98; 97, identifier:fil_dim; 98, integer:2; 99, keyword_argument; 99, 100; 99, 101; 100, identifier:dtype; 101, attribute; 101, 102; 101, 103; 102, identifier:numpy; 103, identifier:float32; 104, for_statement; 104, 105; 104, 108; 104, 112; 105, pattern_list; 105, 106; 105, 107; 106, identifier:n; 107, identifier:coef; 108, call; 108, 109; 108, 110; 109, identifier:enumerate; 110, argument_list; 110, 111; 111, identifier:coefs; 112, block; 112, 113; 112, 121; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 120; 115, subscript; 115, 116; 115, 117; 116, identifier:result; 117, binary_operator:-; 117, 118; 117, 119; 118, identifier:fil_dim; 119, identifier:n; 120, identifier:coef; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 128; 123, subscript; 123, 124; 123, 125; 124, identifier:result; 125, binary_operator:+; 125, 126; 125, 127; 126, identifier:fil_dim; 127, identifier:n; 128, identifier:coef; 129, comment; 130, expression_statement; 130, 131; 131, augmented_assignment:/=; 131, 132; 131, 133; 132, identifier:result; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:result; 136, identifier:sum; 137, argument_list; 138, return_statement; 138, 139; 139, identifier:result; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:x_sigma; 143, call; 143, 144; 143, 145; 144, identifier:max; 145, argument_list; 145, 146; 145, 147; 146, identifier:x_sigma; 147, float:0.0; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:y_sigma; 151, call; 151, 152; 151, 153; 152, identifier:max; 153, argument_list; 153, 154; 153, 155; 154, identifier:y_sigma; 155, float:0.0; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:x_fil; 159, call; 159, 160; 159, 161; 160, identifier:filter_1D; 161, argument_list; 161, 162; 162, identifier:x_sigma; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:y_fil; 166, call; 166, 167; 166, 168; 167, identifier:filter_1D; 168, argument_list; 168, 169; 169, identifier:y_sigma; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:result; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:numpy; 176, identifier:empty; 177, argument_list; 177, 178; 177, 190; 178, list:[y_fil.shape[0], x_fil.shape[0], 1]; 178, 179; 178, 184; 178, 189; 179, subscript; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:y_fil; 182, identifier:shape; 183, integer:0; 184, subscript; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:x_fil; 187, identifier:shape; 188, integer:0; 189, integer:1; 190, keyword_argument; 190, 191; 190, 192; 191, identifier:dtype; 192, attribute; 192, 193; 192, 194; 193, identifier:numpy; 194, identifier:float32; 195, for_statement; 195, 196; 195, 197; 195, 205; 196, identifier:y; 197, call; 197, 198; 197, 199; 198, identifier:range; 199, argument_list; 199, 200; 200, subscript; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:y_fil; 203, identifier:shape; 204, integer:0; 205, block; 205, 206; 206, for_statement; 206, 207; 206, 208; 206, 216; 207, identifier:x; 208, call; 208, 209; 208, 210; 209, identifier:range; 210, argument_list; 210, 211; 211, subscript; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:x_fil; 214, identifier:shape; 215, integer:0; 216, block; 216, 217; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 224; 219, subscript; 219, 220; 219, 221; 219, 222; 219, 223; 220, identifier:result; 221, identifier:y; 222, identifier:x; 223, integer:0; 224, binary_operator:*; 224, 225; 224, 228; 225, subscript; 225, 226; 225, 227; 226, identifier:x_fil; 227, identifier:x; 228, subscript; 228, 229; 228, 230; 229, identifier:y_fil; 230, identifier:y; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:out_frame; 234, call; 234, 235; 234, 236; 235, identifier:Frame; 236, argument_list; 237, expression_statement; 237, 238; 238, assignment; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:out_frame; 241, identifier:data; 242, identifier:result; 243, expression_statement; 243, 244; 244, assignment; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:out_frame; 247, identifier:type; 248, string:'fil'; 249, expression_statement; 249, 250; 250, assignment; 250, 251; 250, 252; 251, identifier:audit; 252, call; 252, 253; 252, 258; 253, attribute; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:out_frame; 256, identifier:metadata; 257, identifier:get; 258, argument_list; 258, 259; 259, string:'audit'; 260, expression_statement; 260, 261; 261, augmented_assignment:+=; 261, 262; 261, 263; 262, identifier:audit; 263, string:'data = GaussianFilter()\n'; 264, if_statement; 264, 265; 264, 268; 265, comparison_operator:!=; 265, 266; 265, 267; 266, identifier:x_sigma; 267, float:0.0; 268, block; 268, 269; 269, expression_statement; 269, 270; 270, augmented_assignment:+=; 270, 271; 270, 272; 271, identifier:audit; 272, binary_operator:%; 272, 273; 272, 274; 273, string:' x_sigma: %g\n'; 274, parenthesized_expression; 274, 275; 275, identifier:x_sigma; 276, if_statement; 276, 277; 276, 280; 277, comparison_operator:!=; 277, 278; 277, 279; 278, identifier:y_sigma; 279, float:0.0; 280, block; 280, 281; 281, expression_statement; 281, 282; 282, augmented_assignment:+=; 282, 283; 282, 284; 283, identifier:audit; 284, binary_operator:%; 284, 285; 284, 286; 285, string:' y_sigma: %g\n'; 286, parenthesized_expression; 286, 287; 287, identifier:y_sigma; 288, expression_statement; 288, 289; 289, call; 289, 290; 289, 295; 290, attribute; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:out_frame; 293, identifier:metadata; 294, identifier:set; 295, argument_list; 295, 296; 295, 297; 296, string:'audit'; 297, identifier:audit; 298, return_statement; 298, 299; 299, identifier:out_frame | def GaussianFilterCore(x_sigma=0.0, y_sigma=0.0):
"""Gaussian filter generator core.
Alternative to the :py:class:`GaussianFilter` component that can be
used to make a non-reconfigurable resizer::
resize = Resize()
resize.filter(GaussianFilterCore(x_sigma=1.5))
...
start(..., resize, ...)
...
:keyword float x_sigma: Horizontal standard deviation parameter.
:keyword float y_sigma: Vertical standard deviation parameter.
:return: A :py:class:`~pyctools.core.frame.Frame` object containing the
filter.
"""
def filter_1D(sigma):
alpha = 1.0 / (2.0 * (max(sigma, 0.0001) ** 2.0))
coefs = []
coef = 1.0
while coef > 0.0001:
coefs.append(coef)
coef = math.exp(-(alpha * (float(len(coefs) ** 2))))
fil_dim = len(coefs) - 1
result = numpy.zeros(1 + (fil_dim * 2), dtype=numpy.float32)
for n, coef in enumerate(coefs):
result[fil_dim - n] = coef
result[fil_dim + n] = coef
# normalise result
result /= result.sum()
return result
x_sigma = max(x_sigma, 0.0)
y_sigma = max(y_sigma, 0.0)
x_fil = filter_1D(x_sigma)
y_fil = filter_1D(y_sigma)
result = numpy.empty(
[y_fil.shape[0], x_fil.shape[0], 1], dtype=numpy.float32)
for y in range(y_fil.shape[0]):
for x in range(x_fil.shape[0]):
result[y, x, 0] = x_fil[x] * y_fil[y]
out_frame = Frame()
out_frame.data = result
out_frame.type = 'fil'
audit = out_frame.metadata.get('audit')
audit += 'data = GaussianFilter()\n'
if x_sigma != 0.0:
audit += ' x_sigma: %g\n' % (x_sigma)
if y_sigma != 0.0:
audit += ' y_sigma: %g\n' % (y_sigma)
out_frame.metadata.set('audit', audit)
return out_frame |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:either; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 10; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, comment; 10, if_statement; 10, 11; 10, 17; 10, 26; 11, not_operator; 11, 12; 12, call; 12, 13; 12, 14; 13, identifier:hasattr; 14, argument_list; 14, 15; 14, 16; 15, identifier:self; 16, string:'children'; 17, block; 17, 18; 18, return_statement; 18, 19; 19, call; 19, 20; 19, 21; 20, identifier:Either; 21, argument_list; 21, 22; 22, call; 22, 23; 22, 24; 23, identifier:Required; 24, argument_list; 24, 25; 25, identifier:self; 26, else_clause; 26, 27; 27, block; 27, 28; 27, 32; 27, 38; 27, 266; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:ret; 31, list:[]; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:groups; 35, list:[[self]]; 35, 36; 36, list:[self]; 36, 37; 37, identifier:self; 38, while_statement; 38, 39; 38, 40; 39, identifier:groups; 40, block; 40, 41; 40, 50; 40, 61; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:children; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:groups; 47, identifier:pop; 48, argument_list; 48, 49; 49, integer:0; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:types; 53, list_comprehension; 53, 54; 53, 58; 54, call; 54, 55; 54, 56; 55, identifier:type; 56, argument_list; 56, 57; 57, identifier:c; 58, for_in_clause; 58, 59; 58, 60; 59, identifier:c; 60, identifier:children; 61, if_statement; 61, 62; 61, 65; 61, 111; 61, 159; 61, 207; 61, 257; 62, comparison_operator:in; 62, 63; 62, 64; 63, identifier:Either; 64, identifier:types; 65, block; 65, 66; 65, 83; 65, 95; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:either; 69, subscript; 69, 70; 69, 82; 70, list_comprehension; 70, 71; 70, 72; 70, 75; 71, identifier:c; 72, for_in_clause; 72, 73; 72, 74; 73, identifier:c; 74, identifier:children; 75, if_clause; 75, 76; 76, comparison_operator:is; 76, 77; 76, 81; 77, call; 77, 78; 77, 79; 78, identifier:type; 79, argument_list; 79, 80; 80, identifier:c; 81, identifier:Either; 82, integer:0; 83, expression_statement; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:children; 87, identifier:pop; 88, argument_list; 88, 89; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:children; 92, identifier:index; 93, argument_list; 93, 94; 94, identifier:either; 95, for_statement; 95, 96; 95, 97; 95, 100; 96, identifier:c; 97, attribute; 97, 98; 97, 99; 98, identifier:either; 99, identifier:children; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:groups; 105, identifier:append; 106, argument_list; 106, 107; 107, binary_operator:+; 107, 108; 107, 110; 108, list:[c]; 108, 109; 109, identifier:c; 110, identifier:children; 111, elif_clause; 111, 112; 111, 115; 112, comparison_operator:in; 112, 113; 112, 114; 113, identifier:Required; 114, identifier:types; 115, block; 115, 116; 115, 133; 115, 145; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:required; 119, subscript; 119, 120; 119, 132; 120, list_comprehension; 120, 121; 120, 122; 120, 125; 121, identifier:c; 122, for_in_clause; 122, 123; 122, 124; 123, identifier:c; 124, identifier:children; 125, if_clause; 125, 126; 126, comparison_operator:is; 126, 127; 126, 131; 127, call; 127, 128; 127, 129; 128, identifier:type; 129, argument_list; 129, 130; 130, identifier:c; 131, identifier:Required; 132, integer:0; 133, expression_statement; 133, 134; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:children; 137, identifier:pop; 138, argument_list; 138, 139; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:children; 142, identifier:index; 143, argument_list; 143, 144; 144, identifier:required; 145, expression_statement; 145, 146; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:groups; 149, identifier:append; 150, argument_list; 150, 151; 151, binary_operator:+; 151, 152; 151, 158; 152, call; 152, 153; 152, 154; 153, identifier:list; 154, argument_list; 154, 155; 155, attribute; 155, 156; 155, 157; 156, identifier:required; 157, identifier:children; 158, identifier:children; 159, elif_clause; 159, 160; 159, 163; 160, comparison_operator:in; 160, 161; 160, 162; 161, identifier:Optional; 162, identifier:types; 163, block; 163, 164; 163, 181; 163, 193; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:optional; 167, subscript; 167, 168; 167, 180; 168, list_comprehension; 168, 169; 168, 170; 168, 173; 169, identifier:c; 170, for_in_clause; 170, 171; 170, 172; 171, identifier:c; 172, identifier:children; 173, if_clause; 173, 174; 174, comparison_operator:is; 174, 175; 174, 179; 175, call; 175, 176; 175, 177; 176, identifier:type; 177, argument_list; 177, 178; 178, identifier:c; 179, identifier:Optional; 180, integer:0; 181, expression_statement; 181, 182; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:children; 185, identifier:pop; 186, argument_list; 186, 187; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:children; 190, identifier:index; 191, argument_list; 191, 192; 192, identifier:optional; 193, expression_statement; 193, 194; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:groups; 197, identifier:append; 198, argument_list; 198, 199; 199, binary_operator:+; 199, 200; 199, 206; 200, call; 200, 201; 200, 202; 201, identifier:list; 202, argument_list; 202, 203; 203, attribute; 203, 204; 203, 205; 204, identifier:optional; 205, identifier:children; 206, identifier:children; 207, elif_clause; 207, 208; 207, 211; 208, comparison_operator:in; 208, 209; 208, 210; 209, identifier:OneOrMore; 210, identifier:types; 211, block; 211, 212; 211, 229; 211, 241; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:oneormore; 215, subscript; 215, 216; 215, 228; 216, list_comprehension; 216, 217; 216, 218; 216, 221; 217, identifier:c; 218, for_in_clause; 218, 219; 218, 220; 219, identifier:c; 220, identifier:children; 221, if_clause; 221, 222; 222, comparison_operator:is; 222, 223; 222, 227; 223, call; 223, 224; 223, 225; 224, identifier:type; 225, argument_list; 225, 226; 226, identifier:c; 227, identifier:OneOrMore; 228, integer:0; 229, expression_statement; 229, 230; 230, call; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:children; 233, identifier:pop; 234, argument_list; 234, 235; 235, call; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:children; 238, identifier:index; 239, argument_list; 239, 240; 240, identifier:oneormore; 241, expression_statement; 241, 242; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:groups; 245, identifier:append; 246, argument_list; 246, 247; 247, binary_operator:+; 247, 248; 247, 256; 248, binary_operator:*; 248, 249; 248, 255; 249, call; 249, 250; 249, 251; 250, identifier:list; 251, argument_list; 251, 252; 252, attribute; 252, 253; 252, 254; 253, identifier:oneormore; 254, identifier:children; 255, integer:2; 256, identifier:children; 257, else_clause; 257, 258; 258, block; 258, 259; 259, expression_statement; 259, 260; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:ret; 263, identifier:append; 264, argument_list; 264, 265; 265, identifier:children; 266, return_statement; 266, 267; 267, call; 267, 268; 267, 269; 268, identifier:Either; 269, argument_list; 269, 270; 270, list_splat; 270, 271; 271, list_comprehension; 271, 272; 271, 277; 272, call; 272, 273; 272, 274; 273, identifier:Required; 274, argument_list; 274, 275; 275, list_splat; 275, 276; 276, identifier:e; 277, for_in_clause; 277, 278; 277, 279; 278, identifier:e; 279, identifier:ret | def either(self):
"""Transform pattern into an equivalent, with only top-level Either."""
# Currently the pattern will not be equivalent, but more "narrow",
# although good enough to reason about list arguments.
if not hasattr(self, 'children'):
return Either(Required(self))
else:
ret = []
groups = [[self]]
while groups:
children = groups.pop(0)
types = [type(c) for c in children]
if Either in types:
either = [c for c in children if type(c) is Either][0]
children.pop(children.index(either))
for c in either.children:
groups.append([c] + children)
elif Required in types:
required = [c for c in children if type(c) is Required][0]
children.pop(children.index(required))
groups.append(list(required.children) + children)
elif Optional in types:
optional = [c for c in children if type(c) is Optional][0]
children.pop(children.index(optional))
groups.append(list(optional.children) + children)
elif OneOrMore in types:
oneormore = [c for c in children if type(c) is OneOrMore][0]
children.pop(children.index(oneormore))
groups.append(list(oneormore.children) * 2 + children)
else:
ret.append(children)
return Either(*[Required(*e) for e in ret]) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:TranslateManagedObject; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:mObj; 5, identifier:xlateOrg; 6, identifier:xlateMap; 7, block; 7, 8; 7, 10; 7, 17; 7, 22; 7, 30; 7, 41; 7, 204; 7, 362; 8, expression_statement; 8, 9; 9, comment; 10, import_from_statement; 10, 11; 10, 13; 10, 15; 11, dotted_name; 11, 12; 12, identifier:UcsBase; 13, dotted_name; 13, 14; 14, identifier:UcsUtils; 15, dotted_name; 15, 16; 16, identifier:WriteUcsWarning; 17, import_from_statement; 17, 18; 17, 20; 18, dotted_name; 18, 19; 19, identifier:Mos; 20, dotted_name; 20, 21; 21, identifier:OrgOrg; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:xMO; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:mObj; 28, identifier:Clone; 29, argument_list; 30, expression_statement; 30, 31; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:xMO; 34, identifier:SetHandle; 35, argument_list; 35, 36; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:mObj; 39, identifier:GetHandle; 40, argument_list; 41, if_statement; 41, 42; 41, 46; 42, parenthesized_expression; 42, 43; 43, comparison_operator:!=; 43, 44; 43, 45; 44, identifier:xlateOrg; 45, None; 46, block; 46, 47; 46, 59; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:matchObj; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:re; 53, identifier:match; 54, argument_list; 54, 55; 54, 56; 55, string:r'^(org-[\-\.:_a-zA-Z0-9]{1,16}/)*org-[\-\.:_a-zA-Z0-9]{1,16}'; 56, attribute; 56, 57; 56, 58; 57, identifier:xMO; 58, identifier:Dn; 59, if_statement; 59, 60; 59, 61; 60, identifier:matchObj; 61, block; 61, 62; 62, if_statement; 62, 63; 62, 77; 62, 171; 63, comparison_operator:==; 63, 64; 63, 72; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:UcsUtils; 67, identifier:WordL; 68, argument_list; 68, 69; 69, attribute; 69, 70; 69, 71; 70, identifier:xMO; 71, identifier:classId; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:OrgOrg; 75, identifier:ClassId; 76, argument_list; 77, block; 77, 78; 77, 97; 77, 115; 77, 116; 77, 128; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:orgMoMeta; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:UcsUtils; 84, identifier:GetUcsPropertyMeta; 85, argument_list; 85, 86; 85, 96; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:UcsUtils; 89, identifier:WordU; 90, argument_list; 90, 91; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:OrgOrg; 94, identifier:ClassId; 95, argument_list; 96, string:"Meta"; 97, if_statement; 97, 98; 97, 101; 97, 102; 98, comparison_operator:==; 98, 99; 98, 100; 99, identifier:orgMoMeta; 100, None; 101, comment; 102, block; 102, 103; 102, 113; 103, expression_statement; 103, 104; 104, call; 104, 105; 104, 106; 105, identifier:WriteUcsWarning; 106, argument_list; 106, 107; 107, binary_operator:%; 107, 108; 107, 109; 108, string:'[Warning]: Could not translate [%s]'; 109, parenthesized_expression; 109, 110; 110, attribute; 110, 111; 110, 112; 111, identifier:xMO; 112, identifier:Dn; 113, return_statement; 113, 114; 114, identifier:xMO; 115, comment; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:matchObj1; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:re; 122, identifier:findall; 123, argument_list; 123, 124; 123, 125; 124, string:r'(\[[^\]]+\])'; 125, attribute; 125, 126; 125, 127; 126, identifier:orgMoMeta; 127, identifier:rn; 128, if_statement; 128, 129; 128, 130; 128, 138; 129, identifier:matchObj1; 130, block; 130, 131; 131, expression_statement; 131, 132; 132, call; 132, 133; 132, 134; 133, identifier:UpdateMoDnAlongWithNamingProperties; 134, argument_list; 134, 135; 134, 136; 134, 137; 135, identifier:xMO; 136, identifier:orgMoMeta; 137, identifier:xlateOrg; 138, else_clause; 138, 139; 139, block; 139, 140; 139, 164; 139, 165; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:newDn; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:re; 146, identifier:sub; 147, argument_list; 147, 148; 147, 157; 147, 161; 148, binary_operator:%; 148, 149; 148, 150; 149, string:"%s"; 150, parenthesized_expression; 150, 151; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:matchObj; 154, identifier:group; 155, argument_list; 155, 156; 156, integer:0; 157, binary_operator:%; 157, 158; 157, 159; 158, string:"%s"; 159, parenthesized_expression; 159, 160; 160, identifier:xlateOrg; 161, attribute; 161, 162; 161, 163; 162, identifier:xMO; 163, identifier:Dn; 164, comment; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:xMO; 169, identifier:Dn; 170, identifier:newDn; 171, else_clause; 171, 172; 172, block; 172, 173; 172, 197; 172, 198; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:newDn; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:re; 179, identifier:sub; 180, argument_list; 180, 181; 180, 190; 180, 194; 181, binary_operator:%; 181, 182; 181, 183; 182, string:"^%s/"; 183, parenthesized_expression; 183, 184; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:matchObj; 187, identifier:group; 188, argument_list; 188, 189; 189, integer:0; 190, binary_operator:%; 190, 191; 190, 192; 191, string:"%s/"; 192, parenthesized_expression; 192, 193; 193, identifier:xlateOrg; 194, attribute; 194, 195; 194, 196; 195, identifier:xMO; 196, identifier:Dn; 197, comment; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:xMO; 202, identifier:Dn; 203, identifier:newDn; 204, if_statement; 204, 205; 204, 209; 205, parenthesized_expression; 205, 206; 206, comparison_operator:!=; 206, 207; 206, 208; 207, identifier:xlateMap; 208, None; 209, block; 209, 210; 209, 216; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 213; 212, identifier:originalDn; 213, attribute; 213, 214; 213, 215; 214, identifier:xMO; 215, identifier:Dn; 216, if_statement; 216, 217; 216, 220; 216, 290; 217, comparison_operator:in; 217, 218; 217, 219; 218, identifier:originalDn; 219, identifier:xlateMap; 220, block; 220, 221; 220, 238; 220, 254; 220, 255; 220, 267; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:xMoMeta; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:UcsUtils; 227, identifier:GetUcsPropertyMeta; 228, argument_list; 228, 229; 228, 237; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:UcsUtils; 232, identifier:WordU; 233, argument_list; 233, 234; 234, attribute; 234, 235; 234, 236; 235, identifier:xMO; 236, identifier:classId; 237, string:"Meta"; 238, if_statement; 238, 239; 238, 242; 238, 243; 239, comparison_operator:==; 239, 240; 239, 241; 240, identifier:xMoMeta; 241, None; 242, comment; 243, block; 243, 244; 243, 252; 244, expression_statement; 244, 245; 245, call; 245, 246; 245, 247; 246, identifier:WriteUcsWarning; 247, argument_list; 247, 248; 248, binary_operator:%; 248, 249; 248, 250; 249, string:'[Warning]: Could not translate [%s]'; 250, parenthesized_expression; 250, 251; 251, identifier:originalDn; 252, return_statement; 252, 253; 253, identifier:xMO; 254, comment; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 258; 257, identifier:matchObj; 258, call; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:re; 261, identifier:findall; 262, argument_list; 262, 263; 262, 264; 263, string:r'(\[[^\]]+\])'; 264, attribute; 264, 265; 264, 266; 265, identifier:xMoMeta; 266, identifier:rn; 267, if_statement; 267, 268; 267, 269; 267, 279; 268, identifier:matchObj; 269, block; 269, 270; 270, expression_statement; 270, 271; 271, call; 271, 272; 271, 273; 272, identifier:UpdateMoDnAlongWithNamingProperties; 273, argument_list; 273, 274; 273, 275; 273, 276; 274, identifier:xMO; 275, identifier:xMoMeta; 276, subscript; 276, 277; 276, 278; 277, identifier:xlateMap; 278, identifier:originalDn; 279, else_clause; 279, 280; 279, 281; 280, comment; 281, block; 281, 282; 282, expression_statement; 282, 283; 283, assignment; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:xMO; 286, identifier:Dn; 287, subscript; 287, 288; 287, 289; 288, identifier:xlateMap; 289, identifier:originalDn; 290, else_clause; 290, 291; 291, block; 291, 292; 291, 303; 292, expression_statement; 292, 293; 293, assignment; 293, 294; 293, 295; 294, identifier:originalDn; 295, call; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:re; 298, identifier:sub; 299, argument_list; 299, 300; 299, 301; 299, 302; 300, string:r'[/]*[^/]+$'; 301, string:''; 302, identifier:originalDn; 303, while_statement; 303, 304; 303, 312; 304, parenthesized_expression; 304, 305; 305, boolean_operator:or; 305, 306; 305, 309; 306, comparison_operator:!=; 306, 307; 306, 308; 307, identifier:originalDn; 308, None; 309, comparison_operator:==; 309, 310; 309, 311; 310, identifier:originalDn; 311, string:""; 312, block; 312, 313; 312, 333; 312, 354; 312, 355; 312, 361; 313, if_statement; 313, 314; 313, 320; 314, parenthesized_expression; 314, 315; 315, not_operator; 315, 316; 316, parenthesized_expression; 316, 317; 317, comparison_operator:in; 317, 318; 317, 319; 318, identifier:originalDn; 319, identifier:xlateMap; 320, block; 320, 321; 320, 332; 321, expression_statement; 321, 322; 322, assignment; 322, 323; 322, 324; 323, identifier:originalDn; 324, call; 324, 325; 324, 328; 325, attribute; 325, 326; 325, 327; 326, identifier:re; 327, identifier:sub; 328, argument_list; 328, 329; 328, 330; 328, 331; 329, string:r'[/]*[^/]+$'; 330, string:''; 331, identifier:originalDn; 332, continue_statement; 333, expression_statement; 333, 334; 334, assignment; 334, 335; 334, 336; 335, identifier:newDn; 336, call; 336, 337; 336, 340; 337, attribute; 337, 338; 337, 339; 338, identifier:re; 339, identifier:sub; 340, argument_list; 340, 341; 340, 345; 340, 351; 341, binary_operator:%; 341, 342; 341, 343; 342, string:"^%s/"; 343, parenthesized_expression; 343, 344; 344, identifier:originalDn; 345, binary_operator:%; 345, 346; 345, 347; 346, string:"%s/"; 347, parenthesized_expression; 347, 348; 348, subscript; 348, 349; 348, 350; 349, identifier:xlateMap; 350, identifier:originalDn; 351, attribute; 351, 352; 351, 353; 352, identifier:xMO; 353, identifier:Dn; 354, comment; 355, expression_statement; 355, 356; 356, assignment; 356, 357; 356, 360; 357, attribute; 357, 358; 357, 359; 358, identifier:xMO; 359, identifier:Dn; 360, identifier:newDn; 361, break_statement; 362, return_statement; 362, 363; 363, identifier:xMO | def TranslateManagedObject(mObj, xlateOrg, xlateMap):
""" Method used to translate a managedobject. This method is used in CompareManagedObject. """
from UcsBase import UcsUtils, WriteUcsWarning
from Mos import OrgOrg
xMO = mObj.Clone()
xMO.SetHandle(mObj.GetHandle())
if (xlateOrg != None):
matchObj = re.match(r'^(org-[\-\.:_a-zA-Z0-9]{1,16}/)*org-[\-\.:_a-zA-Z0-9]{1,16}', xMO.Dn)
if matchObj:
if UcsUtils.WordL(xMO.classId) == OrgOrg.ClassId():
orgMoMeta = UcsUtils.GetUcsPropertyMeta(UcsUtils.WordU(OrgOrg.ClassId()), "Meta")
if orgMoMeta == None:
# TODO: Add Warning/Error messages in Logger.
WriteUcsWarning('[Warning]: Could not translate [%s]' % (xMO.Dn))
return xMO
# Check for naming property
matchObj1 = re.findall(r'(\[[^\]]+\])', orgMoMeta.rn)
if matchObj1:
UpdateMoDnAlongWithNamingProperties(xMO, orgMoMeta, xlateOrg)
else:
newDn = re.sub("%s" % (matchObj.group(0)), "%s" % (xlateOrg), xMO.Dn)
# print "Translating", xMO.Dn, " => ", newDn
xMO.Dn = newDn
else:
newDn = re.sub("^%s/" % (matchObj.group(0)), "%s/" % (xlateOrg), xMO.Dn)
# print "Translating", xMO.Dn, " => ", newDn
xMO.Dn = newDn
if (xlateMap != None):
originalDn = xMO.Dn
if originalDn in xlateMap:
xMoMeta = UcsUtils.GetUcsPropertyMeta(UcsUtils.WordU(xMO.classId), "Meta")
if xMoMeta == None:
# TODO: Add Warning/Error messages in Logger.
WriteUcsWarning('[Warning]: Could not translate [%s]' % (originalDn))
return xMO
# Check for naming property
matchObj = re.findall(r'(\[[^\]]+\])', xMoMeta.rn)
if matchObj:
UpdateMoDnAlongWithNamingProperties(xMO, xMoMeta, xlateMap[originalDn])
else:
# print "Translating", xMO.Dn, " => ", xlateMap[originalDn]
xMO.Dn = xlateMap[originalDn]
else:
originalDn = re.sub(r'[/]*[^/]+$', '', originalDn)
while (originalDn != None or originalDn == ""):
if (not (originalDn in xlateMap)):
originalDn = re.sub(r'[/]*[^/]+$', '', originalDn)
continue
newDn = re.sub("^%s/" % (originalDn), "%s/" % (xlateMap[originalDn]), xMO.Dn)
# print "Translating", xMO.Dn, " => ", newDn
xMO.Dn = newDn
break
return xMO |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:ImportUcsSession; 3, parameters; 3, 4; 3, 5; 4, identifier:filePath; 5, identifier:key; 6, block; 6, 7; 6, 9; 6, 18; 6, 19; 6, 29; 6, 30; 6, 40; 6, 41; 6, 70; 6, 71; 6, 84; 6, 90; 6, 91; 6, 106; 7, expression_statement; 7, 8; 8, comment; 9, import_from_statement; 9, 10; 9, 12; 9, 14; 9, 16; 10, dotted_name; 10, 11; 11, identifier:UcsBase; 12, dotted_name; 12, 13; 13, identifier:UcsUtils; 14, dotted_name; 14, 15; 15, identifier:WriteUcsWarning; 16, dotted_name; 16, 17; 17, identifier:UcsValidationException; 18, comment; 19, if_statement; 19, 20; 19, 23; 20, comparison_operator:is; 20, 21; 20, 22; 21, identifier:filePath; 22, None; 23, block; 23, 24; 24, raise_statement; 24, 25; 25, call; 25, 26; 25, 27; 26, identifier:UcsValidationException; 27, argument_list; 27, 28; 28, string:"filePath parameter is not provided."; 29, comment; 30, if_statement; 30, 31; 30, 34; 31, comparison_operator:is; 31, 32; 31, 33; 32, identifier:key; 33, None; 34, block; 34, 35; 35, raise_statement; 35, 36; 36, call; 36, 37; 36, 38; 37, identifier:UcsValidationException; 38, argument_list; 38, 39; 39, string:"key parameter is not provided."; 40, comment; 41, if_statement; 41, 42; 41, 61; 42, boolean_operator:or; 42, 43; 42, 52; 43, not_operator; 43, 44; 44, call; 44, 45; 44, 50; 45, attribute; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:os; 48, identifier:path; 49, identifier:isfile; 50, argument_list; 50, 51; 51, identifier:filePath; 52, not_operator; 52, 53; 53, call; 53, 54; 53, 59; 54, attribute; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:os; 57, identifier:path; 58, identifier:exists; 59, argument_list; 59, 60; 60, identifier:filePath; 61, block; 61, 62; 62, raise_statement; 62, 63; 63, call; 63, 64; 63, 65; 64, identifier:UcsValidationException; 65, argument_list; 65, 66; 66, binary_operator:%; 66, 67; 66, 68; 67, string:'[Error]: File <%s> does not exist '; 68, parenthesized_expression; 68, 69; 69, identifier:filePath; 70, comment; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:doc; 74, call; 74, 75; 74, 82; 75, attribute; 75, 76; 75, 81; 76, attribute; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:xml; 79, identifier:dom; 80, identifier:minidom; 81, identifier:parse; 82, argument_list; 82, 83; 83, identifier:filePath; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:topNode; 87, attribute; 87, 88; 87, 89; 88, identifier:doc; 89, identifier:documentElement; 90, comment; 91, if_statement; 91, 92; 91, 103; 92, boolean_operator:or; 92, 93; 92, 96; 93, comparison_operator:is; 93, 94; 93, 95; 94, identifier:topNode; 95, None; 96, comparison_operator:!=; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:topNode; 99, identifier:localName; 100, attribute; 100, 101; 100, 102; 101, identifier:UcsLoginXml; 102, identifier:UCS_HANDLES; 103, block; 103, 104; 104, return_statement; 104, 105; 105, None; 106, if_statement; 106, 107; 106, 113; 106, 114; 106, 115; 107, parenthesized_expression; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:topNode; 111, identifier:hasChildNodes; 112, argument_list; 113, comment; 114, comment; 115, block; 115, 116; 115, 122; 115, 129; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:childList; 119, attribute; 119, 120; 119, 121; 120, identifier:topNode; 121, identifier:childNodes; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:childCount; 125, call; 125, 126; 125, 127; 126, identifier:len; 127, argument_list; 127, 128; 128, identifier:childList; 129, for_statement; 129, 130; 129, 131; 129, 135; 130, identifier:i; 131, call; 131, 132; 131, 133; 132, identifier:range; 133, argument_list; 133, 134; 134, identifier:childCount; 135, block; 135, 136; 135, 145; 135, 156; 135, 166; 135, 170; 135, 174; 135, 178; 135, 182; 135, 186; 135, 207; 135, 228; 135, 256; 135, 277; 135, 298; 135, 299; 135, 318; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:childNode; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:childList; 142, identifier:item; 143, argument_list; 143, 144; 144, identifier:i; 145, if_statement; 145, 146; 145, 154; 146, parenthesized_expression; 146, 147; 147, comparison_operator:!=; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:childNode; 150, identifier:nodeType; 151, attribute; 151, 152; 151, 153; 152, identifier:Node; 153, identifier:ELEMENT_NODE; 154, block; 154, 155; 155, continue_statement; 156, if_statement; 156, 157; 156, 164; 157, comparison_operator:!=; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:childNode; 160, identifier:localName; 161, attribute; 161, 162; 161, 163; 162, identifier:UcsLoginXml; 163, identifier:UCS; 164, block; 164, 165; 165, continue_statement; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:lName; 169, None; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:lUsername; 173, None; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:lPassword; 177, None; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:lNoSsl; 181, False; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:lPort; 185, None; 186, if_statement; 186, 187; 186, 195; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:childNode; 190, identifier:hasAttribute; 191, argument_list; 191, 192; 192, attribute; 192, 193; 192, 194; 193, identifier:UcsLoginXml; 194, identifier:NAME; 195, block; 195, 196; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 199; 198, identifier:lName; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:childNode; 202, identifier:getAttribute; 203, argument_list; 203, 204; 204, attribute; 204, 205; 204, 206; 205, identifier:UcsLoginXml; 206, identifier:NAME; 207, if_statement; 207, 208; 207, 216; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:childNode; 211, identifier:hasAttribute; 212, argument_list; 212, 213; 213, attribute; 213, 214; 213, 215; 214, identifier:UcsLoginXml; 215, identifier:USER_NAME; 216, block; 216, 217; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:lUsername; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:childNode; 223, identifier:getAttribute; 224, argument_list; 224, 225; 225, attribute; 225, 226; 225, 227; 226, identifier:UcsLoginXml; 227, identifier:USER_NAME; 228, if_statement; 228, 229; 228, 237; 228, 238; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:childNode; 232, identifier:hasAttribute; 233, argument_list; 233, 234; 234, attribute; 234, 235; 234, 236; 235, identifier:UcsLoginXml; 236, identifier:PASSWORD; 237, comment; 238, block; 238, 239; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 242; 241, identifier:lPassword; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:UcsUtils; 245, identifier:DecryptPassword; 246, argument_list; 246, 247; 246, 255; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:childNode; 250, identifier:getAttribute; 251, argument_list; 251, 252; 252, attribute; 252, 253; 252, 254; 253, identifier:UcsLoginXml; 254, identifier:PASSWORD; 255, identifier:key; 256, if_statement; 256, 257; 256, 265; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:childNode; 260, identifier:hasAttribute; 261, argument_list; 261, 262; 262, attribute; 262, 263; 262, 264; 263, identifier:UcsLoginXml; 264, identifier:NO_SSL; 265, block; 265, 266; 266, expression_statement; 266, 267; 267, assignment; 267, 268; 267, 269; 268, identifier:lNoSsl; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:childNode; 272, identifier:getAttribute; 273, argument_list; 273, 274; 274, attribute; 274, 275; 274, 276; 275, identifier:UcsLoginXml; 276, identifier:NO_SSL; 277, if_statement; 277, 278; 277, 286; 278, call; 278, 279; 278, 282; 279, attribute; 279, 280; 279, 281; 280, identifier:childNode; 281, identifier:hasAttribute; 282, argument_list; 282, 283; 283, attribute; 283, 284; 283, 285; 284, identifier:UcsLoginXml; 285, identifier:PORT; 286, block; 286, 287; 287, expression_statement; 287, 288; 288, assignment; 288, 289; 288, 290; 289, identifier:lPort; 290, call; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:childNode; 293, identifier:getAttribute; 294, argument_list; 294, 295; 295, attribute; 295, 296; 295, 297; 296, identifier:UcsLoginXml; 297, identifier:PORT; 298, comment; 299, if_statement; 299, 300; 299, 315; 299, 316; 300, parenthesized_expression; 300, 301; 301, boolean_operator:or; 301, 302; 301, 311; 302, boolean_operator:or; 302, 303; 302, 307; 303, parenthesized_expression; 303, 304; 304, comparison_operator:is; 304, 305; 304, 306; 305, identifier:lName; 306, None; 307, parenthesized_expression; 307, 308; 308, comparison_operator:==; 308, 309; 308, 310; 309, identifier:lUsername; 310, None; 311, parenthesized_expression; 311, 312; 312, comparison_operator:==; 312, 313; 312, 314; 313, identifier:lPassword; 314, None; 315, comment; 316, block; 316, 317; 317, continue_statement; 318, try_statement; 318, 319; 318, 347; 319, block; 319, 320; 319, 326; 320, expression_statement; 320, 321; 321, assignment; 321, 322; 321, 323; 322, identifier:handle; 323, call; 323, 324; 323, 325; 324, identifier:UcsHandle; 325, argument_list; 326, expression_statement; 326, 327; 327, call; 327, 328; 327, 331; 328, attribute; 328, 329; 328, 330; 329, identifier:handle; 330, identifier:Login; 331, argument_list; 331, 332; 331, 335; 331, 338; 331, 341; 331, 344; 332, keyword_argument; 332, 333; 332, 334; 333, identifier:name; 334, identifier:lName; 335, keyword_argument; 335, 336; 335, 337; 336, identifier:username; 337, identifier:lUsername; 338, keyword_argument; 338, 339; 338, 340; 339, identifier:password; 340, identifier:lPassword; 341, keyword_argument; 341, 342; 341, 343; 342, identifier:noSsl; 343, identifier:lNoSsl; 344, keyword_argument; 344, 345; 344, 346; 345, identifier:port; 346, identifier:lPort; 347, except_clause; 347, 348; 347, 349; 347, 350; 347, 351; 348, identifier:Exception; 349, identifier:err; 350, comment; 351, block; 351, 352; 352, expression_statement; 352, 353; 353, call; 353, 354; 353, 355; 354, identifier:WriteUcsWarning; 355, argument_list; 355, 356; 356, binary_operator:%; 356, 357; 356, 358; 357, string:"[Connection Error<%s>] %s"; 358, tuple; 358, 359; 358, 360; 359, identifier:lName; 360, call; 360, 361; 360, 362; 361, identifier:str; 362, argument_list; 362, 363; 363, identifier:err | def ImportUcsSession(filePath, key):
"""
This operation will do a login to each UCS which is present in credential file.
- filePath specifies the path of the credential file.
- key specifies string used for secure encryption while ExportUcsSession operation.
"""
from UcsBase import UcsUtils, WriteUcsWarning, UcsValidationException
# from p3 import p3_encrypt, p3_decrypt
if filePath is None:
raise UcsValidationException("filePath parameter is not provided.")
# raise Exception('[Error]: Please provide filePath')
if key is None:
raise UcsValidationException("key parameter is not provided.")
# raise Exception('[Error]: Please provide key')
if not os.path.isfile(filePath) or not os.path.exists(filePath):
raise UcsValidationException('[Error]: File <%s> does not exist ' % (filePath))
# raise Exception('[Error]: File <%s> does not exist ' %(filePath))
doc = xml.dom.minidom.parse(filePath)
topNode = doc.documentElement
# print topNode.localName
if topNode is None or topNode.localName != UcsLoginXml.UCS_HANDLES:
return None
if (topNode.hasChildNodes()):
# childList = topNode._get_childNodes()
# childCount = childList._get_length()
childList = topNode.childNodes
childCount = len(childList)
for i in range(childCount):
childNode = childList.item(i)
if (childNode.nodeType != Node.ELEMENT_NODE):
continue
if childNode.localName != UcsLoginXml.UCS:
continue
lName = None
lUsername = None
lPassword = None
lNoSsl = False
lPort = None
if childNode.hasAttribute(UcsLoginXml.NAME):
lName = childNode.getAttribute(UcsLoginXml.NAME)
if childNode.hasAttribute(UcsLoginXml.USER_NAME):
lUsername = childNode.getAttribute(UcsLoginXml.USER_NAME)
if childNode.hasAttribute(UcsLoginXml.PASSWORD):
# lPassword = p3_decrypt(childNode.getAttribute(UcsLoginXml.PASSWORD), key)
lPassword = UcsUtils.DecryptPassword(childNode.getAttribute(UcsLoginXml.PASSWORD), key)
if childNode.hasAttribute(UcsLoginXml.NO_SSL):
lNoSsl = childNode.getAttribute(UcsLoginXml.NO_SSL)
if childNode.hasAttribute(UcsLoginXml.PORT):
lPort = childNode.getAttribute(UcsLoginXml.PORT)
# Process Login
if ((lName is None) or (lUsername == None) or (lPassword == None)):
# WriteUcsWarning("[Warning] Insufficient information for login ...")
continue
try:
handle = UcsHandle()
handle.Login(name=lName, username=lUsername, password=lPassword, noSsl=lNoSsl, port=lPort)
except Exception, err:
# TODO: Add Warning/Error messages in Logger.
WriteUcsWarning("[Connection Error<%s>] %s" % (lName, str(err))) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:ImportUcsBackup; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:path; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:merge; 10, False; 11, default_parameter; 11, 12; 11, 13; 12, identifier:dumpXml; 13, False; 14, block; 14, 15; 14, 17; 14, 35; 14, 40; 14, 45; 14, 50; 14, 61; 14, 62; 14, 71; 14, 72; 14, 91; 14, 92; 14, 96; 14, 100; 14, 111; 14, 120; 14, 129; 14, 154; 14, 173; 14, 179; 14, 187; 14, 193; 14, 201; 14, 209; 14, 230; 14, 236; 14, 243; 14, 255; 14, 300; 14, 309; 14, 322; 14, 329; 14, 342; 14, 351; 14, 352; 14, 374; 14, 392; 14, 393; 15, expression_statement; 15, 16; 16, comment; 17, import_from_statement; 17, 18; 17, 20; 17, 22; 17, 24; 17, 26; 17, 28; 17, 30; 17, 32; 17, 33; 18, dotted_name; 18, 19; 19, identifier:UcsBase; 20, dotted_name; 20, 21; 21, identifier:WriteUcsWarning; 22, dotted_name; 22, 23; 23, identifier:UcsUtils; 24, dotted_name; 24, 25; 25, identifier:ManagedObject; 26, dotted_name; 26, 27; 27, identifier:WriteObject; 28, dotted_name; 28, 29; 29, identifier:UcsUtils; 30, dotted_name; 30, 31; 31, identifier:UcsException; 32, line_continuation:\; 33, dotted_name; 33, 34; 34, identifier:UcsValidationException; 35, import_from_statement; 35, 36; 35, 38; 36, dotted_name; 36, 37; 37, identifier:Ucs; 38, dotted_name; 38, 39; 39, identifier:ConfigConfig; 40, import_from_statement; 40, 41; 40, 43; 41, dotted_name; 41, 42; 42, identifier:Mos; 43, dotted_name; 43, 44; 44, identifier:MgmtImporter; 45, import_from_statement; 45, 46; 45, 48; 46, dotted_name; 46, 47; 47, identifier:datetime; 48, dotted_name; 48, 49; 49, identifier:datetime; 50, if_statement; 50, 51; 50, 55; 51, parenthesized_expression; 51, 52; 52, attribute; 52, 53; 52, 54; 53, identifier:self; 54, identifier:_transactionInProgress; 55, block; 55, 56; 56, raise_statement; 56, 57; 57, call; 57, 58; 57, 59; 58, identifier:UcsValidationException; 59, argument_list; 59, 60; 60, string:"UCS transaction in progress. Cannot execute ImportUcsBackup. Complete or Undo UCS transaction."; 61, comment; 62, if_statement; 62, 63; 62, 65; 63, not_operator; 63, 64; 64, identifier:path; 65, block; 65, 66; 66, raise_statement; 66, 67; 67, call; 67, 68; 67, 69; 68, identifier:UcsValidationException; 69, argument_list; 69, 70; 70, string:"path parameter is not provided."; 71, comment; 72, if_statement; 72, 73; 72, 82; 73, not_operator; 73, 74; 74, call; 74, 75; 74, 80; 75, attribute; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:os; 78, identifier:path; 79, identifier:exists; 80, argument_list; 80, 81; 81, identifier:path; 82, block; 82, 83; 83, raise_statement; 83, 84; 84, call; 84, 85; 84, 86; 85, identifier:UcsValidationException; 86, argument_list; 86, 87; 87, binary_operator:%; 87, 88; 87, 89; 88, string:"Backup File not found <%s>"; 89, parenthesized_expression; 89, 90; 90, identifier:path; 91, comment; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:dn; 95, None; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:filePath; 99, identifier:path; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:localFile; 103, call; 103, 104; 103, 109; 104, attribute; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:os; 107, identifier:path; 108, identifier:basename; 109, argument_list; 109, 110; 110, identifier:filePath; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:topSystem; 114, call; 114, 115; 114, 116; 115, identifier:ManagedObject; 116, argument_list; 116, 117; 117, attribute; 117, 118; 117, 119; 118, identifier:NamingId; 119, identifier:TOP_SYSTEM; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:mgmtImporter; 123, call; 123, 124; 123, 125; 124, identifier:ManagedObject; 125, argument_list; 125, 126; 126, attribute; 126, 127; 126, 128; 127, identifier:NamingId; 128, identifier:MGMT_IMPORTER; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:mgmtImporter; 133, identifier:Hostname; 134, binary_operator:+; 134, 135; 134, 144; 135, call; 135, 136; 135, 143; 136, attribute; 136, 137; 136, 142; 137, subscript; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:os; 140, identifier:environ; 141, string:'COMPUTERNAME'; 142, identifier:lower; 143, argument_list; 144, call; 144, 145; 144, 152; 145, attribute; 145, 146; 145, 151; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:datetime; 149, identifier:now; 150, argument_list; 151, identifier:strftime; 152, argument_list; 152, 153; 153, string:'%Y%m%d%H%M'; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:dn; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:UcsUtils; 160, identifier:MakeDn; 161, argument_list; 161, 162; 162, list:[topSystem.MakeRn(), mgmtImporter.MakeRn()]; 162, 163; 162, 168; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:topSystem; 166, identifier:MakeRn; 167, argument_list; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:mgmtImporter; 171, identifier:MakeRn; 172, argument_list; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:mgmtImporter; 177, identifier:Dn; 178, identifier:dn; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:mgmtImporter; 183, identifier:Status; 184, attribute; 184, 185; 184, 186; 185, identifier:Status; 186, identifier:CREATED; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:mgmtImporter; 191, identifier:RemoteFile; 192, identifier:filePath; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:mgmtImporter; 197, identifier:Proto; 198, attribute; 198, 199; 198, 200; 199, identifier:MgmtImporter; 200, identifier:CONST_PROTO_HTTP; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:mgmtImporter; 205, identifier:AdminState; 206, attribute; 206, 207; 206, 208; 207, identifier:MgmtImporter; 208, identifier:CONST_ADMIN_STATE_ENABLED; 209, if_statement; 209, 210; 209, 211; 209, 220; 210, identifier:merge; 211, block; 211, 212; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:mgmtImporter; 216, identifier:Action; 217, attribute; 217, 218; 217, 219; 218, identifier:MgmtImporter; 219, identifier:CONST_ACTION_MERGE; 220, else_clause; 220, 221; 221, block; 221, 222; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:mgmtImporter; 226, identifier:Action; 227, attribute; 227, 228; 227, 229; 228, identifier:MgmtImporter; 229, identifier:CONST_ACTION_REPLACE; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:inConfig; 233, call; 233, 234; 233, 235; 234, identifier:ConfigConfig; 235, argument_list; 236, expression_statement; 236, 237; 237, call; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:inConfig; 240, identifier:AddChild; 241, argument_list; 241, 242; 242, identifier:mgmtImporter; 243, expression_statement; 243, 244; 244, assignment; 244, 245; 244, 246; 245, identifier:uri; 246, binary_operator:%; 246, 247; 246, 248; 247, string:"%s/operations/file-%s/importconfig.txt"; 248, tuple; 248, 249; 248, 254; 249, call; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:self; 252, identifier:Uri; 253, argument_list; 254, identifier:localFile; 255, if_statement; 255, 256; 255, 263; 255, 280; 256, comparison_operator:<; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:sys; 259, identifier:version_info; 260, tuple; 260, 261; 260, 262; 261, integer:2; 262, integer:6; 263, block; 263, 264; 263, 272; 264, expression_statement; 264, 265; 265, assignment; 265, 266; 265, 267; 266, identifier:uploadFileHandle; 267, call; 267, 268; 267, 269; 268, identifier:open; 269, argument_list; 269, 270; 269, 271; 270, identifier:filePath; 271, string:'rb'; 272, expression_statement; 272, 273; 273, assignment; 273, 274; 273, 275; 274, identifier:stream; 275, call; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:uploadFileHandle; 278, identifier:read; 279, argument_list; 280, else_clause; 280, 281; 281, block; 281, 282; 281, 288; 282, expression_statement; 282, 283; 283, assignment; 283, 284; 283, 285; 284, identifier:progress; 285, call; 285, 286; 285, 287; 286, identifier:Progress; 287, argument_list; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 291; 290, identifier:stream; 291, call; 291, 292; 291, 293; 292, identifier:file_with_callback; 293, argument_list; 293, 294; 293, 295; 293, 296; 293, 299; 294, identifier:filePath; 295, string:'rb'; 296, attribute; 296, 297; 296, 298; 297, identifier:progress; 298, identifier:update; 299, identifier:filePath; 300, expression_statement; 300, 301; 301, assignment; 301, 302; 301, 303; 302, identifier:request; 303, call; 303, 304; 303, 307; 304, attribute; 304, 305; 304, 306; 305, identifier:urllib2; 306, identifier:Request; 307, argument_list; 307, 308; 308, identifier:uri; 309, expression_statement; 309, 310; 310, call; 310, 311; 310, 314; 311, attribute; 311, 312; 311, 313; 312, identifier:request; 313, identifier:add_header; 314, argument_list; 314, 315; 314, 316; 315, string:'Cookie'; 316, binary_operator:%; 316, 317; 316, 318; 317, string:'ucsm-cookie=%s'; 318, parenthesized_expression; 318, 319; 319, attribute; 319, 320; 319, 321; 320, identifier:self; 321, identifier:_cookie; 322, expression_statement; 322, 323; 323, call; 323, 324; 323, 327; 324, attribute; 324, 325; 324, 326; 325, identifier:request; 326, identifier:add_data; 327, argument_list; 327, 328; 328, identifier:stream; 329, expression_statement; 329, 330; 330, assignment; 330, 331; 330, 332; 331, identifier:response; 332, call; 332, 333; 332, 341; 333, attribute; 333, 334; 333, 340; 334, call; 334, 335; 334, 338; 335, attribute; 335, 336; 335, 337; 336, identifier:urllib2; 337, identifier:urlopen; 338, argument_list; 338, 339; 339, identifier:request; 340, identifier:read; 341, argument_list; 342, if_statement; 342, 343; 342, 345; 343, not_operator; 343, 344; 344, identifier:response; 345, block; 345, 346; 346, raise_statement; 346, 347; 347, call; 347, 348; 347, 349; 348, identifier:UcsValidationException; 349, argument_list; 349, 350; 350, string:"Unable to upload properly."; 351, comment; 352, expression_statement; 352, 353; 353, assignment; 353, 354; 353, 355; 354, identifier:ccm; 355, call; 355, 356; 355, 359; 356, attribute; 356, 357; 356, 358; 357, identifier:self; 358, identifier:ConfigConfMo; 359, argument_list; 359, 360; 359, 363; 359, 366; 359, 371; 360, keyword_argument; 360, 361; 360, 362; 361, identifier:dn; 362, identifier:dn; 363, keyword_argument; 363, 364; 363, 365; 364, identifier:inConfig; 365, identifier:inConfig; 366, keyword_argument; 366, 367; 366, 368; 367, identifier:inHierarchical; 368, attribute; 368, 369; 368, 370; 369, identifier:YesOrNo; 370, identifier:FALSE; 371, keyword_argument; 371, 372; 371, 373; 372, identifier:dumpXml; 373, identifier:dumpXml; 374, if_statement; 374, 375; 374, 381; 375, parenthesized_expression; 375, 376; 376, comparison_operator:!=; 376, 377; 376, 380; 377, attribute; 377, 378; 377, 379; 378, identifier:ccm; 379, identifier:errorCode; 380, integer:0; 381, block; 381, 382; 382, raise_statement; 382, 383; 383, call; 383, 384; 383, 385; 384, identifier:UcsException; 385, argument_list; 385, 386; 385, 389; 386, attribute; 386, 387; 386, 388; 387, identifier:ccm; 388, identifier:errorCode; 389, attribute; 389, 390; 389, 391; 390, identifier:ccm; 391, identifier:errorDescr; 392, comment; 393, return_statement; 393, 394; 394, call; 394, 395; 394, 400; 395, attribute; 395, 396; 395, 399; 396, attribute; 396, 397; 396, 398; 397, identifier:ccm; 398, identifier:OutConfig; 399, identifier:GetChild; 400, argument_list | def ImportUcsBackup(self, path=None, merge=False, dumpXml=False):
"""
Imports backUp.
This operation will upload the UCSM backup taken earlier via GUI or BackupUcs operation for all configuration, system configuration, and
logical configuration files. User can perform an import while the system is up and running.
- path specifies path of the backup file.
- merge specifies whether to merge the backup configuration with the existing UCSM configuration.
"""
from UcsBase import WriteUcsWarning, UcsUtils, ManagedObject, WriteObject, UcsUtils, UcsException, \
UcsValidationException
from Ucs import ConfigConfig
from Mos import MgmtImporter
from datetime import datetime
if (self._transactionInProgress):
raise UcsValidationException(
"UCS transaction in progress. Cannot execute ImportUcsBackup. Complete or Undo UCS transaction.")
# raise Exception("UCS transaction in progress. Cannot execute ImportUcsBackup. Complete or Undo UCS transaction.")
if not path:
raise UcsValidationException("path parameter is not provided.")
# raise Exception("Please provide path")
if not os.path.exists(path):
raise UcsValidationException("Backup File not found <%s>" % (path))
# raise Exception("Backup File not found <%s>" %(path))
dn = None
filePath = path
localFile = os.path.basename(filePath)
topSystem = ManagedObject(NamingId.TOP_SYSTEM)
mgmtImporter = ManagedObject(NamingId.MGMT_IMPORTER)
mgmtImporter.Hostname = os.environ['COMPUTERNAME'].lower() + datetime.now().strftime('%Y%m%d%H%M')
dn = UcsUtils.MakeDn([topSystem.MakeRn(), mgmtImporter.MakeRn()])
mgmtImporter.Dn = dn
mgmtImporter.Status = Status.CREATED
mgmtImporter.RemoteFile = filePath
mgmtImporter.Proto = MgmtImporter.CONST_PROTO_HTTP
mgmtImporter.AdminState = MgmtImporter.CONST_ADMIN_STATE_ENABLED
if merge:
mgmtImporter.Action = MgmtImporter.CONST_ACTION_MERGE
else:
mgmtImporter.Action = MgmtImporter.CONST_ACTION_REPLACE
inConfig = ConfigConfig()
inConfig.AddChild(mgmtImporter)
uri = "%s/operations/file-%s/importconfig.txt" % (self.Uri(), localFile)
if sys.version_info < (2, 6):
uploadFileHandle = open(filePath, 'rb')
stream = uploadFileHandle.read()
else:
progress = Progress()
stream = file_with_callback(filePath, 'rb', progress.update, filePath)
request = urllib2.Request(uri)
request.add_header('Cookie', 'ucsm-cookie=%s' % (self._cookie))
request.add_data(stream)
response = urllib2.urlopen(request).read()
if not response:
raise UcsValidationException("Unable to upload properly.")
# WriteUcsWarning("Unable to upload properly.")
ccm = self.ConfigConfMo(dn=dn, inConfig=inConfig, inHierarchical=YesOrNo.FALSE, dumpXml=dumpXml)
if (ccm.errorCode != 0):
raise UcsException(ccm.errorCode, ccm.errorDescr)
# raise Exception('[Error]: BackupUcs [Code]:' + ccm.errorCode + ' [Description]:' + ccm.errorDescr)
return ccm.OutConfig.GetChild() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_set_suffix_links; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 14; 6, expression_statement; 6, 7; 7, string:'''
Sets all suffix links in all nodes in this trie.
'''; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:_suffix_links_set; 13, True; 14, for_statement; 14, 15; 14, 18; 14, 23; 14, 24; 15, pattern_list; 15, 16; 15, 17; 16, identifier:current; 17, identifier:parent; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:bfs; 22, argument_list; 23, comment; 24, block; 24, 25; 24, 31; 24, 39; 24, 50; 24, 51; 24, 52; 24, 58; 24, 59; 24, 60; 24, 64; 24, 108; 24, 109; 24, 115; 24, 131; 25, if_statement; 25, 26; 25, 29; 26, comparison_operator:is; 26, 27; 26, 28; 27, identifier:parent; 28, None; 29, block; 29, 30; 30, continue_statement; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:current; 35, identifier:longest_prefix; 36, attribute; 36, 37; 36, 38; 37, identifier:parent; 38, identifier:longest_prefix; 39, if_statement; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:parent; 42, identifier:has_value; 43, block; 43, 44; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:current; 48, identifier:longest_prefix; 49, identifier:parent; 50, comment; 51, comment; 52, if_statement; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:current; 55, identifier:has_suffix; 56, block; 56, 57; 57, continue_statement; 58, comment; 59, comment; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:suffix; 63, identifier:parent; 64, while_statement; 64, 65; 64, 66; 65, True; 66, block; 66, 67; 66, 90; 67, if_statement; 67, 68; 67, 72; 67, 82; 68, not_operator; 68, 69; 69, attribute; 69, 70; 69, 71; 70, identifier:suffix; 71, identifier:has_suffix; 72, block; 72, 73; 72, 81; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:current; 77, identifier:suffix; 78, attribute; 78, 79; 78, 80; 79, identifier:self; 80, identifier:root; 81, break_statement; 82, else_clause; 82, 83; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:suffix; 87, attribute; 87, 88; 87, 89; 88, identifier:suffix; 89, identifier:suffix; 90, if_statement; 90, 91; 90, 96; 91, comparison_operator:in; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:current; 94, identifier:uplink; 95, identifier:suffix; 96, block; 96, 97; 96, 107; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:current; 101, identifier:suffix; 102, subscript; 102, 103; 102, 104; 103, identifier:suffix; 104, attribute; 104, 105; 104, 106; 105, identifier:current; 106, identifier:uplink; 107, break_statement; 108, comment; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:suffix; 112, attribute; 112, 113; 112, 114; 113, identifier:current; 114, identifier:suffix; 115, while_statement; 115, 116; 115, 124; 116, boolean_operator:and; 116, 117; 116, 121; 117, not_operator; 117, 118; 118, attribute; 118, 119; 118, 120; 119, identifier:suffix; 120, identifier:has_value; 121, attribute; 121, 122; 121, 123; 122, identifier:suffix; 123, identifier:has_suffix; 124, block; 124, 125; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:suffix; 128, attribute; 128, 129; 128, 130; 129, identifier:suffix; 130, identifier:suffix; 131, if_statement; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:suffix; 134, identifier:has_value; 135, block; 135, 136; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:current; 140, identifier:dict_suffix; 141, identifier:suffix | def _set_suffix_links(self):
'''
Sets all suffix links in all nodes in this trie.
'''
self._suffix_links_set = True
for current, parent in self.bfs():
# skip the root node
if parent is None:
continue
current.longest_prefix = parent.longest_prefix
if parent.has_value:
current.longest_prefix = parent
# the root doesn't get a suffix link
# also, skip previously set suffix links
if current.has_suffix:
continue
# current is not the root and has no suffix
# set current's suffix to parent's suffix
suffix = parent
while True:
if not suffix.has_suffix:
current.suffix = self.root
break
else:
suffix = suffix.suffix
if current.uplink in suffix:
current.suffix = suffix[current.uplink]
break
# now find the dict_suffix value
suffix = current.suffix
while not suffix.has_value and suffix.has_suffix:
suffix = suffix.suffix
if suffix.has_value:
current.dict_suffix = suffix |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_write_mo; 3, parameters; 3, 4; 4, identifier:mo; 5, block; 5, 6; 5, 8; 5, 9; 5, 13; 5, 30; 5, 34; 5, 38; 5, 76; 5, 87; 5, 220; 5, 274; 5, 301; 5, 305; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:classNotFound; 12, False; 13, if_statement; 13, 14; 13, 25; 14, parenthesized_expression; 14, 15; 15, comparison_operator:==; 15, 16; 15, 24; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:UcsUtils; 19, identifier:FindClassIdInMoMetaIgnoreCase; 20, argument_list; 20, 21; 21, attribute; 21, 22; 21, 23; 22, identifier:mo; 23, identifier:classId; 24, None; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:classNotFound; 29, True; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:tabsize; 33, integer:8; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:outstr; 37, string:"\n"; 38, if_statement; 38, 39; 38, 40; 38, 59; 39, identifier:classNotFound; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, augmented_assignment:+=; 42, 43; 42, 44; 43, identifier:outstr; 44, binary_operator:+; 44, 45; 44, 58; 45, binary_operator:+; 45, 46; 45, 47; 46, string:"Managed Object\t\t\t:\t"; 47, call; 47, 48; 47, 49; 48, identifier:str; 49, argument_list; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:UcsUtils; 53, identifier:WordU; 54, argument_list; 54, 55; 55, attribute; 55, 56; 55, 57; 56, identifier:mo; 57, identifier:classId; 58, string:"\n"; 59, else_clause; 59, 60; 60, block; 60, 61; 61, expression_statement; 61, 62; 62, augmented_assignment:+=; 62, 63; 62, 64; 63, identifier:outstr; 64, binary_operator:+; 64, 65; 64, 75; 65, binary_operator:+; 65, 66; 65, 67; 66, string:"Managed Object\t\t\t:\t"; 67, call; 67, 68; 67, 69; 68, identifier:str; 69, argument_list; 69, 70; 70, attribute; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:mo; 73, identifier:propMoMeta; 74, identifier:name; 75, string:"\n"; 76, expression_statement; 76, 77; 77, augmented_assignment:+=; 77, 78; 77, 79; 78, identifier:outstr; 79, binary_operator:+; 79, 80; 79, 86; 80, binary_operator:*; 80, 81; 80, 82; 81, string:"-"; 82, call; 82, 83; 82, 84; 83, identifier:len; 84, argument_list; 84, 85; 85, string:"Managed Object"; 86, string:"\n"; 87, if_statement; 87, 88; 87, 91; 87, 163; 88, parenthesized_expression; 88, 89; 89, not_operator; 89, 90; 90, identifier:classNotFound; 91, block; 91, 92; 92, for_statement; 92, 93; 92, 94; 92, 104; 93, identifier:prop; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:UcsUtils; 97, identifier:GetUcsPropertyMetaAttributeList; 98, argument_list; 98, 99; 99, attribute; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:mo; 102, identifier:propMoMeta; 103, identifier:name; 104, block; 104, 105; 104, 119; 104, 130; 104, 139; 104, 140; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:propMeta; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:UcsUtils; 111, identifier:GetUcsPropertyMeta; 112, argument_list; 112, 113; 112, 118; 113, attribute; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:mo; 116, identifier:propMoMeta; 117, identifier:name; 118, identifier:prop; 119, if_statement; 119, 120; 119, 128; 120, parenthesized_expression; 120, 121; 121, comparison_operator:==; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:propMeta; 124, identifier:access; 125, attribute; 125, 126; 125, 127; 126, identifier:UcsPropertyMeta; 127, identifier:Internal; 128, block; 128, 129; 129, continue_statement; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:val; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:mo; 136, identifier:getattr; 137, argument_list; 137, 138; 138, identifier:prop; 139, comment; 140, expression_statement; 140, 141; 141, augmented_assignment:+=; 141, 142; 141, 143; 142, identifier:outstr; 143, binary_operator:+; 143, 144; 143, 162; 144, binary_operator:+; 144, 145; 144, 158; 145, binary_operator:+; 145, 146; 145, 157; 146, call; 146, 147; 146, 153; 147, attribute; 147, 148; 147, 152; 148, call; 148, 149; 148, 150; 149, identifier:str; 150, argument_list; 150, 151; 151, identifier:prop; 152, identifier:ljust; 153, argument_list; 153, 154; 154, binary_operator:*; 154, 155; 154, 156; 155, identifier:tabsize; 156, integer:4; 157, string:':'; 158, call; 158, 159; 158, 160; 159, identifier:str; 160, argument_list; 160, 161; 161, identifier:val; 162, string:"\n"; 163, else_clause; 163, 164; 164, block; 164, 165; 165, for_statement; 165, 166; 165, 167; 165, 170; 166, identifier:prop; 167, attribute; 167, 168; 167, 169; 168, identifier:mo; 169, identifier:__dict__; 170, block; 170, 171; 170, 184; 170, 192; 171, if_statement; 171, 172; 171, 182; 172, parenthesized_expression; 172, 173; 173, comparison_operator:in; 173, 174; 173, 175; 174, identifier:prop; 175, list:['classId', 'XtraProperty', 'handle', 'propMoMeta', 'dirtyMask', 'child']; 175, 176; 175, 177; 175, 178; 175, 179; 175, 180; 175, 181; 176, string:'classId'; 177, string:'XtraProperty'; 178, string:'handle'; 179, string:'propMoMeta'; 180, string:'dirtyMask'; 181, string:'child'; 182, block; 182, 183; 183, continue_statement; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:val; 187, subscript; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:mo; 190, identifier:__dict__; 191, identifier:prop; 192, expression_statement; 192, 193; 193, augmented_assignment:+=; 193, 194; 193, 195; 194, identifier:outstr; 195, binary_operator:+; 195, 196; 195, 219; 196, binary_operator:+; 196, 197; 196, 215; 197, binary_operator:+; 197, 198; 197, 214; 198, call; 198, 199; 198, 210; 199, attribute; 199, 200; 199, 209; 200, call; 200, 201; 200, 202; 201, identifier:str; 202, argument_list; 202, 203; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:UcsUtils; 206, identifier:WordU; 207, argument_list; 207, 208; 208, identifier:prop; 209, identifier:ljust; 210, argument_list; 210, 211; 211, binary_operator:*; 211, 212; 211, 213; 212, identifier:tabsize; 213, integer:4; 214, string:':'; 215, call; 215, 216; 215, 217; 216, identifier:str; 217, argument_list; 217, 218; 218, identifier:val; 219, string:"\n"; 220, if_statement; 220, 221; 220, 229; 221, call; 221, 222; 221, 227; 222, attribute; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:mo; 225, identifier:__dict__; 226, identifier:has_key; 227, argument_list; 227, 228; 228, string:'XtraProperty'; 229, block; 229, 230; 230, for_statement; 230, 231; 230, 232; 230, 237; 231, identifier:xtraProp; 232, subscript; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:mo; 235, identifier:__dict__; 236, string:'XtraProperty'; 237, block; 237, 238; 238, expression_statement; 238, 239; 239, augmented_assignment:+=; 239, 240; 239, 241; 240, identifier:outstr; 241, binary_operator:+; 241, 242; 241, 273; 242, binary_operator:+; 242, 243; 242, 263; 243, binary_operator:+; 243, 244; 243, 262; 244, binary_operator:+; 244, 245; 244, 246; 245, string:'[X]'; 246, call; 246, 247; 246, 258; 247, attribute; 247, 248; 247, 257; 248, call; 248, 249; 248, 250; 249, identifier:str; 250, argument_list; 250, 251; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:UcsUtils; 254, identifier:WordU; 255, argument_list; 255, 256; 256, identifier:xtraProp; 257, identifier:ljust; 258, argument_list; 258, 259; 259, binary_operator:*; 259, 260; 259, 261; 260, identifier:tabsize; 261, integer:4; 262, string:':'; 263, call; 263, 264; 263, 265; 264, identifier:str; 265, argument_list; 265, 266; 266, subscript; 266, 267; 266, 272; 267, subscript; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:mo; 270, identifier:__dict__; 271, string:'XtraProperty'; 272, identifier:xtraProp; 273, string:"\n"; 274, expression_statement; 274, 275; 275, augmented_assignment:+=; 275, 276; 275, 277; 276, identifier:outstr; 277, binary_operator:+; 277, 278; 277, 300; 278, binary_operator:+; 278, 279; 278, 292; 279, binary_operator:+; 279, 280; 279, 291; 280, call; 280, 281; 280, 287; 281, attribute; 281, 282; 281, 286; 282, call; 282, 283; 282, 284; 283, identifier:str; 284, argument_list; 284, 285; 285, string:"Ucs"; 286, identifier:ljust; 287, argument_list; 287, 288; 288, binary_operator:*; 288, 289; 288, 290; 289, identifier:tabsize; 290, integer:4; 291, string:':'; 292, call; 292, 293; 292, 294; 293, identifier:str; 294, argument_list; 294, 295; 295, attribute; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:mo; 298, identifier:handle; 299, identifier:_ucs; 300, string:"\n"; 301, expression_statement; 301, 302; 302, augmented_assignment:+=; 302, 303; 302, 304; 303, identifier:outstr; 304, string:"\n"; 305, return_statement; 305, 306; 306, identifier:outstr | def _write_mo(mo):
""" Method to return string representation of a managed object. """
# from UcsBase import UcsUtils
classNotFound = False
if (UcsUtils.FindClassIdInMoMetaIgnoreCase(mo.classId) == None):
classNotFound = True
tabsize = 8
outstr = "\n"
if classNotFound:
outstr += "Managed Object\t\t\t:\t" + str(UcsUtils.WordU(mo.classId)) + "\n"
else:
outstr += "Managed Object\t\t\t:\t" + str(mo.propMoMeta.name) + "\n"
outstr += "-" * len("Managed Object") + "\n"
if (not classNotFound):
for prop in UcsUtils.GetUcsPropertyMetaAttributeList(mo.propMoMeta.name):
propMeta = UcsUtils.GetUcsPropertyMeta(mo.propMoMeta.name, prop)
if (propMeta.access == UcsPropertyMeta.Internal):
continue
val = mo.getattr(prop)
# if val != None and val != "":
outstr += str(prop).ljust(tabsize * 4) + ':' + str(val) + "\n"
else:
for prop in mo.__dict__:
if (prop in ['classId', 'XtraProperty', 'handle', 'propMoMeta', 'dirtyMask', 'child']):
continue
val = mo.__dict__[prop]
outstr += str(UcsUtils.WordU(prop)).ljust(tabsize * 4) + ':' + str(val) + "\n"
if mo.__dict__.has_key('XtraProperty'):
for xtraProp in mo.__dict__['XtraProperty']:
outstr += '[X]' + str(UcsUtils.WordU(xtraProp)).ljust(tabsize * 4) + ':' + str(
mo.__dict__['XtraProperty'][xtraProp]) + "\n"
outstr += str("Ucs").ljust(tabsize * 4) + ':' + str(mo.handle._ucs) + "\n"
outstr += "\n"
return outstr |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:LoadFromXml; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:node; 6, identifier:handle; 7, block; 7, 8; 7, 10; 7, 17; 7, 191; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:self; 14, identifier:SetHandle; 15, argument_list; 15, 16; 16, identifier:handle; 17, if_statement; 17, 18; 17, 23; 17, 24; 17, 25; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:node; 21, identifier:hasAttributes; 22, argument_list; 23, comment; 24, comment; 25, block; 25, 26; 25, 32; 25, 39; 25, 149; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:attributes; 29, attribute; 29, 30; 29, 31; 30, identifier:node; 31, identifier:attributes; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:attCount; 35, call; 35, 36; 35, 37; 36, identifier:len; 37, argument_list; 37, 38; 38, identifier:attributes; 39, for_statement; 39, 40; 39, 41; 39, 45; 40, identifier:i; 41, call; 41, 42; 41, 43; 42, identifier:range; 43, argument_list; 43, 44; 44, identifier:attCount; 45, block; 45, 46; 45, 55; 45, 56; 45, 67; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:attNode; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:attributes; 52, identifier:item; 53, argument_list; 53, 54; 54, identifier:i; 55, comment; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:attr; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:UcsUtils; 62, identifier:WordU; 63, argument_list; 63, 64; 64, attribute; 64, 65; 64, 66; 65, identifier:attNode; 66, identifier:localName; 67, if_statement; 67, 68; 67, 79; 67, 128; 68, parenthesized_expression; 68, 69; 69, comparison_operator:!=; 69, 70; 69, 78; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:UcsUtils; 73, identifier:FindClassIdInMoMetaIgnoreCase; 74, argument_list; 74, 75; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:classId; 78, None; 79, block; 79, 80; 80, if_statement; 80, 81; 80, 92; 80, 93; 80, 107; 81, parenthesized_expression; 81, 82; 82, comparison_operator:in; 82, 83; 82, 84; 83, identifier:attr; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:UcsUtils; 87, identifier:GetUcsPropertyMetaAttributeList; 88, argument_list; 88, 89; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:classId; 92, comment; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:setattr; 99, argument_list; 99, 100; 99, 101; 100, identifier:attr; 101, call; 101, 102; 101, 103; 102, identifier:str; 103, argument_list; 103, 104; 104, attribute; 104, 105; 104, 106; 105, identifier:attNode; 106, identifier:value; 107, else_clause; 107, 108; 107, 109; 108, comment; 109, block; 109, 110; 110, expression_statement; 110, 111; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:self; 114, identifier:setattr; 115, argument_list; 115, 116; 115, 122; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:UcsUtils; 119, identifier:WordU; 120, argument_list; 120, 121; 121, identifier:attr; 122, call; 122, 123; 122, 124; 123, identifier:str; 124, argument_list; 124, 125; 125, attribute; 125, 126; 125, 127; 126, identifier:attNode; 127, identifier:value; 128, else_clause; 128, 129; 128, 130; 129, comment; 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:setattr; 136, argument_list; 136, 137; 136, 143; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:UcsUtils; 140, identifier:WordU; 141, argument_list; 141, 142; 142, identifier:attr; 143, call; 143, 144; 143, 145; 144, identifier:str; 145, argument_list; 145, 146; 146, attribute; 146, 147; 146, 148; 147, identifier:attNode; 148, identifier:value; 149, if_statement; 149, 150; 149, 167; 150, boolean_operator:and; 150, 151; 150, 159; 151, comparison_operator:==; 151, 152; 151, 158; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:self; 155, identifier:getattr; 156, argument_list; 156, 157; 157, string:"Rn"; 158, None; 159, comparison_operator:!=; 159, 160; 159, 166; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:self; 163, identifier:getattr; 164, argument_list; 164, 165; 165, string:"Dn"; 166, None; 167, block; 167, 168; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:self; 172, identifier:setattr; 173, argument_list; 173, 174; 173, 175; 174, string:"Rn"; 175, call; 175, 176; 175, 177; 176, identifier:str; 177, argument_list; 177, 178; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:re; 181, identifier:sub; 182, argument_list; 182, 183; 182, 184; 182, 185; 183, string:r'^.*/'; 184, string:''; 185, call; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:self; 188, identifier:getattr; 189, argument_list; 189, 190; 190, string:"Dn"; 191, if_statement; 191, 192; 191, 198; 191, 199; 191, 200; 192, parenthesized_expression; 192, 193; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:node; 196, identifier:hasChildNodes; 197, argument_list; 198, comment; 199, comment; 200, block; 200, 201; 200, 207; 200, 214; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 204; 203, identifier:childList; 204, attribute; 204, 205; 204, 206; 205, identifier:node; 206, identifier:childNodes; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:childCount; 210, call; 210, 211; 210, 212; 211, identifier:len; 212, argument_list; 212, 213; 213, identifier:childList; 214, for_statement; 214, 215; 214, 216; 214, 220; 215, identifier:i; 216, call; 216, 217; 216, 218; 217, identifier:range; 218, argument_list; 218, 219; 219, identifier:childCount; 220, block; 220, 221; 220, 230; 220, 241; 220, 254; 220, 255; 220, 256; 220, 270; 220, 279; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:childNode; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:childList; 227, identifier:item; 228, argument_list; 228, 229; 229, identifier:i; 230, if_statement; 230, 231; 230, 239; 231, parenthesized_expression; 231, 232; 232, comparison_operator:!=; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:childNode; 235, identifier:nodeType; 236, attribute; 236, 237; 236, 238; 237, identifier:Node; 238, identifier:ELEMENT_NODE; 239, block; 239, 240; 240, continue_statement; 241, if_statement; 241, 242; 241, 251; 241, 252; 242, comparison_operator:in; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:childNode; 245, identifier:localName; 246, attribute; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:self; 249, identifier:propMoMeta; 250, identifier:fieldNames; 251, comment; 252, block; 252, 253; 253, pass_statement; 254, comment; 255, comment; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 259; 258, identifier:c; 259, call; 259, 260; 259, 261; 260, identifier:ManagedObject; 261, argument_list; 261, 262; 262, call; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:UcsUtils; 265, identifier:WordU; 266, argument_list; 266, 267; 267, attribute; 267, 268; 267, 269; 268, identifier:childNode; 269, identifier:localName; 270, expression_statement; 270, 271; 271, call; 271, 272; 271, 277; 272, attribute; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:self; 275, identifier:child; 276, identifier:append; 277, argument_list; 277, 278; 278, identifier:c; 279, expression_statement; 279, 280; 280, call; 280, 281; 280, 284; 281, attribute; 281, 282; 281, 283; 282, identifier:c; 283, identifier:LoadFromXml; 284, argument_list; 284, 285; 284, 286; 285, identifier:childNode; 286, identifier:handle | def LoadFromXml(self, node, handle):
""" Method updates the object from the xml representation of the managed object. """
self.SetHandle(handle)
if node.hasAttributes():
# attributes = node._get_attributes()
# attCount = attributes._get_length()
attributes = node.attributes
attCount = len(attributes)
for i in range(attCount):
attNode = attributes.item(i)
# attr = UcsUtils.WordU(attNode._get_name())
attr = UcsUtils.WordU(attNode.localName)
if (UcsUtils.FindClassIdInMoMetaIgnoreCase(self.classId) != None):
if (attr in UcsUtils.GetUcsPropertyMetaAttributeList(self.classId)):
# self.setattr(attr, str(attNode.nodeValue))
self.setattr(attr, str(attNode.value))
else:
# self.setattr(UcsUtils.WordU(attr), str(attNode.nodeValue))
self.setattr(UcsUtils.WordU(attr), str(attNode.value))
else:
# self.setattr(UcsUtils.WordU(attr), str(attNode.nodeValue))
self.setattr(UcsUtils.WordU(attr), str(attNode.value))
if self.getattr("Rn") == None and self.getattr("Dn") != None:
self.setattr("Rn", str(re.sub(r'^.*/', '', self.getattr("Dn"))))
if (node.hasChildNodes()):
# childList = node._get_childNodes()
# childCount = childList._get_length()
childList = node.childNodes
childCount = len(childList)
for i in range(childCount):
childNode = childList.item(i)
if (childNode.nodeType != Node.ELEMENT_NODE):
continue
if childNode.localName in self.propMoMeta.fieldNames:
# .LoadFromXml(childNode, handle)
pass
# TODO: Need code analysis.
# if childNode.localName in self.propMoMeta.childFieldNames:
c = ManagedObject(UcsUtils.WordU(childNode.localName))
self.child.append(c)
c.LoadFromXml(childNode, handle) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 4; 2, function_name:GetJavaInstallationPath; 3, parameters; 4, block; 4, 5; 4, 7; 4, 12; 4, 13; 4, 14; 5, expression_statement; 5, 6; 6, comment; 7, import_statement; 7, 8; 7, 10; 8, dotted_name; 8, 9; 9, identifier:os; 10, dotted_name; 10, 11; 11, identifier:platform; 12, comment; 13, comment; 14, if_statement; 14, 15; 14, 22; 14, 91; 14, 92; 14, 93; 15, comparison_operator:==; 15, 16; 15, 21; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:platform; 19, identifier:system; 20, argument_list; 21, string:"Linux"; 22, block; 22, 23; 22, 34; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:path; 26, call; 26, 27; 26, 32; 27, attribute; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:os; 30, identifier:environ; 31, identifier:get; 32, argument_list; 32, 33; 33, string:'JAVA_HOME'; 34, if_statement; 34, 35; 34, 37; 34, 43; 34, 44; 35, not_operator; 35, 36; 36, identifier:path; 37, block; 37, 38; 38, raise_statement; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:UcsValidationException; 41, argument_list; 41, 42; 42, string:"Please make sure JAVA is installed and variable JAVA_HOME is set properly."; 43, comment; 44, else_clause; 44, 45; 45, block; 45, 46; 45, 58; 45, 70; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:path; 49, call; 49, 50; 49, 55; 50, attribute; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:os; 53, identifier:path; 54, identifier:join; 55, argument_list; 55, 56; 55, 57; 56, identifier:path; 57, string:'bin'; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:path; 61, call; 61, 62; 61, 67; 62, attribute; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:os; 65, identifier:path; 66, identifier:join; 67, argument_list; 67, 68; 67, 69; 68, identifier:path; 69, string:'javaws'; 70, if_statement; 70, 71; 70, 80; 70, 86; 70, 87; 71, not_operator; 71, 72; 72, call; 72, 73; 72, 78; 73, attribute; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:os; 76, identifier:path; 77, identifier:exists; 78, argument_list; 78, 79; 79, identifier:path; 80, block; 80, 81; 81, raise_statement; 81, 82; 82, call; 82, 83; 82, 84; 83, identifier:UcsValidationException; 84, argument_list; 84, 85; 85, string:"javaws is not installed on System."; 86, comment; 87, else_clause; 87, 88; 88, block; 88, 89; 89, return_statement; 89, 90; 90, identifier:path; 91, comment; 92, comment; 93, elif_clause; 93, 94; 93, 109; 94, boolean_operator:or; 94, 95; 94, 102; 95, comparison_operator:==; 95, 96; 95, 101; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:platform; 99, identifier:system; 100, argument_list; 101, string:"Windows"; 102, comparison_operator:==; 102, 103; 102, 108; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:platform; 106, identifier:system; 107, argument_list; 108, string:"Microsoft"; 109, block; 109, 110; 109, 121; 109, 135; 109, 150; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:path; 113, call; 113, 114; 113, 119; 114, attribute; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:os; 117, identifier:environ; 118, identifier:get; 119, argument_list; 119, 120; 120, string:'JAVA_HOME'; 121, if_statement; 121, 122; 121, 125; 122, comparison_operator:==; 122, 123; 122, 124; 123, identifier:path; 124, None; 125, block; 125, 126; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:path; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:UcsUtils; 132, identifier:CheckRegistryKey; 133, argument_list; 133, 134; 134, string:r"SOFTWARE\\JavaSoft\\Java Runtime Environment\\"; 135, if_statement; 135, 136; 135, 139; 135, 140; 136, comparison_operator:==; 136, 137; 136, 138; 137, identifier:path; 138, None; 139, comment; 140, block; 140, 141; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:path; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:UcsUtils; 147, identifier:CheckRegistryKey; 148, argument_list; 148, 149; 149, string:r"SOFTWARE\\Wow6432Node\\JavaSoft\\Java Runtime Environment"; 150, if_statement; 150, 151; 150, 153; 150, 159; 150, 160; 151, not_operator; 151, 152; 152, identifier:path; 153, block; 153, 154; 154, raise_statement; 154, 155; 155, call; 155, 156; 155, 157; 156, identifier:UcsValidationException; 157, argument_list; 157, 158; 158, string:"Please make sure JAVA is installed."; 159, comment; 160, else_clause; 160, 161; 161, block; 161, 162; 161, 174; 161, 186; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:path; 165, call; 165, 166; 165, 171; 166, attribute; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:os; 169, identifier:path; 170, identifier:join; 171, argument_list; 171, 172; 171, 173; 172, identifier:path; 173, string:'bin'; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:path; 177, call; 177, 178; 177, 183; 178, attribute; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:os; 181, identifier:path; 182, identifier:join; 183, argument_list; 183, 184; 183, 185; 184, identifier:path; 185, string:'javaws.exe'; 186, if_statement; 186, 187; 186, 196; 186, 202; 186, 203; 187, not_operator; 187, 188; 188, call; 188, 189; 188, 194; 189, attribute; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:os; 192, identifier:path; 193, identifier:exists; 194, argument_list; 194, 195; 195, identifier:path; 196, block; 196, 197; 197, raise_statement; 197, 198; 198, call; 198, 199; 198, 200; 199, identifier:UcsValidationException; 200, argument_list; 200, 201; 201, string:"javaws.exe is not installed on System."; 202, comment; 203, else_clause; 203, 204; 204, block; 204, 205; 205, return_statement; 205, 206; 206, identifier:path | def GetJavaInstallationPath():
""" Method returns the java installation path in the windows or Linux environment. """
import os, platform
# Get JavaPath for Ubuntu
# if os.name == "posix":
if platform.system() == "Linux":
path = os.environ.get('JAVA_HOME')
if not path:
raise UcsValidationException(
"Please make sure JAVA is installed and variable JAVA_HOME is set properly.")
# raise Exception("Please make sure JAVA is installed and variable JAVA_HOME is set properly.")
else:
path = os.path.join(path, 'bin')
path = os.path.join(path, 'javaws')
if not os.path.exists(path):
raise UcsValidationException("javaws is not installed on System.")
# raise Exception("javaws is not installed on System.")
else:
return path
# Get JavaPath for Windows
# elif os.name == "nt":
elif platform.system() == "Windows" or platform.system() == "Microsoft":
path = os.environ.get('JAVA_HOME')
if path == None:
path = UcsUtils.CheckRegistryKey(r"SOFTWARE\\JavaSoft\\Java Runtime Environment\\")
if path == None: # Check for 32 bit Java on 64 bit machine.
path = UcsUtils.CheckRegistryKey(r"SOFTWARE\\Wow6432Node\\JavaSoft\\Java Runtime Environment")
if not path:
raise UcsValidationException("Please make sure JAVA is installed.")
# raise Exception("Please make sure JAVA is installed.")
else:
path = os.path.join(path, 'bin')
path = os.path.join(path, 'javaws.exe')
if not os.path.exists(path):
raise UcsValidationException("javaws.exe is not installed on System.")
# raise Exception("javaws.exe is not installed on System.")
else:
return path |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:GetSyncMoConfig; 3, parameters; 3, 4; 4, identifier:ConfigDoc; 5, block; 5, 6; 5, 8; 5, 12; 5, 21; 5, 248; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:moConfigMap; 11, dictionary; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:configList; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:ConfigDoc; 18, identifier:getElementsByTagName; 19, argument_list; 19, 20; 20, string:"mo"; 21, for_statement; 21, 22; 21, 23; 21, 24; 22, identifier:moConfigNode; 23, identifier:configList; 24, block; 24, 25; 24, 29; 24, 33; 24, 37; 24, 41; 24, 45; 24, 49; 24, 53; 24, 57; 24, 74; 24, 91; 24, 108; 24, 125; 24, 142; 24, 159; 24, 176; 24, 193; 24, 194; 24, 198; 24, 215; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:classId; 28, None; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:noun; 32, None; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:version; 36, None; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:actionVersion; 40, None; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:action; 44, None; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:ignoreReason; 48, None; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:status; 52, None; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:excludeList; 56, None; 57, if_statement; 57, 58; 57, 64; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:moConfigNode; 61, identifier:hasAttribute; 62, argument_list; 62, 63; 63, string:"classid"; 64, block; 64, 65; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:classId; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:moConfigNode; 71, identifier:getAttribute; 72, argument_list; 72, 73; 73, string:"classid"; 74, if_statement; 74, 75; 74, 81; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:moConfigNode; 78, identifier:hasAttribute; 79, argument_list; 79, 80; 80, string:"noun"; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:noun; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:moConfigNode; 88, identifier:getAttribute; 89, argument_list; 89, 90; 90, string:"noun"; 91, if_statement; 91, 92; 91, 98; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:moConfigNode; 95, identifier:hasAttribute; 96, argument_list; 96, 97; 97, string:"version"; 98, block; 98, 99; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:version; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:moConfigNode; 105, identifier:getAttribute; 106, argument_list; 106, 107; 107, string:"version"; 108, if_statement; 108, 109; 108, 115; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:moConfigNode; 112, identifier:hasAttribute; 113, argument_list; 113, 114; 114, string:"actionVersion"; 115, block; 115, 116; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:actionVersion; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:moConfigNode; 122, identifier:getAttribute; 123, argument_list; 123, 124; 124, string:"actionVersion"; 125, if_statement; 125, 126; 125, 132; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:moConfigNode; 129, identifier:hasAttribute; 130, argument_list; 130, 131; 131, string:"action"; 132, block; 132, 133; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:action; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:moConfigNode; 139, identifier:getAttribute; 140, argument_list; 140, 141; 141, string:"action"; 142, if_statement; 142, 143; 142, 149; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:moConfigNode; 146, identifier:hasAttribute; 147, argument_list; 147, 148; 148, string:"ignoreReason"; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:ignoreReason; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:moConfigNode; 156, identifier:getAttribute; 157, argument_list; 157, 158; 158, string:"ignoreReason"; 159, if_statement; 159, 160; 159, 166; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:moConfigNode; 163, identifier:hasAttribute; 164, argument_list; 164, 165; 165, string:"status"; 166, block; 166, 167; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:status; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:moConfigNode; 173, identifier:getAttribute; 174, argument_list; 174, 175; 175, string:"status"; 176, if_statement; 176, 177; 176, 183; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:moConfigNode; 180, identifier:hasAttribute; 181, argument_list; 181, 182; 182, string:"excludeList"; 183, block; 183, 184; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:excludeList; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:moConfigNode; 190, identifier:getAttribute; 191, argument_list; 191, 192; 192, string:"excludeList"; 193, comment; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:moConfig; 197, None; 198, if_statement; 198, 199; 198, 200; 199, identifier:classId; 200, block; 200, 201; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 204; 203, identifier:moConfig; 204, call; 204, 205; 204, 206; 205, identifier:SyncMoConfig; 206, argument_list; 206, 207; 206, 208; 206, 209; 206, 210; 206, 211; 206, 212; 206, 213; 206, 214; 207, identifier:classId; 208, identifier:noun; 209, identifier:version; 210, identifier:actionVersion; 211, identifier:action; 212, identifier:ignoreReason; 213, identifier:status; 214, identifier:excludeList; 215, if_statement; 215, 216; 215, 217; 216, identifier:moConfig; 217, block; 217, 218; 218, if_statement; 218, 219; 218, 222; 218, 229; 219, comparison_operator:in; 219, 220; 219, 221; 220, identifier:classId; 221, identifier:moConfigMap; 222, block; 222, 223; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 228; 225, subscript; 225, 226; 225, 227; 226, identifier:moConfigMap; 227, identifier:classId; 228, identifier:moConfig; 229, else_clause; 229, 230; 230, block; 230, 231; 230, 235; 230, 242; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:moConfigList; 234, list:[]; 235, expression_statement; 235, 236; 236, call; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:moConfigList; 239, identifier:append; 240, argument_list; 240, 241; 241, identifier:moConfig; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 247; 244, subscript; 244, 245; 244, 246; 245, identifier:moConfigMap; 246, identifier:classId; 247, identifier:moConfigList; 248, return_statement; 248, 249; 249, identifier:moConfigMap | def GetSyncMoConfig(ConfigDoc):
""" Internal support method for SyncManagedObject. """
moConfigMap = {}
configList = ConfigDoc.getElementsByTagName("mo")
for moConfigNode in configList:
classId = None
noun = None
version = None
actionVersion = None
action = None
ignoreReason = None
status = None
excludeList = None
if moConfigNode.hasAttribute("classid"):
classId = moConfigNode.getAttribute("classid")
if moConfigNode.hasAttribute("noun"):
noun = moConfigNode.getAttribute("noun")
if moConfigNode.hasAttribute("version"):
version = moConfigNode.getAttribute("version")
if moConfigNode.hasAttribute("actionVersion"):
actionVersion = moConfigNode.getAttribute("actionVersion")
if moConfigNode.hasAttribute("action"):
action = moConfigNode.getAttribute("action")
if moConfigNode.hasAttribute("ignoreReason"):
ignoreReason = moConfigNode.getAttribute("ignoreReason")
if moConfigNode.hasAttribute("status"):
status = moConfigNode.getAttribute("status")
if moConfigNode.hasAttribute("excludeList"):
excludeList = moConfigNode.getAttribute("excludeList")
# SyncMoConfig Object
moConfig = None
if classId:
moConfig = SyncMoConfig(classId, noun, version, actionVersion, action, ignoreReason, status,
excludeList)
if moConfig:
if classId in moConfigMap:
moConfigMap[classId] = moConfig
else:
moConfigList = []
moConfigList.append(moConfig)
moConfigMap[classId] = moConfigList
return moConfigMap |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 27; 2, function_name:duration; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:t; 5, default_parameter; 5, 6; 5, 7; 6, identifier:now; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:precision; 10, integer:1; 11, default_parameter; 11, 12; 11, 13; 12, identifier:pad; 13, string:', '; 14, default_parameter; 14, 15; 14, 16; 15, identifier:words; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:justnow; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:datetime; 22, identifier:timedelta; 23, argument_list; 23, 24; 24, keyword_argument; 24, 25; 24, 26; 25, identifier:seconds; 26, integer:10; 27, block; 27, 28; 27, 30; 27, 41; 27, 48; 27, 63; 27, 84; 27, 100; 28, expression_statement; 28, 29; 29, string:'''
Time delta compared to ``t``. You can override ``now`` to specify what time
to compare to.
:param t: timestamp, :class:`datetime.date` or :class:`datetime.datetime`
object
:param now: default ``None``, optionally a :class:`datetime.datetime`
object
:param precision: default ``1``, number of fragments to return
:param words: default ``None``, allow words like "yesterday", if set to
``None`` this will be enabled if ``precision`` is set to
``1``
:param justnow: default ``datetime.timedelta(seconds=10)``,
:class:`datetime.timedelta` object passed to :func:`delta`
representing tolerance for considering argument ``t`` as
meaning 'just now'
>>> import time
>>> from datetime import datetime
>>> print(duration(time.time() + 1))
just now
>>> print(duration(time.time() + 11))
11 seconds from now
>>> print(duration(time.time() - 1))
just now
>>> print(duration(time.time() - 11))
11 seconds ago
>>> print(duration(time.time() - 3601))
an hour ago
>>> print(duration(time.time() - 7201))
2 hours ago
>>> print(duration(time.time() - 1234567))
2 weeks ago
>>> print(duration(time.time() + 7200, precision=1))
2 hours from now
>>> print(duration(time.time() - 1234567, precision=3))
2 weeks, 6 hours, 56 minutes ago
>>> print(duration(datetime(2014, 9, 8), now=datetime(2014, 9, 9)))
yesterday
>>> print(duration(datetime(2014, 9, 7, 23), now=datetime(2014, 9, 9)))
1 day ago
>>> print(duration(datetime(2014, 9, 10), now=datetime(2014, 9, 9)))
tomorrow
>>> print(duration(datetime(2014, 9, 11, 1), now=datetime(2014, 9, 9, 23)))
1 day from now
'''; 30, if_statement; 30, 31; 30, 34; 31, comparison_operator:is; 31, 32; 31, 33; 32, identifier:words; 33, None; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:words; 38, comparison_operator:==; 38, 39; 38, 40; 39, identifier:precision; 40, integer:1; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:t1; 44, call; 44, 45; 44, 46; 45, identifier:_to_datetime; 46, argument_list; 46, 47; 47, identifier:t; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:t2; 51, call; 51, 52; 51, 53; 52, identifier:_to_datetime; 53, argument_list; 53, 54; 54, boolean_operator:or; 54, 55; 54, 56; 55, identifier:now; 56, call; 56, 57; 56, 62; 57, attribute; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:datetime; 60, identifier:datetime; 61, identifier:now; 62, argument_list; 63, if_statement; 63, 64; 63, 67; 63, 75; 64, comparison_operator:<; 64, 65; 64, 66; 65, identifier:t1; 66, identifier:t2; 67, block; 67, 68; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:format; 71, call; 71, 72; 71, 73; 72, identifier:_; 73, argument_list; 73, 74; 74, string:'%s ago'; 75, else_clause; 75, 76; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:format; 80, call; 80, 81; 80, 82; 81, identifier:_; 82, argument_list; 82, 83; 83, string:'%s from now'; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 89; 86, pattern_list; 86, 87; 86, 88; 87, identifier:result; 88, identifier:remains; 89, call; 89, 90; 89, 91; 90, identifier:delta; 91, argument_list; 91, 92; 91, 93; 91, 94; 91, 97; 92, identifier:t1; 93, identifier:t2; 94, keyword_argument; 94, 95; 94, 96; 95, identifier:words; 96, identifier:words; 97, keyword_argument; 97, 98; 97, 99; 98, identifier:justnow; 99, identifier:justnow; 100, if_statement; 100, 101; 100, 124; 100, 127; 100, 167; 101, comparison_operator:in; 101, 102; 101, 103; 102, identifier:result; 103, tuple; 103, 104; 103, 108; 103, 112; 103, 116; 103, 120; 104, call; 104, 105; 104, 106; 105, identifier:_; 106, argument_list; 106, 107; 107, string:'just now'; 108, call; 108, 109; 108, 110; 109, identifier:_; 110, argument_list; 110, 111; 111, string:'yesterday'; 112, call; 112, 113; 112, 114; 113, identifier:_; 114, argument_list; 114, 115; 115, string:'tomorrow'; 116, call; 116, 117; 116, 118; 117, identifier:_; 118, argument_list; 118, 119; 119, string:'last week'; 120, call; 120, 121; 120, 122; 121, identifier:_; 122, argument_list; 122, 123; 123, string:'next week'; 124, block; 124, 125; 125, return_statement; 125, 126; 126, identifier:result; 127, elif_clause; 127, 128; 127, 133; 128, boolean_operator:and; 128, 129; 128, 132; 129, comparison_operator:>; 129, 130; 129, 131; 130, identifier:precision; 131, integer:1; 132, identifier:remains; 133, block; 133, 134; 133, 147; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:t3; 137, binary_operator:-; 137, 138; 137, 139; 138, identifier:t2; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:datetime; 142, identifier:timedelta; 143, argument_list; 143, 144; 144, keyword_argument; 144, 145; 144, 146; 145, identifier:seconds; 146, identifier:remains; 147, return_statement; 147, 148; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:pad; 151, identifier:join; 152, argument_list; 152, 153; 153, list:[
result,
duration(t2, t3, precision - 1, pad, words=False),
]; 153, 154; 153, 155; 154, identifier:result; 155, call; 155, 156; 155, 157; 156, identifier:duration; 157, argument_list; 157, 158; 157, 159; 157, 160; 157, 163; 157, 164; 158, identifier:t2; 159, identifier:t3; 160, binary_operator:-; 160, 161; 160, 162; 161, identifier:precision; 162, integer:1; 163, identifier:pad; 164, keyword_argument; 164, 165; 164, 166; 165, identifier:words; 166, False; 167, else_clause; 167, 168; 168, block; 168, 169; 169, return_statement; 169, 170; 170, binary_operator:%; 170, 171; 170, 172; 171, identifier:format; 172, tuple; 172, 173; 173, identifier:result | def duration(t, now=None, precision=1, pad=', ', words=None,
justnow=datetime.timedelta(seconds=10)):
'''
Time delta compared to ``t``. You can override ``now`` to specify what time
to compare to.
:param t: timestamp, :class:`datetime.date` or :class:`datetime.datetime`
object
:param now: default ``None``, optionally a :class:`datetime.datetime`
object
:param precision: default ``1``, number of fragments to return
:param words: default ``None``, allow words like "yesterday", if set to
``None`` this will be enabled if ``precision`` is set to
``1``
:param justnow: default ``datetime.timedelta(seconds=10)``,
:class:`datetime.timedelta` object passed to :func:`delta`
representing tolerance for considering argument ``t`` as
meaning 'just now'
>>> import time
>>> from datetime import datetime
>>> print(duration(time.time() + 1))
just now
>>> print(duration(time.time() + 11))
11 seconds from now
>>> print(duration(time.time() - 1))
just now
>>> print(duration(time.time() - 11))
11 seconds ago
>>> print(duration(time.time() - 3601))
an hour ago
>>> print(duration(time.time() - 7201))
2 hours ago
>>> print(duration(time.time() - 1234567))
2 weeks ago
>>> print(duration(time.time() + 7200, precision=1))
2 hours from now
>>> print(duration(time.time() - 1234567, precision=3))
2 weeks, 6 hours, 56 minutes ago
>>> print(duration(datetime(2014, 9, 8), now=datetime(2014, 9, 9)))
yesterday
>>> print(duration(datetime(2014, 9, 7, 23), now=datetime(2014, 9, 9)))
1 day ago
>>> print(duration(datetime(2014, 9, 10), now=datetime(2014, 9, 9)))
tomorrow
>>> print(duration(datetime(2014, 9, 11, 1), now=datetime(2014, 9, 9, 23)))
1 day from now
'''
if words is None:
words = precision == 1
t1 = _to_datetime(t)
t2 = _to_datetime(now or datetime.datetime.now())
if t1 < t2:
format = _('%s ago')
else:
format = _('%s from now')
result, remains = delta(t1, t2, words=words, justnow=justnow)
if result in (
_('just now'),
_('yesterday'),
_('tomorrow'),
_('last week'),
_('next week'),
):
return result
elif precision > 1 and remains:
t3 = t2 - datetime.timedelta(seconds=remains)
return pad.join([
result,
duration(t2, t3, precision - 1, pad, words=False),
])
else:
return format % (result,) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:search; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:query_string; 6, block; 6, 7; 6, 9; 6, 17; 6, 18; 6, 26; 6, 32; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:query; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:create_query; 16, argument_list; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:parser; 21, call; 21, 22; 21, 23; 22, identifier:QueryParser; 23, argument_list; 23, 24; 23, 25; 24, identifier:query_string; 25, identifier:query; 26, expression_statement; 26, 27; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:parser; 30, identifier:parse; 31, argument_list; 32, return_statement; 32, 33; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:query; 37, argument_list; 37, 38; 38, identifier:query | def search(self, query_string):
"""Performs a search against the index using lunr query syntax.
Results will be returned sorted by their score, the most relevant
results will be returned first.
For more programmatic querying use `lunr.Index.query`.
Args:
query_string (str): A string to parse into a Query.
Returns:
dict: Results of executing the query.
"""
query = self.create_query()
# TODO: should QueryParser be a method of query? should it return one?
parser = QueryParser(query_string, query)
parser.parse()
return self.query(query) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:wrap_error; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:self; 5, identifier:data; 6, identifier:renderer_context; 7, identifier:keys_are_fields; 8, identifier:issue_is_title; 9, block; 9, 10; 9, 12; 9, 22; 9, 33; 9, 37; 9, 129; 9, 137; 9, 143; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:response; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:renderer_context; 18, identifier:get; 19, argument_list; 19, 20; 19, 21; 20, string:"response"; 21, None; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:status_code; 25, call; 25, 26; 25, 27; 26, identifier:str; 27, argument_list; 27, 28; 28, boolean_operator:and; 28, 29; 28, 30; 29, identifier:response; 30, attribute; 30, 31; 30, 32; 31, identifier:response; 32, identifier:status_code; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:errors; 36, list:[]; 37, for_statement; 37, 38; 37, 41; 37, 46; 38, pattern_list; 38, 39; 38, 40; 39, identifier:field; 40, identifier:issues; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:data; 44, identifier:items; 45, argument_list; 46, block; 46, 47; 46, 61; 47, if_statement; 47, 48; 47, 55; 48, call; 48, 49; 48, 50; 49, identifier:isinstance; 50, argument_list; 50, 51; 50, 52; 51, identifier:issues; 52, attribute; 52, 53; 52, 54; 53, identifier:six; 54, identifier:string_types; 55, block; 55, 56; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:issues; 59, list:[issues]; 59, 60; 60, identifier:issues; 61, for_statement; 61, 62; 61, 63; 61, 64; 62, identifier:issue; 63, identifier:issues; 64, block; 64, 65; 64, 73; 64, 79; 64, 96; 64, 122; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:error; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:dict_class; 72, argument_list; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 78; 75, subscript; 75, 76; 75, 77; 76, identifier:error; 77, string:"status"; 78, identifier:status_code; 79, if_statement; 79, 80; 79, 81; 79, 88; 80, identifier:issue_is_title; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 87; 84, subscript; 84, 85; 84, 86; 85, identifier:error; 86, string:"title"; 87, identifier:issue; 88, else_clause; 88, 89; 89, block; 89, 90; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 95; 92, subscript; 92, 93; 92, 94; 93, identifier:error; 94, string:"detail"; 95, identifier:issue; 96, if_statement; 96, 97; 96, 98; 97, identifier:keys_are_fields; 98, block; 98, 99; 99, if_statement; 99, 100; 99, 105; 99, 112; 100, comparison_operator:in; 100, 101; 100, 102; 101, identifier:field; 102, tuple; 102, 103; 102, 104; 103, string:'non_field_errors'; 104, identifier:NON_FIELD_ERRORS; 105, block; 105, 106; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 111; 108, subscript; 108, 109; 108, 110; 109, identifier:error; 110, string:"path"; 111, string:'/-'; 112, else_clause; 112, 113; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 119; 116, subscript; 116, 117; 116, 118; 117, identifier:error; 118, string:"path"; 119, binary_operator:+; 119, 120; 119, 121; 120, string:'/'; 121, identifier:field; 122, expression_statement; 122, 123; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:errors; 126, identifier:append; 127, argument_list; 127, 128; 128, identifier:error; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:wrapper; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:self; 135, identifier:dict_class; 136, argument_list; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 142; 139, subscript; 139, 140; 139, 141; 140, identifier:wrapper; 141, string:"errors"; 142, identifier:errors; 143, return_statement; 143, 144; 144, identifier:wrapper | def wrap_error(
self, data, renderer_context, keys_are_fields, issue_is_title):
"""Convert error native data to the JSON API Error format
JSON API has a different format for errors, but Django REST Framework
doesn't have a separate rendering path for errors. This results in
some guesswork to determine if data is an error, what kind, and how
to handle it.
As of August 2014, there is not a consensus about the error format in
JSON API. The format documentation defines an "errors" collection, and
some possible fields for that collection, but without examples for
common cases. If and when consensus is reached, this format will
probably change.
"""
response = renderer_context.get("response", None)
status_code = str(response and response.status_code)
errors = []
for field, issues in data.items():
if isinstance(issues, six.string_types):
issues = [issues]
for issue in issues:
error = self.dict_class()
error["status"] = status_code
if issue_is_title:
error["title"] = issue
else:
error["detail"] = issue
if keys_are_fields:
if field in ('non_field_errors', NON_FIELD_ERRORS):
error["path"] = '/-'
else:
error["path"] = '/' + field
errors.append(error)
wrapper = self.dict_class()
wrapper["errors"] = errors
return wrapper |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:lunr; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:ref; 5, identifier:fields; 6, identifier:documents; 7, default_parameter; 7, 8; 7, 9; 8, identifier:languages; 9, None; 10, block; 10, 11; 10, 13; 10, 117; 10, 124; 10, 152; 10, 188; 11, expression_statement; 11, 12; 12, comment; 13, if_statement; 13, 14; 13, 21; 13, 89; 14, boolean_operator:and; 14, 15; 14, 18; 15, comparison_operator:is; 15, 16; 15, 17; 16, identifier:languages; 17, None; 18, attribute; 18, 19; 18, 20; 19, identifier:lang; 20, identifier:LANGUAGE_SUPPORT; 21, block; 21, 22; 21, 34; 21, 48; 21, 80; 22, if_statement; 22, 23; 22, 28; 23, call; 23, 24; 23, 25; 24, identifier:isinstance; 25, argument_list; 25, 26; 25, 27; 26, identifier:languages; 27, identifier:basestring; 28, block; 28, 29; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:languages; 32, list:[languages]; 32, 33; 33, identifier:languages; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:unsupported_languages; 37, binary_operator:-; 37, 38; 37, 42; 38, call; 38, 39; 38, 40; 39, identifier:set; 40, argument_list; 40, 41; 41, identifier:languages; 42, call; 42, 43; 42, 44; 43, identifier:set; 44, argument_list; 44, 45; 45, attribute; 45, 46; 45, 47; 46, identifier:lang; 47, identifier:SUPPORTED_LANGUAGES; 48, if_statement; 48, 49; 48, 50; 49, identifier:unsupported_languages; 50, block; 50, 51; 51, raise_statement; 51, 52; 52, call; 52, 53; 52, 54; 53, identifier:RuntimeError; 54, argument_list; 54, 55; 55, call; 55, 56; 55, 61; 56, attribute; 56, 57; 56, 60; 57, concatenated_string; 57, 58; 57, 59; 58, string:"The specified languages {} are not supported, "; 59, string:"please choose one of {}"; 60, identifier:format; 61, argument_list; 61, 62; 61, 68; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, string:", "; 65, identifier:join; 66, argument_list; 66, 67; 67, identifier:unsupported_languages; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, string:", "; 71, identifier:join; 72, argument_list; 72, 73; 73, call; 73, 74; 73, 79; 74, attribute; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:lang; 77, identifier:SUPPORTED_LANGUAGES; 78, identifier:keys; 79, argument_list; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:builder; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:lang; 86, identifier:get_nltk_builder; 87, argument_list; 87, 88; 88, identifier:languages; 89, else_clause; 89, 90; 90, block; 90, 91; 90, 97; 90, 108; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:builder; 94, call; 94, 95; 94, 96; 95, identifier:Builder; 96, argument_list; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 104; 99, attribute; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:builder; 102, identifier:pipeline; 103, identifier:add; 104, argument_list; 104, 105; 104, 106; 104, 107; 105, identifier:trimmer; 106, identifier:stop_word_filter; 107, identifier:stemmer; 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:builder; 113, identifier:search_pipeline; 114, identifier:add; 115, argument_list; 115, 116; 116, identifier:stemmer; 117, expression_statement; 117, 118; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:builder; 121, identifier:ref; 122, argument_list; 122, 123; 123, identifier:ref; 124, for_statement; 124, 125; 124, 126; 124, 127; 125, identifier:field; 126, identifier:fields; 127, block; 127, 128; 128, if_statement; 128, 129; 128, 134; 128, 143; 129, call; 129, 130; 129, 131; 130, identifier:isinstance; 131, argument_list; 131, 132; 131, 133; 132, identifier:field; 133, identifier:dict; 134, block; 134, 135; 135, expression_statement; 135, 136; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:builder; 139, identifier:field; 140, argument_list; 140, 141; 141, dictionary_splat; 141, 142; 142, identifier:field; 143, else_clause; 143, 144; 144, block; 144, 145; 145, expression_statement; 145, 146; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:builder; 149, identifier:field; 150, argument_list; 150, 151; 151, identifier:field; 152, for_statement; 152, 153; 152, 154; 152, 155; 153, identifier:document; 154, identifier:documents; 155, block; 155, 156; 156, if_statement; 156, 157; 156, 164; 156, 179; 157, call; 157, 158; 157, 159; 158, identifier:isinstance; 159, argument_list; 159, 160; 159, 161; 160, identifier:document; 161, tuple; 161, 162; 161, 163; 162, identifier:tuple; 163, identifier:list; 164, block; 164, 165; 165, expression_statement; 165, 166; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:builder; 169, identifier:add; 170, argument_list; 170, 171; 170, 174; 171, subscript; 171, 172; 171, 173; 172, identifier:document; 173, integer:0; 174, keyword_argument; 174, 175; 174, 176; 175, identifier:attributes; 176, subscript; 176, 177; 176, 178; 177, identifier:document; 178, integer:1; 179, else_clause; 179, 180; 180, block; 180, 181; 181, expression_statement; 181, 182; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:builder; 185, identifier:add; 186, argument_list; 186, 187; 187, identifier:document; 188, return_statement; 188, 189; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:builder; 192, identifier:build; 193, argument_list | def lunr(ref, fields, documents, languages=None):
"""A convenience function to configure and construct a lunr.Index.
Args:
ref (str): The key in the documents to be used a the reference.
fields (list): A list of strings defining fields in the documents to
index. Optionally a list of dictionaries with three keys:
`field_name` defining the document's field, `boost` an integer
defining a boost to be applied to the field, and `extractor`
a callable taking the document as a single argument and returning
a string located in the document in a particular way.
documents (list): The list of dictonaries representing the documents
to index. Optionally a 2-tuple of dicts, the first one being
the document and the second the associated attributes to it.
languages (str or list, optional): The languages to use if using
NLTK language support, ignored if NLTK is not available.
Returns:
Index: The populated Index ready to search against.
"""
if languages is not None and lang.LANGUAGE_SUPPORT:
if isinstance(languages, basestring):
languages = [languages]
unsupported_languages = set(languages) - set(lang.SUPPORTED_LANGUAGES)
if unsupported_languages:
raise RuntimeError(
"The specified languages {} are not supported, "
"please choose one of {}".format(
", ".join(unsupported_languages),
", ".join(lang.SUPPORTED_LANGUAGES.keys()),
)
)
builder = lang.get_nltk_builder(languages)
else:
builder = Builder()
builder.pipeline.add(trimmer, stop_word_filter, stemmer)
builder.search_pipeline.add(stemmer)
builder.ref(ref)
for field in fields:
if isinstance(field, dict):
builder.field(**field)
else:
builder.field(field)
for document in documents:
if isinstance(document, (tuple, list)):
builder.add(document[0], attributes=document[1])
else:
builder.add(document)
return builder.build() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:build_trie; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:pattern_filename; 5, identifier:pattern_format; 6, identifier:encoding; 7, identifier:on_word_boundaries; 8, block; 8, 9; 8, 11; 8, 15; 8, 34; 8, 52; 8, 62; 8, 66; 8, 360; 8, 373; 9, expression_statement; 9, 10; 10, string:'''
Constructs a finite state machine for performing string rewriting.
Arguments:
- `pattern_filename`:
- `pattern_format`:
- `encoding`:
- `on_word_boundaries`:
'''; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:boundaries; 14, identifier:on_word_boundaries; 15, if_statement; 15, 16; 15, 22; 16, boolean_operator:or; 16, 17; 16, 20; 17, comparison_operator:==; 17, 18; 17, 19; 18, identifier:pattern_format; 19, string:'auto'; 20, not_operator; 20, 21; 21, identifier:on_word_boundaries; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 28; 25, pattern_list; 25, 26; 25, 27; 26, identifier:tsv; 27, identifier:boundaries; 28, call; 28, 29; 28, 30; 29, identifier:detect_pattern_format; 30, argument_list; 30, 31; 30, 32; 30, 33; 31, identifier:pattern_filename; 32, identifier:encoding; 33, identifier:on_word_boundaries; 34, if_statement; 34, 35; 34, 38; 35, comparison_operator:==; 35, 36; 35, 37; 36, identifier:pattern_format; 37, string:'auto'; 38, block; 38, 39; 39, if_statement; 39, 40; 39, 41; 39, 46; 40, identifier:tsv; 41, block; 41, 42; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:pattern_format; 45, string:'tsv'; 46, else_clause; 46, 47; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:pattern_format; 51, string:'sed'; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:trie; 55, call; 55, 56; 55, 61; 56, attribute; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:fsed; 59, identifier:ahocorasick; 60, identifier:AhoCorasickTrie; 61, argument_list; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:num_candidates; 65, integer:0; 66, with_statement; 66, 67; 66, 76; 67, with_clause; 67, 68; 68, with_item; 68, 69; 69, as_pattern; 69, 70; 69, 74; 70, call; 70, 71; 70, 72; 71, identifier:open_file; 72, argument_list; 72, 73; 73, identifier:pattern_filename; 74, as_pattern_target; 74, 75; 75, identifier:pattern_file; 76, block; 76, 77; 77, for_statement; 77, 78; 77, 81; 77, 85; 78, pattern_list; 78, 79; 78, 80; 79, identifier:lineno; 80, identifier:line; 81, call; 81, 82; 81, 83; 82, identifier:enumerate; 83, argument_list; 83, 84; 84, identifier:pattern_file; 85, block; 85, 86; 85, 100; 85, 109; 85, 110; 85, 293; 85, 297; 85, 325; 85, 332; 85, 339; 85, 354; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:line; 89, call; 89, 90; 89, 98; 90, attribute; 90, 91; 90, 97; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:line; 94, identifier:decode; 95, argument_list; 95, 96; 96, identifier:encoding; 97, identifier:rstrip; 98, argument_list; 98, 99; 99, string:'\n'; 100, if_statement; 100, 101; 100, 107; 101, not_operator; 101, 102; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:line; 105, identifier:strip; 106, argument_list; 107, block; 107, 108; 108, continue_statement; 109, comment; 110, if_statement; 110, 111; 110, 114; 110, 155; 111, comparison_operator:==; 111, 112; 111, 113; 112, identifier:pattern_format; 113, string:'tsv'; 114, block; 114, 115; 114, 124; 114, 149; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:fields; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:line; 121, identifier:split; 122, argument_list; 122, 123; 123, string:'\t'; 124, if_statement; 124, 125; 124, 131; 125, comparison_operator:!=; 125, 126; 125, 130; 126, call; 126, 127; 126, 128; 127, identifier:len; 128, argument_list; 128, 129; 129, identifier:fields; 130, integer:2; 131, block; 131, 132; 131, 148; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:LOGGER; 136, identifier:warning; 137, argument_list; 137, 138; 138, call; 138, 139; 138, 145; 139, attribute; 139, 140; 139, 144; 140, parenthesized_expression; 140, 141; 141, concatenated_string; 141, 142; 141, 143; 142, string:'skipping line {} of pattern file (not '; 143, string:'in tab-separated format): {}'; 144, identifier:format; 145, argument_list; 145, 146; 145, 147; 146, identifier:lineno; 147, identifier:line; 148, continue_statement; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 154; 151, pattern_list; 151, 152; 151, 153; 152, identifier:before; 153, identifier:after; 154, identifier:fields; 155, elif_clause; 155, 156; 155, 159; 156, comparison_operator:==; 156, 157; 156, 158; 157, identifier:pattern_format; 158, string:'sed'; 159, block; 159, 160; 159, 166; 159, 174; 159, 267; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:before; 163, assignment; 163, 164; 163, 165; 164, identifier:after; 165, None; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:line; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:line; 172, identifier:lstrip; 173, argument_list; 174, if_statement; 174, 175; 174, 180; 175, comparison_operator:==; 175, 176; 175, 179; 176, subscript; 176, 177; 176, 178; 177, identifier:line; 178, integer:0; 179, string:'s'; 180, block; 180, 181; 180, 187; 180, 188; 180, 189; 180, 200; 180, 215; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:delim; 184, subscript; 184, 185; 184, 186; 185, identifier:line; 186, integer:1; 187, comment; 188, comment; 189, if_statement; 189, 190; 189, 193; 190, comparison_operator:in; 190, 191; 190, 192; 191, identifier:delim; 192, string:'.^$*+?[](){}|\\'; 193, block; 193, 194; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:delim; 197, binary_operator:+; 197, 198; 197, 199; 198, string:'\\'; 199, identifier:delim; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 203; 202, identifier:fields; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:re; 206, identifier:split; 207, argument_list; 207, 208; 207, 214; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, string:r'(?<!\\){}'; 211, identifier:format; 212, argument_list; 212, 213; 213, identifier:delim; 214, identifier:line; 215, if_statement; 215, 216; 215, 222; 216, comparison_operator:==; 216, 217; 216, 221; 217, call; 217, 218; 217, 219; 218, identifier:len; 219, argument_list; 219, 220; 220, identifier:fields; 221, integer:4; 222, block; 222, 223; 222, 235; 222, 251; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 228; 225, pattern_list; 225, 226; 225, 227; 226, identifier:before; 227, identifier:after; 228, expression_list; 228, 229; 228, 232; 229, subscript; 229, 230; 229, 231; 230, identifier:fields; 231, integer:1; 232, subscript; 232, 233; 232, 234; 233, identifier:fields; 234, integer:2; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 238; 237, identifier:before; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:re; 241, identifier:sub; 242, argument_list; 242, 243; 242, 249; 242, 250; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, string:r'(?<!\\)\\{}'; 246, identifier:format; 247, argument_list; 247, 248; 248, identifier:delim; 249, identifier:delim; 250, identifier:before; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 254; 253, identifier:after; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:re; 257, identifier:sub; 258, argument_list; 258, 259; 258, 265; 258, 266; 259, call; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, string:r'(?<!\\)\\{}'; 262, identifier:format; 263, argument_list; 263, 264; 264, identifier:delim; 265, identifier:delim; 266, identifier:after; 267, if_statement; 267, 268; 267, 275; 268, boolean_operator:or; 268, 269; 268, 272; 269, comparison_operator:is; 269, 270; 269, 271; 270, identifier:before; 271, None; 272, comparison_operator:is; 272, 273; 272, 274; 273, identifier:after; 274, None; 275, block; 275, 276; 275, 292; 276, expression_statement; 276, 277; 277, call; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:LOGGER; 280, identifier:warning; 281, argument_list; 281, 282; 282, call; 282, 283; 282, 289; 283, attribute; 283, 284; 283, 288; 284, parenthesized_expression; 284, 285; 285, concatenated_string; 285, 286; 285, 287; 286, string:'skipping line {} of pattern file (not '; 287, string:'in sed format): {}'; 288, identifier:format; 289, argument_list; 289, 290; 289, 291; 290, identifier:lineno; 291, identifier:line; 292, continue_statement; 293, expression_statement; 293, 294; 294, augmented_assignment:+=; 294, 295; 294, 296; 295, identifier:num_candidates; 296, integer:1; 297, if_statement; 297, 298; 297, 307; 298, boolean_operator:and; 298, 299; 298, 300; 299, identifier:on_word_boundaries; 300, comparison_operator:!=; 300, 301; 300, 302; 301, identifier:before; 302, call; 302, 303; 302, 306; 303, attribute; 303, 304; 303, 305; 304, identifier:before; 305, identifier:strip; 306, argument_list; 307, block; 307, 308; 308, expression_statement; 308, 309; 309, call; 309, 310; 309, 313; 310, attribute; 310, 311; 310, 312; 311, identifier:LOGGER; 312, identifier:warning; 313, argument_list; 313, 314; 314, call; 314, 315; 314, 322; 315, attribute; 315, 316; 315, 321; 316, parenthesized_expression; 316, 317; 317, concatenated_string; 317, 318; 317, 319; 317, 320; 318, string:'before pattern on line {} padded whitespace; '; 319, string:'this may interact strangely with the --words '; 320, string:'option: {}'; 321, identifier:format; 322, argument_list; 322, 323; 322, 324; 323, identifier:lineno; 324, identifier:line; 325, expression_statement; 325, 326; 326, assignment; 326, 327; 326, 328; 327, identifier:before; 328, call; 328, 329; 328, 330; 329, identifier:sub_escapes; 330, argument_list; 330, 331; 331, identifier:before; 332, expression_statement; 332, 333; 333, assignment; 333, 334; 333, 335; 334, identifier:after; 335, call; 335, 336; 335, 337; 336, identifier:sub_escapes; 337, argument_list; 337, 338; 338, identifier:after; 339, if_statement; 339, 340; 339, 341; 340, identifier:boundaries; 341, block; 341, 342; 342, expression_statement; 342, 343; 343, assignment; 343, 344; 343, 345; 344, identifier:before; 345, call; 345, 346; 345, 351; 346, attribute; 346, 347; 346, 350; 347, attribute; 347, 348; 347, 349; 348, identifier:fsed; 349, identifier:ahocorasick; 350, identifier:boundary_transform; 351, argument_list; 351, 352; 351, 353; 352, identifier:before; 353, identifier:on_word_boundaries; 354, expression_statement; 354, 355; 355, assignment; 355, 356; 355, 359; 356, subscript; 356, 357; 356, 358; 357, identifier:trie; 358, identifier:before; 359, identifier:after; 360, expression_statement; 360, 361; 361, call; 361, 362; 361, 365; 362, attribute; 362, 363; 362, 364; 363, identifier:LOGGER; 364, identifier:info; 365, argument_list; 365, 366; 366, call; 366, 367; 366, 370; 367, attribute; 367, 368; 367, 369; 368, string:'{} patterns loaded from {}'; 369, identifier:format; 370, argument_list; 370, 371; 370, 372; 371, identifier:num_candidates; 372, identifier:pattern_filename; 373, return_statement; 373, 374; 374, expression_list; 374, 375; 374, 376; 375, identifier:trie; 376, identifier:boundaries | def build_trie(pattern_filename, pattern_format, encoding, on_word_boundaries):
'''
Constructs a finite state machine for performing string rewriting.
Arguments:
- `pattern_filename`:
- `pattern_format`:
- `encoding`:
- `on_word_boundaries`:
'''
boundaries = on_word_boundaries
if pattern_format == 'auto' or not on_word_boundaries:
tsv, boundaries = detect_pattern_format(pattern_filename, encoding,
on_word_boundaries)
if pattern_format == 'auto':
if tsv:
pattern_format = 'tsv'
else:
pattern_format = 'sed'
trie = fsed.ahocorasick.AhoCorasickTrie()
num_candidates = 0
with open_file(pattern_filename) as pattern_file:
for lineno, line in enumerate(pattern_file):
line = line.decode(encoding).rstrip('\n')
if not line.strip():
continue
# decode the line
if pattern_format == 'tsv':
fields = line.split('\t')
if len(fields) != 2:
LOGGER.warning(('skipping line {} of pattern file (not '
'in tab-separated format): {}').format(lineno, line))
continue
before, after = fields
elif pattern_format == 'sed':
before = after = None
line = line.lstrip()
if line[0] == 's':
delim = line[1]
# delim might be a regex special character;
# escape it if necessary
if delim in '.^$*+?[](){}|\\':
delim = '\\' + delim
fields = re.split(r'(?<!\\){}'.format(delim), line)
if len(fields) == 4:
before, after = fields[1], fields[2]
before = re.sub(r'(?<!\\)\\{}'.format(delim), delim, before)
after = re.sub(r'(?<!\\)\\{}'.format(delim), delim, after)
if before is None or after is None:
LOGGER.warning(('skipping line {} of pattern file (not '
'in sed format): {}').format(lineno, line))
continue
num_candidates += 1
if on_word_boundaries and before != before.strip():
LOGGER.warning(('before pattern on line {} padded whitespace; '
'this may interact strangely with the --words '
'option: {}').format(lineno, line))
before = sub_escapes(before)
after = sub_escapes(after)
if boundaries:
before = fsed.ahocorasick.boundary_transform(before, on_word_boundaries)
trie[before] = after
LOGGER.info('{} patterns loaded from {}'.format(num_candidates,
pattern_filename))
return trie, boundaries |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:make_parser; 3, parameters; 3, 4; 3, 7; 4, default_parameter; 4, 5; 4, 6; 5, identifier:add_help; 6, True; 7, default_parameter; 7, 8; 7, 9; 8, identifier:exclude_args; 9, None; 10, block; 10, 11; 10, 13; 10, 22; 10, 33; 10, 42; 10, 60; 10, 86; 10, 108; 10, 126; 10, 141; 10, 159; 10, 177; 10, 195; 10, 213; 10, 235; 11, expression_statement; 11, 12; 12, comment; 13, if_statement; 13, 14; 13, 17; 14, comparison_operator:is; 14, 15; 14, 16; 15, identifier:exclude_args; 16, None; 17, block; 17, 18; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:exclude_args; 21, list:[]; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:parser; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:argparse; 28, identifier:ArgumentParser; 29, argument_list; 29, 30; 30, keyword_argument; 30, 31; 30, 32; 31, identifier:add_help; 32, identifier:add_help; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:parser; 37, identifier:description; 38, parenthesized_expression; 38, 39; 39, concatenated_string; 39, 40; 39, 41; 40, string:"Filter, transform and export a list of JSON "; 41, string:"objects on stdin to JSON or CSV on stdout"; 42, if_statement; 42, 43; 42, 46; 43, comparison_operator:not; 43, 44; 43, 45; 44, string:"--columns"; 45, identifier:exclude_args; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:parser; 51, identifier:add_argument; 52, argument_list; 52, 53; 52, 54; 52, 57; 53, string:"--columns"; 54, keyword_argument; 54, 55; 54, 56; 55, identifier:dest; 56, string:"columns_file"; 57, keyword_argument; 57, 58; 57, 59; 58, identifier:help; 59, string:"the JSON file specifying the columns to be output"; 60, if_statement; 60, 61; 60, 70; 61, boolean_operator:and; 61, 62; 61, 66; 62, parenthesized_expression; 62, 63; 63, comparison_operator:not; 63, 64; 63, 65; 64, string:"-i"; 65, identifier:exclude_args; 66, parenthesized_expression; 66, 67; 67, comparison_operator:not; 67, 68; 67, 69; 68, string:"--input"; 69, identifier:exclude_args; 70, block; 70, 71; 71, expression_statement; 71, 72; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:parser; 75, identifier:add_argument; 76, argument_list; 76, 77; 76, 78; 76, 79; 76, 82; 76, 85; 77, string:"-i"; 78, string:"--input"; 79, keyword_argument; 79, 80; 79, 81; 80, identifier:help; 81, string:"read input from the given file instead of from stdin"; 82, keyword_argument; 82, 83; 82, 84; 83, identifier:dest; 84, string:'input_data'; 85, comment; 86, if_statement; 86, 87; 86, 96; 87, boolean_operator:and; 87, 88; 87, 92; 88, parenthesized_expression; 88, 89; 89, comparison_operator:not; 89, 90; 89, 91; 90, string:"-c"; 91, identifier:exclude_args; 92, parenthesized_expression; 92, 93; 93, comparison_operator:not; 93, 94; 93, 95; 94, string:"--column"; 95, identifier:exclude_args; 96, block; 96, 97; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:parser; 101, identifier:add_argument; 102, argument_list; 102, 103; 102, 104; 102, 105; 103, string:"-c"; 104, string:"--column"; 105, keyword_argument; 105, 106; 105, 107; 106, identifier:action; 107, identifier:ColumnsAction; 108, if_statement; 108, 109; 108, 112; 109, comparison_operator:not; 109, 110; 109, 111; 110, string:"--pattern"; 111, identifier:exclude_args; 112, block; 112, 113; 113, expression_statement; 113, 114; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:parser; 117, identifier:add_argument; 118, argument_list; 118, 119; 118, 120; 118, 123; 119, string:"--pattern"; 120, keyword_argument; 120, 121; 120, 122; 121, identifier:action; 122, identifier:ColumnsAction; 123, keyword_argument; 123, 124; 123, 125; 124, identifier:nargs; 125, string:'+'; 126, if_statement; 126, 127; 126, 130; 127, comparison_operator:not; 127, 128; 127, 129; 128, string:"--max-length"; 129, identifier:exclude_args; 130, block; 130, 131; 131, expression_statement; 131, 132; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:parser; 135, identifier:add_argument; 136, argument_list; 136, 137; 136, 138; 137, string:"--max-length"; 138, keyword_argument; 138, 139; 138, 140; 139, identifier:action; 140, identifier:ColumnsAction; 141, if_statement; 141, 142; 141, 145; 142, comparison_operator:not; 142, 143; 142, 144; 143, string:"--strip"; 144, identifier:exclude_args; 145, block; 145, 146; 146, expression_statement; 146, 147; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:parser; 150, identifier:add_argument; 151, argument_list; 151, 152; 151, 153; 151, 156; 152, string:"--strip"; 153, keyword_argument; 153, 154; 153, 155; 154, identifier:nargs; 155, string:"?"; 156, keyword_argument; 156, 157; 156, 158; 157, identifier:action; 158, identifier:ColumnsAction; 159, if_statement; 159, 160; 159, 163; 160, comparison_operator:not; 160, 161; 160, 162; 161, string:"--deduplicate"; 162, identifier:exclude_args; 163, block; 163, 164; 164, expression_statement; 164, 165; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:parser; 168, identifier:add_argument; 169, argument_list; 169, 170; 169, 171; 169, 174; 170, string:"--deduplicate"; 171, keyword_argument; 171, 172; 171, 173; 172, identifier:nargs; 173, string:'?'; 174, keyword_argument; 174, 175; 174, 176; 175, identifier:action; 176, identifier:ColumnsAction; 177, if_statement; 177, 178; 177, 181; 178, comparison_operator:not; 178, 179; 178, 180; 179, string:"--case-sensitive"; 180, identifier:exclude_args; 181, block; 181, 182; 182, expression_statement; 182, 183; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:parser; 186, identifier:add_argument; 187, argument_list; 187, 188; 187, 189; 187, 192; 188, string:"--case-sensitive"; 189, keyword_argument; 189, 190; 189, 191; 190, identifier:nargs; 191, string:'?'; 192, keyword_argument; 192, 193; 192, 194; 193, identifier:action; 194, identifier:ColumnsAction; 195, if_statement; 195, 196; 195, 199; 196, comparison_operator:not; 196, 197; 196, 198; 197, string:"--unique"; 198, identifier:exclude_args; 199, block; 199, 200; 200, expression_statement; 200, 201; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:parser; 204, identifier:add_argument; 205, argument_list; 205, 206; 205, 207; 205, 210; 206, string:"--unique"; 207, keyword_argument; 207, 208; 207, 209; 208, identifier:nargs; 209, string:"?"; 210, keyword_argument; 210, 211; 210, 212; 211, identifier:action; 212, identifier:ColumnsAction; 213, if_statement; 213, 214; 213, 223; 214, boolean_operator:and; 214, 215; 214, 219; 215, parenthesized_expression; 215, 216; 216, comparison_operator:not; 216, 217; 216, 218; 217, string:"-p"; 218, identifier:exclude_args; 219, parenthesized_expression; 219, 220; 220, comparison_operator:not; 220, 221; 220, 222; 221, string:"--pretty"; 222, identifier:exclude_args; 223, block; 223, 224; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:parser; 228, identifier:add_argument; 229, argument_list; 229, 230; 229, 231; 229, 232; 230, string:"-p"; 231, string:"--pretty"; 232, keyword_argument; 232, 233; 232, 234; 233, identifier:action; 234, string:"store_true"; 235, return_statement; 235, 236; 236, identifier:parser | def make_parser(add_help=True, exclude_args=None):
"""Return an argparse.ArgumentParser object with losser's arguments.
Other projects can call this to get an ArgumentParser with losser's
command line interface to use as a parent parser for their own parser.
For example::
parent_parser = losser.cli.make_parser(
add_help=False, exclude_args=["-i"])
parser = argparse.ArgumentParser(
description="Export datasets from a CKAN site to JSON or CSV.",
parents=[parent_parser])
parser.add_argument(...
:param add_help: Whether or not to add losser's help text to the parser.
Pass add_help=False if you want to use your own help text in a child
parser.
:type add_help: bool
:param exclude_args: List of losser command-line arguments to exclude, use
this to exclude any default losser arguments that you don't want in
your own command. For example: exclude_args=["-i", "--max-length"].
:type exclude_args: list of strings
"""
if exclude_args is None:
exclude_args = []
parser = argparse.ArgumentParser(add_help=add_help)
parser.description = ("Filter, transform and export a list of JSON "
"objects on stdin to JSON or CSV on stdout")
if "--columns" not in exclude_args:
parser.add_argument(
"--columns", dest="columns_file",
help="the JSON file specifying the columns to be output",
)
if ("-i" not in exclude_args) and ("--input" not in exclude_args):
parser.add_argument(
"-i", "--input",
help="read input from the given file instead of from stdin",
dest='input_data', # Because input is a Python builtin.
)
if ("-c" not in exclude_args) and ("--column" not in exclude_args):
parser.add_argument("-c", "--column", action=ColumnsAction)
if "--pattern" not in exclude_args:
parser.add_argument("--pattern", action=ColumnsAction, nargs='+')
if "--max-length" not in exclude_args:
parser.add_argument("--max-length", action=ColumnsAction)
if "--strip" not in exclude_args:
parser.add_argument("--strip", nargs="?", action=ColumnsAction)
if "--deduplicate" not in exclude_args:
parser.add_argument("--deduplicate", nargs='?', action=ColumnsAction)
if "--case-sensitive" not in exclude_args:
parser.add_argument(
"--case-sensitive", nargs='?', action=ColumnsAction)
if "--unique" not in exclude_args:
parser.add_argument("--unique", nargs="?", action=ColumnsAction)
if ("-p" not in exclude_args) and ("--pretty" not in exclude_args):
parser.add_argument("-p", "--pretty", action="store_true")
return parser |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:parse; 3, parameters; 3, 4; 3, 7; 4, default_parameter; 4, 5; 4, 6; 5, identifier:parser; 6, None; 7, default_parameter; 7, 8; 7, 9; 8, identifier:args; 9, None; 10, block; 10, 11; 10, 13; 10, 23; 10, 47; 10, 72; 10, 118; 10, 171; 11, expression_statement; 11, 12; 12, comment; 13, if_statement; 13, 14; 13, 16; 14, not_operator; 14, 15; 15, identifier:parser; 16, block; 16, 17; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:parser; 20, call; 20, 21; 20, 22; 21, identifier:make_parser; 22, argument_list; 23, try_statement; 23, 24; 23, 34; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:parsed_args; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:parser; 31, identifier:parse_args; 32, argument_list; 32, 33; 33, identifier:args; 34, except_clause; 34, 35; 34, 39; 35, as_pattern; 35, 36; 35, 37; 36, identifier:SystemExit; 37, as_pattern_target; 37, 38; 38, identifier:err; 39, block; 39, 40; 40, raise_statement; 40, 41; 41, call; 41, 42; 41, 43; 42, identifier:CommandLineExit; 43, argument_list; 43, 44; 44, attribute; 44, 45; 44, 46; 45, identifier:err; 46, identifier:code; 47, try_statement; 47, 48; 47, 55; 48, block; 48, 49; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:columns; 52, attribute; 52, 53; 52, 54; 53, identifier:parsed_args; 54, identifier:columns; 55, except_clause; 55, 56; 55, 57; 56, identifier:AttributeError; 57, block; 57, 58; 57, 66; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:columns; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:collections; 64, identifier:OrderedDict; 65, argument_list; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:parsed_args; 70, identifier:columns; 71, identifier:columns; 72, for_statement; 72, 73; 72, 76; 72, 81; 73, pattern_list; 73, 74; 73, 75; 74, identifier:title; 75, identifier:spec; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:columns; 79, identifier:items; 80, argument_list; 81, block; 81, 82; 81, 97; 81, 98; 82, if_statement; 82, 83; 82, 86; 83, comparison_operator:not; 83, 84; 83, 85; 84, string:"pattern"; 85, identifier:spec; 86, block; 86, 87; 87, raise_statement; 87, 88; 88, call; 88, 89; 88, 90; 89, identifier:ColumnWithoutPatternError; 90, argument_list; 90, 91; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, string:'Column "{0}" needs a pattern'; 94, identifier:format; 95, argument_list; 95, 96; 96, identifier:title; 97, comment; 98, if_statement; 98, 99; 98, 107; 99, comparison_operator:==; 99, 100; 99, 106; 100, call; 100, 101; 100, 102; 101, identifier:len; 102, argument_list; 102, 103; 103, subscript; 103, 104; 103, 105; 104, identifier:spec; 105, string:"pattern"; 106, integer:1; 107, block; 107, 108; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 113; 110, subscript; 110, 111; 110, 112; 111, identifier:spec; 112, string:"pattern"; 113, subscript; 113, 114; 113, 117; 114, subscript; 114, 115; 114, 116; 115, identifier:spec; 116, string:"pattern"; 117, integer:0; 118, if_statement; 118, 119; 118, 124; 118, 130; 118, 146; 118, 167; 119, boolean_operator:and; 119, 120; 119, 121; 120, identifier:columns; 121, attribute; 121, 122; 121, 123; 122, identifier:parsed_args; 123, identifier:columns_file; 124, block; 124, 125; 125, raise_statement; 125, 126; 126, call; 126, 127; 126, 128; 127, identifier:ColumnsAndColumnsFileError; 128, argument_list; 128, 129; 129, string:"You can't use the --column and --columns options together (yet)"; 130, elif_clause; 130, 131; 130, 137; 131, boolean_operator:and; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:parsed_args; 134, identifier:columns_file; 135, not_operator; 135, 136; 136, identifier:columns; 137, block; 137, 138; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:parsed_args; 142, identifier:columns; 143, attribute; 143, 144; 143, 145; 144, identifier:parsed_args; 145, identifier:columns_file; 146, elif_clause; 146, 147; 146, 156; 146, 157; 146, 158; 146, 159; 147, boolean_operator:and; 147, 148; 147, 151; 148, parenthesized_expression; 148, 149; 149, not_operator; 149, 150; 150, identifier:columns; 151, parenthesized_expression; 151, 152; 152, not_operator; 152, 153; 153, attribute; 153, 154; 153, 155; 154, identifier:parsed_args; 155, identifier:columns_file; 156, comment; 157, comment; 158, comment; 159, block; 159, 160; 160, raise_statement; 160, 161; 161, call; 161, 162; 161, 163; 162, identifier:NoColumnsError; 163, argument_list; 163, 164; 164, concatenated_string; 164, 165; 164, 166; 165, string:"You must give either a --columns or at least one -c/--column "; 166, string:"argument"; 167, else_clause; 167, 168; 168, block; 168, 169; 169, assert_statement; 169, 170; 170, identifier:columns; 171, return_statement; 171, 172; 172, identifier:parsed_args | def parse(parser=None, args=None):
"""Parse the command line arguments, return an argparse namespace object.
Other projects can call this function and pass in their own ArgumentParser
object (which should have a losser ArgumentParser from make_parser() above
as parent) to do the argument parsing and get the result (this does some
custom post-processing, beyond what argparse's parse_args() does). For
example::
parent_parser = losser.cli.make_parser(...)
parser = argparse.ArgumentParser(parents=[parent_parser])
parser.add_argument(...)
try:
parsed_args = losser.cli.parse(parser=parser)
except losser.cli.CommandLineError as err:
...
:raises CommandLineError: If something went wrong during command-line
parsing. If the exception has a non-empty .message attribute it
contains an error message that hasn't been printed to stdout yet,
otherwise any error message has already been printed.
:raises CommandLineExit: If the result of command-line parsing means that
the command should exit without continuing, but this is not because of
an error (for example if the user passed --help). Any help text will
already have been written to stdout, the exit code that the process
should exit with is in the exception's .code attribute.
CommandLineExit is a subclass of CommandLineError above.
"""
if not parser:
parser = make_parser()
try:
parsed_args = parser.parse_args(args)
except SystemExit as err:
raise CommandLineExit(err.code)
try:
columns = parsed_args.columns
except AttributeError:
columns = collections.OrderedDict()
parsed_args.columns = columns
for title, spec in columns.items():
if "pattern" not in spec:
raise ColumnWithoutPatternError(
'Column "{0}" needs a pattern'.format(title))
# Change length-1 patterns into strings (not lists of one string).
if len(spec["pattern"]) == 1:
spec["pattern"] = spec["pattern"][0]
if columns and parsed_args.columns_file:
raise ColumnsAndColumnsFileError(
"You can't use the --column and --columns options together (yet)")
elif parsed_args.columns_file and not columns:
parsed_args.columns = parsed_args.columns_file
elif (not columns) and (not parsed_args.columns_file):
# Crash if no columns specified.
# In the future we'll support simply converting all JSON fields to CSV
# columns if no columns are specified, and this will be removed.
raise NoColumnsError(
"You must give either a --columns or at least one -c/--column "
"argument")
else:
assert columns
return parsed_args |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 21; 2, function_name:read_value; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 4, identifier:ftype; 5, identifier:prompt; 6, default_parameter; 6, 7; 6, 8; 7, identifier:default; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:minval; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:maxval; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:allowed_single_chars; 17, None; 18, default_parameter; 18, 19; 18, 20; 19, identifier:question_mark; 20, True; 21, block; 21, 22; 21, 24; 21, 25; 21, 29; 21, 30; 21, 43; 21, 44; 21, 81; 21, 82; 21, 119; 21, 120; 21, 185; 21, 186; 21, 190; 21, 404; 22, expression_statement; 22, 23; 23, comment; 24, comment; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:result; 28, None; 29, comment; 30, if_statement; 30, 31; 30, 32; 30, 37; 31, identifier:question_mark; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:cquestion_mark; 36, string:' ? '; 37, else_clause; 37, 38; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:cquestion_mark; 42, string:''; 43, comment; 44, if_statement; 44, 45; 44, 48; 44, 75; 45, comparison_operator:is; 45, 46; 45, 47; 46, identifier:minval; 47, None; 48, block; 48, 49; 49, try_statement; 49, 50; 49, 58; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:iminval; 54, call; 54, 55; 54, 56; 55, identifier:ftype; 56, argument_list; 56, 57; 57, identifier:minval; 58, except_clause; 58, 59; 58, 60; 59, identifier:ValueError; 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, 74; 66, binary_operator:+; 66, 67; 66, 73; 67, binary_operator:+; 67, 68; 67, 69; 68, string:"'"; 69, call; 69, 70; 69, 71; 70, identifier:str; 71, argument_list; 71, 72; 72, identifier:minval; 73, string:"' cannot "; 74, string:"be used as an minval in readi()"; 75, else_clause; 75, 76; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:iminval; 80, None; 81, comment; 82, if_statement; 82, 83; 82, 86; 82, 113; 83, comparison_operator:is; 83, 84; 83, 85; 84, identifier:maxval; 85, None; 86, block; 86, 87; 87, try_statement; 87, 88; 87, 96; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:imaxval; 92, call; 92, 93; 92, 94; 93, identifier:ftype; 94, argument_list; 94, 95; 95, identifier:maxval; 96, except_clause; 96, 97; 96, 98; 97, identifier:ValueError; 98, block; 98, 99; 99, raise_statement; 99, 100; 100, call; 100, 101; 100, 102; 101, identifier:ValueError; 102, argument_list; 102, 103; 103, binary_operator:+; 103, 104; 103, 112; 104, binary_operator:+; 104, 105; 104, 111; 105, binary_operator:+; 105, 106; 105, 107; 106, string:"'"; 107, call; 107, 108; 107, 109; 108, identifier:str; 109, argument_list; 109, 110; 110, identifier:maxval; 111, string:"' cannot "; 112, string:"be used as an maxval in readi()"; 113, else_clause; 113, 114; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:imaxval; 118, None; 119, comment; 120, if_statement; 120, 121; 120, 128; 120, 133; 120, 149; 120, 165; 121, boolean_operator:and; 121, 122; 121, 125; 122, comparison_operator:is; 122, 123; 122, 124; 123, identifier:minval; 124, None; 125, comparison_operator:is; 125, 126; 125, 127; 126, identifier:maxval; 127, None; 128, block; 128, 129; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:cminmax; 132, string:''; 133, elif_clause; 133, 134; 133, 137; 134, comparison_operator:is; 134, 135; 134, 136; 135, identifier:minval; 136, None; 137, block; 137, 138; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:cminmax; 141, binary_operator:+; 141, 142; 141, 148; 142, binary_operator:+; 142, 143; 142, 144; 143, string:' (number <= '; 144, call; 144, 145; 144, 146; 145, identifier:str; 146, argument_list; 146, 147; 147, identifier:imaxval; 148, string:')'; 149, elif_clause; 149, 150; 149, 153; 150, comparison_operator:is; 150, 151; 150, 152; 151, identifier:maxval; 152, None; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:cminmax; 157, binary_operator:+; 157, 158; 157, 164; 158, binary_operator:+; 158, 159; 158, 160; 159, string:' (number >= '; 160, call; 160, 161; 160, 162; 161, identifier:str; 162, argument_list; 162, 163; 163, identifier:iminval; 164, string:')'; 165, else_clause; 165, 166; 166, block; 166, 167; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:cminmax; 170, binary_operator:+; 170, 171; 170, 184; 171, binary_operator:+; 171, 172; 171, 180; 172, binary_operator:+; 172, 173; 172, 179; 173, binary_operator:+; 173, 174; 173, 175; 174, string:' ('; 175, call; 175, 176; 175, 177; 176, identifier:str; 177, argument_list; 177, 178; 178, identifier:minval; 179, string:' <= number <= '; 180, call; 180, 181; 180, 182; 181, identifier:str; 182, argument_list; 182, 183; 183, identifier:maxval; 184, string:')'; 185, comment; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:loop; 189, True; 190, while_statement; 190, 191; 190, 192; 190, 193; 191, identifier:loop; 192, comment; 193, block; 193, 194; 193, 250; 193, 251; 193, 265; 193, 281; 193, 282; 193, 302; 193, 303; 194, if_statement; 194, 195; 194, 198; 194, 219; 195, comparison_operator:is; 195, 196; 195, 197; 196, identifier:default; 197, None; 198, block; 198, 199; 198, 211; 199, expression_statement; 199, 200; 200, call; 200, 201; 200, 202; 201, identifier:print; 202, argument_list; 202, 203; 202, 208; 203, binary_operator:+; 203, 204; 203, 207; 204, binary_operator:+; 204, 205; 204, 206; 205, identifier:prompt; 206, identifier:cminmax; 207, identifier:cquestion_mark; 208, keyword_argument; 208, 209; 208, 210; 209, identifier:end; 210, string:''; 211, expression_statement; 211, 212; 212, call; 212, 213; 212, 218; 213, attribute; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:sys; 216, identifier:stdout; 217, identifier:flush; 218, argument_list; 219, else_clause; 219, 220; 220, block; 220, 221; 220, 242; 221, expression_statement; 221, 222; 222, call; 222, 223; 222, 224; 223, identifier:print; 224, argument_list; 224, 225; 224, 239; 225, binary_operator:+; 225, 226; 225, 238; 226, binary_operator:+; 226, 227; 226, 237; 227, binary_operator:+; 227, 228; 227, 233; 228, binary_operator:+; 228, 229; 228, 232; 229, binary_operator:+; 229, 230; 229, 231; 230, identifier:prompt; 231, identifier:cminmax; 232, string:' ['; 233, call; 233, 234; 233, 235; 234, identifier:str; 235, argument_list; 235, 236; 236, identifier:default; 237, string:']'; 238, identifier:cquestion_mark; 239, keyword_argument; 239, 240; 239, 241; 240, identifier:end; 241, string:''; 242, expression_statement; 242, 243; 243, call; 243, 244; 243, 249; 244, attribute; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:sys; 247, identifier:stdout; 248, identifier:flush; 249, argument_list; 250, comment; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 254; 253, identifier:cresult; 254, call; 254, 255; 254, 264; 255, attribute; 255, 256; 255, 263; 256, call; 256, 257; 256, 262; 257, attribute; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:sys; 260, identifier:stdin; 261, identifier:readline; 262, argument_list; 263, identifier:strip; 264, argument_list; 265, if_statement; 265, 266; 265, 273; 266, boolean_operator:and; 266, 267; 266, 270; 267, comparison_operator:==; 267, 268; 267, 269; 268, identifier:cresult; 269, string:''; 270, comparison_operator:is; 270, 271; 270, 272; 271, identifier:default; 272, None; 273, block; 273, 274; 274, expression_statement; 274, 275; 275, assignment; 275, 276; 275, 277; 276, identifier:cresult; 277, call; 277, 278; 277, 279; 278, identifier:str; 279, argument_list; 279, 280; 280, identifier:default; 281, comment; 282, if_statement; 282, 283; 282, 289; 283, comparison_operator:==; 283, 284; 283, 288; 284, call; 284, 285; 284, 286; 285, identifier:len; 286, argument_list; 286, 287; 287, identifier:cresult; 288, integer:1; 289, block; 289, 290; 290, if_statement; 290, 291; 290, 294; 291, comparison_operator:is; 291, 292; 291, 293; 292, identifier:allowed_single_chars; 293, None; 294, block; 294, 295; 295, if_statement; 295, 296; 295, 299; 296, comparison_operator:in; 296, 297; 296, 298; 297, identifier:cresult; 298, identifier:allowed_single_chars; 299, block; 299, 300; 300, return_statement; 300, 301; 301, identifier:cresult; 302, comment; 303, try_statement; 303, 304; 303, 312; 303, 327; 304, block; 304, 305; 305, expression_statement; 305, 306; 306, assignment; 306, 307; 306, 308; 307, identifier:result; 308, call; 308, 309; 308, 310; 309, identifier:ftype; 310, argument_list; 310, 311; 311, identifier:cresult; 312, except_clause; 312, 313; 312, 314; 313, identifier:ValueError; 314, block; 314, 315; 315, expression_statement; 315, 316; 316, call; 316, 317; 316, 318; 317, identifier:print; 318, argument_list; 318, 319; 319, binary_operator:+; 319, 320; 319, 326; 320, binary_operator:+; 320, 321; 320, 322; 321, string:"*** Error: invalid "; 322, call; 322, 323; 322, 324; 323, identifier:str; 324, argument_list; 324, 325; 325, identifier:ftype; 326, string:" value. Try again!"; 327, else_clause; 327, 328; 327, 329; 328, comment; 329, block; 329, 330; 330, if_statement; 330, 331; 330, 338; 330, 343; 330, 364; 330, 385; 331, boolean_operator:and; 331, 332; 331, 335; 332, comparison_operator:is; 332, 333; 332, 334; 333, identifier:minval; 334, None; 335, comparison_operator:is; 335, 336; 335, 337; 336, identifier:maxval; 337, None; 338, block; 338, 339; 339, expression_statement; 339, 340; 340, assignment; 340, 341; 340, 342; 341, identifier:loop; 342, False; 343, elif_clause; 343, 344; 343, 347; 344, comparison_operator:is; 344, 345; 344, 346; 345, identifier:minval; 346, None; 347, block; 347, 348; 348, if_statement; 348, 349; 348, 352; 348, 357; 349, comparison_operator:<=; 349, 350; 349, 351; 350, identifier:result; 351, identifier:imaxval; 352, block; 352, 353; 353, expression_statement; 353, 354; 354, assignment; 354, 355; 354, 356; 355, identifier:loop; 356, False; 357, else_clause; 357, 358; 358, block; 358, 359; 359, expression_statement; 359, 360; 360, call; 360, 361; 360, 362; 361, identifier:print; 362, argument_list; 362, 363; 363, string:"*** Error: number out of range. Try again!"; 364, elif_clause; 364, 365; 364, 368; 365, comparison_operator:is; 365, 366; 365, 367; 366, identifier:maxval; 367, None; 368, block; 368, 369; 369, if_statement; 369, 370; 369, 373; 369, 378; 370, comparison_operator:>=; 370, 371; 370, 372; 371, identifier:result; 372, identifier:iminval; 373, block; 373, 374; 374, expression_statement; 374, 375; 375, assignment; 375, 376; 375, 377; 376, identifier:loop; 377, False; 378, else_clause; 378, 379; 379, block; 379, 380; 380, expression_statement; 380, 381; 381, call; 381, 382; 381, 383; 382, identifier:print; 383, argument_list; 383, 384; 384, string:"*** Error: number out of range. Try again!"; 385, else_clause; 385, 386; 386, block; 386, 387; 387, if_statement; 387, 388; 387, 392; 387, 397; 388, comparison_operator:<=; 388, 389; 388, 390; 388, 391; 389, identifier:iminval; 390, identifier:result; 391, identifier:imaxval; 392, block; 392, 393; 393, expression_statement; 393, 394; 394, assignment; 394, 395; 394, 396; 395, identifier:loop; 396, False; 397, else_clause; 397, 398; 398, block; 398, 399; 399, expression_statement; 399, 400; 400, call; 400, 401; 400, 402; 401, identifier:print; 402, argument_list; 402, 403; 403, string:"*** Error: number out of range. Try again!"; 404, return_statement; 404, 405; 405, identifier:result | def read_value(ftype, prompt, default=None, minval=None, maxval=None,
allowed_single_chars=None, question_mark=True):
"""Return value read from keyboard
Parameters
----------
ftype : int() or float()
Function defining the expected type.
prompt : str
Prompt string.
default : int or None
Default value.
minval : int or None
Mininum allowed value.
maxval : int or None
Maximum allowed value.
allowed_single_chars : str
String containing allowed valid characters.
question_mark : bool
If True, display question mark after prompt.
Returns
-------
result : integer, float or str
Integer, float of single character.
"""
# avoid PyCharm warning 'might be referenced before assignment'
result = None
# question mark
if question_mark:
cquestion_mark = ' ? '
else:
cquestion_mark = ''
# check minimum value
if minval is not None:
try:
iminval = ftype(minval)
except ValueError:
raise ValueError("'" + str(minval) + "' cannot " +
"be used as an minval in readi()")
else:
iminval = None
# check maximum value
if maxval is not None:
try:
imaxval = ftype(maxval)
except ValueError:
raise ValueError("'" + str(maxval) + "' cannot " +
"be used as an maxval in readi()")
else:
imaxval = None
# minimum and maximum values
if minval is None and maxval is None:
cminmax = ''
elif minval is None:
cminmax = ' (number <= ' + str(imaxval) + ')'
elif maxval is None:
cminmax = ' (number >= ' + str(iminval) + ')'
else:
cminmax = ' (' + str(minval) + ' <= number <= ' + str(maxval) + ')'
# main loop
loop = True
while loop:
# display prompt
if default is None:
print(prompt + cminmax + cquestion_mark, end='')
sys.stdout.flush()
else:
print(prompt + cminmax + ' [' + str(default) + ']' +
cquestion_mark, end='')
sys.stdout.flush()
# read user's input
cresult = sys.stdin.readline().strip()
if cresult == '' and default is not None:
cresult = str(default)
# if valid allowed single character, return character
if len(cresult) == 1:
if allowed_single_chars is not None:
if cresult in allowed_single_chars:
return cresult
# convert to ftype value
try:
result = ftype(cresult)
except ValueError:
print("*** Error: invalid " + str(ftype) + " value. Try again!")
else:
# check number is within expected range
if minval is None and maxval is None:
loop = False
elif minval is None:
if result <= imaxval:
loop = False
else:
print("*** Error: number out of range. Try again!")
elif maxval is None:
if result >= iminval:
loop = False
else:
print("*** Error: number out of range. Try again!")
else:
if iminval <= result <= imaxval:
loop = False
else:
print("*** Error: number out of range. Try again!")
return result |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:select_configuration; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:obresult; 6, block; 6, 7; 6, 9; 6, 18; 6, 25; 6, 26; 6, 34; 6, 44; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:logger; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:logging; 15, identifier:getLogger; 16, argument_list; 16, 17; 17, identifier:__name__; 18, expression_statement; 18, 19; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:logger; 22, identifier:debug; 23, argument_list; 23, 24; 24, string:'calling default configuration selector'; 25, comment; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:ref; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:obresult; 32, identifier:get_sample_frame; 33, argument_list; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:extr; 37, subscript; 37, 38; 37, 43; 38, attribute; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:datamodel; 42, identifier:extractor_map; 43, string:'fits'; 44, if_statement; 44, 45; 44, 46; 44, 47; 44, 189; 45, identifier:ref; 46, comment; 47, block; 47, 48; 47, 58; 47, 118; 47, 119; 47, 129; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:result; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:extr; 54, identifier:extract; 55, argument_list; 55, 56; 55, 57; 56, string:'insconf'; 57, identifier:ref; 58, if_statement; 58, 59; 58, 60; 58, 61; 59, identifier:result; 60, comment; 61, block; 61, 62; 61, 70; 61, 71; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:logger; 66, identifier:debug; 67, argument_list; 67, 68; 67, 69; 68, string:'found insconf config uuid=%s'; 69, identifier:result; 70, comment; 71, if_statement; 71, 72; 71, 77; 71, 84; 72, comparison_operator:in; 72, 73; 72, 74; 73, identifier:result; 74, attribute; 74, 75; 74, 76; 75, identifier:self; 76, identifier:configurations; 77, block; 77, 78; 78, return_statement; 78, 79; 79, subscript; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:self; 82, identifier:configurations; 83, identifier:result; 84, else_clause; 84, 85; 84, 86; 85, comment; 86, block; 86, 87; 87, for_statement; 87, 88; 87, 89; 87, 96; 87, 106; 88, identifier:conf; 89, call; 89, 90; 89, 95; 90, attribute; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:self; 93, identifier:configurations; 94, identifier:values; 95, argument_list; 96, block; 96, 97; 97, if_statement; 97, 98; 97, 103; 98, comparison_operator:==; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:conf; 101, identifier:name; 102, identifier:result; 103, block; 103, 104; 104, return_statement; 104, 105; 105, identifier:conf; 106, else_clause; 106, 107; 107, block; 107, 108; 108, raise_statement; 108, 109; 109, call; 109, 110; 109, 111; 110, identifier:KeyError; 111, argument_list; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, string:'insconf {} does not match any config'; 115, identifier:format; 116, argument_list; 116, 117; 117, identifier:result; 118, comment; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:date_obs; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:extr; 125, identifier:extract; 126, argument_list; 126, 127; 126, 128; 127, string:'observation_date'; 128, identifier:ref; 129, for_statement; 129, 130; 129, 133; 129, 140; 130, pattern_list; 130, 131; 130, 132; 131, identifier:key; 132, identifier:conf; 133, call; 133, 134; 133, 139; 134, attribute; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:configurations; 138, identifier:items; 139, argument_list; 140, block; 140, 141; 140, 148; 140, 169; 141, if_statement; 141, 142; 141, 145; 141, 146; 142, comparison_operator:==; 142, 143; 142, 144; 143, identifier:key; 144, string:'default'; 145, comment; 146, block; 146, 147; 147, continue_statement; 148, if_statement; 148, 149; 148, 154; 148, 163; 149, comparison_operator:is; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:conf; 152, identifier:date_end; 153, None; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:upper_t; 158, comparison_operator:<; 158, 159; 158, 160; 159, identifier:date_obs; 160, attribute; 160, 161; 160, 162; 161, identifier:conf; 162, identifier:date_end; 163, else_clause; 163, 164; 164, block; 164, 165; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:upper_t; 168, True; 169, if_statement; 169, 170; 169, 178; 170, boolean_operator:and; 170, 171; 170, 172; 171, identifier:upper_t; 172, parenthesized_expression; 172, 173; 173, comparison_operator:>=; 173, 174; 173, 175; 174, identifier:date_obs; 175, attribute; 175, 176; 175, 177; 176, identifier:conf; 177, identifier:date_start; 178, block; 178, 179; 178, 187; 179, expression_statement; 179, 180; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:logger; 183, identifier:debug; 184, argument_list; 184, 185; 184, 186; 185, string:'found date match, config uuid=%s'; 186, identifier:key; 187, return_statement; 187, 188; 188, identifier:conf; 189, else_clause; 189, 190; 190, block; 190, 191; 190, 198; 191, expression_statement; 191, 192; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:logger; 195, identifier:debug; 196, argument_list; 196, 197; 197, string:'no match, using default configuration'; 198, return_statement; 198, 199; 199, subscript; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:self; 202, identifier:configurations; 203, string:'default' | def select_configuration(self, obresult):
"""Select instrument configuration based on OB"""
logger = logging.getLogger(__name__)
logger.debug('calling default configuration selector')
# get first possible image
ref = obresult.get_sample_frame()
extr = self.datamodel.extractor_map['fits']
if ref:
# get INSCONF configuration
result = extr.extract('insconf', ref)
if result:
# found the keyword, try to match
logger.debug('found insconf config uuid=%s', result)
# Use insconf as uuid key
if result in self.configurations:
return self.configurations[result]
else:
# Additional check for conf.name
for conf in self.configurations.values():
if conf.name == result:
return conf
else:
raise KeyError('insconf {} does not match any config'.format(result))
# If not, try to match by DATE
date_obs = extr.extract('observation_date', ref)
for key, conf in self.configurations.items():
if key == 'default':
# skip default
continue
if conf.date_end is not None:
upper_t = date_obs < conf.date_end
else:
upper_t = True
if upper_t and (date_obs >= conf.date_start):
logger.debug('found date match, config uuid=%s', key)
return conf
else:
logger.debug('no match, using default configuration')
return self.configurations['default'] |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:pause_debugplot; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:debugplot; 5, default_parameter; 5, 6; 5, 7; 6, identifier:optional_prompt; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:pltshow; 10, False; 11, default_parameter; 11, 12; 11, 13; 12, identifier:tight_layout; 13, True; 14, block; 14, 15; 14, 17; 14, 28; 14, 52; 14, 189; 15, expression_statement; 15, 16; 16, comment; 17, if_statement; 17, 18; 17, 21; 18, comparison_operator:not; 18, 19; 18, 20; 19, identifier:debugplot; 20, identifier:DEBUGPLOT_CODES; 21, block; 21, 22; 22, raise_statement; 22, 23; 23, call; 23, 24; 23, 25; 24, identifier:ValueError; 25, argument_list; 25, 26; 25, 27; 26, string:'Invalid debugplot value:'; 27, identifier:debugplot; 28, if_statement; 28, 29; 28, 32; 28, 42; 29, comparison_operator:<; 29, 30; 29, 31; 30, identifier:debugplot; 31, integer:0; 32, block; 32, 33; 32, 38; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:debugplot_; 36, unary_operator:-; 36, 37; 37, identifier:debugplot; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:pltclose; 41, True; 42, else_clause; 42, 43; 43, block; 43, 44; 43, 48; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:debugplot_; 47, identifier:debugplot; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:pltclose; 51, False; 52, if_statement; 52, 53; 52, 54; 52, 134; 53, identifier:pltshow; 54, block; 54, 55; 55, if_statement; 55, 56; 55, 65; 56, comparison_operator:in; 56, 57; 56, 58; 57, identifier:debugplot_; 58, list:[1, 2, 11, 12, 21, 22]; 58, 59; 58, 60; 58, 61; 58, 62; 58, 63; 58, 64; 59, integer:1; 60, integer:2; 61, integer:11; 62, integer:12; 63, integer:21; 64, integer:22; 65, block; 65, 66; 65, 75; 66, if_statement; 66, 67; 66, 68; 67, identifier:tight_layout; 68, block; 68, 69; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:plt; 73, identifier:tight_layout; 74, argument_list; 75, if_statement; 75, 76; 75, 82; 75, 99; 76, comparison_operator:in; 76, 77; 76, 78; 77, identifier:debugplot_; 78, list:[1, 11, 21]; 78, 79; 78, 80; 78, 81; 79, integer:1; 80, integer:11; 81, integer:21; 82, block; 82, 83; 82, 92; 83, expression_statement; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:plt; 87, identifier:show; 88, argument_list; 88, 89; 89, keyword_argument; 89, 90; 89, 91; 90, identifier:block; 91, False; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:plt; 96, identifier:pause; 97, argument_list; 97, 98; 98, float:0.2; 99, elif_clause; 99, 100; 99, 106; 100, comparison_operator:in; 100, 101; 100, 102; 101, identifier:debugplot_; 102, list:[2, 12, 22]; 102, 103; 102, 104; 102, 105; 103, integer:2; 104, integer:12; 105, integer:22; 106, block; 106, 107; 106, 115; 106, 123; 106, 129; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 110; 109, identifier:print; 110, argument_list; 110, 111; 110, 112; 111, string:'Press "q" to continue...'; 112, keyword_argument; 112, 113; 112, 114; 113, identifier:end; 114, string:''; 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:sys; 120, identifier:stdout; 121, identifier:flush; 122, argument_list; 123, expression_statement; 123, 124; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:plt; 127, identifier:show; 128, argument_list; 129, expression_statement; 129, 130; 130, call; 130, 131; 130, 132; 131, identifier:print; 132, argument_list; 132, 133; 133, string:''; 134, else_clause; 134, 135; 135, block; 135, 136; 136, if_statement; 136, 137; 136, 143; 137, comparison_operator:in; 137, 138; 137, 139; 138, identifier:debugplot_; 139, list:[2, 12, 22]; 139, 140; 139, 141; 139, 142; 140, integer:2; 141, integer:12; 142, integer:22; 143, block; 143, 144; 143, 167; 143, 175; 144, if_statement; 144, 145; 144, 148; 144, 157; 145, comparison_operator:is; 145, 146; 145, 147; 146, identifier:optional_prompt; 147, None; 148, block; 148, 149; 149, expression_statement; 149, 150; 150, call; 150, 151; 150, 152; 151, identifier:print; 152, argument_list; 152, 153; 152, 154; 153, string:'Press <RETURN> to continue...'; 154, keyword_argument; 154, 155; 154, 156; 155, identifier:end; 156, string:''; 157, else_clause; 157, 158; 158, block; 158, 159; 159, expression_statement; 159, 160; 160, call; 160, 161; 160, 162; 161, identifier:print; 162, argument_list; 162, 163; 162, 164; 163, identifier:optional_prompt; 164, keyword_argument; 164, 165; 164, 166; 165, identifier:end; 166, string:''; 167, expression_statement; 167, 168; 168, call; 168, 169; 168, 174; 169, attribute; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:sys; 172, identifier:stdout; 173, identifier:flush; 174, argument_list; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:cdummy; 178, call; 178, 179; 178, 188; 179, attribute; 179, 180; 179, 187; 180, call; 180, 181; 180, 186; 181, attribute; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:sys; 184, identifier:stdin; 185, identifier:readline; 186, argument_list; 187, identifier:strip; 188, argument_list; 189, if_statement; 189, 190; 189, 201; 190, boolean_operator:and; 190, 191; 190, 200; 191, comparison_operator:in; 191, 192; 191, 193; 192, identifier:debugplot_; 193, list:[1, 2, 11, 12, 21, 22]; 193, 194; 193, 195; 193, 196; 193, 197; 193, 198; 193, 199; 194, integer:1; 195, integer:2; 196, integer:11; 197, integer:12; 198, integer:21; 199, integer:22; 200, identifier:pltclose; 201, block; 201, 202; 202, expression_statement; 202, 203; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:plt; 206, identifier:close; 207, argument_list | def pause_debugplot(debugplot, optional_prompt=None, pltshow=False,
tight_layout=True):
"""Ask the user to press RETURN to continue after plotting.
Parameters
----------
debugplot : int
Determines whether intermediate computations and/or plots
are displayed:
00 : no debug, no plots
01 : no debug, plots without pauses
02 : no debug, plots with pauses
10 : debug, no plots
11 : debug, plots without pauses
12 : debug, plots with pauses
21 : debug, extra plots without pauses
22 : debug, extra plots with pause
NOTE: negative values are also valid and indicate that a call
to plt.close() is also performed
optional_prompt : string
Optional prompt.
pltshow : bool
If True, a call to plt.show() is also performed.
tight_layout : bool
If True, and pltshow=True, a call to plt.tight_layout() is
also performed.
"""
if debugplot not in DEBUGPLOT_CODES:
raise ValueError('Invalid debugplot value:', debugplot)
if debugplot < 0:
debugplot_ = -debugplot
pltclose = True
else:
debugplot_ = debugplot
pltclose = False
if pltshow:
if debugplot_ in [1, 2, 11, 12, 21, 22]:
if tight_layout:
plt.tight_layout()
if debugplot_ in [1, 11, 21]:
plt.show(block=False)
plt.pause(0.2)
elif debugplot_ in [2, 12, 22]:
print('Press "q" to continue...', end='')
sys.stdout.flush()
plt.show()
print('')
else:
if debugplot_ in [2, 12, 22]:
if optional_prompt is None:
print('Press <RETURN> to continue...', end='')
else:
print(optional_prompt, end='')
sys.stdout.flush()
cdummy = sys.stdin.readline().strip()
if debugplot_ in [1, 2, 11, 12, 21, 22] and pltclose:
plt.close() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:mode_half_sample; 3, parameters; 3, 4; 3, 5; 4, identifier:a; 5, default_parameter; 5, 6; 5, 7; 6, identifier:is_sorted; 7, False; 8, block; 8, 9; 8, 11; 8, 20; 8, 39; 8, 46; 9, expression_statement; 9, 10; 10, string:'''
Estimate the mode using the Half Sample mode.
A method to estimate the mode, as described in
D. R. Bickel and R. Frühwirth (contributed equally),
"On a fast, robust estimator of the mode: Comparisons to other
robust estimators with applications,"
Computational Statistics and Data Analysis 50, 3500-3530 (2006).
Example
=======
>> import numpy as np
>> np.random.seed(1392838)
>> a = np.random.normal(1000, 200, size=1000)
>> a[:100] = np.random.normal(2000, 300, size=100)
>> b = np.sort(a)
>> mode_half_sample(b, is_sorted=True)
1041.9327885039545
'''; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:a; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:np; 17, identifier:asanyarray; 18, argument_list; 18, 19; 19, identifier:a; 20, if_statement; 20, 21; 20, 23; 20, 33; 21, not_operator; 21, 22; 22, identifier:is_sorted; 23, block; 23, 24; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:sdata; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:np; 30, identifier:sort; 31, argument_list; 31, 32; 32, identifier:a; 33, else_clause; 33, 34; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:sdata; 38, identifier:a; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:n; 42, call; 42, 43; 42, 44; 43, identifier:len; 44, argument_list; 44, 45; 45, identifier:sdata; 46, if_statement; 46, 47; 46, 50; 46, 55; 46, 71; 46, 131; 47, comparison_operator:==; 47, 48; 47, 49; 48, identifier:n; 49, integer:1; 50, block; 50, 51; 51, return_statement; 51, 52; 52, subscript; 52, 53; 52, 54; 53, identifier:sdata; 54, integer:0; 55, elif_clause; 55, 56; 55, 59; 56, comparison_operator:==; 56, 57; 56, 58; 57, identifier:n; 58, integer:2; 59, block; 59, 60; 60, return_statement; 60, 61; 61, binary_operator:*; 61, 62; 61, 63; 62, float:0.5; 63, parenthesized_expression; 63, 64; 64, binary_operator:+; 64, 65; 64, 68; 65, subscript; 65, 66; 65, 67; 66, identifier:sdata; 67, integer:0; 68, subscript; 68, 69; 68, 70; 69, identifier:sdata; 70, integer:1; 71, elif_clause; 71, 72; 71, 75; 72, comparison_operator:==; 72, 73; 72, 74; 73, identifier:n; 74, integer:3; 75, block; 75, 76; 75, 93; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:ind; 79, binary_operator:-; 79, 80; 79, 90; 80, binary_operator:+; 80, 81; 80, 85; 81, unary_operator:-; 81, 82; 82, subscript; 82, 83; 82, 84; 83, identifier:sdata; 84, integer:0; 85, binary_operator:*; 85, 86; 85, 87; 86, integer:2; 87, subscript; 87, 88; 87, 89; 88, identifier:sdata; 89, integer:1; 90, subscript; 90, 91; 90, 92; 91, identifier:sdata; 92, integer:2; 93, if_statement; 93, 94; 93, 97; 93, 109; 93, 125; 94, comparison_operator:<; 94, 95; 94, 96; 95, identifier:ind; 96, integer:0; 97, block; 97, 98; 98, return_statement; 98, 99; 99, binary_operator:*; 99, 100; 99, 101; 100, float:0.5; 101, parenthesized_expression; 101, 102; 102, binary_operator:+; 102, 103; 102, 106; 103, subscript; 103, 104; 103, 105; 104, identifier:sdata; 105, integer:0; 106, subscript; 106, 107; 106, 108; 107, identifier:sdata; 108, integer:1; 109, elif_clause; 109, 110; 109, 113; 110, comparison_operator:>; 110, 111; 110, 112; 111, identifier:ind; 112, integer:0; 113, block; 113, 114; 114, return_statement; 114, 115; 115, binary_operator:*; 115, 116; 115, 117; 116, float:0.5; 117, parenthesized_expression; 117, 118; 118, binary_operator:+; 118, 119; 118, 122; 119, subscript; 119, 120; 119, 121; 120, identifier:sdata; 121, integer:1; 122, subscript; 122, 123; 122, 124; 123, identifier:sdata; 124, integer:2; 125, else_clause; 125, 126; 126, block; 126, 127; 127, return_statement; 127, 128; 128, subscript; 128, 129; 128, 130; 129, identifier:sdata; 130, integer:1; 131, else_clause; 131, 132; 132, block; 132, 133; 132, 147; 132, 169; 132, 177; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:N; 136, call; 136, 137; 136, 138; 137, identifier:int; 138, argument_list; 138, 139; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:math; 142, identifier:ceil; 143, argument_list; 143, 144; 144, binary_operator:/; 144, 145; 144, 146; 145, identifier:n; 146, float:2.0; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:w; 150, binary_operator:-; 150, 151; 150, 159; 151, subscript; 151, 152; 151, 153; 152, identifier:sdata; 153, slice; 153, 154; 153, 158; 154, parenthesized_expression; 154, 155; 155, binary_operator:-; 155, 156; 155, 157; 156, identifier:N; 157, integer:1; 158, colon; 159, subscript; 159, 160; 159, 161; 160, identifier:sdata; 161, slice; 161, 162; 161, 163; 162, colon; 163, parenthesized_expression; 163, 164; 164, binary_operator:+; 164, 165; 164, 168; 165, binary_operator:-; 165, 166; 165, 167; 166, identifier:n; 167, identifier:N; 168, integer:1; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:ar; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:w; 175, identifier:argmin; 176, argument_list; 177, return_statement; 177, 178; 178, call; 178, 179; 178, 180; 179, identifier:mode_half_sample; 180, argument_list; 180, 181; 180, 189; 181, subscript; 181, 182; 181, 183; 182, identifier:sdata; 183, slice; 183, 184; 183, 185; 183, 186; 184, identifier:ar; 185, colon; 186, binary_operator:+; 186, 187; 186, 188; 187, identifier:ar; 188, identifier:N; 189, keyword_argument; 189, 190; 189, 191; 190, identifier:is_sorted; 191, True | def mode_half_sample(a, is_sorted=False):
'''
Estimate the mode using the Half Sample mode.
A method to estimate the mode, as described in
D. R. Bickel and R. Frühwirth (contributed equally),
"On a fast, robust estimator of the mode: Comparisons to other
robust estimators with applications,"
Computational Statistics and Data Analysis 50, 3500-3530 (2006).
Example
=======
>> import numpy as np
>> np.random.seed(1392838)
>> a = np.random.normal(1000, 200, size=1000)
>> a[:100] = np.random.normal(2000, 300, size=100)
>> b = np.sort(a)
>> mode_half_sample(b, is_sorted=True)
1041.9327885039545
'''
a = np.asanyarray(a)
if not is_sorted:
sdata = np.sort(a)
else:
sdata = a
n = len(sdata)
if n == 1:
return sdata[0]
elif n == 2:
return 0.5 * (sdata[0] + sdata[1])
elif n == 3:
ind = -sdata[0] + 2 * sdata[1] - sdata[2]
if ind < 0:
return 0.5 * (sdata[0] + sdata[1])
elif ind > 0:
return 0.5 * (sdata[1] + sdata[2])
else:
return sdata[1]
else:
N = int(math.ceil(n / 2.0))
w = sdata[(N-1):] - sdata[:(n-N+1)]
ar = w.argmin()
return mode_half_sample(sdata[ar:ar+N], is_sorted=True) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:author_id_normalize_and_schema; 3, parameters; 3, 4; 3, 5; 4, identifier:uid; 5, default_parameter; 5, 6; 5, 7; 6, identifier:schema; 7, None; 8, block; 8, 9; 8, 11; 8, 49; 8, 72; 8, 85; 8, 111; 8, 119; 8, 151; 8, 158; 8, 159; 9, expression_statement; 9, 10; 10, comment; 11, function_definition; 11, 12; 11, 13; 11, 16; 12, function_name:_get_uid_normalized_in_schema; 13, parameters; 13, 14; 13, 15; 14, identifier:_uid; 15, identifier:_schema; 16, block; 16, 17; 16, 25; 16, 34; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 22; 19, pattern_list; 19, 20; 19, 21; 20, identifier:regex; 21, identifier:template; 22, subscript; 22, 23; 22, 24; 23, identifier:_RE_AUTHORS_UID; 24, identifier:_schema; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:match; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:regex; 31, identifier:match; 32, argument_list; 32, 33; 33, identifier:_uid; 34, if_statement; 34, 35; 34, 36; 35, identifier:match; 36, block; 36, 37; 37, return_statement; 37, 38; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:template; 41, identifier:format; 42, argument_list; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:match; 46, identifier:group; 47, argument_list; 47, 48; 48, string:'uid'; 49, if_statement; 49, 50; 49, 62; 50, boolean_operator:and; 50, 51; 50, 57; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:idutils; 54, identifier:is_orcid; 55, argument_list; 55, 56; 56, identifier:uid; 57, comparison_operator:in; 57, 58; 57, 59; 58, identifier:schema; 59, tuple; 59, 60; 59, 61; 60, None; 61, string:'ORCID'; 62, block; 62, 63; 63, return_statement; 63, 64; 64, expression_list; 64, 65; 64, 71; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:idutils; 68, identifier:normalize_orcid; 69, argument_list; 69, 70; 70, identifier:uid; 71, string:'ORCID'; 72, if_statement; 72, 73; 72, 78; 72, 79; 73, boolean_operator:and; 73, 74; 73, 75; 74, identifier:schema; 75, comparison_operator:not; 75, 76; 75, 77; 76, identifier:schema; 77, identifier:_RE_AUTHORS_UID; 78, comment; 79, block; 79, 80; 80, raise_statement; 80, 81; 81, call; 81, 82; 81, 83; 82, identifier:UnknownUIDSchema; 83, argument_list; 83, 84; 84, identifier:uid; 85, if_statement; 85, 86; 85, 87; 86, identifier:schema; 87, block; 87, 88; 87, 96; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:normalized_uid; 91, call; 91, 92; 91, 93; 92, identifier:_get_uid_normalized_in_schema; 93, argument_list; 93, 94; 93, 95; 94, identifier:uid; 95, identifier:schema; 96, if_statement; 96, 97; 96, 98; 96, 103; 97, identifier:normalized_uid; 98, block; 98, 99; 99, return_statement; 99, 100; 100, expression_list; 100, 101; 100, 102; 101, identifier:normalized_uid; 102, identifier:schema; 103, else_clause; 103, 104; 104, block; 104, 105; 105, raise_statement; 105, 106; 106, call; 106, 107; 106, 108; 107, identifier:SchemaUIDConflict; 108, argument_list; 108, 109; 108, 110; 109, identifier:schema; 110, identifier:uid; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 116; 113, pattern_list; 113, 114; 113, 115; 114, identifier:match_schema; 115, identifier:normalized_uid; 116, expression_list; 116, 117; 116, 118; 117, None; 118, None; 119, for_statement; 119, 120; 119, 121; 119, 122; 120, identifier:candidate_schema; 121, identifier:_RE_AUTHORS_UID; 122, block; 122, 123; 122, 131; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:candidate_uid; 126, call; 126, 127; 126, 128; 127, identifier:_get_uid_normalized_in_schema; 128, argument_list; 128, 129; 128, 130; 129, identifier:uid; 130, identifier:candidate_schema; 131, if_statement; 131, 132; 131, 133; 132, identifier:candidate_uid; 133, block; 133, 134; 133, 143; 133, 147; 134, if_statement; 134, 135; 134, 136; 134, 137; 135, identifier:match_schema; 136, comment; 137, block; 137, 138; 138, raise_statement; 138, 139; 139, call; 139, 140; 139, 141; 140, identifier:UnknownUIDSchema; 141, argument_list; 141, 142; 142, identifier:uid; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:match_schema; 146, identifier:candidate_schema; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:normalized_uid; 150, identifier:candidate_uid; 151, if_statement; 151, 152; 151, 153; 152, identifier:match_schema; 153, block; 153, 154; 154, return_statement; 154, 155; 155, expression_list; 155, 156; 155, 157; 156, identifier:normalized_uid; 157, identifier:match_schema; 158, comment; 159, raise_statement; 159, 160; 160, call; 160, 161; 160, 162; 161, identifier:UnknownUIDSchema; 162, argument_list; 162, 163; 163, identifier:uid | def author_id_normalize_and_schema(uid, schema=None):
"""Detect and normalize an author UID schema.
Args:
uid (string): a UID string
schema (string): try to resolve to schema
Returns:
Tuple[string, string]: a tuple (uid, schema) where:
- uid: the UID normalized to comply with the id.json schema
- schema: a schema of the UID or *None* if not recognised
Raise:
UnknownUIDSchema: if UID is too little to definitively guess the schema
SchemaUIDConflict: if specified schema is not matching the given UID
"""
def _get_uid_normalized_in_schema(_uid, _schema):
regex, template = _RE_AUTHORS_UID[_schema]
match = regex.match(_uid)
if match:
return template.format(match.group('uid'))
if idutils.is_orcid(uid) and schema in (None, 'ORCID'):
return idutils.normalize_orcid(uid), 'ORCID'
if schema and schema not in _RE_AUTHORS_UID:
# Schema explicitly specified, but this function can't handle it
raise UnknownUIDSchema(uid)
if schema:
normalized_uid = _get_uid_normalized_in_schema(uid, schema)
if normalized_uid:
return normalized_uid, schema
else:
raise SchemaUIDConflict(schema, uid)
match_schema, normalized_uid = None, None
for candidate_schema in _RE_AUTHORS_UID:
candidate_uid = _get_uid_normalized_in_schema(uid, candidate_schema)
if candidate_uid:
if match_schema:
# Valid against more than one candidate schema, ambiguous
raise UnknownUIDSchema(uid)
match_schema = candidate_schema
normalized_uid = candidate_uid
if match_schema:
return normalized_uid, match_schema
# No guessess have been found
raise UnknownUIDSchema(uid) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:get_schema_path; 3, parameters; 3, 4; 3, 5; 4, identifier:schema; 5, default_parameter; 5, 6; 5, 7; 6, identifier:resolved; 7, False; 8, block; 8, 9; 8, 11; 8, 43; 8, 107; 8, 114; 8, 186; 9, expression_statement; 9, 10; 10, comment; 11, function_definition; 11, 12; 11, 13; 11, 15; 12, function_name:_strip_first_path_elem; 13, parameters; 13, 14; 14, identifier:path; 15, block; 15, 16; 15, 18; 15, 36; 16, expression_statement; 16, 17; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:stripped_path; 21, subscript; 21, 22; 21, 33; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:path; 25, identifier:split; 26, argument_list; 26, 27; 26, 32; 27, attribute; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:os; 30, identifier:path; 31, identifier:sep; 32, integer:1; 33, slice; 33, 34; 33, 35; 34, integer:1; 35, colon; 36, return_statement; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, string:''; 40, identifier:join; 41, argument_list; 41, 42; 42, identifier:stripped_path; 43, function_definition; 43, 44; 43, 45; 43, 47; 44, function_name:_schema_to_normalized_path; 45, parameters; 45, 46; 46, identifier:schema; 47, block; 47, 48; 47, 50; 47, 72; 47, 92; 47, 105; 48, expression_statement; 48, 49; 49, comment; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:path; 53, call; 53, 54; 53, 59; 54, attribute; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:os; 57, identifier:path; 58, identifier:normpath; 59, argument_list; 59, 60; 60, binary_operator:+; 60, 61; 60, 66; 61, attribute; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:os; 64, identifier:path; 65, identifier:sep; 66, attribute; 66, 67; 66, 71; 67, call; 67, 68; 67, 69; 68, identifier:urlsplit; 69, argument_list; 69, 70; 70, identifier:schema; 71, identifier:path; 72, if_statement; 72, 73; 72, 83; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:path; 76, identifier:startswith; 77, argument_list; 77, 78; 78, attribute; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:os; 81, identifier:path; 82, identifier:sep; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:path; 87, subscript; 87, 88; 87, 89; 88, identifier:path; 89, slice; 89, 90; 89, 91; 90, integer:1; 91, colon; 92, if_statement; 92, 93; 92, 100; 93, not_operator; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:path; 97, identifier:endswith; 98, argument_list; 98, 99; 99, string:'.json'; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, augmented_assignment:+=; 102, 103; 102, 104; 103, identifier:path; 104, string:'.json'; 105, return_statement; 105, 106; 106, identifier:path; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:path; 110, call; 110, 111; 110, 112; 111, identifier:_schema_to_normalized_path; 112, argument_list; 112, 113; 113, identifier:schema; 114, while_statement; 114, 115; 114, 116; 115, identifier:path; 116, block; 116, 117; 116, 160; 116, 179; 117, if_statement; 117, 118; 117, 119; 117, 139; 118, identifier:resolved; 119, block; 119, 120; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:schema_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:abspath; 129, argument_list; 129, 130; 130, call; 130, 131; 130, 136; 131, attribute; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:os; 134, identifier:path; 135, identifier:join; 136, argument_list; 136, 137; 136, 138; 137, identifier:_resolved_schema_root_path; 138, identifier:path; 139, else_clause; 139, 140; 140, block; 140, 141; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:schema_path; 144, call; 144, 145; 144, 150; 145, attribute; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:os; 148, identifier:path; 149, identifier:abspath; 150, argument_list; 150, 151; 151, call; 151, 152; 151, 157; 152, attribute; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:os; 155, identifier:path; 156, identifier:join; 157, argument_list; 157, 158; 157, 159; 158, identifier:_schema_root_path; 159, identifier:path; 160, if_statement; 160, 161; 160, 169; 161, call; 161, 162; 161, 167; 162, attribute; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:os; 165, identifier:path; 166, identifier:exists; 167, argument_list; 167, 168; 168, identifier:schema_path; 169, block; 169, 170; 170, return_statement; 170, 171; 171, call; 171, 172; 171, 177; 172, attribute; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:os; 175, identifier:path; 176, identifier:abspath; 177, argument_list; 177, 178; 178, identifier:schema_path; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:path; 182, call; 182, 183; 182, 184; 183, identifier:_strip_first_path_elem; 184, argument_list; 184, 185; 185, identifier:path; 186, raise_statement; 186, 187; 187, call; 187, 188; 187, 189; 188, identifier:SchemaNotFound; 189, argument_list; 189, 190; 190, keyword_argument; 190, 191; 190, 192; 191, identifier:schema; 192, identifier:schema | def get_schema_path(schema, resolved=False):
"""Retrieve the installed path for the given schema.
Args:
schema(str): relative or absolute url of the schema to validate, for
example, 'records/authors.json' or 'jobs.json', or just the name of the
schema, like 'jobs'.
resolved(bool): if True, the returned path points to a fully resolved
schema, that is to the schema with all `$ref` replaced by their
targets.
Returns:
str: path to the given schema name.
Raises:
SchemaNotFound: if no schema could be found.
"""
def _strip_first_path_elem(path):
"""Pass doctests.
Strip the first element of the given path, returning an empty string if
there are no more elements. For example, 'something/other' will end up
as 'other', but passing then 'other' will return ''
"""
stripped_path = path.split(os.path.sep, 1)[1:]
return ''.join(stripped_path)
def _schema_to_normalized_path(schema):
"""Pass doctests.
Extracts the path from the url, makes sure to get rid of any '..' in
the path and adds the json extension if not there.
"""
path = os.path.normpath(os.path.sep + urlsplit(schema).path)
if path.startswith(os.path.sep):
path = path[1:]
if not path.endswith('.json'):
path += '.json'
return path
path = _schema_to_normalized_path(schema)
while path:
if resolved:
schema_path = os.path.abspath(os.path.join(_resolved_schema_root_path, path))
else:
schema_path = os.path.abspath(os.path.join(_schema_root_path, path))
if os.path.exists(schema_path):
return os.path.abspath(schema_path)
path = _strip_first_path_elem(path)
raise SchemaNotFound(schema=schema) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_license_from_url; 3, parameters; 3, 4; 4, identifier:url; 5, block; 5, 6; 5, 8; 5, 13; 5, 23; 5, 169; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 11; 9, not_operator; 9, 10; 10, identifier:url; 11, block; 11, 12; 12, return_statement; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:split_url; 16, call; 16, 17; 16, 18; 17, identifier:urlsplit; 18, argument_list; 18, 19; 18, 20; 19, identifier:url; 20, keyword_argument; 20, 21; 20, 22; 21, identifier:scheme; 22, string:'http'; 23, if_statement; 23, 24; 23, 33; 23, 123; 23, 162; 24, comparison_operator:==; 24, 25; 24, 32; 25, call; 25, 26; 25, 31; 26, attribute; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:split_url; 29, identifier:netloc; 30, identifier:lower; 31, argument_list; 32, string:'creativecommons.org'; 33, block; 33, 34; 34, if_statement; 34, 35; 34, 40; 34, 85; 35, comparison_operator:in; 35, 36; 35, 37; 36, string:'publicdomain'; 37, attribute; 37, 38; 37, 39; 38, identifier:split_url; 39, identifier:path; 40, block; 40, 41; 40, 52; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:match; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:_RE_PUBLIC_DOMAIN_URL; 47, identifier:match; 48, argument_list; 48, 49; 49, attribute; 49, 50; 49, 51; 50, identifier:split_url; 51, identifier:path; 52, if_statement; 52, 53; 52, 56; 52, 62; 53, comparison_operator:is; 53, 54; 53, 55; 54, identifier:match; 55, None; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:license; 60, list:['public domain']; 60, 61; 61, string:'public domain'; 62, else_clause; 62, 63; 63, block; 63, 64; 63, 69; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:license; 67, list:['CC0']; 67, 68; 68, string:'CC0'; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:license; 73, identifier:extend; 74, generator_expression; 74, 75; 74, 76; 74, 83; 75, identifier:part; 76, for_in_clause; 76, 77; 76, 78; 77, identifier:part; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:match; 81, identifier:groups; 82, argument_list; 83, if_clause; 83, 84; 84, identifier:part; 85, else_clause; 85, 86; 86, block; 86, 87; 86, 92; 86, 103; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:license; 90, list:['CC']; 90, 91; 91, string:'CC'; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:match; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:_RE_LICENSE_URL; 98, identifier:match; 99, argument_list; 99, 100; 100, attribute; 100, 101; 100, 102; 101, identifier:split_url; 102, identifier:path; 103, expression_statement; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:license; 107, identifier:extend; 108, generator_expression; 108, 109; 108, 114; 108, 121; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:part; 112, identifier:upper; 113, argument_list; 114, for_in_clause; 114, 115; 114, 116; 115, identifier:part; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:match; 119, identifier:groups; 120, argument_list; 121, if_clause; 121, 122; 122, identifier:part; 123, elif_clause; 123, 124; 123, 129; 124, comparison_operator:==; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:split_url; 127, identifier:netloc; 128, string:'arxiv.org'; 129, block; 129, 130; 129, 135; 129, 146; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:license; 133, list:['arXiv']; 133, 134; 134, string:'arXiv'; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:match; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:_RE_LICENSE_URL; 141, identifier:match; 142, argument_list; 142, 143; 143, attribute; 143, 144; 143, 145; 144, identifier:split_url; 145, identifier:path; 146, expression_statement; 146, 147; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:license; 150, identifier:extend; 151, generator_expression; 151, 152; 151, 153; 151, 160; 152, identifier:part; 153, for_in_clause; 153, 154; 153, 155; 154, identifier:part; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:match; 158, identifier:groups; 159, argument_list; 160, if_clause; 160, 161; 161, identifier:part; 162, else_clause; 162, 163; 163, block; 163, 164; 164, raise_statement; 164, 165; 165, call; 165, 166; 165, 167; 166, identifier:ValueError; 167, argument_list; 167, 168; 168, string:'Unknown license URL'; 169, return_statement; 169, 170; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, string:u' '; 173, identifier:join; 174, argument_list; 174, 175; 175, identifier:license | def get_license_from_url(url):
"""Get the license abbreviation from an URL.
Args:
url(str): canonical url of the license.
Returns:
str: the corresponding license abbreviation.
Raises:
ValueError: when the url is not recognized
"""
if not url:
return
split_url = urlsplit(url, scheme='http')
if split_url.netloc.lower() == 'creativecommons.org':
if 'publicdomain' in split_url.path:
match = _RE_PUBLIC_DOMAIN_URL.match(split_url.path)
if match is None:
license = ['public domain']
else:
license = ['CC0']
license.extend(part for part in match.groups() if part)
else:
license = ['CC']
match = _RE_LICENSE_URL.match(split_url.path)
license.extend(part.upper() for part in match.groups() if part)
elif split_url.netloc == 'arxiv.org':
license = ['arXiv']
match = _RE_LICENSE_URL.match(split_url.path)
license.extend(part for part in match.groups() if part)
else:
raise ValueError('Unknown license URL')
return u' '.join(license) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:convert_old_publication_info_to_new; 3, parameters; 3, 4; 4, identifier:publication_infos; 5, block; 5, 6; 5, 8; 5, 12; 5, 16; 5, 282; 5, 304; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:result; 11, list:[]; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:hidden_publication_infos; 15, list:[]; 16, for_statement; 16, 17; 16, 18; 16, 19; 17, identifier:publication_info; 18, identifier:publication_infos; 19, block; 19, 20; 19, 29; 19, 38; 19, 64; 19, 73; 19, 150; 19, 253; 19, 263; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:_publication_info; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:copy; 26, identifier:deepcopy; 27, argument_list; 27, 28; 28, identifier:publication_info; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:journal_title; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:_publication_info; 35, identifier:get; 36, argument_list; 36, 37; 37, string:'journal_title'; 38, try_statement; 38, 39; 38, 60; 39, block; 39, 40; 39, 46; 39, 52; 39, 59; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:journal_title; 43, subscript; 43, 44; 43, 45; 44, identifier:_JOURNALS_RENAMED_OLD_TO_NEW; 45, identifier:journal_title; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 51; 48, subscript; 48, 49; 48, 50; 49, identifier:_publication_info; 50, string:'journal_title'; 51, identifier:journal_title; 52, expression_statement; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:result; 56, identifier:append; 57, argument_list; 57, 58; 58, identifier:_publication_info; 59, continue_statement; 60, except_clause; 60, 61; 60, 62; 61, identifier:KeyError; 62, block; 62, 63; 63, pass_statement; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:journal_volume; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:_publication_info; 70, identifier:get; 71, argument_list; 71, 72; 72, string:'journal_volume'; 73, if_statement; 73, 74; 73, 86; 74, boolean_operator:and; 74, 75; 74, 80; 75, boolean_operator:and; 75, 76; 75, 79; 76, comparison_operator:in; 76, 77; 76, 78; 77, identifier:journal_title; 78, identifier:_JOURNALS_WITH_YEAR_ADDED_TO_VOLUME; 79, identifier:journal_volume; 80, comparison_operator:==; 80, 81; 80, 85; 81, call; 81, 82; 81, 83; 82, identifier:len; 83, argument_list; 83, 84; 84, identifier:journal_volume; 85, integer:4; 86, block; 86, 87; 86, 142; 86, 149; 87, try_statement; 87, 88; 87, 102; 87, 106; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:was_last_century; 92, comparison_operator:>; 92, 93; 92, 101; 93, call; 93, 94; 93, 95; 94, identifier:int; 95, argument_list; 95, 96; 96, subscript; 96, 97; 96, 98; 97, identifier:journal_volume; 98, slice; 98, 99; 98, 100; 99, colon; 100, integer:2; 101, integer:50; 102, except_clause; 102, 103; 102, 104; 103, identifier:ValueError; 104, block; 104, 105; 105, pass_statement; 106, else_clause; 106, 107; 107, block; 107, 108; 107, 132; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 113; 110, subscript; 110, 111; 110, 112; 111, identifier:_publication_info; 112, string:'year'; 113, call; 113, 114; 113, 115; 114, identifier:int; 115, argument_list; 115, 116; 116, conditional_expression:if; 116, 117; 116, 124; 116, 125; 117, binary_operator:+; 117, 118; 117, 119; 118, string:'19'; 119, subscript; 119, 120; 119, 121; 120, identifier:journal_volume; 121, slice; 121, 122; 121, 123; 122, colon; 123, integer:2; 124, identifier:was_last_century; 125, binary_operator:+; 125, 126; 125, 127; 126, string:'20'; 127, subscript; 127, 128; 127, 129; 128, identifier:journal_volume; 129, slice; 129, 130; 129, 131; 130, colon; 131, integer:2; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 137; 134, subscript; 134, 135; 134, 136; 135, identifier:_publication_info; 136, string:'journal_volume'; 137, subscript; 137, 138; 137, 139; 138, identifier:journal_volume; 139, slice; 139, 140; 139, 141; 140, integer:2; 141, colon; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:result; 146, identifier:append; 147, argument_list; 147, 148; 148, identifier:_publication_info; 149, continue_statement; 150, if_statement; 150, 151; 150, 162; 151, boolean_operator:and; 151, 152; 151, 155; 152, boolean_operator:and; 152, 153; 152, 154; 153, identifier:journal_title; 154, identifier:journal_volume; 155, comparison_operator:not; 155, 156; 155, 161; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:journal_title; 159, identifier:lower; 160, argument_list; 161, identifier:JOURNALS_IGNORED_IN_OLD_TO_NEW; 162, block; 162, 163; 162, 172; 162, 181; 162, 187; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:volume_starts_with_a_letter; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:_RE_VOLUME_STARTS_WITH_A_LETTER; 169, identifier:match; 170, argument_list; 170, 171; 171, identifier:journal_volume; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:volume_ends_with_a_letter; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:_RE_VOLUME_ENDS_WITH_A_LETTER; 178, identifier:match; 179, argument_list; 179, 180; 180, identifier:journal_volume; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:match; 184, boolean_operator:or; 184, 185; 184, 186; 185, identifier:volume_starts_with_a_letter; 186, identifier:volume_ends_with_a_letter; 187, if_statement; 187, 188; 187, 189; 188, identifier:match; 189, block; 189, 190; 189, 198; 189, 242; 190, expression_statement; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:_publication_info; 194, identifier:pop; 195, argument_list; 195, 196; 195, 197; 196, string:'journal_record'; 197, None; 198, if_statement; 198, 199; 198, 206; 198, 213; 199, comparison_operator:in; 199, 200; 199, 201; 200, identifier:journal_title; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:_JOURNALS_RENAMED_OLD_TO_NEW; 204, identifier:values; 205, argument_list; 206, block; 206, 207; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 212; 209, subscript; 209, 210; 209, 211; 210, identifier:_publication_info; 211, string:'journal_title'; 212, identifier:journal_title; 213, else_clause; 213, 214; 214, block; 214, 215; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 220; 217, subscript; 217, 218; 217, 219; 218, identifier:_publication_info; 219, string:'journal_title'; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, string:''; 223, identifier:join; 224, argument_list; 224, 225; 225, list:[
journal_title,
'' if journal_title.endswith('.') else ' ',
match.group('letter'),
]; 225, 226; 225, 227; 225, 236; 226, identifier:journal_title; 227, conditional_expression:if; 227, 228; 227, 229; 227, 235; 228, string:''; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:journal_title; 232, identifier:endswith; 233, argument_list; 233, 234; 234, string:'.'; 235, string:' '; 236, call; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:match; 239, identifier:group; 240, argument_list; 240, 241; 241, string:'letter'; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 247; 244, subscript; 244, 245; 244, 246; 245, identifier:_publication_info; 246, string:'journal_volume'; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:match; 250, identifier:group; 251, argument_list; 251, 252; 252, string:'volume'; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 256; 255, identifier:hidden; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:_publication_info; 259, identifier:pop; 260, argument_list; 260, 261; 260, 262; 261, string:'hidden'; 262, None; 263, if_statement; 263, 264; 263, 265; 263, 273; 264, identifier:hidden; 265, block; 265, 266; 266, expression_statement; 266, 267; 267, call; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:hidden_publication_infos; 270, identifier:append; 271, argument_list; 271, 272; 272, identifier:_publication_info; 273, else_clause; 273, 274; 274, block; 274, 275; 275, expression_statement; 275, 276; 276, call; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:result; 279, identifier:append; 280, argument_list; 280, 281; 281, identifier:_publication_info; 282, for_statement; 282, 283; 282, 284; 282, 285; 283, identifier:publication_info; 284, identifier:hidden_publication_infos; 285, block; 285, 286; 286, if_statement; 286, 287; 286, 290; 287, comparison_operator:not; 287, 288; 287, 289; 288, identifier:publication_info; 289, identifier:result; 290, block; 290, 291; 290, 297; 291, expression_statement; 291, 292; 292, assignment; 292, 293; 292, 296; 293, subscript; 293, 294; 293, 295; 294, identifier:publication_info; 295, string:'hidden'; 296, True; 297, expression_statement; 297, 298; 298, call; 298, 299; 298, 302; 299, attribute; 299, 300; 299, 301; 300, identifier:result; 301, identifier:append; 302, argument_list; 302, 303; 303, identifier:publication_info; 304, return_statement; 304, 305; 305, identifier:result | def convert_old_publication_info_to_new(publication_infos):
"""Convert a ``publication_info`` value from the old format to the new.
On Legacy different series of the same journal were modeled by adding the
letter part of the name to the journal volume. For example, a paper published
in Physical Review D contained::
{
'publication_info': [
{
'journal_title': 'Phys.Rev.',
'journal_volume': 'D43',
},
],
}
On Labs we instead represent each series with a different journal record. As
a consequence, the above example becomes::
{
'publication_info': [
{
'journal_title': 'Phys.Rev.D',
'journal_volume': '43',
},
],
}
This function handles this translation from the old format to the new. Please
also see the tests for various edge cases that this function also handles.
Args:
publication_infos: a ``publication_info`` in the old format.
Returns:
list(dict): a ``publication_info`` in the new format.
"""
result = []
hidden_publication_infos = []
for publication_info in publication_infos:
_publication_info = copy.deepcopy(publication_info)
journal_title = _publication_info.get('journal_title')
try:
journal_title = _JOURNALS_RENAMED_OLD_TO_NEW[journal_title]
_publication_info['journal_title'] = journal_title
result.append(_publication_info)
continue
except KeyError:
pass
journal_volume = _publication_info.get('journal_volume')
if journal_title in _JOURNALS_WITH_YEAR_ADDED_TO_VOLUME and journal_volume and len(journal_volume) == 4:
try:
was_last_century = int(journal_volume[:2]) > 50
except ValueError:
pass
else:
_publication_info['year'] = int('19' + journal_volume[:2] if was_last_century else '20' + journal_volume[:2])
_publication_info['journal_volume'] = journal_volume[2:]
result.append(_publication_info)
continue
if journal_title and journal_volume and journal_title.lower() not in JOURNALS_IGNORED_IN_OLD_TO_NEW:
volume_starts_with_a_letter = _RE_VOLUME_STARTS_WITH_A_LETTER.match(journal_volume)
volume_ends_with_a_letter = _RE_VOLUME_ENDS_WITH_A_LETTER.match(journal_volume)
match = volume_starts_with_a_letter or volume_ends_with_a_letter
if match:
_publication_info.pop('journal_record', None)
if journal_title in _JOURNALS_RENAMED_OLD_TO_NEW.values():
_publication_info['journal_title'] = journal_title
else:
_publication_info['journal_title'] = ''.join([
journal_title,
'' if journal_title.endswith('.') else ' ',
match.group('letter'),
])
_publication_info['journal_volume'] = match.group('volume')
hidden = _publication_info.pop('hidden', None)
if hidden:
hidden_publication_infos.append(_publication_info)
else:
result.append(_publication_info)
for publication_info in hidden_publication_infos:
if publication_info not in result:
publication_info['hidden'] = True
result.append(publication_info)
return result |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:construct_reference_system; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:symbols; 5, default_parameter; 5, 6; 5, 7; 6, identifier:candidates; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:options; 10, None; 11, block; 11, 12; 11, 14; 11, 35; 11, 39; 11, 52; 11, 90; 11, 94; 11, 95; 11, 96; 11, 221; 11, 225; 11, 236; 11, 237; 11, 238; 11, 239; 11, 240; 11, 241; 11, 242; 11, 243; 11, 244; 12, expression_statement; 12, 13; 13, comment; 14, if_statement; 14, 15; 14, 24; 14, 29; 15, boolean_operator:and; 15, 16; 15, 21; 16, call; 16, 17; 16, 18; 17, identifier:hasattr; 18, argument_list; 18, 19; 18, 20; 19, identifier:options; 20, string:'no_hydrogen'; 21, attribute; 21, 22; 21, 23; 22, identifier:options; 23, identifier:no_hydrogen; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:add_hydrogen; 28, False; 29, else_clause; 29, 30; 30, block; 30, 31; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:add_hydrogen; 34, True; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:references; 38, dictionary; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:sorted_candidates; 42, list:[
'H2',
'H2O',
'NH3',
'N2',
'CH4',
'CO',
'H2S',
'HCl',
'O2']; 42, 43; 42, 44; 42, 45; 42, 46; 42, 47; 42, 48; 42, 49; 42, 50; 42, 51; 43, string:'H2'; 44, string:'H2O'; 45, string:'NH3'; 46, string:'N2'; 47, string:'CH4'; 48, string:'CO'; 49, string:'H2S'; 50, string:'HCl'; 51, string:'O2'; 52, if_statement; 52, 53; 52, 56; 52, 61; 53, comparison_operator:is; 53, 54; 53, 55; 54, identifier:candidates; 55, None; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:candidates; 60, identifier:sorted_candidates; 61, else_clause; 61, 62; 62, block; 62, 63; 62, 75; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:odd_candidates; 66, list_comprehension; 66, 67; 66, 68; 66, 71; 67, identifier:c; 68, for_in_clause; 68, 69; 68, 70; 69, identifier:c; 70, identifier:candidates; 71, if_clause; 71, 72; 72, comparison_operator:not; 72, 73; 72, 74; 73, identifier:c; 74, identifier:sorted_candidates; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:candidates; 78, binary_operator:+; 78, 79; 78, 88; 78, 89; 79, list_comprehension; 79, 80; 79, 81; 79, 84; 80, identifier:c; 81, for_in_clause; 81, 82; 81, 83; 82, identifier:c; 83, identifier:sorted_candidates; 84, if_clause; 84, 85; 85, comparison_operator:in; 85, 86; 85, 87; 86, identifier:c; 87, identifier:candidates; 88, line_continuation:\; 89, identifier:odd_candidates; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:added_symbols; 93, list:[]; 94, comment; 95, comment; 96, for_statement; 96, 97; 96, 98; 96, 99; 97, identifier:symbol; 98, identifier:symbols; 99, block; 99, 100; 99, 107; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:added_symbols; 104, identifier:append; 105, argument_list; 105, 106; 106, identifier:symbol; 107, for_statement; 107, 108; 107, 109; 107, 110; 107, 182; 108, identifier:candidate; 109, identifier:candidates; 110, block; 110, 111; 110, 122; 110, 123; 110, 124; 110, 125; 110, 126; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:_symbols; 114, call; 114, 115; 114, 120; 115, attribute; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:ase; 118, identifier:symbols; 119, identifier:string2symbols; 120, argument_list; 120, 121; 121, identifier:candidate; 122, comment; 123, comment; 124, comment; 125, comment; 126, if_statement; 126, 127; 126, 174; 127, boolean_operator:and; 127, 128; 127, 166; 127, 167; 128, boolean_operator:and; 128, 129; 128, 147; 128, 148; 129, comparison_operator:<=; 129, 130; 129, 134; 130, call; 130, 131; 130, 132; 131, identifier:set; 132, argument_list; 132, 133; 133, identifier:added_symbols; 134, call; 134, 135; 134, 136; 135, identifier:set; 136, argument_list; 136, 137; 137, binary_operator:+; 137, 138; 137, 146; 138, call; 138, 139; 138, 140; 139, identifier:list; 140, argument_list; 140, 141; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:references; 144, identifier:keys; 145, argument_list; 146, identifier:_symbols; 147, line_continuation:\; 148, comparison_operator:<=; 148, 149; 148, 162; 149, call; 149, 150; 149, 151; 150, identifier:set; 151, argument_list; 151, 152; 152, binary_operator:+; 152, 153; 152, 161; 153, call; 153, 154; 153, 155; 154, identifier:list; 155, argument_list; 155, 156; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:references; 159, identifier:keys; 160, argument_list; 161, identifier:_symbols; 162, call; 162, 163; 162, 164; 163, identifier:set; 164, argument_list; 164, 165; 165, identifier:symbols; 166, line_continuation:\; 167, comparison_operator:not; 167, 168; 167, 169; 168, identifier:candidate; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:references; 172, identifier:values; 173, argument_list; 174, block; 174, 175; 174, 181; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 180; 177, subscript; 177, 178; 177, 179; 178, identifier:references; 179, identifier:symbol; 180, identifier:candidate; 181, break_statement; 182, else_clause; 182, 183; 183, block; 183, 184; 184, raise_statement; 184, 185; 185, call; 185, 186; 185, 187; 186, identifier:UserWarning; 187, argument_list; 187, 188; 188, call; 188, 189; 188, 198; 189, attribute; 189, 190; 189, 197; 190, parenthesized_expression; 190, 191; 191, concatenated_string; 191, 192; 191, 193; 191, 194; 191, 195; 191, 196; 192, string:"No candidate satisfied {symbol}. Add more candidates\n"; 193, string:" Symbols {symbols}\n"; 194, string:" _Symbols {_symbols}\n"; 195, string:" References {references}\n"; 196, string:" Candidates {candidates}\n"; 197, identifier:format; 198, argument_list; 198, 199; 198, 202; 198, 205; 198, 208; 198, 211; 199, keyword_argument; 199, 200; 199, 201; 200, identifier:symbol; 201, identifier:symbol; 202, keyword_argument; 202, 203; 202, 204; 203, identifier:symbols; 204, identifier:symbols; 205, keyword_argument; 205, 206; 205, 207; 206, identifier:_symbols; 207, identifier:_symbols; 208, keyword_argument; 208, 209; 208, 210; 209, identifier:candidates; 210, identifier:candidates; 211, keyword_argument; 211, 212; 211, 213; 212, identifier:references; 213, call; 213, 214; 213, 215; 214, identifier:list; 215, argument_list; 215, 216; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:references; 219, identifier:keys; 220, argument_list; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:sorted_references; 224, list:[]; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:references; 228, call; 228, 229; 228, 230; 229, identifier:list; 230, argument_list; 230, 231; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:references; 234, identifier:items; 235, argument_list; 236, comment; 237, comment; 238, comment; 239, comment; 240, comment; 241, comment; 242, comment; 243, comment; 244, return_statement; 244, 245; 245, identifier:references | def construct_reference_system(
symbols,
candidates=None,
options=None,
):
"""Take a list of symbols and construct gas phase
references system, when possible avoiding O2.
Candidates can be rearranged, where earlier candidates
get higher preference than later candidates
assume symbols sorted by atomic number
"""
if hasattr(options, 'no_hydrogen') and options.no_hydrogen:
add_hydrogen = False
else:
add_hydrogen = True
references = {}
sorted_candidates = [
'H2',
'H2O',
'NH3',
'N2',
'CH4',
'CO',
'H2S',
'HCl',
'O2']
if candidates is None:
candidates = sorted_candidates
else:
odd_candidates = [c for c in candidates if c not in sorted_candidates]
candidates = [c for c in sorted_candidates if c in candidates] \
+ odd_candidates
added_symbols = []
# go symbols in adsorbate
# to add reference species in procedural manner
for symbol in symbols:
added_symbols.append(symbol)
for candidate in candidates:
_symbols = ase.symbols.string2symbols(candidate)
# Add partial adsorbate species
# is subset of reference species
# and reference species
# is subset of full adsorbate species set
if set(added_symbols) <= set(list(references.keys()) + _symbols) \
and set(list(references.keys()) + _symbols) <= set(symbols) \
and candidate not in references.values():
references[symbol] = candidate
break
else:
raise UserWarning((
"No candidate satisfied {symbol}. Add more candidates\n"
" Symbols {symbols}\n"
" _Symbols {_symbols}\n"
" References {references}\n"
" Candidates {candidates}\n"
).format(
symbol=symbol,
symbols=symbols,
_symbols=_symbols,
candidates=candidates,
references=list(references.keys()),
))
sorted_references = []
references = list(references.items())
# put references in order so that each reference
# only adds one one additional species in each step
# while references:
# for i, reference in enumerate(references):
# if len(set(ase.symbols.string2symbols(reference[1])) -
# set(x[0] for x in sorted_references)) == 1:
# sorted_references.append(references.pop(i))
# break
return references |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:gen_triplets_master; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:wv_master; 5, default_parameter; 5, 6; 5, 7; 6, identifier:geometry; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:debugplot; 10, integer:0; 11, block; 11, 12; 11, 14; 11, 20; 11, 21; 11, 27; 11, 69; 11, 70; 11, 71; 11, 72; 11, 73; 11, 74; 11, 87; 11, 95; 11, 96; 11, 97; 11, 104; 11, 143; 11, 144; 11, 145; 11, 154; 11, 198; 11, 199; 11, 200; 11, 209; 11, 210; 11, 216; 11, 226; 11, 383; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:nlines_master; 17, attribute; 17, 18; 17, 19; 18, identifier:wv_master; 19, identifier:size; 20, comment; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:wv_previous; 24, subscript; 24, 25; 24, 26; 25, identifier:wv_master; 26, integer:0; 27, for_statement; 27, 28; 27, 29; 27, 34; 28, identifier:i; 29, call; 29, 30; 29, 31; 30, identifier:range; 31, argument_list; 31, 32; 31, 33; 32, integer:1; 33, identifier:nlines_master; 34, block; 34, 35; 34, 63; 35, if_statement; 35, 36; 35, 41; 36, comparison_operator:>=; 36, 37; 36, 38; 37, identifier:wv_previous; 38, subscript; 38, 39; 38, 40; 39, identifier:wv_master; 40, identifier:i; 41, block; 41, 42; 42, raise_statement; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:ValueError; 45, argument_list; 45, 46; 46, binary_operator:+; 46, 47; 46, 62; 47, binary_operator:+; 47, 48; 47, 56; 48, binary_operator:+; 48, 49; 48, 55; 49, binary_operator:+; 49, 50; 49, 51; 50, string:'Wavelengths:\n--> '; 51, call; 51, 52; 51, 53; 52, identifier:str; 53, argument_list; 53, 54; 54, identifier:wv_previous; 55, string:'\n--> '; 56, call; 56, 57; 56, 58; 57, identifier:str; 58, argument_list; 58, 59; 59, subscript; 59, 60; 59, 61; 60, identifier:wv_master; 61, identifier:i; 62, string:'\nin master table are duplicated or not sorted'; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:wv_previous; 66, subscript; 66, 67; 66, 68; 67, identifier:wv_master; 68, identifier:i; 69, comment; 70, comment; 71, comment; 72, comment; 73, comment; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:iter_comb_triplets; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:itertools; 80, identifier:combinations; 81, argument_list; 81, 82; 81, 86; 82, call; 82, 83; 82, 84; 83, identifier:range; 84, argument_list; 84, 85; 85, identifier:nlines_master; 86, integer:3; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:triplets_master_list; 90, list_comprehension; 90, 91; 90, 92; 91, identifier:val; 92, for_in_clause; 92, 93; 92, 94; 93, identifier:val; 94, identifier:iter_comb_triplets; 95, comment; 96, comment; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:ntriplets_master; 100, call; 100, 101; 100, 102; 101, identifier:len; 102, argument_list; 102, 103; 103, identifier:triplets_master_list; 104, if_statement; 104, 105; 104, 115; 104, 136; 105, comparison_operator:==; 105, 106; 105, 107; 106, identifier:ntriplets_master; 107, call; 107, 108; 107, 109; 108, identifier:comb; 109, argument_list; 109, 110; 109, 111; 109, 112; 110, identifier:nlines_master; 111, integer:3; 112, keyword_argument; 112, 113; 112, 114; 113, identifier:exact; 114, True; 115, block; 115, 116; 116, if_statement; 116, 117; 116, 123; 117, comparison_operator:>=; 117, 118; 117, 122; 118, call; 118, 119; 118, 120; 119, identifier:abs; 120, argument_list; 120, 121; 121, identifier:debugplot; 122, integer:10; 123, block; 123, 124; 123, 130; 124, expression_statement; 124, 125; 125, call; 125, 126; 125, 127; 126, identifier:print; 127, argument_list; 127, 128; 127, 129; 128, string:'>>> Total number of lines in master table:'; 129, identifier:nlines_master; 130, expression_statement; 130, 131; 131, call; 131, 132; 131, 133; 132, identifier:print; 133, argument_list; 133, 134; 133, 135; 134, string:'>>> Number of triplets in master table...:'; 135, identifier:ntriplets_master; 136, else_clause; 136, 137; 137, block; 137, 138; 138, raise_statement; 138, 139; 139, call; 139, 140; 139, 141; 140, identifier:ValueError; 141, argument_list; 141, 142; 142, string:'Invalid number of combinations'; 143, comment; 144, comment; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:ratios_master; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:np; 151, identifier:zeros; 152, argument_list; 152, 153; 153, identifier:ntriplets_master; 154, for_statement; 154, 155; 154, 158; 154, 162; 155, pattern_list; 155, 156; 155, 157; 156, identifier:index; 157, identifier:value; 158, call; 158, 159; 158, 160; 159, identifier:enumerate; 160, argument_list; 160, 161; 161, identifier:triplets_master_list; 162, block; 162, 163; 162, 170; 162, 180; 162, 190; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 169; 165, pattern_list; 165, 166; 165, 167; 165, 168; 166, identifier:i1; 167, identifier:i2; 168, identifier:i3; 169, identifier:value; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:delta1; 173, binary_operator:-; 173, 174; 173, 177; 174, subscript; 174, 175; 174, 176; 175, identifier:wv_master; 176, identifier:i2; 177, subscript; 177, 178; 177, 179; 178, identifier:wv_master; 179, identifier:i1; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:delta2; 183, binary_operator:-; 183, 184; 183, 187; 184, subscript; 184, 185; 184, 186; 185, identifier:wv_master; 186, identifier:i3; 187, subscript; 187, 188; 187, 189; 188, identifier:wv_master; 189, identifier:i1; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 195; 192, subscript; 192, 193; 192, 194; 193, identifier:ratios_master; 194, identifier:index; 195, binary_operator:/; 195, 196; 195, 197; 196, identifier:delta1; 197, identifier:delta2; 198, comment; 199, comment; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 203; 202, identifier:isort_ratios_master; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:np; 206, identifier:argsort; 207, argument_list; 207, 208; 208, identifier:ratios_master; 209, comment; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 213; 212, identifier:ratios_master_sorted; 213, subscript; 213, 214; 213, 215; 214, identifier:ratios_master; 215, identifier:isort_ratios_master; 216, expression_statement; 216, 217; 217, assignment; 217, 218; 217, 219; 218, identifier:triplets_master_sorted_list; 219, list_comprehension; 219, 220; 219, 223; 220, subscript; 220, 221; 220, 222; 221, identifier:triplets_master_list; 222, identifier:i; 223, for_in_clause; 223, 224; 223, 225; 224, identifier:i; 225, identifier:isort_ratios_master; 226, if_statement; 226, 227; 226, 235; 226, 236; 227, comparison_operator:in; 227, 228; 227, 232; 228, call; 228, 229; 228, 230; 229, identifier:abs; 230, argument_list; 230, 231; 231, identifier:debugplot; 232, list:[21, 22]; 232, 233; 232, 234; 233, integer:21; 234, integer:22; 235, comment; 236, block; 236, 237; 236, 248; 236, 262; 236, 263; 236, 271; 236, 279; 236, 288; 236, 301; 236, 319; 236, 333; 236, 340; 236, 347; 236, 366; 236, 367; 236, 372; 237, expression_statement; 237, 238; 238, assignment; 238, 239; 238, 240; 239, identifier:bins_in; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:np; 243, identifier:linspace; 244, argument_list; 244, 245; 244, 246; 244, 247; 245, float:0.0; 246, float:1.0; 247, integer:41; 248, expression_statement; 248, 249; 249, assignment; 249, 250; 249, 253; 250, pattern_list; 250, 251; 250, 252; 251, identifier:hist; 252, identifier:bins_out; 253, call; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:np; 256, identifier:histogram; 257, argument_list; 257, 258; 257, 259; 258, identifier:ratios_master; 259, keyword_argument; 259, 260; 259, 261; 260, identifier:bins; 261, identifier:bins_in; 262, comment; 263, import_from_statement; 263, 264; 263, 269; 264, dotted_name; 264, 265; 264, 266; 264, 267; 264, 268; 265, identifier:numina; 266, identifier:array; 267, identifier:display; 268, identifier:matplotlib_qt; 269, dotted_name; 269, 270; 270, identifier:plt; 271, expression_statement; 271, 272; 272, assignment; 272, 273; 272, 274; 273, identifier:fig; 274, call; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:plt; 277, identifier:figure; 278, argument_list; 279, expression_statement; 279, 280; 280, assignment; 280, 281; 280, 282; 281, identifier:ax; 282, call; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, identifier:fig; 285, identifier:add_subplot; 286, argument_list; 286, 287; 287, integer:111; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 291; 290, identifier:width_hist; 291, binary_operator:*; 291, 292; 291, 293; 292, float:0.8; 293, parenthesized_expression; 293, 294; 294, binary_operator:-; 294, 295; 294, 298; 295, subscript; 295, 296; 295, 297; 296, identifier:bins_out; 297, integer:1; 298, subscript; 298, 299; 298, 300; 299, identifier:bins_out; 300, integer:0; 301, expression_statement; 301, 302; 302, assignment; 302, 303; 302, 304; 303, identifier:center; 304, binary_operator:/; 304, 305; 304, 318; 305, parenthesized_expression; 305, 306; 306, binary_operator:+; 306, 307; 306, 313; 307, subscript; 307, 308; 307, 309; 308, identifier:bins_out; 309, slice; 309, 310; 309, 311; 310, colon; 311, unary_operator:-; 311, 312; 312, integer:1; 313, subscript; 313, 314; 313, 315; 314, identifier:bins_out; 315, slice; 315, 316; 315, 317; 316, integer:1; 317, colon; 318, integer:2; 319, expression_statement; 319, 320; 320, call; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:ax; 323, identifier:bar; 324, argument_list; 324, 325; 324, 326; 324, 327; 324, 330; 325, identifier:center; 326, identifier:hist; 327, keyword_argument; 327, 328; 327, 329; 328, identifier:align; 329, string:'center'; 330, keyword_argument; 330, 331; 330, 332; 331, identifier:width; 332, identifier:width_hist; 333, expression_statement; 333, 334; 334, call; 334, 335; 334, 338; 335, attribute; 335, 336; 335, 337; 336, identifier:ax; 337, identifier:set_xlabel; 338, argument_list; 338, 339; 339, string:'distance ratio in each triplet'; 340, expression_statement; 340, 341; 341, call; 341, 342; 341, 345; 342, attribute; 342, 343; 342, 344; 343, identifier:ax; 344, identifier:set_ylabel; 345, argument_list; 345, 346; 346, string:'Number of triplets'; 347, expression_statement; 347, 348; 348, call; 348, 349; 348, 352; 349, attribute; 349, 350; 349, 351; 350, identifier:ax; 351, identifier:set_title; 352, argument_list; 352, 353; 353, binary_operator:+; 353, 354; 353, 362; 354, binary_operator:+; 354, 355; 354, 361; 355, binary_operator:+; 355, 356; 355, 357; 356, string:"Number of lines/triplets: "; 357, call; 357, 358; 357, 359; 358, identifier:str; 359, argument_list; 359, 360; 360, identifier:nlines_master; 361, string:"/"; 362, call; 362, 363; 362, 364; 363, identifier:str; 364, argument_list; 364, 365; 365, identifier:ntriplets_master; 366, comment; 367, expression_statement; 367, 368; 368, call; 368, 369; 368, 370; 369, identifier:set_window_geometry; 370, argument_list; 370, 371; 371, identifier:geometry; 372, expression_statement; 372, 373; 373, call; 373, 374; 373, 375; 374, identifier:pause_debugplot; 375, argument_list; 375, 376; 375, 377; 375, 380; 376, identifier:debugplot; 377, keyword_argument; 377, 378; 377, 379; 378, identifier:pltshow; 379, True; 380, keyword_argument; 380, 381; 380, 382; 381, identifier:tight_layout; 382, True; 383, return_statement; 383, 384; 384, expression_list; 384, 385; 384, 386; 384, 387; 385, identifier:ntriplets_master; 386, identifier:ratios_master_sorted; 387, identifier:triplets_master_sorted_list | def gen_triplets_master(wv_master, geometry=None, debugplot=0):
"""Compute information associated to triplets in master table.
Determine all the possible triplets that can be generated from the
array `wv_master`. In addition, the relative position of the
central line of each triplet is also computed.
Parameters
----------
wv_master : 1d numpy array, float
Array with wavelengths corresponding to the master table
(Angstroms).
geometry : tuple (4 integers) or None
x, y, dx, dy values employed to set the window geometry.
debugplot : int
Determines whether intermediate computations and/or plots
are displayed. The valid codes are defined in
numina.array.display.pause_debugplot.
Returns
-------
ntriplets_master : int
Number of triplets built from master table.
ratios_master_sorted : 1d numpy array, float
Array with values of the relative position of the central line
of each triplet, sorted in ascending order.
triplets_master_sorted_list : list of tuples
List with tuples of three numbers, corresponding to the three
line indices in the master table. The list is sorted to be in
correspondence with `ratios_master_sorted`.
"""
nlines_master = wv_master.size
# Check that the wavelengths in the master table are sorted
wv_previous = wv_master[0]
for i in range(1, nlines_master):
if wv_previous >= wv_master[i]:
raise ValueError('Wavelengths:\n--> ' +
str(wv_previous) + '\n--> ' + str(wv_master[i]) +
'\nin master table are duplicated or not sorted')
wv_previous = wv_master[i]
# Generate all the possible triplets with the numbers of the lines
# in the master table. Each triplet is defined as a tuple of three
# numbers corresponding to the three line indices in the master
# table. The collection of tuples is stored in an ordinary python
# list.
iter_comb_triplets = itertools.combinations(range(nlines_master), 3)
triplets_master_list = [val for val in iter_comb_triplets]
# Verify that the number of triplets coincides with the expected
# value.
ntriplets_master = len(triplets_master_list)
if ntriplets_master == comb(nlines_master, 3, exact=True):
if abs(debugplot) >= 10:
print('>>> Total number of lines in master table:',
nlines_master)
print('>>> Number of triplets in master table...:',
ntriplets_master)
else:
raise ValueError('Invalid number of combinations')
# For each triplet, compute the relative position of the central
# line.
ratios_master = np.zeros(ntriplets_master)
for index, value in enumerate(triplets_master_list):
i1, i2, i3 = value
delta1 = wv_master[i2] - wv_master[i1]
delta2 = wv_master[i3] - wv_master[i1]
ratios_master[index] = delta1 / delta2
# Compute the array of indices that index the above ratios in
# sorted order.
isort_ratios_master = np.argsort(ratios_master)
# Simultaneous sort of position ratios and triplets.
ratios_master_sorted = ratios_master[isort_ratios_master]
triplets_master_sorted_list = [triplets_master_list[i]
for i in isort_ratios_master]
if abs(debugplot) in [21, 22]:
# compute and plot histogram with position ratios
bins_in = np.linspace(0.0, 1.0, 41)
hist, bins_out = np.histogram(ratios_master, bins=bins_in)
#
from numina.array.display.matplotlib_qt import plt
fig = plt.figure()
ax = fig.add_subplot(111)
width_hist = 0.8*(bins_out[1]-bins_out[0])
center = (bins_out[:-1]+bins_out[1:])/2
ax.bar(center, hist, align='center', width=width_hist)
ax.set_xlabel('distance ratio in each triplet')
ax.set_ylabel('Number of triplets')
ax.set_title("Number of lines/triplets: " +
str(nlines_master) + "/" + str(ntriplets_master))
# set window geometry
set_window_geometry(geometry)
pause_debugplot(debugplot, pltshow=True, tight_layout=True)
return ntriplets_master, ratios_master_sorted, triplets_master_sorted_list |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:match_wv_arrays; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:wv_master; 5, identifier:wv_expected_all_peaks; 6, identifier:delta_wv_max; 7, block; 7, 8; 7, 10; 7, 11; 7, 20; 7, 21; 7, 22; 7, 34; 7, 35; 7, 36; 7, 48; 7, 54; 7, 55; 7, 56; 7, 352; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:wv_verified_all_peaks; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:np; 17, identifier:zeros_like; 18, argument_list; 18, 19; 19, identifier:wv_expected_all_peaks; 20, comment; 21, comment; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:wv_unused; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:np; 28, identifier:ones_like; 29, argument_list; 29, 30; 29, 31; 30, identifier:wv_expected_all_peaks; 31, keyword_argument; 31, 32; 31, 33; 32, identifier:dtype; 33, identifier:bool; 34, comment; 35, comment; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:minimum_delta_wv; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:np; 42, identifier:ones_like; 43, argument_list; 43, 44; 43, 45; 44, identifier:wv_expected_all_peaks; 45, keyword_argument; 45, 46; 45, 47; 46, identifier:dtype; 47, identifier:float; 48, expression_statement; 48, 49; 49, augmented_assignment:*=; 49, 50; 49, 51; 50, identifier:minimum_delta_wv; 51, attribute; 51, 52; 51, 53; 52, identifier:np; 53, identifier:infty; 54, comment; 55, comment; 56, for_statement; 56, 57; 56, 58; 56, 65; 57, identifier:i; 58, call; 58, 59; 58, 60; 59, identifier:range; 60, argument_list; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:len; 63, argument_list; 63, 64; 64, identifier:wv_master; 65, block; 65, 66; 65, 78; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:j; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:np; 72, identifier:searchsorted; 73, argument_list; 73, 74; 73, 75; 74, identifier:wv_expected_all_peaks; 75, subscript; 75, 76; 75, 77; 76, identifier:wv_master; 77, identifier:i; 78, if_statement; 78, 79; 78, 82; 78, 149; 78, 223; 79, comparison_operator:==; 79, 80; 79, 81; 80, identifier:j; 81, integer:0; 82, block; 82, 83; 82, 96; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:delta_wv; 86, call; 86, 87; 86, 88; 87, identifier:abs; 88, argument_list; 88, 89; 89, binary_operator:-; 89, 90; 89, 93; 90, subscript; 90, 91; 90, 92; 91, identifier:wv_master; 92, identifier:i; 93, subscript; 93, 94; 93, 95; 94, identifier:wv_expected_all_peaks; 95, identifier:j; 96, if_statement; 96, 97; 96, 100; 97, comparison_operator:<; 97, 98; 97, 99; 98, identifier:delta_wv; 99, identifier:delta_wv_max; 100, block; 100, 101; 101, if_statement; 101, 102; 101, 105; 101, 126; 102, subscript; 102, 103; 102, 104; 103, identifier:wv_unused; 104, identifier:j; 105, block; 105, 106; 105, 114; 105, 120; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 111; 108, subscript; 108, 109; 108, 110; 109, identifier:wv_verified_all_peaks; 110, identifier:j; 111, subscript; 111, 112; 111, 113; 112, identifier:wv_master; 113, identifier:i; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 119; 116, subscript; 116, 117; 116, 118; 117, identifier:wv_unused; 118, identifier:j; 119, False; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 125; 122, subscript; 122, 123; 122, 124; 123, identifier:minimum_delta_wv; 124, identifier:j; 125, identifier:delta_wv; 126, else_clause; 126, 127; 127, block; 127, 128; 128, if_statement; 128, 129; 128, 134; 129, comparison_operator:<; 129, 130; 129, 131; 130, identifier:delta_wv; 131, subscript; 131, 132; 131, 133; 132, identifier:minimum_delta_wv; 133, identifier:j; 134, block; 134, 135; 134, 143; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 140; 137, subscript; 137, 138; 137, 139; 138, identifier:wv_verified_all_peaks; 139, identifier:j; 140, subscript; 140, 141; 140, 142; 141, identifier:wv_master; 142, identifier:i; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 148; 145, subscript; 145, 146; 145, 147; 146, identifier:minimum_delta_wv; 147, identifier:j; 148, identifier:delta_wv; 149, elif_clause; 149, 150; 149, 156; 150, comparison_operator:==; 150, 151; 150, 152; 151, identifier:j; 152, call; 152, 153; 152, 154; 153, identifier:len; 154, argument_list; 154, 155; 155, identifier:wv_expected_all_peaks; 156, block; 156, 157; 156, 172; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:delta_wv; 160, call; 160, 161; 160, 162; 161, identifier:abs; 162, argument_list; 162, 163; 163, binary_operator:-; 163, 164; 163, 167; 164, subscript; 164, 165; 164, 166; 165, identifier:wv_master; 166, identifier:i; 167, subscript; 167, 168; 167, 169; 168, identifier:wv_expected_all_peaks; 169, binary_operator:-; 169, 170; 169, 171; 170, identifier:j; 171, integer:1; 172, if_statement; 172, 173; 172, 176; 173, comparison_operator:<; 173, 174; 173, 175; 174, identifier:delta_wv; 175, identifier:delta_wv_max; 176, block; 176, 177; 177, if_statement; 177, 178; 177, 183; 177, 202; 178, subscript; 178, 179; 178, 180; 179, identifier:wv_unused; 180, binary_operator:-; 180, 181; 180, 182; 181, identifier:j; 182, integer:1; 183, block; 183, 184; 183, 194; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 191; 186, subscript; 186, 187; 186, 188; 187, identifier:wv_verified_all_peaks; 188, binary_operator:-; 188, 189; 188, 190; 189, identifier:j; 190, integer:1; 191, subscript; 191, 192; 191, 193; 192, identifier:wv_master; 193, identifier:i; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 201; 196, subscript; 196, 197; 196, 198; 197, identifier:wv_unused; 198, binary_operator:-; 198, 199; 198, 200; 199, identifier:j; 200, integer:1; 201, False; 202, else_clause; 202, 203; 203, block; 203, 204; 204, if_statement; 204, 205; 204, 212; 205, comparison_operator:<; 205, 206; 205, 207; 206, identifier:delta_wv; 207, subscript; 207, 208; 207, 209; 208, identifier:minimum_delta_wv; 209, binary_operator:-; 209, 210; 209, 211; 210, identifier:j; 211, integer:1; 212, block; 212, 213; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 220; 215, subscript; 215, 216; 215, 217; 216, identifier:wv_verified_all_peaks; 217, binary_operator:-; 217, 218; 217, 219; 218, identifier:j; 219, integer:1; 220, subscript; 220, 221; 220, 222; 221, identifier:wv_master; 222, identifier:i; 223, else_clause; 223, 224; 224, block; 224, 225; 224, 240; 224, 253; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:delta_wv1; 228, call; 228, 229; 228, 230; 229, identifier:abs; 230, argument_list; 230, 231; 231, binary_operator:-; 231, 232; 231, 235; 232, subscript; 232, 233; 232, 234; 233, identifier:wv_master; 234, identifier:i; 235, subscript; 235, 236; 235, 237; 236, identifier:wv_expected_all_peaks; 237, binary_operator:-; 237, 238; 237, 239; 238, identifier:j; 239, integer:1; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:delta_wv2; 243, call; 243, 244; 243, 245; 244, identifier:abs; 245, argument_list; 245, 246; 246, binary_operator:-; 246, 247; 246, 250; 247, subscript; 247, 248; 247, 249; 248, identifier:wv_master; 249, identifier:i; 250, subscript; 250, 251; 250, 252; 251, identifier:wv_expected_all_peaks; 252, identifier:j; 253, if_statement; 253, 254; 253, 257; 253, 309; 254, comparison_operator:<; 254, 255; 254, 256; 255, identifier:delta_wv1; 256, identifier:delta_wv2; 257, block; 257, 258; 258, if_statement; 258, 259; 258, 262; 259, comparison_operator:<; 259, 260; 259, 261; 260, identifier:delta_wv1; 261, identifier:delta_wv_max; 262, block; 262, 263; 263, if_statement; 263, 264; 263, 269; 263, 288; 264, subscript; 264, 265; 264, 266; 265, identifier:wv_unused; 266, binary_operator:-; 266, 267; 266, 268; 267, identifier:j; 268, integer:1; 269, block; 269, 270; 269, 280; 270, expression_statement; 270, 271; 271, assignment; 271, 272; 271, 277; 272, subscript; 272, 273; 272, 274; 273, identifier:wv_verified_all_peaks; 274, binary_operator:-; 274, 275; 274, 276; 275, identifier:j; 276, integer:1; 277, subscript; 277, 278; 277, 279; 278, identifier:wv_master; 279, identifier:i; 280, expression_statement; 280, 281; 281, assignment; 281, 282; 281, 287; 282, subscript; 282, 283; 282, 284; 283, identifier:wv_unused; 284, binary_operator:-; 284, 285; 284, 286; 285, identifier:j; 286, integer:1; 287, False; 288, else_clause; 288, 289; 289, block; 289, 290; 290, if_statement; 290, 291; 290, 298; 291, comparison_operator:<; 291, 292; 291, 293; 292, identifier:delta_wv1; 293, subscript; 293, 294; 293, 295; 294, identifier:minimum_delta_wv; 295, binary_operator:-; 295, 296; 295, 297; 296, identifier:j; 297, integer:1; 298, block; 298, 299; 299, expression_statement; 299, 300; 300, assignment; 300, 301; 300, 306; 301, subscript; 301, 302; 301, 303; 302, identifier:wv_verified_all_peaks; 303, binary_operator:-; 303, 304; 303, 305; 304, identifier:j; 305, integer:1; 306, subscript; 306, 307; 306, 308; 307, identifier:wv_master; 308, identifier:i; 309, else_clause; 309, 310; 310, block; 310, 311; 311, if_statement; 311, 312; 311, 315; 312, comparison_operator:<; 312, 313; 312, 314; 313, identifier:delta_wv2; 314, identifier:delta_wv_max; 315, block; 315, 316; 316, if_statement; 316, 317; 316, 320; 316, 335; 317, subscript; 317, 318; 317, 319; 318, identifier:wv_unused; 319, identifier:j; 320, block; 320, 321; 320, 329; 321, expression_statement; 321, 322; 322, assignment; 322, 323; 322, 326; 323, subscript; 323, 324; 323, 325; 324, identifier:wv_verified_all_peaks; 325, identifier:j; 326, subscript; 326, 327; 326, 328; 327, identifier:wv_master; 328, identifier:i; 329, expression_statement; 329, 330; 330, assignment; 330, 331; 330, 334; 331, subscript; 331, 332; 331, 333; 332, identifier:wv_unused; 333, identifier:j; 334, False; 335, else_clause; 335, 336; 336, block; 336, 337; 337, if_statement; 337, 338; 337, 343; 338, comparison_operator:<; 338, 339; 338, 340; 339, identifier:delta_wv2; 340, subscript; 340, 341; 340, 342; 341, identifier:minimum_delta_wv; 342, identifier:j; 343, block; 343, 344; 344, expression_statement; 344, 345; 345, assignment; 345, 346; 345, 349; 346, subscript; 346, 347; 346, 348; 347, identifier:wv_verified_all_peaks; 348, identifier:j; 349, subscript; 349, 350; 349, 351; 350, identifier:wv_master; 351, identifier:i; 352, return_statement; 352, 353; 353, identifier:wv_verified_all_peaks | def match_wv_arrays(wv_master, wv_expected_all_peaks, delta_wv_max):
"""Match two lists with wavelengths.
Assign individual wavelengths from wv_master to each expected
wavelength when the latter is within the maximum allowed range.
Parameters
----------
wv_master : numpy array
Array containing the master wavelengths.
wv_expected_all_peaks : numpy array
Array containing the expected wavelengths (computed, for
example, from an approximate polynomial calibration applied to
the location of the line peaks).
delta_wv_max : float
Maximum distance to accept that the master wavelength
corresponds to the expected wavelength.
Returns
-------
wv_verified_all_peaks : numpy array
Verified wavelengths from master list.
"""
# initialize the output array to zero
wv_verified_all_peaks = np.zeros_like(wv_expected_all_peaks)
# initialize to True array to indicate that no peak has already
# been verified (this flag avoids duplication)
wv_unused = np.ones_like(wv_expected_all_peaks, dtype=bool)
# initialize to np.infty array to store minimum distance to already
# identified line
minimum_delta_wv = np.ones_like(wv_expected_all_peaks, dtype=float)
minimum_delta_wv *= np.infty
# since it is likely that len(wv_master) < len(wv_expected_all_peaks),
# it is more convenient to execute the search in the following order
for i in range(len(wv_master)):
j = np.searchsorted(wv_expected_all_peaks, wv_master[i])
if j == 0:
delta_wv = abs(wv_master[i] - wv_expected_all_peaks[j])
if delta_wv < delta_wv_max:
if wv_unused[j]:
wv_verified_all_peaks[j] = wv_master[i]
wv_unused[j] = False
minimum_delta_wv[j] = delta_wv
else:
if delta_wv < minimum_delta_wv[j]:
wv_verified_all_peaks[j] = wv_master[i]
minimum_delta_wv[j] = delta_wv
elif j == len(wv_expected_all_peaks):
delta_wv = abs(wv_master[i] - wv_expected_all_peaks[j-1])
if delta_wv < delta_wv_max:
if wv_unused[j-1]:
wv_verified_all_peaks[j-1] = wv_master[i]
wv_unused[j-1] = False
else:
if delta_wv < minimum_delta_wv[j-1]:
wv_verified_all_peaks[j-1] = wv_master[i]
else:
delta_wv1 = abs(wv_master[i] - wv_expected_all_peaks[j-1])
delta_wv2 = abs(wv_master[i] - wv_expected_all_peaks[j])
if delta_wv1 < delta_wv2:
if delta_wv1 < delta_wv_max:
if wv_unused[j-1]:
wv_verified_all_peaks[j-1] = wv_master[i]
wv_unused[j-1] = False
else:
if delta_wv1 < minimum_delta_wv[j-1]:
wv_verified_all_peaks[j-1] = wv_master[i]
else:
if delta_wv2 < delta_wv_max:
if wv_unused[j]:
wv_verified_all_peaks[j] = wv_master[i]
wv_unused[j] = False
else:
if delta_wv2 < minimum_delta_wv[j]:
wv_verified_all_peaks[j] = wv_master[i]
return wv_verified_all_peaks |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 41; 2, function_name:add_publication_info; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 3, 29; 3, 32; 3, 35; 3, 38; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:year; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:cnum; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:artid; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:page_end; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:page_start; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:journal_issue; 22, None; 23, default_parameter; 23, 24; 23, 25; 24, identifier:journal_title; 25, None; 26, default_parameter; 26, 27; 26, 28; 27, identifier:journal_volume; 28, None; 29, default_parameter; 29, 30; 29, 31; 30, identifier:pubinfo_freetext; 31, None; 32, default_parameter; 32, 33; 32, 34; 33, identifier:material; 34, None; 35, default_parameter; 35, 36; 35, 37; 36, identifier:parent_record; 37, None; 38, default_parameter; 38, 39; 38, 40; 39, identifier:parent_isbn; 40, None; 41, block; 41, 42; 41, 44; 41, 45; 41, 46; 41, 77; 41, 81; 41, 114; 41, 132; 41, 146; 41, 176; 41, 184; 42, expression_statement; 42, 43; 43, comment; 44, comment; 45, comment; 46, if_statement; 46, 47; 46, 63; 47, boolean_operator:and; 47, 48; 47, 49; 48, identifier:journal_title; 49, call; 49, 50; 49, 51; 50, identifier:all; 51, generator_expression; 51, 52; 51, 54; 52, not_operator; 52, 53; 53, identifier:field; 54, for_in_clause; 54, 55; 54, 56; 55, identifier:field; 56, tuple; 56, 57; 56, 58; 56, 59; 56, 60; 56, 61; 56, 62; 57, identifier:cnum; 58, identifier:artid; 59, identifier:journal_issue; 60, identifier:journal_volume; 61, identifier:page_start; 62, identifier:page_end; 63, block; 63, 64; 63, 76; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:add_public_note; 69, argument_list; 69, 70; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, string:'Submitted to {}'; 73, identifier:format; 74, argument_list; 74, 75; 75, identifier:journal_title; 76, return_statement; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:publication_item; 80, dictionary; 81, for_statement; 81, 82; 81, 83; 81, 94; 82, identifier:key; 83, tuple; 83, 84; 83, 85; 83, 86; 83, 87; 83, 88; 83, 89; 83, 90; 83, 91; 83, 92; 83, 93; 84, string:'cnum'; 85, string:'artid'; 86, string:'page_end'; 87, string:'page_start'; 88, string:'journal_issue'; 89, string:'journal_title'; 90, string:'journal_volume'; 91, string:'year'; 92, string:'pubinfo_freetext'; 93, string:'material'; 94, block; 94, 95; 95, if_statement; 95, 96; 95, 103; 96, comparison_operator:is; 96, 97; 96, 102; 97, subscript; 97, 98; 97, 101; 98, call; 98, 99; 98, 100; 99, identifier:locals; 100, argument_list; 101, identifier:key; 102, None; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 109; 106, subscript; 106, 107; 106, 108; 107, identifier:publication_item; 108, identifier:key; 109, subscript; 109, 110; 109, 113; 110, call; 110, 111; 110, 112; 111, identifier:locals; 112, argument_list; 113, identifier:key; 114, if_statement; 114, 115; 114, 118; 115, comparison_operator:is; 115, 116; 115, 117; 116, identifier:parent_record; 117, None; 118, block; 118, 119; 118, 126; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:parent_item; 122, dictionary; 122, 123; 123, pair; 123, 124; 123, 125; 124, string:'$ref'; 125, identifier:parent_record; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 131; 128, subscript; 128, 129; 128, 130; 129, identifier:publication_item; 130, string:'parent_record'; 131, identifier:parent_item; 132, if_statement; 132, 133; 132, 136; 133, comparison_operator:is; 133, 134; 133, 135; 134, identifier:parent_isbn; 135, None; 136, block; 136, 137; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 142; 139, subscript; 139, 140; 139, 141; 140, identifier:publication_item; 141, string:'parent_isbn'; 142, call; 142, 143; 142, 144; 143, identifier:normalize_isbn; 144, argument_list; 144, 145; 145, identifier:parent_isbn; 146, if_statement; 146, 147; 146, 150; 147, boolean_operator:and; 147, 148; 147, 149; 148, identifier:page_start; 149, identifier:page_end; 150, block; 150, 151; 151, try_statement; 151, 152; 151, 170; 152, block; 152, 153; 153, expression_statement; 153, 154; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:self; 157, identifier:add_number_of_pages; 158, argument_list; 158, 159; 159, binary_operator:+; 159, 160; 159, 169; 160, binary_operator:-; 160, 161; 160, 165; 161, call; 161, 162; 161, 163; 162, identifier:int; 163, argument_list; 163, 164; 164, identifier:page_end; 165, call; 165, 166; 165, 167; 166, identifier:int; 167, argument_list; 167, 168; 168, identifier:page_start; 169, integer:1; 170, except_clause; 170, 171; 170, 174; 171, tuple; 171, 172; 171, 173; 172, identifier:TypeError; 173, identifier:ValueError; 174, block; 174, 175; 175, pass_statement; 176, expression_statement; 176, 177; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:self; 180, identifier:_append_to; 181, argument_list; 181, 182; 181, 183; 182, string:'publication_info'; 183, identifier:publication_item; 184, if_statement; 184, 185; 184, 193; 185, call; 185, 186; 185, 187; 186, identifier:is_citeable; 187, argument_list; 187, 188; 188, subscript; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:self; 191, identifier:record; 192, string:'publication_info'; 193, block; 193, 194; 194, expression_statement; 194, 195; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:self; 198, identifier:set_citeable; 199, argument_list; 199, 200; 200, True | def add_publication_info(
self,
year=None,
cnum=None,
artid=None,
page_end=None,
page_start=None,
journal_issue=None,
journal_title=None,
journal_volume=None,
pubinfo_freetext=None,
material=None,
parent_record=None,
parent_isbn=None,
):
"""Add publication info.
:param year: year of publication
:type year: integer
:param cnum: inspire conference number
:type cnum: string
:param artid: article id
:type artid: string
:param page_end: final page for the article
:type page_end: string
:param page_start: initial page for the article
:type page_start: string
:param journal_issue: issue of the journal where
the document has been published
:type journal_issue: string
:param journal_title: title of the journal where
the document has been published
:type journal_title: string
:param journal_volume: volume of the journal where
the document has been published
:type journal_volume: string
:param pubinfo_freetext: Unstructured text describing the publication
information.
:type pubinfo_freetext: string
:param material: material of the article
:type material: string
:param parent_record: reference for the parent record
:type parent_record: string
:param parent_isbn: isbn for the parent record
:type parent_isbn: string
"""
# If only journal title is present, and no other fields, assume the
# paper was submitted, but not yet published
if journal_title and all(
not field for field in (cnum, artid, journal_issue,
journal_volume, page_start, page_end)):
self.add_public_note('Submitted to {}'.format(journal_title))
return
publication_item = {}
for key in ('cnum', 'artid', 'page_end', 'page_start',
'journal_issue', 'journal_title',
'journal_volume', 'year', 'pubinfo_freetext', 'material'):
if locals()[key] is not None:
publication_item[key] = locals()[key]
if parent_record is not None:
parent_item = {'$ref': parent_record}
publication_item['parent_record'] = parent_item
if parent_isbn is not None:
publication_item['parent_isbn'] = normalize_isbn(parent_isbn)
if page_start and page_end:
try:
self.add_number_of_pages(
int(page_end) - int(page_start) + 1
)
except (TypeError, ValueError):
pass
self._append_to('publication_info', publication_item)
if is_citeable(self.record['publication_info']):
self.set_citeable(True) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:compute_operation; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 3, 12; 4, identifier:file1; 5, identifier:file2; 6, identifier:operation; 7, identifier:output; 8, identifier:display; 9, identifier:args_z1z2; 10, identifier:args_bbox; 11, identifier:args_keystitle; 12, identifier:args_geometry; 13, block; 13, 14; 13, 16; 13, 17; 13, 53; 13, 59; 13, 65; 13, 66; 13, 93; 13, 94; 13, 130; 13, 136; 13, 142; 13, 143; 13, 170; 13, 171; 13, 181; 13, 191; 13, 192; 13, 248; 13, 249; 13, 266; 13, 276; 13, 277; 14, expression_statement; 14, 15; 15, comment; 16, comment; 17, with_statement; 17, 18; 17, 29; 18, with_clause; 18, 19; 19, with_item; 19, 20; 20, as_pattern; 20, 21; 20, 27; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:fits; 24, identifier:open; 25, argument_list; 25, 26; 26, identifier:file1; 27, as_pattern_target; 27, 28; 28, identifier:hdulist; 29, block; 29, 30; 29, 38; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:image_header1; 33, attribute; 33, 34; 33, 37; 34, subscript; 34, 35; 34, 36; 35, identifier:hdulist; 36, integer:0; 37, identifier:header; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:image1; 41, call; 41, 42; 41, 49; 42, attribute; 42, 43; 42, 48; 43, attribute; 43, 44; 43, 47; 44, subscript; 44, 45; 44, 46; 45, identifier:hdulist; 46, integer:0; 47, identifier:data; 48, identifier:astype; 49, argument_list; 49, 50; 50, attribute; 50, 51; 50, 52; 51, identifier:np; 52, identifier:float; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:naxis1; 56, subscript; 56, 57; 56, 58; 57, identifier:image_header1; 58, string:'naxis1'; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:naxis2; 62, subscript; 62, 63; 62, 64; 63, identifier:image_header1; 64, string:'naxis2'; 65, comment; 66, if_statement; 66, 67; 66, 70; 67, comparison_operator:==; 67, 68; 67, 69; 68, identifier:display; 69, string:'all'; 70, block; 70, 71; 71, expression_statement; 71, 72; 72, call; 72, 73; 72, 74; 73, identifier:ximshow_file; 74, argument_list; 74, 75; 74, 78; 74, 81; 74, 84; 74, 87; 74, 90; 75, attribute; 75, 76; 75, 77; 76, identifier:file1; 77, identifier:name; 78, keyword_argument; 78, 79; 78, 80; 79, identifier:args_z1z2; 80, identifier:args_z1z2; 81, keyword_argument; 81, 82; 81, 83; 82, identifier:args_bbox; 83, identifier:args_bbox; 84, keyword_argument; 84, 85; 84, 86; 85, identifier:args_keystitle; 86, identifier:args_keystitle; 87, keyword_argument; 87, 88; 87, 89; 88, identifier:args_geometry; 89, identifier:args_geometry; 90, keyword_argument; 90, 91; 90, 92; 91, identifier:debugplot; 92, integer:12; 93, comment; 94, with_statement; 94, 95; 94, 106; 95, with_clause; 95, 96; 96, with_item; 96, 97; 97, as_pattern; 97, 98; 97, 104; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:fits; 101, identifier:open; 102, argument_list; 102, 103; 103, identifier:file2; 104, as_pattern_target; 104, 105; 105, identifier:hdulist; 106, block; 106, 107; 106, 115; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:image_header2; 110, attribute; 110, 111; 110, 114; 111, subscript; 111, 112; 111, 113; 112, identifier:hdulist; 113, integer:0; 114, identifier:header; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:image2; 118, call; 118, 119; 118, 126; 119, attribute; 119, 120; 119, 125; 120, attribute; 120, 121; 120, 124; 121, subscript; 121, 122; 121, 123; 122, identifier:hdulist; 123, integer:0; 124, identifier:data; 125, identifier:astype; 126, argument_list; 126, 127; 127, attribute; 127, 128; 127, 129; 128, identifier:np; 129, identifier:float; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:naxis1_; 133, subscript; 133, 134; 133, 135; 134, identifier:image_header2; 135, string:'naxis1'; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:naxis2_; 139, subscript; 139, 140; 139, 141; 140, identifier:image_header2; 141, string:'naxis2'; 142, comment; 143, if_statement; 143, 144; 143, 147; 144, comparison_operator:==; 144, 145; 144, 146; 145, identifier:display; 146, string:'all'; 147, block; 147, 148; 148, expression_statement; 148, 149; 149, call; 149, 150; 149, 151; 150, identifier:ximshow_file; 151, argument_list; 151, 152; 151, 155; 151, 158; 151, 161; 151, 164; 151, 167; 152, attribute; 152, 153; 152, 154; 153, identifier:file2; 154, identifier:name; 155, keyword_argument; 155, 156; 155, 157; 156, identifier:args_z1z2; 157, identifier:args_z1z2; 158, keyword_argument; 158, 159; 158, 160; 159, identifier:args_bbox; 160, identifier:args_bbox; 161, keyword_argument; 161, 162; 161, 163; 162, identifier:args_keystitle; 163, identifier:args_keystitle; 164, keyword_argument; 164, 165; 164, 166; 165, identifier:args_geometry; 166, identifier:args_geometry; 167, keyword_argument; 167, 168; 167, 169; 168, identifier:debugplot; 169, integer:12; 170, comment; 171, if_statement; 171, 172; 171, 175; 172, comparison_operator:!=; 172, 173; 172, 174; 173, identifier:naxis1; 174, identifier:naxis1_; 175, block; 175, 176; 176, raise_statement; 176, 177; 177, call; 177, 178; 177, 179; 178, identifier:ValueError; 179, argument_list; 179, 180; 180, string:"NAXIS1 values are different."; 181, if_statement; 181, 182; 181, 185; 182, comparison_operator:!=; 182, 183; 182, 184; 183, identifier:naxis2; 184, identifier:naxis2_; 185, block; 185, 186; 186, raise_statement; 186, 187; 187, call; 187, 188; 187, 189; 188, identifier:ValueError; 189, argument_list; 189, 190; 190, string:"NAXIS2 values are different."; 191, comment; 192, if_statement; 192, 193; 192, 196; 192, 203; 192, 214; 192, 225; 192, 236; 193, comparison_operator:==; 193, 194; 193, 195; 194, identifier:operation; 195, string:"+"; 196, block; 196, 197; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:solution; 200, binary_operator:+; 200, 201; 200, 202; 201, identifier:image1; 202, identifier:image2; 203, elif_clause; 203, 204; 203, 207; 204, comparison_operator:==; 204, 205; 204, 206; 205, identifier:operation; 206, string:"-"; 207, block; 207, 208; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 211; 210, identifier:solution; 211, binary_operator:-; 211, 212; 211, 213; 212, identifier:image1; 213, identifier:image2; 214, elif_clause; 214, 215; 214, 218; 215, comparison_operator:==; 215, 216; 215, 217; 216, identifier:operation; 217, string:"*"; 218, block; 218, 219; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 222; 221, identifier:solution; 222, binary_operator:*; 222, 223; 222, 224; 223, identifier:image1; 224, identifier:image2; 225, elif_clause; 225, 226; 225, 229; 226, comparison_operator:==; 226, 227; 226, 228; 227, identifier:operation; 228, string:"/"; 229, block; 229, 230; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:solution; 233, binary_operator:/; 233, 234; 233, 235; 234, identifier:image1; 235, identifier:image2; 236, else_clause; 236, 237; 237, block; 237, 238; 238, raise_statement; 238, 239; 239, call; 239, 240; 239, 241; 240, identifier:ValueError; 241, argument_list; 241, 242; 242, binary_operator:+; 242, 243; 242, 244; 243, string:"Unexpected operation="; 244, call; 244, 245; 244, 246; 245, identifier:str; 246, argument_list; 246, 247; 247, identifier:operation; 248, comment; 249, expression_statement; 249, 250; 250, assignment; 250, 251; 250, 252; 251, identifier:hdu; 252, call; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:fits; 255, identifier:PrimaryHDU; 256, argument_list; 256, 257; 256, 265; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:solution; 260, identifier:astype; 261, argument_list; 261, 262; 262, attribute; 262, 263; 262, 264; 263, identifier:np; 264, identifier:float; 265, identifier:image_header1; 266, expression_statement; 266, 267; 267, call; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:hdu; 270, identifier:writeto; 271, argument_list; 271, 272; 271, 273; 272, identifier:output; 273, keyword_argument; 273, 274; 273, 275; 274, identifier:overwrite; 275, True; 276, comment; 277, if_statement; 277, 278; 277, 283; 278, comparison_operator:in; 278, 279; 278, 280; 279, identifier:display; 280, list:['all', 'result']; 280, 281; 280, 282; 281, string:'all'; 282, string:'result'; 283, block; 283, 284; 284, expression_statement; 284, 285; 285, call; 285, 286; 285, 287; 286, identifier:ximshow_file; 287, argument_list; 287, 288; 287, 291; 287, 294; 287, 297; 287, 300; 287, 303; 288, attribute; 288, 289; 288, 290; 289, identifier:output; 290, identifier:name; 291, keyword_argument; 291, 292; 291, 293; 292, identifier:args_z1z2; 293, identifier:args_z1z2; 294, keyword_argument; 294, 295; 294, 296; 295, identifier:args_bbox; 296, identifier:args_bbox; 297, keyword_argument; 297, 298; 297, 299; 298, identifier:args_keystitle; 299, identifier:args_keystitle; 300, keyword_argument; 300, 301; 300, 302; 301, identifier:args_geometry; 302, identifier:args_geometry; 303, keyword_argument; 303, 304; 303, 305; 304, identifier:debugplot; 305, integer:12 | def compute_operation(file1, file2, operation, output, display,
args_z1z2, args_bbox, args_keystitle, args_geometry):
"""Compute output = file1 operation file2.
Parameters
----------
file1 : file object
First FITS file.
file2 : file object
Second FITS file.
operation : string
Mathematical operation.
output : file object
Output FITS file.
display : string
Character string indication whether the images are displayed.
Valid values are 'all', 'result' and 'none' (default).
args_z1z2 : string or None
String providing the image cuts tuple: z1, z2, minmax or None.
args_bbox : string or None
String providing the bounding box tuple: nc1, nc2, ns1, ns2.
args_keystitle : string or None
Tuple of FITS keywords.format: key1,key2,...,keyn.format
args_geometry : string or None
Tuple x,y,dx,dy to define the Qt backend geometry.
"""
# read first FITS file
with fits.open(file1) as hdulist:
image_header1 = hdulist[0].header
image1 = hdulist[0].data.astype(np.float)
naxis1 = image_header1['naxis1']
naxis2 = image_header1['naxis2']
# if required, display file1
if display == 'all':
ximshow_file(file1.name,
args_z1z2=args_z1z2, args_bbox=args_bbox,
args_keystitle=args_keystitle,
args_geometry=args_geometry,
debugplot=12)
# read second FITS file
with fits.open(file2) as hdulist:
image_header2 = hdulist[0].header
image2 = hdulist[0].data.astype(np.float)
naxis1_ = image_header2['naxis1']
naxis2_ = image_header2['naxis2']
# if required, display file2
if display == 'all':
ximshow_file(file2.name,
args_z1z2=args_z1z2, args_bbox=args_bbox,
args_keystitle=args_keystitle,
args_geometry=args_geometry,
debugplot=12)
# check dimensions
if naxis1 != naxis1_:
raise ValueError("NAXIS1 values are different.")
if naxis2 != naxis2_:
raise ValueError("NAXIS2 values are different.")
# compute operation
if operation == "+":
solution = image1 + image2
elif operation == "-":
solution = image1 - image2
elif operation == "*":
solution = image1 * image2
elif operation == "/":
solution = image1 / image2
else:
raise ValueError("Unexpected operation=" + str(operation))
# save output file
hdu = fits.PrimaryHDU(solution.astype(np.float), image_header1)
hdu.writeto(output, overwrite=True)
# if required, display result
if display in ['all', 'result']:
ximshow_file(output.name,
args_z1z2=args_z1z2, args_bbox=args_bbox,
args_keystitle=args_keystitle,
args_geometry=args_geometry,
debugplot=12) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:summary; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:x; 5, default_parameter; 5, 6; 5, 7; 6, identifier:rm_nan; 7, False; 8, default_parameter; 8, 9; 8, 10; 9, identifier:debug; 10, False; 11, block; 11, 12; 11, 14; 11, 15; 11, 67; 11, 88; 11, 89; 11, 108; 11, 109; 11, 116; 11, 122; 11, 242; 11, 358; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, if_statement; 15, 16; 15, 24; 15, 34; 16, comparison_operator:is; 16, 17; 16, 21; 17, call; 17, 18; 17, 19; 18, identifier:type; 19, argument_list; 19, 20; 20, identifier:x; 21, attribute; 21, 22; 21, 23; 22, identifier:np; 23, identifier:ndarray; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:xx; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:np; 31, identifier:copy; 32, argument_list; 32, 33; 33, identifier:x; 34, else_clause; 34, 35; 35, block; 35, 36; 36, if_statement; 36, 37; 36, 43; 36, 53; 37, comparison_operator:is; 37, 38; 37, 42; 38, call; 38, 39; 38, 40; 39, identifier:type; 40, argument_list; 40, 41; 41, identifier:x; 42, identifier:list; 43, block; 43, 44; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:xx; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:np; 50, identifier:array; 51, argument_list; 51, 52; 52, identifier:x; 53, else_clause; 53, 54; 54, block; 54, 55; 55, raise_statement; 55, 56; 56, call; 56, 57; 56, 58; 57, identifier:ValueError; 58, argument_list; 58, 59; 59, binary_operator:+; 59, 60; 59, 66; 60, binary_operator:+; 60, 61; 60, 62; 61, string:'x='; 62, call; 62, 63; 62, 64; 63, identifier:str; 64, argument_list; 64, 65; 65, identifier:x; 66, string:' must be a numpy.ndarray'; 67, if_statement; 67, 68; 67, 73; 68, comparison_operator:is; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:xx; 71, identifier:ndim; 72, integer:1; 73, block; 73, 74; 74, raise_statement; 74, 75; 75, call; 75, 76; 75, 77; 76, identifier:ValueError; 77, argument_list; 77, 78; 78, binary_operator:+; 78, 79; 78, 87; 79, binary_operator:+; 79, 80; 79, 81; 80, string:'xx.dim='; 81, call; 81, 82; 81, 83; 82, identifier:str; 83, argument_list; 83, 84; 84, attribute; 84, 85; 84, 86; 85, identifier:xx; 86, identifier:ndim; 87, string:' must be 1'; 88, comment; 89, if_statement; 89, 90; 89, 91; 90, identifier:rm_nan; 91, block; 91, 92; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:xx; 95, subscript; 95, 96; 95, 97; 96, identifier:xx; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:np; 100, identifier:logical_not; 101, argument_list; 101, 102; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:np; 105, identifier:isnan; 106, argument_list; 106, 107; 107, identifier:xx; 108, comment; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:npoints; 112, call; 112, 113; 112, 114; 113, identifier:len; 114, argument_list; 114, 115; 115, identifier:xx; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:ok; 119, comparison_operator:>; 119, 120; 119, 121; 120, identifier:npoints; 121, integer:0; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:result; 125, dictionary; 125, 126; 125, 129; 125, 140; 125, 152; 125, 164; 125, 175; 125, 187; 125, 198; 125, 209; 125, 218; 125, 230; 126, pair; 126, 127; 126, 128; 127, string:'npoints'; 128, identifier:npoints; 129, pair; 129, 130; 129, 131; 130, string:'minimum'; 131, conditional_expression:if; 131, 132; 131, 138; 131, 139; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:np; 135, identifier:min; 136, argument_list; 136, 137; 137, identifier:xx; 138, identifier:ok; 139, integer:0; 140, pair; 140, 141; 140, 142; 141, string:'percentile25'; 142, conditional_expression:if; 142, 143; 142, 150; 142, 151; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:np; 146, identifier:percentile; 147, argument_list; 147, 148; 147, 149; 148, identifier:xx; 149, integer:25; 150, identifier:ok; 151, integer:0; 152, pair; 152, 153; 152, 154; 153, string:'median'; 154, conditional_expression:if; 154, 155; 154, 162; 154, 163; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:np; 158, identifier:percentile; 159, argument_list; 159, 160; 159, 161; 160, identifier:xx; 161, integer:50; 162, identifier:ok; 163, integer:0; 164, pair; 164, 165; 164, 166; 165, string:'mean'; 166, conditional_expression:if; 166, 167; 166, 173; 166, 174; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:np; 170, identifier:mean; 171, argument_list; 171, 172; 172, identifier:xx; 173, identifier:ok; 174, integer:0; 175, pair; 175, 176; 175, 177; 176, string:'percentile75'; 177, conditional_expression:if; 177, 178; 177, 185; 177, 186; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:np; 181, identifier:percentile; 182, argument_list; 182, 183; 182, 184; 183, identifier:xx; 184, integer:75; 185, identifier:ok; 186, integer:0; 187, pair; 187, 188; 187, 189; 188, string:'maximum'; 189, conditional_expression:if; 189, 190; 189, 196; 189, 197; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:np; 193, identifier:max; 194, argument_list; 194, 195; 195, identifier:xx; 196, identifier:ok; 197, integer:0; 198, pair; 198, 199; 198, 200; 199, string:'std'; 200, conditional_expression:if; 200, 201; 200, 207; 200, 208; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:np; 204, identifier:std; 205, argument_list; 205, 206; 206, identifier:xx; 207, identifier:ok; 208, integer:0; 209, pair; 209, 210; 209, 211; 210, string:'robust_std'; 211, conditional_expression:if; 211, 212; 211, 216; 211, 217; 212, call; 212, 213; 212, 214; 213, identifier:robust_std; 214, argument_list; 214, 215; 215, identifier:xx; 216, identifier:ok; 217, integer:0; 218, pair; 218, 219; 218, 220; 219, string:'percentile15'; 220, conditional_expression:if; 220, 221; 220, 228; 220, 229; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:np; 224, identifier:percentile; 225, argument_list; 225, 226; 225, 227; 226, identifier:xx; 227, float:15.86553; 228, identifier:ok; 229, integer:0; 230, pair; 230, 231; 230, 232; 231, string:'percentile84'; 232, conditional_expression:if; 232, 233; 232, 240; 232, 241; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:np; 236, identifier:percentile; 237, argument_list; 237, 238; 237, 239; 238, identifier:xx; 239, float:84.13447; 240, identifier:ok; 241, integer:0; 242, if_statement; 242, 243; 242, 244; 243, identifier:debug; 244, block; 244, 245; 244, 250; 244, 255; 244, 260; 244, 268; 244, 276; 244, 284; 244, 292; 244, 300; 244, 308; 244, 316; 244, 321; 244, 329; 244, 337; 244, 345; 244, 353; 245, expression_statement; 245, 246; 246, call; 246, 247; 246, 248; 247, identifier:print; 248, argument_list; 248, 249; 249, string:'>>> ========================================'; 250, expression_statement; 250, 251; 251, call; 251, 252; 251, 253; 252, identifier:print; 253, argument_list; 253, 254; 254, string:'>>> STATISTICAL SUMMARY:'; 255, expression_statement; 255, 256; 256, call; 256, 257; 256, 258; 257, identifier:print; 258, argument_list; 258, 259; 259, string:'>>> ----------------------------------------'; 260, expression_statement; 260, 261; 261, call; 261, 262; 261, 263; 262, identifier:print; 263, argument_list; 263, 264; 263, 265; 264, string:'>>> Number of points.........:'; 265, subscript; 265, 266; 265, 267; 266, identifier:result; 267, string:'npoints'; 268, expression_statement; 268, 269; 269, call; 269, 270; 269, 271; 270, identifier:print; 271, argument_list; 271, 272; 271, 273; 272, string:'>>> Minimum..................:'; 273, subscript; 273, 274; 273, 275; 274, identifier:result; 275, string:'minimum'; 276, expression_statement; 276, 277; 277, call; 277, 278; 277, 279; 278, identifier:print; 279, argument_list; 279, 280; 279, 281; 280, string:'>>> 1st Quartile.............:'; 281, subscript; 281, 282; 281, 283; 282, identifier:result; 283, string:'percentile25'; 284, expression_statement; 284, 285; 285, call; 285, 286; 285, 287; 286, identifier:print; 287, argument_list; 287, 288; 287, 289; 288, string:'>>> Median...................:'; 289, subscript; 289, 290; 289, 291; 290, identifier:result; 291, string:'median'; 292, expression_statement; 292, 293; 293, call; 293, 294; 293, 295; 294, identifier:print; 295, argument_list; 295, 296; 295, 297; 296, string:'>>> Mean.....................:'; 297, subscript; 297, 298; 297, 299; 298, identifier:result; 299, string:'mean'; 300, expression_statement; 300, 301; 301, call; 301, 302; 301, 303; 302, identifier:print; 303, argument_list; 303, 304; 303, 305; 304, string:'>>> 3rd Quartile.............:'; 305, subscript; 305, 306; 305, 307; 306, identifier:result; 307, string:'percentile75'; 308, expression_statement; 308, 309; 309, call; 309, 310; 309, 311; 310, identifier:print; 311, argument_list; 311, 312; 311, 313; 312, string:'>>> Maximum..................:'; 313, subscript; 313, 314; 313, 315; 314, identifier:result; 315, string:'maximum'; 316, expression_statement; 316, 317; 317, call; 317, 318; 317, 319; 318, identifier:print; 319, argument_list; 319, 320; 320, string:'>>> ----------------------------------------'; 321, expression_statement; 321, 322; 322, call; 322, 323; 322, 324; 323, identifier:print; 324, argument_list; 324, 325; 324, 326; 325, string:'>>> Standard deviation.......:'; 326, subscript; 326, 327; 326, 328; 327, identifier:result; 328, string:'std'; 329, expression_statement; 329, 330; 330, call; 330, 331; 330, 332; 331, identifier:print; 332, argument_list; 332, 333; 332, 334; 333, string:'>>> Robust standard deviation:'; 334, subscript; 334, 335; 334, 336; 335, identifier:result; 336, string:'robust_std'; 337, expression_statement; 337, 338; 338, call; 338, 339; 338, 340; 339, identifier:print; 340, argument_list; 340, 341; 340, 342; 341, string:'>>> 0.1586553 percentile.....:'; 342, subscript; 342, 343; 342, 344; 343, identifier:result; 344, string:'percentile15'; 345, expression_statement; 345, 346; 346, call; 346, 347; 346, 348; 347, identifier:print; 348, argument_list; 348, 349; 348, 350; 349, string:'>>> 0.8413447 percentile.....:'; 350, subscript; 350, 351; 350, 352; 351, identifier:result; 352, string:'percentile84'; 353, expression_statement; 353, 354; 354, call; 354, 355; 354, 356; 355, identifier:print; 356, argument_list; 356, 357; 357, string:'>>> ========================================'; 358, return_statement; 358, 359; 359, identifier:result | def summary(x, rm_nan=False, debug=False):
"""Compute basic statistical parameters.
Parameters
----------
x : 1d numpy array, float
Input array with values which statistical properties are
requested.
rm_nan : bool
If True, filter out NaN values before computing statistics.
debug : bool
If True prints computed values.
Returns
-------
result : Python dictionary
Number of points, minimum, percentile 25, percentile 50
(median), mean, percentile 75, maximum, standard deviation,
robust standard deviation, percentile 15.866 (equivalent
to -1 sigma in a normal distribution) and percentile 84.134
(+1 sigma).
"""
# protections
if type(x) is np.ndarray:
xx = np.copy(x)
else:
if type(x) is list:
xx = np.array(x)
else:
raise ValueError('x=' + str(x) + ' must be a numpy.ndarray')
if xx.ndim is not 1:
raise ValueError('xx.dim=' + str(xx.ndim) + ' must be 1')
# filter out NaN's
if rm_nan:
xx = xx[np.logical_not(np.isnan(xx))]
# compute basic statistics
npoints = len(xx)
ok = npoints > 0
result = {
'npoints' : npoints,
'minimum' : np.min(xx) if ok else 0,
'percentile25' : np.percentile(xx, 25) if ok else 0,
'median' : np.percentile(xx, 50) if ok else 0,
'mean' : np.mean(xx) if ok else 0,
'percentile75': np.percentile(xx, 75) if ok else 0,
'maximum' : np.max(xx) if ok else 0,
'std': np.std(xx) if ok else 0,
'robust_std' : robust_std(xx) if ok else 0,
'percentile15': np.percentile(xx, 15.86553) if ok else 0,
'percentile84': np.percentile(xx, 84.13447) if ok else 0
}
if debug:
print('>>> ========================================')
print('>>> STATISTICAL SUMMARY:')
print('>>> ----------------------------------------')
print('>>> Number of points.........:', result['npoints'])
print('>>> Minimum..................:', result['minimum'])
print('>>> 1st Quartile.............:', result['percentile25'])
print('>>> Median...................:', result['median'])
print('>>> Mean.....................:', result['mean'])
print('>>> 3rd Quartile.............:', result['percentile75'])
print('>>> Maximum..................:', result['maximum'])
print('>>> ----------------------------------------')
print('>>> Standard deviation.......:', result['std'])
print('>>> Robust standard deviation:', result['robust_std'])
print('>>> 0.1586553 percentile.....:', result['percentile15'])
print('>>> 0.8413447 percentile.....:', result['percentile84'])
print('>>> ========================================')
return result |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:find_peaks_spectrum; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:sx; 5, identifier:nwinwidth; 6, default_parameter; 6, 7; 6, 8; 7, identifier:threshold; 8, integer:0; 9, default_parameter; 9, 10; 9, 11; 10, identifier:debugplot; 11, integer:0; 12, block; 12, 13; 12, 15; 12, 58; 12, 64; 12, 70; 12, 106; 12, 110; 12, 111; 12, 135; 12, 139; 12, 286; 12, 295; 12, 314; 13, expression_statement; 13, 14; 14, comment; 15, if_statement; 15, 16; 15, 24; 15, 37; 16, comparison_operator:is; 16, 17; 16, 21; 17, call; 17, 18; 17, 19; 18, identifier:type; 19, argument_list; 19, 20; 20, identifier:sx; 21, attribute; 21, 22; 21, 23; 22, identifier:np; 23, identifier:ndarray; 24, block; 24, 25; 25, raise_statement; 25, 26; 26, call; 26, 27; 26, 28; 27, identifier:ValueError; 28, argument_list; 28, 29; 29, binary_operator:+; 29, 30; 29, 36; 30, binary_operator:+; 30, 31; 30, 32; 31, string:"sx="; 32, call; 32, 33; 32, 34; 33, identifier:str; 34, argument_list; 34, 35; 35, identifier:sx; 36, string:" must be a numpy.ndarray"; 37, elif_clause; 37, 38; 37, 43; 38, comparison_operator:is; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:sx; 41, identifier:ndim; 42, integer:1; 43, block; 43, 44; 44, raise_statement; 44, 45; 45, call; 45, 46; 45, 47; 46, identifier:ValueError; 47, argument_list; 47, 48; 48, binary_operator:+; 48, 49; 48, 57; 49, binary_operator:+; 49, 50; 49, 51; 50, string:"sx.ndim="; 51, call; 51, 52; 51, 53; 52, identifier:str; 53, argument_list; 53, 54; 54, attribute; 54, 55; 54, 56; 55, identifier:sx; 56, identifier:ndim; 57, string:" must be 1"; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:sx_shape; 61, attribute; 61, 62; 61, 63; 62, identifier:sx; 63, identifier:shape; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:nmed; 67, binary_operator://; 67, 68; 67, 69; 68, identifier:nwinwidth; 69, integer:2; 70, if_statement; 70, 71; 70, 74; 71, comparison_operator:>=; 71, 72; 71, 73; 72, identifier:debugplot; 73, integer:10; 74, block; 74, 75; 74, 81; 74, 87; 74, 93; 74, 99; 75, expression_statement; 75, 76; 76, call; 76, 77; 76, 78; 77, identifier:print; 78, argument_list; 78, 79; 78, 80; 79, string:'find_peaks_spectrum> sx shape......:'; 80, identifier:sx_shape; 81, expression_statement; 81, 82; 82, call; 82, 83; 82, 84; 83, identifier:print; 84, argument_list; 84, 85; 84, 86; 85, string:'find_peaks_spectrum> nwinwidth.....:'; 86, identifier:nwinwidth; 87, expression_statement; 87, 88; 88, call; 88, 89; 88, 90; 89, identifier:print; 90, argument_list; 90, 91; 90, 92; 91, string:'find_peaks_spectrum> nmed..........:'; 92, identifier:nmed; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 96; 95, identifier:print; 96, argument_list; 96, 97; 96, 98; 97, string:'find_peaks_spectrum> data_threshold:'; 98, identifier:threshold; 99, expression_statement; 99, 100; 100, call; 100, 101; 100, 102; 101, identifier:print; 102, argument_list; 102, 103; 102, 104; 102, 105; 103, string:'find_peaks_spectrum> the first and last'; 104, identifier:nmed; 105, string:'pixels will be ignored'; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:xpeaks; 109, list:[]; 110, comment; 111, if_statement; 111, 112; 111, 117; 112, comparison_operator:<; 112, 113; 112, 116; 113, subscript; 113, 114; 113, 115; 114, identifier:sx_shape; 115, integer:0; 116, identifier:nwinwidth; 117, block; 117, 118; 117, 124; 117, 130; 118, expression_statement; 118, 119; 119, call; 119, 120; 119, 121; 120, identifier:print; 121, argument_list; 121, 122; 121, 123; 122, string:'find_peaks_spectrum> sx shape......:'; 123, identifier:sx_shape; 124, expression_statement; 124, 125; 125, call; 125, 126; 125, 127; 126, identifier:print; 127, argument_list; 127, 128; 127, 129; 128, string:'find_peaks_spectrum> nwinwidth.....:'; 129, identifier:nwinwidth; 130, raise_statement; 130, 131; 131, call; 131, 132; 131, 133; 132, identifier:ValueError; 133, argument_list; 133, 134; 134, string:'sx.shape < nwinwidth'; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:i; 138, identifier:nmed; 139, while_statement; 139, 140; 139, 147; 140, comparison_operator:<; 140, 141; 140, 142; 141, identifier:i; 142, binary_operator:-; 142, 143; 142, 146; 143, subscript; 143, 144; 143, 145; 144, identifier:sx_shape; 145, integer:0; 146, identifier:nmed; 147, block; 147, 148; 148, if_statement; 148, 149; 148, 154; 148, 280; 149, comparison_operator:>; 149, 150; 149, 153; 150, subscript; 150, 151; 150, 152; 151, identifier:sx; 152, identifier:i; 153, identifier:threshold; 154, block; 154, 155; 154, 159; 154, 163; 154, 167; 154, 206; 154, 258; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:peak_ok; 158, True; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:j; 162, integer:0; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:loop; 166, True; 167, while_statement; 167, 168; 167, 169; 168, identifier:loop; 169, block; 169, 170; 169, 193; 169, 197; 170, if_statement; 170, 171; 170, 188; 171, comparison_operator:>; 171, 172; 171, 179; 172, subscript; 172, 173; 172, 174; 173, identifier:sx; 174, binary_operator:+; 174, 175; 174, 178; 175, binary_operator:-; 175, 176; 175, 177; 176, identifier:i; 177, identifier:nmed; 178, identifier:j; 179, subscript; 179, 180; 179, 181; 180, identifier:sx; 181, binary_operator:+; 181, 182; 181, 187; 182, binary_operator:+; 182, 183; 182, 186; 183, binary_operator:-; 183, 184; 183, 185; 184, identifier:i; 185, identifier:nmed; 186, identifier:j; 187, integer:1; 188, block; 188, 189; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:peak_ok; 192, False; 193, expression_statement; 193, 194; 194, augmented_assignment:+=; 194, 195; 194, 196; 195, identifier:j; 196, integer:1; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:loop; 200, boolean_operator:and; 200, 201; 200, 205; 201, parenthesized_expression; 201, 202; 202, comparison_operator:<; 202, 203; 202, 204; 203, identifier:j; 204, identifier:nmed; 205, identifier:peak_ok; 206, if_statement; 206, 207; 206, 208; 207, identifier:peak_ok; 208, block; 208, 209; 208, 215; 208, 219; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:j; 212, binary_operator:+; 212, 213; 212, 214; 213, identifier:nmed; 214, integer:1; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:loop; 218, True; 219, while_statement; 219, 220; 219, 221; 220, identifier:loop; 221, block; 221, 222; 221, 245; 221, 249; 222, if_statement; 222, 223; 222, 240; 223, comparison_operator:<; 223, 224; 223, 233; 224, subscript; 224, 225; 224, 226; 225, identifier:sx; 226, binary_operator:-; 226, 227; 226, 232; 227, binary_operator:+; 227, 228; 227, 231; 228, binary_operator:-; 228, 229; 228, 230; 229, identifier:i; 230, identifier:nmed; 231, identifier:j; 232, integer:1; 233, subscript; 233, 234; 233, 235; 234, identifier:sx; 235, binary_operator:+; 235, 236; 235, 239; 236, binary_operator:-; 236, 237; 236, 238; 237, identifier:i; 238, identifier:nmed; 239, identifier:j; 240, block; 240, 241; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 244; 243, identifier:peak_ok; 244, False; 245, expression_statement; 245, 246; 246, augmented_assignment:+=; 246, 247; 246, 248; 247, identifier:j; 248, integer:1; 249, expression_statement; 249, 250; 250, assignment; 250, 251; 250, 252; 251, identifier:loop; 252, boolean_operator:and; 252, 253; 252, 257; 253, parenthesized_expression; 253, 254; 254, comparison_operator:<; 254, 255; 254, 256; 255, identifier:j; 256, identifier:nwinwidth; 257, identifier:peak_ok; 258, if_statement; 258, 259; 258, 260; 258, 274; 259, identifier:peak_ok; 260, block; 260, 261; 260, 268; 261, expression_statement; 261, 262; 262, call; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:xpeaks; 265, identifier:append; 266, argument_list; 266, 267; 267, identifier:i; 268, expression_statement; 268, 269; 269, augmented_assignment:+=; 269, 270; 269, 271; 270, identifier:i; 271, binary_operator:-; 271, 272; 271, 273; 272, identifier:nwinwidth; 273, integer:1; 274, else_clause; 274, 275; 275, block; 275, 276; 276, expression_statement; 276, 277; 277, augmented_assignment:+=; 277, 278; 277, 279; 278, identifier:i; 279, integer:1; 280, else_clause; 280, 281; 281, block; 281, 282; 282, expression_statement; 282, 283; 283, augmented_assignment:+=; 283, 284; 283, 285; 284, identifier:i; 285, integer:1; 286, expression_statement; 286, 287; 287, assignment; 287, 288; 287, 289; 288, identifier:ixpeaks; 289, call; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:np; 292, identifier:array; 293, argument_list; 293, 294; 294, identifier:xpeaks; 295, if_statement; 295, 296; 295, 299; 296, comparison_operator:>=; 296, 297; 296, 298; 297, identifier:debugplot; 298, integer:10; 299, block; 299, 300; 299, 309; 300, expression_statement; 300, 301; 301, call; 301, 302; 301, 303; 302, identifier:print; 303, argument_list; 303, 304; 303, 305; 304, string:'find_peaks_spectrum> number of peaks found:'; 305, call; 305, 306; 305, 307; 306, identifier:len; 307, argument_list; 307, 308; 308, identifier:ixpeaks; 309, expression_statement; 309, 310; 310, call; 310, 311; 310, 312; 311, identifier:print; 312, argument_list; 312, 313; 313, identifier:ixpeaks; 314, return_statement; 314, 315; 315, identifier:ixpeaks | def find_peaks_spectrum(sx, nwinwidth, threshold=0, debugplot=0):
"""Find peaks in array.
The algorithm imposes that the signal at both sides of the peak
decreases monotonically.
Parameters
----------
sx : 1d numpy array, floats
Input array.
nwinwidth : int
Width of the window where each peak must be found.
threshold : float
Minimum signal in the peaks.
debugplot : int
Determines whether intermediate computations and/or plots
are displayed:
00 : no debug, no plots
01 : no debug, plots without pauses
02 : no debug, plots with pauses
10 : debug, no plots
11 : debug, plots without pauses
12 : debug, plots with pauses
Returns
-------
ixpeaks : 1d numpy array, int
Peak locations, in array coordinates (integers).
"""
if type(sx) is not np.ndarray:
raise ValueError("sx=" + str(sx) + " must be a numpy.ndarray")
elif sx.ndim is not 1:
raise ValueError("sx.ndim=" + str(sx.ndim) + " must be 1")
sx_shape = sx.shape
nmed = nwinwidth//2
if debugplot >= 10:
print('find_peaks_spectrum> sx shape......:', sx_shape)
print('find_peaks_spectrum> nwinwidth.....:', nwinwidth)
print('find_peaks_spectrum> nmed..........:', nmed)
print('find_peaks_spectrum> data_threshold:', threshold)
print('find_peaks_spectrum> the first and last', nmed,
'pixels will be ignored')
xpeaks = [] # list to store the peaks
if sx_shape[0] < nwinwidth:
print('find_peaks_spectrum> sx shape......:', sx_shape)
print('find_peaks_spectrum> nwinwidth.....:', nwinwidth)
raise ValueError('sx.shape < nwinwidth')
i = nmed
while i < sx_shape[0] - nmed:
if sx[i] > threshold:
peak_ok = True
j = 0
loop = True
while loop:
if sx[i - nmed + j] > sx[i - nmed + j + 1]:
peak_ok = False
j += 1
loop = (j < nmed) and peak_ok
if peak_ok:
j = nmed + 1
loop = True
while loop:
if sx[i - nmed + j - 1] < sx[i - nmed + j]:
peak_ok = False
j += 1
loop = (j < nwinwidth) and peak_ok
if peak_ok:
xpeaks.append(i)
i += nwinwidth - 1
else:
i += 1
else:
i += 1
ixpeaks = np.array(xpeaks)
if debugplot >= 10:
print('find_peaks_spectrum> number of peaks found:', len(ixpeaks))
print(ixpeaks)
return ixpeaks |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:shortlex; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:start; 5, identifier:other; 6, default_parameter; 6, 7; 6, 8; 7, identifier:excludestart; 8, False; 9, block; 9, 10; 9, 12; 9, 19; 9, 31; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 15; 13, not_operator; 13, 14; 14, identifier:excludestart; 15, block; 15, 16; 16, expression_statement; 16, 17; 17, yield; 17, 18; 18, identifier:start; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:queue; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:collections; 25, identifier:deque; 26, argument_list; 26, 27; 27, list:[(start, other)]; 27, 28; 28, tuple; 28, 29; 28, 30; 29, identifier:start; 30, identifier:other; 31, while_statement; 31, 32; 31, 33; 32, identifier:queue; 33, block; 33, 34; 33, 44; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 39; 36, pattern_list; 36, 37; 36, 38; 37, identifier:current; 38, identifier:other; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:queue; 42, identifier:popleft; 43, argument_list; 44, while_statement; 44, 45; 44, 46; 45, identifier:other; 46, block; 46, 47; 46, 61; 46, 67; 46, 70; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 52; 49, pattern_list; 49, 50; 49, 51; 50, identifier:first; 51, identifier:other; 52, expression_list; 52, 53; 52, 56; 53, subscript; 53, 54; 53, 55; 54, identifier:other; 55, integer:0; 56, subscript; 56, 57; 56, 58; 57, identifier:other; 58, slice; 58, 59; 58, 60; 59, integer:1; 60, colon; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:result; 64, binary_operator:|; 64, 65; 64, 66; 65, identifier:current; 66, identifier:first; 67, expression_statement; 67, 68; 68, yield; 68, 69; 69, identifier:result; 70, if_statement; 70, 71; 70, 72; 71, identifier:other; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:queue; 77, identifier:append; 78, argument_list; 78, 79; 79, tuple; 79, 80; 79, 81; 80, identifier:result; 81, identifier:other | def shortlex(start, other, excludestart=False):
"""Yield all unions of start with other in shortlex order.
>>> ['{:03b}'.format(s) for s in shortlex(0, [0b100, 0b010, 0b001])]
['000', '100', '010', '001', '110', '101', '011', '111']
>>> ', '.join(''.join(sorted(s))
... for s in shortlex(set(), [{'a'}, {'b'}, {'c'}, {'d'}]))
', a, b, c, d, ab, ac, ad, bc, bd, cd, abc, abd, acd, bcd, abcd'
>>> assert list(shortlex(set(), [{1}, {2}], excludestart=True)) == \
[{1}, {2}, {1, 2}]
"""
if not excludestart:
yield start
queue = collections.deque([(start, other)])
while queue:
current, other = queue.popleft()
while other:
first, other = other[0], other[1:]
result = current | first
yield result
if other:
queue.append((result, other)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:reverse_shortlex; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:end; 5, identifier:other; 6, default_parameter; 6, 7; 6, 8; 7, identifier:excludeend; 8, False; 9, block; 9, 10; 9, 12; 9, 19; 9, 31; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 15; 13, not_operator; 13, 14; 14, identifier:excludeend; 15, block; 15, 16; 16, expression_statement; 16, 17; 17, yield; 17, 18; 18, identifier:end; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:queue; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:collections; 25, identifier:deque; 26, argument_list; 26, 27; 27, list:[(end, other)]; 27, 28; 28, tuple; 28, 29; 28, 30; 29, identifier:end; 30, identifier:other; 31, while_statement; 31, 32; 31, 33; 32, identifier:queue; 33, block; 33, 34; 33, 44; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 39; 36, pattern_list; 36, 37; 36, 38; 37, identifier:current; 38, identifier:other; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:queue; 42, identifier:popleft; 43, argument_list; 44, while_statement; 44, 45; 44, 46; 45, identifier:other; 46, block; 46, 47; 46, 61; 46, 67; 46, 70; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 52; 49, pattern_list; 49, 50; 49, 51; 50, identifier:first; 51, identifier:other; 52, expression_list; 52, 53; 52, 56; 53, subscript; 53, 54; 53, 55; 54, identifier:other; 55, integer:0; 56, subscript; 56, 57; 56, 58; 57, identifier:other; 58, slice; 58, 59; 58, 60; 59, integer:1; 60, colon; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:result; 64, binary_operator:&; 64, 65; 64, 66; 65, identifier:current; 66, identifier:first; 67, expression_statement; 67, 68; 68, yield; 68, 69; 69, identifier:result; 70, if_statement; 70, 71; 70, 72; 71, identifier:other; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:queue; 77, identifier:append; 78, argument_list; 78, 79; 79, tuple; 79, 80; 79, 81; 80, identifier:result; 81, identifier:other | def reverse_shortlex(end, other, excludeend=False):
"""Yield all intersections of end with other in reverse shortlex order.
>>> ['{:03b}'.format(s) for s in reverse_shortlex(0b111, [0b011, 0b101, 0b110])]
['111', '011', '101', '110', '001', '010', '100', '000']
>>> ', '.join(''.join(sorted(s))
... for s in reverse_shortlex({'a', 'b', 'c', 'd'},
... [{'b', 'c', 'd'}, {'a', 'c', 'd'}, {'a', 'b', 'd'}, {'a', 'b', 'c'}]))
'abcd, bcd, acd, abd, abc, cd, bd, bc, ad, ac, ab, d, c, b, a, '
>>> assert list(reverse_shortlex({1, 2}, [{1}, {2}], excludeend=True)) == \
[{1}, {2}, set()]
"""
if not excludeend:
yield end
queue = collections.deque([(end, other)])
while queue:
current, other = queue.popleft()
while other:
first, other = other[0], other[1:]
result = current & first
yield result
if other:
queue.append((result, other)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:gnuplot; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:script_name; 5, default_parameter; 5, 6; 5, 7; 6, identifier:args_dict; 7, dictionary; 8, default_parameter; 8, 9; 8, 10; 9, identifier:data; 10, list:[]; 11, default_parameter; 11, 12; 11, 13; 12, identifier:silent; 13, True; 14, block; 14, 15; 14, 17; 14, 21; 14, 45; 14, 172; 14, 178; 14, 185; 14, 192; 15, expression_statement; 15, 16; 16, string:'''
Call a Gnuplot script, passing it arguments and
datasets.
Args:
scipt_name(str): The name of the Gnuplot script.
args_dict(dict): A dictionary of parameters to pass
to the script. The `key` is the name of the variable
that the `item` will be passed to the Gnuplot script
with.
data(list): A list of lists containing lists to be plotted.
The lists can be accessed by plotting the variable
`data` in the Gnuplot script. The first list in the
list of lists corresponds to the first column in data,
and so on.
silent (bool): `True` if Gnuplot stdout should be silenced,
`False` if not.
Returns:
str: The Gnuplot command used to call the script.
'''; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:gnuplot_command; 20, string:'gnuplot'; 21, if_statement; 21, 22; 21, 23; 22, identifier:data; 23, block; 23, 24; 23, 29; 23, 37; 24, assert_statement; 24, 25; 24, 28; 25, comparison_operator:not; 25, 26; 25, 27; 26, string:'data'; 27, identifier:args_dict; 28, string:'Can\'t use \'data\' variable twice.'; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:data_temp; 32, call; 32, 33; 32, 34; 33, identifier:_GnuplotDataTemp; 34, argument_list; 34, 35; 35, list_splat; 35, 36; 36, identifier:data; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 42; 39, subscript; 39, 40; 39, 41; 40, identifier:args_dict; 41, string:'data'; 42, attribute; 42, 43; 42, 44; 43, identifier:data_temp; 44, identifier:name; 45, if_statement; 45, 46; 45, 47; 46, identifier:args_dict; 47, block; 47, 48; 47, 52; 47, 159; 47, 168; 48, expression_statement; 48, 49; 49, augmented_assignment:+=; 49, 50; 49, 51; 50, identifier:gnuplot_command; 51, string:' -e "'; 52, for_statement; 52, 53; 52, 54; 52, 59; 53, identifier:arg; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:args_dict; 57, identifier:items; 58, argument_list; 59, block; 59, 60; 59, 68; 59, 155; 60, expression_statement; 60, 61; 61, augmented_assignment:+=; 61, 62; 61, 63; 62, identifier:gnuplot_command; 63, binary_operator:+; 63, 64; 63, 67; 64, subscript; 64, 65; 64, 66; 65, identifier:arg; 66, integer:0; 67, string:'='; 68, if_statement; 68, 69; 68, 76; 68, 87; 68, 113; 68, 144; 69, call; 69, 70; 69, 71; 70, identifier:isinstance; 71, argument_list; 71, 72; 71, 75; 72, subscript; 72, 73; 72, 74; 73, identifier:arg; 74, integer:1; 75, identifier:str; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, augmented_assignment:+=; 78, 79; 78, 80; 79, identifier:gnuplot_command; 80, binary_operator:+; 80, 81; 80, 86; 81, binary_operator:+; 81, 82; 81, 83; 82, string:'\''; 83, subscript; 83, 84; 83, 85; 84, identifier:arg; 85, integer:1; 86, string:'\''; 87, elif_clause; 87, 88; 87, 95; 88, call; 88, 89; 88, 90; 89, identifier:isinstance; 90, argument_list; 90, 91; 90, 94; 91, subscript; 91, 92; 91, 93; 92, identifier:arg; 93, integer:1; 94, identifier:bool; 95, block; 95, 96; 96, if_statement; 96, 97; 96, 102; 96, 107; 97, comparison_operator:is; 97, 98; 97, 101; 98, subscript; 98, 99; 98, 100; 99, identifier:arg; 100, integer:1; 101, True; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, augmented_assignment:+=; 104, 105; 104, 106; 105, identifier:gnuplot_command; 106, string:'1'; 107, else_clause; 107, 108; 108, block; 108, 109; 109, expression_statement; 109, 110; 110, augmented_assignment:+=; 110, 111; 110, 112; 111, identifier:gnuplot_command; 112, string:'0'; 113, elif_clause; 113, 114; 113, 121; 114, call; 114, 115; 114, 116; 115, identifier:hasattr; 116, argument_list; 116, 117; 116, 120; 117, subscript; 117, 118; 117, 119; 118, identifier:arg; 119, integer:1; 120, string:'__iter__'; 121, block; 121, 122; 122, expression_statement; 122, 123; 123, augmented_assignment:+=; 123, 124; 123, 125; 124, identifier:gnuplot_command; 125, binary_operator:+; 125, 126; 125, 143; 126, binary_operator:+; 126, 127; 126, 128; 127, string:'\''; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, string:' '; 131, identifier:join; 132, argument_list; 132, 133; 133, list_comprehension; 133, 134; 133, 138; 134, call; 134, 135; 134, 136; 135, identifier:str; 136, argument_list; 136, 137; 137, identifier:v; 138, for_in_clause; 138, 139; 138, 140; 139, identifier:v; 140, subscript; 140, 141; 140, 142; 141, identifier:arg; 142, integer:1; 143, string:'\''; 144, else_clause; 144, 145; 145, block; 145, 146; 146, expression_statement; 146, 147; 147, augmented_assignment:+=; 147, 148; 147, 149; 148, identifier:gnuplot_command; 149, call; 149, 150; 149, 151; 150, identifier:str; 151, argument_list; 151, 152; 152, subscript; 152, 153; 152, 154; 153, identifier:arg; 154, integer:1; 155, expression_statement; 155, 156; 156, augmented_assignment:+=; 156, 157; 156, 158; 157, identifier:gnuplot_command; 158, string:'; '; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:gnuplot_command; 162, subscript; 162, 163; 162, 164; 163, identifier:gnuplot_command; 164, slice; 164, 165; 164, 166; 165, colon; 166, unary_operator:-; 166, 167; 167, integer:1; 168, expression_statement; 168, 169; 169, augmented_assignment:+=; 169, 170; 169, 171; 170, identifier:gnuplot_command; 171, string:'"'; 172, expression_statement; 172, 173; 173, augmented_assignment:+=; 173, 174; 173, 175; 174, identifier:gnuplot_command; 175, binary_operator:+; 175, 176; 175, 177; 176, string:' '; 177, identifier:script_name; 178, if_statement; 178, 179; 178, 180; 179, identifier:silent; 180, block; 180, 181; 181, expression_statement; 181, 182; 182, augmented_assignment:+=; 182, 183; 182, 184; 183, identifier:gnuplot_command; 184, string:' > /dev/null 2>&1'; 185, expression_statement; 185, 186; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:os; 189, identifier:system; 190, argument_list; 190, 191; 191, identifier:gnuplot_command; 192, return_statement; 192, 193; 193, identifier:gnuplot_command | def gnuplot(script_name, args_dict={}, data=[], silent=True):
'''
Call a Gnuplot script, passing it arguments and
datasets.
Args:
scipt_name(str): The name of the Gnuplot script.
args_dict(dict): A dictionary of parameters to pass
to the script. The `key` is the name of the variable
that the `item` will be passed to the Gnuplot script
with.
data(list): A list of lists containing lists to be plotted.
The lists can be accessed by plotting the variable
`data` in the Gnuplot script. The first list in the
list of lists corresponds to the first column in data,
and so on.
silent (bool): `True` if Gnuplot stdout should be silenced,
`False` if not.
Returns:
str: The Gnuplot command used to call the script.
'''
gnuplot_command = 'gnuplot'
if data:
assert 'data' not in args_dict, \
'Can\'t use \'data\' variable twice.'
data_temp = _GnuplotDataTemp(*data)
args_dict['data'] = data_temp.name
if args_dict:
gnuplot_command += ' -e "'
for arg in args_dict.items():
gnuplot_command += arg[0] + '='
if isinstance(arg[1], str):
gnuplot_command += '\'' + arg[1] + '\''
elif isinstance(arg[1], bool):
if arg[1] is True:
gnuplot_command += '1'
else:
gnuplot_command += '0'
elif hasattr(arg[1], '__iter__'):
gnuplot_command += '\'' + ' '.join([str(v) for v in arg[1]]) + '\''
else:
gnuplot_command += str(arg[1])
gnuplot_command += '; '
gnuplot_command = gnuplot_command[:-1]
gnuplot_command += '"'
gnuplot_command += ' ' + script_name
if silent:
gnuplot_command += ' > /dev/null 2>&1'
os.system(gnuplot_command)
return gnuplot_command |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:read; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:skip; 7, list:[]; 8, default_parameter; 8, 9; 8, 10; 9, identifier:goto_metal; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:goto_reaction; 13, None; 14, block; 14, 15; 14, 17; 14, 38; 14, 40; 14, 60; 14, 69; 14, 78; 14, 87; 14, 91; 15, expression_statement; 15, 16; 16, comment; 17, if_statement; 17, 18; 17, 24; 18, comparison_operator:>; 18, 19; 18, 23; 19, call; 19, 20; 19, 21; 20, identifier:len; 21, argument_list; 21, 22; 22, identifier:skip; 23, integer:0; 24, block; 24, 25; 25, for_statement; 25, 26; 25, 27; 25, 28; 26, identifier:skip_f; 27, identifier:skip; 28, block; 28, 29; 29, expression_statement; 29, 30; 30, call; 30, 31; 30, 36; 31, attribute; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:omit_folders; 35, identifier:append; 36, argument_list; 36, 37; 37, identifier:skip_f; 38, expression_statement; 38, 39; 39, comment; 40, if_statement; 40, 41; 40, 53; 41, call; 41, 42; 41, 47; 42, attribute; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:os; 45, identifier:path; 46, identifier:isfile; 47, argument_list; 47, 48; 48, binary_operator:+; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:data_base; 52, string:'/publication.txt'; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, augmented_assignment:-=; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:user_base_level; 59, integer:1; 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:stdout; 66, identifier:write; 67, argument_list; 67, 68; 68, string:'---------------------- \n'; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 76; 71, attribute; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:self; 74, identifier:stdout; 75, identifier:write; 76, argument_list; 76, 77; 77, string:'Starting folderreader! \n'; 78, expression_statement; 78, 79; 79, call; 79, 80; 79, 85; 80, attribute; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:self; 83, identifier:stdout; 84, identifier:write; 85, argument_list; 85, 86; 86, string:'---------------------- \n'; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:found_reaction; 90, False; 91, for_statement; 91, 92; 91, 96; 91, 104; 92, pattern_list; 92, 93; 92, 94; 92, 95; 93, identifier:root; 94, identifier:dirs; 95, identifier:files; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:os; 99, identifier:walk; 100, argument_list; 100, 101; 101, attribute; 101, 102; 101, 103; 102, identifier:self; 103, identifier:user_base; 104, block; 104, 105; 104, 124; 104, 140; 104, 154; 104, 174; 104, 208; 104, 267; 104, 281; 104, 327; 105, for_statement; 105, 106; 105, 107; 105, 110; 105, 111; 106, identifier:omit_folder; 107, attribute; 107, 108; 107, 109; 108, identifier:self; 109, identifier:omit_folders; 110, comment; 111, block; 111, 112; 112, if_statement; 112, 113; 112, 116; 113, comparison_operator:in; 113, 114; 113, 115; 114, identifier:omit_folder; 115, identifier:dirs; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:dirs; 121, identifier:remove; 122, argument_list; 122, 123; 123, identifier:omit_folder; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:level; 127, binary_operator:-; 127, 128; 127, 137; 128, call; 128, 129; 128, 130; 129, identifier:len; 130, argument_list; 130, 131; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:root; 134, identifier:split; 135, argument_list; 135, 136; 136, string:"/"; 137, attribute; 137, 138; 137, 139; 138, identifier:self; 139, identifier:user_base_level; 140, if_statement; 140, 141; 140, 146; 141, comparison_operator:==; 141, 142; 141, 143; 142, identifier:level; 143, attribute; 143, 144; 143, 145; 144, identifier:self; 145, identifier:pub_level; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:self; 151, identifier:read_pub; 152, argument_list; 152, 153; 153, identifier:root; 154, if_statement; 154, 155; 154, 160; 155, comparison_operator:==; 155, 156; 155, 157; 156, identifier:level; 157, attribute; 157, 158; 157, 159; 158, identifier:self; 159, identifier:DFT_level; 160, block; 160, 161; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:self; 165, identifier:DFT_code; 166, call; 166, 167; 166, 172; 167, attribute; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:os; 170, identifier:path; 171, identifier:basename; 172, argument_list; 172, 173; 173, identifier:root; 174, if_statement; 174, 175; 174, 180; 175, comparison_operator:==; 175, 176; 175, 177; 176, identifier:level; 177, attribute; 177, 178; 177, 179; 178, identifier:self; 179, identifier:XC_level; 180, block; 180, 181; 180, 194; 180, 202; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:self; 185, identifier:DFT_functional; 186, call; 186, 187; 186, 192; 187, attribute; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:os; 190, identifier:path; 191, identifier:basename; 192, argument_list; 192, 193; 193, identifier:root; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:self; 198, identifier:gas_folder; 199, binary_operator:+; 199, 200; 199, 201; 200, identifier:root; 201, string:'/gas/'; 202, expression_statement; 202, 203; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:self; 206, identifier:read_gas; 207, argument_list; 208, if_statement; 208, 209; 208, 214; 209, comparison_operator:==; 209, 210; 209, 211; 210, identifier:level; 211, attribute; 211, 212; 211, 213; 212, identifier:self; 213, identifier:reference_level; 214, block; 214, 215; 214, 228; 214, 260; 215, if_statement; 215, 216; 215, 226; 216, comparison_operator:in; 216, 217; 216, 218; 217, string:'gas'; 218, call; 218, 219; 218, 224; 219, attribute; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:os; 222, identifier:path; 223, identifier:basename; 224, argument_list; 224, 225; 225, identifier:root; 226, block; 226, 227; 227, continue_statement; 228, if_statement; 228, 229; 228, 232; 229, comparison_operator:is; 229, 230; 229, 231; 230, identifier:goto_metal; 231, None; 232, block; 232, 233; 233, if_statement; 233, 234; 233, 244; 233, 249; 234, comparison_operator:==; 234, 235; 234, 243; 235, call; 235, 236; 235, 241; 236, attribute; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:os; 239, identifier:path; 240, identifier:basename; 241, argument_list; 241, 242; 242, identifier:root; 243, identifier:goto_metal; 244, block; 244, 245; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 248; 247, identifier:goto_metal; 248, None; 249, else_clause; 249, 250; 250, block; 250, 251; 250, 258; 250, 259; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 257; 253, subscript; 253, 254; 253, 255; 254, identifier:dirs; 255, slice; 255, 256; 256, colon; 257, list:[]; 258, comment; 259, continue_statement; 260, expression_statement; 260, 261; 261, call; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:self; 264, identifier:read_bulk; 265, argument_list; 265, 266; 266, identifier:root; 267, if_statement; 267, 268; 267, 273; 268, comparison_operator:==; 268, 269; 268, 270; 269, identifier:level; 270, attribute; 270, 271; 270, 272; 271, identifier:self; 272, identifier:slab_level; 273, block; 273, 274; 274, expression_statement; 274, 275; 275, call; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:self; 278, identifier:read_slab; 279, argument_list; 279, 280; 280, identifier:root; 281, if_statement; 281, 282; 281, 287; 282, comparison_operator:==; 282, 283; 282, 284; 283, identifier:level; 284, attribute; 284, 285; 284, 286; 285, identifier:self; 286, identifier:reaction_level; 287, block; 287, 288; 287, 320; 288, if_statement; 288, 289; 288, 292; 289, comparison_operator:is; 289, 290; 289, 291; 290, identifier:goto_reaction; 291, None; 292, block; 292, 293; 293, if_statement; 293, 294; 293, 304; 293, 309; 294, comparison_operator:==; 294, 295; 294, 303; 295, call; 295, 296; 295, 301; 296, attribute; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:os; 299, identifier:path; 300, identifier:basename; 301, argument_list; 301, 302; 302, identifier:root; 303, identifier:goto_reaction; 304, block; 304, 305; 305, expression_statement; 305, 306; 306, assignment; 306, 307; 306, 308; 307, identifier:goto_reaction; 308, None; 309, else_clause; 309, 310; 310, block; 310, 311; 310, 318; 310, 319; 311, expression_statement; 311, 312; 312, assignment; 312, 313; 312, 317; 313, subscript; 313, 314; 313, 315; 314, identifier:dirs; 315, slice; 315, 316; 316, colon; 317, list:[]; 318, comment; 319, continue_statement; 320, expression_statement; 320, 321; 321, call; 321, 322; 321, 325; 322, attribute; 322, 323; 322, 324; 323, identifier:self; 324, identifier:read_reaction; 325, argument_list; 325, 326; 326, identifier:root; 327, if_statement; 327, 328; 327, 333; 328, comparison_operator:==; 328, 329; 328, 330; 329, identifier:level; 330, attribute; 330, 331; 330, 332; 331, identifier:self; 332, identifier:final_level; 333, block; 333, 334; 333, 340; 333, 347; 334, expression_statement; 334, 335; 335, assignment; 335, 336; 335, 339; 336, attribute; 336, 337; 336, 338; 337, identifier:self; 338, identifier:root; 339, identifier:root; 340, expression_statement; 340, 341; 341, call; 341, 342; 341, 345; 342, attribute; 342, 343; 342, 344; 343, identifier:self; 344, identifier:read_energies; 345, argument_list; 345, 346; 346, identifier:root; 347, if_statement; 347, 348; 347, 353; 348, comparison_operator:is; 348, 349; 348, 352; 349, attribute; 349, 350; 349, 351; 350, identifier:self; 351, identifier:key_value_pairs_reaction; 352, None; 353, block; 353, 354; 354, expression_statement; 354, 355; 355, yield; 355, 356; 356, attribute; 356, 357; 356, 358; 357, identifier:self; 358, identifier:key_value_pairs_reaction | def read(self, skip=[], goto_metal=None, goto_reaction=None):
"""
Get reactions from folders.
Parameters
----------
skip: list of str
list of folders not to read
goto_reaction: str
Skip ahead to this metal
goto_reaction:
Skip ahead to this reacion
"""
if len(skip) > 0:
for skip_f in skip:
self.omit_folders.append(skip_f)
""" If publication level is input"""
if os.path.isfile(self.data_base + '/publication.txt'):
self.user_base_level -= 1
self.stdout.write('---------------------- \n')
self.stdout.write('Starting folderreader! \n')
self.stdout.write('---------------------- \n')
found_reaction = False
for root, dirs, files in os.walk(self.user_base):
for omit_folder in self.omit_folders: # user specified omit_folder
if omit_folder in dirs:
dirs.remove(omit_folder)
level = len(root.split("/")) - self.user_base_level
if level == self.pub_level:
self.read_pub(root)
if level == self.DFT_level:
self.DFT_code = os.path.basename(root)
if level == self.XC_level:
self.DFT_functional = os.path.basename(root)
self.gas_folder = root + '/gas/'
self.read_gas()
if level == self.reference_level:
if 'gas' in os.path.basename(root):
continue
if goto_metal is not None:
if os.path.basename(root) == goto_metal:
goto_metal = None
else:
dirs[:] = [] # don't read any sub_dirs
continue
self.read_bulk(root)
if level == self.slab_level:
self.read_slab(root)
if level == self.reaction_level:
if goto_reaction is not None:
if os.path.basename(root) == goto_reaction:
goto_reaction = None
else:
dirs[:] = [] # don't read any sub_dirs
continue
self.read_reaction(root)
if level == self.final_level:
self.root = root
self.read_energies(root)
if self.key_value_pairs_reaction is not None:
yield self.key_value_pairs_reaction |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:location; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:wave_field; 6, default_parameter; 6, 7; 6, 8; 7, identifier:depth; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:index; 11, None; 12, block; 12, 13; 12, 15; 12, 29; 12, 126; 13, expression_statement; 13, 14; 14, comment; 15, if_statement; 15, 16; 15, 22; 16, not_operator; 16, 17; 17, call; 17, 18; 17, 19; 18, identifier:isinstance; 19, argument_list; 19, 20; 19, 21; 20, identifier:wave_field; 21, identifier:WaveField; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:wave_field; 26, subscript; 26, 27; 26, 28; 27, identifier:WaveField; 28, identifier:wave_field; 29, if_statement; 29, 30; 29, 37; 29, 94; 29, 122; 30, boolean_operator:and; 30, 31; 30, 34; 31, comparison_operator:is; 31, 32; 31, 33; 32, identifier:index; 33, None; 34, comparison_operator:is; 34, 35; 34, 36; 35, identifier:depth; 36, None; 37, block; 37, 38; 38, for_statement; 38, 39; 38, 42; 38, 51; 38, 71; 39, pattern_list; 39, 40; 39, 41; 40, identifier:i; 41, identifier:layer; 42, call; 42, 43; 42, 44; 43, identifier:enumerate; 44, argument_list; 44, 45; 45, subscript; 45, 46; 45, 47; 46, identifier:self; 47, slice; 47, 48; 47, 49; 48, colon; 49, unary_operator:-; 49, 50; 50, integer:1; 51, block; 51, 52; 52, if_statement; 52, 53; 52, 61; 53, comparison_operator:<=; 53, 54; 53, 57; 53, 58; 54, attribute; 54, 55; 54, 56; 55, identifier:layer; 56, identifier:depth; 57, identifier:depth; 58, attribute; 58, 59; 58, 60; 59, identifier:layer; 60, identifier:depth_base; 61, block; 61, 62; 61, 70; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:depth_within; 65, binary_operator:-; 65, 66; 65, 67; 66, identifier:depth; 67, attribute; 67, 68; 67, 69; 68, identifier:layer; 69, identifier:depth; 70, break_statement; 71, else_clause; 71, 72; 71, 73; 72, comment; 73, block; 73, 74; 73, 83; 73, 90; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:i; 77, binary_operator:-; 77, 78; 77, 82; 78, call; 78, 79; 78, 80; 79, identifier:len; 80, argument_list; 80, 81; 81, identifier:self; 82, integer:1; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:layer; 86, subscript; 86, 87; 86, 88; 87, identifier:self; 88, unary_operator:-; 88, 89; 89, integer:1; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:depth_within; 93, integer:0; 94, elif_clause; 94, 95; 94, 102; 95, boolean_operator:and; 95, 96; 95, 99; 96, comparison_operator:is; 96, 97; 96, 98; 97, identifier:index; 98, None; 99, comparison_operator:is; 99, 100; 99, 101; 100, identifier:depth; 101, None; 102, block; 102, 103; 102, 109; 102, 118; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:layer; 106, subscript; 106, 107; 106, 108; 107, identifier:self; 108, identifier:index; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:i; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:self; 115, identifier:index; 116, argument_list; 116, 117; 117, identifier:layer; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:depth_within; 121, integer:0; 122, else_clause; 122, 123; 123, block; 123, 124; 124, raise_statement; 124, 125; 125, identifier:NotImplementedError; 126, return_statement; 126, 127; 127, call; 127, 128; 127, 129; 128, identifier:Location; 129, argument_list; 129, 130; 129, 131; 129, 132; 129, 133; 130, identifier:i; 131, identifier:layer; 132, identifier:wave_field; 133, identifier:depth_within | def location(self, wave_field, depth=None, index=None):
"""Create a Location for a specific depth.
Parameters
----------
wave_field: str
Wave field. See :class:`Location` for possible values.
depth: float, optional
Depth corresponding to the :class`Location` of interest. If
provided, then index is ignored.
index: int, optional
Index corresponding to layer of interest in :class:`Profile`. If
provided, then depth is ignored and location is provided a top
of layer.
Returns
-------
Location
Corresponding :class:`Location` object.
"""
if not isinstance(wave_field, WaveField):
wave_field = WaveField[wave_field]
if index is None and depth is not None:
for i, layer in enumerate(self[:-1]):
if layer.depth <= depth < layer.depth_base:
depth_within = depth - layer.depth
break
else:
# Bedrock
i = len(self) - 1
layer = self[-1]
depth_within = 0
elif index is not None and depth is None:
layer = self[index]
i = self.index(layer)
depth_within = 0
else:
raise NotImplementedError
return Location(i, layer, wave_field, depth_within) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.