sequence
stringlengths
546
16.2k
code
stringlengths
108
19.3k
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:_scan_xgettext_from_files; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 4, identifier:target; 5, identifier:source; 6, identifier:env; 7, default_parameter; 7, 8; 7, 9; 8, identifier:files; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:path; 12, None; 13, block; 13, 14; 13, 16; 13, 19; 13, 23; 13, 28; 13, 35; 13, 51; 13, 73; 13, 89; 13, 100; 13, 104; 13, 168; 13, 169; 13, 184; 13, 185; 13, 197; 13, 209; 13, 218; 13, 332; 14, expression_statement; 14, 15; 15, comment; 16, import_statement; 16, 17; 17, dotted_name; 17, 18; 18, identifier:re; 19, import_statement; 19, 20; 20, dotted_name; 20, 21; 20, 22; 21, identifier:SCons; 22, identifier:Util; 23, import_statement; 23, 24; 24, dotted_name; 24, 25; 24, 26; 24, 27; 25, identifier:SCons; 26, identifier:Node; 27, identifier:FS; 28, if_statement; 28, 29; 28, 32; 29, comparison_operator:is; 29, 30; 29, 31; 30, identifier:files; 31, None; 32, block; 32, 33; 33, return_statement; 33, 34; 34, integer:0; 35, if_statement; 35, 36; 35, 45; 36, not_operator; 36, 37; 37, call; 37, 38; 37, 43; 38, attribute; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:SCons; 41, identifier:Util; 42, identifier:is_List; 43, argument_list; 43, 44; 44, identifier:files; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:files; 49, list:[files]; 49, 50; 50, identifier:files; 51, if_statement; 51, 52; 51, 55; 52, comparison_operator:is; 52, 53; 52, 54; 53, identifier:path; 54, None; 55, block; 55, 56; 56, if_statement; 56, 57; 56, 60; 56, 67; 57, comparison_operator:in; 57, 58; 57, 59; 58, string:'XGETTEXTPATH'; 59, identifier:env; 60, block; 60, 61; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:path; 64, subscript; 64, 65; 64, 66; 65, identifier:env; 66, string:'XGETTEXTPATH'; 67, else_clause; 67, 68; 68, block; 68, 69; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:path; 72, list:[]; 73, if_statement; 73, 74; 73, 83; 74, not_operator; 74, 75; 75, call; 75, 76; 75, 81; 76, attribute; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:SCons; 79, identifier:Util; 80, identifier:is_List; 81, argument_list; 81, 82; 82, identifier:path; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:path; 87, list:[path]; 87, 88; 88, identifier:path; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:path; 92, call; 92, 93; 92, 98; 93, attribute; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:SCons; 96, identifier:Util; 97, identifier:flatten; 98, argument_list; 98, 99; 99, identifier:path; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:dirs; 103, tuple; 104, for_statement; 104, 105; 104, 106; 104, 107; 105, identifier:p; 106, identifier:path; 107, block; 107, 108; 107, 161; 108, if_statement; 108, 109; 108, 121; 109, not_operator; 109, 110; 110, call; 110, 111; 110, 112; 111, identifier:isinstance; 112, argument_list; 112, 113; 112, 114; 113, identifier:p; 114, attribute; 114, 115; 114, 120; 115, attribute; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:SCons; 118, identifier:Node; 119, identifier:FS; 120, identifier:Base; 121, block; 121, 122; 121, 147; 122, if_statement; 122, 123; 122, 131; 123, call; 123, 124; 123, 129; 124, attribute; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:SCons; 127, identifier:Util; 128, identifier:is_String; 129, argument_list; 129, 130; 130, identifier:p; 131, block; 131, 132; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:p; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:env; 138, identifier:subst; 139, argument_list; 139, 140; 139, 141; 139, 144; 140, identifier:p; 141, keyword_argument; 141, 142; 141, 143; 142, identifier:source; 143, identifier:source; 144, keyword_argument; 144, 145; 144, 146; 145, identifier:target; 146, identifier:target; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:p; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:env; 153, identifier:arg2nodes; 154, argument_list; 154, 155; 154, 156; 155, identifier:p; 156, attribute; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:env; 159, identifier:fs; 160, identifier:Dir; 161, expression_statement; 161, 162; 162, augmented_assignment:+=; 162, 163; 162, 164; 163, identifier:dirs; 164, call; 164, 165; 164, 166; 165, identifier:tuple; 166, argument_list; 166, 167; 167, identifier:p; 168, comment; 169, if_statement; 169, 170; 169, 172; 170, not_operator; 170, 171; 171, identifier:dirs; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:dirs; 176, tuple; 176, 177; 177, call; 177, 178; 177, 183; 178, attribute; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:env; 181, identifier:fs; 182, identifier:getcwd; 183, argument_list; 184, comment; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:re_comment; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:re; 191, identifier:compile; 192, argument_list; 192, 193; 192, 194; 193, string:r'^#[^\n\r]*$\r?\n?'; 194, attribute; 194, 195; 194, 196; 195, identifier:re; 196, identifier:M; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:re_emptyln; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:re; 203, identifier:compile; 204, argument_list; 204, 205; 204, 206; 205, string:r'^[ \t\r]*$\r?\n?'; 206, attribute; 206, 207; 206, 208; 207, identifier:re; 208, identifier:M; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:re_trailws; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:re; 215, identifier:compile; 216, argument_list; 216, 217; 217, string:r'[ \t\r]+$'; 218, for_statement; 218, 219; 218, 220; 218, 221; 218, 222; 219, identifier:f; 220, identifier:files; 221, comment; 222, block; 222, 223; 223, if_statement; 223, 224; 223, 241; 224, boolean_operator:and; 224, 225; 224, 236; 225, call; 225, 226; 225, 227; 226, identifier:isinstance; 227, argument_list; 227, 228; 227, 229; 228, identifier:f; 229, attribute; 229, 230; 229, 235; 230, attribute; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:SCons; 233, identifier:Node; 234, identifier:FS; 235, identifier:Base; 236, call; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:f; 239, identifier:rexists; 240, argument_list; 241, block; 241, 242; 241, 250; 241, 260; 241, 270; 241, 280; 241, 288; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 245; 244, identifier:contents; 245, call; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:f; 248, identifier:get_text_contents; 249, argument_list; 250, expression_statement; 250, 251; 251, assignment; 251, 252; 251, 253; 252, identifier:contents; 253, call; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:re_comment; 256, identifier:sub; 257, argument_list; 257, 258; 257, 259; 258, string:""; 259, identifier:contents; 260, expression_statement; 260, 261; 261, assignment; 261, 262; 261, 263; 262, identifier:contents; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:re_emptyln; 266, identifier:sub; 267, argument_list; 267, 268; 267, 269; 268, string:""; 269, identifier:contents; 270, expression_statement; 270, 271; 271, assignment; 271, 272; 271, 273; 272, identifier:contents; 273, call; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:re_trailws; 276, identifier:sub; 277, argument_list; 277, 278; 277, 279; 278, string:""; 279, identifier:contents; 280, expression_statement; 280, 281; 281, assignment; 281, 282; 281, 283; 282, identifier:depnames; 283, call; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:contents; 286, identifier:splitlines; 287, argument_list; 288, for_statement; 288, 289; 288, 290; 288, 291; 289, identifier:depname; 290, identifier:depnames; 291, block; 291, 292; 291, 306; 291, 324; 292, expression_statement; 292, 293; 293, assignment; 293, 294; 293, 295; 294, identifier:depfile; 295, call; 295, 296; 295, 303; 296, attribute; 296, 297; 296, 302; 297, attribute; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:SCons; 300, identifier:Node; 301, identifier:FS; 302, identifier:find_file; 303, argument_list; 303, 304; 303, 305; 304, identifier:depname; 305, identifier:dirs; 306, if_statement; 306, 307; 306, 309; 307, not_operator; 307, 308; 308, identifier:depfile; 309, block; 309, 310; 310, expression_statement; 310, 311; 311, assignment; 311, 312; 311, 313; 312, identifier:depfile; 313, call; 313, 314; 313, 317; 314, attribute; 314, 315; 314, 316; 315, identifier:env; 316, identifier:arg2nodes; 317, argument_list; 317, 318; 317, 319; 318, identifier:depname; 319, attribute; 319, 320; 319, 323; 320, subscript; 320, 321; 320, 322; 321, identifier:dirs; 322, integer:0; 323, identifier:File; 324, expression_statement; 324, 325; 325, call; 325, 326; 325, 329; 326, attribute; 326, 327; 326, 328; 327, identifier:env; 328, identifier:Depends; 329, argument_list; 329, 330; 329, 331; 330, identifier:target; 331, identifier:depfile; 332, return_statement; 332, 333; 333, integer:0
def _scan_xgettext_from_files(target, source, env, files=None, path=None): """ Parses `POTFILES.in`-like file and returns list of extracted file names. """ import re import SCons.Util import SCons.Node.FS if files is None: return 0 if not SCons.Util.is_List(files): files = [files] if path is None: if 'XGETTEXTPATH' in env: path = env['XGETTEXTPATH'] else: path = [] if not SCons.Util.is_List(path): path = [path] path = SCons.Util.flatten(path) dirs = () for p in path: if not isinstance(p, SCons.Node.FS.Base): if SCons.Util.is_String(p): p = env.subst(p, source=source, target=target) p = env.arg2nodes(p, env.fs.Dir) dirs += tuple(p) # cwd is the default search path (when no path is defined by user) if not dirs: dirs = (env.fs.getcwd(),) # Parse 'POTFILE.in' files. re_comment = re.compile(r'^#[^\n\r]*$\r?\n?', re.M) re_emptyln = re.compile(r'^[ \t\r]*$\r?\n?', re.M) re_trailws = re.compile(r'[ \t\r]+$') for f in files: # Find files in search path $XGETTEXTPATH if isinstance(f, SCons.Node.FS.Base) and f.rexists(): contents = f.get_text_contents() contents = re_comment.sub("", contents) contents = re_emptyln.sub("", contents) contents = re_trailws.sub("", contents) depnames = contents.splitlines() for depname in depnames: depfile = SCons.Node.FS.find_file(depname, dirs) if not depfile: depfile = env.arg2nodes(depname, dirs[0].File) env.Depends(target, depfile) return 0
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:build_wxsfile_file_section; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 4, identifier:root; 5, identifier:files; 6, identifier:NAME; 7, identifier:VERSION; 8, identifier:VENDOR; 9, identifier:filename_set; 10, identifier:id_set; 11, block; 11, 12; 11, 14; 11, 25; 11, 32; 11, 38; 11, 223; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:root; 17, call; 17, 18; 17, 19; 18, identifier:create_default_directory_layout; 19, argument_list; 19, 20; 19, 21; 19, 22; 19, 23; 19, 24; 20, identifier:root; 21, identifier:NAME; 22, identifier:VERSION; 23, identifier:VENDOR; 24, identifier:filename_set; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:components; 28, call; 28, 29; 28, 30; 29, identifier:create_feature_dict; 30, argument_list; 30, 31; 31, identifier:files; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:factory; 35, call; 35, 36; 35, 37; 36, identifier:Document; 37, argument_list; 38, function_definition; 38, 39; 38, 40; 38, 43; 39, function_name:get_directory; 40, parameters; 40, 41; 40, 42; 41, identifier:node; 42, identifier:dir; 43, block; 43, 44; 43, 46; 43, 56; 43, 60; 43, 73; 43, 74; 43, 75; 43, 79; 43, 80; 43, 92; 43, 153; 43, 221; 44, expression_statement; 44, 45; 45, comment; 46, if_statement; 46, 47; 46, 53; 47, boolean_operator:or; 47, 48; 47, 51; 48, comparison_operator:==; 48, 49; 48, 50; 49, identifier:dir; 50, string:''; 51, not_operator; 51, 52; 52, identifier:dir; 53, block; 53, 54; 54, return_statement; 54, 55; 55, identifier:node; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:Directory; 59, identifier:node; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:dir_parts; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:dir; 66, identifier:split; 67, argument_list; 67, 68; 68, attribute; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:os; 71, identifier:path; 72, identifier:sep; 73, comment; 74, comment; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:upper_dir; 78, string:''; 79, comment; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:dir_parts; 83, list_comprehension; 83, 84; 83, 85; 83, 88; 84, identifier:d; 85, for_in_clause; 85, 86; 85, 87; 86, identifier:d; 87, identifier:dir_parts; 88, if_clause; 88, 89; 89, comparison_operator:!=; 89, 90; 89, 91; 90, identifier:d; 91, string:''; 92, for_statement; 92, 93; 92, 94; 92, 98; 93, identifier:d; 94, subscript; 94, 95; 94, 96; 95, identifier:dir_parts; 96, slice; 96, 97; 97, colon; 98, block; 98, 99; 98, 128; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:already_created; 102, list_comprehension; 102, 103; 102, 104; 102, 109; 103, identifier:c; 104, for_in_clause; 104, 105; 104, 106; 105, identifier:c; 106, attribute; 106, 107; 106, 108; 107, identifier:Directory; 108, identifier:childNodes; 109, if_clause; 109, 110; 110, boolean_operator:and; 110, 111; 110, 116; 111, comparison_operator:==; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:c; 114, identifier:nodeName; 115, string:'Directory'; 116, comparison_operator:==; 116, 117; 116, 124; 117, attribute; 117, 118; 117, 123; 118, subscript; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:c; 121, identifier:attributes; 122, string:'LongName'; 123, identifier:value; 124, call; 124, 125; 124, 126; 125, identifier:escape; 126, argument_list; 126, 127; 127, identifier:d; 128, if_statement; 128, 129; 128, 132; 128, 150; 129, comparison_operator:!=; 129, 130; 129, 131; 130, identifier:already_created; 131, list:[]; 132, block; 132, 133; 132, 139; 132, 146; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:Directory; 136, subscript; 136, 137; 136, 138; 137, identifier:already_created; 138, integer:0; 139, expression_statement; 139, 140; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:dir_parts; 143, identifier:remove; 144, argument_list; 144, 145; 145, identifier:d; 146, expression_statement; 146, 147; 147, augmented_assignment:+=; 147, 148; 147, 149; 148, identifier:upper_dir; 149, identifier:d; 150, else_clause; 150, 151; 151, block; 151, 152; 152, break_statement; 153, for_statement; 153, 154; 153, 155; 153, 156; 154, identifier:d; 155, identifier:dir_parts; 156, block; 156, 157; 156, 166; 156, 177; 156, 192; 156, 196; 156, 208; 156, 217; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:nDirectory; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:factory; 163, identifier:createElement; 164, argument_list; 164, 165; 165, string:'Directory'; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 173; 168, subscript; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:nDirectory; 171, identifier:attributes; 172, string:'LongName'; 173, call; 173, 174; 173, 175; 174, identifier:escape; 175, argument_list; 175, 176; 176, identifier:d; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 184; 179, subscript; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:nDirectory; 182, identifier:attributes; 183, string:'Name'; 184, call; 184, 185; 184, 186; 185, identifier:escape; 186, argument_list; 186, 187; 187, call; 187, 188; 187, 189; 188, identifier:gen_dos_short_file_name; 189, argument_list; 189, 190; 189, 191; 190, identifier:d; 191, identifier:filename_set; 192, expression_statement; 192, 193; 193, augmented_assignment:+=; 193, 194; 193, 195; 194, identifier:upper_dir; 195, identifier:d; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 203; 198, subscript; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:nDirectory; 201, identifier:attributes; 202, string:'Id'; 203, call; 203, 204; 203, 205; 204, identifier:convert_to_id; 205, argument_list; 205, 206; 205, 207; 206, identifier:upper_dir; 207, identifier:id_set; 208, expression_statement; 208, 209; 209, call; 209, 210; 209, 215; 210, attribute; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:Directory; 213, identifier:childNodes; 214, identifier:append; 215, argument_list; 215, 216; 216, identifier:nDirectory; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:Directory; 220, identifier:nDirectory; 221, return_statement; 221, 222; 222, identifier:Directory; 223, for_statement; 223, 224; 223, 225; 223, 226; 224, identifier:file; 225, identifier:files; 226, block; 226, 227; 226, 242; 226, 253; 226, 264; 226, 296; 226, 297; 226, 329; 226, 338; 226, 351; 226, 364; 226, 377; 226, 390; 226, 403; 226, 404; 226, 413; 226, 421; 226, 433; 226, 434; 226, 435; 226, 443; 226, 452; 227, expression_statement; 227, 228; 228, assignment; 228, 229; 228, 232; 229, pattern_list; 229, 230; 229, 231; 230, identifier:drive; 231, identifier:path; 232, call; 232, 233; 232, 238; 233, attribute; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:os; 236, identifier:path; 237, identifier:splitdrive; 238, argument_list; 238, 239; 239, attribute; 239, 240; 239, 241; 240, identifier:file; 241, identifier:PACKAGING_INSTALL_LOCATION; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 245; 244, identifier:filename; 245, call; 245, 246; 245, 251; 246, attribute; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:os; 249, identifier:path; 250, identifier:basename; 251, argument_list; 251, 252; 252, identifier:path; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 256; 255, identifier:dirname; 256, call; 256, 257; 256, 262; 257, attribute; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:os; 260, identifier:path; 261, identifier:dirname; 262, argument_list; 262, 263; 263, identifier:path; 264, expression_statement; 264, 265; 265, assignment; 265, 266; 265, 267; 266, identifier:h; 267, dictionary; 267, 268; 267, 269; 267, 272; 267, 279; 267, 282; 267, 289; 268, comment; 269, pair; 269, 270; 269, 271; 270, string:'PACKAGING_X_MSI_VITAL'; 271, string:'yes'; 272, pair; 272, 273; 272, 274; 273, string:'PACKAGING_X_MSI_FILEID'; 274, call; 274, 275; 274, 276; 275, identifier:convert_to_id; 276, argument_list; 276, 277; 276, 278; 277, identifier:filename; 278, identifier:id_set; 279, pair; 279, 280; 279, 281; 280, string:'PACKAGING_X_MSI_LONGNAME'; 281, identifier:filename; 282, pair; 282, 283; 282, 284; 283, string:'PACKAGING_X_MSI_SHORTNAME'; 284, call; 284, 285; 284, 286; 285, identifier:gen_dos_short_file_name; 286, argument_list; 286, 287; 286, 288; 287, identifier:filename; 288, identifier:filename_set; 289, pair; 289, 290; 289, 291; 290, string:'PACKAGING_X_MSI_SOURCE'; 291, call; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, identifier:file; 294, identifier:get_path; 295, argument_list; 296, comment; 297, for_statement; 297, 298; 297, 301; 297, 321; 298, pattern_list; 298, 299; 298, 300; 299, identifier:k; 300, identifier:v; 301, list_comprehension; 301, 302; 301, 305; 301, 314; 302, tuple; 302, 303; 302, 304; 303, identifier:k; 304, identifier:v; 305, for_in_clause; 305, 306; 305, 309; 306, tuple_pattern; 306, 307; 306, 308; 307, identifier:k; 308, identifier:v; 309, call; 309, 310; 309, 313; 310, attribute; 310, 311; 310, 312; 311, identifier:h; 312, identifier:items; 313, argument_list; 314, if_clause; 314, 315; 315, not_operator; 315, 316; 316, call; 316, 317; 316, 318; 317, identifier:hasattr; 318, argument_list; 318, 319; 318, 320; 319, identifier:file; 320, identifier:k; 321, block; 321, 322; 322, expression_statement; 322, 323; 323, call; 323, 324; 323, 325; 324, identifier:setattr; 325, argument_list; 325, 326; 325, 327; 325, 328; 326, identifier:file; 327, identifier:k; 328, identifier:v; 329, expression_statement; 329, 330; 330, assignment; 330, 331; 330, 332; 331, identifier:File; 332, call; 332, 333; 332, 336; 333, attribute; 333, 334; 333, 335; 334, identifier:factory; 335, identifier:createElement; 336, argument_list; 336, 337; 337, string:'File'; 338, expression_statement; 338, 339; 339, assignment; 339, 340; 339, 345; 340, subscript; 340, 341; 340, 344; 341, attribute; 341, 342; 341, 343; 342, identifier:File; 343, identifier:attributes; 344, string:'LongName'; 345, call; 345, 346; 345, 347; 346, identifier:escape; 347, argument_list; 347, 348; 348, attribute; 348, 349; 348, 350; 349, identifier:file; 350, identifier:PACKAGING_X_MSI_LONGNAME; 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:File; 356, identifier:attributes; 357, string:'Name'; 358, call; 358, 359; 358, 360; 359, identifier:escape; 360, argument_list; 360, 361; 361, attribute; 361, 362; 361, 363; 362, identifier:file; 363, identifier:PACKAGING_X_MSI_SHORTNAME; 364, expression_statement; 364, 365; 365, assignment; 365, 366; 365, 371; 366, subscript; 366, 367; 366, 370; 367, attribute; 367, 368; 367, 369; 368, identifier:File; 369, identifier:attributes; 370, string:'Source'; 371, call; 371, 372; 371, 373; 372, identifier:escape; 373, argument_list; 373, 374; 374, attribute; 374, 375; 374, 376; 375, identifier:file; 376, identifier:PACKAGING_X_MSI_SOURCE; 377, expression_statement; 377, 378; 378, assignment; 378, 379; 378, 384; 379, subscript; 379, 380; 379, 383; 380, attribute; 380, 381; 380, 382; 381, identifier:File; 382, identifier:attributes; 383, string:'Id'; 384, call; 384, 385; 384, 386; 385, identifier:escape; 386, argument_list; 386, 387; 387, attribute; 387, 388; 387, 389; 388, identifier:file; 389, identifier:PACKAGING_X_MSI_FILEID; 390, expression_statement; 390, 391; 391, assignment; 391, 392; 391, 397; 392, subscript; 392, 393; 392, 396; 393, attribute; 393, 394; 393, 395; 394, identifier:File; 395, identifier:attributes; 396, string:'Vital'; 397, call; 397, 398; 397, 399; 398, identifier:escape; 399, argument_list; 399, 400; 400, attribute; 400, 401; 400, 402; 401, identifier:file; 402, identifier:PACKAGING_X_MSI_VITAL; 403, comment; 404, expression_statement; 404, 405; 405, assignment; 405, 406; 405, 407; 406, identifier:Component; 407, call; 407, 408; 407, 411; 408, attribute; 408, 409; 408, 410; 409, identifier:factory; 410, identifier:createElement; 411, argument_list; 411, 412; 412, string:'Component'; 413, expression_statement; 413, 414; 414, assignment; 414, 415; 414, 420; 415, subscript; 415, 416; 415, 419; 416, attribute; 416, 417; 416, 418; 417, identifier:Component; 418, identifier:attributes; 419, string:'DiskId'; 420, string:'1'; 421, expression_statement; 421, 422; 422, assignment; 422, 423; 422, 428; 423, subscript; 423, 424; 423, 427; 424, attribute; 424, 425; 424, 426; 425, identifier:Component; 426, identifier:attributes; 427, string:'Id'; 428, call; 428, 429; 428, 430; 429, identifier:convert_to_id; 430, argument_list; 430, 431; 430, 432; 431, identifier:filename; 432, identifier:id_set; 433, comment; 434, comment; 435, expression_statement; 435, 436; 436, assignment; 436, 437; 436, 438; 437, identifier:Directory; 438, call; 438, 439; 438, 440; 439, identifier:get_directory; 440, argument_list; 440, 441; 440, 442; 441, identifier:root; 442, identifier:dirname; 443, expression_statement; 443, 444; 444, call; 444, 445; 444, 450; 445, attribute; 445, 446; 445, 449; 446, attribute; 446, 447; 446, 448; 447, identifier:Directory; 448, identifier:childNodes; 449, identifier:append; 450, argument_list; 450, 451; 451, identifier:Component; 452, expression_statement; 452, 453; 453, call; 453, 454; 453, 459; 454, attribute; 454, 455; 454, 458; 455, attribute; 455, 456; 455, 457; 456, identifier:Component; 457, identifier:childNodes; 458, identifier:append; 459, argument_list; 459, 460; 460, identifier:File
def build_wxsfile_file_section(root, files, NAME, VERSION, VENDOR, filename_set, id_set): """ Builds the Component sections of the wxs file with their included files. Files need to be specified in 8.3 format and in the long name format, long filenames will be converted automatically. Features are specficied with the 'X_MSI_FEATURE' or 'DOC' FileTag. """ root = create_default_directory_layout( root, NAME, VERSION, VENDOR, filename_set ) components = create_feature_dict( files ) factory = Document() def get_directory( node, dir ): """ Returns the node under the given node representing the directory. Returns the component node if dir is None or empty. """ if dir == '' or not dir: return node Directory = node dir_parts = dir.split(os.path.sep) # to make sure that our directory ids are unique, the parent folders are # consecutively added to upper_dir upper_dir = '' # walk down the xml tree finding parts of the directory dir_parts = [d for d in dir_parts if d != ''] for d in dir_parts[:]: already_created = [c for c in Directory.childNodes if c.nodeName == 'Directory' and c.attributes['LongName'].value == escape(d)] if already_created != []: Directory = already_created[0] dir_parts.remove(d) upper_dir += d else: break for d in dir_parts: nDirectory = factory.createElement( 'Directory' ) nDirectory.attributes['LongName'] = escape( d ) nDirectory.attributes['Name'] = escape( gen_dos_short_file_name( d, filename_set ) ) upper_dir += d nDirectory.attributes['Id'] = convert_to_id( upper_dir, id_set ) Directory.childNodes.append( nDirectory ) Directory = nDirectory return Directory for file in files: drive, path = os.path.splitdrive( file.PACKAGING_INSTALL_LOCATION ) filename = os.path.basename( path ) dirname = os.path.dirname( path ) h = { # tagname : default value 'PACKAGING_X_MSI_VITAL' : 'yes', 'PACKAGING_X_MSI_FILEID' : convert_to_id(filename, id_set), 'PACKAGING_X_MSI_LONGNAME' : filename, 'PACKAGING_X_MSI_SHORTNAME' : gen_dos_short_file_name(filename, filename_set), 'PACKAGING_X_MSI_SOURCE' : file.get_path(), } # fill in the default tags given above. for k,v in [ (k, v) for (k,v) in h.items() if not hasattr(file, k) ]: setattr( file, k, v ) File = factory.createElement( 'File' ) File.attributes['LongName'] = escape( file.PACKAGING_X_MSI_LONGNAME ) File.attributes['Name'] = escape( file.PACKAGING_X_MSI_SHORTNAME ) File.attributes['Source'] = escape( file.PACKAGING_X_MSI_SOURCE ) File.attributes['Id'] = escape( file.PACKAGING_X_MSI_FILEID ) File.attributes['Vital'] = escape( file.PACKAGING_X_MSI_VITAL ) # create the <Component> Tag under which this file should appear Component = factory.createElement('Component') Component.attributes['DiskId'] = '1' Component.attributes['Id'] = convert_to_id( filename, id_set ) # hang the component node under the root node and the file node # under the component node. Directory = get_directory( root, dirname ) Directory.childNodes.append( Component ) Component.childNodes.append( File )
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 25; 2, function_name:FromReadings; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 13; 3, 16; 3, 19; 3, 22; 4, identifier:cls; 5, identifier:uuid; 6, identifier:readings; 7, identifier:events; 8, default_parameter; 8, 9; 8, 10; 9, identifier:report_id; 10, attribute; 10, 11; 10, 12; 11, identifier:IOTileReading; 12, identifier:InvalidReadingID; 13, default_parameter; 13, 14; 13, 15; 14, identifier:selector; 15, integer:0xFFFF; 16, default_parameter; 16, 17; 16, 18; 17, identifier:streamer; 18, integer:0x100; 19, default_parameter; 19, 20; 19, 21; 20, identifier:sent_timestamp; 21, integer:0; 22, default_parameter; 22, 23; 22, 24; 23, identifier:received_time; 24, None; 25, block; 25, 26; 25, 28; 25, 34; 25, 40; 25, 104; 25, 116; 25, 128; 25, 164; 25, 179; 26, expression_statement; 26, 27; 27, comment; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:lowest_id; 31, attribute; 31, 32; 31, 33; 32, identifier:IOTileReading; 33, identifier:InvalidReadingID; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:highest_id; 37, attribute; 37, 38; 37, 39; 38, identifier:IOTileReading; 39, identifier:InvalidReadingID; 40, for_statement; 40, 41; 40, 42; 40, 55; 41, identifier:item; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:itertools; 45, identifier:chain; 46, argument_list; 46, 47; 46, 51; 47, call; 47, 48; 47, 49; 48, identifier:iter; 49, argument_list; 49, 50; 50, identifier:readings; 51, call; 51, 52; 51, 53; 52, identifier:iter; 53, argument_list; 53, 54; 54, identifier:events; 55, block; 55, 56; 55, 66; 55, 85; 56, if_statement; 56, 57; 56, 64; 57, comparison_operator:==; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:item; 60, identifier:reading_id; 61, attribute; 61, 62; 61, 63; 62, identifier:IOTileReading; 63, identifier:InvalidReadingID; 64, block; 64, 65; 65, continue_statement; 66, if_statement; 66, 67; 66, 78; 67, boolean_operator:or; 67, 68; 67, 73; 68, comparison_operator:==; 68, 69; 68, 70; 69, identifier:lowest_id; 70, attribute; 70, 71; 70, 72; 71, identifier:IOTileReading; 72, identifier:InvalidReadingID; 73, comparison_operator:<; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:item; 76, identifier:reading_id; 77, identifier:lowest_id; 78, block; 78, 79; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:lowest_id; 82, attribute; 82, 83; 82, 84; 83, identifier:item; 84, identifier:reading_id; 85, if_statement; 85, 86; 85, 97; 86, boolean_operator:or; 86, 87; 86, 92; 87, comparison_operator:==; 87, 88; 87, 89; 88, identifier:highest_id; 89, attribute; 89, 90; 89, 91; 90, identifier:IOTileReading; 91, identifier:InvalidReadingID; 92, comparison_operator:>; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:item; 95, identifier:reading_id; 96, identifier:highest_id; 97, block; 97, 98; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:highest_id; 101, attribute; 101, 102; 101, 103; 102, identifier:item; 103, identifier:reading_id; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:reading_list; 107, list_comprehension; 107, 108; 107, 113; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:x; 111, identifier:asdict; 112, argument_list; 113, for_in_clause; 113, 114; 113, 115; 114, identifier:x; 115, identifier:readings; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:event_list; 119, list_comprehension; 119, 120; 119, 125; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:x; 123, identifier:asdict; 124, argument_list; 125, for_in_clause; 125, 126; 125, 127; 126, identifier:x; 127, identifier:events; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:report_dict; 131, dictionary; 131, 132; 131, 137; 131, 140; 131, 143; 131, 146; 131, 149; 131, 152; 131, 155; 131, 158; 131, 161; 132, pair; 132, 133; 132, 134; 133, string:"format"; 134, attribute; 134, 135; 134, 136; 135, identifier:cls; 136, identifier:FORMAT_TAG; 137, pair; 137, 138; 137, 139; 138, string:"device"; 139, identifier:uuid; 140, pair; 140, 141; 140, 142; 141, string:"streamer_index"; 142, identifier:streamer; 143, pair; 143, 144; 143, 145; 144, string:"streamer_selector"; 145, identifier:selector; 146, pair; 146, 147; 146, 148; 147, string:"incremental_id"; 148, identifier:report_id; 149, pair; 149, 150; 149, 151; 150, string:"lowest_id"; 151, identifier:lowest_id; 152, pair; 152, 153; 152, 154; 153, string:"highest_id"; 154, identifier:highest_id; 155, pair; 155, 156; 155, 157; 156, string:"device_sent_timestamp"; 157, identifier:sent_timestamp; 158, pair; 158, 159; 158, 160; 159, string:"events"; 160, identifier:event_list; 161, pair; 161, 162; 161, 163; 162, string:"data"; 163, identifier:reading_list; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:encoded; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:msgpack; 170, identifier:packb; 171, argument_list; 171, 172; 171, 173; 171, 176; 172, identifier:report_dict; 173, keyword_argument; 173, 174; 173, 175; 174, identifier:default; 175, identifier:_encode_datetime; 176, keyword_argument; 176, 177; 176, 178; 177, identifier:use_bin_type; 178, True; 179, return_statement; 179, 180; 180, call; 180, 181; 180, 182; 181, identifier:FlexibleDictionaryReport; 182, argument_list; 182, 183; 182, 184; 182, 187; 182, 190; 183, identifier:encoded; 184, keyword_argument; 184, 185; 184, 186; 185, identifier:signed; 186, False; 187, keyword_argument; 187, 188; 187, 189; 188, identifier:encrypted; 189, False; 190, keyword_argument; 190, 191; 190, 192; 191, identifier:received_time; 192, identifier:received_time
def FromReadings(cls, uuid, readings, events, report_id=IOTileReading.InvalidReadingID, selector=0xFFFF, streamer=0x100, sent_timestamp=0, received_time=None): """Create a flexible dictionary report from a list of readings and events. Args: uuid (int): The uuid of the device that this report came from readings (list of IOTileReading): A list of IOTileReading objects containing the data in the report events (list of IOTileEvent): A list of the events contained in the report. report_id (int): The id of the report. If not provided it defaults to IOTileReading.InvalidReadingID. Note that you can specify anything you want for the report id but for actual IOTile devices the report id will always be greater than the id of all of the readings contained in the report since devices generate ids sequentially. selector (int): The streamer selector of this report. This can be anything but if the report came from a device, it would correspond with the query the device used to pick readings to go into the report. streamer (int): The streamer id that this reading was sent from. sent_timestamp (int): The device's uptime that sent this report. received_time(datetime): The UTC time when this report was received from an IOTile device. If it is being created now, received_time defaults to datetime.utcnow(). Returns: FlexibleDictionaryReport: A report containing the readings and events passed in. """ lowest_id = IOTileReading.InvalidReadingID highest_id = IOTileReading.InvalidReadingID for item in itertools.chain(iter(readings), iter(events)): if item.reading_id == IOTileReading.InvalidReadingID: continue if lowest_id == IOTileReading.InvalidReadingID or item.reading_id < lowest_id: lowest_id = item.reading_id if highest_id == IOTileReading.InvalidReadingID or item.reading_id > highest_id: highest_id = item.reading_id reading_list = [x.asdict() for x in readings] event_list = [x.asdict() for x in events] report_dict = { "format": cls.FORMAT_TAG, "device": uuid, "streamer_index": streamer, "streamer_selector": selector, "incremental_id": report_id, "lowest_id": lowest_id, "highest_id": highest_id, "device_sent_timestamp": sent_timestamp, "events": event_list, "data": reading_list } encoded = msgpack.packb(report_dict, default=_encode_datetime, use_bin_type=True) return FlexibleDictionaryReport(encoded, signed=False, encrypted=False, received_time=received_time)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_object_instance_content; 3, parameters; 3, 4; 4, identifier:obj; 5, block; 5, 6; 5, 8; 5, 14; 5, 21; 5, 41; 5, 47; 5, 59; 5, 71; 5, 90; 5, 91; 5, 141; 5, 147; 5, 168; 5, 176; 5, 198; 5, 204; 5, 208; 5, 232; 5, 239; 5, 246; 5, 253; 5, 260; 5, 267; 5, 274; 5, 281; 5, 296; 5, 303; 5, 310; 5, 317; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:retval; 11, call; 11, 12; 11, 13; 12, identifier:bytearray; 13, argument_list; 14, if_statement; 14, 15; 14, 18; 15, comparison_operator:is; 15, 16; 15, 17; 16, identifier:obj; 17, None; 18, block; 18, 19; 19, return_statement; 19, 20; 20, string:b'N.'; 21, if_statement; 21, 22; 21, 31; 22, call; 22, 23; 22, 24; 23, identifier:isinstance; 24, argument_list; 24, 25; 24, 26; 25, identifier:obj; 26, attribute; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:SCons; 29, identifier:Util; 30, identifier:BaseStringTypes; 31, block; 31, 32; 32, return_statement; 32, 33; 33, call; 33, 34; 33, 39; 34, attribute; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:SCons; 37, identifier:Util; 38, identifier:to_bytes; 39, argument_list; 39, 40; 40, identifier:obj; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:inst_class; 44, attribute; 44, 45; 44, 46; 45, identifier:obj; 46, identifier:__class__; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:inst_class_name; 50, call; 50, 51; 50, 52; 51, identifier:bytearray; 52, argument_list; 52, 53; 52, 58; 53, attribute; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:obj; 56, identifier:__class__; 57, identifier:__name__; 58, string:'utf-8'; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:inst_class_module; 62, call; 62, 63; 62, 64; 63, identifier:bytearray; 64, argument_list; 64, 65; 64, 70; 65, attribute; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:obj; 68, identifier:__class__; 69, identifier:__module__; 70, string:'utf-8'; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:inst_class_hierarchy; 74, call; 74, 75; 74, 76; 75, identifier:bytearray; 76, argument_list; 76, 77; 76, 89; 77, call; 77, 78; 77, 79; 78, identifier:repr; 79, argument_list; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:inspect; 83, identifier:getclasstree; 84, argument_list; 84, 85; 85, list:[obj.__class__,]; 85, 86; 86, attribute; 86, 87; 86, 88; 87, identifier:obj; 88, identifier:__class__; 89, string:'utf-8'; 90, comment; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:properties; 94, list_comprehension; 94, 95; 94, 103; 94, 109; 95, tuple; 95, 96; 95, 97; 96, identifier:p; 97, call; 97, 98; 97, 99; 98, identifier:getattr; 99, argument_list; 99, 100; 99, 101; 99, 102; 100, identifier:obj; 101, identifier:p; 102, string:"None"; 103, for_in_clause; 103, 104; 103, 105; 104, identifier:p; 105, call; 105, 106; 105, 107; 106, identifier:dir; 107, argument_list; 107, 108; 108, identifier:obj; 109, if_clause; 109, 110; 110, not_operator; 110, 111; 111, parenthesized_expression; 111, 112; 112, boolean_operator:or; 112, 113; 112, 131; 113, boolean_operator:or; 113, 114; 113, 121; 114, comparison_operator:==; 114, 115; 114, 120; 115, subscript; 115, 116; 115, 117; 116, identifier:p; 117, slice; 117, 118; 117, 119; 118, colon; 119, integer:2; 120, string:'__'; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:inspect; 124, identifier:ismethod; 125, argument_list; 125, 126; 126, call; 126, 127; 126, 128; 127, identifier:getattr; 128, argument_list; 128, 129; 128, 130; 129, identifier:obj; 130, identifier:p; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:inspect; 134, identifier:isbuiltin; 135, argument_list; 135, 136; 136, call; 136, 137; 136, 138; 137, identifier:getattr; 138, argument_list; 138, 139; 138, 140; 139, identifier:obj; 140, identifier:p; 141, expression_statement; 141, 142; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:properties; 145, identifier:sort; 146, argument_list; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:properties_str; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, string:','; 153, identifier:join; 154, argument_list; 154, 155; 155, list_comprehension; 155, 156; 155, 165; 156, binary_operator:%; 156, 157; 156, 158; 157, string:"%s=%s"; 158, tuple; 158, 159; 158, 162; 159, subscript; 159, 160; 159, 161; 160, identifier:p; 161, integer:0; 162, subscript; 162, 163; 162, 164; 163, identifier:p; 164, integer:1; 165, for_in_clause; 165, 166; 165, 167; 166, identifier:p; 167, identifier:properties; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:properties_bytes; 171, call; 171, 172; 171, 173; 172, identifier:bytearray; 173, argument_list; 173, 174; 173, 175; 174, identifier:properties_str; 175, string:'utf-8'; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:methods; 179, list_comprehension; 179, 180; 179, 181; 179, 187; 180, identifier:p; 181, for_in_clause; 181, 182; 181, 183; 182, identifier:p; 183, call; 183, 184; 183, 185; 184, identifier:dir; 185, argument_list; 185, 186; 186, identifier:obj; 187, if_clause; 187, 188; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:inspect; 191, identifier:ismethod; 192, argument_list; 192, 193; 193, call; 193, 194; 193, 195; 194, identifier:getattr; 195, argument_list; 195, 196; 195, 197; 196, identifier:obj; 197, identifier:p; 198, expression_statement; 198, 199; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:methods; 202, identifier:sort; 203, argument_list; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:method_contents; 207, list:[]; 208, for_statement; 208, 209; 208, 210; 208, 211; 208, 212; 209, identifier:m; 210, identifier:methods; 211, comment; 212, block; 212, 213; 212, 224; 212, 225; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 216; 215, identifier:v; 216, call; 216, 217; 216, 218; 217, identifier:_function_contents; 218, argument_list; 218, 219; 219, call; 219, 220; 219, 221; 220, identifier:getattr; 221, argument_list; 221, 222; 221, 223; 222, identifier:obj; 223, identifier:m; 224, comment; 225, expression_statement; 225, 226; 226, call; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:method_contents; 229, identifier:append; 230, argument_list; 230, 231; 231, identifier:v; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 235; 234, identifier:retval; 235, call; 235, 236; 235, 237; 236, identifier:bytearray; 237, argument_list; 237, 238; 238, string:b'{'; 239, expression_statement; 239, 240; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:retval; 243, identifier:extend; 244, argument_list; 244, 245; 245, identifier:inst_class_name; 246, expression_statement; 246, 247; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:retval; 250, identifier:extend; 251, argument_list; 251, 252; 252, string:b":"; 253, expression_statement; 253, 254; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:retval; 257, identifier:extend; 258, argument_list; 258, 259; 259, identifier:inst_class_module; 260, expression_statement; 260, 261; 261, call; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:retval; 264, identifier:extend; 265, argument_list; 265, 266; 266, string:b'}[['; 267, expression_statement; 267, 268; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:retval; 271, identifier:extend; 272, argument_list; 272, 273; 273, identifier:inst_class_hierarchy; 274, expression_statement; 274, 275; 275, call; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:retval; 278, identifier:extend; 279, argument_list; 279, 280; 280, string:b']]{{'; 281, expression_statement; 281, 282; 282, call; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, identifier:retval; 285, identifier:extend; 286, argument_list; 286, 287; 287, call; 287, 288; 287, 294; 288, attribute; 288, 289; 288, 293; 289, call; 289, 290; 289, 291; 290, identifier:bytearray; 291, argument_list; 291, 292; 292, string:b","; 293, identifier:join; 294, argument_list; 294, 295; 295, identifier:method_contents; 296, expression_statement; 296, 297; 297, call; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:retval; 300, identifier:extend; 301, argument_list; 301, 302; 302, string:b"}}{{{"; 303, expression_statement; 303, 304; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:retval; 307, identifier:extend; 308, argument_list; 308, 309; 309, identifier:properties_bytes; 310, expression_statement; 310, 311; 311, call; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:retval; 314, identifier:extend; 315, argument_list; 315, 316; 316, string:b'}}}'; 317, return_statement; 317, 318; 318, identifier:retval
def _object_instance_content(obj): """ Returns consistant content for a action class or an instance thereof :Parameters: - `obj` Should be either and action class or an instance thereof :Returns: bytearray or bytes representing the obj suitable for generating a signature from. """ retval = bytearray() if obj is None: return b'N.' if isinstance(obj, SCons.Util.BaseStringTypes): return SCons.Util.to_bytes(obj) inst_class = obj.__class__ inst_class_name = bytearray(obj.__class__.__name__,'utf-8') inst_class_module = bytearray(obj.__class__.__module__,'utf-8') inst_class_hierarchy = bytearray(repr(inspect.getclasstree([obj.__class__,])),'utf-8') # print("ICH:%s : %s"%(inst_class_hierarchy, repr(obj))) properties = [(p, getattr(obj, p, "None")) for p in dir(obj) if not (p[:2] == '__' or inspect.ismethod(getattr(obj, p)) or inspect.isbuiltin(getattr(obj,p))) ] properties.sort() properties_str = ','.join(["%s=%s"%(p[0],p[1]) for p in properties]) properties_bytes = bytearray(properties_str,'utf-8') methods = [p for p in dir(obj) if inspect.ismethod(getattr(obj, p))] methods.sort() method_contents = [] for m in methods: # print("Method:%s"%m) v = _function_contents(getattr(obj, m)) # print("[%s->]V:%s [%s]"%(m,v,type(v))) method_contents.append(v) retval = bytearray(b'{') retval.extend(inst_class_name) retval.extend(b":") retval.extend(inst_class_module) retval.extend(b'}[[') retval.extend(inst_class_hierarchy) retval.extend(b']]{{') retval.extend(bytearray(b",").join(method_contents)) retval.extend(b"}}{{{") retval.extend(properties_bytes) retval.extend(b'}}}') return retval
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:execute; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:self; 5, identifier:target; 6, identifier:source; 7, identifier:env; 8, default_parameter; 8, 9; 8, 10; 9, identifier:executor; 10, None; 11, block; 11, 12; 11, 14; 11, 22; 11, 30; 11, 50; 11, 96; 11, 109; 11, 116; 11, 117; 11, 182; 11, 201; 11, 223; 11, 224; 11, 286; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:escape_list; 17, attribute; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:SCons; 20, identifier:Subst; 21, identifier:escape_list; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:flatten_sequence; 25, attribute; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:SCons; 28, identifier:Util; 29, identifier:flatten_sequence; 30, try_statement; 30, 31; 30, 38; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:shell; 35, subscript; 35, 36; 35, 37; 36, identifier:env; 37, string:'SHELL'; 38, except_clause; 38, 39; 38, 40; 39, identifier:KeyError; 40, block; 40, 41; 41, raise_statement; 41, 42; 42, call; 42, 43; 42, 48; 43, attribute; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:SCons; 46, identifier:Errors; 47, identifier:UserError; 48, argument_list; 48, 49; 49, string:'Missing SHELL construction variable.'; 50, try_statement; 50, 51; 50, 58; 50, 70; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:spawn; 55, subscript; 55, 56; 55, 57; 56, identifier:env; 57, string:'SPAWN'; 58, except_clause; 58, 59; 58, 60; 59, identifier:KeyError; 60, block; 60, 61; 61, raise_statement; 61, 62; 62, call; 62, 63; 62, 68; 63, attribute; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:SCons; 66, identifier:Errors; 67, identifier:UserError; 68, argument_list; 68, 69; 69, string:'Missing SPAWN construction variable.'; 70, else_clause; 70, 71; 71, block; 71, 72; 72, if_statement; 72, 73; 72, 77; 73, call; 73, 74; 73, 75; 74, identifier:is_String; 75, argument_list; 75, 76; 76, identifier:spawn; 77, block; 77, 78; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:spawn; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:env; 84, identifier:subst; 85, argument_list; 85, 86; 85, 87; 85, 90; 86, identifier:spawn; 87, keyword_argument; 87, 88; 87, 89; 88, identifier:raw; 89, integer:1; 90, keyword_argument; 90, 91; 90, 92; 91, identifier:conv; 92, lambda; 92, 93; 92, 95; 93, lambda_parameters; 93, 94; 94, identifier:x; 95, identifier:x; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:escape; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:env; 102, identifier:get; 103, argument_list; 103, 104; 103, 105; 104, string:'ESCAPE'; 105, lambda; 105, 106; 105, 108; 106, lambda_parameters; 106, 107; 107, identifier:x; 108, identifier:x; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:ENV; 112, call; 112, 113; 112, 114; 113, identifier:get_default_ENV; 114, argument_list; 114, 115; 115, identifier:env; 116, comment; 117, for_statement; 117, 118; 117, 121; 117, 126; 118, pattern_list; 118, 119; 118, 120; 119, identifier:key; 120, identifier:value; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:ENV; 124, identifier:items; 125, argument_list; 126, block; 126, 127; 127, if_statement; 127, 128; 127, 133; 128, not_operator; 128, 129; 129, call; 129, 130; 129, 131; 130, identifier:is_String; 131, argument_list; 131, 132; 132, identifier:value; 133, block; 133, 134; 134, if_statement; 134, 135; 134, 139; 134, 140; 134, 141; 134, 142; 134, 167; 135, call; 135, 136; 135, 137; 136, identifier:is_List; 137, argument_list; 137, 138; 138, identifier:value; 139, comment; 140, comment; 141, comment; 142, block; 142, 143; 142, 150; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:value; 146, call; 146, 147; 146, 148; 147, identifier:flatten_sequence; 148, argument_list; 148, 149; 149, identifier:value; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 155; 152, subscript; 152, 153; 152, 154; 153, identifier:ENV; 154, identifier:key; 155, call; 155, 156; 155, 161; 156, attribute; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:os; 159, identifier:pathsep; 160, identifier:join; 161, argument_list; 161, 162; 162, call; 162, 163; 162, 164; 163, identifier:map; 164, argument_list; 164, 165; 164, 166; 165, identifier:str; 166, identifier:value; 167, else_clause; 167, 168; 167, 169; 167, 170; 167, 171; 167, 172; 168, comment; 169, comment; 170, comment; 171, comment; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 178; 175, subscript; 175, 176; 175, 177; 176, identifier:ENV; 177, identifier:key; 178, call; 178, 179; 178, 180; 179, identifier:str; 180, argument_list; 180, 181; 181, identifier:value; 182, if_statement; 182, 183; 182, 184; 183, identifier:executor; 184, block; 184, 185; 184, 193; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:target; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:executor; 191, identifier:get_all_targets; 192, argument_list; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:source; 196, call; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:executor; 199, identifier:get_all_sources; 200, argument_list; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 207; 203, pattern_list; 203, 204; 203, 205; 203, 206; 204, identifier:cmd_list; 205, identifier:ignore; 206, identifier:silent; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:self; 210, identifier:process; 211, argument_list; 211, 212; 211, 213; 211, 221; 211, 222; 212, identifier:target; 213, call; 213, 214; 213, 215; 214, identifier:list; 215, argument_list; 215, 216; 216, call; 216, 217; 216, 218; 217, identifier:map; 218, argument_list; 218, 219; 218, 220; 219, identifier:rfile; 220, identifier:source; 221, identifier:env; 222, identifier:executor; 223, comment; 224, for_statement; 224, 225; 224, 226; 224, 231; 224, 232; 225, identifier:cmd_line; 226, call; 226, 227; 226, 228; 227, identifier:filter; 228, argument_list; 228, 229; 228, 230; 229, identifier:len; 230, identifier:cmd_list; 231, comment; 232, block; 232, 233; 232, 241; 232, 254; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 236; 235, identifier:cmd_line; 236, call; 236, 237; 236, 238; 237, identifier:escape_list; 238, argument_list; 238, 239; 238, 240; 239, identifier:cmd_line; 240, identifier:escape; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 244; 243, identifier:result; 244, call; 244, 245; 244, 246; 245, identifier:spawn; 246, argument_list; 246, 247; 246, 248; 246, 249; 246, 252; 246, 253; 247, identifier:shell; 248, identifier:escape; 249, subscript; 249, 250; 249, 251; 250, identifier:cmd_line; 251, integer:0; 252, identifier:cmd_line; 253, identifier:ENV; 254, if_statement; 254, 255; 254, 259; 255, boolean_operator:and; 255, 256; 255, 258; 256, not_operator; 256, 257; 257, identifier:ignore; 258, identifier:result; 259, block; 259, 260; 259, 266; 260, expression_statement; 260, 261; 261, assignment; 261, 262; 261, 263; 262, identifier:msg; 263, binary_operator:%; 263, 264; 263, 265; 264, string:"Error %s"; 265, identifier:result; 266, return_statement; 266, 267; 267, call; 267, 268; 267, 273; 268, attribute; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:SCons; 271, identifier:Errors; 272, identifier:BuildError; 273, argument_list; 273, 274; 273, 277; 273, 280; 273, 283; 274, keyword_argument; 274, 275; 274, 276; 275, identifier:errstr; 276, identifier:msg; 277, keyword_argument; 277, 278; 277, 279; 278, identifier:status; 279, identifier:result; 280, keyword_argument; 280, 281; 280, 282; 281, identifier:action; 282, identifier:self; 283, keyword_argument; 283, 284; 283, 285; 284, identifier:command; 285, identifier:cmd_line; 286, return_statement; 286, 287; 287, integer:0
def execute(self, target, source, env, executor=None): """Execute a command action. This will handle lists of commands as well as individual commands, because construction variable substitution may turn a single "command" into a list. This means that this class can actually handle lists of commands, even though that's not how we use it externally. """ escape_list = SCons.Subst.escape_list flatten_sequence = SCons.Util.flatten_sequence try: shell = env['SHELL'] except KeyError: raise SCons.Errors.UserError('Missing SHELL construction variable.') try: spawn = env['SPAWN'] except KeyError: raise SCons.Errors.UserError('Missing SPAWN construction variable.') else: if is_String(spawn): spawn = env.subst(spawn, raw=1, conv=lambda x: x) escape = env.get('ESCAPE', lambda x: x) ENV = get_default_ENV(env) # Ensure that the ENV values are all strings: for key, value in ENV.items(): if not is_String(value): if is_List(value): # If the value is a list, then we assume it is a # path list, because that's a pretty common list-like # value to stick in an environment variable: value = flatten_sequence(value) ENV[key] = os.pathsep.join(map(str, value)) else: # If it isn't a string or a list, then we just coerce # it to a string, which is the proper way to handle # Dir and File instances and will produce something # reasonable for just about everything else: ENV[key] = str(value) if executor: target = executor.get_all_targets() source = executor.get_all_sources() cmd_list, ignore, silent = self.process(target, list(map(rfile, source)), env, executor) # Use len() to filter out any "command" that's zero-length. for cmd_line in filter(len, cmd_list): # Escape the command line for the interpreter we are using. cmd_line = escape_list(cmd_line, escape) result = spawn(shell, escape, cmd_line[0], cmd_line, ENV) if not ignore and result: msg = "Error %s" % result return SCons.Errors.BuildError(errstr=msg, status=result, action=self, command=cmd_line) return 0
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_try_load_module; 3, parameters; 3, 4; 3, 5; 4, identifier:path; 5, default_parameter; 5, 6; 5, 7; 6, identifier:import_name; 7, None; 8, block; 8, 9; 8, 11; 8, 20; 8, 24; 8, 53; 8, 66; 8, 75; 8, 98; 8, 111; 8, 127; 8, 146; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:logger; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:logging; 17, identifier:getLogger; 18, argument_list; 18, 19; 19, identifier:__name__; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:obj_name; 23, None; 24, if_statement; 24, 25; 24, 39; 24, 40; 25, boolean_operator:and; 25, 26; 25, 32; 26, comparison_operator:>; 26, 27; 26, 31; 27, call; 27, 28; 27, 29; 28, identifier:len; 29, argument_list; 29, 30; 30, identifier:path; 31, integer:2; 32, comparison_operator:in; 32, 33; 32, 34; 33, string:':'; 34, subscript; 34, 35; 34, 36; 35, identifier:path; 36, slice; 36, 37; 36, 38; 37, integer:2; 38, colon; 39, comment; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 47; 43, pattern_list; 43, 44; 43, 45; 43, 46; 44, identifier:path; 45, identifier:_; 46, identifier:obj_name; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:path; 50, identifier:rpartition; 51, argument_list; 51, 52; 52, string:":"; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 58; 55, pattern_list; 55, 56; 55, 57; 56, identifier:folder; 57, identifier:basename; 58, call; 58, 59; 58, 64; 59, attribute; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:os; 62, identifier:path; 63, identifier:split; 64, argument_list; 64, 65; 65, identifier:path; 66, if_statement; 66, 67; 66, 70; 67, comparison_operator:==; 67, 68; 67, 69; 68, identifier:folder; 69, string:''; 70, block; 70, 71; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:folder; 74, string:'./'; 75, if_statement; 75, 76; 75, 89; 76, boolean_operator:or; 76, 77; 76, 80; 77, comparison_operator:==; 77, 78; 77, 79; 78, identifier:basename; 79, string:''; 80, not_operator; 80, 81; 81, call; 81, 82; 81, 87; 82, attribute; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:os; 85, identifier:path; 86, identifier:exists; 87, argument_list; 87, 88; 88, identifier:path; 89, block; 89, 90; 90, raise_statement; 90, 91; 91, call; 91, 92; 91, 93; 92, identifier:ArgumentError; 93, argument_list; 93, 94; 93, 95; 94, string:"Could not find python module to load extension"; 95, keyword_argument; 95, 96; 95, 97; 96, identifier:path; 97, identifier:path; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 103; 100, pattern_list; 100, 101; 100, 102; 101, identifier:basename; 102, identifier:ext; 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:splitext; 109, argument_list; 109, 110; 110, identifier:basename; 111, if_statement; 111, 112; 111, 118; 112, comparison_operator:not; 112, 113; 112, 114; 113, identifier:ext; 114, tuple; 114, 115; 114, 116; 114, 117; 115, string:".py"; 116, string:".pyc"; 117, string:""; 118, block; 118, 119; 119, raise_statement; 119, 120; 120, call; 120, 121; 120, 122; 121, identifier:ArgumentError; 122, argument_list; 122, 123; 122, 124; 123, string:"Attempted to load module is not a python package or module (.py or .pyc)"; 124, keyword_argument; 124, 125; 124, 126; 125, identifier:path; 126, identifier:path; 127, if_statement; 127, 128; 127, 131; 127, 136; 128, comparison_operator:is; 128, 129; 128, 130; 129, identifier:import_name; 130, None; 131, block; 131, 132; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:import_name; 135, identifier:basename; 136, else_clause; 136, 137; 137, block; 137, 138; 138, expression_statement; 138, 139; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:logger; 142, identifier:debug; 143, argument_list; 143, 144; 143, 145; 144, string:"Importing module as subpackage: %s"; 145, identifier:import_name; 146, try_statement; 146, 147; 146, 232; 147, block; 147, 148; 147, 152; 147, 166; 147, 167; 147, 196; 147, 228; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:fileobj; 151, None; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 158; 154, pattern_list; 154, 155; 154, 156; 154, 157; 155, identifier:fileobj; 156, identifier:pathname; 157, identifier:description; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:imp; 161, identifier:find_module; 162, argument_list; 162, 163; 162, 164; 163, identifier:basename; 164, list:[folder]; 164, 165; 165, identifier:folder; 166, comment; 167, if_statement; 167, 168; 167, 173; 167, 182; 168, comparison_operator:in; 168, 169; 168, 170; 169, identifier:basename; 170, attribute; 170, 171; 170, 172; 171, identifier:sys; 172, identifier:modules; 173, block; 173, 174; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:mod; 177, subscript; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:sys; 180, identifier:modules; 181, identifier:basename; 182, else_clause; 182, 183; 183, block; 183, 184; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:mod; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:imp; 190, identifier:load_module; 191, argument_list; 191, 192; 191, 193; 191, 194; 191, 195; 192, identifier:import_name; 193, identifier:fileobj; 194, identifier:pathname; 195, identifier:description; 196, if_statement; 196, 197; 196, 200; 197, comparison_operator:is; 197, 198; 197, 199; 198, identifier:obj_name; 199, None; 200, block; 200, 201; 200, 220; 201, if_statement; 201, 202; 201, 207; 202, comparison_operator:not; 202, 203; 202, 204; 203, identifier:obj_name; 204, attribute; 204, 205; 204, 206; 205, identifier:mod; 206, identifier:__dict__; 207, block; 207, 208; 208, raise_statement; 208, 209; 209, call; 209, 210; 209, 211; 210, identifier:ArgumentError; 211, argument_list; 211, 212; 211, 217; 212, binary_operator:%; 212, 213; 212, 214; 213, string:"Cannot find named object '%s' inside module '%s'"; 214, tuple; 214, 215; 214, 216; 215, identifier:obj_name; 216, identifier:basename; 217, keyword_argument; 217, 218; 217, 219; 218, identifier:path; 219, identifier:path; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 223; 222, identifier:mod; 223, subscript; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:mod; 226, identifier:__dict__; 227, identifier:obj_name; 228, return_statement; 228, 229; 229, expression_list; 229, 230; 229, 231; 230, identifier:basename; 231, identifier:mod; 232, finally_clause; 232, 233; 233, block; 233, 234; 234, if_statement; 234, 235; 234, 238; 235, comparison_operator:is; 235, 236; 235, 237; 236, identifier:fileobj; 237, None; 238, block; 238, 239; 239, expression_statement; 239, 240; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:fileobj; 243, identifier:close; 244, argument_list
def _try_load_module(path, import_name=None): """Try to programmatically load a python module by path. Path should point to a python file (optionally without the .py) at the end. If it ends in a :<name> then name must point to an object defined in the module, which is returned instead of the module itself. Args: path (str): The path of the module to load import_name (str): The explicity name that the module should be given. If not specified, this defaults to being the basename() of path. However, if the module is inside of a support package, you should pass the correct name so that relative imports proceed correctly. Returns: str, object: The basename of the module loaded and the requested object. """ logger = logging.getLogger(__name__) obj_name = None if len(path) > 2 and ':' in path[2:]: # Don't flag windows C: type paths path, _, obj_name = path.rpartition(":") folder, basename = os.path.split(path) if folder == '': folder = './' if basename == '' or not os.path.exists(path): raise ArgumentError("Could not find python module to load extension", path=path) basename, ext = os.path.splitext(basename) if ext not in (".py", ".pyc", ""): raise ArgumentError("Attempted to load module is not a python package or module (.py or .pyc)", path=path) if import_name is None: import_name = basename else: logger.debug("Importing module as subpackage: %s", import_name) try: fileobj = None fileobj, pathname, description = imp.find_module(basename, [folder]) # Don't load modules twice if basename in sys.modules: mod = sys.modules[basename] else: mod = imp.load_module(import_name, fileobj, pathname, description) if obj_name is not None: if obj_name not in mod.__dict__: raise ArgumentError("Cannot find named object '%s' inside module '%s'" % (obj_name, basename), path=path) mod = mod.__dict__[obj_name] return basename, mod finally: if fileobj is not None: fileobj.close()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 21; 2, function_name:load_extensions; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 4, identifier:self; 5, identifier:group; 6, default_parameter; 6, 7; 6, 8; 7, identifier:name_filter; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:comp_filter; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:class_filter; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:product_name; 17, None; 18, default_parameter; 18, 19; 18, 20; 19, identifier:unique; 20, False; 21, block; 21, 22; 21, 24; 21, 28; 21, 135; 21, 210; 21, 252; 21, 271; 21, 325; 22, expression_statement; 22, 23; 23, comment; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:found_extensions; 27, list:[]; 28, if_statement; 28, 29; 28, 32; 29, comparison_operator:is; 29, 30; 29, 31; 30, identifier:product_name; 31, None; 32, block; 32, 33; 33, for_statement; 33, 34; 33, 35; 33, 40; 34, identifier:comp; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:iter_components; 39, argument_list; 40, block; 40, 41; 40, 53; 40, 62; 41, if_statement; 41, 42; 41, 51; 42, boolean_operator:and; 42, 43; 42, 46; 43, comparison_operator:is; 43, 44; 43, 45; 44, identifier:comp_filter; 45, None; 46, comparison_operator:!=; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:comp; 49, identifier:name; 50, identifier:comp_filter; 51, block; 51, 52; 52, continue_statement; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:products; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:comp; 59, identifier:find_products; 60, argument_list; 60, 61; 61, identifier:product_name; 62, for_statement; 62, 63; 62, 64; 62, 65; 63, identifier:product; 64, identifier:products; 65, block; 65, 66; 66, try_statement; 66, 67; 66, 120; 67, block; 67, 68; 67, 86; 67, 113; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:entries; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:self; 74, identifier:load_extension; 75, argument_list; 75, 76; 75, 77; 75, 80; 75, 83; 76, identifier:product; 77, keyword_argument; 77, 78; 77, 79; 78, identifier:name_filter; 79, identifier:name_filter; 80, keyword_argument; 80, 81; 80, 82; 81, identifier:class_filter; 82, identifier:class_filter; 83, keyword_argument; 83, 84; 83, 85; 84, identifier:component; 85, identifier:comp; 86, if_statement; 86, 87; 86, 97; 86, 98; 87, boolean_operator:and; 87, 88; 87, 94; 88, comparison_operator:==; 88, 89; 88, 93; 89, call; 89, 90; 89, 91; 90, identifier:len; 91, argument_list; 91, 92; 92, identifier:entries; 93, integer:0; 94, comparison_operator:is; 94, 95; 94, 96; 95, identifier:name_filter; 96, None; 97, comment; 98, block; 98, 99; 98, 112; 99, expression_statement; 99, 100; 100, call; 100, 101; 100, 106; 101, attribute; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:_logger; 105, identifier:warn; 106, argument_list; 106, 107; 106, 108; 106, 109; 107, string:"Found no valid extensions in product %s of component %s"; 108, identifier:product; 109, attribute; 109, 110; 109, 111; 110, identifier:comp; 111, identifier:path; 112, continue_statement; 113, expression_statement; 113, 114; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:found_extensions; 117, identifier:extend; 118, argument_list; 118, 119; 119, identifier:entries; 120, except_clause; 120, 121; 120, 122; 121, comment; 122, block; 122, 123; 123, expression_statement; 123, 124; 124, call; 124, 125; 124, 130; 125, attribute; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:self; 128, identifier:_logger; 129, identifier:exception; 130, argument_list; 130, 131; 130, 132; 130, 133; 130, 134; 131, string:"Unable to load extension %s from local component %s at path %s"; 132, identifier:product_name; 133, identifier:comp; 134, identifier:product; 135, for_statement; 135, 136; 135, 137; 135, 143; 136, identifier:entry; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:self; 140, identifier:_iter_entrypoint_group; 141, argument_list; 141, 142; 142, identifier:group; 143, block; 143, 144; 143, 150; 143, 160; 143, 192; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:name; 147, attribute; 147, 148; 147, 149; 148, identifier:entry; 149, identifier:name; 150, if_statement; 150, 151; 150, 158; 151, boolean_operator:and; 151, 152; 151, 155; 152, comparison_operator:is; 152, 153; 152, 154; 153, identifier:name_filter; 154, None; 155, comparison_operator:!=; 155, 156; 155, 157; 156, identifier:name; 157, identifier:name_filter; 158, block; 158, 159; 159, continue_statement; 160, try_statement; 160, 161; 160, 170; 161, block; 161, 162; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:ext; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:entry; 168, identifier:load; 169, argument_list; 170, except_clause; 170, 171; 170, 172; 171, comment; 172, block; 172, 173; 172, 191; 173, expression_statement; 173, 174; 174, call; 174, 175; 174, 180; 175, attribute; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:self; 178, identifier:_logger; 179, identifier:warn; 180, argument_list; 180, 181; 180, 182; 180, 185; 180, 188; 181, string:"Unable to load %s from %s"; 182, attribute; 182, 183; 182, 184; 183, identifier:entry; 184, identifier:name; 185, attribute; 185, 186; 185, 187; 186, identifier:entry; 187, identifier:distro; 188, keyword_argument; 188, 189; 188, 190; 189, identifier:exc_info; 190, True; 191, continue_statement; 192, expression_statement; 192, 193; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:found_extensions; 196, identifier:extend; 197, generator_expression; 197, 198; 197, 201; 198, tuple; 198, 199; 198, 200; 199, identifier:name; 200, identifier:x; 201, for_in_clause; 201, 202; 201, 203; 202, identifier:x; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:self; 206, identifier:_filter_subclasses; 207, argument_list; 207, 208; 207, 209; 208, identifier:ext; 209, identifier:class_filter; 210, for_statement; 210, 211; 210, 214; 210, 223; 211, tuple_pattern; 211, 212; 211, 213; 212, identifier:name; 213, identifier:ext; 214, call; 214, 215; 214, 220; 215, attribute; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:self; 218, identifier:_registered_extensions; 219, identifier:get; 220, argument_list; 220, 221; 220, 222; 221, identifier:group; 222, list:[]; 223, block; 223, 224; 223, 234; 224, if_statement; 224, 225; 224, 232; 225, boolean_operator:and; 225, 226; 225, 229; 226, comparison_operator:is; 226, 227; 226, 228; 227, identifier:name_filter; 228, None; 229, comparison_operator:!=; 229, 230; 229, 231; 230, identifier:name; 231, identifier:name_filter; 232, block; 232, 233; 233, continue_statement; 234, expression_statement; 234, 235; 235, call; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:found_extensions; 238, identifier:extend; 239, generator_expression; 239, 240; 239, 243; 240, tuple; 240, 241; 240, 242; 241, identifier:name; 242, identifier:x; 243, for_in_clause; 243, 244; 243, 245; 244, identifier:x; 245, call; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:self; 248, identifier:_filter_subclasses; 249, argument_list; 249, 250; 249, 251; 250, identifier:ext; 251, identifier:class_filter; 252, expression_statement; 252, 253; 253, assignment; 253, 254; 253, 255; 254, identifier:found_extensions; 255, list_comprehension; 255, 256; 255, 259; 255, 264; 256, tuple; 256, 257; 256, 258; 257, identifier:name; 258, identifier:x; 259, for_in_clause; 259, 260; 259, 263; 260, pattern_list; 260, 261; 260, 262; 261, identifier:name; 262, identifier:x; 263, identifier:found_extensions; 264, if_clause; 264, 265; 265, call; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, identifier:self; 268, identifier:_filter_nonextensions; 269, argument_list; 269, 270; 270, identifier:x; 271, if_statement; 271, 272; 271, 275; 272, comparison_operator:is; 272, 273; 272, 274; 273, identifier:unique; 274, True; 275, block; 275, 276; 275, 321; 276, if_statement; 276, 277; 276, 283; 276, 302; 277, comparison_operator:>; 277, 278; 277, 282; 278, call; 278, 279; 278, 280; 279, identifier:len; 280, argument_list; 280, 281; 281, identifier:found_extensions; 282, integer:1; 283, block; 283, 284; 284, raise_statement; 284, 285; 285, call; 285, 286; 285, 287; 286, identifier:ArgumentError; 287, argument_list; 287, 288; 287, 299; 288, binary_operator:%; 288, 289; 288, 290; 289, string:"Extension %s should have had exactly one instance of class %s, found %d"; 290, tuple; 290, 291; 290, 292; 290, 295; 291, identifier:group; 292, attribute; 292, 293; 292, 294; 293, identifier:class_filter; 294, identifier:__name__; 295, call; 295, 296; 295, 297; 296, identifier:len; 297, argument_list; 297, 298; 298, identifier:found_extensions; 299, keyword_argument; 299, 300; 299, 301; 300, identifier:classes; 301, identifier:found_extensions; 302, elif_clause; 302, 303; 302, 309; 303, comparison_operator:==; 303, 304; 303, 308; 304, call; 304, 305; 304, 306; 305, identifier:len; 306, argument_list; 306, 307; 307, identifier:found_extensions; 308, integer:0; 309, block; 309, 310; 310, raise_statement; 310, 311; 311, call; 311, 312; 311, 313; 312, identifier:ArgumentError; 313, argument_list; 313, 314; 314, binary_operator:%; 314, 315; 314, 316; 315, string:"Extension %s had no instances of class %s"; 316, tuple; 316, 317; 316, 318; 317, identifier:group; 318, attribute; 318, 319; 318, 320; 319, identifier:class_filter; 320, identifier:__name__; 321, return_statement; 321, 322; 322, subscript; 322, 323; 322, 324; 323, identifier:found_extensions; 324, integer:0; 325, return_statement; 325, 326; 326, identifier:found_extensions
def load_extensions(self, group, name_filter=None, comp_filter=None, class_filter=None, product_name=None, unique=False): """Dynamically load and return extension objects of a given type. This is the centralized way for all parts of CoreTools to allow plugin behavior. Whenever a plugin is needed, this method should be called to load it. Examples of plugins are proxy modules, emulated tiles, iotile-build autobuilders, etc. Each kind of plugin will typically be a subclass of a certain base class and can be provided one of three ways: 1. It can be registered as an entry point in a pip installed package. The entry point group must map the group passed to load_extensions. 2. It can be listed as a product of an IOTile component that is stored in this ComponentRegistry. The relevant python file inside the component will be imported dynamically as needed. 3. It can be programmatically registered by calling ``register_extension()`` on this class with a string name and an object. This is equivalent to exposing that same object as an entry point with the given name. There is special behavior of this function if class_filter is passed and the object returned by one of the above three methods is a python module. The module will be search for object definitions that match the defined class. The order of the returned objects list is only partially defined. Locally installed components are searched before pip installed packages with entry points. The order of results within each group is not specified. Args: group (str): The extension type that you wish to enumerate. This will be used as the entry_point group for searching pip installed packages. name_filter (str): Only return objects with the given name comp_filter (str): When searching through installed components (not entry_points), only search through components with the given name. class_filter (type or tuple of types): An object that will be passed to instanceof() to verify that all extension objects have the correct types. If not passed, no checking will be done. product_name (str): If this extension can be provided by a registered local component, the name of the product that should be loaded. unique (bool): If True (default is False), there must be exactly one object found inside this extension that matches all of the other criteria. Returns: list of (str, object): A list of the found and loaded extension objects. The string returned with each extension is the name of the entry_point or the base name of the file in the component or the value provided by the call to register_extension depending on how the extension was found. If unique is True, then the list only contains a single entry and that entry will be directly returned. """ found_extensions = [] if product_name is not None: for comp in self.iter_components(): if comp_filter is not None and comp.name != comp_filter: continue products = comp.find_products(product_name) for product in products: try: entries = self.load_extension(product, name_filter=name_filter, class_filter=class_filter, component=comp) if len(entries) == 0 and name_filter is None: # Don't warn if we're filtering by name since most extensions won't match self._logger.warn("Found no valid extensions in product %s of component %s", product, comp.path) continue found_extensions.extend(entries) except: # pylint:disable=bare-except;We don't want a broken extension to take down the whole system self._logger.exception("Unable to load extension %s from local component %s at path %s", product_name, comp, product) for entry in self._iter_entrypoint_group(group): name = entry.name if name_filter is not None and name != name_filter: continue try: ext = entry.load() except: # pylint:disable=bare-except; self._logger.warn("Unable to load %s from %s", entry.name, entry.distro, exc_info=True) continue found_extensions.extend((name, x) for x in self._filter_subclasses(ext, class_filter)) for (name, ext) in self._registered_extensions.get(group, []): if name_filter is not None and name != name_filter: continue found_extensions.extend((name, x) for x in self._filter_subclasses(ext, class_filter)) found_extensions = [(name, x) for name, x in found_extensions if self._filter_nonextensions(x)] if unique is True: if len(found_extensions) > 1: raise ArgumentError("Extension %s should have had exactly one instance of class %s, found %d" % (group, class_filter.__name__, len(found_extensions)), classes=found_extensions) elif len(found_extensions) == 0: raise ArgumentError("Extension %s had no instances of class %s" % (group, class_filter.__name__)) return found_extensions[0] return found_extensions
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 2, function_name:load_extension; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 4, identifier:self; 5, identifier:path; 6, default_parameter; 6, 7; 6, 8; 7, identifier:name_filter; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:class_filter; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:unique; 14, False; 15, default_parameter; 15, 16; 15, 17; 16, identifier:component; 17, None; 18, block; 18, 19; 18, 21; 18, 25; 18, 38; 18, 50; 18, 61; 18, 77; 18, 96; 18, 102; 18, 147; 19, expression_statement; 19, 20; 20, comment; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:import_name; 24, None; 25, if_statement; 25, 26; 25, 29; 26, comparison_operator:is; 26, 27; 26, 28; 27, identifier:component; 28, None; 29, block; 29, 30; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:import_name; 33, call; 33, 34; 33, 35; 34, identifier:_ensure_package_loaded; 35, argument_list; 35, 36; 35, 37; 36, identifier:path; 37, identifier:component; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 43; 40, pattern_list; 40, 41; 40, 42; 41, identifier:name; 42, identifier:ext; 43, call; 43, 44; 43, 45; 44, identifier:_try_load_module; 45, argument_list; 45, 46; 45, 47; 46, identifier:path; 47, keyword_argument; 47, 48; 47, 49; 48, identifier:import_name; 49, identifier:import_name; 50, if_statement; 50, 51; 50, 58; 51, boolean_operator:and; 51, 52; 51, 55; 52, comparison_operator:is; 52, 53; 52, 54; 53, identifier:name_filter; 54, None; 55, comparison_operator:!=; 55, 56; 55, 57; 56, identifier:name; 57, identifier:name_filter; 58, block; 58, 59; 59, return_statement; 59, 60; 60, list:[]; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:found; 64, list_comprehension; 64, 65; 64, 68; 65, tuple; 65, 66; 65, 67; 66, identifier:name; 67, identifier:x; 68, for_in_clause; 68, 69; 68, 70; 69, identifier:x; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:_filter_subclasses; 74, argument_list; 74, 75; 74, 76; 75, identifier:ext; 76, identifier:class_filter; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:found; 80, list_comprehension; 80, 81; 80, 84; 80, 89; 81, tuple; 81, 82; 81, 83; 82, identifier:name; 83, identifier:x; 84, for_in_clause; 84, 85; 84, 88; 85, pattern_list; 85, 86; 85, 87; 86, identifier:name; 87, identifier:x; 88, identifier:found; 89, if_clause; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:self; 93, identifier:_filter_nonextensions; 94, argument_list; 94, 95; 95, identifier:x; 96, if_statement; 96, 97; 96, 99; 97, not_operator; 97, 98; 98, identifier:unique; 99, block; 99, 100; 100, return_statement; 100, 101; 101, identifier:found; 102, if_statement; 102, 103; 102, 109; 102, 128; 103, comparison_operator:>; 103, 104; 103, 108; 104, call; 104, 105; 104, 106; 105, identifier:len; 106, argument_list; 106, 107; 107, identifier:found; 108, integer:1; 109, block; 109, 110; 110, raise_statement; 110, 111; 111, call; 111, 112; 111, 113; 112, identifier:ArgumentError; 113, argument_list; 113, 114; 113, 125; 114, binary_operator:%; 114, 115; 114, 116; 115, string:"Extension %s should have had exactly one instance of class %s, found %d"; 116, tuple; 116, 117; 116, 118; 116, 121; 117, identifier:path; 118, attribute; 118, 119; 118, 120; 119, identifier:class_filter; 120, identifier:__name__; 121, call; 121, 122; 121, 123; 122, identifier:len; 123, argument_list; 123, 124; 124, identifier:found; 125, keyword_argument; 125, 126; 125, 127; 126, identifier:classes; 127, identifier:found; 128, elif_clause; 128, 129; 128, 135; 129, comparison_operator:==; 129, 130; 129, 134; 130, call; 130, 131; 130, 132; 131, identifier:len; 132, argument_list; 132, 133; 133, identifier:found; 134, integer:0; 135, block; 135, 136; 136, raise_statement; 136, 137; 137, call; 137, 138; 137, 139; 138, identifier:ArgumentError; 139, argument_list; 139, 140; 140, binary_operator:%; 140, 141; 140, 142; 141, string:"Extension %s had no instances of class %s"; 142, tuple; 142, 143; 142, 144; 143, identifier:path; 144, attribute; 144, 145; 144, 146; 145, identifier:class_filter; 146, identifier:__name__; 147, return_statement; 147, 148; 148, subscript; 148, 149; 148, 150; 149, identifier:found; 150, integer:0
def load_extension(self, path, name_filter=None, class_filter=None, unique=False, component=None): """Load a single python module extension. This function is similar to using the imp module directly to load a module and potentially inspecting the objects it declares to filter them by class. Args: path (str): The path to the python file to load name_filter (str): If passed, the basename of the module must match name or nothing is returned. class_filter (type): If passed, only instance of this class are returned. unique (bool): If True (default is False), there must be exactly one object found inside this extension that matches all of the other criteria. component (IOTile): The component that this extension comes from if it is loaded from an installed component. This is used to properly import the extension as a submodule of the component's support package. Returns: list of (name, type): A list of the objects found at the extension path. If unique is True, then the list only contains a single entry and that entry will be directly returned. """ import_name = None if component is not None: import_name = _ensure_package_loaded(path, component) name, ext = _try_load_module(path, import_name=import_name) if name_filter is not None and name != name_filter: return [] found = [(name, x) for x in self._filter_subclasses(ext, class_filter)] found = [(name, x) for name, x in found if self._filter_nonextensions(x)] if not unique: return found if len(found) > 1: raise ArgumentError("Extension %s should have had exactly one instance of class %s, found %d" % (path, class_filter.__name__, len(found)), classes=found) elif len(found) == 0: raise ArgumentError("Extension %s had no instances of class %s" % (path, class_filter.__name__)) return found[0]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:solutionEmitter; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:target; 5, identifier:source; 6, identifier:env; 7, block; 7, 8; 7, 10; 7, 11; 7, 12; 7, 13; 7, 26; 7, 27; 7, 45; 7, 54; 7, 62; 7, 332; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, comment; 12, comment; 13, if_statement; 13, 14; 13, 21; 14, comparison_operator:==; 14, 15; 14, 18; 15, subscript; 15, 16; 15, 17; 16, identifier:source; 17, integer:0; 18, subscript; 18, 19; 18, 20; 19, identifier:target; 20, integer:0; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:source; 25, list:[]; 26, comment; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 32; 29, tuple_pattern; 29, 30; 29, 31; 30, identifier:base; 31, identifier:suff; 32, call; 32, 33; 32, 38; 33, attribute; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:SCons; 36, identifier:Util; 37, identifier:splitext; 38, argument_list; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:str; 41, argument_list; 41, 42; 42, subscript; 42, 43; 42, 44; 43, identifier:target; 44, integer:0; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:suff; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:env; 51, identifier:subst; 52, argument_list; 52, 53; 53, string:'$MSVSSOLUTIONSUFFIX'; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 59; 56, subscript; 56, 57; 56, 58; 57, identifier:target; 58, integer:0; 59, binary_operator:+; 59, 60; 59, 61; 60, identifier:base; 61, identifier:suff; 62, if_statement; 62, 63; 62, 65; 63, not_operator; 63, 64; 64, identifier:source; 65, block; 65, 66; 65, 70; 65, 108; 65, 204; 65, 242; 65, 305; 65, 318; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:source; 69, string:'sln_inputs:'; 70, if_statement; 70, 71; 70, 74; 71, comparison_operator:in; 71, 72; 71, 73; 72, string:'name'; 73, identifier:env; 74, block; 74, 75; 75, if_statement; 75, 76; 75, 86; 75, 97; 76, call; 76, 77; 76, 82; 77, attribute; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:SCons; 80, identifier:Util; 81, identifier:is_String; 82, argument_list; 82, 83; 83, subscript; 83, 84; 83, 85; 84, identifier:env; 85, string:'name'; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:source; 90, binary_operator:+; 90, 91; 90, 92; 91, identifier:source; 92, binary_operator:%; 92, 93; 92, 94; 93, string:' "%s"'; 94, subscript; 94, 95; 94, 96; 95, identifier:env; 96, string:'name'; 97, else_clause; 97, 98; 98, block; 98, 99; 99, raise_statement; 99, 100; 100, call; 100, 101; 100, 106; 101, attribute; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:SCons; 104, identifier:Errors; 105, identifier:InternalError; 106, argument_list; 106, 107; 107, string:"name must be a string"; 108, if_statement; 108, 109; 108, 112; 108, 193; 109, comparison_operator:in; 109, 110; 109, 111; 110, string:'variant'; 111, identifier:env; 112, block; 112, 113; 113, if_statement; 113, 114; 113, 124; 113, 135; 113, 182; 114, call; 114, 115; 114, 120; 115, attribute; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:SCons; 118, identifier:Util; 119, identifier:is_String; 120, argument_list; 120, 121; 121, subscript; 121, 122; 121, 123; 122, identifier:env; 123, string:'variant'; 124, block; 124, 125; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:source; 128, binary_operator:+; 128, 129; 128, 130; 129, identifier:source; 130, binary_operator:%; 130, 131; 130, 132; 131, string:' "%s"'; 132, subscript; 132, 133; 132, 134; 133, identifier:env; 134, string:'variant'; 135, elif_clause; 135, 136; 135, 146; 136, call; 136, 137; 136, 142; 137, attribute; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:SCons; 140, identifier:Util; 141, identifier:is_List; 142, argument_list; 142, 143; 143, subscript; 143, 144; 143, 145; 144, identifier:env; 145, string:'variant'; 146, block; 146, 147; 147, for_statement; 147, 148; 147, 149; 147, 152; 148, identifier:variant; 149, subscript; 149, 150; 149, 151; 150, identifier:env; 151, string:'variant'; 152, block; 152, 153; 153, if_statement; 153, 154; 153, 162; 153, 171; 154, call; 154, 155; 154, 160; 155, attribute; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:SCons; 158, identifier:Util; 159, identifier:is_String; 160, argument_list; 160, 161; 161, identifier:variant; 162, block; 162, 163; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:source; 166, binary_operator:+; 166, 167; 166, 168; 167, identifier:source; 168, binary_operator:%; 168, 169; 168, 170; 169, string:' "%s"'; 170, identifier:variant; 171, else_clause; 171, 172; 172, block; 172, 173; 173, raise_statement; 173, 174; 174, call; 174, 175; 174, 180; 175, attribute; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:SCons; 178, identifier:Errors; 179, identifier:InternalError; 180, argument_list; 180, 181; 181, string:"name must be a string or a list of strings"; 182, else_clause; 182, 183; 183, block; 183, 184; 184, raise_statement; 184, 185; 185, call; 185, 186; 185, 191; 186, attribute; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:SCons; 189, identifier:Errors; 190, identifier:InternalError; 191, argument_list; 191, 192; 192, string:"variant must be a string or a list of strings"; 193, else_clause; 193, 194; 194, block; 194, 195; 195, raise_statement; 195, 196; 196, call; 196, 197; 196, 202; 197, attribute; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:SCons; 200, identifier:Errors; 201, identifier:InternalError; 202, argument_list; 202, 203; 203, string:"variant must be specified"; 204, if_statement; 204, 205; 204, 208; 205, comparison_operator:in; 205, 206; 205, 207; 206, string:'slnguid'; 207, identifier:env; 208, block; 208, 209; 209, if_statement; 209, 210; 209, 220; 209, 231; 210, call; 210, 211; 210, 216; 211, attribute; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:SCons; 214, identifier:Util; 215, identifier:is_String; 216, argument_list; 216, 217; 217, subscript; 217, 218; 217, 219; 218, identifier:env; 219, string:'slnguid'; 220, block; 220, 221; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:source; 224, binary_operator:+; 224, 225; 224, 226; 225, identifier:source; 226, binary_operator:%; 226, 227; 226, 228; 227, string:' "%s"'; 228, subscript; 228, 229; 228, 230; 229, identifier:env; 230, string:'slnguid'; 231, else_clause; 231, 232; 232, block; 232, 233; 233, raise_statement; 233, 234; 234, call; 234, 235; 234, 240; 235, attribute; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:SCons; 238, identifier:Errors; 239, identifier:InternalError; 240, argument_list; 240, 241; 241, string:"slnguid must be a string"; 242, if_statement; 242, 243; 242, 246; 243, comparison_operator:in; 243, 244; 243, 245; 244, string:'projects'; 245, identifier:env; 246, block; 246, 247; 247, if_statement; 247, 248; 247, 258; 247, 269; 248, call; 248, 249; 248, 254; 249, attribute; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:SCons; 252, identifier:Util; 253, identifier:is_String; 254, argument_list; 254, 255; 255, subscript; 255, 256; 255, 257; 256, identifier:env; 257, string:'projects'; 258, block; 258, 259; 259, expression_statement; 259, 260; 260, assignment; 260, 261; 260, 262; 261, identifier:source; 262, binary_operator:+; 262, 263; 262, 264; 263, identifier:source; 264, binary_operator:%; 264, 265; 264, 266; 265, string:' "%s"'; 266, subscript; 266, 267; 266, 268; 267, identifier:env; 268, string:'projects'; 269, elif_clause; 269, 270; 269, 280; 270, call; 270, 271; 270, 276; 271, attribute; 271, 272; 271, 275; 272, attribute; 272, 273; 272, 274; 273, identifier:SCons; 274, identifier:Util; 275, identifier:is_List; 276, argument_list; 276, 277; 277, subscript; 277, 278; 277, 279; 278, identifier:env; 279, string:'projects'; 280, block; 280, 281; 281, for_statement; 281, 282; 281, 283; 281, 286; 282, identifier:t; 283, subscript; 283, 284; 283, 285; 284, identifier:env; 285, string:'projects'; 286, block; 286, 287; 287, if_statement; 287, 288; 287, 296; 288, call; 288, 289; 288, 294; 289, attribute; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:SCons; 292, identifier:Util; 293, identifier:is_String; 294, argument_list; 294, 295; 295, identifier:t; 296, block; 296, 297; 297, expression_statement; 297, 298; 298, assignment; 298, 299; 298, 300; 299, identifier:source; 300, binary_operator:+; 300, 301; 300, 302; 301, identifier:source; 302, binary_operator:%; 302, 303; 302, 304; 303, string:' "%s"'; 304, identifier:t; 305, expression_statement; 305, 306; 306, assignment; 306, 307; 306, 308; 307, identifier:source; 308, binary_operator:+; 308, 309; 308, 310; 309, identifier:source; 310, binary_operator:%; 310, 311; 310, 312; 311, string:' "%s"'; 312, call; 312, 313; 312, 314; 313, identifier:str; 314, argument_list; 314, 315; 315, subscript; 315, 316; 315, 317; 316, identifier:target; 317, integer:0; 318, expression_statement; 318, 319; 319, assignment; 319, 320; 319, 321; 320, identifier:source; 321, list:[SCons.Node.Python.Value(source)]; 321, 322; 322, call; 322, 323; 322, 330; 323, attribute; 323, 324; 323, 329; 324, attribute; 324, 325; 324, 328; 325, attribute; 325, 326; 325, 327; 326, identifier:SCons; 327, identifier:Node; 328, identifier:Python; 329, identifier:Value; 330, argument_list; 330, 331; 331, identifier:source; 332, return_statement; 332, 333; 333, tuple; 333, 334; 333, 338; 334, list:[target[0]]; 334, 335; 335, subscript; 335, 336; 335, 337; 336, identifier:target; 337, integer:0; 338, identifier:source
def solutionEmitter(target, source, env): """Sets up the DSW dependencies.""" # todo: Not sure what sets source to what user has passed as target, # but this is what happens. When that is fixed, we also won't have # to make the user always append env['MSVSSOLUTIONSUFFIX'] to target. if source[0] == target[0]: source = [] # make sure the suffix is correct for the version of MSVS we're running. (base, suff) = SCons.Util.splitext(str(target[0])) suff = env.subst('$MSVSSOLUTIONSUFFIX') target[0] = base + suff if not source: source = 'sln_inputs:' if 'name' in env: if SCons.Util.is_String(env['name']): source = source + ' "%s"' % env['name'] else: raise SCons.Errors.InternalError("name must be a string") if 'variant' in env: if SCons.Util.is_String(env['variant']): source = source + ' "%s"' % env['variant'] elif SCons.Util.is_List(env['variant']): for variant in env['variant']: if SCons.Util.is_String(variant): source = source + ' "%s"' % variant else: raise SCons.Errors.InternalError("name must be a string or a list of strings") else: raise SCons.Errors.InternalError("variant must be a string or a list of strings") else: raise SCons.Errors.InternalError("variant must be specified") if 'slnguid' in env: if SCons.Util.is_String(env['slnguid']): source = source + ' "%s"' % env['slnguid'] else: raise SCons.Errors.InternalError("slnguid must be a string") if 'projects' in env: if SCons.Util.is_String(env['projects']): source = source + ' "%s"' % env['projects'] elif SCons.Util.is_List(env['projects']): for t in env['projects']: if SCons.Util.is_String(t): source = source + ' "%s"' % t source = source + ' "%s"' % str(target[0]) source = [SCons.Node.Python.Value(source)] return ([target[0]], source)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:process_message; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:message; 6, default_parameter; 6, 7; 6, 8; 7, identifier:wait; 8, True; 9, block; 9, 10; 9, 12; 9, 22; 9, 26; 9, 156; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:to_check; 15, call; 15, 16; 15, 17; 16, identifier:deque; 17, argument_list; 17, 18; 18, list:[self._waiters]; 18, 19; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:_waiters; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:ignored; 25, True; 26, while_statement; 26, 27; 26, 33; 27, comparison_operator:>; 27, 28; 27, 32; 28, call; 28, 29; 28, 30; 29, identifier:len; 30, argument_list; 30, 31; 31, identifier:to_check; 32, integer:0; 33, block; 33, 34; 33, 42; 33, 54; 33, 110; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:context; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:to_check; 40, identifier:popleft; 41, argument_list; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:waiters; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:context; 48, identifier:get; 49, argument_list; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:OperationManager; 52, identifier:_LEAF; 53, list:[]; 54, for_statement; 54, 55; 54, 56; 54, 57; 55, identifier:waiter; 56, identifier:waiters; 57, block; 57, 58; 57, 106; 58, if_statement; 58, 59; 58, 66; 58, 74; 59, call; 59, 60; 59, 61; 60, identifier:isinstance; 61, argument_list; 61, 62; 61, 63; 62, identifier:waiter; 63, attribute; 63, 64; 63, 65; 64, identifier:asyncio; 65, identifier:Future; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:waiter; 71, identifier:set_result; 72, argument_list; 72, 73; 73, identifier:message; 74, else_clause; 74, 75; 75, block; 75, 76; 76, try_statement; 76, 77; 76, 89; 77, block; 77, 78; 78, expression_statement; 78, 79; 79, await; 79, 80; 80, call; 80, 81; 80, 82; 81, identifier:_wait_or_launch; 82, argument_list; 82, 83; 82, 86; 82, 87; 82, 88; 83, attribute; 83, 84; 83, 85; 84, identifier:self; 85, identifier:_loop; 86, identifier:waiter; 87, identifier:message; 88, identifier:wait; 89, except_clause; 89, 90; 89, 91; 90, comment; 91, block; 91, 92; 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:_logger; 98, identifier:warning; 99, argument_list; 99, 100; 99, 101; 99, 102; 99, 103; 100, string:"Error calling every_match callback, callback=%s, message=%s"; 101, identifier:waiter; 102, identifier:message; 103, keyword_argument; 103, 104; 103, 105; 104, identifier:exc_info; 105, True; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:ignored; 109, False; 110, for_statement; 110, 111; 110, 112; 110, 113; 111, identifier:key; 112, identifier:context; 113, block; 113, 114; 113, 122; 113, 130; 113, 136; 113, 142; 114, if_statement; 114, 115; 114, 120; 115, comparison_operator:is; 115, 116; 115, 117; 116, identifier:key; 117, attribute; 117, 118; 117, 119; 118, identifier:OperationManager; 119, identifier:_LEAF; 120, block; 120, 121; 121, continue_statement; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:message_val; 125, call; 125, 126; 125, 127; 126, identifier:_get_key; 127, argument_list; 127, 128; 127, 129; 128, identifier:message; 129, identifier:key; 130, if_statement; 130, 131; 130, 134; 131, comparison_operator:is; 131, 132; 131, 133; 132, identifier:message_val; 133, identifier:_MISSING; 134, block; 134, 135; 135, continue_statement; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:next_level; 139, subscript; 139, 140; 139, 141; 140, identifier:context; 141, identifier:key; 142, if_statement; 142, 143; 142, 146; 143, comparison_operator:in; 143, 144; 143, 145; 144, identifier:message_val; 145, identifier:next_level; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:to_check; 151, identifier:append; 152, argument_list; 152, 153; 153, subscript; 153, 154; 153, 155; 154, identifier:next_level; 155, identifier:message_val; 156, return_statement; 156, 157; 157, not_operator; 157, 158; 158, identifier:ignored
async def process_message(self, message, wait=True): """Process a message to see if it wakes any waiters. This will check waiters registered to see if they match the given message. If so, they are awoken and passed the message. All matching waiters will be woken. This method returns False if the message matched no waiters so it was ignored. Normally you want to use wait=True (the default behavior) to guarantee that all callbacks have finished before this method returns. However, sometimes that can cause a deadlock if those callbacks would themselves invoke behavior that requires whatever is waiting for this method to be alive. In that case you can pass wait=False to ensure that the caller of this method does not block. Args: message (dict or object): The message that we should process wait (bool): Whether to block until all callbacks have finished or to return once the callbacks have been launched. Returns: bool: True if at least one waiter matched, otherwise False. """ to_check = deque([self._waiters]) ignored = True while len(to_check) > 0: context = to_check.popleft() waiters = context.get(OperationManager._LEAF, []) for waiter in waiters: if isinstance(waiter, asyncio.Future): waiter.set_result(message) else: try: await _wait_or_launch(self._loop, waiter, message, wait) except: #pylint:disable=bare-except;We can't let a user callback break this routine self._logger.warning("Error calling every_match callback, callback=%s, message=%s", waiter, message, exc_info=True) ignored = False for key in context: if key is OperationManager._LEAF: continue message_val = _get_key(message, key) if message_val is _MISSING: continue next_level = context[key] if message_val in next_level: to_check.append(next_level[message_val]) return not ignored
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:compile; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:values; 6, block; 6, 7; 6, 9; 6, 21; 6, 33; 6, 45; 6, 58; 6, 62; 6, 78; 6, 109; 6, 124; 6, 195; 7, expression_statement; 7, 8; 8, comment; 9, function_definition; 9, 10; 9, 11; 9, 13; 10, function_name:is_international; 11, parameters; 11, 12; 12, identifier:tag; 13, block; 13, 14; 14, return_statement; 14, 15; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:tag; 18, identifier:endswith; 19, argument_list; 19, 20; 20, string:'_'; 21, function_definition; 21, 22; 21, 23; 21, 25; 22, function_name:get_country_code; 23, parameters; 23, 24; 24, identifier:tag; 25, block; 25, 26; 26, return_statement; 26, 27; 27, subscript; 27, 28; 27, 29; 28, identifier:tag; 29, slice; 29, 30; 29, 32; 30, unary_operator:-; 30, 31; 31, integer:2; 32, colon; 33, function_definition; 33, 34; 33, 35; 33, 37; 34, function_name:strip_country_code; 35, parameters; 35, 36; 36, identifier:tag; 37, block; 37, 38; 38, return_statement; 38, 39; 39, subscript; 39, 40; 39, 41; 40, identifier:tag; 41, slice; 41, 42; 41, 43; 42, colon; 43, unary_operator:-; 43, 44; 44, integer:2; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:replacements; 48, call; 48, 49; 48, 50; 49, identifier:list; 50, argument_list; 50, 51; 51, call; 51, 52; 51, 57; 52, attribute; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:self; 55, identifier:tagset; 56, identifier:items; 57, argument_list; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:str; 61, string:""; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:domestic; 65, list_comprehension; 65, 66; 65, 67; 65, 70; 66, identifier:t; 67, for_in_clause; 67, 68; 67, 69; 68, identifier:t; 69, identifier:replacements; 70, if_clause; 70, 71; 71, not_operator; 71, 72; 72, call; 72, 73; 72, 74; 73, identifier:is_international; 74, argument_list; 74, 75; 75, subscript; 75, 76; 75, 77; 76, identifier:t; 77, integer:0; 78, for_statement; 78, 79; 78, 82; 78, 83; 79, pattern_list; 79, 80; 79, 81; 80, identifier:key; 81, identifier:replacement; 82, identifier:domestic; 83, block; 83, 84; 84, try_statement; 84, 85; 84, 96; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:str; 89, binary_operator:+; 89, 90; 89, 91; 90, identifier:str; 91, binary_operator:%; 91, 92; 91, 93; 92, identifier:replacement; 93, subscript; 93, 94; 93, 95; 94, identifier:values; 95, identifier:key; 96, except_clause; 96, 97; 96, 101; 97, as_pattern; 97, 98; 97, 99; 98, identifier:KeyError; 99, as_pattern_target; 99, 100; 100, identifier:e; 101, block; 101, 102; 102, if_statement; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:self; 105, identifier:mandatory; 106, block; 106, 107; 107, raise_statement; 107, 108; 108, identifier:e; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:international; 112, list_comprehension; 112, 113; 112, 114; 112, 117; 113, identifier:t; 114, for_in_clause; 114, 115; 114, 116; 115, identifier:t; 116, identifier:replacements; 117, if_clause; 117, 118; 118, call; 118, 119; 118, 120; 119, identifier:is_international; 120, argument_list; 120, 121; 121, subscript; 121, 122; 121, 123; 122, identifier:t; 123, integer:0; 124, for_statement; 124, 125; 124, 128; 124, 129; 125, pattern_list; 125, 126; 125, 127; 126, identifier:key; 127, identifier:replacement; 128, identifier:international; 129, block; 129, 130; 130, try_statement; 130, 131; 130, 182; 131, block; 131, 132; 131, 153; 131, 170; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:x; 135, list_comprehension; 135, 136; 135, 137; 135, 144; 136, identifier:t; 137, for_in_clause; 137, 138; 137, 139; 138, identifier:t; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:values; 142, identifier:items; 143, argument_list; 144, if_clause; 144, 145; 145, comparison_operator:==; 145, 146; 145, 152; 146, call; 146, 147; 146, 148; 147, identifier:strip_country_code; 148, argument_list; 148, 149; 149, subscript; 149, 150; 149, 151; 150, identifier:t; 151, integer:0; 152, identifier:key; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:int_values_for_key; 156, list_comprehension; 156, 157; 156, 167; 157, tuple; 157, 158; 157, 164; 158, call; 158, 159; 158, 160; 159, identifier:get_country_code; 160, argument_list; 160, 161; 161, subscript; 161, 162; 161, 163; 162, identifier:t; 163, integer:0; 164, subscript; 164, 165; 164, 166; 165, identifier:t; 166, integer:1; 167, for_in_clause; 167, 168; 167, 169; 168, identifier:t; 169, identifier:x; 170, for_statement; 170, 171; 170, 172; 170, 173; 171, identifier:v; 172, identifier:int_values_for_key; 173, block; 173, 174; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:str; 177, binary_operator:+; 177, 178; 177, 179; 178, identifier:str; 179, binary_operator:%; 179, 180; 179, 181; 180, identifier:replacement; 181, identifier:v; 182, except_clause; 182, 183; 182, 187; 183, as_pattern; 183, 184; 183, 185; 184, identifier:KeyError; 185, as_pattern_target; 185, 186; 186, identifier:e; 187, block; 187, 188; 188, if_statement; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:self; 191, identifier:mandatory; 192, block; 192, 193; 193, raise_statement; 193, 194; 194, identifier:e; 195, return_statement; 195, 196; 196, identifier:str
def compile(self, values): """ Compiles the tagset and returns a str containing the result """ def is_international(tag): return tag.endswith('_') def get_country_code(tag): return tag[-2:] def strip_country_code(tag): return tag[:-2] replacements = list(self.tagset.items()) str = "" domestic = [t for t in replacements if not is_international(t[0])] for key, replacement in domestic: try: str = str + replacement % values[key] except KeyError as e: if self.mandatory: raise e international = [t for t in replacements if is_international(t[0])] for key, replacement in international: try: x = [t for t in values.items() if strip_country_code(t[0]) == key] int_values_for_key = [(get_country_code(t[0]),t[1]) for t in x] for v in int_values_for_key: str = str + replacement % v except KeyError as e: if self.mandatory: raise e return str
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 29; 2, function_name:FromReadings; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 12; 3, 15; 3, 20; 3, 23; 3, 26; 4, identifier:cls; 5, identifier:uuid; 6, identifier:readings; 7, default_parameter; 7, 8; 7, 9; 8, identifier:root_key; 9, attribute; 9, 10; 9, 11; 10, identifier:AuthProvider; 11, identifier:NoKey; 12, default_parameter; 12, 13; 12, 14; 13, identifier:signer; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:report_id; 17, attribute; 17, 18; 17, 19; 18, identifier:IOTileReading; 19, identifier:InvalidReadingID; 20, default_parameter; 20, 21; 20, 22; 21, identifier:selector; 22, integer:0xFFFF; 23, default_parameter; 23, 24; 23, 25; 24, identifier:streamer; 25, integer:0; 26, default_parameter; 26, 27; 26, 28; 27, identifier:sent_timestamp; 28, integer:0; 29, block; 29, 30; 29, 32; 29, 38; 29, 44; 29, 57; 29, 63; 29, 69; 29, 87; 29, 109; 29, 129; 29, 136; 29, 142; 29, 175; 29, 186; 29, 197; 29, 198; 29, 264; 29, 299; 29, 318; 29, 325; 29, 331; 30, expression_statement; 30, 31; 31, comment; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:lowest_id; 35, attribute; 35, 36; 35, 37; 36, identifier:IOTileReading; 37, identifier:InvalidReadingID; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:highest_id; 41, attribute; 41, 42; 41, 43; 42, identifier:IOTileReading; 43, identifier:InvalidReadingID; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:report_len; 47, binary_operator:+; 47, 48; 47, 56; 48, binary_operator:+; 48, 49; 48, 50; 49, integer:20; 50, binary_operator:*; 50, 51; 50, 52; 51, integer:16; 52, call; 52, 53; 52, 54; 53, identifier:len; 54, argument_list; 54, 55; 55, identifier:readings; 56, integer:24; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:len_low; 60, binary_operator:&; 60, 61; 60, 62; 61, identifier:report_len; 62, integer:0xFF; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:len_high; 66, binary_operator:>>; 66, 67; 66, 68; 67, identifier:report_len; 68, integer:8; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:unique_readings; 72, list_comprehension; 72, 73; 72, 76; 72, 79; 73, attribute; 73, 74; 73, 75; 74, identifier:x; 75, identifier:reading_id; 76, for_in_clause; 76, 77; 76, 78; 77, identifier:x; 78, identifier:readings; 79, if_clause; 79, 80; 80, comparison_operator:!=; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:x; 83, identifier:reading_id; 84, attribute; 84, 85; 84, 86; 85, identifier:IOTileReading; 86, identifier:InvalidReadingID; 87, if_statement; 87, 88; 87, 94; 88, comparison_operator:>; 88, 89; 88, 93; 89, call; 89, 90; 89, 91; 90, identifier:len; 91, argument_list; 91, 92; 92, identifier:unique_readings; 93, integer:0; 94, block; 94, 95; 94, 102; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:lowest_id; 98, call; 98, 99; 98, 100; 99, identifier:min; 100, argument_list; 100, 101; 101, identifier:unique_readings; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:highest_id; 105, call; 105, 106; 105, 107; 106, identifier:max; 107, argument_list; 107, 108; 108, identifier:unique_readings; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:header; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:struct; 115, identifier:pack; 116, argument_list; 116, 117; 116, 118; 116, 121; 116, 122; 116, 123; 116, 124; 116, 125; 116, 126; 116, 127; 116, 128; 117, string:"<BBHLLLBBH"; 118, attribute; 118, 119; 118, 120; 119, identifier:cls; 120, identifier:ReportType; 121, identifier:len_low; 122, identifier:len_high; 123, identifier:uuid; 124, identifier:report_id; 125, identifier:sent_timestamp; 126, identifier:root_key; 127, identifier:streamer; 128, identifier:selector; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:header; 132, call; 132, 133; 132, 134; 133, identifier:bytearray; 134, argument_list; 134, 135; 135, identifier:header; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:packed_readings; 139, call; 139, 140; 139, 141; 140, identifier:bytearray; 141, argument_list; 142, for_statement; 142, 143; 142, 144; 142, 145; 143, identifier:reading; 144, identifier:readings; 145, block; 145, 146; 145, 168; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:packed_reading; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:struct; 152, identifier:pack; 153, argument_list; 153, 154; 153, 155; 153, 158; 153, 159; 153, 162; 153, 165; 154, string:"<HHLLL"; 155, attribute; 155, 156; 155, 157; 156, identifier:reading; 157, identifier:stream; 158, integer:0; 159, attribute; 159, 160; 159, 161; 160, identifier:reading; 161, identifier:reading_id; 162, attribute; 162, 163; 162, 164; 163, identifier:reading; 164, identifier:raw_time; 165, attribute; 165, 166; 165, 167; 166, identifier:reading; 167, identifier:value; 168, expression_statement; 168, 169; 169, augmented_assignment:+=; 169, 170; 169, 171; 170, identifier:packed_readings; 171, call; 171, 172; 171, 173; 172, identifier:bytearray; 173, argument_list; 173, 174; 174, identifier:packed_reading; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:footer_stats; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:struct; 181, identifier:pack; 182, argument_list; 182, 183; 182, 184; 182, 185; 183, string:"<LL"; 184, identifier:lowest_id; 185, identifier:highest_id; 186, if_statement; 186, 187; 186, 190; 187, comparison_operator:is; 187, 188; 187, 189; 188, identifier:signer; 189, None; 190, block; 190, 191; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 194; 193, identifier:signer; 194, call; 194, 195; 194, 196; 195, identifier:ChainedAuthProvider; 196, argument_list; 197, comment; 198, if_statement; 198, 199; 198, 204; 198, 254; 199, comparison_operator:!=; 199, 200; 199, 201; 200, identifier:root_key; 201, attribute; 201, 202; 201, 203; 202, identifier:signer; 203, identifier:NoKey; 204, block; 204, 205; 204, 209; 204, 244; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:enc_data; 208, identifier:packed_readings; 209, try_statement; 209, 210; 209, 228; 210, block; 210, 211; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:result; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:signer; 217, identifier:encrypt_report; 218, argument_list; 218, 219; 218, 220; 218, 221; 218, 222; 218, 225; 219, identifier:uuid; 220, identifier:root_key; 221, identifier:enc_data; 222, keyword_argument; 222, 223; 222, 224; 223, identifier:report_id; 224, identifier:report_id; 225, keyword_argument; 225, 226; 225, 227; 226, identifier:sent_timestamp; 227, identifier:sent_timestamp; 228, except_clause; 228, 229; 228, 230; 229, identifier:NotFoundError; 230, block; 230, 231; 231, raise_statement; 231, 232; 232, call; 232, 233; 232, 234; 233, identifier:ExternalError; 234, argument_list; 234, 235; 234, 238; 234, 241; 235, concatenated_string; 235, 236; 235, 237; 236, string:"Could not encrypt report because no AuthProvider supported "; 237, string:"the requested encryption method for the requested device"; 238, keyword_argument; 238, 239; 238, 240; 239, identifier:device_id; 240, identifier:uuid; 241, keyword_argument; 241, 242; 241, 243; 242, identifier:root_key; 243, identifier:root_key; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 247; 246, identifier:signed_data; 247, binary_operator:+; 247, 248; 247, 253; 248, binary_operator:+; 248, 249; 248, 250; 249, identifier:header; 250, subscript; 250, 251; 250, 252; 251, identifier:result; 252, string:'data'; 253, identifier:footer_stats; 254, else_clause; 254, 255; 255, block; 255, 256; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 259; 258, identifier:signed_data; 259, binary_operator:+; 259, 260; 259, 263; 260, binary_operator:+; 260, 261; 260, 262; 261, identifier:header; 262, identifier:packed_readings; 263, identifier:footer_stats; 264, try_statement; 264, 265; 264, 283; 265, block; 265, 266; 266, expression_statement; 266, 267; 267, assignment; 267, 268; 267, 269; 268, identifier:signature; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:signer; 272, identifier:sign_report; 273, argument_list; 273, 274; 273, 275; 273, 276; 273, 277; 273, 280; 274, identifier:uuid; 275, identifier:root_key; 276, identifier:signed_data; 277, keyword_argument; 277, 278; 277, 279; 278, identifier:report_id; 279, identifier:report_id; 280, keyword_argument; 280, 281; 280, 282; 281, identifier:sent_timestamp; 282, identifier:sent_timestamp; 283, except_clause; 283, 284; 283, 285; 284, identifier:NotFoundError; 285, block; 285, 286; 286, raise_statement; 286, 287; 287, call; 287, 288; 287, 289; 288, identifier:ExternalError; 289, argument_list; 289, 290; 289, 293; 289, 296; 290, concatenated_string; 290, 291; 290, 292; 291, string:"Could not sign report because no AuthProvider supported the requested "; 292, string:"signature method for the requested device"; 293, keyword_argument; 293, 294; 293, 295; 294, identifier:device_id; 295, identifier:uuid; 296, keyword_argument; 296, 297; 296, 298; 297, identifier:root_key; 298, identifier:root_key; 299, expression_statement; 299, 300; 300, assignment; 300, 301; 300, 302; 301, identifier:footer; 302, call; 302, 303; 302, 306; 303, attribute; 303, 304; 303, 305; 304, identifier:struct; 305, identifier:pack; 306, argument_list; 306, 307; 306, 308; 307, string:"16s"; 308, call; 308, 309; 308, 310; 309, identifier:bytes; 310, argument_list; 310, 311; 311, subscript; 311, 312; 311, 315; 312, subscript; 312, 313; 312, 314; 313, identifier:signature; 314, string:'signature'; 315, slice; 315, 316; 315, 317; 316, colon; 317, integer:16; 318, expression_statement; 318, 319; 319, assignment; 319, 320; 319, 321; 320, identifier:footer; 321, call; 321, 322; 321, 323; 322, identifier:bytearray; 323, argument_list; 323, 324; 324, identifier:footer; 325, expression_statement; 325, 326; 326, assignment; 326, 327; 326, 328; 327, identifier:data; 328, binary_operator:+; 328, 329; 328, 330; 329, identifier:signed_data; 330, identifier:footer; 331, return_statement; 331, 332; 332, call; 332, 333; 332, 334; 333, identifier:SignedListReport; 334, argument_list; 334, 335; 335, identifier:data
def FromReadings(cls, uuid, readings, root_key=AuthProvider.NoKey, signer=None, report_id=IOTileReading.InvalidReadingID, selector=0xFFFF, streamer=0, sent_timestamp=0): """Generate an instance of the report format from a list of readings and a uuid. The signed list report is created using the passed readings and signed using the specified method and AuthProvider. If no auth provider is specified, the report is signed using the default authorization chain. Args: uuid (int): The uuid of the deviec that this report came from readings (list): A list of IOTileReading objects containing the data in the report root_key (int): The key that should be used to sign the report (must be supported by an auth_provider) signer (AuthProvider): An optional preconfigured AuthProvider that should be used to sign this report. If no AuthProvider is provided, the default ChainedAuthProvider is used. report_id (int): The id of the report. If not provided it defaults to IOTileReading.InvalidReadingID. Note that you can specify anything you want for the report id but for actual IOTile devices the report id will always be greater than the id of all of the readings contained in the report since devices generate ids sequentially. selector (int): The streamer selector of this report. This can be anything but if the report came from a device, it would correspond with the query the device used to pick readings to go into the report. streamer (int): The streamer id that this reading was sent from. sent_timestamp (int): The device's uptime that sent this report. """ lowest_id = IOTileReading.InvalidReadingID highest_id = IOTileReading.InvalidReadingID report_len = 20 + 16*len(readings) + 24 len_low = report_len & 0xFF len_high = report_len >> 8 unique_readings = [x.reading_id for x in readings if x.reading_id != IOTileReading.InvalidReadingID] if len(unique_readings) > 0: lowest_id = min(unique_readings) highest_id = max(unique_readings) header = struct.pack("<BBHLLLBBH", cls.ReportType, len_low, len_high, uuid, report_id, sent_timestamp, root_key, streamer, selector) header = bytearray(header) packed_readings = bytearray() for reading in readings: packed_reading = struct.pack("<HHLLL", reading.stream, 0, reading.reading_id, reading.raw_time, reading.value) packed_readings += bytearray(packed_reading) footer_stats = struct.pack("<LL", lowest_id, highest_id) if signer is None: signer = ChainedAuthProvider() # If we are supposed to encrypt this report, do the encryption if root_key != signer.NoKey: enc_data = packed_readings try: result = signer.encrypt_report(uuid, root_key, enc_data, report_id=report_id, sent_timestamp=sent_timestamp) except NotFoundError: raise ExternalError("Could not encrypt report because no AuthProvider supported " "the requested encryption method for the requested device", device_id=uuid, root_key=root_key) signed_data = header + result['data'] + footer_stats else: signed_data = header + packed_readings + footer_stats try: signature = signer.sign_report(uuid, root_key, signed_data, report_id=report_id, sent_timestamp=sent_timestamp) except NotFoundError: raise ExternalError("Could not sign report because no AuthProvider supported the requested " "signature method for the requested device", device_id=uuid, root_key=root_key) footer = struct.pack("16s", bytes(signature['signature'][:16])) footer = bytearray(footer) data = signed_data + footer return SignedListReport(data)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:format_snippet; 3, parameters; 3, 4; 4, identifier:sensor_graph; 5, block; 5, 6; 5, 8; 5, 12; 5, 13; 5, 20; 5, 27; 5, 34; 5, 35; 5, 55; 5, 56; 5, 110; 5, 111; 5, 151; 5, 152; 5, 159; 5, 166; 5, 167; 5, 178; 5, 189; 5, 228; 5, 229; 5, 236; 5, 243; 5, 313; 5, 314; 5, 321; 5, 328; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:output; 11, list:[]; 12, comment; 13, expression_statement; 13, 14; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:output; 17, identifier:append; 18, argument_list; 18, 19; 19, string:"disable"; 20, expression_statement; 20, 21; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:output; 24, identifier:append; 25, argument_list; 25, 26; 26, string:"clear"; 27, expression_statement; 27, 28; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:output; 31, identifier:append; 32, argument_list; 32, 33; 33, string:"reset"; 34, comment; 35, for_statement; 35, 36; 35, 37; 35, 42; 36, identifier:node; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:sensor_graph; 40, identifier:dump_nodes; 41, argument_list; 42, block; 42, 43; 43, expression_statement; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:output; 47, identifier:append; 48, argument_list; 48, 49; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, string:'add_node "{}"'; 52, identifier:format; 53, argument_list; 53, 54; 54, identifier:node; 55, comment; 56, for_statement; 56, 57; 56, 58; 56, 61; 57, identifier:streamer; 58, attribute; 58, 59; 58, 60; 59, identifier:sensor_graph; 60, identifier:streamers; 61, block; 61, 62; 61, 85; 61, 103; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:line; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, string:"add_streamer '{}' '{}' {} {} {}"; 68, identifier:format; 69, argument_list; 69, 70; 69, 73; 69, 76; 69, 79; 69, 82; 70, attribute; 70, 71; 70, 72; 71, identifier:streamer; 72, identifier:selector; 73, attribute; 73, 74; 73, 75; 74, identifier:streamer; 75, identifier:dest; 76, attribute; 76, 77; 76, 78; 77, identifier:streamer; 78, identifier:automatic; 79, attribute; 79, 80; 79, 81; 80, identifier:streamer; 81, identifier:format; 82, attribute; 82, 83; 82, 84; 83, identifier:streamer; 84, identifier:report_type; 85, if_statement; 85, 86; 85, 91; 86, comparison_operator:is; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:streamer; 89, identifier:with_other; 90, None; 91, block; 91, 92; 92, expression_statement; 92, 93; 93, augmented_assignment:+=; 93, 94; 93, 95; 94, identifier:line; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, string:' --withother {}'; 98, identifier:format; 99, argument_list; 99, 100; 100, attribute; 100, 101; 100, 102; 101, identifier:streamer; 102, identifier:with_other; 103, expression_statement; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:output; 107, identifier:append; 108, argument_list; 108, 109; 109, identifier:line; 110, comment; 111, for_statement; 111, 112; 111, 115; 111, 137; 112, pattern_list; 112, 113; 112, 114; 113, identifier:stream; 114, identifier:value; 115, call; 115, 116; 115, 117; 116, identifier:sorted; 117, argument_list; 117, 118; 117, 125; 118, call; 118, 119; 118, 124; 119, attribute; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:sensor_graph; 122, identifier:constant_database; 123, identifier:items; 124, argument_list; 125, keyword_argument; 125, 126; 125, 127; 126, identifier:key; 127, lambda; 127, 128; 127, 130; 128, lambda_parameters; 128, 129; 129, identifier:x; 130, call; 130, 131; 130, 136; 131, attribute; 131, 132; 131, 135; 132, subscript; 132, 133; 132, 134; 133, identifier:x; 134, integer:0; 135, identifier:encode; 136, argument_list; 137, block; 137, 138; 138, expression_statement; 138, 139; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:output; 142, identifier:append; 143, argument_list; 143, 144; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, string:"set_constant '{}' {}"; 147, identifier:format; 148, argument_list; 148, 149; 148, 150; 149, identifier:stream; 150, identifier:value; 151, comment; 152, expression_statement; 152, 153; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:output; 156, identifier:append; 157, argument_list; 157, 158; 158, string:"persist"; 159, expression_statement; 159, 160; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:output; 163, identifier:append; 164, argument_list; 164, 165; 165, string:"back"; 166, comment; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:app_tag; 170, call; 170, 171; 170, 176; 171, attribute; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:sensor_graph; 174, identifier:metadata_database; 175, identifier:get; 176, argument_list; 176, 177; 177, string:'app_tag'; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:app_version; 181, call; 181, 182; 181, 187; 182, attribute; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:sensor_graph; 185, identifier:metadata_database; 186, identifier:get; 187, argument_list; 187, 188; 188, string:'app_version'; 189, if_statement; 189, 190; 189, 193; 190, comparison_operator:is; 190, 191; 190, 192; 191, identifier:app_tag; 192, None; 193, block; 193, 194; 193, 203; 193, 210; 193, 221; 194, if_statement; 194, 195; 194, 198; 195, comparison_operator:is; 195, 196; 195, 197; 196, identifier:app_version; 197, None; 198, block; 198, 199; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:app_version; 202, string:"0.0"; 203, expression_statement; 203, 204; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:output; 207, identifier:append; 208, argument_list; 208, 209; 209, string:"test_interface"; 210, expression_statement; 210, 211; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:output; 214, identifier:append; 215, argument_list; 215, 216; 216, binary_operator:%; 216, 217; 216, 218; 217, string:"set_version app %d --version '%s'"; 218, tuple; 218, 219; 218, 220; 219, identifier:app_tag; 220, identifier:app_version; 221, expression_statement; 221, 222; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:output; 225, identifier:append; 226, argument_list; 226, 227; 227, string:"back"; 228, comment; 229, expression_statement; 229, 230; 230, call; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:output; 233, identifier:append; 234, argument_list; 234, 235; 235, string:"config_database"; 236, expression_statement; 236, 237; 237, call; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:output; 240, identifier:append; 241, argument_list; 241, 242; 242, string:"clear_variables"; 243, for_statement; 243, 244; 243, 247; 243, 254; 244, pattern_list; 244, 245; 244, 246; 245, identifier:slot; 246, identifier:conf_vars; 247, call; 247, 248; 247, 253; 248, attribute; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:sensor_graph; 251, identifier:config_database; 252, identifier:items; 253, argument_list; 254, block; 254, 255; 255, for_statement; 255, 256; 255, 259; 255, 264; 256, pattern_list; 256, 257; 256, 258; 257, identifier:conf_var; 258, identifier:conf_def; 259, call; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:conf_vars; 262, identifier:items; 263, argument_list; 264, block; 264, 265; 264, 271; 264, 298; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 270; 267, pattern_list; 267, 268; 267, 269; 268, identifier:conf_type; 269, identifier:conf_val; 270, identifier:conf_def; 271, if_statement; 271, 272; 271, 275; 271, 285; 272, comparison_operator:==; 272, 273; 272, 274; 273, identifier:conf_type; 274, string:'binary'; 275, block; 275, 276; 276, expression_statement; 276, 277; 277, assignment; 277, 278; 277, 279; 278, identifier:conf_val; 279, binary_operator:+; 279, 280; 279, 281; 280, string:'hex:'; 281, call; 281, 282; 281, 283; 282, identifier:hexlify; 283, argument_list; 283, 284; 284, identifier:conf_val; 285, elif_clause; 285, 286; 285, 291; 286, call; 286, 287; 286, 288; 287, identifier:isinstance; 288, argument_list; 288, 289; 288, 290; 289, identifier:conf_val; 290, identifier:str; 291, block; 291, 292; 292, expression_statement; 292, 293; 293, assignment; 293, 294; 293, 295; 294, identifier:conf_val; 295, binary_operator:%; 295, 296; 295, 297; 296, string:'"%s"'; 297, identifier:conf_val; 298, expression_statement; 298, 299; 299, call; 299, 300; 299, 303; 300, attribute; 300, 301; 300, 302; 301, identifier:output; 302, identifier:append; 303, argument_list; 303, 304; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, string:"set_variable '{}' {} {} {}"; 307, identifier:format; 308, argument_list; 308, 309; 308, 310; 308, 311; 308, 312; 309, identifier:slot; 310, identifier:conf_var; 311, identifier:conf_type; 312, identifier:conf_val; 313, comment; 314, expression_statement; 314, 315; 315, call; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:output; 318, identifier:append; 319, argument_list; 319, 320; 320, string:"back"; 321, expression_statement; 321, 322; 322, call; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:output; 325, identifier:append; 326, argument_list; 326, 327; 327, string:"reset"; 328, return_statement; 328, 329; 329, binary_operator:+; 329, 330; 329, 336; 330, call; 330, 331; 330, 334; 331, attribute; 331, 332; 331, 333; 332, string:"\n"; 333, identifier:join; 334, argument_list; 334, 335; 335, identifier:output; 336, string:'\n'
def format_snippet(sensor_graph): """Format this sensor graph as iotile command snippets. This includes commands to reset and clear previously stored sensor graphs. Args: sensor_graph (SensorGraph): the sensor graph that we want to format """ output = [] # Clear any old sensor graph output.append("disable") output.append("clear") output.append("reset") # Load in the nodes for node in sensor_graph.dump_nodes(): output.append('add_node "{}"'.format(node)) # Load in the streamers for streamer in sensor_graph.streamers: line = "add_streamer '{}' '{}' {} {} {}".format(streamer.selector, streamer.dest, streamer.automatic, streamer.format, streamer.report_type) if streamer.with_other is not None: line += ' --withother {}'.format(streamer.with_other) output.append(line) # Load all the constants for stream, value in sorted(sensor_graph.constant_database.items(), key=lambda x: x[0].encode()): output.append("set_constant '{}' {}".format(stream, value)) # Persist the sensor graph output.append("persist") output.append("back") # If we have an app tag and version set program them in app_tag = sensor_graph.metadata_database.get('app_tag') app_version = sensor_graph.metadata_database.get('app_version') if app_tag is not None: if app_version is None: app_version = "0.0" output.append("test_interface") output.append("set_version app %d --version '%s'" % (app_tag, app_version)) output.append("back") # Load in the config variables if any output.append("config_database") output.append("clear_variables") for slot, conf_vars in sensor_graph.config_database.items(): for conf_var, conf_def in conf_vars.items(): conf_type, conf_val = conf_def if conf_type == 'binary': conf_val = 'hex:' + hexlify(conf_val) elif isinstance(conf_val, str): conf_val = '"%s"' % conf_val output.append("set_variable '{}' {} {} {}".format(slot, conf_var, conf_type, conf_val)) # Restart the device to load in the new sg output.append("back") output.append("reset") return "\n".join(output) + '\n'
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_process_scan_event; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:response; 6, block; 6, 7; 6, 9; 6, 15; 6, 24; 6, 30; 6, 46; 6, 72; 6, 73; 6, 101; 6, 107; 6, 108; 6, 109; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:payload; 12, attribute; 12, 13; 12, 14; 13, identifier:response; 14, identifier:payload; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:length; 18, binary_operator:-; 18, 19; 18, 23; 19, call; 19, 20; 19, 21; 20, identifier:len; 21, argument_list; 21, 22; 22, identifier:payload; 23, integer:10; 24, if_statement; 24, 25; 24, 28; 25, comparison_operator:<; 25, 26; 25, 27; 26, identifier:length; 27, integer:0; 28, block; 28, 29; 29, return_statement; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 39; 32, pattern_list; 32, 33; 32, 34; 32, 35; 32, 36; 32, 37; 32, 38; 33, identifier:rssi; 34, identifier:packet_type; 35, identifier:sender; 36, identifier:_addr_type; 37, identifier:_bond; 38, identifier:data; 39, call; 39, 40; 39, 41; 40, identifier:unpack; 41, argument_list; 41, 42; 41, 45; 42, binary_operator:%; 42, 43; 42, 44; 43, string:"<bB6sBB%ds"; 44, identifier:length; 45, identifier:payload; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:string_address; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, string:':'; 52, identifier:join; 53, argument_list; 53, 54; 54, list_comprehension; 54, 55; 54, 60; 55, call; 55, 56; 55, 57; 56, identifier:format; 57, argument_list; 57, 58; 57, 59; 58, identifier:x; 59, string:"02X"; 60, for_in_clause; 60, 61; 60, 62; 61, identifier:x; 62, call; 62, 63; 62, 64; 63, identifier:bytearray; 64, argument_list; 64, 65; 65, subscript; 65, 66; 65, 67; 66, identifier:sender; 67, slice; 67, 68; 67, 69; 67, 70; 68, colon; 69, colon; 70, unary_operator:-; 70, 71; 71, integer:1; 72, comment; 73, if_statement; 73, 74; 73, 80; 73, 92; 74, comparison_operator:>; 74, 75; 74, 79; 75, call; 75, 76; 75, 77; 76, identifier:len; 77, argument_list; 77, 78; 78, identifier:data; 79, integer:0; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:data; 84, call; 84, 85; 84, 86; 85, identifier:bytearray; 86, argument_list; 86, 87; 87, subscript; 87, 88; 87, 89; 88, identifier:data; 89, slice; 89, 90; 89, 91; 90, integer:1; 91, colon; 92, else_clause; 92, 93; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:data; 97, call; 97, 98; 97, 99; 98, identifier:bytearray; 99, argument_list; 99, 100; 100, list:[]; 101, expression_statement; 101, 102; 102, augmented_assignment:+=; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:self; 105, identifier:_scan_event_count; 106, integer:1; 107, comment; 108, comment; 109, if_statement; 109, 110; 109, 116; 109, 204; 110, comparison_operator:in; 110, 111; 110, 112; 111, identifier:packet_type; 112, tuple; 112, 113; 112, 114; 112, 115; 113, integer:0; 114, integer:2; 115, integer:6; 116, block; 116, 117; 116, 126; 117, if_statement; 117, 118; 117, 124; 118, comparison_operator:!=; 118, 119; 118, 123; 119, call; 119, 120; 119, 121; 120, identifier:len; 121, argument_list; 121, 122; 122, identifier:data; 123, integer:31; 124, block; 124, 125; 125, return_statement; 126, if_statement; 126, 127; 126, 144; 126, 160; 126, 200; 127, boolean_operator:and; 127, 128; 127, 139; 128, boolean_operator:and; 128, 129; 128, 134; 129, comparison_operator:==; 129, 130; 129, 133; 130, subscript; 130, 131; 130, 132; 131, identifier:data; 132, integer:22; 133, integer:0xFF; 134, comparison_operator:==; 134, 135; 134, 138; 135, subscript; 135, 136; 135, 137; 136, identifier:data; 137, integer:23; 138, integer:0xC0; 139, comparison_operator:==; 139, 140; 139, 143; 140, subscript; 140, 141; 140, 142; 141, identifier:data; 142, integer:24; 143, integer:0x3; 144, block; 144, 145; 144, 151; 145, expression_statement; 145, 146; 146, augmented_assignment:+=; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:self; 149, identifier:_v1_scan_count; 150, integer:1; 151, expression_statement; 151, 152; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:self; 155, identifier:_parse_v1_advertisement; 156, argument_list; 156, 157; 156, 158; 156, 159; 157, identifier:rssi; 158, identifier:string_address; 159, identifier:data; 160, elif_clause; 160, 161; 160, 184; 161, boolean_operator:and; 161, 162; 161, 179; 162, boolean_operator:and; 162, 163; 162, 174; 163, boolean_operator:and; 163, 164; 163, 169; 164, comparison_operator:==; 164, 165; 164, 168; 165, subscript; 165, 166; 165, 167; 166, identifier:data; 167, integer:3; 168, integer:27; 169, comparison_operator:==; 169, 170; 169, 173; 170, subscript; 170, 171; 170, 172; 171, identifier:data; 172, integer:4; 173, integer:0x16; 174, comparison_operator:==; 174, 175; 174, 178; 175, subscript; 175, 176; 175, 177; 176, identifier:data; 177, integer:5; 178, integer:0xdd; 179, comparison_operator:==; 179, 180; 179, 183; 180, subscript; 180, 181; 180, 182; 181, identifier:data; 182, integer:6; 183, integer:0xfd; 184, block; 184, 185; 184, 191; 185, expression_statement; 185, 186; 186, augmented_assignment:+=; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:self; 189, identifier:_v2_scan_count; 190, integer:1; 191, expression_statement; 191, 192; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:self; 195, identifier:_parse_v2_advertisement; 196, argument_list; 196, 197; 196, 198; 196, 199; 197, identifier:rssi; 198, identifier:string_address; 199, identifier:data; 200, else_clause; 200, 201; 201, block; 201, 202; 201, 203; 202, pass_statement; 203, comment; 204, elif_clause; 204, 205; 204, 208; 205, comparison_operator:==; 205, 206; 205, 207; 206, identifier:packet_type; 207, integer:4; 208, block; 208, 209; 208, 215; 209, expression_statement; 209, 210; 210, augmented_assignment:+=; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:self; 213, identifier:_v1_scan_response_count; 214, integer:1; 215, expression_statement; 215, 216; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:self; 219, identifier:_parse_v1_scan_response; 220, argument_list; 220, 221; 220, 222; 221, identifier:string_address; 222, identifier:data
def _process_scan_event(self, response): """Parse the BLE advertisement packet. If it's an IOTile device, parse and add to the scanned devices. Then, parse advertisement and determine if it matches V1 or V2. There are two supported type of advertisements: v1: There is both an advertisement and a scan response (if active scanning is enabled). v2: There is only an advertisement and no scan response. """ payload = response.payload length = len(payload) - 10 if length < 0: return rssi, packet_type, sender, _addr_type, _bond, data = unpack("<bB6sBB%ds" % length, payload) string_address = ':'.join([format(x, "02X") for x in bytearray(sender[::-1])]) # Scan data is prepended with a length if len(data) > 0: data = bytearray(data[1:]) else: data = bytearray([]) self._scan_event_count += 1 # If this is an advertisement packet, see if its an IOTile device # packet_type = 4 is scan_response, 0, 2 and 6 are advertisements if packet_type in (0, 2, 6): if len(data) != 31: return if data[22] == 0xFF and data[23] == 0xC0 and data[24] == 0x3: self._v1_scan_count += 1 self._parse_v1_advertisement(rssi, string_address, data) elif data[3] == 27 and data[4] == 0x16 and data[5] == 0xdd and data[6] == 0xfd: self._v2_scan_count += 1 self._parse_v2_advertisement(rssi, string_address, data) else: pass # This just means the advertisement was from a non-IOTile device elif packet_type == 4: self._v1_scan_response_count += 1 self._parse_v1_scan_response(string_address, data)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_check_ver_range; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:version; 6, identifier:ver_range; 7, block; 7, 8; 7, 10; 7, 18; 7, 19; 7, 30; 7, 54; 7, 78; 7, 79; 7, 139; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 17; 12, pattern_list; 12, 13; 12, 14; 12, 15; 12, 16; 13, identifier:lower; 14, identifier:upper; 15, identifier:lower_inc; 16, identifier:upper_inc; 17, identifier:ver_range; 18, comment; 19, if_statement; 19, 20; 19, 27; 20, boolean_operator:and; 20, 21; 20, 24; 21, comparison_operator:is; 21, 22; 21, 23; 22, identifier:lower; 23, None; 24, comparison_operator:is; 24, 25; 24, 26; 25, identifier:upper; 26, None; 27, block; 27, 28; 28, return_statement; 28, 29; 29, True; 30, if_statement; 30, 31; 30, 34; 31, comparison_operator:is; 31, 32; 31, 33; 32, identifier:lower; 33, None; 34, block; 34, 35; 35, if_statement; 35, 36; 35, 41; 35, 44; 36, boolean_operator:and; 36, 37; 36, 38; 37, identifier:lower_inc; 38, comparison_operator:<; 38, 39; 38, 40; 39, identifier:version; 40, identifier:lower; 41, block; 41, 42; 42, return_statement; 42, 43; 43, False; 44, elif_clause; 44, 45; 44, 51; 45, boolean_operator:and; 45, 46; 45, 48; 46, not_operator; 46, 47; 47, identifier:lower_inc; 48, comparison_operator:<=; 48, 49; 48, 50; 49, identifier:version; 50, identifier:lower; 51, block; 51, 52; 52, return_statement; 52, 53; 53, False; 54, if_statement; 54, 55; 54, 58; 55, comparison_operator:is; 55, 56; 55, 57; 56, identifier:upper; 57, None; 58, block; 58, 59; 59, if_statement; 59, 60; 59, 65; 59, 68; 60, boolean_operator:and; 60, 61; 60, 62; 61, identifier:upper_inc; 62, comparison_operator:>; 62, 63; 62, 64; 63, identifier:version; 64, identifier:upper; 65, block; 65, 66; 66, return_statement; 66, 67; 67, False; 68, elif_clause; 68, 69; 68, 75; 69, boolean_operator:and; 69, 70; 69, 72; 70, not_operator; 70, 71; 71, identifier:upper_inc; 72, comparison_operator:>=; 72, 73; 72, 74; 73, identifier:version; 74, identifier:upper; 75, block; 75, 76; 76, return_statement; 76, 77; 77, False; 78, comment; 79, if_statement; 79, 80; 79, 83; 79, 84; 80, attribute; 80, 81; 80, 82; 81, identifier:version; 82, identifier:is_prerelease; 83, comment; 84, block; 84, 85; 84, 108; 84, 109; 85, if_statement; 85, 86; 85, 105; 86, boolean_operator:and; 86, 87; 86, 96; 87, parenthesized_expression; 87, 88; 88, boolean_operator:or; 88, 89; 88, 92; 89, comparison_operator:is; 89, 90; 89, 91; 90, identifier:lower; 91, None; 92, not_operator; 92, 93; 93, attribute; 93, 94; 93, 95; 94, identifier:lower; 95, identifier:is_prerelease; 96, parenthesized_expression; 96, 97; 97, boolean_operator:or; 97, 98; 97, 101; 98, comparison_operator:is; 98, 99; 98, 100; 99, identifier:upper; 100, None; 101, not_operator; 101, 102; 102, attribute; 102, 103; 102, 104; 103, identifier:upper; 104, identifier:is_prerelease; 105, block; 105, 106; 106, return_statement; 106, 107; 107, False; 108, comment; 109, if_statement; 109, 110; 109, 136; 110, boolean_operator:and; 110, 111; 110, 123; 110, 124; 111, parenthesized_expression; 111, 112; 112, boolean_operator:and; 112, 113; 112, 116; 113, comparison_operator:is; 113, 114; 113, 115; 114, identifier:lower; 115, None; 116, comparison_operator:!=; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:version; 119, identifier:release_tuple; 120, attribute; 120, 121; 120, 122; 121, identifier:lower; 122, identifier:release_tuple; 123, line_continuation:\; 124, parenthesized_expression; 124, 125; 125, boolean_operator:and; 125, 126; 125, 129; 126, comparison_operator:is; 126, 127; 126, 128; 127, identifier:upper; 128, None; 129, comparison_operator:!=; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:version; 132, identifier:release_tuple; 133, attribute; 133, 134; 133, 135; 134, identifier:upper; 135, identifier:release_tuple; 136, block; 136, 137; 137, return_statement; 137, 138; 138, False; 139, return_statement; 139, 140; 140, True
def _check_ver_range(self, version, ver_range): """Check if version is included in ver_range """ lower, upper, lower_inc, upper_inc = ver_range #If the range extends over everything, we automatically match if lower is None and upper is None: return True if lower is not None: if lower_inc and version < lower: return False elif not lower_inc and version <= lower: return False if upper is not None: if upper_inc and version > upper: return False elif not upper_inc and version >= upper: return False # Prereleases have special matching requirements if version.is_prerelease: # Prereleases cannot match ranges that are not defined as prereleases if (lower is None or not lower.is_prerelease) and (upper is None or not upper.is_prerelease): return False # Prereleases without the same major.minor.patch as a range end point cannot match if (lower is not None and version.release_tuple != lower.release_tuple) and \ (upper is not None and version.release_tuple != upper.release_tuple): return False return True
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:is_LaTeX; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:flist; 5, identifier:env; 6, identifier:abspath; 7, block; 7, 8; 7, 10; 7, 11; 7, 12; 7, 13; 7, 22; 7, 30; 7, 55; 7, 56; 7, 84; 7, 99; 7, 100; 7, 261; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, comment; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:savedpath; 16, call; 16, 17; 16, 18; 17, identifier:modify_env_var; 18, argument_list; 18, 19; 18, 20; 18, 21; 19, identifier:env; 20, string:'TEXINPUTS'; 21, identifier:abspath; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:paths; 25, subscript; 25, 26; 25, 29; 26, subscript; 26, 27; 26, 28; 27, identifier:env; 28, string:'ENV'; 29, string:'TEXINPUTS'; 30, if_statement; 30, 31; 30, 39; 30, 41; 31, call; 31, 32; 31, 37; 32, attribute; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:SCons; 35, identifier:Util; 36, identifier:is_List; 37, argument_list; 37, 38; 38, identifier:paths; 39, block; 39, 40; 40, pass_statement; 41, else_clause; 41, 42; 41, 43; 42, comment; 43, block; 43, 44; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:paths; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:paths; 50, identifier:split; 51, argument_list; 51, 52; 52, attribute; 52, 53; 52, 54; 53, identifier:os; 54, identifier:pathsep; 55, comment; 56, if_statement; 56, 57; 56, 60; 56, 74; 57, comparison_operator:is; 57, 58; 57, 59; 58, identifier:savedpath; 59, identifier:_null; 60, block; 60, 61; 61, try_statement; 61, 62; 61, 69; 62, block; 62, 63; 63, delete_statement; 63, 64; 64, subscript; 64, 65; 64, 68; 65, subscript; 65, 66; 65, 67; 66, identifier:env; 67, string:'ENV'; 68, string:'TEXINPUTS'; 69, except_clause; 69, 70; 69, 71; 70, identifier:KeyError; 71, block; 71, 72; 71, 73; 72, pass_statement; 73, comment; 74, else_clause; 74, 75; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 83; 78, subscript; 78, 79; 78, 82; 79, subscript; 79, 80; 79, 81; 80, identifier:env; 81, string:'ENV'; 82, string:'TEXINPUTS'; 83, identifier:savedpath; 84, if_statement; 84, 85; 84, 86; 85, identifier:Verbose; 86, block; 86, 87; 86, 93; 87, expression_statement; 87, 88; 88, call; 88, 89; 88, 90; 89, identifier:print; 90, argument_list; 90, 91; 90, 92; 91, string:"is_LaTeX search path "; 92, identifier:paths; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 96; 95, identifier:print; 96, argument_list; 96, 97; 96, 98; 97, string:"files to search :"; 98, identifier:flist; 99, comment; 100, for_statement; 100, 101; 100, 102; 100, 103; 101, identifier:f; 102, identifier:flist; 103, block; 103, 104; 103, 116; 103, 124; 103, 147; 103, 160; 103, 161; 103, 165; 103, 177; 103, 191; 103, 192; 103, 193; 103, 194; 103, 249; 104, if_statement; 104, 105; 104, 106; 105, identifier:Verbose; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 110; 109, identifier:print; 110, argument_list; 110, 111; 110, 112; 111, string:" checking for Latex source "; 112, call; 112, 113; 112, 114; 113, identifier:str; 114, argument_list; 114, 115; 115, identifier:f; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:content; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:f; 122, identifier:get_text_contents; 123, argument_list; 124, if_statement; 124, 125; 124, 131; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:LaTeX_re; 128, identifier:search; 129, argument_list; 129, 130; 130, identifier:content; 131, block; 131, 132; 131, 145; 132, if_statement; 132, 133; 132, 134; 133, identifier:Verbose; 134, block; 134, 135; 135, expression_statement; 135, 136; 136, call; 136, 137; 136, 138; 137, identifier:print; 138, argument_list; 138, 139; 139, binary_operator:%; 139, 140; 139, 141; 140, string:"file %s is a LaTeX file"; 141, call; 141, 142; 141, 143; 142, identifier:str; 143, argument_list; 143, 144; 144, identifier:f; 145, return_statement; 145, 146; 146, integer:1; 147, if_statement; 147, 148; 147, 149; 148, identifier:Verbose; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, call; 151, 152; 151, 153; 152, identifier:print; 153, argument_list; 153, 154; 154, binary_operator:%; 154, 155; 154, 156; 155, string:"file %s is not a LaTeX file"; 156, call; 156, 157; 156, 158; 157, identifier:str; 158, argument_list; 158, 159; 159, identifier:f; 160, comment; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:inc_files; 164, list:[ ]; 165, expression_statement; 165, 166; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:inc_files; 169, identifier:extend; 170, argument_list; 170, 171; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:include_re; 174, identifier:findall; 175, argument_list; 175, 176; 176, identifier:content; 177, if_statement; 177, 178; 177, 179; 178, identifier:Verbose; 179, block; 179, 180; 180, expression_statement; 180, 181; 181, call; 181, 182; 181, 183; 182, identifier:print; 183, argument_list; 183, 184; 183, 190; 184, binary_operator:%; 184, 185; 184, 186; 185, string:"files included by '%s': "; 186, call; 186, 187; 186, 188; 187, identifier:str; 188, argument_list; 188, 189; 189, identifier:f; 190, identifier:inc_files; 191, comment; 192, comment; 193, comment; 194, for_statement; 194, 195; 194, 196; 194, 197; 195, identifier:src; 196, identifier:inc_files; 197, block; 197, 198; 197, 214; 197, 215; 197, 220; 197, 229; 197, 243; 197, 244; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:srcNode; 201, call; 201, 202; 201, 203; 202, identifier:FindFile; 203, argument_list; 203, 204; 203, 205; 203, 209; 203, 210; 203, 211; 204, identifier:src; 205, list:['.tex','.ltx','.latex']; 205, 206; 205, 207; 205, 208; 206, string:'.tex'; 207, string:'.ltx'; 208, string:'.latex'; 209, identifier:paths; 210, identifier:env; 211, keyword_argument; 211, 212; 211, 213; 212, identifier:requireExt; 213, False; 214, comment; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:fileList; 218, list:[srcNode,]; 218, 219; 219, identifier:srcNode; 220, if_statement; 220, 221; 220, 222; 221, identifier:Verbose; 222, block; 222, 223; 223, expression_statement; 223, 224; 224, call; 224, 225; 224, 226; 225, identifier:print; 226, argument_list; 226, 227; 226, 228; 227, string:"FindFile found "; 228, identifier:srcNode; 229, if_statement; 229, 230; 229, 233; 230, comparison_operator:is; 230, 231; 230, 232; 231, identifier:srcNode; 232, None; 233, block; 233, 234; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 237; 236, identifier:file_test; 237, call; 237, 238; 237, 239; 238, identifier:is_LaTeX; 239, argument_list; 239, 240; 239, 241; 239, 242; 240, identifier:fileList; 241, identifier:env; 242, identifier:abspath; 243, comment; 244, if_statement; 244, 245; 244, 246; 245, identifier:file_test; 246, block; 246, 247; 247, return_statement; 247, 248; 248, identifier:file_test; 249, if_statement; 249, 250; 249, 251; 250, identifier:Verbose; 251, block; 251, 252; 252, expression_statement; 252, 253; 253, call; 253, 254; 253, 255; 254, identifier:print; 255, argument_list; 255, 256; 255, 257; 256, string:" done scanning "; 257, call; 257, 258; 257, 259; 258, identifier:str; 259, argument_list; 259, 260; 260, identifier:f; 261, return_statement; 261, 262; 262, integer:0
def is_LaTeX(flist,env,abspath): """Scan a file list to decide if it's TeX- or LaTeX-flavored.""" # We need to scan files that are included in case the # \documentclass command is in them. # get path list from both env['TEXINPUTS'] and env['ENV']['TEXINPUTS'] savedpath = modify_env_var(env, 'TEXINPUTS', abspath) paths = env['ENV']['TEXINPUTS'] if SCons.Util.is_List(paths): pass else: # Split at os.pathsep to convert into absolute path paths = paths.split(os.pathsep) # now that we have the path list restore the env if savedpath is _null: try: del env['ENV']['TEXINPUTS'] except KeyError: pass # was never set else: env['ENV']['TEXINPUTS'] = savedpath if Verbose: print("is_LaTeX search path ",paths) print("files to search :",flist) # Now that we have the search path and file list, check each one for f in flist: if Verbose: print(" checking for Latex source ",str(f)) content = f.get_text_contents() if LaTeX_re.search(content): if Verbose: print("file %s is a LaTeX file" % str(f)) return 1 if Verbose: print("file %s is not a LaTeX file" % str(f)) # now find included files inc_files = [ ] inc_files.extend( include_re.findall(content) ) if Verbose: print("files included by '%s': "%str(f),inc_files) # inc_files is list of file names as given. need to find them # using TEXINPUTS paths. # search the included files for src in inc_files: srcNode = FindFile(src,['.tex','.ltx','.latex'],paths,env,requireExt=False) # make this a list since is_LaTeX takes a list. fileList = [srcNode,] if Verbose: print("FindFile found ",srcNode) if srcNode is not None: file_test = is_LaTeX(fileList, env, abspath) # return on first file that finds latex is needed. if file_test: return file_test if Verbose: print(" done scanning ",str(f)) return 0
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:processDefines; 3, parameters; 3, 4; 4, identifier:defs; 5, block; 5, 6; 5, 8; 5, 258; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 17; 8, 181; 8, 248; 9, call; 9, 10; 9, 15; 10, attribute; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:SCons; 13, identifier:Util; 14, identifier:is_List; 15, argument_list; 15, 16; 16, identifier:defs; 17, block; 17, 18; 17, 22; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:l; 21, list:[]; 22, for_statement; 22, 23; 22, 24; 22, 25; 23, identifier:d; 24, identifier:defs; 25, block; 25, 26; 26, if_statement; 26, 27; 26, 30; 26, 32; 26, 91; 26, 145; 26, 165; 27, comparison_operator:is; 27, 28; 27, 29; 28, identifier:d; 29, None; 30, block; 30, 31; 31, continue_statement; 32, elif_clause; 32, 33; 32, 47; 33, boolean_operator:or; 33, 34; 33, 42; 34, call; 34, 35; 34, 40; 35, attribute; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:SCons; 38, identifier:Util; 39, identifier:is_List; 40, argument_list; 40, 41; 41, identifier:d; 42, call; 42, 43; 42, 44; 43, identifier:isinstance; 44, argument_list; 44, 45; 44, 46; 45, identifier:d; 46, identifier:tuple; 47, block; 47, 48; 48, if_statement; 48, 49; 48, 55; 48, 77; 49, comparison_operator:>=; 49, 50; 49, 54; 50, call; 50, 51; 50, 52; 51, identifier:len; 52, argument_list; 52, 53; 53, identifier:d; 54, integer:2; 55, block; 55, 56; 56, expression_statement; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:l; 60, identifier:append; 61, argument_list; 61, 62; 62, binary_operator:+; 62, 63; 62, 71; 63, binary_operator:+; 63, 64; 63, 70; 64, call; 64, 65; 64, 66; 65, identifier:str; 66, argument_list; 66, 67; 67, subscript; 67, 68; 67, 69; 68, identifier:d; 69, integer:0; 70, string:'='; 71, call; 71, 72; 71, 73; 72, identifier:str; 73, argument_list; 73, 74; 74, subscript; 74, 75; 74, 76; 75, identifier:d; 76, integer:1; 77, else_clause; 77, 78; 78, block; 78, 79; 79, expression_statement; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:l; 83, identifier:append; 84, argument_list; 84, 85; 85, call; 85, 86; 85, 87; 86, identifier:str; 87, argument_list; 87, 88; 88, subscript; 88, 89; 88, 90; 89, identifier:d; 90, integer:0; 91, elif_clause; 91, 92; 91, 100; 92, call; 92, 93; 92, 98; 93, attribute; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:SCons; 96, identifier:Util; 97, identifier:is_Dict; 98, argument_list; 98, 99; 99, identifier:d; 100, block; 100, 101; 101, for_statement; 101, 102; 101, 105; 101, 110; 102, pattern_list; 102, 103; 102, 104; 103, identifier:macro; 104, identifier:value; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:d; 108, identifier:items; 109, argument_list; 110, block; 110, 111; 111, if_statement; 111, 112; 111, 115; 111, 133; 112, comparison_operator:is; 112, 113; 112, 114; 113, identifier:value; 114, None; 115, block; 115, 116; 116, expression_statement; 116, 117; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:l; 120, identifier:append; 121, argument_list; 121, 122; 122, binary_operator:+; 122, 123; 122, 129; 123, binary_operator:+; 123, 124; 123, 128; 124, call; 124, 125; 124, 126; 125, identifier:str; 126, argument_list; 126, 127; 127, identifier:macro; 128, string:'='; 129, call; 129, 130; 129, 131; 130, identifier:str; 131, argument_list; 131, 132; 132, identifier:value; 133, else_clause; 133, 134; 134, block; 134, 135; 135, expression_statement; 135, 136; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:l; 139, identifier:append; 140, argument_list; 140, 141; 141, call; 141, 142; 141, 143; 142, identifier:str; 143, argument_list; 143, 144; 144, identifier:macro; 145, elif_clause; 145, 146; 145, 154; 146, call; 146, 147; 146, 152; 147, attribute; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:SCons; 150, identifier:Util; 151, identifier:is_String; 152, argument_list; 152, 153; 153, identifier:d; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:l; 159, identifier:append; 160, argument_list; 160, 161; 161, call; 161, 162; 161, 163; 162, identifier:str; 163, argument_list; 163, 164; 164, identifier:d; 165, else_clause; 165, 166; 166, block; 166, 167; 167, raise_statement; 167, 168; 168, call; 168, 169; 168, 174; 169, attribute; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:SCons; 172, identifier:Errors; 173, identifier:UserError; 174, argument_list; 174, 175; 175, binary_operator:%; 175, 176; 175, 177; 176, string:"DEFINE %s is not a list, dict, string or None."; 177, call; 177, 178; 177, 179; 178, identifier:repr; 179, argument_list; 179, 180; 180, identifier:d; 181, elif_clause; 181, 182; 181, 190; 181, 191; 181, 192; 181, 193; 181, 194; 181, 195; 181, 196; 182, call; 182, 183; 182, 188; 183, attribute; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:SCons; 186, identifier:Util; 187, identifier:is_Dict; 188, argument_list; 188, 189; 189, identifier:defs; 190, comment; 191, comment; 192, comment; 193, comment; 194, comment; 195, comment; 196, block; 196, 197; 196, 201; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:l; 200, list:[]; 201, for_statement; 201, 202; 201, 205; 201, 213; 202, pattern_list; 202, 203; 202, 204; 203, identifier:k; 204, identifier:v; 205, call; 205, 206; 205, 207; 206, identifier:sorted; 207, argument_list; 207, 208; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:defs; 211, identifier:items; 212, argument_list; 213, block; 213, 214; 214, if_statement; 214, 215; 214, 218; 214, 229; 215, comparison_operator:is; 215, 216; 215, 217; 216, identifier:v; 217, None; 218, block; 218, 219; 219, expression_statement; 219, 220; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:l; 223, identifier:append; 224, argument_list; 224, 225; 225, call; 225, 226; 225, 227; 226, identifier:str; 227, argument_list; 227, 228; 228, identifier:k; 229, else_clause; 229, 230; 230, block; 230, 231; 231, expression_statement; 231, 232; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:l; 235, identifier:append; 236, argument_list; 236, 237; 237, binary_operator:+; 237, 238; 237, 244; 238, binary_operator:+; 238, 239; 238, 243; 239, call; 239, 240; 239, 241; 240, identifier:str; 241, argument_list; 241, 242; 242, identifier:k; 243, string:'='; 244, call; 244, 245; 244, 246; 245, identifier:str; 246, argument_list; 246, 247; 247, identifier:v; 248, else_clause; 248, 249; 249, block; 249, 250; 250, expression_statement; 250, 251; 251, assignment; 251, 252; 251, 253; 252, identifier:l; 253, list:[str(defs)]; 253, 254; 254, call; 254, 255; 254, 256; 255, identifier:str; 256, argument_list; 256, 257; 257, identifier:defs; 258, return_statement; 258, 259; 259, identifier:l
def processDefines(defs): """process defines, resolving strings, lists, dictionaries, into a list of strings """ if SCons.Util.is_List(defs): l = [] for d in defs: if d is None: continue elif SCons.Util.is_List(d) or isinstance(d, tuple): if len(d) >= 2: l.append(str(d[0]) + '=' + str(d[1])) else: l.append(str(d[0])) elif SCons.Util.is_Dict(d): for macro,value in d.items(): if value is not None: l.append(str(macro) + '=' + str(value)) else: l.append(str(macro)) elif SCons.Util.is_String(d): l.append(str(d)) else: raise SCons.Errors.UserError("DEFINE %s is not a list, dict, string or None."%repr(d)) elif SCons.Util.is_Dict(defs): # The items in a dictionary are stored in random order, but # if the order of the command-line options changes from # invocation to invocation, then the signature of the command # line will change and we'll get random unnecessary rebuilds. # Consequently, we have to sort the keys to ensure a # consistent order... l = [] for k,v in sorted(defs.items()): if v is None: l.append(str(k)) else: l.append(str(k) + '=' + str(v)) else: l = [str(defs)] return l
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:local_services; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 26; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 17; 9, not_operator; 9, 10; 10, call; 10, 11; 10, 16; 11, attribute; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:self; 14, identifier:_loop; 15, identifier:inside_loop; 16, argument_list; 17, block; 17, 18; 18, expression_statement; 18, 19; 19, call; 19, 20; 19, 25; 20, attribute; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:_state_lock; 24, identifier:acquire; 25, argument_list; 26, try_statement; 26, 27; 26, 55; 27, block; 27, 28; 28, return_statement; 28, 29; 29, call; 29, 30; 29, 31; 30, identifier:sorted; 31, argument_list; 31, 32; 31, 47; 32, list_comprehension; 32, 33; 32, 36; 33, tuple; 33, 34; 33, 35; 34, identifier:index; 35, identifier:name; 36, for_in_clause; 36, 37; 36, 40; 37, pattern_list; 37, 38; 37, 39; 38, identifier:index; 39, identifier:name; 40, call; 40, 41; 40, 46; 41, attribute; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:self; 44, identifier:_name_map; 45, identifier:items; 46, argument_list; 47, keyword_argument; 47, 48; 47, 49; 48, identifier:key; 49, lambda; 49, 50; 49, 52; 50, lambda_parameters; 50, 51; 51, identifier:element; 52, subscript; 52, 53; 52, 54; 53, identifier:element; 54, integer:0; 55, finally_clause; 55, 56; 56, block; 56, 57; 57, if_statement; 57, 58; 57, 66; 58, not_operator; 58, 59; 59, call; 59, 60; 59, 65; 60, attribute; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:self; 63, identifier:_loop; 64, identifier:inside_loop; 65, argument_list; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, call; 68, 69; 68, 74; 69, attribute; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:_state_lock; 73, identifier:release; 74, argument_list
def local_services(self): """Get a list of id, name pairs for all of the known synced services. This method is safe to call outside of the background event loop without any race condition. Internally it uses a thread-safe mutex to protect the local copies of supervisor data and ensure that it cannot change while this method is iterating over it. Returns: list (id, name): A list of tuples with id and service name sorted by id from low to high """ if not self._loop.inside_loop(): self._state_lock.acquire() try: return sorted([(index, name) for index, name in self._name_map.items()], key=lambda element: element[0]) finally: if not self._loop.inside_loop(): self._state_lock.release()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:generate; 3, parameters; 3, 4; 4, identifier:env; 5, block; 5, 6; 5, 8; 5, 16; 5, 24; 5, 32; 5, 201; 5, 202; 5, 234; 5, 249; 5, 289; 5, 329; 5, 330; 5, 338; 5, 346; 5, 359; 5, 366; 5, 373; 5, 374; 5, 375; 5, 376; 5, 377; 5, 378; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:CLVar; 11, attribute; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:SCons; 14, identifier:Util; 15, identifier:CLVar; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:Action; 19, attribute; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:SCons; 22, identifier:Action; 23, identifier:Action; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:Builder; 27, attribute; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:SCons; 30, identifier:Builder; 31, identifier:Builder; 32, expression_statement; 32, 33; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:env; 36, identifier:SetDefault; 37, argument_list; 37, 38; 37, 44; 37, 55; 37, 66; 37, 77; 37, 88; 37, 99; 37, 102; 37, 103; 37, 106; 37, 107; 37, 108; 37, 109; 37, 115; 37, 121; 37, 127; 37, 133; 37, 134; 37, 137; 37, 140; 37, 143; 37, 146; 37, 149; 37, 152; 37, 155; 37, 158; 37, 161; 37, 162; 37, 163; 37, 164; 37, 181; 37, 182; 37, 183; 37, 187; 37, 188; 37, 189; 38, keyword_argument; 38, 39; 38, 40; 39, identifier:QTDIR; 40, call; 40, 41; 40, 42; 41, identifier:_detect; 42, argument_list; 42, 43; 43, identifier:env; 44, keyword_argument; 44, 45; 44, 46; 45, identifier:QT_BINPATH; 46, call; 46, 47; 46, 52; 47, attribute; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:os; 50, identifier:path; 51, identifier:join; 52, argument_list; 52, 53; 52, 54; 53, string:'$QTDIR'; 54, string:'bin'; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:QT_CPPPATH; 57, call; 57, 58; 57, 63; 58, attribute; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:os; 61, identifier:path; 62, identifier:join; 63, argument_list; 63, 64; 63, 65; 64, string:'$QTDIR'; 65, string:'include'; 66, keyword_argument; 66, 67; 66, 68; 67, identifier:QT_LIBPATH; 68, call; 68, 69; 68, 74; 69, attribute; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:os; 72, identifier:path; 73, identifier:join; 74, argument_list; 74, 75; 74, 76; 75, string:'$QTDIR'; 76, string:'lib'; 77, keyword_argument; 77, 78; 77, 79; 78, identifier:QT_MOC; 79, call; 79, 80; 79, 85; 80, attribute; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:os; 83, identifier:path; 84, identifier:join; 85, argument_list; 85, 86; 85, 87; 86, string:'$QT_BINPATH'; 87, string:'moc'; 88, keyword_argument; 88, 89; 88, 90; 89, identifier:QT_UIC; 90, call; 90, 91; 90, 96; 91, attribute; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:os; 94, identifier:path; 95, identifier:join; 96, argument_list; 96, 97; 96, 98; 97, string:'$QT_BINPATH'; 98, string:'uic'; 99, keyword_argument; 99, 100; 99, 101; 100, identifier:QT_LIB; 101, string:'qt'; 102, comment; 103, keyword_argument; 103, 104; 103, 105; 104, identifier:QT_AUTOSCAN; 105, integer:1; 106, comment; 107, comment; 108, comment; 109, keyword_argument; 109, 110; 109, 111; 110, identifier:QT_UICIMPLFLAGS; 111, call; 111, 112; 111, 113; 112, identifier:CLVar; 113, argument_list; 113, 114; 114, string:''; 115, keyword_argument; 115, 116; 115, 117; 116, identifier:QT_UICDECLFLAGS; 117, call; 117, 118; 117, 119; 118, identifier:CLVar; 119, argument_list; 119, 120; 120, string:''; 121, keyword_argument; 121, 122; 121, 123; 122, identifier:QT_MOCFROMHFLAGS; 123, call; 123, 124; 123, 125; 124, identifier:CLVar; 125, argument_list; 125, 126; 126, string:''; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:QT_MOCFROMCXXFLAGS; 129, call; 129, 130; 129, 131; 130, identifier:CLVar; 131, argument_list; 131, 132; 132, string:'-i'; 133, comment; 134, keyword_argument; 134, 135; 134, 136; 135, identifier:QT_UICDECLPREFIX; 136, string:''; 137, keyword_argument; 137, 138; 137, 139; 138, identifier:QT_UICDECLSUFFIX; 139, string:'.h'; 140, keyword_argument; 140, 141; 140, 142; 141, identifier:QT_UICIMPLPREFIX; 142, string:'uic_'; 143, keyword_argument; 143, 144; 143, 145; 144, identifier:QT_UICIMPLSUFFIX; 145, string:'$CXXFILESUFFIX'; 146, keyword_argument; 146, 147; 146, 148; 147, identifier:QT_MOCHPREFIX; 148, string:'moc_'; 149, keyword_argument; 149, 150; 149, 151; 150, identifier:QT_MOCHSUFFIX; 151, string:'$CXXFILESUFFIX'; 152, keyword_argument; 152, 153; 152, 154; 153, identifier:QT_MOCCXXPREFIX; 154, string:''; 155, keyword_argument; 155, 156; 155, 157; 156, identifier:QT_MOCCXXSUFFIX; 157, string:'.moc'; 158, keyword_argument; 158, 159; 158, 160; 159, identifier:QT_UISUFFIX; 160, string:'.ui'; 161, comment; 162, comment; 163, comment; 164, keyword_argument; 164, 165; 164, 166; 165, identifier:QT_UICCOM; 166, list:[ CLVar('$QT_UIC $QT_UICDECLFLAGS -o ${TARGETS[0]} $SOURCE'), CLVar('$QT_UIC $QT_UICIMPLFLAGS -impl ${TARGETS[0].file} ' '-o ${TARGETS[1]} $SOURCE'), CLVar('$QT_MOC $QT_MOCFROMHFLAGS -o ${TARGETS[2]} ${TARGETS[0]}')]; 166, 167; 166, 171; 166, 177; 167, call; 167, 168; 167, 169; 168, identifier:CLVar; 169, argument_list; 169, 170; 170, string:'$QT_UIC $QT_UICDECLFLAGS -o ${TARGETS[0]} $SOURCE'; 171, call; 171, 172; 171, 173; 172, identifier:CLVar; 173, argument_list; 173, 174; 174, concatenated_string; 174, 175; 174, 176; 175, string:'$QT_UIC $QT_UICIMPLFLAGS -impl ${TARGETS[0].file} '; 176, string:'-o ${TARGETS[1]} $SOURCE'; 177, call; 177, 178; 177, 179; 178, identifier:CLVar; 179, argument_list; 179, 180; 180, string:'$QT_MOC $QT_MOCFROMHFLAGS -o ${TARGETS[2]} ${TARGETS[0]}'; 181, comment; 182, comment; 183, keyword_argument; 183, 184; 183, 185; 184, identifier:QT_MOCFROMHCOM; 185, parenthesized_expression; 185, 186; 186, string:'$QT_MOC $QT_MOCFROMHFLAGS -o ${TARGETS[0]} $SOURCE'; 187, comment; 188, comment; 189, keyword_argument; 189, 190; 189, 191; 190, identifier:QT_MOCFROMCXXCOM; 191, list:[ CLVar('$QT_MOC $QT_MOCFROMCXXFLAGS -o ${TARGETS[0]} $SOURCE'), Action(checkMocIncluded,None)]; 191, 192; 191, 196; 192, call; 192, 193; 192, 194; 193, identifier:CLVar; 194, argument_list; 194, 195; 195, string:'$QT_MOC $QT_MOCFROMCXXFLAGS -o ${TARGETS[0]} $SOURCE'; 196, call; 196, 197; 196, 198; 197, identifier:Action; 198, argument_list; 198, 199; 198, 200; 199, identifier:checkMocIncluded; 200, None; 201, comment; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 205; 204, identifier:uicBld; 205, call; 205, 206; 205, 207; 206, identifier:Builder; 207, argument_list; 207, 208; 207, 219; 207, 222; 207, 225; 207, 228; 207, 231; 208, keyword_argument; 208, 209; 208, 210; 209, identifier:action; 210, call; 210, 211; 210, 216; 211, attribute; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:SCons; 214, identifier:Action; 215, identifier:Action; 216, argument_list; 216, 217; 216, 218; 217, string:'$QT_UICCOM'; 218, string:'$QT_UICCOMSTR'; 219, keyword_argument; 219, 220; 219, 221; 220, identifier:emitter; 221, identifier:uicEmitter; 222, keyword_argument; 222, 223; 222, 224; 223, identifier:src_suffix; 224, string:'$QT_UISUFFIX'; 225, keyword_argument; 225, 226; 225, 227; 226, identifier:suffix; 227, string:'$QT_UICDECLSUFFIX'; 228, keyword_argument; 228, 229; 228, 230; 229, identifier:prefix; 230, string:'$QT_UICDECLPREFIX'; 231, keyword_argument; 231, 232; 231, 233; 232, identifier:source_scanner; 233, identifier:uicScanner; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 237; 236, identifier:mocBld; 237, call; 237, 238; 237, 239; 238, identifier:Builder; 239, argument_list; 239, 240; 239, 243; 239, 246; 240, keyword_argument; 240, 241; 240, 242; 241, identifier:action; 242, dictionary; 243, keyword_argument; 243, 244; 243, 245; 244, identifier:prefix; 245, dictionary; 246, keyword_argument; 246, 247; 246, 248; 247, identifier:suffix; 248, dictionary; 249, for_statement; 249, 250; 249, 251; 249, 252; 250, identifier:h; 251, identifier:header_extensions; 252, block; 252, 253; 252, 265; 252, 273; 252, 281; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 256; 255, identifier:act; 256, call; 256, 257; 256, 262; 257, attribute; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:SCons; 260, identifier:Action; 261, identifier:Action; 262, argument_list; 262, 263; 262, 264; 263, string:'$QT_MOCFROMHCOM'; 264, string:'$QT_MOCFROMHCOMSTR'; 265, expression_statement; 265, 266; 266, call; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:mocBld; 269, identifier:add_action; 270, argument_list; 270, 271; 270, 272; 271, identifier:h; 272, identifier:act; 273, expression_statement; 273, 274; 274, assignment; 274, 275; 274, 280; 275, subscript; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:mocBld; 278, identifier:prefix; 279, identifier:h; 280, string:'$QT_MOCHPREFIX'; 281, expression_statement; 281, 282; 282, assignment; 282, 283; 282, 288; 283, subscript; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:mocBld; 286, identifier:suffix; 287, identifier:h; 288, string:'$QT_MOCHSUFFIX'; 289, for_statement; 289, 290; 289, 291; 289, 292; 290, identifier:cxx; 291, identifier:cxx_suffixes; 292, block; 292, 293; 292, 305; 292, 313; 292, 321; 293, expression_statement; 293, 294; 294, assignment; 294, 295; 294, 296; 295, identifier:act; 296, call; 296, 297; 296, 302; 297, attribute; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:SCons; 300, identifier:Action; 301, identifier:Action; 302, argument_list; 302, 303; 302, 304; 303, string:'$QT_MOCFROMCXXCOM'; 304, string:'$QT_MOCFROMCXXCOMSTR'; 305, expression_statement; 305, 306; 306, call; 306, 307; 306, 310; 307, attribute; 307, 308; 307, 309; 308, identifier:mocBld; 309, identifier:add_action; 310, argument_list; 310, 311; 310, 312; 311, identifier:cxx; 312, identifier:act; 313, expression_statement; 313, 314; 314, assignment; 314, 315; 314, 320; 315, subscript; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:mocBld; 318, identifier:prefix; 319, identifier:cxx; 320, string:'$QT_MOCCXXPREFIX'; 321, expression_statement; 321, 322; 322, assignment; 322, 323; 322, 328; 323, subscript; 323, 324; 323, 327; 324, attribute; 324, 325; 324, 326; 325, identifier:mocBld; 326, identifier:suffix; 327, identifier:cxx; 328, string:'$QT_MOCCXXSUFFIX'; 329, comment; 330, expression_statement; 330, 331; 331, assignment; 331, 332; 331, 337; 332, subscript; 332, 333; 332, 336; 333, subscript; 333, 334; 333, 335; 334, identifier:env; 335, string:'BUILDERS'; 336, string:'Uic'; 337, identifier:uicBld; 338, expression_statement; 338, 339; 339, assignment; 339, 340; 339, 345; 340, subscript; 340, 341; 340, 344; 341, subscript; 341, 342; 341, 343; 342, identifier:env; 343, string:'BUILDERS'; 344, string:'Moc'; 345, identifier:mocBld; 346, expression_statement; 346, 347; 347, assignment; 347, 348; 347, 351; 348, pattern_list; 348, 349; 348, 350; 349, identifier:static_obj; 350, identifier:shared_obj; 351, call; 351, 352; 351, 357; 352, attribute; 352, 353; 352, 356; 353, attribute; 353, 354; 353, 355; 354, identifier:SCons; 355, identifier:Tool; 356, identifier:createObjBuilders; 357, argument_list; 357, 358; 358, identifier:env; 359, expression_statement; 359, 360; 360, call; 360, 361; 360, 364; 361, attribute; 361, 362; 361, 363; 362, identifier:static_obj; 363, identifier:add_src_builder; 364, argument_list; 364, 365; 365, string:'Uic'; 366, expression_statement; 366, 367; 367, call; 367, 368; 367, 371; 368, attribute; 368, 369; 368, 370; 369, identifier:shared_obj; 370, identifier:add_src_builder; 371, argument_list; 371, 372; 372, string:'Uic'; 373, comment; 374, comment; 375, comment; 376, comment; 377, comment; 378, expression_statement; 378, 379; 379, call; 379, 380; 379, 383; 380, attribute; 380, 381; 380, 382; 381, identifier:env; 382, identifier:AppendUnique; 383, argument_list; 383, 384; 383, 388; 383, 392; 383, 396; 383, 400; 383, 401; 383, 405; 383, 409; 384, keyword_argument; 384, 385; 384, 386; 385, identifier:PROGEMITTER; 386, list:[AutomocStatic]; 386, 387; 387, identifier:AutomocStatic; 388, keyword_argument; 388, 389; 388, 390; 389, identifier:SHLIBEMITTER; 390, list:[AutomocShared]; 390, 391; 391, identifier:AutomocShared; 392, keyword_argument; 392, 393; 392, 394; 393, identifier:LDMODULEEMITTER; 394, list:[AutomocShared]; 394, 395; 395, identifier:AutomocShared; 396, keyword_argument; 396, 397; 396, 398; 397, identifier:LIBEMITTER; 398, list:[AutomocStatic]; 398, 399; 399, identifier:AutomocStatic; 400, comment; 401, keyword_argument; 401, 402; 401, 403; 402, identifier:CPPPATH; 403, list:["$QT_CPPPATH"]; 403, 404; 404, string:"$QT_CPPPATH"; 405, keyword_argument; 405, 406; 405, 407; 406, identifier:LIBPATH; 407, list:["$QT_LIBPATH"]; 407, 408; 408, string:"$QT_LIBPATH"; 409, keyword_argument; 409, 410; 409, 411; 410, identifier:LIBS; 411, list:['$QT_LIB']; 411, 412; 412, string:'$QT_LIB'
def generate(env): """Add Builders and construction variables for qt to an Environment.""" CLVar = SCons.Util.CLVar Action = SCons.Action.Action Builder = SCons.Builder.Builder env.SetDefault(QTDIR = _detect(env), QT_BINPATH = os.path.join('$QTDIR', 'bin'), QT_CPPPATH = os.path.join('$QTDIR', 'include'), QT_LIBPATH = os.path.join('$QTDIR', 'lib'), QT_MOC = os.path.join('$QT_BINPATH','moc'), QT_UIC = os.path.join('$QT_BINPATH','uic'), QT_LIB = 'qt', # may be set to qt-mt QT_AUTOSCAN = 1, # scan for moc'able sources # Some QT specific flags. I don't expect someone wants to # manipulate those ... QT_UICIMPLFLAGS = CLVar(''), QT_UICDECLFLAGS = CLVar(''), QT_MOCFROMHFLAGS = CLVar(''), QT_MOCFROMCXXFLAGS = CLVar('-i'), # suffixes/prefixes for the headers / sources to generate QT_UICDECLPREFIX = '', QT_UICDECLSUFFIX = '.h', QT_UICIMPLPREFIX = 'uic_', QT_UICIMPLSUFFIX = '$CXXFILESUFFIX', QT_MOCHPREFIX = 'moc_', QT_MOCHSUFFIX = '$CXXFILESUFFIX', QT_MOCCXXPREFIX = '', QT_MOCCXXSUFFIX = '.moc', QT_UISUFFIX = '.ui', # Commands for the qt support ... # command to generate header, implementation and moc-file # from a .ui file QT_UICCOM = [ CLVar('$QT_UIC $QT_UICDECLFLAGS -o ${TARGETS[0]} $SOURCE'), CLVar('$QT_UIC $QT_UICIMPLFLAGS -impl ${TARGETS[0].file} ' '-o ${TARGETS[1]} $SOURCE'), CLVar('$QT_MOC $QT_MOCFROMHFLAGS -o ${TARGETS[2]} ${TARGETS[0]}')], # command to generate meta object information for a class # declarated in a header QT_MOCFROMHCOM = ( '$QT_MOC $QT_MOCFROMHFLAGS -o ${TARGETS[0]} $SOURCE'), # command to generate meta object information for a class # declarated in a cpp file QT_MOCFROMCXXCOM = [ CLVar('$QT_MOC $QT_MOCFROMCXXFLAGS -o ${TARGETS[0]} $SOURCE'), Action(checkMocIncluded,None)]) # ... and the corresponding builders uicBld = Builder(action=SCons.Action.Action('$QT_UICCOM', '$QT_UICCOMSTR'), emitter=uicEmitter, src_suffix='$QT_UISUFFIX', suffix='$QT_UICDECLSUFFIX', prefix='$QT_UICDECLPREFIX', source_scanner=uicScanner) mocBld = Builder(action={}, prefix={}, suffix={}) for h in header_extensions: act = SCons.Action.Action('$QT_MOCFROMHCOM', '$QT_MOCFROMHCOMSTR') mocBld.add_action(h, act) mocBld.prefix[h] = '$QT_MOCHPREFIX' mocBld.suffix[h] = '$QT_MOCHSUFFIX' for cxx in cxx_suffixes: act = SCons.Action.Action('$QT_MOCFROMCXXCOM', '$QT_MOCFROMCXXCOMSTR') mocBld.add_action(cxx, act) mocBld.prefix[cxx] = '$QT_MOCCXXPREFIX' mocBld.suffix[cxx] = '$QT_MOCCXXSUFFIX' # register the builders env['BUILDERS']['Uic'] = uicBld env['BUILDERS']['Moc'] = mocBld static_obj, shared_obj = SCons.Tool.createObjBuilders(env) static_obj.add_src_builder('Uic') shared_obj.add_src_builder('Uic') # We use the emitters of Program / StaticLibrary / SharedLibrary # to scan for moc'able files # We can't refer to the builders directly, we have to fetch them # as Environment attributes because that sets them up to be called # correctly later by our emitter. env.AppendUnique(PROGEMITTER =[AutomocStatic], SHLIBEMITTER=[AutomocShared], LDMODULEEMITTER=[AutomocShared], LIBEMITTER =[AutomocStatic], # Of course, we need to link against the qt libraries CPPPATH=["$QT_CPPPATH"], LIBPATH=["$QT_LIBPATH"], LIBS=['$QT_LIB'])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:emit_rmic_classes; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:target; 5, identifier:source; 6, identifier:env; 7, block; 7, 8; 7, 10; 7, 20; 7, 29; 7, 67; 7, 80; 7, 105; 7, 109; 7, 216; 7, 221; 7, 238; 7, 242; 7, 299; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:class_suffix; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:env; 16, identifier:get; 17, argument_list; 17, 18; 17, 19; 18, string:'JAVACLASSSUFFIX'; 19, string:'.class'; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:classdir; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:env; 26, identifier:get; 27, argument_list; 27, 28; 28, string:'JAVACLASSDIR'; 29, if_statement; 29, 30; 29, 32; 30, not_operator; 30, 31; 31, identifier:classdir; 32, block; 32, 33; 33, try_statement; 33, 34; 33, 41; 33, 48; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:s; 38, subscript; 38, 39; 38, 40; 39, identifier:source; 40, integer:0; 41, except_clause; 41, 42; 41, 43; 42, identifier:IndexError; 43, block; 43, 44; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:classdir; 47, string:'.'; 48, else_clause; 48, 49; 49, block; 49, 50; 50, try_statement; 50, 51; 50, 60; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:classdir; 55, attribute; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:s; 58, identifier:attributes; 59, identifier:java_classdir; 60, except_clause; 60, 61; 60, 62; 61, identifier:AttributeError; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:classdir; 66, string:'.'; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:classdir; 70, call; 70, 71; 70, 79; 71, attribute; 71, 72; 71, 78; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:env; 75, identifier:Dir; 76, argument_list; 76, 77; 77, identifier:classdir; 78, identifier:rdir; 79, argument_list; 80, if_statement; 80, 81; 80, 87; 80, 92; 81, comparison_operator:==; 81, 82; 81, 86; 82, call; 82, 83; 82, 84; 83, identifier:str; 84, argument_list; 84, 85; 85, identifier:classdir; 86, string:'.'; 87, block; 87, 88; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:c_; 91, None; 92, else_clause; 92, 93; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:c_; 97, binary_operator:+; 97, 98; 97, 102; 98, call; 98, 99; 98, 100; 99, identifier:str; 100, argument_list; 100, 101; 101, identifier:classdir; 102, attribute; 102, 103; 102, 104; 103, identifier:os; 104, identifier:sep; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:slist; 108, list:[]; 109, for_statement; 109, 110; 109, 111; 109, 112; 110, identifier:src; 111, identifier:source; 112, block; 112, 113; 112, 185; 112, 193; 112, 201; 112, 209; 113, try_statement; 113, 114; 113, 123; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:classname; 118, attribute; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:src; 121, identifier:attributes; 122, identifier:java_classname; 123, except_clause; 123, 124; 123, 125; 124, identifier:AttributeError; 125, block; 125, 126; 125, 133; 125, 158; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:classname; 129, call; 129, 130; 129, 131; 130, identifier:str; 131, argument_list; 131, 132; 132, identifier:src; 133, if_statement; 133, 134; 133, 146; 134, boolean_operator:and; 134, 135; 134, 136; 135, identifier:c_; 136, comparison_operator:==; 136, 137; 136, 145; 137, subscript; 137, 138; 137, 139; 138, identifier:classname; 139, slice; 139, 140; 139, 141; 140, colon; 141, call; 141, 142; 141, 143; 142, identifier:len; 143, argument_list; 143, 144; 144, identifier:c_; 145, identifier:c_; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:classname; 150, subscript; 150, 151; 150, 152; 151, identifier:classname; 152, slice; 152, 153; 152, 157; 153, call; 153, 154; 153, 155; 154, identifier:len; 155, argument_list; 155, 156; 156, identifier:c_; 157, colon; 158, if_statement; 158, 159; 158, 172; 159, boolean_operator:and; 159, 160; 159, 161; 160, identifier:class_suffix; 161, comparison_operator:==; 161, 162; 161, 171; 162, subscript; 162, 163; 162, 164; 163, identifier:classname; 164, slice; 164, 165; 164, 166; 165, colon; 166, unary_operator:-; 166, 167; 167, call; 167, 168; 167, 169; 168, identifier:len; 169, argument_list; 169, 170; 170, identifier:class_suffix; 171, identifier:class_suffix; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:classname; 176, subscript; 176, 177; 176, 178; 177, identifier:classname; 178, slice; 178, 179; 178, 184; 179, unary_operator:-; 179, 180; 180, call; 180, 181; 180, 182; 181, identifier:len; 182, argument_list; 182, 183; 183, identifier:class_suffix; 184, colon; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:s; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:src; 191, identifier:rfile; 192, argument_list; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 200; 195, attribute; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:s; 198, identifier:attributes; 199, identifier:java_classdir; 200, identifier:classdir; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 208; 203, attribute; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:s; 206, identifier:attributes; 207, identifier:java_classname; 208, identifier:classname; 209, expression_statement; 209, 210; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:slist; 213, identifier:append; 214, argument_list; 214, 215; 215, identifier:s; 216, expression_statement; 216, 217; 217, assignment; 217, 218; 217, 219; 218, identifier:stub_suffixes; 219, list:['_Stub']; 219, 220; 220, string:'_Stub'; 221, if_statement; 221, 222; 221, 230; 222, comparison_operator:==; 222, 223; 222, 229; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:env; 226, identifier:get; 227, argument_list; 227, 228; 228, string:'JAVAVERSION'; 229, string:'1.4'; 230, block; 230, 231; 231, expression_statement; 231, 232; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:stub_suffixes; 235, identifier:append; 236, argument_list; 236, 237; 237, string:'_Skel'; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 241; 240, identifier:tlist; 241, list:[]; 242, for_statement; 242, 243; 242, 244; 242, 245; 243, identifier:s; 244, identifier:source; 245, block; 245, 246; 246, for_statement; 246, 247; 246, 248; 246, 249; 247, identifier:suff; 248, identifier:stub_suffixes; 249, block; 249, 250; 249, 271; 249, 282; 249, 292; 250, expression_statement; 250, 251; 251, assignment; 251, 252; 251, 253; 252, identifier:fname; 253, binary_operator:+; 253, 254; 253, 270; 254, binary_operator:+; 254, 255; 254, 268; 254, 269; 255, call; 255, 256; 255, 263; 256, attribute; 256, 257; 256, 262; 257, attribute; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:s; 260, identifier:attributes; 261, identifier:java_classname; 262, identifier:replace; 263, argument_list; 263, 264; 263, 265; 264, string:'.'; 265, attribute; 265, 266; 265, 267; 266, identifier:os; 267, identifier:sep; 268, line_continuation:\; 269, identifier:suff; 270, identifier:class_suffix; 271, expression_statement; 271, 272; 272, assignment; 272, 273; 272, 274; 273, identifier:t; 274, call; 274, 275; 274, 280; 275, attribute; 275, 276; 275, 279; 276, subscript; 276, 277; 276, 278; 277, identifier:target; 278, integer:0; 279, identifier:File; 280, argument_list; 280, 281; 281, identifier:fname; 282, expression_statement; 282, 283; 283, assignment; 283, 284; 283, 289; 284, attribute; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:t; 287, identifier:attributes; 288, identifier:java_lookupdir; 289, subscript; 289, 290; 289, 291; 290, identifier:target; 291, integer:0; 292, expression_statement; 292, 293; 293, call; 293, 294; 293, 297; 294, attribute; 294, 295; 294, 296; 295, identifier:tlist; 296, identifier:append; 297, argument_list; 297, 298; 298, identifier:t; 299, return_statement; 299, 300; 300, expression_list; 300, 301; 300, 302; 301, identifier:tlist; 302, identifier:source
def emit_rmic_classes(target, source, env): """Create and return lists of Java RMI stub and skeleton class files to be created from a set of class files. """ class_suffix = env.get('JAVACLASSSUFFIX', '.class') classdir = env.get('JAVACLASSDIR') if not classdir: try: s = source[0] except IndexError: classdir = '.' else: try: classdir = s.attributes.java_classdir except AttributeError: classdir = '.' classdir = env.Dir(classdir).rdir() if str(classdir) == '.': c_ = None else: c_ = str(classdir) + os.sep slist = [] for src in source: try: classname = src.attributes.java_classname except AttributeError: classname = str(src) if c_ and classname[:len(c_)] == c_: classname = classname[len(c_):] if class_suffix and classname[:-len(class_suffix)] == class_suffix: classname = classname[-len(class_suffix):] s = src.rfile() s.attributes.java_classdir = classdir s.attributes.java_classname = classname slist.append(s) stub_suffixes = ['_Stub'] if env.get('JAVAVERSION') == '1.4': stub_suffixes.append('_Skel') tlist = [] for s in source: for suff in stub_suffixes: fname = s.attributes.java_classname.replace('.', os.sep) + \ suff + class_suffix t = target[0].File(fname) t.attributes.java_lookupdir = target[0] tlist.append(t) return tlist, source
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_probe_services; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:handle; 6, block; 6, 7; 6, 9; 6, 13; 6, 54; 6, 95; 6, 109; 6, 132; 6, 144; 6, 153; 6, 164; 6, 177; 6, 190; 6, 202; 6, 203; 6, 209; 6, 222; 6, 244; 6, 248; 6, 258; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:code; 12, integer:0x2800; 13, function_definition; 13, 14; 13, 15; 13, 17; 14, function_name:event_filter_func; 15, parameters; 15, 16; 16, identifier:event; 17, block; 17, 18; 17, 52; 18, if_statement; 18, 19; 18, 31; 19, parenthesized_expression; 19, 20; 20, boolean_operator:and; 20, 21; 20, 26; 21, comparison_operator:==; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:event; 24, identifier:command_class; 25, integer:4; 26, comparison_operator:==; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:event; 29, identifier:command; 30, integer:2; 31, block; 31, 32; 31, 48; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 36; 34, pattern_list; 34, 35; 35, identifier:event_handle; 36, call; 36, 37; 36, 38; 37, identifier:unpack; 38, argument_list; 38, 39; 38, 40; 39, string:"B"; 40, subscript; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:event; 43, identifier:payload; 44, slice; 44, 45; 44, 46; 44, 47; 45, integer:0; 46, colon; 47, integer:1; 48, return_statement; 48, 49; 49, comparison_operator:==; 49, 50; 49, 51; 50, identifier:event_handle; 51, identifier:handle; 52, return_statement; 52, 53; 53, False; 54, function_definition; 54, 55; 54, 56; 54, 58; 55, function_name:end_filter_func; 56, parameters; 56, 57; 57, identifier:event; 58, block; 58, 59; 58, 93; 59, if_statement; 59, 60; 59, 72; 60, parenthesized_expression; 60, 61; 61, boolean_operator:and; 61, 62; 61, 67; 62, comparison_operator:==; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:event; 65, identifier:command_class; 66, integer:4; 67, comparison_operator:==; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:event; 70, identifier:command; 71, integer:1; 72, block; 72, 73; 72, 89; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 77; 75, pattern_list; 75, 76; 76, identifier:event_handle; 77, call; 77, 78; 77, 79; 78, identifier:unpack; 79, argument_list; 79, 80; 79, 81; 80, string:"B"; 81, subscript; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:event; 84, identifier:payload; 85, slice; 85, 86; 85, 87; 85, 88; 86, integer:0; 87, colon; 88, integer:1; 89, return_statement; 89, 90; 90, comparison_operator:==; 90, 91; 90, 92; 91, identifier:event_handle; 92, identifier:handle; 93, return_statement; 93, 94; 94, False; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:payload; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:struct; 101, identifier:pack; 102, argument_list; 102, 103; 102, 104; 102, 105; 102, 106; 102, 107; 102, 108; 103, string:'<BHHBH'; 104, identifier:handle; 105, integer:1; 106, integer:0xFFFF; 107, integer:2; 108, identifier:code; 109, try_statement; 109, 110; 109, 122; 110, block; 110, 111; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:response; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:_send_command; 118, argument_list; 118, 119; 118, 120; 118, 121; 119, integer:4; 120, integer:1; 121, identifier:payload; 122, except_clause; 122, 123; 122, 124; 123, identifier:InternalTimeoutError; 124, block; 124, 125; 125, return_statement; 125, 126; 126, expression_list; 126, 127; 126, 128; 127, False; 128, dictionary; 128, 129; 129, pair; 129, 130; 129, 131; 130, string:'reason'; 131, string:'Timeout waiting for command response'; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 137; 134, pattern_list; 134, 135; 134, 136; 135, identifier:handle; 136, identifier:result; 137, call; 137, 138; 137, 139; 138, identifier:unpack; 139, argument_list; 139, 140; 139, 141; 140, string:"<BH"; 141, attribute; 141, 142; 141, 143; 142, identifier:response; 143, identifier:payload; 144, if_statement; 144, 145; 144, 148; 145, comparison_operator:!=; 145, 146; 145, 147; 146, identifier:result; 147, integer:0; 148, block; 148, 149; 149, return_statement; 149, 150; 150, expression_list; 150, 151; 150, 152; 151, False; 152, None; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:events; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:self; 159, identifier:_wait_process_events; 160, argument_list; 160, 161; 160, 162; 160, 163; 161, float:0.5; 162, identifier:event_filter_func; 163, identifier:end_filter_func; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:gatt_events; 167, list_comprehension; 167, 168; 167, 169; 167, 172; 168, identifier:x; 169, for_in_clause; 169, 170; 169, 171; 170, identifier:x; 171, identifier:events; 172, if_clause; 172, 173; 173, call; 173, 174; 173, 175; 174, identifier:event_filter_func; 175, argument_list; 175, 176; 176, identifier:x; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:end_events; 180, list_comprehension; 180, 181; 180, 182; 180, 185; 181, identifier:x; 182, for_in_clause; 182, 183; 182, 184; 183, identifier:x; 184, identifier:events; 185, if_clause; 185, 186; 186, call; 186, 187; 186, 188; 187, identifier:end_filter_func; 188, argument_list; 188, 189; 189, identifier:x; 190, if_statement; 190, 191; 190, 197; 191, comparison_operator:==; 191, 192; 191, 196; 192, call; 192, 193; 192, 194; 193, identifier:len; 194, argument_list; 194, 195; 195, identifier:end_events; 196, integer:0; 197, block; 197, 198; 198, return_statement; 198, 199; 199, expression_list; 199, 200; 199, 201; 200, False; 201, None; 202, comment; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:end_event; 206, subscript; 206, 207; 206, 208; 207, identifier:end_events; 208, integer:0; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 215; 211, pattern_list; 211, 212; 211, 213; 211, 214; 212, identifier:_; 213, identifier:result; 214, identifier:_; 215, call; 215, 216; 215, 217; 216, identifier:unpack; 217, argument_list; 217, 218; 217, 219; 218, string:"<BHH"; 219, attribute; 219, 220; 219, 221; 220, identifier:end_event; 221, identifier:payload; 222, if_statement; 222, 223; 222, 226; 223, comparison_operator:!=; 223, 224; 223, 225; 224, identifier:result; 225, integer:0; 226, block; 226, 227; 226, 240; 227, expression_statement; 227, 228; 228, call; 228, 229; 228, 234; 229, attribute; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:self; 232, identifier:_logger; 233, identifier:warn; 234, argument_list; 234, 235; 235, binary_operator:%; 235, 236; 235, 237; 236, string:"Error enumerating GATT table, protocol error code = %d (0x%X)"; 237, tuple; 237, 238; 237, 239; 238, identifier:result; 239, identifier:result; 240, return_statement; 240, 241; 241, expression_list; 241, 242; 241, 243; 242, False; 243, None; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 247; 246, identifier:services; 247, dictionary; 248, for_statement; 248, 249; 248, 250; 248, 251; 249, identifier:event; 250, identifier:gatt_events; 251, block; 251, 252; 252, expression_statement; 252, 253; 253, call; 253, 254; 253, 255; 254, identifier:process_gatt_service; 255, argument_list; 255, 256; 255, 257; 256, identifier:services; 257, identifier:event; 258, return_statement; 258, 259; 259, expression_list; 259, 260; 259, 261; 260, True; 261, dictionary; 261, 262; 262, pair; 262, 263; 262, 264; 263, string:'services'; 264, identifier:services
def _probe_services(self, handle): """Probe for all primary services and characteristics in those services Args: handle (int): the connection handle to probe """ code = 0x2800 def event_filter_func(event): if (event.command_class == 4 and event.command == 2): event_handle, = unpack("B", event.payload[0:1]) return event_handle == handle return False def end_filter_func(event): if (event.command_class == 4 and event.command == 1): event_handle, = unpack("B", event.payload[0:1]) return event_handle == handle return False payload = struct.pack('<BHHBH', handle, 1, 0xFFFF, 2, code) try: response = self._send_command(4, 1, payload) except InternalTimeoutError: return False, {'reason': 'Timeout waiting for command response'} handle, result = unpack("<BH", response.payload) if result != 0: return False, None events = self._wait_process_events(0.5, event_filter_func, end_filter_func) gatt_events = [x for x in events if event_filter_func(x)] end_events = [x for x in events if end_filter_func(x)] if len(end_events) == 0: return False, None #Make sure we successfully probed the gatt table end_event = end_events[0] _, result, _ = unpack("<BHH", end_event.payload) if result != 0: self._logger.warn("Error enumerating GATT table, protocol error code = %d (0x%X)" % (result, result)) return False, None services = {} for event in gatt_events: process_gatt_service(services, event) return True, {'services': services}
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:_write_handle; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 4, identifier:self; 5, identifier:conn; 6, identifier:handle; 7, identifier:ack; 8, identifier:value; 9, default_parameter; 9, 10; 9, 11; 10, identifier:timeout; 11, float:1.0; 12, block; 12, 13; 12, 15; 12, 19; 12, 23; 12, 62; 12, 69; 12, 81; 12, 96; 12, 135; 12, 147; 12, 162; 12, 224; 13, expression_statement; 13, 14; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:conn_handle; 18, identifier:conn; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:char_handle; 22, identifier:handle; 23, function_definition; 23, 24; 23, 25; 23, 27; 24, function_name:write_handle_acked; 25, parameters; 25, 26; 26, identifier:event; 27, block; 27, 28; 28, if_statement; 28, 29; 28, 40; 29, boolean_operator:and; 29, 30; 29, 35; 30, comparison_operator:==; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:event; 33, identifier:command_class; 34, integer:4; 35, comparison_operator:==; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:event; 38, identifier:command; 39, integer:1; 40, block; 40, 41; 40, 54; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 47; 43, pattern_list; 43, 44; 43, 45; 43, 46; 44, identifier:conn; 45, identifier:_; 46, identifier:char; 47, call; 47, 48; 47, 49; 48, identifier:unpack; 49, argument_list; 49, 50; 49, 51; 50, string:"<BHH"; 51, attribute; 51, 52; 51, 53; 52, identifier:event; 53, identifier:payload; 54, return_statement; 54, 55; 55, boolean_operator:and; 55, 56; 55, 59; 56, comparison_operator:==; 56, 57; 56, 58; 57, identifier:conn_handle; 58, identifier:conn; 59, comparison_operator:==; 59, 60; 59, 61; 60, identifier:char_handle; 61, identifier:char; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:data_len; 65, call; 65, 66; 65, 67; 66, identifier:len; 67, argument_list; 67, 68; 68, identifier:value; 69, if_statement; 69, 70; 69, 73; 70, comparison_operator:>; 70, 71; 70, 72; 71, identifier:data_len; 72, integer:20; 73, block; 73, 74; 74, return_statement; 74, 75; 75, expression_list; 75, 76; 75, 77; 76, False; 77, dictionary; 77, 78; 78, pair; 78, 79; 78, 80; 79, string:'reason'; 80, string:'Data too long to write'; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:payload; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:struct; 87, identifier:pack; 88, argument_list; 88, 89; 88, 92; 88, 93; 88, 94; 88, 95; 89, binary_operator:%; 89, 90; 89, 91; 90, string:"<BHB%ds"; 91, identifier:data_len; 92, identifier:conn_handle; 93, identifier:char_handle; 94, identifier:data_len; 95, identifier:value; 96, try_statement; 96, 97; 96, 125; 97, block; 97, 98; 98, if_statement; 98, 99; 98, 100; 98, 112; 99, identifier:ack; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:response; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:self; 107, identifier:_send_command; 108, argument_list; 108, 109; 108, 110; 108, 111; 109, integer:4; 110, integer:5; 111, identifier:payload; 112, else_clause; 112, 113; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:response; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:self; 120, identifier:_send_command; 121, argument_list; 121, 122; 121, 123; 121, 124; 122, integer:4; 123, integer:6; 124, identifier:payload; 125, except_clause; 125, 126; 125, 127; 126, identifier:InternalTimeoutError; 127, block; 127, 128; 128, return_statement; 128, 129; 129, expression_list; 129, 130; 129, 131; 130, False; 131, dictionary; 131, 132; 132, pair; 132, 133; 132, 134; 133, string:'reason'; 134, string:'Timeout waiting for response to command in _write_handle'; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 140; 137, pattern_list; 137, 138; 137, 139; 138, identifier:_; 139, identifier:result; 140, call; 140, 141; 140, 142; 141, identifier:unpack; 142, argument_list; 142, 143; 142, 144; 143, string:"<BH"; 144, attribute; 144, 145; 144, 146; 145, identifier:response; 146, identifier:payload; 147, if_statement; 147, 148; 147, 151; 148, comparison_operator:!=; 148, 149; 148, 150; 149, identifier:result; 150, integer:0; 151, block; 151, 152; 152, return_statement; 152, 153; 153, expression_list; 153, 154; 153, 155; 154, False; 155, dictionary; 155, 156; 155, 159; 156, pair; 156, 157; 156, 158; 157, string:'reason'; 158, string:'Error writing to handle'; 159, pair; 159, 160; 159, 161; 160, string:'error_code'; 161, identifier:result; 162, if_statement; 162, 163; 162, 164; 163, identifier:ack; 164, block; 164, 165; 164, 179; 164, 194; 164, 209; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:events; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:self; 171, identifier:_wait_process_events; 172, argument_list; 172, 173; 172, 174; 172, 178; 173, identifier:timeout; 174, lambda; 174, 175; 174, 177; 175, lambda_parameters; 175, 176; 176, identifier:x; 177, False; 178, identifier:write_handle_acked; 179, if_statement; 179, 180; 179, 186; 180, comparison_operator:==; 180, 181; 180, 185; 181, call; 181, 182; 181, 183; 182, identifier:len; 183, argument_list; 183, 184; 184, identifier:events; 185, integer:0; 186, block; 186, 187; 187, return_statement; 187, 188; 188, expression_list; 188, 189; 188, 190; 189, False; 190, dictionary; 190, 191; 191, pair; 191, 192; 191, 193; 192, string:'reason'; 193, string:'Timeout waiting for acknowledge on write'; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 200; 196, pattern_list; 196, 197; 196, 198; 196, 199; 197, identifier:_; 198, identifier:result; 199, identifier:_; 200, call; 200, 201; 200, 202; 201, identifier:unpack; 202, argument_list; 202, 203; 202, 204; 203, string:"<BHH"; 204, attribute; 204, 205; 204, 208; 205, subscript; 205, 206; 205, 207; 206, identifier:events; 207, integer:0; 208, identifier:payload; 209, if_statement; 209, 210; 209, 213; 210, comparison_operator:!=; 210, 211; 210, 212; 211, identifier:result; 212, integer:0; 213, block; 213, 214; 214, return_statement; 214, 215; 215, expression_list; 215, 216; 215, 217; 216, False; 217, dictionary; 217, 218; 217, 221; 218, pair; 218, 219; 218, 220; 219, string:'reason'; 220, string:'Error received during write to handle'; 221, pair; 221, 222; 221, 223; 222, string:'error_code'; 223, identifier:result; 224, return_statement; 224, 225; 225, expression_list; 225, 226; 225, 227; 226, True; 227, None
def _write_handle(self, conn, handle, ack, value, timeout=1.0): """Write to a BLE device characteristic by its handle Args: conn (int): The connection handle for the device we should read from handle (int): The characteristics handle we should read ack (bool): Should this be an acknowledges write or unacknowledged timeout (float): How long to wait before failing value (bytearray): The value that we should write """ conn_handle = conn char_handle = handle def write_handle_acked(event): if event.command_class == 4 and event.command == 1: conn, _, char = unpack("<BHH", event.payload) return conn_handle == conn and char_handle == char data_len = len(value) if data_len > 20: return False, {'reason': 'Data too long to write'} payload = struct.pack("<BHB%ds" % data_len, conn_handle, char_handle, data_len, value) try: if ack: response = self._send_command(4, 5, payload) else: response = self._send_command(4, 6, payload) except InternalTimeoutError: return False, {'reason': 'Timeout waiting for response to command in _write_handle'} _, result = unpack("<BH", response.payload) if result != 0: return False, {'reason': 'Error writing to handle', 'error_code': result} if ack: events = self._wait_process_events(timeout, lambda x: False, write_handle_acked) if len(events) == 0: return False, {'reason': 'Timeout waiting for acknowledge on write'} _, result, _ = unpack("<BHH", events[0].payload) if result != 0: return False, {'reason': 'Error received during write to handle', 'error_code': result} return True, None
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:build_specfiles; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:source; 5, identifier:target; 6, identifier:env; 7, block; 7, 8; 7, 10; 7, 11; 7, 12; 7, 13; 7, 14; 7, 15; 7, 16; 7, 17; 7, 21; 7, 79; 7, 87; 7, 112; 7, 116; 7, 128; 7, 129; 7, 130; 7, 131; 7, 132; 7, 133; 7, 134; 7, 135; 7, 136; 7, 137; 7, 138; 7, 139; 7, 140; 7, 178; 7, 223; 7, 224; 7, 225; 7, 242; 7, 243; 7, 257; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, comment; 12, comment; 13, comment; 14, comment; 15, comment; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:opened_files; 20, dictionary; 21, function_definition; 21, 22; 21, 23; 21, 26; 22, function_name:open_file; 23, parameters; 23, 24; 23, 25; 24, identifier:needle; 25, identifier:haystack; 26, block; 26, 27; 27, try_statement; 27, 28; 27, 33; 28, block; 28, 29; 29, return_statement; 29, 30; 30, subscript; 30, 31; 30, 32; 31, identifier:opened_files; 32, identifier:needle; 33, except_clause; 33, 34; 33, 35; 34, identifier:KeyError; 35, block; 35, 36; 35, 61; 35, 75; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:file; 39, subscript; 39, 40; 39, 60; 40, call; 40, 41; 40, 42; 41, identifier:filter; 42, argument_list; 42, 43; 42, 59; 43, lambda; 43, 44; 43, 46; 44, lambda_parameters; 44, 45; 45, identifier:x; 46, comparison_operator:!=; 46, 47; 46, 57; 47, call; 47, 48; 47, 55; 48, attribute; 48, 49; 48, 54; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:x; 52, identifier:get_path; 53, argument_list; 54, identifier:rfind; 55, argument_list; 55, 56; 56, identifier:needle; 57, unary_operator:-; 57, 58; 58, integer:1; 59, identifier:haystack; 60, integer:0; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 66; 63, subscript; 63, 64; 63, 65; 64, identifier:opened_files; 65, identifier:needle; 66, call; 66, 67; 66, 68; 67, identifier:open; 68, argument_list; 68, 69; 68, 74; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:file; 72, identifier:get_abspath; 73, argument_list; 74, string:'w'; 75, return_statement; 75, 76; 76, subscript; 76, 77; 76, 78; 77, identifier:opened_files; 78, identifier:needle; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:control_file; 82, call; 82, 83; 82, 84; 83, identifier:open_file; 84, argument_list; 84, 85; 84, 86; 85, string:'control'; 86, identifier:target; 87, if_statement; 87, 88; 87, 91; 88, comparison_operator:not; 88, 89; 88, 90; 89, string:'X_IPK_DESCRIPTION'; 90, identifier:env; 91, block; 91, 92; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 97; 94, subscript; 94, 95; 94, 96; 95, identifier:env; 96, string:'X_IPK_DESCRIPTION'; 97, binary_operator:%; 97, 98; 97, 99; 98, string:"%s\n %s"; 99, tuple; 99, 100; 99, 103; 100, subscript; 100, 101; 100, 102; 101, identifier:env; 102, string:'SUMMARY'; 103, call; 103, 104; 103, 109; 104, attribute; 104, 105; 104, 108; 105, subscript; 105, 106; 105, 107; 106, identifier:env; 107, string:'DESCRIPTION'; 108, identifier:replace; 109, argument_list; 109, 110; 109, 111; 110, string:'\n'; 111, string:'\n '; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:content; 115, comment; 116, expression_statement; 116, 117; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:control_file; 120, identifier:write; 121, argument_list; 121, 122; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:env; 125, identifier:subst; 126, argument_list; 126, 127; 127, identifier:content; 128, comment; 129, comment; 130, comment; 131, comment; 132, comment; 133, comment; 134, comment; 135, comment; 136, comment; 137, comment; 138, comment; 139, comment; 140, for_statement; 140, 141; 140, 142; 140, 154; 141, identifier:f; 142, list_comprehension; 142, 143; 142, 144; 142, 147; 143, identifier:x; 144, for_in_clause; 144, 145; 144, 146; 145, identifier:x; 146, identifier:source; 147, if_clause; 147, 148; 148, comparison_operator:in; 148, 149; 148, 150; 149, string:'PACKAGING_CONFIG'; 150, call; 150, 151; 150, 152; 151, identifier:dir; 152, argument_list; 152, 153; 153, identifier:x; 154, block; 154, 155; 154, 162; 154, 171; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:config; 158, call; 158, 159; 158, 160; 159, identifier:open_file; 160, argument_list; 160, 161; 161, string:'conffiles'; 162, expression_statement; 162, 163; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:config; 166, identifier:write; 167, argument_list; 167, 168; 168, attribute; 168, 169; 168, 170; 169, identifier:f; 170, identifier:PACKAGING_INSTALL_LOCATION; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:config; 175, identifier:write; 176, argument_list; 176, 177; 177, string:'\n'; 178, for_statement; 178, 179; 178, 180; 178, 185; 179, identifier:str; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, string:'POSTRM PRERM POSTINST PREINST'; 183, identifier:split; 184, argument_list; 185, block; 185, 186; 185, 192; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:name; 189, binary_operator:%; 189, 190; 189, 191; 190, string:"PACKAGING_X_IPK_%s"; 191, identifier:str; 192, for_statement; 192, 193; 192, 194; 192, 206; 193, identifier:f; 194, list_comprehension; 194, 195; 194, 196; 194, 199; 195, identifier:x; 196, for_in_clause; 196, 197; 196, 198; 197, identifier:x; 198, identifier:source; 199, if_clause; 199, 200; 200, comparison_operator:in; 200, 201; 200, 202; 201, identifier:name; 202, call; 202, 203; 202, 204; 203, identifier:dir; 204, argument_list; 204, 205; 205, identifier:x; 206, block; 206, 207; 206, 214; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:file; 210, call; 210, 211; 210, 212; 211, identifier:open_file; 212, argument_list; 212, 213; 213, identifier:name; 214, expression_statement; 214, 215; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:file; 218, identifier:write; 219, argument_list; 219, 220; 220, subscript; 220, 221; 220, 222; 221, identifier:env; 222, identifier:str; 223, comment; 224, comment; 225, for_statement; 225, 226; 225, 227; 225, 235; 226, identifier:f; 227, call; 227, 228; 227, 229; 228, identifier:list; 229, argument_list; 229, 230; 230, call; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:opened_files; 233, identifier:values; 234, argument_list; 235, block; 235, 236; 236, expression_statement; 236, 237; 237, call; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:f; 240, identifier:close; 241, argument_list; 242, comment; 243, if_statement; 243, 244; 243, 247; 244, comparison_operator:in; 244, 245; 244, 246; 245, string:'CHANGE_SPECFILE'; 246, identifier:env; 247, block; 247, 248; 248, expression_statement; 248, 249; 249, augmented_assignment:+=; 249, 250; 249, 251; 250, identifier:content; 251, call; 251, 252; 251, 255; 252, subscript; 252, 253; 252, 254; 253, identifier:env; 254, string:'CHANGE_SPECFILE'; 255, argument_list; 255, 256; 256, identifier:target; 257, return_statement; 257, 258; 258, integer:0
def build_specfiles(source, target, env): """ Filter the targets for the needed files and use the variables in env to create the specfile. """ # # At first we care for the CONTROL/control file, which is the main file for ipk. # # For this we need to open multiple files in random order, so we store into # a dict so they can be easily accessed. # # opened_files={} def open_file(needle, haystack): try: return opened_files[needle] except KeyError: file=filter(lambda x: x.get_path().rfind(needle)!=-1, haystack)[0] opened_files[needle]=open(file.get_abspath(), 'w') return opened_files[needle] control_file=open_file('control', target) if 'X_IPK_DESCRIPTION' not in env: env['X_IPK_DESCRIPTION']="%s\n %s"%(env['SUMMARY'], env['DESCRIPTION'].replace('\n', '\n ')) content = """ Package: $NAME Version: $VERSION Priority: $X_IPK_PRIORITY Section: $X_IPK_SECTION Source: $SOURCE_URL Architecture: $ARCHITECTURE Maintainer: $X_IPK_MAINTAINER Depends: $X_IPK_DEPENDS Description: $X_IPK_DESCRIPTION """ control_file.write(env.subst(content)) # # now handle the various other files, which purpose it is to set post-, # pre-scripts and mark files as config files. # # We do so by filtering the source files for files which are marked with # the "config" tag and afterwards we do the same for x_ipk_postrm, # x_ipk_prerm, x_ipk_postinst and x_ipk_preinst tags. # # The first one will write the name of the file into the file # CONTROL/configfiles, the latter add the content of the x_ipk_* variable # into the same named file. # for f in [x for x in source if 'PACKAGING_CONFIG' in dir(x)]: config=open_file('conffiles') config.write(f.PACKAGING_INSTALL_LOCATION) config.write('\n') for str in 'POSTRM PRERM POSTINST PREINST'.split(): name="PACKAGING_X_IPK_%s"%str for f in [x for x in source if name in dir(x)]: file=open_file(name) file.write(env[str]) # # close all opened files for f in list(opened_files.values()): f.close() # call a user specified function if 'CHANGE_SPECFILE' in env: content += env['CHANGE_SPECFILE'](target) return 0
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:emit_java_headers; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:target; 5, identifier:source; 6, identifier:env; 7, block; 7, 8; 7, 10; 7, 20; 7, 29; 7, 67; 7, 80; 7, 105; 7, 109; 7, 221; 7, 231; 7, 249; 7, 360; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:class_suffix; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:env; 16, identifier:get; 17, argument_list; 17, 18; 17, 19; 18, string:'JAVACLASSSUFFIX'; 19, string:'.class'; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:classdir; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:env; 26, identifier:get; 27, argument_list; 27, 28; 28, string:'JAVACLASSDIR'; 29, if_statement; 29, 30; 29, 32; 30, not_operator; 30, 31; 31, identifier:classdir; 32, block; 32, 33; 33, try_statement; 33, 34; 33, 41; 33, 48; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:s; 38, subscript; 38, 39; 38, 40; 39, identifier:source; 40, integer:0; 41, except_clause; 41, 42; 41, 43; 42, identifier:IndexError; 43, block; 43, 44; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:classdir; 47, string:'.'; 48, else_clause; 48, 49; 49, block; 49, 50; 50, try_statement; 50, 51; 50, 60; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:classdir; 55, attribute; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:s; 58, identifier:attributes; 59, identifier:java_classdir; 60, except_clause; 60, 61; 60, 62; 61, identifier:AttributeError; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:classdir; 66, string:'.'; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:classdir; 70, call; 70, 71; 70, 79; 71, attribute; 71, 72; 71, 78; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:env; 75, identifier:Dir; 76, argument_list; 76, 77; 77, identifier:classdir; 78, identifier:rdir; 79, argument_list; 80, if_statement; 80, 81; 80, 87; 80, 92; 81, comparison_operator:==; 81, 82; 81, 86; 82, call; 82, 83; 82, 84; 83, identifier:str; 84, argument_list; 84, 85; 85, identifier:classdir; 86, string:'.'; 87, block; 87, 88; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:c_; 91, None; 92, else_clause; 92, 93; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:c_; 97, binary_operator:+; 97, 98; 97, 102; 98, call; 98, 99; 98, 100; 99, identifier:str; 100, argument_list; 100, 101; 101, identifier:classdir; 102, attribute; 102, 103; 102, 104; 103, identifier:os; 104, identifier:sep; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:slist; 108, list:[]; 109, for_statement; 109, 110; 109, 111; 109, 112; 110, identifier:src; 111, identifier:source; 112, block; 112, 113; 112, 198; 112, 206; 112, 214; 113, try_statement; 113, 114; 113, 123; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:classname; 118, attribute; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:src; 121, identifier:attributes; 122, identifier:java_classname; 123, except_clause; 123, 124; 123, 125; 124, identifier:AttributeError; 125, block; 125, 126; 125, 133; 125, 158; 125, 185; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:classname; 129, call; 129, 130; 129, 131; 130, identifier:str; 131, argument_list; 131, 132; 132, identifier:src; 133, if_statement; 133, 134; 133, 146; 134, boolean_operator:and; 134, 135; 134, 136; 135, identifier:c_; 136, comparison_operator:==; 136, 137; 136, 145; 137, subscript; 137, 138; 137, 139; 138, identifier:classname; 139, slice; 139, 140; 139, 141; 140, colon; 141, call; 141, 142; 141, 143; 142, identifier:len; 143, argument_list; 143, 144; 144, identifier:c_; 145, identifier:c_; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:classname; 150, subscript; 150, 151; 150, 152; 151, identifier:classname; 152, slice; 152, 153; 152, 157; 153, call; 153, 154; 153, 155; 154, identifier:len; 155, argument_list; 155, 156; 156, identifier:c_; 157, colon; 158, if_statement; 158, 159; 158, 172; 159, boolean_operator:and; 159, 160; 159, 161; 160, identifier:class_suffix; 161, comparison_operator:==; 161, 162; 161, 171; 162, subscript; 162, 163; 162, 164; 163, identifier:classname; 164, slice; 164, 165; 164, 170; 165, unary_operator:-; 165, 166; 166, call; 166, 167; 166, 168; 167, identifier:len; 168, argument_list; 168, 169; 169, identifier:class_suffix; 170, colon; 171, identifier:class_suffix; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:classname; 176, subscript; 176, 177; 176, 178; 177, identifier:classname; 178, slice; 178, 179; 178, 180; 179, colon; 180, unary_operator:-; 180, 181; 181, call; 181, 182; 181, 183; 182, identifier:len; 183, argument_list; 183, 184; 184, identifier:class_suffix; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:classname; 188, call; 188, 189; 188, 196; 189, attribute; 189, 190; 189, 195; 190, attribute; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:SCons; 193, identifier:Tool; 194, identifier:javac; 195, identifier:classname; 196, argument_list; 196, 197; 197, identifier:classname; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:s; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:src; 204, identifier:rfile; 205, argument_list; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 213; 208, attribute; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:s; 211, identifier:attributes; 212, identifier:java_classname; 213, identifier:classname; 214, expression_statement; 214, 215; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:slist; 218, identifier:append; 219, argument_list; 219, 220; 220, identifier:s; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:s; 224, call; 224, 225; 224, 230; 225, attribute; 225, 226; 225, 229; 226, subscript; 226, 227; 226, 228; 227, identifier:source; 228, integer:0; 229, identifier:rfile; 230, argument_list; 231, if_statement; 231, 232; 231, 240; 232, not_operator; 232, 233; 233, call; 233, 234; 233, 235; 234, identifier:hasattr; 235, argument_list; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:s; 238, identifier:attributes; 239, string:'java_classdir'; 240, block; 240, 241; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 248; 243, attribute; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:s; 246, identifier:attributes; 247, identifier:java_classdir; 248, identifier:classdir; 249, if_statement; 249, 250; 249, 263; 249, 268; 250, comparison_operator:is; 250, 251; 250, 256; 251, attribute; 251, 252; 251, 255; 252, subscript; 252, 253; 252, 254; 253, identifier:target; 254, integer:0; 255, identifier:__class__; 256, attribute; 256, 257; 256, 262; 257, attribute; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:SCons; 260, identifier:Node; 261, identifier:FS; 262, identifier:File; 263, block; 263, 264; 264, expression_statement; 264, 265; 265, assignment; 265, 266; 265, 267; 266, identifier:tlist; 267, identifier:target; 268, else_clause; 268, 269; 269, block; 269, 270; 269, 308; 269, 312; 270, if_statement; 270, 271; 270, 285; 271, not_operator; 271, 272; 272, call; 272, 273; 272, 274; 273, identifier:isinstance; 274, argument_list; 274, 275; 274, 278; 275, subscript; 275, 276; 275, 277; 276, identifier:target; 277, integer:0; 278, attribute; 278, 279; 278, 284; 279, attribute; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:SCons; 282, identifier:Node; 283, identifier:FS; 284, identifier:Dir; 285, block; 285, 286; 285, 300; 286, expression_statement; 286, 287; 287, assignment; 287, 288; 287, 293; 288, attribute; 288, 289; 288, 292; 289, subscript; 289, 290; 289, 291; 290, identifier:target; 291, integer:0; 292, identifier:__class__; 293, attribute; 293, 294; 293, 299; 294, attribute; 294, 295; 294, 298; 295, attribute; 295, 296; 295, 297; 296, identifier:SCons; 297, identifier:Node; 298, identifier:FS; 299, identifier:Dir; 300, expression_statement; 300, 301; 301, call; 301, 302; 301, 307; 302, attribute; 302, 303; 302, 306; 303, subscript; 303, 304; 303, 305; 304, identifier:target; 305, integer:0; 306, identifier:_morph; 307, argument_list; 308, expression_statement; 308, 309; 309, assignment; 309, 310; 309, 311; 310, identifier:tlist; 311, list:[]; 312, for_statement; 312, 313; 312, 314; 312, 315; 313, identifier:s; 314, identifier:source; 315, block; 315, 316; 315, 332; 315, 343; 315, 353; 316, expression_statement; 316, 317; 317, assignment; 317, 318; 317, 319; 318, identifier:fname; 319, binary_operator:+; 319, 320; 319, 331; 320, call; 320, 321; 320, 328; 321, attribute; 321, 322; 321, 327; 322, attribute; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:s; 325, identifier:attributes; 326, identifier:java_classname; 327, identifier:replace; 328, argument_list; 328, 329; 328, 330; 329, string:'.'; 330, string:'_'; 331, string:'.h'; 332, expression_statement; 332, 333; 333, assignment; 333, 334; 333, 335; 334, identifier:t; 335, call; 335, 336; 335, 341; 336, attribute; 336, 337; 336, 340; 337, subscript; 337, 338; 337, 339; 338, identifier:target; 339, integer:0; 340, identifier:File; 341, argument_list; 341, 342; 342, identifier:fname; 343, expression_statement; 343, 344; 344, assignment; 344, 345; 344, 350; 345, attribute; 345, 346; 345, 349; 346, attribute; 346, 347; 346, 348; 347, identifier:t; 348, identifier:attributes; 349, identifier:java_lookupdir; 350, subscript; 350, 351; 350, 352; 351, identifier:target; 352, integer:0; 353, expression_statement; 353, 354; 354, call; 354, 355; 354, 358; 355, attribute; 355, 356; 355, 357; 356, identifier:tlist; 357, identifier:append; 358, argument_list; 358, 359; 359, identifier:t; 360, return_statement; 360, 361; 361, expression_list; 361, 362; 361, 363; 362, identifier:tlist; 363, identifier:source
def emit_java_headers(target, source, env): """Create and return lists of Java stub header files that will be created from a set of class files. """ class_suffix = env.get('JAVACLASSSUFFIX', '.class') classdir = env.get('JAVACLASSDIR') if not classdir: try: s = source[0] except IndexError: classdir = '.' else: try: classdir = s.attributes.java_classdir except AttributeError: classdir = '.' classdir = env.Dir(classdir).rdir() if str(classdir) == '.': c_ = None else: c_ = str(classdir) + os.sep slist = [] for src in source: try: classname = src.attributes.java_classname except AttributeError: classname = str(src) if c_ and classname[:len(c_)] == c_: classname = classname[len(c_):] if class_suffix and classname[-len(class_suffix):] == class_suffix: classname = classname[:-len(class_suffix)] classname = SCons.Tool.javac.classname(classname) s = src.rfile() s.attributes.java_classname = classname slist.append(s) s = source[0].rfile() if not hasattr(s.attributes, 'java_classdir'): s.attributes.java_classdir = classdir if target[0].__class__ is SCons.Node.FS.File: tlist = target else: if not isinstance(target[0], SCons.Node.FS.Dir): target[0].__class__ = SCons.Node.FS.Dir target[0]._morph() tlist = [] for s in source: fname = s.attributes.java_classname.replace('.', '_') + '.h' t = target[0].File(fname) t.attributes.java_lookupdir = target[0] tlist.append(t) return tlist, source
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_parse_resource_usage; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:cls; 5, identifier:action_dict; 6, identifier:declarations; 7, block; 7, 8; 7, 10; 7, 20; 7, 38; 7, 56; 7, 60; 7, 165; 7, 166; 7, 190; 7, 210; 7, 230; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:raw_used; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:action_dict; 16, identifier:pop; 17, argument_list; 17, 18; 17, 19; 18, string:'use'; 19, list:[]; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:opened; 23, list_comprehension; 23, 24; 23, 29; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:x; 27, identifier:strip; 28, argument_list; 29, for_in_clause; 29, 30; 29, 31; 30, identifier:x; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:action_dict; 34, identifier:pop; 35, argument_list; 35, 36; 35, 37; 36, string:'open_before'; 37, list:[]; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:closed; 41, list_comprehension; 41, 42; 41, 47; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:x; 45, identifier:strip; 46, argument_list; 47, for_in_clause; 47, 48; 47, 49; 48, identifier:x; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:action_dict; 52, identifier:pop; 53, argument_list; 53, 54; 53, 55; 54, string:'close_after'; 55, list:[]; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:used; 59, dictionary; 60, for_statement; 60, 61; 60, 62; 60, 63; 61, identifier:resource; 62, identifier:raw_used; 63, block; 63, 64; 63, 140; 63, 159; 64, if_statement; 64, 65; 64, 68; 64, 126; 65, comparison_operator:in; 65, 66; 65, 67; 66, string:'as'; 67, identifier:resource; 68, block; 68, 69; 68, 81; 68, 89; 68, 97; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 75; 71, pattern_list; 71, 72; 71, 73; 71, 74; 72, identifier:global_name; 73, identifier:_; 74, identifier:local_name; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:resource; 78, identifier:partition; 79, argument_list; 79, 80; 80, string:'as'; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:global_name; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:global_name; 87, identifier:strip; 88, argument_list; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:local_name; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:local_name; 95, identifier:strip; 96, argument_list; 97, if_statement; 97, 98; 97, 111; 98, boolean_operator:or; 98, 99; 98, 105; 99, comparison_operator:==; 99, 100; 99, 104; 100, call; 100, 101; 100, 102; 101, identifier:len; 102, argument_list; 102, 103; 103, identifier:global_name; 104, integer:0; 105, comparison_operator:==; 105, 106; 105, 110; 106, call; 106, 107; 106, 108; 107, identifier:len; 108, argument_list; 108, 109; 109, identifier:local_name; 110, integer:0; 111, block; 111, 112; 112, raise_statement; 112, 113; 113, call; 113, 114; 113, 115; 114, identifier:RecipeFileInvalid; 115, argument_list; 115, 116; 115, 117; 115, 120; 115, 123; 116, string:"Resource usage specified in action with invalid name using 'as' statement"; 117, keyword_argument; 117, 118; 117, 119; 118, identifier:global_name; 119, identifier:global_name; 120, keyword_argument; 120, 121; 120, 122; 121, identifier:local_name; 122, identifier:local_name; 123, keyword_argument; 123, 124; 123, 125; 124, identifier:statement; 125, identifier:resource; 126, else_clause; 126, 127; 127, block; 127, 128; 127, 136; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:global_name; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:resource; 134, identifier:strip; 135, argument_list; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:local_name; 139, identifier:global_name; 140, if_statement; 140, 141; 140, 144; 141, comparison_operator:in; 141, 142; 141, 143; 142, identifier:local_name; 143, identifier:used; 144, block; 144, 145; 145, raise_statement; 145, 146; 146, call; 146, 147; 146, 148; 147, identifier:RecipeFileInvalid; 148, argument_list; 148, 149; 148, 150; 148, 153; 148, 156; 149, string:"Resource specified twice for action"; 150, keyword_argument; 150, 151; 150, 152; 151, identifier:args; 152, identifier:action_dict; 153, keyword_argument; 153, 154; 153, 155; 154, identifier:resource; 155, identifier:local_name; 156, keyword_argument; 156, 157; 156, 158; 157, identifier:used_resources; 158, identifier:used; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 164; 161, subscript; 161, 162; 161, 163; 162, identifier:used; 163, identifier:local_name; 164, identifier:global_name; 165, comment; 166, for_statement; 166, 167; 166, 168; 166, 181; 167, identifier:name; 168, generator_expression; 168, 169; 168, 170; 168, 177; 169, identifier:x; 170, for_in_clause; 170, 171; 170, 172; 171, identifier:x; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:used; 175, identifier:values; 176, argument_list; 177, if_clause; 177, 178; 178, comparison_operator:not; 178, 179; 178, 180; 179, identifier:x; 180, identifier:declarations; 181, block; 181, 182; 182, raise_statement; 182, 183; 183, call; 183, 184; 183, 185; 184, identifier:RecipeFileInvalid; 185, argument_list; 185, 186; 185, 187; 186, string:"Action makes use of non-declared shared resource"; 187, keyword_argument; 187, 188; 187, 189; 188, identifier:name; 189, identifier:name; 190, for_statement; 190, 191; 190, 192; 190, 201; 191, identifier:name; 192, generator_expression; 192, 193; 192, 194; 192, 197; 193, identifier:x; 194, for_in_clause; 194, 195; 194, 196; 195, identifier:x; 196, identifier:opened; 197, if_clause; 197, 198; 198, comparison_operator:not; 198, 199; 198, 200; 199, identifier:x; 200, identifier:declarations; 201, block; 201, 202; 202, raise_statement; 202, 203; 203, call; 203, 204; 203, 205; 204, identifier:RecipeFileInvalid; 205, argument_list; 205, 206; 205, 207; 206, string:"Action specified a non-declared shared resource in open_before"; 207, keyword_argument; 207, 208; 207, 209; 208, identifier:name; 209, identifier:name; 210, for_statement; 210, 211; 210, 212; 210, 221; 211, identifier:name; 212, generator_expression; 212, 213; 212, 214; 212, 217; 213, identifier:x; 214, for_in_clause; 214, 215; 214, 216; 215, identifier:x; 216, identifier:closed; 217, if_clause; 217, 218; 218, comparison_operator:not; 218, 219; 218, 220; 219, identifier:x; 220, identifier:declarations; 221, block; 221, 222; 222, raise_statement; 222, 223; 223, call; 223, 224; 223, 225; 224, identifier:RecipeFileInvalid; 225, argument_list; 225, 226; 225, 227; 226, string:"Action specified a non-declared shared resource in close_after"; 227, keyword_argument; 227, 228; 227, 229; 228, identifier:name; 229, identifier:name; 230, return_statement; 230, 231; 231, call; 231, 232; 231, 233; 232, identifier:ResourceUsage; 233, argument_list; 233, 234; 233, 235; 233, 236; 234, identifier:used; 235, identifier:opened; 236, identifier:closed
def _parse_resource_usage(cls, action_dict, declarations): """Parse out what resources are used, opened and closed in an action step.""" raw_used = action_dict.pop('use', []) opened = [x.strip() for x in action_dict.pop('open_before', [])] closed = [x.strip() for x in action_dict.pop('close_after', [])] used = {} for resource in raw_used: if 'as' in resource: global_name, _, local_name = resource.partition('as') global_name = global_name.strip() local_name = local_name.strip() if len(global_name) == 0 or len(local_name) == 0: raise RecipeFileInvalid("Resource usage specified in action with invalid name using 'as' statement", global_name=global_name, local_name=local_name, statement=resource) else: global_name = resource.strip() local_name = global_name if local_name in used: raise RecipeFileInvalid("Resource specified twice for action", args=action_dict, resource=local_name, used_resources=used) used[local_name] = global_name # Make sure we only use, open and close declared resources for name in (x for x in used.values() if x not in declarations): raise RecipeFileInvalid("Action makes use of non-declared shared resource", name=name) for name in (x for x in opened if x not in declarations): raise RecipeFileInvalid("Action specified a non-declared shared resource in open_before", name=name) for name in (x for x in closed if x not in declarations): raise RecipeFileInvalid("Action specified a non-declared shared resource in close_after", name=name) return ResourceUsage(used, opened, closed)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:format_script; 3, parameters; 3, 4; 4, identifier:sensor_graph; 5, block; 5, 6; 5, 8; 5, 12; 5, 25; 5, 37; 5, 49; 5, 71; 5, 90; 5, 131; 5, 143; 5, 152; 5, 214; 5, 215; 5, 226; 5, 237; 5, 257; 5, 264; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:records; 11, list:[]; 12, expression_statement; 12, 13; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:records; 16, identifier:append; 17, argument_list; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:SetGraphOnlineRecord; 20, argument_list; 20, 21; 20, 22; 21, False; 22, keyword_argument; 22, 23; 22, 24; 23, identifier:address; 24, integer:8; 25, expression_statement; 25, 26; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:records; 29, identifier:append; 30, argument_list; 30, 31; 31, call; 31, 32; 31, 33; 32, identifier:ClearDataRecord; 33, argument_list; 33, 34; 34, keyword_argument; 34, 35; 34, 36; 35, identifier:address; 36, integer:8; 37, expression_statement; 37, 38; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:records; 41, identifier:append; 42, argument_list; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:ResetGraphRecord; 45, argument_list; 45, 46; 46, keyword_argument; 46, 47; 46, 48; 47, identifier:address; 48, integer:8; 49, for_statement; 49, 50; 49, 51; 49, 54; 50, identifier:node; 51, attribute; 51, 52; 51, 53; 52, identifier:sensor_graph; 53, identifier:nodes; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:records; 59, identifier:append; 60, argument_list; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:AddNodeRecord; 63, argument_list; 63, 64; 63, 68; 64, call; 64, 65; 64, 66; 65, identifier:str; 66, argument_list; 66, 67; 67, identifier:node; 68, keyword_argument; 68, 69; 68, 70; 69, identifier:address; 70, integer:8; 71, for_statement; 71, 72; 71, 73; 71, 76; 72, identifier:streamer; 73, attribute; 73, 74; 73, 75; 74, identifier:sensor_graph; 75, identifier:streamers; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:records; 81, identifier:append; 82, argument_list; 82, 83; 83, call; 83, 84; 83, 85; 84, identifier:AddStreamerRecord; 85, argument_list; 85, 86; 85, 87; 86, identifier:streamer; 87, keyword_argument; 87, 88; 87, 89; 88, identifier:address; 89, integer:8; 90, for_statement; 90, 91; 90, 94; 90, 116; 91, pattern_list; 91, 92; 91, 93; 92, identifier:stream; 93, identifier:value; 94, call; 94, 95; 94, 96; 95, identifier:sorted; 96, argument_list; 96, 97; 96, 104; 97, call; 97, 98; 97, 103; 98, attribute; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:sensor_graph; 101, identifier:constant_database; 102, identifier:items; 103, argument_list; 104, keyword_argument; 104, 105; 104, 106; 105, identifier:key; 106, lambda; 106, 107; 106, 109; 107, lambda_parameters; 107, 108; 108, identifier:x; 109, call; 109, 110; 109, 115; 110, attribute; 110, 111; 110, 114; 111, subscript; 111, 112; 111, 113; 112, identifier:x; 113, integer:0; 114, identifier:encode; 115, argument_list; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:records; 121, identifier:append; 122, argument_list; 122, 123; 123, call; 123, 124; 123, 125; 124, identifier:SetConstantRecord; 125, argument_list; 125, 126; 125, 127; 125, 128; 126, identifier:stream; 127, identifier:value; 128, keyword_argument; 128, 129; 128, 130; 129, identifier:address; 130, integer:8; 131, expression_statement; 131, 132; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:records; 135, identifier:append; 136, argument_list; 136, 137; 137, call; 137, 138; 137, 139; 138, identifier:PersistGraphRecord; 139, argument_list; 139, 140; 140, keyword_argument; 140, 141; 140, 142; 141, identifier:address; 142, integer:8; 143, expression_statement; 143, 144; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:records; 147, identifier:append; 148, argument_list; 148, 149; 149, call; 149, 150; 149, 151; 150, identifier:ClearConfigVariablesRecord; 151, argument_list; 152, for_statement; 152, 153; 152, 154; 152, 170; 153, identifier:slot; 154, call; 154, 155; 154, 156; 155, identifier:sorted; 156, argument_list; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:sensor_graph; 159, identifier:config_database; 160, keyword_argument; 160, 161; 160, 162; 161, identifier:key; 162, lambda; 162, 163; 162, 165; 163, lambda_parameters; 163, 164; 164, identifier:x; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:x; 168, identifier:encode; 169, argument_list; 170, block; 170, 171; 171, for_statement; 171, 172; 171, 173; 171, 181; 172, identifier:config_id; 173, call; 173, 174; 173, 175; 174, identifier:sorted; 175, argument_list; 175, 176; 176, subscript; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:sensor_graph; 179, identifier:config_database; 180, identifier:slot; 181, block; 181, 182; 181, 194; 181, 202; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 187; 184, pattern_list; 184, 185; 184, 186; 185, identifier:config_type; 186, identifier:value; 187, subscript; 187, 188; 187, 193; 188, subscript; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:sensor_graph; 191, identifier:config_database; 192, identifier:slot; 193, identifier:config_id; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:byte_value; 197, call; 197, 198; 197, 199; 198, identifier:_convert_to_bytes; 199, argument_list; 199, 200; 199, 201; 200, identifier:config_type; 201, identifier:value; 202, expression_statement; 202, 203; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:records; 206, identifier:append; 207, argument_list; 207, 208; 208, call; 208, 209; 208, 210; 209, identifier:SetConfigRecord; 210, argument_list; 210, 211; 210, 212; 210, 213; 211, identifier:slot; 212, identifier:config_id; 213, identifier:byte_value; 214, comment; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:app_tag; 218, call; 218, 219; 218, 224; 219, attribute; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:sensor_graph; 222, identifier:metadata_database; 223, identifier:get; 224, argument_list; 224, 225; 225, string:'app_tag'; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 229; 228, identifier:app_version; 229, call; 229, 230; 229, 235; 230, attribute; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:sensor_graph; 233, identifier:metadata_database; 234, identifier:get; 235, argument_list; 235, 236; 236, string:'app_version'; 237, if_statement; 237, 238; 237, 241; 238, comparison_operator:is; 238, 239; 238, 240; 239, identifier:app_tag; 240, None; 241, block; 241, 242; 242, expression_statement; 242, 243; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:records; 246, identifier:append; 247, argument_list; 247, 248; 248, call; 248, 249; 248, 250; 249, identifier:SetDeviceTagRecord; 250, argument_list; 250, 251; 250, 254; 251, keyword_argument; 251, 252; 251, 253; 252, identifier:app_tag; 253, identifier:app_tag; 254, keyword_argument; 254, 255; 254, 256; 255, identifier:app_version; 256, identifier:app_version; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 260; 259, identifier:script; 260, call; 260, 261; 260, 262; 261, identifier:UpdateScript; 262, argument_list; 262, 263; 263, identifier:records; 264, return_statement; 264, 265; 265, call; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, identifier:script; 268, identifier:encode; 269, argument_list
def format_script(sensor_graph): """Create a binary script containing this sensor graph. This function produces a repeatable script by applying a known sorting order to all constants and config variables when iterating over those dictionaries. Args: sensor_graph (SensorGraph): the sensor graph that we want to format Returns: bytearray: The binary script data. """ records = [] records.append(SetGraphOnlineRecord(False, address=8)) records.append(ClearDataRecord(address=8)) records.append(ResetGraphRecord(address=8)) for node in sensor_graph.nodes: records.append(AddNodeRecord(str(node), address=8)) for streamer in sensor_graph.streamers: records.append(AddStreamerRecord(streamer, address=8)) for stream, value in sorted(sensor_graph.constant_database.items(), key=lambda x: x[0].encode()): records.append(SetConstantRecord(stream, value, address=8)) records.append(PersistGraphRecord(address=8)) records.append(ClearConfigVariablesRecord()) for slot in sorted(sensor_graph.config_database, key=lambda x: x.encode()): for config_id in sorted(sensor_graph.config_database[slot]): config_type, value = sensor_graph.config_database[slot][config_id] byte_value = _convert_to_bytes(config_type, value) records.append(SetConfigRecord(slot, config_id, byte_value)) # If we have an app tag and version set program them in app_tag = sensor_graph.metadata_database.get('app_tag') app_version = sensor_graph.metadata_database.get('app_version') if app_tag is not None: records.append(SetDeviceTagRecord(app_tag=app_tag, app_version=app_version)) script = UpdateScript(records) return script.encode()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:push; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:stream; 6, identifier:reading; 7, block; 7, 8; 7, 10; 7, 11; 7, 20; 7, 30; 7, 139; 7, 140; 7, 172; 7, 173; 7, 174; 7, 196; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:reading; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:copy; 17, identifier:copy; 18, argument_list; 18, 19; 19, identifier:reading; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:reading; 24, identifier:stream; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:stream; 28, identifier:encode; 29, argument_list; 30, if_statement; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:stream; 33, identifier:buffered; 34, block; 34, 35; 34, 41; 34, 60; 34, 116; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:output_buffer; 38, attribute; 38, 39; 38, 40; 39, identifier:stream; 40, identifier:output; 41, if_statement; 41, 42; 41, 47; 42, comparison_operator:is; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:id_assigner; 46, None; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:reading; 52, identifier:reading_id; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:id_assigner; 57, argument_list; 57, 58; 57, 59; 58, identifier:stream; 59, identifier:reading; 60, try_statement; 60, 61; 60, 71; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 69; 64, attribute; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:self; 67, identifier:_engine; 68, identifier:push; 69, argument_list; 69, 70; 70, identifier:reading; 71, except_clause; 71, 72; 71, 73; 71, 74; 72, identifier:StorageFullError; 73, comment; 74, block; 74, 75; 74, 98; 74, 107; 75, if_statement; 75, 76; 75, 96; 76, boolean_operator:or; 76, 77; 76, 86; 77, parenthesized_expression; 77, 78; 78, boolean_operator:and; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:stream; 81, identifier:output; 82, not_operator; 82, 83; 83, attribute; 83, 84; 83, 85; 84, identifier:self; 85, identifier:_rollover_streaming; 86, parenthesized_expression; 86, 87; 87, boolean_operator:and; 87, 88; 87, 92; 88, not_operator; 88, 89; 89, attribute; 89, 90; 89, 91; 90, identifier:stream; 91, identifier:output; 92, not_operator; 92, 93; 93, attribute; 93, 94; 93, 95; 94, identifier:self; 95, identifier:_rollover_storage; 96, block; 96, 97; 97, raise_statement; 98, expression_statement; 98, 99; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:self; 102, identifier:_erase_buffer; 103, argument_list; 103, 104; 104, attribute; 104, 105; 104, 106; 105, identifier:stream; 106, identifier:output; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 114; 109, attribute; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:self; 112, identifier:_engine; 113, identifier:push; 114, argument_list; 114, 115; 115, identifier:reading; 116, for_statement; 116, 117; 116, 118; 116, 121; 116, 122; 117, identifier:walker; 118, attribute; 118, 119; 118, 120; 119, identifier:self; 120, identifier:_queue_walkers; 121, comment; 122, block; 122, 123; 123, if_statement; 123, 124; 123, 131; 124, comparison_operator:==; 124, 125; 124, 130; 125, attribute; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:walker; 128, identifier:selector; 129, identifier:output; 130, identifier:output_buffer; 131, block; 131, 132; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:walker; 136, identifier:notify_added; 137, argument_list; 137, 138; 138, identifier:stream; 139, comment; 140, for_statement; 140, 141; 140, 142; 140, 145; 141, identifier:selector; 142, attribute; 142, 143; 142, 144; 143, identifier:self; 144, identifier:_monitors; 145, block; 145, 146; 146, if_statement; 146, 147; 146, 157; 147, boolean_operator:or; 147, 148; 147, 151; 148, comparison_operator:is; 148, 149; 148, 150; 149, identifier:selector; 150, None; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:selector; 154, identifier:matches; 155, argument_list; 155, 156; 156, identifier:stream; 157, block; 157, 158; 158, for_statement; 158, 159; 158, 160; 158, 165; 159, identifier:callback; 160, subscript; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:self; 163, identifier:_monitors; 164, identifier:selector; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, call; 167, 168; 167, 169; 168, identifier:callback; 169, argument_list; 169, 170; 169, 171; 170, identifier:stream; 171, identifier:reading; 172, comment; 173, comment; 174, for_statement; 174, 175; 174, 176; 174, 179; 175, identifier:walker; 176, attribute; 176, 177; 176, 178; 177, identifier:self; 178, identifier:_virtual_walkers; 179, block; 179, 180; 180, if_statement; 180, 181; 180, 187; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:walker; 184, identifier:matches; 185, argument_list; 185, 186; 186, identifier:stream; 187, block; 187, 188; 188, expression_statement; 188, 189; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:walker; 192, identifier:push; 193, argument_list; 193, 194; 193, 195; 194, identifier:stream; 195, identifier:reading; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 203; 198, subscript; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:self; 201, identifier:_last_values; 202, identifier:stream; 203, identifier:reading
def push(self, stream, reading): """Push a reading into a stream, updating any associated stream walkers. Args: stream (DataStream): the stream to push the reading into reading (IOTileReading): the reading to push """ # Make sure the stream is correct reading = copy.copy(reading) reading.stream = stream.encode() if stream.buffered: output_buffer = stream.output if self.id_assigner is not None: reading.reading_id = self.id_assigner(stream, reading) try: self._engine.push(reading) except StorageFullError: # If we are in fill-stop mode, don't auto erase old data. if (stream.output and not self._rollover_streaming) or (not stream.output and not self._rollover_storage): raise self._erase_buffer(stream.output) self._engine.push(reading) for walker in self._queue_walkers: # Only notify the walkers that are on this queue if walker.selector.output == output_buffer: walker.notify_added(stream) # Activate any monitors we have for this stream for selector in self._monitors: if selector is None or selector.matches(stream): for callback in self._monitors[selector]: callback(stream, reading) # Virtual streams live only in their walkers, so update each walker # that contains this stream. for walker in self._virtual_walkers: if walker.matches(stream): walker.push(stream, reading) self._last_values[stream] = reading
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:inspect_last; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:stream; 6, default_parameter; 6, 7; 6, 8; 7, identifier:only_allocated; 8, False; 9, block; 9, 10; 9, 12; 9, 50; 9, 63; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 14; 13, identifier:only_allocated; 14, block; 14, 15; 14, 19; 14, 38; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:found; 18, False; 19, for_statement; 19, 20; 19, 21; 19, 24; 20, identifier:walker; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:_virtual_walkers; 24, block; 24, 25; 25, if_statement; 25, 26; 25, 32; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:walker; 29, identifier:matches; 30, argument_list; 30, 31; 31, identifier:stream; 32, block; 32, 33; 32, 37; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:found; 36, True; 37, break_statement; 38, if_statement; 38, 39; 38, 41; 39, not_operator; 39, 40; 40, identifier:found; 41, block; 41, 42; 42, raise_statement; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:UnresolvedIdentifierError; 45, argument_list; 45, 46; 45, 47; 46, string:"inspect_last could not find an allocated virtual streamer for the desired stream"; 47, keyword_argument; 47, 48; 47, 49; 48, identifier:stream; 49, identifier:stream; 50, if_statement; 50, 51; 50, 56; 51, comparison_operator:in; 51, 52; 51, 53; 52, identifier:stream; 53, attribute; 53, 54; 53, 55; 54, identifier:self; 55, identifier:_last_values; 56, block; 56, 57; 57, return_statement; 57, 58; 58, subscript; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:_last_values; 62, identifier:stream; 63, raise_statement; 63, 64; 64, call; 64, 65; 64, 66; 65, identifier:StreamEmptyError; 66, argument_list; 66, 67; 66, 68; 67, string:u"inspect_last called on stream that has never been written to"; 68, keyword_argument; 68, 69; 68, 70; 69, identifier:stream; 70, identifier:stream
def inspect_last(self, stream, only_allocated=False): """Return the last value pushed into a stream. This function works even if the stream is virtual and no virtual walker has been created for it. It is primarily useful to aid in debugging sensor graphs. Args: stream (DataStream): The stream to inspect. only_allocated (bool): Optional parameter to only allow inspection of allocated virtual streams. This is useful for mimicking the behavior of an embedded device that does not have a _last_values array. Returns: IOTileReading: The data in the stream Raises: StreamEmptyError: if there has never been data written to the stream. UnresolvedIdentifierError: if only_allocated is True and there has not been a virtual stream walker allocated to listen to this stream. """ if only_allocated: found = False for walker in self._virtual_walkers: if walker.matches(stream): found = True break if not found: raise UnresolvedIdentifierError("inspect_last could not find an allocated virtual streamer for the desired stream", stream=stream) if stream in self._last_values: return self._last_values[stream] raise StreamEmptyError(u"inspect_last called on stream that has never been written to", stream=stream)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_dllEmitter; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:target; 5, identifier:source; 6, identifier:env; 7, identifier:paramtp; 8, block; 8, 9; 8, 11; 8, 22; 8, 26; 8, 30; 8, 45; 8, 55; 8, 77; 8, 86; 8, 127; 8, 148; 8, 192; 8, 235; 8, 283; 8, 340; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, call; 12, 13; 12, 20; 13, attribute; 13, 14; 13, 19; 14, attribute; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:SCons; 17, identifier:Tool; 18, identifier:msvc; 19, identifier:validate_vars; 20, argument_list; 20, 21; 21, identifier:env; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:extratargets; 25, list:[]; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:extrasources; 29, list:[]; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:dll; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:env; 36, identifier:FindIxes; 37, argument_list; 37, 38; 37, 39; 37, 42; 38, identifier:target; 39, binary_operator:%; 39, 40; 39, 41; 40, string:'%sPREFIX'; 41, identifier:paramtp; 42, binary_operator:%; 42, 43; 42, 44; 43, string:'%sSUFFIX'; 44, identifier:paramtp; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:no_import_lib; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:env; 51, identifier:get; 52, argument_list; 52, 53; 52, 54; 53, string:'no_import_lib'; 54, integer:0; 55, if_statement; 55, 56; 55, 58; 56, not_operator; 56, 57; 57, identifier:dll; 58, block; 58, 59; 59, raise_statement; 59, 60; 60, call; 60, 61; 60, 66; 61, attribute; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:SCons; 64, identifier:Errors; 65, identifier:UserError; 66, argument_list; 66, 67; 67, binary_operator:%; 67, 68; 67, 69; 68, string:'A shared library should have exactly one target with the suffix: %s'; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:env; 72, identifier:subst; 73, argument_list; 73, 74; 74, binary_operator:%; 74, 75; 74, 76; 75, string:'$%sSUFFIX'; 76, identifier:paramtp; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:insert_def; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:env; 83, identifier:subst; 84, argument_list; 84, 85; 85, string:"$WINDOWS_INSERT_DEF"; 86, if_statement; 86, 87; 86, 105; 86, 106; 87, boolean_operator:and; 87, 88; 87, 95; 87, 96; 88, not_operator; 88, 89; 89, comparison_operator:in; 89, 90; 89, 91; 90, identifier:insert_def; 91, list:['', '0', 0]; 91, 92; 91, 93; 91, 94; 92, string:''; 93, string:'0'; 94, integer:0; 95, line_continuation:\; 96, not_operator; 96, 97; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:env; 100, identifier:FindIxes; 101, argument_list; 101, 102; 101, 103; 101, 104; 102, identifier:source; 103, string:"WINDOWSDEFPREFIX"; 104, string:"WINDOWSDEFSUFFIX"; 105, comment; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:extrasources; 111, identifier:append; 112, argument_list; 112, 113; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:env; 116, identifier:ReplaceIxes; 117, argument_list; 117, 118; 117, 119; 117, 122; 117, 125; 117, 126; 118, identifier:dll; 119, binary_operator:%; 119, 120; 119, 121; 120, string:'%sPREFIX'; 121, identifier:paramtp; 122, binary_operator:%; 122, 123; 122, 124; 123, string:'%sSUFFIX'; 124, identifier:paramtp; 125, string:"WINDOWSDEFPREFIX"; 126, string:"WINDOWSDEFSUFFIX"; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 132; 129, pattern_list; 129, 130; 129, 131; 130, identifier:version_num; 131, identifier:suite; 132, call; 132, 133; 132, 140; 133, attribute; 133, 134; 133, 139; 134, attribute; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:SCons; 137, identifier:Tool; 138, identifier:msvs; 139, identifier:msvs_parse_version; 140, argument_list; 140, 141; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:env; 144, identifier:get; 145, argument_list; 145, 146; 145, 147; 146, string:'MSVS_VERSION'; 147, string:'6.0'; 148, if_statement; 148, 149; 148, 170; 148, 171; 149, boolean_operator:and; 149, 150; 149, 153; 149, 154; 150, comparison_operator:>=; 150, 151; 150, 152; 151, identifier:version_num; 152, float:8.0; 153, line_continuation:\; 154, parenthesized_expression; 154, 155; 155, boolean_operator:or; 155, 156; 155, 163; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:env; 159, identifier:get; 160, argument_list; 160, 161; 160, 162; 161, string:'WINDOWS_INSERT_MANIFEST'; 162, integer:0; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:env; 166, identifier:get; 167, argument_list; 167, 168; 167, 169; 168, string:'WINDOWS_EMBED_MANIFEST'; 169, integer:0; 170, comment; 171, block; 171, 172; 172, expression_statement; 172, 173; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:extratargets; 176, identifier:append; 177, argument_list; 177, 178; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:env; 181, identifier:ReplaceIxes; 182, argument_list; 182, 183; 182, 184; 182, 187; 182, 190; 182, 191; 183, identifier:dll; 184, binary_operator:%; 184, 185; 184, 186; 185, string:'%sPREFIX'; 186, identifier:paramtp; 187, binary_operator:%; 187, 188; 187, 189; 188, string:'%sSUFFIX'; 189, identifier:paramtp; 190, string:"WINDOWSSHLIBMANIFESTPREFIX"; 191, string:"WINDOWSSHLIBMANIFESTSUFFIX"; 192, if_statement; 192, 193; 192, 200; 193, boolean_operator:and; 193, 194; 193, 197; 194, comparison_operator:in; 194, 195; 194, 196; 195, string:'PDB'; 196, identifier:env; 197, subscript; 197, 198; 197, 199; 198, identifier:env; 199, string:'PDB'; 200, block; 200, 201; 200, 218; 200, 225; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 204; 203, identifier:pdb; 204, subscript; 204, 205; 204, 217; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:env; 208, identifier:arg2nodes; 209, argument_list; 209, 210; 209, 211; 209, 214; 210, string:'$PDB'; 211, keyword_argument; 211, 212; 211, 213; 212, identifier:target; 213, identifier:target; 214, keyword_argument; 214, 215; 214, 216; 215, identifier:source; 216, identifier:source; 217, integer:0; 218, expression_statement; 218, 219; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:extratargets; 222, identifier:append; 223, argument_list; 223, 224; 224, identifier:pdb; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 234; 227, attribute; 227, 228; 227, 233; 228, attribute; 228, 229; 228, 232; 229, subscript; 229, 230; 229, 231; 230, identifier:target; 231, integer:0; 232, identifier:attributes; 233, identifier:pdb; 234, identifier:pdb; 235, if_statement; 235, 236; 235, 247; 235, 248; 235, 249; 236, boolean_operator:and; 236, 237; 236, 240; 237, comparison_operator:>=; 237, 238; 237, 239; 238, identifier:version_num; 239, float:11.0; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:env; 243, identifier:get; 244, argument_list; 244, 245; 244, 246; 245, string:'PCH'; 246, integer:0; 247, comment; 248, comment; 249, block; 249, 250; 249, 270; 249, 271; 250, expression_statement; 250, 251; 251, assignment; 251, 252; 251, 253; 252, identifier:pchobj; 253, binary_operator:+; 253, 254; 253, 269; 254, subscript; 254, 255; 254, 268; 255, call; 255, 256; 255, 261; 256, attribute; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:SCons; 259, identifier:Util; 260, identifier:splitext; 261, argument_list; 261, 262; 262, call; 262, 263; 262, 264; 263, identifier:str; 264, argument_list; 264, 265; 265, subscript; 265, 266; 265, 267; 266, identifier:env; 267, string:'PCH'; 268, integer:0; 269, string:'.obj'; 270, comment; 271, if_statement; 271, 272; 271, 275; 272, comparison_operator:not; 272, 273; 272, 274; 273, identifier:pchobj; 274, identifier:extrasources; 275, block; 275, 276; 276, expression_statement; 276, 277; 277, call; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:extrasources; 280, identifier:append; 281, argument_list; 281, 282; 282, identifier:pchobj; 283, if_statement; 283, 284; 283, 297; 283, 298; 284, boolean_operator:and; 284, 285; 284, 287; 284, 288; 285, not_operator; 285, 286; 286, identifier:no_import_lib; 287, line_continuation:\; 288, not_operator; 288, 289; 289, call; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:env; 292, identifier:FindIxes; 293, argument_list; 293, 294; 293, 295; 293, 296; 294, identifier:target; 295, string:"LIBPREFIX"; 296, string:"LIBSUFFIX"; 297, comment; 298, block; 298, 299; 298, 319; 298, 320; 299, expression_statement; 299, 300; 300, call; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:extratargets; 303, identifier:append; 304, argument_list; 304, 305; 305, call; 305, 306; 305, 309; 306, attribute; 306, 307; 306, 308; 307, identifier:env; 308, identifier:ReplaceIxes; 309, argument_list; 309, 310; 309, 311; 309, 314; 309, 317; 309, 318; 310, identifier:dll; 311, binary_operator:%; 311, 312; 311, 313; 312, string:'%sPREFIX'; 313, identifier:paramtp; 314, binary_operator:%; 314, 315; 314, 316; 315, string:'%sSUFFIX'; 316, identifier:paramtp; 317, string:"LIBPREFIX"; 318, string:"LIBSUFFIX"; 319, comment; 320, expression_statement; 320, 321; 321, call; 321, 322; 321, 325; 322, attribute; 322, 323; 322, 324; 323, identifier:extratargets; 324, identifier:append; 325, argument_list; 325, 326; 326, call; 326, 327; 326, 330; 327, attribute; 327, 328; 327, 329; 328, identifier:env; 329, identifier:ReplaceIxes; 330, argument_list; 330, 331; 330, 332; 330, 335; 330, 338; 330, 339; 331, identifier:dll; 332, binary_operator:%; 332, 333; 332, 334; 333, string:'%sPREFIX'; 334, identifier:paramtp; 335, binary_operator:%; 335, 336; 335, 337; 336, string:'%sSUFFIX'; 337, identifier:paramtp; 338, string:"WINDOWSEXPPREFIX"; 339, string:"WINDOWSEXPSUFFIX"; 340, return_statement; 340, 341; 341, tuple; 341, 342; 341, 345; 342, binary_operator:+; 342, 343; 342, 344; 343, identifier:target; 344, identifier:extratargets; 345, binary_operator:+; 345, 346; 345, 347; 346, identifier:source; 347, identifier:extrasources
def _dllEmitter(target, source, env, paramtp): """Common implementation of dll emitter.""" SCons.Tool.msvc.validate_vars(env) extratargets = [] extrasources = [] dll = env.FindIxes(target, '%sPREFIX' % paramtp, '%sSUFFIX' % paramtp) no_import_lib = env.get('no_import_lib', 0) if not dll: raise SCons.Errors.UserError('A shared library should have exactly one target with the suffix: %s' % env.subst('$%sSUFFIX' % paramtp)) insert_def = env.subst("$WINDOWS_INSERT_DEF") if not insert_def in ['', '0', 0] and \ not env.FindIxes(source, "WINDOWSDEFPREFIX", "WINDOWSDEFSUFFIX"): # append a def file to the list of sources extrasources.append( env.ReplaceIxes(dll, '%sPREFIX' % paramtp, '%sSUFFIX' % paramtp, "WINDOWSDEFPREFIX", "WINDOWSDEFSUFFIX")) version_num, suite = SCons.Tool.msvs.msvs_parse_version(env.get('MSVS_VERSION', '6.0')) if version_num >= 8.0 and \ (env.get('WINDOWS_INSERT_MANIFEST', 0) or env.get('WINDOWS_EMBED_MANIFEST', 0)): # MSVC 8 and above automatically generate .manifest files that must be installed extratargets.append( env.ReplaceIxes(dll, '%sPREFIX' % paramtp, '%sSUFFIX' % paramtp, "WINDOWSSHLIBMANIFESTPREFIX", "WINDOWSSHLIBMANIFESTSUFFIX")) if 'PDB' in env and env['PDB']: pdb = env.arg2nodes('$PDB', target=target, source=source)[0] extratargets.append(pdb) target[0].attributes.pdb = pdb if version_num >= 11.0 and env.get('PCH', 0): # MSVC 11 and above need the PCH object file to be added to the link line, # otherwise you get link error LNK2011. pchobj = SCons.Util.splitext(str(env['PCH']))[0] + '.obj' # print "prog_emitter, version %s, appending pchobj %s"%(version_num, pchobj) if pchobj not in extrasources: extrasources.append(pchobj) if not no_import_lib and \ not env.FindIxes(target, "LIBPREFIX", "LIBSUFFIX"): # Append an import library to the list of targets. extratargets.append( env.ReplaceIxes(dll, '%sPREFIX' % paramtp, '%sSUFFIX' % paramtp, "LIBPREFIX", "LIBSUFFIX")) # and .exp file is created if there are exports from a DLL extratargets.append( env.ReplaceIxes(dll, '%sPREFIX' % paramtp, '%sSUFFIX' % paramtp, "WINDOWSEXPPREFIX", "WINDOWSEXPSUFFIX")) return (target+extratargets, source+extrasources)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:setup_environment; 3, parameters; 3, 4; 3, 5; 4, identifier:chip; 5, default_parameter; 5, 6; 5, 7; 6, identifier:args_file; 7, None; 8, block; 8, 9; 8, 11; 8, 17; 8, 18; 8, 59; 8, 65; 8, 71; 8, 77; 8, 83; 8, 93; 8, 99; 8, 100; 8, 106; 8, 112; 8, 118; 8, 124; 8, 130; 8, 131; 8, 132; 8, 138; 8, 139; 8, 172; 8, 173; 8, 184; 8, 195; 8, 209; 8, 210; 8, 224; 8, 225; 8, 240; 8, 246; 8, 262; 8, 263; 8, 279; 8, 295; 8, 311; 8, 312; 8, 318; 8, 324; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:config; 14, call; 14, 15; 14, 16; 15, identifier:ConfigManager; 16, argument_list; 17, comment; 18, if_statement; 18, 19; 18, 26; 18, 42; 19, comparison_operator:==; 19, 20; 19, 25; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:platform; 23, identifier:system; 24, argument_list; 25, string:'Windows'; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:env; 30, call; 30, 31; 30, 32; 31, identifier:Environment; 32, argument_list; 32, 33; 32, 37; 33, keyword_argument; 33, 34; 33, 35; 34, identifier:tools; 35, list:['mingw']; 35, 36; 36, string:'mingw'; 37, keyword_argument; 37, 38; 37, 39; 38, identifier:ENV; 39, attribute; 39, 40; 39, 41; 40, identifier:os; 41, identifier:environ; 42, else_clause; 42, 43; 43, block; 43, 44; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:env; 47, call; 47, 48; 47, 49; 48, identifier:Environment; 49, argument_list; 49, 50; 49, 54; 50, keyword_argument; 50, 51; 50, 52; 51, identifier:tools; 52, list:['default']; 52, 53; 53, string:'default'; 54, keyword_argument; 54, 55; 54, 56; 55, identifier:ENV; 56, attribute; 56, 57; 56, 58; 57, identifier:os; 58, identifier:environ; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 64; 61, subscript; 61, 62; 61, 63; 62, identifier:env; 63, string:'INCPREFIX'; 64, string:'-I"'; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 70; 67, subscript; 67, 68; 67, 69; 68, identifier:env; 69, string:'INCSUFFIX'; 70, string:'"'; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 76; 73, subscript; 73, 74; 73, 75; 74, identifier:env; 75, string:'CPPDEFPREFIX'; 76, string:''; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 82; 79, subscript; 79, 80; 79, 81; 80, identifier:env; 81, string:'CPPDEFSUFFIX'; 82, string:''; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 88; 85, subscript; 85, 86; 85, 87; 86, identifier:env; 87, string:'CPPPATH'; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:chip; 91, identifier:includes; 92, argument_list; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 98; 95, subscript; 95, 96; 95, 97; 96, identifier:env; 97, string:'ARCH'; 98, identifier:chip; 99, comment; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 105; 102, subscript; 102, 103; 102, 104; 103, identifier:env; 104, string:'CC'; 105, string:'arm-none-eabi-gcc'; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 111; 108, subscript; 108, 109; 108, 110; 109, identifier:env; 110, string:'AS'; 111, string:'arm-none-eabi-gcc'; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 117; 114, subscript; 114, 115; 114, 116; 115, identifier:env; 116, string:'LINK'; 117, string:'arm-none-eabi-gcc'; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 123; 120, subscript; 120, 121; 120, 122; 121, identifier:env; 122, string:'AR'; 123, string:'arm-none-eabi-ar'; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 129; 126, subscript; 126, 127; 126, 128; 127, identifier:env; 128, string:'RANLIB'; 129, string:'arm-none-eabi-ranlib'; 130, comment; 131, comment; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 137; 134, subscript; 134, 135; 134, 136; 135, identifier:env; 136, string:'ASCOM'; 137, string:"$AS $ASFLAGS -o $TARGET -c $SOURCES"; 138, comment; 139, if_statement; 139, 140; 139, 147; 140, not_operator; 140, 141; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:config; 144, identifier:get; 145, argument_list; 145, 146; 146, string:'build:show-commands'; 147, block; 147, 148; 147, 154; 147, 160; 147, 166; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 153; 150, subscript; 150, 151; 150, 152; 151, identifier:env; 152, string:'CCCOMSTR'; 153, string:"Compiling $TARGET"; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 159; 156, subscript; 156, 157; 156, 158; 157, identifier:env; 158, string:'ARCOMSTR'; 159, string:"Building static library $TARGET"; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 165; 162, subscript; 162, 163; 162, 164; 163, identifier:env; 164, string:'RANLIBCOMSTR'; 165, string:"Indexing static library $TARGET"; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 171; 168, subscript; 168, 169; 168, 170; 169, identifier:env; 170, string:'LINKCOMSTR'; 171, string:"Linking $TARGET"; 172, comment; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 178; 175, subscript; 175, 176; 175, 177; 176, identifier:env; 177, string:'CCFLAGS'; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:chip; 181, identifier:combined_properties; 182, argument_list; 182, 183; 183, string:'cflags'; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 189; 186, subscript; 186, 187; 186, 188; 187, identifier:env; 188, string:'LINKFLAGS'; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:chip; 192, identifier:combined_properties; 193, argument_list; 193, 194; 194, string:'ldflags'; 195, expression_statement; 195, 196; 196, call; 196, 197; 196, 202; 197, attribute; 197, 198; 197, 201; 198, subscript; 198, 199; 198, 200; 199, identifier:env; 200, string:'ARFLAGS'; 201, identifier:append; 202, argument_list; 202, 203; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:chip; 206, identifier:combined_properties; 207, argument_list; 207, 208; 208, string:'arflags'; 209, comment; 210, expression_statement; 210, 211; 211, call; 211, 212; 211, 217; 212, attribute; 212, 213; 212, 216; 213, subscript; 213, 214; 213, 215; 214, identifier:env; 215, string:'ASFLAGS'; 216, identifier:append; 217, argument_list; 217, 218; 218, call; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:chip; 221, identifier:combined_properties; 222, argument_list; 222, 223; 223, string:'asflags'; 224, comment; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:defines; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:utilities; 231, identifier:build_defines; 232, argument_list; 232, 233; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:chip; 236, identifier:property; 237, argument_list; 237, 238; 237, 239; 238, string:'defines'; 239, dictionary; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 245; 242, subscript; 242, 243; 242, 244; 243, identifier:env; 244, string:'CPPDEFINES'; 245, identifier:defines; 246, if_statement; 246, 247; 246, 250; 247, comparison_operator:is; 247, 248; 247, 249; 248, identifier:args_file; 249, None; 250, block; 250, 251; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 256; 253, subscript; 253, 254; 253, 255; 254, identifier:env; 255, string:'CCCOM'; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, string:"$CC $CCFLAGS $CPPFLAGS @{} -c -o $TARGET $SOURCES"; 259, identifier:format; 260, argument_list; 260, 261; 261, identifier:args_file; 262, comment; 263, expression_statement; 263, 264; 264, call; 264, 265; 264, 270; 265, attribute; 265, 266; 265, 269; 266, subscript; 266, 267; 266, 268; 267, identifier:env; 268, string:'CCFLAGS'; 269, identifier:append; 270, argument_list; 270, 271; 271, binary_operator:%; 271, 272; 271, 273; 272, string:'-mcpu=%s'; 273, call; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:chip; 276, identifier:property; 277, argument_list; 277, 278; 278, string:'cpu'; 279, expression_statement; 279, 280; 280, call; 280, 281; 280, 286; 281, attribute; 281, 282; 281, 285; 282, subscript; 282, 283; 282, 284; 283, identifier:env; 284, string:'ASFLAGS'; 285, identifier:append; 286, argument_list; 286, 287; 287, binary_operator:%; 287, 288; 287, 289; 288, string:'-mcpu=%s'; 289, call; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:chip; 292, identifier:property; 293, argument_list; 293, 294; 294, string:'cpu'; 295, expression_statement; 295, 296; 296, call; 296, 297; 296, 302; 297, attribute; 297, 298; 297, 301; 298, subscript; 298, 299; 298, 300; 299, identifier:env; 300, string:'LINKFLAGS'; 301, identifier:append; 302, argument_list; 302, 303; 303, binary_operator:%; 303, 304; 303, 305; 304, string:'-mcpu=%s'; 305, call; 305, 306; 305, 309; 306, attribute; 306, 307; 306, 308; 307, identifier:chip; 308, identifier:property; 309, argument_list; 309, 310; 310, string:'cpu'; 311, comment; 312, expression_statement; 312, 313; 313, assignment; 313, 314; 313, 317; 314, subscript; 314, 315; 314, 316; 315, identifier:env; 316, string:'LIBPATH'; 317, list:[]; 318, expression_statement; 318, 319; 319, assignment; 319, 320; 319, 323; 320, subscript; 320, 321; 320, 322; 321, identifier:env; 322, string:'LIBS'; 323, list:[]; 324, return_statement; 324, 325; 325, identifier:env
def setup_environment(chip, args_file=None): """Setup the SCons environment for compiling arm cortex code. This will return an env that has all of the correct settings and create a command line arguments file for GCC that contains all of the required flags. The use of a command line argument file passed with @./file_path is important since there can be many flags that exceed the maximum allowed length of a command line on Windows. """ config = ConfigManager() # Make sure we never get MSVC settings for windows since that has the wrong command line flags for gcc if platform.system() == 'Windows': env = Environment(tools=['mingw'], ENV=os.environ) else: env = Environment(tools=['default'], ENV=os.environ) env['INCPREFIX'] = '-I"' env['INCSUFFIX'] = '"' env['CPPDEFPREFIX'] = '' env['CPPDEFSUFFIX'] = '' env['CPPPATH'] = chip.includes() env['ARCH'] = chip # Setup Cross Compiler env['CC'] = 'arm-none-eabi-gcc' env['AS'] = 'arm-none-eabi-gcc' env['LINK'] = 'arm-none-eabi-gcc' env['AR'] = 'arm-none-eabi-ar' env['RANLIB'] = 'arm-none-eabi-ranlib' # AS command line is by default setup for call as directly so we need # to modify it to call via *-gcc to allow for preprocessing env['ASCOM'] = "$AS $ASFLAGS -o $TARGET -c $SOURCES" # Setup nice display strings unless we're asked to show raw commands if not config.get('build:show-commands'): env['CCCOMSTR'] = "Compiling $TARGET" env['ARCOMSTR'] = "Building static library $TARGET" env['RANLIBCOMSTR'] = "Indexing static library $TARGET" env['LINKCOMSTR'] = "Linking $TARGET" # Setup Compiler Flags env['CCFLAGS'] = chip.combined_properties('cflags') env['LINKFLAGS'] = chip.combined_properties('ldflags') env['ARFLAGS'].append(chip.combined_properties('arflags')) # There are default ARFLAGS that are necessary to keep env['ASFLAGS'].append(chip.combined_properties('asflags')) # Add in compile tile definitions defines = utilities.build_defines(chip.property('defines', {})) env['CPPDEFINES'] = defines if args_file is not None: env['CCCOM'] = "$CC $CCFLAGS $CPPFLAGS @{} -c -o $TARGET $SOURCES".format(args_file) # Setup Target Architecture env['CCFLAGS'].append('-mcpu=%s' % chip.property('cpu')) env['ASFLAGS'].append('-mcpu=%s' % chip.property('cpu')) env['LINKFLAGS'].append('-mcpu=%s' % chip.property('cpu')) # Initialize library paths (all libraries are added via dependencies) env['LIBPATH'] = [] env['LIBS'] = [] return env
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:fastqRead; 3, parameters; 3, 4; 4, identifier:fileHandleOrFile; 5, block; 5, 6; 5, 8; 5, 15; 5, 23; 5, 211; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:fileHandle; 11, call; 11, 12; 11, 13; 12, identifier:_getFileHandle; 13, argument_list; 13, 14; 14, identifier:fileHandleOrFile; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:line; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:fileHandle; 21, identifier:readline; 22, argument_list; 23, while_statement; 23, 24; 23, 27; 24, comparison_operator:!=; 24, 25; 24, 26; 25, identifier:line; 26, string:''; 27, block; 27, 28; 27, 203; 28, if_statement; 28, 29; 28, 34; 29, comparison_operator:==; 29, 30; 29, 33; 30, subscript; 30, 31; 30, 32; 31, identifier:line; 32, integer:0; 33, string:'@'; 34, block; 34, 35; 34, 45; 34, 58; 34, 66; 34, 80; 34, 100; 34, 155; 34, 197; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:name; 38, subscript; 38, 39; 38, 40; 39, identifier:line; 40, slice; 40, 41; 40, 42; 40, 43; 41, integer:1; 42, colon; 43, unary_operator:-; 43, 44; 44, integer:1; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:seq; 48, subscript; 48, 49; 48, 54; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:fileHandle; 52, identifier:readline; 53, argument_list; 54, slice; 54, 55; 54, 56; 55, colon; 56, unary_operator:-; 56, 57; 57, integer:1; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:plus; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:fileHandle; 64, identifier:readline; 65, argument_list; 66, if_statement; 66, 67; 66, 72; 67, comparison_operator:!=; 67, 68; 67, 71; 68, subscript; 68, 69; 68, 70; 69, identifier:plus; 70, integer:0; 71, string:'+'; 72, block; 72, 73; 73, raise_statement; 73, 74; 74, call; 74, 75; 74, 76; 75, identifier:RuntimeError; 76, argument_list; 76, 77; 77, binary_operator:%; 77, 78; 77, 79; 78, string:"Got unexpected line: %s"; 79, identifier:plus; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:qualValues; 83, list_comprehension; 83, 84; 83, 88; 84, call; 84, 85; 84, 86; 85, identifier:ord; 86, argument_list; 86, 87; 87, identifier:i; 88, for_in_clause; 88, 89; 88, 90; 89, identifier:i; 90, subscript; 90, 91; 90, 96; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:fileHandle; 94, identifier:readline; 95, argument_list; 96, slice; 96, 97; 96, 98; 97, colon; 98, unary_operator:-; 98, 99; 99, integer:1; 100, if_statement; 100, 101; 100, 110; 100, 133; 101, comparison_operator:!=; 101, 102; 101, 106; 102, call; 102, 103; 102, 104; 103, identifier:len; 104, argument_list; 104, 105; 105, identifier:seq; 106, call; 106, 107; 106, 108; 107, identifier:len; 108, argument_list; 108, 109; 109, identifier:qualValues; 110, block; 110, 111; 110, 129; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:logger; 115, identifier:critical; 116, argument_list; 116, 117; 117, binary_operator:%; 117, 118; 117, 119; 118, string:"Got a mismatch between the number of sequence characters (%s) and number of qual values (%s) for sequence: %s, ignoring returning None"; 119, tuple; 119, 120; 119, 124; 119, 128; 120, call; 120, 121; 120, 122; 121, identifier:len; 122, argument_list; 122, 123; 123, identifier:seq; 124, call; 124, 125; 124, 126; 125, identifier:len; 126, argument_list; 126, 127; 127, identifier:qualValues; 128, identifier:name; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:qualValues; 132, None; 133, else_clause; 133, 134; 134, block; 134, 135; 135, for_statement; 135, 136; 135, 137; 135, 138; 136, identifier:i; 137, identifier:qualValues; 138, block; 138, 139; 139, if_statement; 139, 140; 139, 147; 140, boolean_operator:or; 140, 141; 140, 144; 141, comparison_operator:<; 141, 142; 141, 143; 142, identifier:i; 143, integer:33; 144, comparison_operator:>; 144, 145; 144, 146; 145, identifier:i; 146, integer:126; 147, block; 147, 148; 148, raise_statement; 148, 149; 149, call; 149, 150; 149, 151; 150, identifier:RuntimeError; 151, argument_list; 151, 152; 152, binary_operator:%; 152, 153; 152, 154; 153, string:"Got a qual value out of range %s (range is 33 to 126)"; 154, identifier:i; 155, for_statement; 155, 156; 155, 157; 155, 158; 155, 159; 156, identifier:i; 157, identifier:seq; 158, comment; 159, block; 159, 160; 160, if_statement; 160, 161; 160, 184; 161, not_operator; 161, 162; 162, parenthesized_expression; 162, 163; 163, boolean_operator:or; 163, 164; 163, 181; 164, boolean_operator:or; 164, 165; 164, 173; 165, parenthesized_expression; 165, 166; 166, boolean_operator:and; 166, 167; 166, 170; 167, comparison_operator:>=; 167, 168; 167, 169; 168, identifier:i; 169, string:'A'; 170, comparison_operator:<=; 170, 171; 170, 172; 171, identifier:i; 172, string:'Z'; 173, parenthesized_expression; 173, 174; 174, boolean_operator:and; 174, 175; 174, 178; 175, comparison_operator:>=; 175, 176; 175, 177; 176, identifier:i; 177, string:'a'; 178, comparison_operator:<=; 178, 179; 178, 180; 179, identifier:i; 180, string:'z'; 181, comparison_operator:==; 181, 182; 181, 183; 182, identifier:i; 183, string:'-'; 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, 191; 190, string:"Invalid FASTQ character, ASCII code = \'%d\', found in input sequence %s"; 191, tuple; 191, 192; 191, 196; 192, call; 192, 193; 192, 194; 193, identifier:ord; 194, argument_list; 194, 195; 195, identifier:i; 196, identifier:name; 197, expression_statement; 197, 198; 198, yield; 198, 199; 199, expression_list; 199, 200; 199, 201; 199, 202; 200, identifier:name; 201, identifier:seq; 202, identifier:qualValues; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:line; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:fileHandle; 209, identifier:readline; 210, argument_list; 211, if_statement; 211, 212; 211, 219; 212, call; 212, 213; 212, 214; 213, identifier:isinstance; 214, argument_list; 214, 215; 214, 216; 215, identifier:fileHandleOrFile; 216, attribute; 216, 217; 216, 218; 217, string:""; 218, identifier:__class__; 219, block; 219, 220; 220, expression_statement; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:fileHandle; 224, identifier:close; 225, argument_list
def fastqRead(fileHandleOrFile): """Reads a fastq file iteratively """ fileHandle = _getFileHandle(fileHandleOrFile) line = fileHandle.readline() while line != '': if line[0] == '@': name = line[1:-1] seq = fileHandle.readline()[:-1] plus = fileHandle.readline() if plus[0] != '+': raise RuntimeError("Got unexpected line: %s" % plus) qualValues = [ ord(i) for i in fileHandle.readline()[:-1] ] if len(seq) != len(qualValues): logger.critical("Got a mismatch between the number of sequence characters (%s) and number of qual values (%s) for sequence: %s, ignoring returning None" % (len(seq), len(qualValues), name)) qualValues = None else: for i in qualValues: if i < 33 or i > 126: raise RuntimeError("Got a qual value out of range %s (range is 33 to 126)" % i) for i in seq: #For safety and sanity I only allows roman alphabet characters in fasta sequences. if not ((i >= 'A' and i <= 'Z') or (i >= 'a' and i <= 'z') or i == '-'): raise RuntimeError("Invalid FASTQ character, ASCII code = \'%d\', found in input sequence %s" % (ord(i), name)) yield name, seq, qualValues line = fileHandle.readline() if isinstance(fileHandleOrFile, "".__class__): fileHandle.close()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:newickTreeParser; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 9; 3, 12; 4, identifier:newickTree; 5, default_parameter; 5, 6; 5, 7; 6, identifier:defaultDistance; 7, identifier:DEFAULT_DISTANCE; 8, line_continuation:\; 9, default_parameter; 9, 10; 9, 11; 10, identifier:sortNonBinaryNodes; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:reportUnaryNodes; 14, False; 15, block; 15, 16; 15, 18; 15, 28; 15, 38; 15, 48; 15, 58; 15, 68; 15, 82; 15, 94; 15, 142; 15, 189; 15, 429; 16, expression_statement; 16, 17; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:newickTree; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:newickTree; 24, identifier:replace; 25, argument_list; 25, 26; 25, 27; 26, string:"("; 27, string:" ( "; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:newickTree; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:newickTree; 34, identifier:replace; 35, argument_list; 35, 36; 35, 37; 36, string:")"; 37, string:" ) "; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:newickTree; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:newickTree; 44, identifier:replace; 45, argument_list; 45, 46; 45, 47; 46, string:":"; 47, string:" : "; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:newickTree; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:newickTree; 54, identifier:replace; 55, argument_list; 55, 56; 55, 57; 56, string:";"; 57, string:""; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:newickTree; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:newickTree; 64, identifier:replace; 65, argument_list; 65, 66; 65, 67; 66, string:","; 67, string:" , "; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:newickTree; 71, call; 71, 72; 71, 80; 72, attribute; 72, 73; 72, 79; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:re; 76, identifier:compile; 77, argument_list; 77, 78; 78, string:"[\s]*"; 79, identifier:split; 80, argument_list; 80, 81; 81, identifier:newickTree; 82, while_statement; 82, 83; 82, 86; 83, comparison_operator:in; 83, 84; 83, 85; 84, string:""; 85, identifier:newickTree; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:newickTree; 91, identifier:remove; 92, argument_list; 92, 93; 93, string:""; 94, function_definition; 94, 95; 94, 96; 94, 99; 95, function_name:fn; 96, parameters; 96, 97; 96, 98; 97, identifier:newickTree; 98, identifier:i; 99, block; 99, 100; 99, 140; 100, if_statement; 100, 101; 100, 109; 101, comparison_operator:<; 101, 102; 101, 105; 102, subscript; 102, 103; 102, 104; 103, identifier:i; 104, integer:0; 105, call; 105, 106; 105, 107; 106, identifier:len; 107, argument_list; 107, 108; 108, identifier:newickTree; 109, block; 109, 110; 110, if_statement; 110, 111; 110, 118; 111, comparison_operator:==; 111, 112; 111, 117; 112, subscript; 112, 113; 112, 114; 113, identifier:newickTree; 114, subscript; 114, 115; 114, 116; 115, identifier:i; 116, integer:0; 117, string:':'; 118, block; 118, 119; 118, 132; 118, 138; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:d; 122, call; 122, 123; 122, 124; 123, identifier:float; 124, argument_list; 124, 125; 125, subscript; 125, 126; 125, 127; 126, identifier:newickTree; 127, binary_operator:+; 127, 128; 127, 131; 128, subscript; 128, 129; 128, 130; 129, identifier:i; 130, integer:0; 131, integer:1; 132, expression_statement; 132, 133; 133, augmented_assignment:+=; 133, 134; 133, 137; 134, subscript; 134, 135; 134, 136; 135, identifier:i; 136, integer:0; 137, integer:2; 138, return_statement; 138, 139; 139, identifier:d; 140, return_statement; 140, 141; 141, identifier:defaultDistance; 142, function_definition; 142, 143; 142, 144; 142, 147; 143, function_name:fn2; 144, parameters; 144, 145; 144, 146; 145, identifier:newickTree; 146, identifier:i; 147, block; 147, 148; 147, 187; 148, if_statement; 148, 149; 148, 157; 149, comparison_operator:<; 149, 150; 149, 153; 150, subscript; 150, 151; 150, 152; 151, identifier:i; 152, integer:0; 153, call; 153, 154; 153, 155; 154, identifier:len; 155, argument_list; 155, 156; 156, identifier:newickTree; 157, block; 157, 158; 157, 166; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:j; 161, subscript; 161, 162; 161, 163; 162, identifier:newickTree; 163, subscript; 163, 164; 163, 165; 164, identifier:i; 165, integer:0; 166, if_statement; 166, 167; 166, 178; 167, boolean_operator:and; 167, 168; 167, 175; 168, boolean_operator:and; 168, 169; 168, 172; 169, comparison_operator:!=; 169, 170; 169, 171; 170, identifier:j; 171, string:':'; 172, comparison_operator:!=; 172, 173; 172, 174; 173, identifier:j; 174, string:')'; 175, comparison_operator:!=; 175, 176; 175, 177; 176, identifier:j; 177, string:','; 178, block; 178, 179; 178, 185; 179, expression_statement; 179, 180; 180, augmented_assignment:+=; 180, 181; 180, 184; 181, subscript; 181, 182; 181, 183; 182, identifier:i; 183, integer:0; 184, integer:1; 185, return_statement; 185, 186; 186, identifier:j; 187, return_statement; 187, 188; 188, None; 189, function_definition; 189, 190; 189, 191; 189, 194; 190, function_name:fn3; 191, parameters; 191, 192; 191, 193; 192, identifier:newickTree; 193, identifier:i; 194, block; 194, 195; 194, 408; 194, 416; 195, if_statement; 195, 196; 195, 203; 195, 204; 196, comparison_operator:==; 196, 197; 196, 202; 197, subscript; 197, 198; 197, 199; 198, identifier:newickTree; 199, subscript; 199, 200; 199, 201; 200, identifier:i; 201, integer:0; 202, string:'('; 203, comment; 204, block; 204, 205; 204, 209; 204, 215; 204, 219; 204, 254; 204, 260; 204, 291; 204, 301; 204, 307; 204, 406; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:subTreeList; 208, list:[]; 209, expression_statement; 209, 210; 210, augmented_assignment:+=; 210, 211; 210, 214; 211, subscript; 211, 212; 211, 213; 212, identifier:i; 213, integer:0; 214, integer:1; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:k; 218, list:[]; 219, while_statement; 219, 220; 219, 227; 220, comparison_operator:!=; 220, 221; 220, 226; 221, subscript; 221, 222; 221, 223; 222, identifier:newickTree; 223, subscript; 223, 224; 223, 225; 224, identifier:i; 225, integer:0; 226, string:')'; 227, block; 227, 228; 227, 243; 228, if_statement; 228, 229; 228, 236; 229, comparison_operator:==; 229, 230; 229, 235; 230, subscript; 230, 231; 230, 232; 231, identifier:newickTree; 232, subscript; 232, 233; 232, 234; 233, identifier:i; 234, integer:0; 235, string:','; 236, block; 236, 237; 237, expression_statement; 237, 238; 238, augmented_assignment:+=; 238, 239; 238, 242; 239, subscript; 239, 240; 239, 241; 240, identifier:i; 241, integer:0; 242, integer:1; 243, expression_statement; 243, 244; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:subTreeList; 247, identifier:append; 248, argument_list; 248, 249; 249, call; 249, 250; 249, 251; 250, identifier:fn3; 251, argument_list; 251, 252; 251, 253; 252, identifier:newickTree; 253, identifier:i; 254, expression_statement; 254, 255; 255, augmented_assignment:+=; 255, 256; 255, 259; 256, subscript; 256, 257; 256, 258; 257, identifier:i; 258, integer:0; 259, integer:1; 260, function_definition; 260, 261; 260, 262; 260, 265; 261, function_name:cmp; 262, parameters; 262, 263; 262, 264; 263, identifier:i; 264, identifier:j; 265, block; 265, 266; 265, 278; 265, 289; 266, if_statement; 266, 267; 266, 274; 267, comparison_operator:<; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:i; 270, identifier:distance; 271, attribute; 271, 272; 271, 273; 272, identifier:j; 273, identifier:distance; 274, block; 274, 275; 275, return_statement; 275, 276; 276, unary_operator:-; 276, 277; 277, integer:1; 278, if_statement; 278, 279; 278, 286; 279, comparison_operator:>; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:i; 282, identifier:distance; 283, attribute; 283, 284; 283, 285; 284, identifier:j; 285, identifier:distance; 286, block; 286, 287; 287, return_statement; 287, 288; 288, integer:1; 289, return_statement; 289, 290; 290, integer:0; 291, if_statement; 291, 292; 291, 293; 292, identifier:sortNonBinaryNodes; 293, block; 293, 294; 294, expression_statement; 294, 295; 295, call; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:subTreeList; 298, identifier:sort; 299, argument_list; 299, 300; 300, identifier:cmp; 301, expression_statement; 301, 302; 302, assignment; 302, 303; 302, 304; 303, identifier:subTree1; 304, subscript; 304, 305; 304, 306; 305, identifier:subTreeList; 306, integer:0; 307, if_statement; 307, 308; 307, 314; 307, 354; 307, 388; 308, comparison_operator:>; 308, 309; 308, 313; 309, call; 309, 310; 309, 311; 310, identifier:len; 311, argument_list; 311, 312; 312, identifier:subTreeList; 313, integer:1; 314, block; 314, 315; 314, 334; 314, 344; 315, for_statement; 315, 316; 315, 317; 315, 322; 316, identifier:subTree2; 317, subscript; 317, 318; 317, 319; 318, identifier:subTreeList; 319, slice; 319, 320; 319, 321; 320, integer:1; 321, colon; 322, block; 322, 323; 323, expression_statement; 323, 324; 324, assignment; 324, 325; 324, 326; 325, identifier:subTree1; 326, call; 326, 327; 326, 328; 327, identifier:BinaryTree; 328, argument_list; 328, 329; 328, 330; 328, 331; 328, 332; 328, 333; 329, float:0.0; 330, True; 331, identifier:subTree1; 332, identifier:subTree2; 333, None; 334, expression_statement; 334, 335; 335, assignment; 335, 336; 335, 339; 336, attribute; 336, 337; 336, 338; 337, identifier:subTree1; 338, identifier:iD; 339, call; 339, 340; 339, 341; 340, identifier:fn2; 341, argument_list; 341, 342; 341, 343; 342, identifier:newickTree; 343, identifier:i; 344, expression_statement; 344, 345; 345, augmented_assignment:+=; 345, 346; 345, 349; 346, attribute; 346, 347; 346, 348; 347, identifier:subTree1; 348, identifier:distance; 349, call; 349, 350; 349, 351; 350, identifier:fn; 351, argument_list; 351, 352; 351, 353; 352, identifier:newickTree; 353, identifier:i; 354, elif_clause; 354, 355; 354, 356; 355, identifier:reportUnaryNodes; 356, block; 356, 357; 356, 368; 356, 378; 357, expression_statement; 357, 358; 358, assignment; 358, 359; 358, 360; 359, identifier:subTree1; 360, call; 360, 361; 360, 362; 361, identifier:BinaryTree; 362, argument_list; 362, 363; 362, 364; 362, 365; 362, 366; 362, 367; 363, float:0.0; 364, True; 365, identifier:subTree1; 366, None; 367, None; 368, expression_statement; 368, 369; 369, assignment; 369, 370; 369, 373; 370, attribute; 370, 371; 370, 372; 371, identifier:subTree1; 372, identifier:iD; 373, call; 373, 374; 373, 375; 374, identifier:fn2; 375, argument_list; 375, 376; 375, 377; 376, identifier:newickTree; 377, identifier:i; 378, expression_statement; 378, 379; 379, augmented_assignment:+=; 379, 380; 379, 383; 380, attribute; 380, 381; 380, 382; 381, identifier:subTree1; 382, identifier:distance; 383, call; 383, 384; 383, 385; 384, identifier:fn; 385, argument_list; 385, 386; 385, 387; 386, identifier:newickTree; 387, identifier:i; 388, else_clause; 388, 389; 389, block; 389, 390; 389, 396; 390, expression_statement; 390, 391; 391, call; 391, 392; 391, 393; 392, identifier:fn2; 393, argument_list; 393, 394; 393, 395; 394, identifier:newickTree; 395, identifier:i; 396, expression_statement; 396, 397; 397, augmented_assignment:+=; 397, 398; 397, 401; 398, attribute; 398, 399; 398, 400; 399, identifier:subTree1; 400, identifier:distance; 401, call; 401, 402; 401, 403; 402, identifier:fn; 403, argument_list; 403, 404; 403, 405; 404, identifier:newickTree; 405, identifier:i; 406, return_statement; 406, 407; 407, identifier:subTree1; 408, expression_statement; 408, 409; 409, assignment; 409, 410; 409, 411; 410, identifier:leafID; 411, call; 411, 412; 411, 413; 412, identifier:fn2; 413, argument_list; 413, 414; 413, 415; 414, identifier:newickTree; 415, identifier:i; 416, return_statement; 416, 417; 417, call; 417, 418; 417, 419; 418, identifier:BinaryTree; 419, argument_list; 419, 420; 419, 425; 419, 426; 419, 427; 419, 428; 420, call; 420, 421; 420, 422; 421, identifier:fn; 422, argument_list; 422, 423; 422, 424; 423, identifier:newickTree; 424, identifier:i; 425, False; 426, None; 427, None; 428, identifier:leafID; 429, return_statement; 429, 430; 430, call; 430, 431; 430, 432; 431, identifier:fn3; 432, argument_list; 432, 433; 432, 434; 433, identifier:newickTree; 434, list:[0]; 434, 435; 435, integer:0
def newickTreeParser(newickTree, defaultDistance=DEFAULT_DISTANCE, \ sortNonBinaryNodes=False, reportUnaryNodes=False): """ lax newick tree parser """ newickTree = newickTree.replace("(", " ( ") newickTree = newickTree.replace(")", " ) ") newickTree = newickTree.replace(":", " : ") newickTree = newickTree.replace(";", "") newickTree = newickTree.replace(",", " , ") newickTree = re.compile("[\s]*").split(newickTree) while "" in newickTree: newickTree.remove("") def fn(newickTree, i): if i[0] < len(newickTree): if newickTree[i[0]] == ':': d = float(newickTree[i[0]+1]) i[0] += 2 return d return defaultDistance def fn2(newickTree, i): if i[0] < len(newickTree): j = newickTree[i[0]] if j != ':' and j != ')' and j != ',': i[0] += 1 return j return None def fn3(newickTree, i): if newickTree[i[0]] == '(': #subTree1 = None subTreeList = [] i[0] += 1 k = [] while newickTree[i[0]] != ')': if newickTree[i[0]] == ',': i[0] += 1 subTreeList.append(fn3(newickTree, i)) i[0] += 1 def cmp(i, j): if i.distance < j.distance: return -1 if i.distance > j.distance: return 1 return 0 if sortNonBinaryNodes: subTreeList.sort(cmp) subTree1 = subTreeList[0] if len(subTreeList) > 1: for subTree2 in subTreeList[1:]: subTree1 = BinaryTree(0.0, True, subTree1, subTree2, None) subTree1.iD = fn2(newickTree, i) subTree1.distance += fn(newickTree, i) elif reportUnaryNodes: subTree1 = BinaryTree(0.0, True, subTree1, None, None) subTree1.iD = fn2(newickTree, i) subTree1.distance += fn(newickTree, i) else: fn2(newickTree, i) subTree1.distance += fn(newickTree, i) return subTree1 leafID = fn2(newickTree, i) return BinaryTree(fn(newickTree, i), False, None, None, leafID) return fn3(newickTree, [0])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:parse_iptables_rule; 3, parameters; 3, 4; 4, identifier:line; 5, block; 5, 6; 5, 8; 5, 16; 5, 20; 5, 24; 5, 28; 5, 32; 5, 90; 5, 154; 5, 161; 5, 177; 6, expression_statement; 6, 7; 7, string:''' Parse one iptables rule. Returns a dict where each iptables code argument is mapped to a name using IPTABLES_ARGS. '''; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:bits; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:line; 14, identifier:split; 15, argument_list; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:definition; 19, dictionary; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:key; 23, None; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:args; 27, list:[]; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:not_arg; 31, False; 32, function_definition; 32, 33; 32, 34; 32, 35; 33, function_name:add_args; 34, parameters; 35, block; 35, 36; 35, 45; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:arg_string; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, string:' '; 42, identifier:join; 43, argument_list; 43, 44; 44, identifier:args; 45, if_statement; 45, 46; 45, 49; 45, 73; 46, comparison_operator:in; 46, 47; 46, 48; 47, identifier:key; 48, identifier:IPTABLES_ARGS; 49, block; 49, 50; 49, 67; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:definition_key; 53, parenthesized_expression; 53, 54; 54, conditional_expression:if; 54, 55; 54, 63; 54, 64; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, string:'not_{0}'; 58, identifier:format; 59, argument_list; 59, 60; 60, subscript; 60, 61; 60, 62; 61, identifier:IPTABLES_ARGS; 62, identifier:key; 63, identifier:not_arg; 64, subscript; 64, 65; 64, 66; 65, identifier:IPTABLES_ARGS; 66, identifier:key; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 72; 69, subscript; 69, 70; 69, 71; 70, identifier:definition; 71, identifier:definition_key; 72, identifier:arg_string; 73, else_clause; 73, 74; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, call; 76, 77; 76, 86; 77, attribute; 77, 78; 77, 85; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:definition; 81, identifier:setdefault; 82, argument_list; 82, 83; 82, 84; 83, string:'extras'; 84, list:[]; 85, identifier:extend; 86, argument_list; 86, 87; 87, tuple; 87, 88; 87, 89; 88, identifier:key; 89, identifier:arg_string; 90, for_statement; 90, 91; 90, 92; 90, 93; 91, identifier:bit; 92, identifier:bits; 93, block; 93, 94; 94, if_statement; 94, 95; 94, 98; 94, 118; 94, 145; 95, comparison_operator:==; 95, 96; 95, 97; 96, identifier:bit; 97, string:'!'; 98, block; 98, 99; 98, 114; 99, if_statement; 99, 100; 99, 101; 100, identifier:key; 101, block; 101, 102; 101, 106; 101, 110; 102, expression_statement; 102, 103; 103, call; 103, 104; 103, 105; 104, identifier:add_args; 105, argument_list; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:args; 109, list:[]; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:key; 113, None; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:not_arg; 117, True; 118, elif_clause; 118, 119; 118, 125; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:bit; 122, identifier:startswith; 123, argument_list; 123, 124; 124, string:'-'; 125, block; 125, 126; 125, 141; 126, if_statement; 126, 127; 126, 128; 127, identifier:key; 128, block; 128, 129; 128, 133; 128, 137; 129, expression_statement; 129, 130; 130, call; 130, 131; 130, 132; 131, identifier:add_args; 132, argument_list; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:args; 136, list:[]; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:not_arg; 140, False; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:key; 144, identifier:bit; 145, else_clause; 145, 146; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:args; 151, identifier:append; 152, argument_list; 152, 153; 153, identifier:bit; 154, if_statement; 154, 155; 154, 156; 155, identifier:key; 156, block; 156, 157; 157, expression_statement; 157, 158; 158, call; 158, 159; 158, 160; 159, identifier:add_args; 160, argument_list; 161, if_statement; 161, 162; 161, 165; 162, comparison_operator:in; 162, 163; 162, 164; 163, string:'extras'; 164, identifier:definition; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 171; 168, subscript; 168, 169; 168, 170; 169, identifier:definition; 170, string:'extras'; 171, call; 171, 172; 171, 173; 172, identifier:set; 173, argument_list; 173, 174; 174, subscript; 174, 175; 174, 176; 175, identifier:definition; 176, string:'extras'; 177, return_statement; 177, 178; 178, identifier:definition
def parse_iptables_rule(line): ''' Parse one iptables rule. Returns a dict where each iptables code argument is mapped to a name using IPTABLES_ARGS. ''' bits = line.split() definition = {} key = None args = [] not_arg = False def add_args(): arg_string = ' '.join(args) if key in IPTABLES_ARGS: definition_key = ( 'not_{0}'.format(IPTABLES_ARGS[key]) if not_arg else IPTABLES_ARGS[key] ) definition[definition_key] = arg_string else: definition.setdefault('extras', []).extend((key, arg_string)) for bit in bits: if bit == '!': if key: add_args() args = [] key = None not_arg = True elif bit.startswith('-'): if key: add_args() args = [] not_arg = False key = bit else: args.append(bit) if key: add_args() if 'extras' in definition: definition['extras'] = set(definition['extras']) return definition
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:make_command; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:command; 5, default_parameter; 5, 6; 5, 7; 6, identifier:env; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:su_user; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:sudo; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:sudo_user; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:preserve_sudo_env; 19, False; 20, block; 20, 21; 20, 23; 20, 27; 20, 54; 20, 88; 20, 89; 20, 128; 20, 129; 20, 136; 20, 137; 20, 162; 20, 163; 20, 209; 21, expression_statement; 21, 22; 22, string:''' Builds a shell command with various kwargs. '''; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:debug_meta; 26, dictionary; 27, for_statement; 27, 28; 27, 31; 27, 44; 28, pattern_list; 28, 29; 28, 30; 29, identifier:key; 30, identifier:value; 31, tuple; 31, 32; 31, 35; 31, 38; 31, 41; 32, tuple; 32, 33; 32, 34; 33, string:'sudo'; 34, identifier:sudo; 35, tuple; 35, 36; 35, 37; 36, string:'sudo_user'; 37, identifier:sudo_user; 38, tuple; 38, 39; 38, 40; 39, string:'su_user'; 40, identifier:su_user; 41, tuple; 41, 42; 41, 43; 42, string:'env'; 43, identifier:env; 44, block; 44, 45; 45, if_statement; 45, 46; 45, 47; 46, identifier:value; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 53; 50, subscript; 50, 51; 50, 52; 51, identifier:debug_meta; 52, identifier:key; 53, identifier:value; 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; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, string:'Building command ({0}): {1}'; 63, identifier:format; 64, argument_list; 64, 65; 64, 87; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, string:' '; 68, identifier:join; 69, generator_expression; 69, 70; 69, 77; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, string:'{0}: {1}'; 73, identifier:format; 74, argument_list; 74, 75; 74, 76; 75, identifier:key; 76, identifier:value; 77, for_in_clause; 77, 78; 77, 81; 78, pattern_list; 78, 79; 78, 80; 79, identifier:key; 80, identifier:value; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:six; 84, identifier:iteritems; 85, argument_list; 85, 86; 86, identifier:debug_meta; 87, identifier:command; 88, comment; 89, if_statement; 89, 90; 89, 91; 90, identifier:env; 91, block; 91, 92; 91, 118; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:env_string; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, string:' '; 98, identifier:join; 99, argument_list; 99, 100; 100, list_comprehension; 100, 101; 100, 108; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, string:'{0}={1}'; 104, identifier:format; 105, argument_list; 105, 106; 105, 107; 106, identifier:key; 107, identifier:value; 108, for_in_clause; 108, 109; 108, 112; 109, pattern_list; 109, 110; 109, 111; 110, identifier:key; 111, identifier:value; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:six; 115, identifier:iteritems; 116, argument_list; 116, 117; 117, identifier:env; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:command; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, string:'export {0}; {1}'; 124, identifier:format; 125, argument_list; 125, 126; 125, 127; 126, identifier:env_string; 127, identifier:command; 128, comment; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:command; 132, call; 132, 133; 132, 134; 133, identifier:shlex_quote; 134, argument_list; 134, 135; 135, identifier:command; 136, comment; 137, if_statement; 137, 138; 137, 139; 137, 150; 137, 151; 138, identifier:su_user; 139, block; 139, 140; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:command; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, string:'su {0} -c {1}'; 146, identifier:format; 147, argument_list; 147, 148; 147, 149; 148, identifier:su_user; 149, identifier:command; 150, comment; 151, else_clause; 151, 152; 152, block; 152, 153; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:command; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, string:'sh -c {0}'; 159, identifier:format; 160, argument_list; 160, 161; 161, identifier:command; 162, comment; 163, if_statement; 163, 164; 163, 165; 164, identifier:sudo; 165, block; 165, 166; 165, 172; 165, 182; 165, 194; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:sudo_bits; 169, list:['sudo', '-H']; 169, 170; 169, 171; 170, string:'sudo'; 171, string:'-H'; 172, if_statement; 172, 173; 172, 174; 173, identifier:preserve_sudo_env; 174, block; 174, 175; 175, expression_statement; 175, 176; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:sudo_bits; 179, identifier:append; 180, argument_list; 180, 181; 181, string:'-E'; 182, if_statement; 182, 183; 182, 184; 183, identifier:sudo_user; 184, block; 184, 185; 185, expression_statement; 185, 186; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:sudo_bits; 189, identifier:extend; 190, argument_list; 190, 191; 191, tuple; 191, 192; 191, 193; 192, string:'-u'; 193, identifier:sudo_user; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:command; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, string:'{0} {1}'; 200, identifier:format; 201, argument_list; 201, 202; 201, 208; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, string:' '; 205, identifier:join; 206, argument_list; 206, 207; 207, identifier:sudo_bits; 208, identifier:command; 209, return_statement; 209, 210; 210, identifier:command
def make_command( command, env=None, su_user=None, sudo=False, sudo_user=None, preserve_sudo_env=False, ): ''' Builds a shell command with various kwargs. ''' debug_meta = {} for key, value in ( ('sudo', sudo), ('sudo_user', sudo_user), ('su_user', su_user), ('env', env), ): if value: debug_meta[key] = value logger.debug('Building command ({0}): {1}'.format(' '.join( '{0}: {1}'.format(key, value) for key, value in six.iteritems(debug_meta) ), command)) # Use env & build our actual command if env: env_string = ' '.join([ '{0}={1}'.format(key, value) for key, value in six.iteritems(env) ]) command = 'export {0}; {1}'.format(env_string, command) # Quote the command as a string command = shlex_quote(command) # Switch user with su if su_user: command = 'su {0} -c {1}'.format(su_user, command) # Otherwise just sh wrap the command else: command = 'sh -c {0}'.format(command) # Use sudo (w/user?) if sudo: sudo_bits = ['sudo', '-H'] if preserve_sudo_env: sudo_bits.append('-E') if sudo_user: sudo_bits.extend(('-u', sudo_user)) command = '{0} {1}'.format(' '.join(sudo_bits), command) return command
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:read_buffer; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:io; 5, default_parameter; 5, 6; 5, 7; 6, identifier:print_output; 7, False; 8, default_parameter; 8, 9; 8, 10; 9, identifier:print_func; 10, None; 11, block; 11, 12; 11, 14; 11, 15; 11, 16; 11, 17; 11, 18; 11, 19; 11, 20; 11, 21; 11, 22; 11, 23; 11, 24; 11, 25; 11, 26; 11, 27; 11, 28; 11, 29; 11, 30; 11, 31; 11, 32; 11, 33; 11, 34; 11, 35; 11, 36; 11, 37; 11, 38; 11, 39; 11, 40; 11, 41; 11, 42; 11, 43; 11, 84; 11, 88; 11, 132; 12, expression_statement; 12, 13; 13, string:''' Reads a file-like buffer object into lines and optionally prints the output. '''; 14, comment; 15, comment; 16, comment; 17, comment; 18, comment; 19, comment; 20, comment; 21, comment; 22, comment; 23, comment; 24, comment; 25, comment; 26, comment; 27, comment; 28, comment; 29, comment; 30, comment; 31, comment; 32, comment; 33, comment; 34, comment; 35, comment; 36, comment; 37, comment; 38, comment; 39, comment; 40, comment; 41, comment; 42, comment; 43, function_definition; 43, 44; 43, 45; 43, 47; 44, function_name:_print; 45, parameters; 45, 46; 46, identifier:line; 47, block; 47, 48; 48, if_statement; 48, 49; 48, 50; 49, identifier:print_output; 50, block; 50, 51; 50, 67; 50, 79; 51, if_statement; 51, 52; 51, 53; 51, 61; 52, identifier:print_func; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:formatted_line; 57, call; 57, 58; 57, 59; 58, identifier:print_func; 59, argument_list; 59, 60; 60, identifier:line; 61, else_clause; 61, 62; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:formatted_line; 66, identifier:line; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:encoded_line; 70, call; 70, 71; 70, 77; 71, attribute; 71, 72; 71, 76; 72, call; 72, 73; 72, 74; 73, identifier:unicode; 74, argument_list; 74, 75; 75, identifier:formatted_line; 76, identifier:encode; 77, argument_list; 77, 78; 78, string:'utf-8'; 79, expression_statement; 79, 80; 80, call; 80, 81; 80, 82; 81, identifier:print; 82, argument_list; 82, 83; 83, identifier:encoded_line; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:out; 87, list:[]; 88, for_statement; 88, 89; 88, 90; 88, 91; 88, 92; 89, identifier:line; 90, identifier:io; 91, comment; 92, block; 92, 93; 92, 112; 92, 120; 92, 127; 93, if_statement; 93, 94; 93, 102; 94, not_operator; 94, 95; 95, call; 95, 96; 95, 97; 96, identifier:isinstance; 97, argument_list; 97, 98; 97, 99; 98, identifier:line; 99, attribute; 99, 100; 99, 101; 100, identifier:six; 101, identifier:text_type; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:line; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:line; 109, identifier:decode; 110, argument_list; 110, 111; 111, string:'utf-8'; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:line; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:line; 118, identifier:strip; 119, argument_list; 120, expression_statement; 120, 121; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:out; 124, identifier:append; 125, argument_list; 125, 126; 126, identifier:line; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 130; 129, identifier:_print; 130, argument_list; 130, 131; 131, identifier:line; 132, return_statement; 132, 133; 133, identifier:out
def read_buffer(io, print_output=False, print_func=None): ''' Reads a file-like buffer object into lines and optionally prints the output. ''' # TODO: research this further - some steps towards handling stdin (ie password requests # from programs that don't notice there's no TTY to accept passwords from!). This just # prints output as below, but stores partial lines in a buffer, which could be printed # when ready to accept input. Or detected and raise an error. # GitHub issue: https://github.com/Fizzadar/pyinfra/issues/40 # buff = '' # data = io.read(1) # while data: # # Append to the buffer # buff += data # # Newlines in the buffer? Break them out # if '\n' in buff: # lines = buff.split('\n') # # Set the buffer back to just the last line # buff = lines[-1] # # Get the other lines, strip them # lines = [ # line.strip() # for line in lines[:-1] # ] # out.extend(lines) # for line in lines: # _print(line) # # Get next data # data = io.read(1) # if buff: # line = buff.strip() # out.append(line) # _print(line) def _print(line): if print_output: if print_func: formatted_line = print_func(line) else: formatted_line = line encoded_line = unicode(formatted_line).encode('utf-8') print(encoded_line) out = [] for line in io: # Handle local Popen shells returning list of bytes, not strings if not isinstance(line, six.text_type): line = line.decode('utf-8') line = line.strip() out.append(line) _print(line) return out
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 23; 2, function_name:download; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 4, identifier:state; 5, identifier:host; 6, identifier:source_url; 7, identifier:destination; 8, default_parameter; 8, 9; 8, 10; 9, identifier:user; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:group; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:mode; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:cache_time; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:force; 22, False; 23, block; 23, 24; 23, 26; 23, 27; 23, 38; 23, 39; 23, 54; 23, 55; 23, 59; 23, 60; 23, 113; 23, 114; 24, expression_statement; 24, 25; 25, string:''' Download files from remote locations. + source_url: source URl of the file + destination: where to save the file + user: user to own the files + group: group to own the files + mode: permissions of the files + cache_time: if the file exists already, re-download after this time (in s) + force: always download the file, even if it already exists '''; 26, comment; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:info; 30, call; 30, 31; 30, 36; 31, attribute; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:host; 34, identifier:fact; 35, identifier:file; 36, argument_list; 36, 37; 37, identifier:destination; 38, comment; 39, if_statement; 39, 40; 39, 43; 40, comparison_operator:is; 40, 41; 40, 42; 41, identifier:info; 42, False; 43, block; 43, 44; 44, raise_statement; 44, 45; 45, call; 45, 46; 45, 47; 46, identifier:OperationError; 47, argument_list; 47, 48; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, string:'Destination {0} already exists and is not a file'; 51, identifier:format; 52, argument_list; 52, 53; 53, identifier:destination; 54, comment; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:download; 58, identifier:force; 59, comment; 60, if_statement; 60, 61; 60, 64; 60, 69; 60, 70; 60, 71; 61, comparison_operator:is; 61, 62; 61, 63; 62, identifier:info; 63, None; 64, block; 64, 65; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:download; 68, True; 69, comment; 70, comment; 71, elif_clause; 71, 72; 71, 73; 71, 74; 71, 75; 72, identifier:cache_time; 73, comment; 74, comment; 75, block; 75, 76; 75, 98; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:cache_time; 79, binary_operator:-; 79, 80; 79, 92; 80, call; 80, 81; 80, 88; 81, attribute; 81, 82; 81, 87; 82, attribute; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:host; 85, identifier:fact; 86, identifier:date; 87, identifier:replace; 88, argument_list; 88, 89; 89, keyword_argument; 89, 90; 89, 91; 90, identifier:tzinfo; 91, None; 92, call; 92, 93; 92, 94; 93, identifier:timedelta; 94, argument_list; 94, 95; 95, keyword_argument; 95, 96; 95, 97; 96, identifier:seconds; 97, identifier:cache_time; 98, if_statement; 98, 99; 98, 108; 99, boolean_operator:and; 99, 100; 99, 103; 100, subscript; 100, 101; 100, 102; 101, identifier:info; 102, string:'mtime'; 103, comparison_operator:>; 103, 104; 103, 107; 104, subscript; 104, 105; 104, 106; 105, identifier:info; 106, string:'mtime'; 107, identifier:cache_time; 108, block; 108, 109; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:download; 112, True; 113, comment; 114, if_statement; 114, 115; 114, 116; 115, identifier:download; 116, block; 116, 117; 116, 126; 116, 139; 117, expression_statement; 117, 118; 118, yield; 118, 119; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, string:'wget -q {0} -O {1}'; 122, identifier:format; 123, argument_list; 123, 124; 123, 125; 124, identifier:source_url; 125, identifier:destination; 126, if_statement; 126, 127; 126, 130; 127, boolean_operator:or; 127, 128; 127, 129; 128, identifier:user; 129, identifier:group; 130, block; 130, 131; 131, expression_statement; 131, 132; 132, yield; 132, 133; 133, call; 133, 134; 133, 135; 134, identifier:chown; 135, argument_list; 135, 136; 135, 137; 135, 138; 136, identifier:destination; 137, identifier:user; 138, identifier:group; 139, if_statement; 139, 140; 139, 141; 140, identifier:mode; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, yield; 143, 144; 144, call; 144, 145; 144, 146; 145, identifier:chmod; 146, argument_list; 146, 147; 146, 148; 147, identifier:destination; 148, identifier:mode
def download( state, host, source_url, destination, user=None, group=None, mode=None, cache_time=None, force=False, ): ''' Download files from remote locations. + source_url: source URl of the file + destination: where to save the file + user: user to own the files + group: group to own the files + mode: permissions of the files + cache_time: if the file exists already, re-download after this time (in s) + force: always download the file, even if it already exists ''' # Get destination info info = host.fact.file(destination) # Destination is a directory? if info is False: raise OperationError( 'Destination {0} already exists and is not a file'.format(destination), ) # Do we download the file? Force by default download = force # Doesn't exist, lets download it if info is None: download = True # Destination file exists & cache_time: check when the file was last modified, # download if old elif cache_time: # Time on files is not tz-aware, and will be the same tz as the server's time, # so we can safely remove the tzinfo from host.fact.date before comparison. cache_time = host.fact.date.replace(tzinfo=None) - timedelta(seconds=cache_time) if info['mtime'] and info['mtime'] > cache_time: download = True # If we download, always do user/group/mode as SSH user may be different if download: yield 'wget -q {0} -O {1}'.format(source_url, destination) if user or group: yield chown(destination, user, group) if mode: yield chmod(destination, mode)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 29; 2, function_name:sync; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 4, identifier:state; 5, identifier:host; 6, identifier:source; 7, identifier:destination; 8, default_parameter; 8, 9; 8, 10; 9, identifier:user; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:group; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:mode; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:delete; 19, False; 20, default_parameter; 20, 21; 20, 22; 21, identifier:exclude; 22, None; 23, default_parameter; 23, 24; 23, 25; 24, identifier:exclude_dir; 25, None; 26, default_parameter; 26, 27; 26, 28; 27, identifier:add_deploy_dir; 28, True; 29, block; 29, 30; 29, 32; 29, 33; 29, 34; 29, 57; 29, 58; 29, 77; 29, 78; 29, 97; 29, 98; 29, 118; 29, 119; 29, 139; 29, 143; 29, 147; 29, 249; 29, 250; 29, 264; 29, 265; 29, 290; 29, 291; 29, 318; 29, 319; 30, expression_statement; 30, 31; 31, string:''' Syncs a local directory with a remote one, with delete support. Note that delete will remove extra files on the remote side, but not extra directories. + source: local directory to sync + destination: remote directory to sync to + user: user to own the files and directories + group: group to own the files and directories + mode: permissions of the files + delete: delete remote files not present locally + exclude: string or list/tuple of strings to match & exclude files (eg *.pyc) + exclude_dir: string or list/tuple of strings to match & exclude directories (eg node_modules) '''; 32, comment; 33, comment; 34, if_statement; 34, 35; 34, 44; 35, not_operator; 35, 36; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:source; 39, identifier:endswith; 40, argument_list; 40, 41; 41, attribute; 41, 42; 41, 43; 42, identifier:path; 43, identifier:sep; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:source; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, string:'{0}{1}'; 51, identifier:format; 52, argument_list; 52, 53; 52, 54; 53, identifier:source; 54, attribute; 54, 55; 54, 56; 55, identifier:path; 56, identifier:sep; 57, comment; 58, if_statement; 58, 59; 58, 64; 59, boolean_operator:and; 59, 60; 59, 61; 60, identifier:add_deploy_dir; 61, attribute; 61, 62; 61, 63; 62, identifier:state; 63, identifier:deploy_dir; 64, block; 64, 65; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:source; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:path; 71, identifier:join; 72, argument_list; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:state; 75, identifier:deploy_dir; 76, identifier:source; 77, comment; 78, if_statement; 78, 79; 78, 86; 79, not_operator; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:path; 83, identifier:isdir; 84, argument_list; 84, 85; 85, identifier:source; 86, block; 86, 87; 87, raise_statement; 87, 88; 88, call; 88, 89; 88, 90; 89, identifier:IOError; 90, argument_list; 90, 91; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, string:'No such directory: {0}'; 94, identifier:format; 95, argument_list; 95, 96; 96, identifier:source; 97, comment; 98, if_statement; 98, 99; 98, 102; 99, comparison_operator:is; 99, 100; 99, 101; 100, identifier:exclude; 101, None; 102, block; 102, 103; 103, if_statement; 103, 104; 103, 112; 104, not_operator; 104, 105; 105, call; 105, 106; 105, 107; 106, identifier:isinstance; 107, argument_list; 107, 108; 107, 109; 108, identifier:exclude; 109, tuple; 109, 110; 109, 111; 110, identifier:list; 111, identifier:tuple; 112, block; 112, 113; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:exclude; 116, list:[exclude]; 116, 117; 117, identifier:exclude; 118, comment; 119, if_statement; 119, 120; 119, 123; 120, comparison_operator:is; 120, 121; 120, 122; 121, identifier:exclude_dir; 122, None; 123, block; 123, 124; 124, if_statement; 124, 125; 124, 133; 125, not_operator; 125, 126; 126, call; 126, 127; 126, 128; 127, identifier:isinstance; 128, argument_list; 128, 129; 128, 130; 129, identifier:exclude_dir; 130, tuple; 130, 131; 130, 132; 131, identifier:list; 132, identifier:tuple; 133, block; 133, 134; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:exclude_dir; 137, list:[exclude_dir]; 137, 138; 138, identifier:exclude_dir; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:put_files; 142, list:[]; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:ensure_dirnames; 146, list:[]; 147, for_statement; 147, 148; 147, 152; 147, 156; 148, pattern_list; 148, 149; 148, 150; 148, 151; 149, identifier:dirname; 150, identifier:_; 151, identifier:filenames; 152, call; 152, 153; 152, 154; 153, identifier:walk; 154, argument_list; 154, 155; 155, identifier:source; 156, block; 156, 157; 156, 167; 156, 168; 156, 184; 156, 194; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:remote_dirname; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:dirname; 163, identifier:replace; 164, argument_list; 164, 165; 164, 166; 165, identifier:source; 166, string:''; 167, comment; 168, if_statement; 168, 169; 168, 182; 169, boolean_operator:and; 169, 170; 169, 171; 170, identifier:exclude_dir; 171, call; 171, 172; 171, 173; 172, identifier:any; 173, generator_expression; 173, 174; 173, 179; 174, call; 174, 175; 174, 176; 175, identifier:fnmatch; 176, argument_list; 176, 177; 176, 178; 177, identifier:remote_dirname; 178, identifier:match; 179, for_in_clause; 179, 180; 179, 181; 180, identifier:match; 181, identifier:exclude_dir; 182, block; 182, 183; 183, continue_statement; 184, if_statement; 184, 185; 184, 186; 185, identifier:remote_dirname; 186, block; 186, 187; 187, expression_statement; 187, 188; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:ensure_dirnames; 191, identifier:append; 192, argument_list; 192, 193; 193, identifier:remote_dirname; 194, for_statement; 194, 195; 194, 196; 194, 197; 195, identifier:filename; 196, identifier:filenames; 197, block; 197, 198; 197, 208; 197, 209; 197, 225; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:full_filename; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:path; 204, identifier:join; 205, argument_list; 205, 206; 205, 207; 206, identifier:dirname; 207, identifier:filename; 208, comment; 209, if_statement; 209, 210; 209, 223; 210, boolean_operator:and; 210, 211; 210, 212; 211, identifier:exclude; 212, call; 212, 213; 212, 214; 213, identifier:any; 214, generator_expression; 214, 215; 214, 220; 215, call; 215, 216; 215, 217; 216, identifier:fnmatch; 217, argument_list; 217, 218; 217, 219; 218, identifier:full_filename; 219, identifier:match; 220, for_in_clause; 220, 221; 220, 222; 221, identifier:match; 222, identifier:exclude; 223, block; 223, 224; 224, continue_statement; 225, expression_statement; 225, 226; 226, call; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:put_files; 229, identifier:append; 230, argument_list; 230, 231; 231, tuple; 231, 232; 231, 233; 231, 234; 231, 235; 232, comment; 233, identifier:full_filename; 234, comment; 235, call; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, string:'/'; 238, identifier:join; 239, generator_expression; 239, 240; 239, 241; 239, 247; 240, identifier:item; 241, for_in_clause; 241, 242; 241, 243; 242, identifier:item; 243, tuple; 243, 244; 243, 245; 243, 246; 244, identifier:destination; 245, identifier:remote_dirname; 246, identifier:filename; 247, if_clause; 247, 248; 248, identifier:item; 249, comment; 250, expression_statement; 250, 251; 251, yield; 251, 252; 252, call; 252, 253; 252, 254; 253, identifier:directory; 254, argument_list; 254, 255; 254, 256; 254, 257; 254, 258; 254, 261; 255, identifier:state; 256, identifier:host; 257, identifier:destination; 258, keyword_argument; 258, 259; 258, 260; 259, identifier:user; 260, identifier:user; 261, keyword_argument; 261, 262; 261, 263; 262, identifier:group; 263, identifier:group; 264, comment; 265, for_statement; 265, 266; 265, 267; 265, 268; 266, identifier:dirname; 267, identifier:ensure_dirnames; 268, block; 268, 269; 269, expression_statement; 269, 270; 270, yield; 270, 271; 271, call; 271, 272; 271, 273; 272, identifier:directory; 273, argument_list; 273, 274; 273, 275; 273, 276; 273, 284; 273, 287; 274, identifier:state; 275, identifier:host; 276, call; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, string:'/'; 279, identifier:join; 280, argument_list; 280, 281; 281, tuple; 281, 282; 281, 283; 282, identifier:destination; 283, identifier:dirname; 284, keyword_argument; 284, 285; 284, 286; 285, identifier:user; 286, identifier:user; 287, keyword_argument; 287, 288; 287, 289; 288, identifier:group; 289, identifier:group; 290, comment; 291, for_statement; 291, 292; 291, 295; 291, 296; 292, pattern_list; 292, 293; 292, 294; 293, identifier:local_filename; 294, identifier:remote_filename; 295, identifier:put_files; 296, block; 296, 297; 297, expression_statement; 297, 298; 298, yield; 298, 299; 299, call; 299, 300; 299, 301; 300, identifier:put; 301, argument_list; 301, 302; 301, 303; 301, 304; 301, 305; 301, 306; 301, 309; 301, 312; 301, 315; 302, identifier:state; 303, identifier:host; 304, identifier:local_filename; 305, identifier:remote_filename; 306, keyword_argument; 306, 307; 306, 308; 307, identifier:user; 308, identifier:user; 309, keyword_argument; 309, 310; 309, 311; 310, identifier:group; 311, identifier:group; 312, keyword_argument; 312, 313; 312, 314; 313, identifier:mode; 314, identifier:mode; 315, keyword_argument; 315, 316; 315, 317; 316, identifier:add_deploy_dir; 317, False; 318, comment; 319, if_statement; 319, 320; 319, 321; 320, identifier:delete; 321, block; 321, 322; 321, 338; 321, 351; 321, 357; 322, expression_statement; 322, 323; 323, assignment; 323, 324; 323, 325; 324, identifier:remote_filenames; 325, call; 325, 326; 325, 327; 326, identifier:set; 327, argument_list; 327, 328; 328, boolean_operator:or; 328, 329; 328, 337; 329, call; 329, 330; 329, 335; 330, attribute; 330, 331; 330, 334; 331, attribute; 331, 332; 331, 333; 332, identifier:host; 333, identifier:fact; 334, identifier:find_files; 335, argument_list; 335, 336; 336, identifier:destination; 337, list:[]; 338, expression_statement; 338, 339; 339, assignment; 339, 340; 339, 341; 340, identifier:wanted_filenames; 341, call; 341, 342; 341, 343; 342, identifier:set; 343, argument_list; 343, 344; 344, list_comprehension; 344, 345; 344, 346; 345, identifier:remote_filename; 346, for_in_clause; 346, 347; 346, 350; 347, pattern_list; 347, 348; 347, 349; 348, identifier:_; 349, identifier:remote_filename; 350, identifier:put_files; 351, expression_statement; 351, 352; 352, assignment; 352, 353; 352, 354; 353, identifier:files_to_delete; 354, binary_operator:-; 354, 355; 354, 356; 355, identifier:remote_filenames; 356, identifier:wanted_filenames; 357, for_statement; 357, 358; 357, 359; 357, 360; 357, 361; 358, identifier:filename; 359, identifier:files_to_delete; 360, comment; 361, block; 361, 362; 361, 378; 362, if_statement; 362, 363; 362, 376; 363, boolean_operator:and; 363, 364; 363, 365; 364, identifier:exclude; 365, call; 365, 366; 365, 367; 366, identifier:any; 367, generator_expression; 367, 368; 367, 373; 368, call; 368, 369; 368, 370; 369, identifier:fnmatch; 370, argument_list; 370, 371; 370, 372; 371, identifier:filename; 372, identifier:match; 373, for_in_clause; 373, 374; 373, 375; 374, identifier:match; 375, identifier:exclude; 376, block; 376, 377; 377, continue_statement; 378, expression_statement; 378, 379; 379, yield; 379, 380; 380, call; 380, 381; 380, 382; 381, identifier:file; 382, argument_list; 382, 383; 382, 384; 382, 385; 382, 386; 383, identifier:state; 384, identifier:host; 385, identifier:filename; 386, keyword_argument; 386, 387; 386, 388; 387, identifier:present; 388, False
def sync( state, host, source, destination, user=None, group=None, mode=None, delete=False, exclude=None, exclude_dir=None, add_deploy_dir=True, ): ''' Syncs a local directory with a remote one, with delete support. Note that delete will remove extra files on the remote side, but not extra directories. + source: local directory to sync + destination: remote directory to sync to + user: user to own the files and directories + group: group to own the files and directories + mode: permissions of the files + delete: delete remote files not present locally + exclude: string or list/tuple of strings to match & exclude files (eg *.pyc) + exclude_dir: string or list/tuple of strings to match & exclude directories (eg node_modules) ''' # If we don't enforce the source ending with /, remote_dirname below might start with # a /, which makes the path.join cut off the destination bit. if not source.endswith(path.sep): source = '{0}{1}'.format(source, path.sep) # Add deploy directory? if add_deploy_dir and state.deploy_dir: source = path.join(state.deploy_dir, source) # Ensure the source directory exists if not path.isdir(source): raise IOError('No such directory: {0}'.format(source)) # Ensure exclude is a list/tuple if exclude is not None: if not isinstance(exclude, (list, tuple)): exclude = [exclude] # Ensure exclude_dir is a list/tuple if exclude_dir is not None: if not isinstance(exclude_dir, (list, tuple)): exclude_dir = [exclude_dir] put_files = [] ensure_dirnames = [] for dirname, _, filenames in walk(source): remote_dirname = dirname.replace(source, '') # Should we exclude this dir? if exclude_dir and any(fnmatch(remote_dirname, match) for match in exclude_dir): continue if remote_dirname: ensure_dirnames.append(remote_dirname) for filename in filenames: full_filename = path.join(dirname, filename) # Should we exclude this file? if exclude and any(fnmatch(full_filename, match) for match in exclude): continue put_files.append(( # Join local as normal (unix, win) full_filename, # Join remote as unix like '/'.join( item for item in (destination, remote_dirname, filename) if item ), )) # Ensure the destination directory yield directory( state, host, destination, user=user, group=group, ) # Ensure any remote dirnames for dirname in ensure_dirnames: yield directory( state, host, '/'.join((destination, dirname)), user=user, group=group, ) # Put each file combination for local_filename, remote_filename in put_files: yield put( state, host, local_filename, remote_filename, user=user, group=group, mode=mode, add_deploy_dir=False, ) # Delete any extra files if delete: remote_filenames = set(host.fact.find_files(destination) or []) wanted_filenames = set([remote_filename for _, remote_filename in put_files]) files_to_delete = remote_filenames - wanted_filenames for filename in files_to_delete: # Should we exclude this file? if exclude and any(fnmatch(filename, match) for match in exclude): continue yield file(state, host, filename, present=False)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:put; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:state; 5, identifier:host; 6, identifier:local_filename; 7, identifier:remote_filename; 8, default_parameter; 8, 9; 8, 10; 9, identifier:user; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:group; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:mode; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:add_deploy_dir; 19, True; 20, block; 20, 21; 20, 23; 20, 24; 20, 81; 20, 88; 20, 99; 20, 100; 21, expression_statement; 21, 22; 22, string:''' Copy a local file to the remote system. + local_filename: local filename + remote_filename: remote filename + user: user to own the files + group: group to own the files + mode: permissions of the files '''; 23, comment; 24, if_statement; 24, 25; 24, 30; 24, 35; 24, 36; 25, call; 25, 26; 25, 27; 26, identifier:hasattr; 27, argument_list; 27, 28; 27, 29; 28, identifier:local_filename; 29, string:'read'; 30, block; 30, 31; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:local_file; 34, identifier:local_filename; 35, comment; 36, else_clause; 36, 37; 36, 38; 37, comment; 38, block; 38, 39; 38, 58; 38, 62; 39, if_statement; 39, 40; 39, 45; 40, boolean_operator:and; 40, 41; 40, 42; 41, identifier:add_deploy_dir; 42, attribute; 42, 43; 42, 44; 43, identifier:state; 44, identifier:deploy_dir; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:local_filename; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:path; 52, identifier:join; 53, argument_list; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:state; 56, identifier:deploy_dir; 57, identifier:local_filename; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:local_file; 61, identifier:local_filename; 62, if_statement; 62, 63; 62, 70; 63, not_operator; 63, 64; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:path; 67, identifier:isfile; 68, argument_list; 68, 69; 69, identifier:local_file; 70, block; 70, 71; 71, raise_statement; 71, 72; 72, call; 72, 73; 72, 74; 73, identifier:IOError; 74, argument_list; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, string:'No such file: {0}'; 78, identifier:format; 79, argument_list; 79, 80; 80, identifier:local_file; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:mode; 84, call; 84, 85; 84, 86; 85, identifier:ensure_mode_int; 86, argument_list; 86, 87; 87, identifier:mode; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:remote_file; 91, call; 91, 92; 91, 97; 92, attribute; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:host; 95, identifier:fact; 96, identifier:file; 97, argument_list; 97, 98; 98, identifier:remote_filename; 99, comment; 100, if_statement; 100, 101; 100, 103; 100, 132; 100, 133; 101, not_operator; 101, 102; 102, identifier:remote_file; 103, block; 103, 104; 103, 109; 103, 122; 104, expression_statement; 104, 105; 105, yield; 105, 106; 106, tuple; 106, 107; 106, 108; 107, identifier:local_file; 108, identifier:remote_filename; 109, if_statement; 109, 110; 109, 113; 110, boolean_operator:or; 110, 111; 110, 112; 111, identifier:user; 112, identifier:group; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, yield; 115, 116; 116, call; 116, 117; 116, 118; 117, identifier:chown; 118, argument_list; 118, 119; 118, 120; 118, 121; 119, identifier:remote_filename; 120, identifier:user; 121, identifier:group; 122, if_statement; 122, 123; 122, 124; 123, identifier:mode; 124, block; 124, 125; 125, expression_statement; 125, 126; 126, yield; 126, 127; 127, call; 127, 128; 127, 129; 128, identifier:chmod; 129, argument_list; 129, 130; 129, 131; 130, identifier:remote_filename; 131, identifier:mode; 132, comment; 133, else_clause; 133, 134; 134, block; 134, 135; 134, 142; 134, 153; 134, 154; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:local_sum; 138, call; 138, 139; 138, 140; 139, identifier:get_file_sha1; 140, argument_list; 140, 141; 141, identifier:local_filename; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:remote_sum; 145, call; 145, 146; 145, 151; 146, attribute; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:host; 149, identifier:fact; 150, identifier:sha1_file; 151, argument_list; 151, 152; 152, identifier:remote_filename; 153, comment; 154, if_statement; 154, 155; 154, 158; 154, 187; 155, comparison_operator:!=; 155, 156; 155, 157; 156, identifier:local_sum; 157, identifier:remote_sum; 158, block; 158, 159; 158, 164; 158, 177; 159, expression_statement; 159, 160; 160, yield; 160, 161; 161, tuple; 161, 162; 161, 163; 162, identifier:local_file; 163, identifier:remote_filename; 164, if_statement; 164, 165; 164, 168; 165, boolean_operator:or; 165, 166; 165, 167; 166, identifier:user; 167, identifier:group; 168, block; 168, 169; 169, expression_statement; 169, 170; 170, yield; 170, 171; 171, call; 171, 172; 171, 173; 172, identifier:chown; 173, argument_list; 173, 174; 173, 175; 173, 176; 174, identifier:remote_filename; 175, identifier:user; 176, identifier:group; 177, if_statement; 177, 178; 177, 179; 178, identifier:mode; 179, block; 179, 180; 180, expression_statement; 180, 181; 181, yield; 181, 182; 182, call; 182, 183; 182, 184; 183, identifier:chmod; 184, argument_list; 184, 185; 184, 186; 185, identifier:remote_filename; 186, identifier:mode; 187, else_clause; 187, 188; 187, 189; 188, comment; 189, block; 189, 190; 189, 206; 189, 207; 190, if_statement; 190, 191; 190, 198; 191, boolean_operator:and; 191, 192; 191, 193; 192, identifier:mode; 193, comparison_operator:!=; 193, 194; 193, 197; 194, subscript; 194, 195; 194, 196; 195, identifier:remote_file; 196, string:'mode'; 197, identifier:mode; 198, block; 198, 199; 199, expression_statement; 199, 200; 200, yield; 200, 201; 201, call; 201, 202; 201, 203; 202, identifier:chmod; 203, argument_list; 203, 204; 203, 205; 204, identifier:remote_filename; 205, identifier:mode; 206, comment; 207, if_statement; 207, 208; 207, 226; 208, parenthesized_expression; 208, 209; 209, boolean_operator:or; 209, 210; 209, 218; 210, parenthesized_expression; 210, 211; 211, boolean_operator:and; 211, 212; 211, 213; 212, identifier:user; 213, comparison_operator:!=; 213, 214; 213, 217; 214, subscript; 214, 215; 214, 216; 215, identifier:remote_file; 216, string:'user'; 217, identifier:user; 218, parenthesized_expression; 218, 219; 219, boolean_operator:and; 219, 220; 219, 221; 220, identifier:group; 221, comparison_operator:!=; 221, 222; 221, 225; 222, subscript; 222, 223; 222, 224; 223, identifier:remote_file; 224, string:'group'; 225, identifier:group; 226, block; 226, 227; 227, expression_statement; 227, 228; 228, yield; 228, 229; 229, call; 229, 230; 229, 231; 230, identifier:chown; 231, argument_list; 231, 232; 231, 233; 231, 234; 232, identifier:remote_filename; 233, identifier:user; 234, identifier:group
def put( state, host, local_filename, remote_filename, user=None, group=None, mode=None, add_deploy_dir=True, ): ''' Copy a local file to the remote system. + local_filename: local filename + remote_filename: remote filename + user: user to own the files + group: group to own the files + mode: permissions of the files ''' # Upload IO objects as-is if hasattr(local_filename, 'read'): local_file = local_filename # Assume string filename else: # Add deploy directory? if add_deploy_dir and state.deploy_dir: local_filename = path.join(state.deploy_dir, local_filename) local_file = local_filename if not path.isfile(local_file): raise IOError('No such file: {0}'.format(local_file)) mode = ensure_mode_int(mode) remote_file = host.fact.file(remote_filename) # No remote file, always upload and user/group/mode if supplied if not remote_file: yield (local_file, remote_filename) if user or group: yield chown(remote_filename, user, group) if mode: yield chmod(remote_filename, mode) # File exists, check sum and check user/group/mode if supplied else: local_sum = get_file_sha1(local_filename) remote_sum = host.fact.sha1_file(remote_filename) # Check sha1sum, upload if needed if local_sum != remote_sum: yield (local_file, remote_filename) if user or group: yield chown(remote_filename, user, group) if mode: yield chmod(remote_filename, mode) else: # Check mode if mode and remote_file['mode'] != mode: yield chmod(remote_filename, mode) # Check user/group if ( (user and remote_file['user'] != user) or (group and remote_file['group'] != group) ): yield chown(remote_filename, user, group)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_run_single_op; 3, parameters; 3, 4; 3, 5; 4, identifier:state; 5, identifier:op_hash; 6, block; 6, 7; 6, 9; 6, 17; 6, 21; 6, 33; 6, 45; 6, 108; 6, 114; 6, 306; 6, 307; 6, 320; 7, expression_statement; 7, 8; 8, string:''' Run a single operation for all servers. Can be configured to run in serial. '''; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:op_meta; 12, subscript; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:state; 15, identifier:op_meta; 16, identifier:op_hash; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:op_types; 20, list:[]; 21, if_statement; 21, 22; 21, 25; 22, subscript; 22, 23; 22, 24; 23, identifier:op_meta; 24, string:'serial'; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:op_types; 30, identifier:append; 31, argument_list; 31, 32; 32, string:'serial'; 33, if_statement; 33, 34; 33, 37; 34, subscript; 34, 35; 34, 36; 35, identifier:op_meta; 36, string:'run_once'; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:op_types; 42, identifier:append; 43, argument_list; 43, 44; 44, string:'run once'; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:logger; 49, identifier:info; 50, argument_list; 50, 51; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, string:'{0} {1} {2}'; 54, identifier:format; 55, argument_list; 55, 56; 55, 81; 55, 97; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:click; 59, identifier:style; 60, argument_list; 60, 61; 60, 80; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, string:'--> Starting{0}operation:'; 64, identifier:format; 65, argument_list; 65, 66; 66, conditional_expression:if; 66, 67; 66, 78; 66, 79; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, string:' {0} '; 70, identifier:format; 71, argument_list; 71, 72; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, string:', '; 75, identifier:join; 76, argument_list; 76, 77; 77, identifier:op_types; 78, identifier:op_types; 79, string:' '; 80, string:'blue'; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:click; 84, identifier:style; 85, argument_list; 85, 86; 85, 94; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, string:', '; 89, identifier:join; 90, argument_list; 90, 91; 91, subscript; 91, 92; 91, 93; 92, identifier:op_meta; 93, string:'names'; 94, keyword_argument; 94, 95; 94, 96; 95, identifier:bold; 96, True; 97, conditional_expression:if; 97, 98; 97, 104; 97, 107; 98, call; 98, 99; 98, 100; 99, identifier:tuple; 100, argument_list; 100, 101; 101, subscript; 101, 102; 101, 103; 102, identifier:op_meta; 103, string:'args'; 104, subscript; 104, 105; 104, 106; 105, identifier:op_meta; 106, string:'args'; 107, string:''; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:failed_hosts; 111, call; 111, 112; 111, 113; 112, identifier:set; 113, argument_list; 114, if_statement; 114, 115; 114, 118; 114, 164; 115, subscript; 115, 116; 115, 117; 116, identifier:op_meta; 117, string:'serial'; 118, block; 118, 119; 119, with_statement; 119, 120; 119, 131; 119, 132; 120, with_clause; 120, 121; 121, with_item; 121, 122; 122, as_pattern; 122, 123; 122, 129; 123, call; 123, 124; 123, 125; 124, identifier:progress_spinner; 125, argument_list; 125, 126; 126, attribute; 126, 127; 126, 128; 127, identifier:state; 128, identifier:inventory; 129, as_pattern_target; 129, 130; 130, identifier:progress; 131, comment; 132, block; 132, 133; 133, for_statement; 133, 134; 133, 135; 133, 138; 134, identifier:host; 135, attribute; 135, 136; 135, 137; 136, identifier:state; 137, identifier:inventory; 138, block; 138, 139; 138, 148; 138, 153; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:result; 142, call; 142, 143; 142, 144; 143, identifier:_run_server_op; 144, argument_list; 144, 145; 144, 146; 144, 147; 145, identifier:state; 146, identifier:host; 147, identifier:op_hash; 148, expression_statement; 148, 149; 149, call; 149, 150; 149, 151; 150, identifier:progress; 151, argument_list; 151, 152; 152, identifier:host; 153, if_statement; 153, 154; 153, 156; 154, not_operator; 154, 155; 155, identifier:result; 156, block; 156, 157; 157, expression_statement; 157, 158; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:failed_hosts; 161, identifier:add; 162, argument_list; 162, 163; 163, identifier:host; 164, else_clause; 164, 165; 164, 166; 165, comment; 166, block; 166, 167; 166, 174; 166, 175; 166, 218; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:batches; 170, list:[state.inventory]; 170, 171; 171, attribute; 171, 172; 171, 173; 172, identifier:state; 173, identifier:inventory; 174, comment; 175, if_statement; 175, 176; 175, 179; 176, subscript; 176, 177; 176, 178; 177, identifier:op_meta; 178, string:'parallel'; 179, block; 179, 180; 179, 186; 179, 195; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:parallel; 183, subscript; 183, 184; 183, 185; 184, identifier:op_meta; 185, string:'parallel'; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:hosts; 189, call; 189, 190; 189, 191; 190, identifier:list; 191, argument_list; 191, 192; 192, attribute; 192, 193; 192, 194; 193, identifier:state; 194, identifier:inventory; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:batches; 198, list_comprehension; 198, 199; 198, 207; 199, subscript; 199, 200; 199, 201; 200, identifier:hosts; 201, slice; 201, 202; 201, 203; 201, 204; 202, identifier:i; 203, colon; 204, binary_operator:+; 204, 205; 204, 206; 205, identifier:i; 206, identifier:parallel; 207, for_in_clause; 207, 208; 207, 209; 208, identifier:i; 209, call; 209, 210; 209, 211; 210, identifier:range; 211, argument_list; 211, 212; 211, 213; 211, 217; 212, integer:0; 213, call; 213, 214; 213, 215; 214, identifier:len; 215, argument_list; 215, 216; 216, identifier:hosts; 217, identifier:parallel; 218, for_statement; 218, 219; 218, 220; 218, 221; 219, identifier:batch; 220, identifier:batches; 221, block; 221, 222; 222, with_statement; 222, 223; 222, 232; 222, 233; 223, with_clause; 223, 224; 224, with_item; 224, 225; 225, as_pattern; 225, 226; 225, 230; 226, call; 226, 227; 226, 228; 227, identifier:progress_spinner; 228, argument_list; 228, 229; 229, identifier:batch; 230, as_pattern_target; 230, 231; 231, identifier:progress; 232, comment; 233, block; 233, 234; 233, 254; 233, 255; 233, 279; 233, 280; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 237; 236, identifier:greenlet_to_host; 237, dictionary_comprehension; 237, 238; 237, 251; 238, pair; 238, 239; 238, 250; 239, call; 239, 240; 239, 245; 240, attribute; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:state; 243, identifier:pool; 244, identifier:spawn; 245, argument_list; 245, 246; 245, 247; 245, 248; 245, 249; 246, identifier:_run_server_op; 247, identifier:state; 248, identifier:host; 249, identifier:op_hash; 250, identifier:host; 251, for_in_clause; 251, 252; 251, 253; 252, identifier:host; 253, identifier:batch; 254, comment; 255, for_statement; 255, 256; 255, 257; 255, 267; 256, identifier:greenlet; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:gevent; 260, identifier:iwait; 261, argument_list; 261, 262; 262, call; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:greenlet_to_host; 265, identifier:keys; 266, argument_list; 267, block; 267, 268; 267, 274; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 271; 270, identifier:host; 271, subscript; 271, 272; 271, 273; 272, identifier:greenlet_to_host; 273, identifier:greenlet; 274, expression_statement; 274, 275; 275, call; 275, 276; 275, 277; 276, identifier:progress; 277, argument_list; 277, 278; 278, identifier:host; 279, comment; 280, for_statement; 280, 281; 280, 284; 280, 290; 281, pattern_list; 281, 282; 281, 283; 282, identifier:greenlet; 283, identifier:host; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:six; 287, identifier:iteritems; 288, argument_list; 288, 289; 289, identifier:greenlet_to_host; 290, block; 290, 291; 291, if_statement; 291, 292; 291, 298; 292, not_operator; 292, 293; 293, call; 293, 294; 293, 297; 294, attribute; 294, 295; 294, 296; 295, identifier:greenlet; 296, identifier:get; 297, argument_list; 298, block; 298, 299; 299, expression_statement; 299, 300; 300, call; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:failed_hosts; 303, identifier:add; 304, argument_list; 304, 305; 305, identifier:host; 306, comment; 307, if_statement; 307, 308; 307, 312; 308, not_operator; 308, 309; 309, subscript; 309, 310; 309, 311; 310, identifier:op_meta; 311, string:'ignore_errors'; 312, block; 312, 313; 313, expression_statement; 313, 314; 314, call; 314, 315; 314, 318; 315, attribute; 315, 316; 315, 317; 316, identifier:state; 317, identifier:fail_hosts; 318, argument_list; 318, 319; 319, identifier:failed_hosts; 320, if_statement; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:pyinfra; 323, identifier:is_cli; 324, block; 324, 325; 325, expression_statement; 325, 326; 326, call; 326, 327; 326, 328; 327, identifier:print; 328, argument_list
def _run_single_op(state, op_hash): ''' Run a single operation for all servers. Can be configured to run in serial. ''' op_meta = state.op_meta[op_hash] op_types = [] if op_meta['serial']: op_types.append('serial') if op_meta['run_once']: op_types.append('run once') logger.info('{0} {1} {2}'.format( click.style('--> Starting{0}operation:'.format( ' {0} '.format(', '.join(op_types)) if op_types else ' ', ), 'blue'), click.style(', '.join(op_meta['names']), bold=True), tuple(op_meta['args']) if op_meta['args'] else '', )) failed_hosts = set() if op_meta['serial']: with progress_spinner(state.inventory) as progress: # For each host, run the op for host in state.inventory: result = _run_server_op(state, host, op_hash) progress(host) if not result: failed_hosts.add(host) else: # Start with the whole inventory in one batch batches = [state.inventory] # If parallel set break up the inventory into a series of batches if op_meta['parallel']: parallel = op_meta['parallel'] hosts = list(state.inventory) batches = [ hosts[i:i + parallel] for i in range(0, len(hosts), parallel) ] for batch in batches: with progress_spinner(batch) as progress: # Spawn greenlet for each host greenlet_to_host = { state.pool.spawn(_run_server_op, state, host, op_hash): host for host in batch } # Trigger CLI progress as hosts complete if provided for greenlet in gevent.iwait(greenlet_to_host.keys()): host = greenlet_to_host[greenlet] progress(host) # Get all the results for greenlet, host in six.iteritems(greenlet_to_host): if not greenlet.get(): failed_hosts.add(host) # Now all the batches/hosts are complete, fail any failures if not op_meta['ignore_errors']: state.fail_hosts(failed_hosts) if pyinfra.is_cli: print()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:send; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 48; 5, 74; 5, 75; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, if_statement; 9, 10; 9, 14; 9, 21; 10, not_operator; 10, 11; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:_receive_has_been_called; 14, block; 14, 15; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:all_addrs; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:_dest_addrs; 21, else_clause; 21, 22; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:all_addrs; 26, list_comprehension; 26, 27; 26, 28; 26, 42; 27, identifier:a; 28, for_in_clause; 28, 29; 28, 32; 29, tuple_pattern; 29, 30; 29, 31; 30, identifier:i; 31, identifier:a; 32, call; 32, 33; 32, 34; 33, identifier:list; 34, argument_list; 34, 35; 35, call; 35, 36; 35, 41; 36, attribute; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:_id_to_addr; 40, identifier:items; 41, argument_list; 42, if_clause; 42, 43; 43, comparison_operator:in; 43, 44; 43, 45; 44, identifier:i; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:_remaining_ids; 48, if_statement; 48, 49; 48, 54; 48, 55; 48, 56; 48, 57; 48, 58; 49, comparison_operator:is; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:_last_used_id; 53, None; 54, comment; 55, comment; 56, comment; 57, comment; 58, block; 58, 59; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:self; 63, identifier:_last_used_id; 64, binary_operator:&; 64, 65; 64, 73; 65, call; 65, 66; 65, 67; 66, identifier:int; 67, argument_list; 67, 68; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:time; 71, identifier:time; 72, argument_list; 73, integer:0xffff; 74, comment; 75, for_statement; 75, 76; 75, 77; 75, 78; 75, 79; 76, identifier:addr; 77, identifier:all_addrs; 78, comment; 79, block; 79, 80; 79, 93; 79, 94; 79, 95; 79, 105; 79, 106; 79, 107; 79, 108; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:_last_used_id; 85, binary_operator:&; 85, 86; 85, 92; 86, parenthesized_expression; 86, 87; 87, binary_operator:+; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:_last_used_id; 91, integer:1; 92, integer:0xffff; 93, comment; 94, comment; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 104; 97, subscript; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:_id_to_addr; 101, attribute; 101, 102; 101, 103; 102, identifier:self; 103, identifier:_last_used_id; 104, identifier:addr; 105, comment; 106, comment; 107, comment; 108, expression_statement; 108, 109; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:self; 112, identifier:_send_ping; 113, argument_list; 113, 114; 113, 115; 114, identifier:addr; 115, keyword_argument; 115, 116; 115, 117; 116, identifier:payload; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:struct; 120, identifier:pack; 121, argument_list; 121, 122; 121, 123; 122, string:"d"; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:time; 126, identifier:time; 127, argument_list
def send(self): """ Send pings to multiple addresses, ensuring unique IDs for each request. This operation is non-blocking. Use 'receive' to get the results. Send can be called multiple times. If there are any addresses left from the previous send, from which results have not been received yet, then it will resend pings to those remaining addresses. """ # Collect all the addresses for which we have not seen responses yet. if not self._receive_has_been_called: all_addrs = self._dest_addrs else: all_addrs = [a for (i, a) in list(self._id_to_addr.items()) if i in self._remaining_ids] if self._last_used_id is None: # Will attempt to continue at the last request ID we used. But if # we never sent anything before then we create a first ID # 'randomly' from the current time. ID is only a 16 bit field, so # need to trim it down. self._last_used_id = int(time.time()) & 0xffff # Send ICMPecho to all addresses... for addr in all_addrs: # Make a unique ID, wrapping around at 65535. self._last_used_id = (self._last_used_id + 1) & 0xffff # Remember the address for each ID so we can produce meaningful # result lists later on. self._id_to_addr[self._last_used_id] = addr # Send an ICMPecho request packet. We specify a payload consisting # of the current time stamp. This is returned to us in the # response and allows us to calculate the 'ping time'. self._send_ping(addr, payload=struct.pack("d", time.time()))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_parse; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:registers; 6, block; 6, 7; 6, 9; 6, 21; 6, 40; 6, 41; 6, 55; 6, 65; 6, 66; 6, 89; 6, 99; 6, 100; 6, 124; 6, 137; 6, 138; 6, 149; 6, 161; 6, 162; 6, 163; 6, 164; 6, 165; 6, 166; 6, 172; 6, 173; 6, 183; 6, 184; 6, 190; 6, 191; 6, 199; 6, 238; 6, 239; 6, 240; 6, 253; 6, 263; 6, 269; 6, 270; 6, 280; 6, 281; 6, 293; 6, 294; 6, 300; 6, 301; 6, 311; 6, 312; 6, 318; 6, 319; 6, 333; 6, 347; 6, 348; 6, 349; 6, 374; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:result; 12, dictionary; 12, 13; 12, 18; 13, pair; 13, 14; 13, 15; 14, string:'ip'; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:ip; 18, pair; 18, 19; 18, 20; 19, string:'connected'; 20, True; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:decoder; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:BinaryPayloadDecoder; 27, identifier:fromRegisters; 28, argument_list; 28, 29; 28, 30; 28, 35; 29, identifier:registers; 30, keyword_argument; 30, 31; 30, 32; 31, identifier:byteorder; 32, attribute; 32, 33; 32, 34; 33, identifier:Endian; 34, identifier:Big; 35, keyword_argument; 35, 36; 35, 37; 36, identifier:wordorder; 37, attribute; 37, 38; 37, 39; 38, identifier:Endian; 39, identifier:Little; 40, comment; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:b; 44, list:[decoder.decode_bits(), decoder.decode_bits()]; 44, 45; 44, 50; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:decoder; 48, identifier:decode_bits; 49, argument_list; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:decoder; 53, identifier:decode_bits; 54, argument_list; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:reg_40001; 58, binary_operator:+; 58, 59; 58, 62; 59, subscript; 59, 60; 59, 61; 60, identifier:b; 61, integer:1; 62, subscript; 62, 63; 62, 64; 63, identifier:b; 64, integer:0; 65, comment; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:monitor_integer; 69, call; 69, 70; 69, 71; 70, identifier:sum; 71, generator_expression; 71, 72; 71, 75; 71, 87; 72, binary_operator:<<; 72, 73; 72, 74; 73, integer:1; 74, identifier:i; 75, for_in_clause; 75, 76; 75, 79; 76, pattern_list; 76, 77; 76, 78; 77, identifier:i; 78, identifier:b; 79, call; 79, 80; 79, 81; 80, identifier:enumerate; 81, argument_list; 81, 82; 82, subscript; 82, 83; 82, 84; 83, identifier:reg_40001; 84, slice; 84, 85; 84, 86; 85, colon; 86, integer:4; 87, if_clause; 87, 88; 88, identifier:b; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 94; 91, subscript; 91, 92; 91, 93; 92, identifier:result; 93, string:'state'; 94, subscript; 94, 95; 94, 98; 95, subscript; 95, 96; 95, 97; 96, identifier:options; 97, string:'monitor state'; 98, identifier:monitor_integer; 99, comment; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:fault_integer; 103, call; 103, 104; 103, 105; 104, identifier:sum; 105, generator_expression; 105, 106; 105, 109; 105, 122; 106, binary_operator:<<; 106, 107; 106, 108; 107, integer:1; 108, identifier:i; 109, for_in_clause; 109, 110; 109, 113; 110, pattern_list; 110, 111; 110, 112; 111, identifier:i; 112, identifier:b; 113, call; 113, 114; 113, 115; 114, identifier:enumerate; 115, argument_list; 115, 116; 116, subscript; 116, 117; 116, 118; 117, identifier:reg_40001; 118, slice; 118, 119; 118, 120; 118, 121; 119, integer:4; 120, colon; 121, integer:6; 122, if_clause; 122, 123; 123, identifier:b; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 129; 126, subscript; 126, 127; 126, 128; 127, identifier:result; 128, string:'fault'; 129, dictionary; 129, 130; 130, pair; 130, 131; 130, 132; 131, string:'status'; 132, subscript; 132, 133; 132, 136; 133, subscript; 133, 134; 133, 135; 134, identifier:options; 135, string:'fault status'; 136, identifier:fault_integer; 137, comment; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 143; 140, pattern_list; 140, 141; 140, 142; 141, identifier:low; 142, identifier:high; 143, subscript; 143, 144; 143, 145; 144, identifier:reg_40001; 145, slice; 145, 146; 145, 147; 145, 148; 146, integer:6; 147, colon; 148, integer:8; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 154; 151, subscript; 151, 152; 151, 153; 152, identifier:result; 153, string:'alarm'; 154, subscript; 154, 155; 154, 158; 155, subscript; 155, 156; 155, 157; 156, identifier:options; 157, string:'alarm level'; 158, binary_operator:+; 158, 159; 158, 160; 159, identifier:low; 160, identifier:high; 161, comment; 162, comment; 163, comment; 164, comment; 165, comment; 166, expression_statement; 166, 167; 167, augmented_assignment:+=; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:decoder; 170, identifier:_pointer; 171, integer:2; 172, comment; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 178; 175, subscript; 175, 176; 175, 177; 176, identifier:result; 177, string:'concentration'; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:decoder; 181, identifier:decode_32bit_float; 182, argument_list; 183, comment; 184, expression_statement; 184, 185; 185, augmented_assignment:+=; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:decoder; 188, identifier:_pointer; 189, integer:2; 190, comment; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 194; 193, identifier:fault_number; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:decoder; 197, identifier:decode_16bit_uint; 198, argument_list; 199, if_statement; 199, 200; 199, 203; 200, comparison_operator:!=; 200, 201; 200, 202; 201, identifier:fault_number; 202, integer:0; 203, block; 203, 204; 203, 219; 203, 227; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:code; 207, binary_operator:+; 207, 208; 207, 215; 208, parenthesized_expression; 208, 209; 209, conditional_expression:if; 209, 210; 209, 211; 209, 214; 210, string:'m'; 211, comparison_operator:<; 211, 212; 211, 213; 212, identifier:fault_number; 213, integer:30; 214, string:'F'; 215, call; 215, 216; 215, 217; 216, identifier:str; 217, argument_list; 217, 218; 218, identifier:fault_number; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 226; 221, subscript; 221, 222; 221, 225; 222, subscript; 222, 223; 222, 224; 223, identifier:result; 224, string:'fault'; 225, string:'code'; 226, identifier:code; 227, expression_statement; 227, 228; 228, call; 228, 229; 228, 234; 229, attribute; 229, 230; 229, 233; 230, subscript; 230, 231; 230, 232; 231, identifier:result; 232, string:'fault'; 233, identifier:update; 234, argument_list; 234, 235; 235, subscript; 235, 236; 235, 237; 236, identifier:faults; 237, identifier:code; 238, comment; 239, comment; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:unit_bit; 243, call; 243, 244; 243, 251; 244, attribute; 244, 245; 244, 250; 245, call; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:decoder; 248, identifier:decode_bits; 249, argument_list; 250, identifier:index; 251, argument_list; 251, 252; 252, True; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 258; 255, subscript; 255, 256; 255, 257; 256, identifier:result; 257, string:'units'; 258, subscript; 258, 259; 258, 262; 259, subscript; 259, 260; 259, 261; 260, identifier:options; 261, string:'concentration unit'; 262, identifier:unit_bit; 263, expression_statement; 263, 264; 264, augmented_assignment:+=; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:decoder; 267, identifier:_pointer; 268, integer:1; 269, comment; 270, expression_statement; 270, 271; 271, assignment; 271, 272; 271, 275; 272, subscript; 272, 273; 272, 274; 273, identifier:result; 274, string:'temperature'; 275, call; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:decoder; 278, identifier:decode_16bit_int; 279, argument_list; 280, comment; 281, expression_statement; 281, 282; 282, assignment; 282, 283; 282, 286; 283, subscript; 283, 284; 283, 285; 284, identifier:result; 285, string:'life'; 286, binary_operator:/; 286, 287; 286, 292; 287, call; 287, 288; 287, 291; 288, attribute; 288, 289; 288, 290; 289, identifier:decoder; 290, identifier:decode_16bit_uint; 291, argument_list; 292, float:24.0; 293, comment; 294, expression_statement; 294, 295; 295, augmented_assignment:+=; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:decoder; 298, identifier:_pointer; 299, integer:2; 300, comment; 301, expression_statement; 301, 302; 302, assignment; 302, 303; 302, 306; 303, subscript; 303, 304; 303, 305; 304, identifier:result; 305, string:'flow'; 306, call; 306, 307; 306, 310; 307, attribute; 307, 308; 307, 309; 308, identifier:decoder; 309, identifier:decode_16bit_uint; 310, argument_list; 311, comment; 312, expression_statement; 312, 313; 313, augmented_assignment:+=; 313, 314; 313, 317; 314, attribute; 314, 315; 314, 316; 315, identifier:decoder; 316, identifier:_pointer; 317, integer:2; 318, comment; 319, expression_statement; 319, 320; 320, assignment; 320, 321; 320, 324; 321, subscript; 321, 322; 321, 323; 322, identifier:result; 323, string:'low-alarm threshold'; 324, call; 324, 325; 324, 326; 325, identifier:round; 326, argument_list; 326, 327; 326, 332; 327, call; 327, 328; 327, 331; 328, attribute; 328, 329; 328, 330; 329, identifier:decoder; 330, identifier:decode_32bit_float; 331, argument_list; 332, integer:6; 333, expression_statement; 333, 334; 334, assignment; 334, 335; 334, 338; 335, subscript; 335, 336; 335, 337; 336, identifier:result; 337, string:'high-alarm threshold'; 338, call; 338, 339; 338, 340; 339, identifier:round; 340, argument_list; 340, 341; 340, 346; 341, call; 341, 342; 341, 345; 342, attribute; 342, 343; 342, 344; 343, identifier:decoder; 344, identifier:decode_32bit_float; 345, argument_list; 346, integer:6; 347, comment; 348, comment; 349, if_statement; 349, 350; 349, 355; 350, comparison_operator:==; 350, 351; 350, 354; 351, subscript; 351, 352; 351, 353; 352, identifier:result; 353, string:'units'; 354, string:'ppb'; 355, block; 355, 356; 355, 362; 355, 368; 356, expression_statement; 356, 357; 357, augmented_assignment:*=; 357, 358; 357, 361; 358, subscript; 358, 359; 358, 360; 359, identifier:result; 360, string:'concentration'; 361, integer:1000; 362, expression_statement; 362, 363; 363, augmented_assignment:*=; 363, 364; 363, 367; 364, subscript; 364, 365; 364, 366; 365, identifier:result; 366, string:'low-alarm threshold'; 367, integer:1000; 368, expression_statement; 368, 369; 369, augmented_assignment:*=; 369, 370; 369, 373; 370, subscript; 370, 371; 370, 372; 371, identifier:result; 372, string:'high-alarm threshold'; 373, integer:1000; 374, return_statement; 374, 375; 375, identifier:result
def _parse(self, registers): """Parse the response, returning a dictionary.""" result = {'ip': self.ip, 'connected': True} decoder = BinaryPayloadDecoder.fromRegisters(registers, byteorder=Endian.Big, wordorder=Endian.Little) # Register 40001 is a collection of alarm status signals b = [decoder.decode_bits(), decoder.decode_bits()] reg_40001 = b[1] + b[0] # Bits 0-3 map to the monitor state monitor_integer = sum(1 << i for i, b in enumerate(reg_40001[:4]) if b) result['state'] = options['monitor state'][monitor_integer] # Bits 4-5 map to fault status fault_integer = sum(1 << i for i, b in enumerate(reg_40001[4:6]) if b) result['fault'] = {'status': options['fault status'][fault_integer]} # Bits 6 and 7 tell if low and high alarms are active low, high = reg_40001[6:8] result['alarm'] = options['alarm level'][low + high] # Bits 8-10 tell if internal sensor relays 1-3 are energized. Skipping. # Bit 11 is a heartbeat bit that toggles every two seconds. Skipping. # Bit 12 tells if relays are under modbus control. Skipping. # Remaining bits are empty. Skipping. # Register 40002 has a gas ID and a sensor cartridge ID. Skipping. decoder._pointer += 2 # Registers 40003-40004 are the gas concentration as a float result['concentration'] = decoder.decode_32bit_float() # Register 40005 is the concentration as an int. Skipping. decoder._pointer += 2 # Register 40006 is the number of the most important fault. fault_number = decoder.decode_16bit_uint() if fault_number != 0: code = ('m' if fault_number < 30 else 'F') + str(fault_number) result['fault']['code'] = code result['fault'].update(faults[code]) # Register 40007 holds the concentration unit in the second byte # Instead of being an int, it's the position of the up bit unit_bit = decoder.decode_bits().index(True) result['units'] = options['concentration unit'][unit_bit] decoder._pointer += 1 # Register 40008 holds the sensor temperature in Celsius result['temperature'] = decoder.decode_16bit_int() # Register 40009 holds number of hours remaining in cell life result['life'] = decoder.decode_16bit_uint() / 24.0 # Register 40010 holds the number of heartbeats (16 LSB). Skipping. decoder._pointer += 2 # Register 40011 is the sample flow rate in cc / min result['flow'] = decoder.decode_16bit_uint() # Register 40012 is blank. Skipping. decoder._pointer += 2 # Registers 40013-40016 are the alarm concentration thresholds result['low-alarm threshold'] = round(decoder.decode_32bit_float(), 6) result['high-alarm threshold'] = round(decoder.decode_32bit_float(), 6) # Despite what the manual says, thresholds are always reported in ppm. # Let's fix that to match the concentration units. if result['units'] == 'ppb': result['concentration'] *= 1000 result['low-alarm threshold'] *= 1000 result['high-alarm threshold'] *= 1000 return result
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:ParseCFlags; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:start_line; 7, integer:0; 8, block; 8, 9; 8, 11; 8, 15; 8, 16; 8, 20; 8, 24; 8, 227; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:modname; 14, None; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:modlist; 19, list:[]; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:flag; 23, None; 24, for_statement; 24, 25; 24, 26; 24, 36; 24, 37; 25, identifier:line_num; 26, call; 26, 27; 26, 28; 27, identifier:range; 28, argument_list; 28, 29; 28, 30; 29, identifier:start_line; 30, call; 30, 31; 30, 32; 31, identifier:len; 32, argument_list; 32, 33; 33, attribute; 33, 34; 33, 35; 34, identifier:self; 35, identifier:output; 36, comment; 37, block; 37, 38; 37, 50; 37, 71; 37, 82; 37, 146; 37, 157; 37, 200; 37, 201; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:line; 41, call; 41, 42; 41, 49; 42, attribute; 42, 43; 42, 48; 43, subscript; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:self; 46, identifier:output; 47, identifier:line_num; 48, identifier:rstrip; 49, argument_list; 50, if_statement; 50, 51; 50, 53; 50, 54; 51, not_operator; 51, 52; 52, identifier:line; 53, comment; 54, block; 54, 55; 54, 70; 55, if_statement; 55, 56; 55, 57; 55, 58; 56, identifier:flag; 57, comment; 58, block; 58, 59; 58, 66; 59, expression_statement; 59, 60; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:modlist; 63, identifier:append; 64, argument_list; 64, 65; 65, identifier:flag; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:flag; 69, None; 70, continue_statement; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:mobj; 74, call; 74, 75; 74, 80; 75, attribute; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:self; 78, identifier:module_c_re; 79, identifier:match; 80, argument_list; 80, 81; 81, identifier:line; 82, if_statement; 82, 83; 82, 84; 82, 85; 83, identifier:mobj; 84, comment; 85, block; 85, 86; 85, 95; 85, 104; 85, 114; 85, 123; 85, 133; 85, 141; 85, 145; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:modname; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:mobj; 92, identifier:group; 93, argument_list; 93, 94; 94, integer:1; 95, expression_statement; 95, 96; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:logging; 99, identifier:debug; 100, argument_list; 100, 101; 101, binary_operator:%; 101, 102; 101, 103; 102, string:'Module: %s'; 103, identifier:line; 104, if_statement; 104, 105; 104, 106; 105, identifier:flag; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:modlist; 111, identifier:append; 112, argument_list; 112, 113; 113, identifier:flag; 114, expression_statement; 114, 115; 115, call; 115, 116; 115, 121; 116, attribute; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:module_list; 120, identifier:append; 121, argument_list; 121, 122; 122, identifier:modname; 123, expression_statement; 123, 124; 124, call; 124, 125; 124, 130; 125, attribute; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:self; 128, identifier:modules; 129, identifier:setdefault; 130, argument_list; 130, 131; 130, 132; 131, identifier:modname; 132, list:[]; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:modlist; 136, subscript; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:self; 139, identifier:modules; 140, identifier:modname; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:flag; 144, None; 145, continue_statement; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:mobj; 149, call; 149, 150; 149, 155; 150, attribute; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:self; 153, identifier:flag_c_re; 154, identifier:match; 155, argument_list; 155, 156; 156, identifier:line; 157, if_statement; 157, 158; 157, 159; 157, 160; 158, identifier:mobj; 159, comment; 160, block; 160, 161; 160, 172; 160, 181; 160, 199; 161, if_statement; 161, 162; 161, 163; 161, 164; 162, identifier:flag; 163, comment; 164, block; 164, 165; 165, expression_statement; 165, 166; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:modlist; 169, identifier:append; 170, argument_list; 170, 171; 171, identifier:flag; 172, expression_statement; 172, 173; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:logging; 176, identifier:debug; 177, argument_list; 177, 178; 178, binary_operator:%; 178, 179; 178, 180; 179, string:'Flag: %s'; 180, identifier:line; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:flag; 184, call; 184, 185; 184, 186; 185, identifier:Flag; 186, argument_list; 186, 187; 186, 193; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:mobj; 190, identifier:group; 191, argument_list; 191, 192; 192, integer:1; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:mobj; 196, identifier:group; 197, argument_list; 197, 198; 198, integer:2; 199, continue_statement; 200, comment; 201, if_statement; 201, 202; 201, 203; 201, 216; 202, identifier:flag; 203, block; 203, 204; 204, expression_statement; 204, 205; 205, augmented_assignment:+=; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:flag; 208, identifier:help; 209, binary_operator:+; 209, 210; 209, 211; 210, string:' '; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:line; 214, identifier:strip; 215, argument_list; 216, else_clause; 216, 217; 217, block; 217, 218; 218, expression_statement; 218, 219; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:logging; 222, identifier:info; 223, argument_list; 223, 224; 224, binary_operator:%; 224, 225; 224, 226; 225, string:'Extra: %s'; 226, identifier:line; 227, if_statement; 227, 228; 227, 229; 228, identifier:flag; 229, block; 229, 230; 230, expression_statement; 230, 231; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:modlist; 234, identifier:append; 235, argument_list; 235, 236; 236, identifier:flag
def ParseCFlags(self, start_line=0): """Parse C style flags.""" modname = None # name of current module modlist = [] flag = None for line_num in range(start_line, len(self.output)): # collect flags line = self.output[line_num].rstrip() if not line: # blank lines terminate flags if flag: # save last flag modlist.append(flag) flag = None continue mobj = self.module_c_re.match(line) if mobj: # start of a new module modname = mobj.group(1) logging.debug('Module: %s' % line) if flag: modlist.append(flag) self.module_list.append(modname) self.modules.setdefault(modname, []) modlist = self.modules[modname] flag = None continue mobj = self.flag_c_re.match(line) if mobj: # start of a new flag if flag: # save last flag modlist.append(flag) logging.debug('Flag: %s' % line) flag = Flag(mobj.group(1), mobj.group(2)) continue # append to flag help. type and default are part of the main text if flag: flag.help += ' ' + line.strip() else: logging.info('Extra: %s' % line) if flag: modlist.append(flag)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 1, 13; 2, function_name:DEFINE_alias; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:name; 5, identifier:original_name; 6, default_parameter; 6, 7; 6, 8; 7, identifier:flag_values; 8, identifier:FLAGS; 9, default_parameter; 9, 10; 9, 11; 10, identifier:module_name; 11, None; 12, comment; 13, block; 13, 14; 13, 16; 13, 26; 13, 32; 13, 56; 13, 92; 13, 100; 13, 101; 14, expression_statement; 14, 15; 15, comment; 16, if_statement; 16, 17; 16, 20; 17, comparison_operator:not; 17, 18; 17, 19; 18, identifier:original_name; 19, identifier:flag_values; 20, block; 20, 21; 21, raise_statement; 21, 22; 22, call; 22, 23; 22, 24; 23, identifier:UnrecognizedFlagError; 24, argument_list; 24, 25; 25, identifier:original_name; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:flag; 29, subscript; 29, 30; 29, 31; 30, identifier:flag_values; 31, identifier:original_name; 32, class_definition; 32, 33; 32, 34; 32, 36; 33, identifier:_Parser; 34, argument_list; 34, 35; 35, identifier:ArgumentParser; 36, block; 36, 37; 36, 39; 37, expression_statement; 37, 38; 38, comment; 39, function_definition; 39, 40; 39, 41; 39, 44; 40, function_name:parse; 41, parameters; 41, 42; 41, 43; 42, identifier:self; 43, identifier:argument; 44, block; 44, 45; 44, 52; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:flag; 49, identifier:parse; 50, argument_list; 50, 51; 51, identifier:argument; 52, return_statement; 52, 53; 53, attribute; 53, 54; 53, 55; 54, identifier:flag; 55, identifier:value; 56, class_definition; 56, 57; 56, 58; 56, 60; 57, identifier:_FlagAlias; 58, argument_list; 58, 59; 59, identifier:Flag; 60, block; 60, 61; 60, 63; 60, 75; 61, expression_statement; 61, 62; 62, comment; 63, decorated_definition; 63, 64; 63, 66; 64, decorator; 64, 65; 65, identifier:property; 66, function_definition; 66, 67; 66, 68; 66, 70; 67, function_name:value; 68, parameters; 68, 69; 69, identifier:self; 70, block; 70, 71; 71, return_statement; 71, 72; 72, attribute; 72, 73; 72, 74; 73, identifier:flag; 74, identifier:value; 75, decorated_definition; 75, 76; 75, 80; 76, decorator; 76, 77; 77, attribute; 77, 78; 77, 79; 78, identifier:value; 79, identifier:setter; 80, function_definition; 80, 81; 80, 82; 80, 85; 81, function_name:value; 82, parameters; 82, 83; 82, 84; 83, identifier:self; 84, identifier:value; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:flag; 90, identifier:value; 91, identifier:value; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:help_msg; 95, binary_operator:%; 95, 96; 95, 97; 96, string:'Alias for --%s.'; 97, attribute; 97, 98; 97, 99; 98, identifier:flag; 99, identifier:name; 100, comment; 101, expression_statement; 101, 102; 102, call; 102, 103; 102, 104; 103, identifier:DEFINE_flag; 104, argument_list; 104, 105; 104, 124; 104, 125; 105, call; 105, 106; 105, 107; 106, identifier:_FlagAlias; 107, argument_list; 107, 108; 107, 111; 107, 114; 107, 115; 107, 118; 107, 119; 108, call; 108, 109; 108, 110; 109, identifier:_Parser; 110, argument_list; 111, attribute; 111, 112; 111, 113; 112, identifier:flag; 113, identifier:serializer; 114, identifier:name; 115, attribute; 115, 116; 115, 117; 116, identifier:flag; 117, identifier:default; 118, identifier:help_msg; 119, keyword_argument; 119, 120; 119, 121; 120, identifier:boolean; 121, attribute; 121, 122; 121, 123; 122, identifier:flag; 123, identifier:boolean; 124, identifier:flag_values; 125, identifier:module_name
def DEFINE_alias(name, original_name, flag_values=FLAGS, module_name=None): # pylint: disable=g-bad-name """Defines an alias flag for an existing one. Args: name: A string, name of the alias flag. original_name: A string, name of the original flag. flag_values: FlagValues object with which the flag will be registered. module_name: A string, the name of the module that defines this flag. Raises: gflags.FlagError: UnrecognizedFlagError: if the referenced flag doesn't exist. DuplicateFlagError: if the alias name has been used by some existing flag. """ if original_name not in flag_values: raise UnrecognizedFlagError(original_name) flag = flag_values[original_name] class _Parser(ArgumentParser): """The parser for the alias flag calls the original flag parser.""" def parse(self, argument): flag.parse(argument) return flag.value class _FlagAlias(Flag): """Overrides Flag class so alias value is copy of original flag value.""" @property def value(self): return flag.value @value.setter def value(self, value): flag.value = value help_msg = 'Alias for --%s.' % flag.name # If alias_name has been used, gflags.DuplicatedFlag will be raised. DEFINE_flag(_FlagAlias(_Parser(), flag.serializer, name, flag.default, help_msg, boolean=flag.boolean), flag_values, module_name)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:acquire; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:cls; 5, default_parameter; 5, 6; 5, 7; 6, identifier:name; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:lifetime; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:mechs; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:usage; 16, string:'both'; 17, default_parameter; 17, 18; 17, 19; 18, identifier:store; 19, None; 20, block; 20, 21; 20, 23; 20, 75; 21, expression_statement; 21, 22; 22, comment; 23, if_statement; 23, 24; 23, 27; 23, 40; 24, comparison_operator:is; 24, 25; 24, 26; 25, identifier:store; 26, None; 27, block; 27, 28; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:res; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:rcreds; 34, identifier:acquire_cred; 35, argument_list; 35, 36; 35, 37; 35, 38; 35, 39; 36, identifier:name; 37, identifier:lifetime; 38, identifier:mechs; 39, identifier:usage; 40, else_clause; 40, 41; 41, block; 41, 42; 41, 55; 41, 62; 42, if_statement; 42, 43; 42, 46; 43, comparison_operator:is; 43, 44; 43, 45; 44, identifier:rcred_cred_store; 45, None; 46, block; 46, 47; 47, raise_statement; 47, 48; 48, call; 48, 49; 48, 50; 49, identifier:NotImplementedError; 50, argument_list; 50, 51; 51, concatenated_string; 51, 52; 51, 53; 51, 54; 52, string:"Your GSSAPI implementation does "; 53, string:"not have support for manipulating "; 54, string:"credential stores"; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:store; 58, call; 58, 59; 58, 60; 59, identifier:_encode_dict; 60, argument_list; 60, 61; 61, identifier:store; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:res; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:rcred_cred_store; 68, identifier:acquire_cred_from; 69, argument_list; 69, 70; 69, 71; 69, 72; 69, 73; 69, 74; 70, identifier:store; 71, identifier:name; 72, identifier:lifetime; 73, identifier:mechs; 74, identifier:usage; 75, return_statement; 75, 76; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:tuples; 79, identifier:AcquireCredResult; 80, argument_list; 80, 81; 80, 89; 80, 92; 81, call; 81, 82; 81, 83; 82, identifier:cls; 83, argument_list; 83, 84; 84, keyword_argument; 84, 85; 84, 86; 85, identifier:base; 86, attribute; 86, 87; 86, 88; 87, identifier:res; 88, identifier:creds; 89, attribute; 89, 90; 89, 91; 90, identifier:res; 91, identifier:mechs; 92, attribute; 92, 93; 92, 94; 93, identifier:res; 94, identifier:lifetime
def acquire(cls, name=None, lifetime=None, mechs=None, usage='both', store=None): """Acquire GSSAPI credentials This method acquires credentials. If the `store` argument is used, the credentials will be acquired from the given credential store (if supported). Otherwise, the credentials are acquired from the default store. The credential store information is a dictionary containing mechanisms-specific keys and values pointing to a credential store or stores. Using a non-default store requires support for the credentials store extension. Args: name (Name): the name associated with the credentials, or None for the default name lifetime (int): the desired lifetime of the credentials, or None for indefinite mechs (list): the desired :class:`MechType` OIDs to be used with the credentials, or None for the default set usage (str): the usage for the credentials -- either 'both', 'initiate', or 'accept' store (dict): the credential store information pointing to the credential store from which to acquire the credentials, or None for the default store (:requires-ext:`cred_store`) Returns: AcquireCredResult: the acquired credentials and information about them Raises: BadMechanismError BadNameTypeError BadNameError ExpiredCredentialsError MissingCredentialsError """ if store is None: res = rcreds.acquire_cred(name, lifetime, mechs, usage) else: if rcred_cred_store is None: raise NotImplementedError("Your GSSAPI implementation does " "not have support for manipulating " "credential stores") store = _encode_dict(store) res = rcred_cred_store.acquire_cred_from(store, name, lifetime, mechs, usage) return tuples.AcquireCredResult(cls(base=res.creds), res.mechs, res.lifetime)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:store; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:store; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:usage; 10, string:'both'; 11, default_parameter; 11, 12; 11, 13; 12, identifier:mech; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:overwrite; 16, False; 17, default_parameter; 17, 18; 17, 19; 18, identifier:set_default; 19, False; 20, block; 20, 21; 20, 23; 21, expression_statement; 21, 22; 22, comment; 23, if_statement; 23, 24; 23, 27; 23, 51; 24, comparison_operator:is; 24, 25; 24, 26; 25, identifier:store; 26, None; 27, block; 27, 28; 27, 40; 28, if_statement; 28, 29; 28, 32; 29, comparison_operator:is; 29, 30; 29, 31; 30, identifier:rcred_rfc5588; 31, None; 32, block; 32, 33; 33, raise_statement; 33, 34; 34, call; 34, 35; 34, 36; 35, identifier:NotImplementedError; 36, argument_list; 36, 37; 37, concatenated_string; 37, 38; 37, 39; 38, string:"Your GSSAPI implementation does "; 39, string:"not have support for RFC 5588"; 40, return_statement; 40, 41; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:rcred_rfc5588; 44, identifier:store_cred; 45, argument_list; 45, 46; 45, 47; 45, 48; 45, 49; 45, 50; 46, identifier:self; 47, identifier:usage; 48, identifier:mech; 49, identifier:overwrite; 50, identifier:set_default; 51, else_clause; 51, 52; 52, block; 52, 53; 52, 66; 52, 73; 53, if_statement; 53, 54; 53, 57; 54, comparison_operator:is; 54, 55; 54, 56; 55, identifier:rcred_cred_store; 56, None; 57, block; 57, 58; 58, raise_statement; 58, 59; 59, call; 59, 60; 59, 61; 60, identifier:NotImplementedError; 61, argument_list; 61, 62; 62, concatenated_string; 62, 63; 62, 64; 62, 65; 63, string:"Your GSSAPI implementation does "; 64, string:"not have support for manipulating "; 65, string:"credential stores directly"; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:store; 69, call; 69, 70; 69, 71; 70, identifier:_encode_dict; 71, argument_list; 71, 72; 72, identifier:store; 73, return_statement; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:rcred_cred_store; 77, identifier:store_cred_into; 78, argument_list; 78, 79; 78, 80; 78, 81; 78, 82; 78, 83; 78, 84; 79, identifier:store; 80, identifier:self; 81, identifier:usage; 82, identifier:mech; 83, identifier:overwrite; 84, identifier:set_default
def store(self, store=None, usage='both', mech=None, overwrite=False, set_default=False): """Store these credentials into the given store This method stores the current credentials into the specified credentials store. If the default store is used, support for :rfc:`5588` is required. Otherwise, support for the credentials store extension is required. :requires-ext:`rfc5588` or :requires-ext:`cred_store` Args: store (dict): the store into which to store the credentials, or None for the default store. usage (str): the usage to store the credentials with -- either 'both', 'initiate', or 'accept' mech (OID): the :class:`MechType` to associate with the stored credentials overwrite (bool): whether or not to overwrite existing credentials stored with the same name, etc set_default (bool): whether or not to set these credentials as the default credentials for the given store. Returns: StoreCredResult: the results of the credential storing operation Raises: GSSError ExpiredCredentialsError MissingCredentialsError OperationUnavailableError DuplicateCredentialsElementError """ if store is None: if rcred_rfc5588 is None: raise NotImplementedError("Your GSSAPI implementation does " "not have support for RFC 5588") return rcred_rfc5588.store_cred(self, usage, mech, overwrite, set_default) else: if rcred_cred_store is None: raise NotImplementedError("Your GSSAPI implementation does " "not have support for manipulating " "credential stores directly") store = _encode_dict(store) return rcred_cred_store.store_cred_into(store, self, usage, mech, overwrite, set_default)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 22; 2, function_name:add; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 3, 16; 3, 19; 4, identifier:self; 5, identifier:name; 6, identifier:mech; 7, default_parameter; 7, 8; 7, 9; 8, identifier:usage; 9, string:'both'; 10, default_parameter; 10, 11; 10, 12; 11, identifier:init_lifetime; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:accept_lifetime; 15, None; 16, default_parameter; 16, 17; 16, 18; 17, identifier:impersonator; 18, None; 19, default_parameter; 19, 20; 19, 21; 20, identifier:store; 21, None; 22, block; 22, 23; 22, 25; 22, 41; 22, 129; 23, expression_statement; 23, 24; 24, comment; 25, if_statement; 25, 26; 25, 33; 26, boolean_operator:and; 26, 27; 26, 30; 27, comparison_operator:is; 27, 28; 27, 29; 28, identifier:store; 29, None; 30, comparison_operator:is; 30, 31; 30, 32; 31, identifier:impersonator; 32, None; 33, block; 33, 34; 34, raise_statement; 34, 35; 35, call; 35, 36; 35, 37; 36, identifier:ValueError; 37, argument_list; 37, 38; 38, concatenated_string; 38, 39; 38, 40; 39, string:'You cannot use both the `impersonator` and '; 40, string:'`store` arguments at the same time'; 41, if_statement; 41, 42; 41, 45; 41, 81; 41, 113; 42, comparison_operator:is; 42, 43; 42, 44; 43, identifier:store; 44, None; 45, block; 45, 46; 45, 59; 45, 66; 46, if_statement; 46, 47; 46, 50; 47, comparison_operator:is; 47, 48; 47, 49; 48, identifier:rcred_cred_store; 49, None; 50, block; 50, 51; 51, raise_statement; 51, 52; 52, call; 52, 53; 52, 54; 53, identifier:NotImplementedError; 54, argument_list; 54, 55; 55, concatenated_string; 55, 56; 55, 57; 55, 58; 56, string:"Your GSSAPI implementation does "; 57, string:"not have support for manipulating "; 58, string:"credential stores"; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:store; 62, call; 62, 63; 62, 64; 63, identifier:_encode_dict; 64, argument_list; 64, 65; 65, identifier:store; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:res; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:rcred_cred_store; 72, identifier:add_cred_from; 73, argument_list; 73, 74; 73, 75; 73, 76; 73, 77; 73, 78; 73, 79; 73, 80; 74, identifier:store; 75, identifier:self; 76, identifier:name; 77, identifier:mech; 78, identifier:usage; 79, identifier:init_lifetime; 80, identifier:accept_lifetime; 81, elif_clause; 81, 82; 81, 85; 82, comparison_operator:is; 82, 83; 82, 84; 83, identifier:impersonator; 84, None; 85, block; 85, 86; 85, 98; 86, if_statement; 86, 87; 86, 90; 87, comparison_operator:is; 87, 88; 87, 89; 88, identifier:rcred_s4u; 89, None; 90, block; 90, 91; 91, raise_statement; 91, 92; 92, call; 92, 93; 92, 94; 93, identifier:NotImplementedError; 94, argument_list; 94, 95; 95, concatenated_string; 95, 96; 95, 97; 96, string:"Your GSSAPI implementation does "; 97, string:"not have support for S4U"; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:res; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:rcred_s4u; 104, identifier:add_cred_impersonate_name; 105, argument_list; 105, 106; 105, 107; 105, 108; 105, 109; 105, 110; 105, 111; 105, 112; 106, identifier:self; 107, identifier:impersonator; 108, identifier:name; 109, identifier:mech; 110, identifier:usage; 111, identifier:init_lifetime; 112, identifier:accept_lifetime; 113, else_clause; 113, 114; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:res; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:rcreds; 121, identifier:add_cred; 122, argument_list; 122, 123; 122, 124; 122, 125; 122, 126; 122, 127; 122, 128; 123, identifier:self; 124, identifier:name; 125, identifier:mech; 126, identifier:usage; 127, identifier:init_lifetime; 128, identifier:accept_lifetime; 129, return_statement; 129, 130; 130, call; 130, 131; 130, 132; 131, identifier:Credentials; 132, argument_list; 132, 133; 133, attribute; 133, 134; 133, 135; 134, identifier:res; 135, identifier:creds
def add(self, name, mech, usage='both', init_lifetime=None, accept_lifetime=None, impersonator=None, store=None): """Acquire more credentials to add to the current set This method works like :meth:`acquire`, except that it adds the acquired credentials for a single mechanism to a copy of the current set, instead of creating a new set for multiple mechanisms. Unlike :meth:`acquire`, you cannot pass None desired name or mechanism. If the `impersonator` argument is used, the credentials will impersonate the given name using the impersonator credentials (:requires-ext:`s4u`). If the `store` argument is used, the credentials will be acquired from the given credential store (:requires-ext:`cred_store`). Otherwise, the credentials are acquired from the default store. The credential store information is a dictionary containing mechanisms-specific keys and values pointing to a credential store or stores. Note that the `store` argument is not compatible with the `impersonator` argument. Args: name (Name): the name associated with the credentials mech (OID): the desired :class:`MechType` to be used with the credentials usage (str): the usage for the credentials -- either 'both', 'initiate', or 'accept' init_lifetime (int): the desired initiate lifetime of the credentials, or None for indefinite accept_lifetime (int): the desired accept lifetime of the credentials, or None for indefinite impersonator (Credentials): the credentials to use to impersonate the given name, or None to not acquire normally (:requires-ext:`s4u`) store (dict): the credential store information pointing to the credential store from which to acquire the credentials, or None for the default store (:requires-ext:`cred_store`) Returns: Credentials: the credentials set containing the current credentials and the newly acquired ones. Raises: BadMechanismError BadNameTypeError BadNameError DuplicateCredentialsElementError ExpiredCredentialsError MissingCredentialsError """ if store is not None and impersonator is not None: raise ValueError('You cannot use both the `impersonator` and ' '`store` arguments at the same time') if store is not None: if rcred_cred_store is None: raise NotImplementedError("Your GSSAPI implementation does " "not have support for manipulating " "credential stores") store = _encode_dict(store) res = rcred_cred_store.add_cred_from(store, self, name, mech, usage, init_lifetime, accept_lifetime) elif impersonator is not None: if rcred_s4u is None: raise NotImplementedError("Your GSSAPI implementation does " "not have support for S4U") res = rcred_s4u.add_cred_impersonate_name(self, impersonator, name, mech, usage, init_lifetime, accept_lifetime) else: res = rcreds.add_cred(self, name, mech, usage, init_lifetime, accept_lifetime) return Credentials(res.creds)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 107; 2, function_name:velocity_graph; 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; 3, 41; 3, 44; 3, 47; 3, 50; 3, 53; 3, 56; 3, 59; 3, 62; 3, 65; 3, 68; 3, 71; 3, 74; 3, 77; 3, 80; 3, 83; 3, 86; 3, 89; 3, 92; 3, 95; 3, 98; 3, 101; 3, 104; 4, identifier:adata; 5, default_parameter; 5, 6; 5, 7; 6, identifier:basis; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:vkey; 10, string:'velocity'; 11, default_parameter; 11, 12; 11, 13; 12, identifier:which_graph; 13, string:'velocity'; 14, default_parameter; 14, 15; 14, 16; 15, identifier:n_neighbors; 16, integer:10; 17, default_parameter; 17, 18; 17, 19; 18, identifier:alpha; 19, float:.8; 20, default_parameter; 20, 21; 20, 22; 21, identifier:perc; 22, integer:90; 23, default_parameter; 23, 24; 23, 25; 24, identifier:edge_width; 25, float:.2; 26, default_parameter; 26, 27; 26, 28; 27, identifier:edge_color; 28, string:'grey'; 29, default_parameter; 29, 30; 29, 31; 30, identifier:color; 31, None; 32, default_parameter; 32, 33; 32, 34; 33, identifier:use_raw; 34, None; 35, default_parameter; 35, 36; 35, 37; 36, identifier:layer; 37, None; 38, default_parameter; 38, 39; 38, 40; 39, identifier:color_map; 40, None; 41, default_parameter; 41, 42; 41, 43; 42, identifier:colorbar; 43, True; 44, default_parameter; 44, 45; 44, 46; 45, identifier:palette; 46, None; 47, default_parameter; 47, 48; 47, 49; 48, identifier:size; 49, None; 50, default_parameter; 50, 51; 50, 52; 51, identifier:sort_order; 52, True; 53, default_parameter; 53, 54; 53, 55; 54, identifier:groups; 55, None; 56, default_parameter; 56, 57; 56, 58; 57, identifier:components; 58, None; 59, default_parameter; 59, 60; 59, 61; 60, identifier:projection; 61, string:'2d'; 62, default_parameter; 62, 63; 62, 64; 63, identifier:legend_loc; 64, string:'on data'; 65, default_parameter; 65, 66; 65, 67; 66, identifier:legend_fontsize; 67, None; 68, default_parameter; 68, 69; 68, 70; 69, identifier:legend_fontweight; 70, None; 71, default_parameter; 71, 72; 71, 73; 72, identifier:right_margin; 73, None; 74, default_parameter; 74, 75; 74, 76; 75, identifier:left_margin; 76, None; 77, default_parameter; 77, 78; 77, 79; 78, identifier:xlabel; 79, None; 80, default_parameter; 80, 81; 80, 82; 81, identifier:ylabel; 82, None; 83, default_parameter; 83, 84; 83, 85; 84, identifier:title; 85, None; 86, default_parameter; 86, 87; 86, 88; 87, identifier:fontsize; 88, None; 89, default_parameter; 89, 90; 89, 91; 90, identifier:figsize; 91, None; 92, default_parameter; 92, 93; 92, 94; 93, identifier:dpi; 94, None; 95, default_parameter; 95, 96; 95, 97; 96, identifier:frameon; 97, None; 98, default_parameter; 98, 99; 98, 100; 99, identifier:show; 100, True; 101, default_parameter; 101, 102; 101, 103; 102, identifier:save; 103, None; 104, default_parameter; 104, 105; 104, 106; 105, identifier:ax; 106, None; 107, block; 107, 108; 107, 110; 107, 122; 107, 133; 107, 212; 107, 236; 107, 243; 107, 314; 107, 373; 107, 392; 108, expression_statement; 108, 109; 109, comment; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:basis; 113, conditional_expression:if; 113, 114; 113, 118; 113, 121; 114, call; 114, 115; 114, 116; 115, identifier:default_basis; 116, argument_list; 116, 117; 117, identifier:adata; 118, comparison_operator:is; 118, 119; 118, 120; 119, identifier:basis; 120, None; 121, identifier:basis; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:title; 125, conditional_expression:if; 125, 126; 125, 129; 125, 132; 126, binary_operator:+; 126, 127; 126, 128; 127, identifier:which_graph; 128, string:' graph'; 129, comparison_operator:is; 129, 130; 129, 131; 130, identifier:title; 131, None; 132, identifier:title; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:scatter_kwargs; 136, dictionary; 136, 137; 136, 140; 136, 143; 136, 146; 136, 149; 136, 152; 136, 155; 136, 158; 136, 161; 136, 164; 136, 167; 136, 170; 136, 173; 136, 176; 136, 179; 136, 182; 136, 185; 136, 188; 136, 191; 136, 194; 136, 197; 136, 200; 136, 203; 136, 206; 136, 209; 137, pair; 137, 138; 137, 139; 138, string:"basis"; 139, identifier:basis; 140, pair; 140, 141; 140, 142; 141, string:"perc"; 142, identifier:perc; 143, pair; 143, 144; 143, 145; 144, string:"use_raw"; 145, identifier:use_raw; 146, pair; 146, 147; 146, 148; 147, string:"sort_order"; 148, identifier:sort_order; 149, pair; 149, 150; 149, 151; 150, string:"alpha"; 151, identifier:alpha; 152, pair; 152, 153; 152, 154; 153, string:"components"; 154, identifier:components; 155, pair; 155, 156; 155, 157; 156, string:"projection"; 157, identifier:projection; 158, pair; 158, 159; 158, 160; 159, string:"legend_loc"; 160, identifier:legend_loc; 161, pair; 161, 162; 161, 163; 162, string:"groups"; 163, identifier:groups; 164, pair; 164, 165; 164, 166; 165, string:"legend_fontsize"; 166, identifier:legend_fontsize; 167, pair; 167, 168; 167, 169; 168, string:"legend_fontweight"; 169, identifier:legend_fontweight; 170, pair; 170, 171; 170, 172; 171, string:"palette"; 172, identifier:palette; 173, pair; 173, 174; 173, 175; 174, string:"color_map"; 175, identifier:color_map; 176, pair; 176, 177; 176, 178; 177, string:"frameon"; 178, identifier:frameon; 179, pair; 179, 180; 179, 181; 180, string:"title"; 181, identifier:title; 182, pair; 182, 183; 182, 184; 183, string:"xlabel"; 184, identifier:xlabel; 185, pair; 185, 186; 185, 187; 186, string:"ylabel"; 187, identifier:ylabel; 188, pair; 188, 189; 188, 190; 189, string:"right_margin"; 190, identifier:right_margin; 191, pair; 191, 192; 191, 193; 192, string:"left_margin"; 193, identifier:left_margin; 194, pair; 194, 195; 194, 196; 195, string:"colorbar"; 196, identifier:colorbar; 197, pair; 197, 198; 197, 199; 198, string:"dpi"; 199, identifier:dpi; 200, pair; 200, 201; 200, 202; 201, string:"fontsize"; 202, identifier:fontsize; 203, pair; 203, 204; 203, 205; 204, string:"show"; 205, False; 206, pair; 206, 207; 206, 208; 207, string:"save"; 208, None; 209, pair; 209, 210; 209, 211; 210, string:"figsize"; 211, identifier:figsize; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:ax; 215, call; 215, 216; 215, 217; 216, identifier:scatter; 217, argument_list; 217, 218; 217, 219; 217, 222; 217, 225; 217, 228; 217, 231; 217, 234; 218, identifier:adata; 219, keyword_argument; 219, 220; 219, 221; 220, identifier:layer; 221, identifier:layer; 222, keyword_argument; 222, 223; 222, 224; 223, identifier:color; 224, identifier:color; 225, keyword_argument; 225, 226; 225, 227; 226, identifier:size; 227, identifier:size; 228, keyword_argument; 228, 229; 228, 230; 229, identifier:ax; 230, identifier:ax; 231, keyword_argument; 231, 232; 231, 233; 232, identifier:zorder; 233, integer:0; 234, dictionary_splat; 234, 235; 235, identifier:scatter_kwargs; 236, import_from_statement; 236, 237; 236, 239; 236, 241; 237, dotted_name; 237, 238; 238, identifier:networkx; 239, dotted_name; 239, 240; 240, identifier:Graph; 241, dotted_name; 241, 242; 242, identifier:draw_networkx_edges; 243, if_statement; 243, 244; 243, 247; 243, 293; 244, comparison_operator:==; 244, 245; 244, 246; 245, identifier:which_graph; 246, string:'neighbors'; 247, block; 247, 248; 247, 258; 248, expression_statement; 248, 249; 249, assignment; 249, 250; 249, 251; 250, identifier:T; 251, subscript; 251, 252; 251, 257; 252, subscript; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:adata; 255, identifier:uns; 256, string:'neighbors'; 257, string:'connectivities'; 258, if_statement; 258, 259; 258, 262; 259, comparison_operator:is; 259, 260; 259, 261; 260, identifier:perc; 261, None; 262, block; 262, 263; 262, 275; 262, 287; 263, expression_statement; 263, 264; 264, assignment; 264, 265; 264, 266; 265, identifier:threshold; 266, call; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:np; 269, identifier:percentile; 270, argument_list; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:T; 273, identifier:data; 274, identifier:perc; 275, expression_statement; 275, 276; 276, assignment; 276, 277; 276, 286; 277, subscript; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:T; 280, identifier:data; 281, comparison_operator:<; 281, 282; 281, 285; 282, attribute; 282, 283; 282, 284; 283, identifier:T; 284, identifier:data; 285, identifier:threshold; 286, integer:0; 287, expression_statement; 287, 288; 288, call; 288, 289; 288, 292; 289, attribute; 289, 290; 289, 291; 290, identifier:T; 291, identifier:eliminate_zeros; 292, argument_list; 293, else_clause; 293, 294; 294, block; 294, 295; 295, expression_statement; 295, 296; 296, assignment; 296, 297; 296, 298; 297, identifier:T; 298, call; 298, 299; 298, 300; 299, identifier:transition_matrix; 300, argument_list; 300, 301; 300, 302; 300, 305; 300, 308; 300, 311; 301, identifier:adata; 302, keyword_argument; 302, 303; 302, 304; 303, identifier:vkey; 304, identifier:vkey; 305, keyword_argument; 305, 306; 305, 307; 306, identifier:weight_indirect_neighbors; 307, integer:0; 308, keyword_argument; 308, 309; 308, 310; 309, identifier:n_neighbors; 310, identifier:n_neighbors; 311, keyword_argument; 311, 312; 311, 313; 312, identifier:perc; 313, identifier:perc; 314, with_statement; 314, 315; 314, 322; 315, with_clause; 315, 316; 316, with_item; 316, 317; 317, call; 317, 318; 317, 321; 318, attribute; 318, 319; 318, 320; 319, identifier:warnings; 320, identifier:catch_warnings; 321, argument_list; 322, block; 322, 323; 322, 330; 322, 356; 322, 364; 323, expression_statement; 323, 324; 324, call; 324, 325; 324, 328; 325, attribute; 325, 326; 325, 327; 326, identifier:warnings; 327, identifier:simplefilter; 328, argument_list; 328, 329; 329, string:"ignore"; 330, expression_statement; 330, 331; 331, assignment; 331, 332; 331, 333; 332, identifier:edges; 333, call; 333, 334; 333, 335; 334, identifier:draw_networkx_edges; 335, argument_list; 335, 336; 335, 340; 335, 347; 335, 350; 335, 353; 336, call; 336, 337; 336, 338; 337, identifier:Graph; 338, argument_list; 338, 339; 339, identifier:T; 340, subscript; 340, 341; 340, 344; 341, attribute; 341, 342; 341, 343; 342, identifier:adata; 343, identifier:obsm; 344, binary_operator:+; 344, 345; 344, 346; 345, string:'X_'; 346, identifier:basis; 347, keyword_argument; 347, 348; 347, 349; 348, identifier:width; 349, identifier:edge_width; 350, keyword_argument; 350, 351; 350, 352; 351, identifier:edge_color; 352, identifier:edge_color; 353, keyword_argument; 353, 354; 353, 355; 354, identifier:ax; 355, identifier:ax; 356, expression_statement; 356, 357; 357, call; 357, 358; 357, 361; 358, attribute; 358, 359; 358, 360; 359, identifier:edges; 360, identifier:set_zorder; 361, argument_list; 361, 362; 362, unary_operator:-; 362, 363; 363, integer:2; 364, expression_statement; 364, 365; 365, call; 365, 366; 365, 369; 366, attribute; 366, 367; 366, 368; 367, identifier:edges; 368, identifier:set_rasterized; 369, argument_list; 369, 370; 370, attribute; 370, 371; 370, 372; 371, identifier:settings; 372, identifier:_vector_friendly; 373, expression_statement; 373, 374; 374, call; 374, 375; 374, 376; 375, identifier:savefig_or_show; 376, argument_list; 376, 377; 376, 383; 376, 386; 376, 389; 377, conditional_expression:if; 377, 378; 377, 379; 377, 382; 378, string:''; 379, comparison_operator:is; 379, 380; 379, 381; 380, identifier:basis; 381, None; 382, identifier:basis; 383, keyword_argument; 383, 384; 383, 385; 384, identifier:dpi; 385, identifier:dpi; 386, keyword_argument; 386, 387; 386, 388; 387, identifier:save; 388, identifier:save; 389, keyword_argument; 389, 390; 389, 391; 390, identifier:show; 391, identifier:show; 392, if_statement; 392, 393; 392, 395; 393, not_operator; 393, 394; 394, identifier:show; 395, block; 395, 396; 396, return_statement; 396, 397; 397, identifier:ax
def velocity_graph(adata, basis=None, vkey='velocity', which_graph='velocity', n_neighbors=10, alpha=.8, perc=90, edge_width=.2, edge_color='grey', color=None, use_raw=None, layer=None, color_map=None, colorbar=True, palette=None, size=None, sort_order=True, groups=None, components=None, projection='2d', legend_loc='on data', legend_fontsize=None, legend_fontweight=None, right_margin=None, left_margin=None, xlabel=None, ylabel=None, title=None, fontsize=None, figsize=None, dpi=None, frameon=None, show=True, save=None, ax=None): """\ Plot of the velocity graph. Arguments --------- adata: :class:`~anndata.AnnData` Annotated data matrix. vkey: `str` or `None` (default: `None`) Key for annotations of observations/cells or variables/genes. which_graph: `'velocity'` or `'neighbors'` (default: `'velocity'`) Whether to show transitions from velocity graph or connectivities from neighbors graph. {scatter} Returns ------- `matplotlib.Axis` if `show==False` """ basis = default_basis(adata) if basis is None else basis title = which_graph + ' graph' if title is None else title scatter_kwargs = {"basis": basis, "perc": perc, "use_raw": use_raw, "sort_order": sort_order, "alpha": alpha, "components": components, "projection": projection, "legend_loc": legend_loc, "groups": groups, "legend_fontsize": legend_fontsize, "legend_fontweight": legend_fontweight, "palette": palette, "color_map": color_map, "frameon": frameon, "title": title, "xlabel": xlabel, "ylabel": ylabel, "right_margin": right_margin, "left_margin": left_margin, "colorbar": colorbar, "dpi": dpi, "fontsize": fontsize, "show": False, "save": None, "figsize": figsize, } ax = scatter(adata, layer=layer, color=color, size=size, ax=ax, zorder=0, **scatter_kwargs) from networkx import Graph, draw_networkx_edges if which_graph == 'neighbors': T = adata.uns['neighbors']['connectivities'] if perc is not None: threshold = np.percentile(T.data, perc) T.data[T.data < threshold] = 0 T.eliminate_zeros() else: T = transition_matrix(adata, vkey=vkey, weight_indirect_neighbors=0, n_neighbors=n_neighbors, perc=perc) with warnings.catch_warnings(): warnings.simplefilter("ignore") edges = draw_networkx_edges(Graph(T), adata.obsm['X_' + basis], width=edge_width, edge_color=edge_color, ax=ax) edges.set_zorder(-2) edges.set_rasterized(settings._vector_friendly) savefig_or_show('' if basis is None else basis, dpi=dpi, save=save, show=show) if not show: return ax
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:cleanup; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:data; 5, default_parameter; 5, 6; 5, 7; 6, identifier:clean; 7, string:'layers'; 8, default_parameter; 8, 9; 8, 10; 9, identifier:keep; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:copy; 13, False; 14, block; 14, 15; 14, 17; 14, 28; 14, 53; 14, 66; 14, 103; 14, 128; 14, 156; 15, expression_statement; 15, 16; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:adata; 20, conditional_expression:if; 20, 21; 20, 26; 20, 27; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:data; 24, identifier:copy; 25, argument_list; 26, identifier:copy; 27, identifier:data; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:keep; 31, conditional_expression:if; 31, 32; 31, 37; 31, 42; 32, call; 32, 33; 32, 34; 33, identifier:list; 34, argument_list; 34, 35; 35, list:[keep]; 35, 36; 36, identifier:keep; 37, call; 37, 38; 37, 39; 38, identifier:isinstance; 39, argument_list; 39, 40; 39, 41; 40, identifier:keep; 41, identifier:str; 42, conditional_expression:if; 42, 43; 42, 46; 42, 49; 43, call; 43, 44; 43, 45; 44, identifier:list; 45, argument_list; 46, comparison_operator:is; 46, 47; 46, 48; 47, identifier:keep; 48, None; 49, call; 49, 50; 49, 51; 50, identifier:list; 51, argument_list; 51, 52; 52, identifier:keep; 53, expression_statement; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:keep; 57, identifier:extend; 58, argument_list; 58, 59; 59, list:['spliced', 'unspliced', 'Ms', 'Mu', 'clusters', 'neighbors']; 59, 60; 59, 61; 59, 62; 59, 63; 59, 64; 59, 65; 60, string:'spliced'; 61, string:'unspliced'; 62, string:'Ms'; 63, string:'Mu'; 64, string:'clusters'; 65, string:'neighbors'; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:ann_dict; 69, dictionary; 69, 70; 69, 77; 69, 84; 69, 91; 70, pair; 70, 71; 70, 72; 71, string:'obs'; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:adata; 75, identifier:obs_keys; 76, argument_list; 77, pair; 77, 78; 77, 79; 78, string:'var'; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:adata; 82, identifier:var_keys; 83, argument_list; 84, pair; 84, 85; 84, 86; 85, string:'uns'; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:adata; 89, identifier:uns_keys; 90, argument_list; 91, pair; 91, 92; 91, 93; 92, string:'layers'; 93, call; 93, 94; 93, 95; 94, identifier:list; 95, argument_list; 95, 96; 96, call; 96, 97; 96, 102; 97, attribute; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:adata; 100, identifier:layers; 101, identifier:keys; 102, argument_list; 103, if_statement; 103, 104; 103, 107; 104, comparison_operator:not; 104, 105; 104, 106; 105, string:'all'; 106, identifier:clean; 107, block; 107, 108; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:ann_dict; 111, dictionary_comprehension; 111, 112; 111, 115; 111, 124; 112, pair; 112, 113; 112, 114; 113, identifier:ann; 114, identifier:values; 115, for_in_clause; 115, 116; 115, 119; 116, tuple_pattern; 116, 117; 116, 118; 117, identifier:ann; 118, identifier:values; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:ann_dict; 122, identifier:items; 123, argument_list; 124, if_clause; 124, 125; 125, comparison_operator:in; 125, 126; 125, 127; 126, identifier:ann; 127, identifier:clean; 128, for_statement; 128, 129; 128, 132; 128, 137; 129, tuple_pattern; 129, 130; 129, 131; 130, identifier:ann; 131, identifier:values; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:ann_dict; 135, identifier:items; 136, argument_list; 137, block; 137, 138; 138, for_statement; 138, 139; 138, 140; 138, 141; 139, identifier:value; 140, identifier:values; 141, block; 141, 142; 142, if_statement; 142, 143; 142, 146; 143, comparison_operator:not; 143, 144; 143, 145; 144, identifier:value; 145, identifier:keep; 146, block; 146, 147; 147, delete_statement; 147, 148; 148, parenthesized_expression; 148, 149; 149, subscript; 149, 150; 149, 155; 150, call; 150, 151; 150, 152; 151, identifier:getattr; 152, argument_list; 152, 153; 152, 154; 153, identifier:adata; 154, identifier:ann; 155, identifier:value; 156, return_statement; 156, 157; 157, conditional_expression:if; 157, 158; 157, 159; 157, 160; 158, identifier:adata; 159, identifier:copy; 160, None
def cleanup(data, clean='layers', keep=None, copy=False): """Deletes attributes not needed. Arguments --------- data: :class:`~anndata.AnnData` Annotated data matrix. clean: `str` or list of `str` (default: `layers`) Which attributes to consider for freeing memory. keep: `str` or list of `str` (default: None) Which attributes to keep. copy: `bool` (default: `False`) Return a copy instead of writing to adata. Returns ------- Returns or updates `adata` with selection of attributes kept. """ adata = data.copy() if copy else data keep = list([keep]) if isinstance(keep, str) else list() if keep is None else list(keep) keep.extend(['spliced', 'unspliced', 'Ms', 'Mu', 'clusters', 'neighbors']) ann_dict = {'obs': adata.obs_keys(), 'var': adata.var_keys(), 'uns': adata.uns_keys(), 'layers': list(adata.layers.keys())} if 'all' not in clean: ann_dict = {ann: values for (ann, values) in ann_dict.items() if ann in clean} for (ann, values) in ann_dict.items(): for value in values: if value not in keep: del(getattr(adata, ann)[value]) return adata if copy else None
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 38; 2, function_name:filter_genes; 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; 4, identifier:data; 5, default_parameter; 5, 6; 5, 7; 6, identifier:min_counts; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:min_cells; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:max_counts; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:max_cells; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:min_counts_u; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:min_cells_u; 22, None; 23, default_parameter; 23, 24; 23, 25; 24, identifier:max_counts_u; 25, None; 26, default_parameter; 26, 27; 26, 28; 27, identifier:max_cells_u; 28, None; 29, default_parameter; 29, 30; 29, 31; 30, identifier:min_shared_counts; 31, None; 32, default_parameter; 32, 33; 32, 34; 33, identifier:min_shared_cells; 34, None; 35, default_parameter; 35, 36; 35, 37; 36, identifier:copy; 37, False; 38, block; 38, 39; 38, 41; 38, 52; 38, 53; 38, 58; 38, 78; 38, 95; 38, 445; 39, expression_statement; 39, 40; 40, comment; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:adata; 44, conditional_expression:if; 44, 45; 44, 50; 44, 51; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:data; 48, identifier:copy; 49, argument_list; 50, identifier:copy; 51, identifier:data; 52, comment; 53, expression_statement; 53, 54; 54, call; 54, 55; 54, 56; 55, identifier:set_initial_size; 56, argument_list; 56, 57; 57, identifier:adata; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:layers; 61, list_comprehension; 61, 62; 61, 63; 61, 68; 62, identifier:layer; 63, for_in_clause; 63, 64; 63, 65; 64, identifier:layer; 65, list:['spliced', 'unspliced']; 65, 66; 65, 67; 66, string:'spliced'; 67, string:'unspliced'; 68, if_clause; 68, 69; 69, comparison_operator:in; 69, 70; 69, 71; 70, identifier:layer; 71, call; 71, 72; 71, 77; 72, attribute; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:adata; 75, identifier:layers; 76, identifier:keys; 77, argument_list; 78, if_statement; 78, 79; 78, 86; 79, boolean_operator:or; 79, 80; 79, 83; 80, comparison_operator:is; 80, 81; 80, 82; 81, identifier:min_shared_counts; 82, None; 83, comparison_operator:is; 83, 84; 83, 85; 84, identifier:min_shared_cells; 85, None; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:layers; 91, identifier:extend; 92, argument_list; 92, 93; 93, list:['shared']; 93, 94; 94, string:'shared'; 95, for_statement; 95, 96; 95, 97; 95, 98; 96, identifier:layer; 97, identifier:layers; 98, block; 98, 99; 98, 148; 98, 241; 98, 255; 98, 286; 98, 317; 98, 327; 99, if_statement; 99, 100; 99, 103; 99, 116; 99, 133; 100, comparison_operator:is; 100, 101; 100, 102; 101, identifier:layer; 102, string:'spliced'; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 111; 106, pattern_list; 106, 107; 106, 108; 106, 109; 106, 110; 107, identifier:_min_counts; 108, identifier:_min_cells; 109, identifier:_max_counts; 110, identifier:_max_cells; 111, expression_list; 111, 112; 111, 113; 111, 114; 111, 115; 112, identifier:min_counts; 113, identifier:min_cells; 114, identifier:max_counts; 115, identifier:max_cells; 116, elif_clause; 116, 117; 116, 120; 117, comparison_operator:is; 117, 118; 117, 119; 118, identifier:layer; 119, string:'unspliced'; 120, block; 120, 121; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 128; 123, pattern_list; 123, 124; 123, 125; 123, 126; 123, 127; 124, identifier:_min_counts; 125, identifier:_min_cells; 126, identifier:_max_counts; 127, identifier:_max_cells; 128, expression_list; 128, 129; 128, 130; 128, 131; 128, 132; 129, identifier:min_counts_u; 130, identifier:min_cells_u; 131, identifier:max_counts_u; 132, identifier:max_cells_u; 133, else_clause; 133, 134; 133, 135; 134, comment; 135, block; 135, 136; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 143; 138, pattern_list; 138, 139; 138, 140; 138, 141; 138, 142; 139, identifier:_min_counts; 140, identifier:_min_cells; 141, identifier:_max_counts; 142, identifier:_max_cells; 143, expression_list; 143, 144; 143, 145; 143, 146; 143, 147; 144, identifier:min_shared_counts; 145, identifier:min_shared_cells; 146, None; 147, None; 148, if_statement; 148, 149; 148, 158; 148, 167; 149, comparison_operator:in; 149, 150; 149, 151; 150, identifier:layer; 151, call; 151, 152; 151, 157; 152, attribute; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:adata; 155, identifier:layers; 156, identifier:keys; 157, argument_list; 158, block; 158, 159; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:X; 162, subscript; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:adata; 165, identifier:layers; 166, identifier:layer; 167, else_clause; 167, 168; 167, 169; 168, comment; 169, block; 169, 170; 169, 186; 169, 214; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 175; 172, pattern_list; 172, 173; 172, 174; 173, identifier:Xs; 174, identifier:Xu; 175, expression_list; 175, 176; 175, 181; 176, subscript; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:adata; 179, identifier:layers; 180, string:'spliced'; 181, subscript; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:adata; 184, identifier:layers; 185, string:'unspliced'; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:nonzeros; 189, conditional_expression:if; 189, 190; 189, 201; 189, 205; 190, call; 190, 191; 190, 197; 191, attribute; 191, 192; 191, 196; 192, parenthesized_expression; 192, 193; 193, comparison_operator:>; 193, 194; 193, 195; 194, identifier:Xs; 195, integer:0; 196, identifier:multiply; 197, argument_list; 197, 198; 198, comparison_operator:>; 198, 199; 198, 200; 199, identifier:Xu; 200, integer:0; 201, call; 201, 202; 201, 203; 202, identifier:issparse; 203, argument_list; 203, 204; 204, identifier:Xs; 205, binary_operator:*; 205, 206; 205, 210; 206, parenthesized_expression; 206, 207; 207, comparison_operator:>; 207, 208; 207, 209; 208, identifier:Xs; 209, integer:0; 210, parenthesized_expression; 210, 211; 211, comparison_operator:>; 211, 212; 211, 213; 212, identifier:Xu; 213, integer:0; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:X; 217, conditional_expression:if; 217, 218; 217, 231; 217, 235; 218, binary_operator:+; 218, 219; 218, 225; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:nonzeros; 222, identifier:multiply; 223, argument_list; 223, 224; 224, identifier:Xs; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:nonzeros; 228, identifier:multiply; 229, argument_list; 229, 230; 230, identifier:Xu; 231, call; 231, 232; 231, 233; 232, identifier:issparse; 233, argument_list; 233, 234; 234, identifier:nonzeros; 235, binary_operator:*; 235, 236; 235, 237; 236, identifier:nonzeros; 237, parenthesized_expression; 237, 238; 238, binary_operator:+; 238, 239; 238, 240; 239, identifier:Xs; 240, identifier:Xu; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 244; 243, identifier:gene_subset; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:np; 247, identifier:ones; 248, argument_list; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:adata; 251, identifier:n_vars; 252, keyword_argument; 252, 253; 252, 254; 253, identifier:dtype; 254, identifier:bool; 255, if_statement; 255, 256; 255, 263; 256, boolean_operator:or; 256, 257; 256, 260; 257, comparison_operator:is; 257, 258; 257, 259; 258, identifier:_min_counts; 259, None; 260, comparison_operator:is; 260, 261; 260, 262; 261, identifier:_max_counts; 262, None; 263, block; 263, 264; 263, 279; 264, expression_statement; 264, 265; 265, assignment; 265, 266; 265, 269; 266, pattern_list; 266, 267; 266, 268; 267, identifier:gene_subset; 268, identifier:_; 269, call; 269, 270; 269, 271; 270, identifier:filter; 271, argument_list; 271, 272; 271, 273; 271, 276; 272, identifier:X; 273, keyword_argument; 273, 274; 273, 275; 274, identifier:min_counts; 275, identifier:_min_counts; 276, keyword_argument; 276, 277; 276, 278; 277, identifier:max_counts; 278, identifier:_max_counts; 279, expression_statement; 279, 280; 280, call; 280, 281; 280, 284; 281, attribute; 281, 282; 281, 283; 282, identifier:adata; 283, identifier:_inplace_subset_var; 284, argument_list; 284, 285; 285, identifier:gene_subset; 286, if_statement; 286, 287; 286, 294; 287, boolean_operator:or; 287, 288; 287, 291; 288, comparison_operator:is; 288, 289; 288, 290; 289, identifier:_min_cells; 290, None; 291, comparison_operator:is; 291, 292; 291, 293; 292, identifier:_max_cells; 293, None; 294, block; 294, 295; 294, 310; 295, expression_statement; 295, 296; 296, assignment; 296, 297; 296, 300; 297, pattern_list; 297, 298; 297, 299; 298, identifier:gene_subset; 299, identifier:_; 300, call; 300, 301; 300, 302; 301, identifier:filter; 302, argument_list; 302, 303; 302, 304; 302, 307; 303, identifier:X; 304, keyword_argument; 304, 305; 304, 306; 305, identifier:min_cells; 306, identifier:_min_cells; 307, keyword_argument; 307, 308; 307, 309; 308, identifier:max_cells; 309, identifier:_max_cells; 310, expression_statement; 310, 311; 311, call; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:adata; 314, identifier:_inplace_subset_var; 315, argument_list; 315, 316; 316, identifier:gene_subset; 317, expression_statement; 317, 318; 318, assignment; 318, 319; 318, 320; 319, identifier:s; 320, call; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:np; 323, identifier:sum; 324, argument_list; 324, 325; 325, unary_operator:~; 325, 326; 326, identifier:gene_subset; 327, if_statement; 327, 328; 327, 331; 328, comparison_operator:>; 328, 329; 328, 330; 329, identifier:s; 330, integer:0; 331, block; 331, 332; 331, 347; 331, 396; 332, expression_statement; 332, 333; 333, call; 333, 334; 333, 337; 334, attribute; 334, 335; 334, 336; 335, identifier:logg; 336, identifier:info; 337, argument_list; 337, 338; 337, 344; 338, call; 338, 339; 338, 342; 339, attribute; 339, 340; 339, 341; 340, string:'Filtered out {} genes that are detected'; 341, identifier:format; 342, argument_list; 342, 343; 343, identifier:s; 344, keyword_argument; 344, 345; 344, 346; 345, identifier:end; 346, string:' '; 347, if_statement; 347, 348; 347, 355; 348, boolean_operator:or; 348, 349; 348, 352; 349, comparison_operator:is; 349, 350; 349, 351; 350, identifier:_min_cells; 351, None; 352, comparison_operator:is; 352, 353; 352, 354; 353, identifier:_min_counts; 354, None; 355, block; 355, 356; 356, expression_statement; 356, 357; 357, call; 357, 358; 357, 361; 358, attribute; 358, 359; 358, 360; 359, identifier:logg; 360, identifier:info; 361, argument_list; 361, 362; 361, 363; 361, 393; 362, string:'in less than'; 363, conditional_expression:if; 363, 364; 363, 377; 363, 380; 364, binary_operator:+; 364, 365; 364, 376; 365, binary_operator:+; 365, 366; 365, 372; 366, binary_operator:+; 366, 367; 366, 371; 367, call; 367, 368; 367, 369; 368, identifier:str; 369, argument_list; 369, 370; 370, identifier:_min_cells; 371, string:' cells ('; 372, call; 372, 373; 372, 374; 373, identifier:str; 374, argument_list; 374, 375; 375, identifier:layer; 376, string:').'; 377, comparison_operator:is; 377, 378; 377, 379; 378, identifier:_min_counts; 379, None; 380, binary_operator:+; 380, 381; 380, 392; 381, binary_operator:+; 381, 382; 381, 388; 382, binary_operator:+; 382, 383; 382, 387; 383, call; 383, 384; 383, 385; 384, identifier:str; 385, argument_list; 385, 386; 386, identifier:_min_counts; 387, string:' counts ('; 388, call; 388, 389; 388, 390; 389, identifier:str; 390, argument_list; 390, 391; 391, identifier:layer; 392, string:').'; 393, keyword_argument; 393, 394; 393, 395; 394, identifier:no_indent; 395, True; 396, if_statement; 396, 397; 396, 404; 397, boolean_operator:or; 397, 398; 397, 401; 398, comparison_operator:is; 398, 399; 398, 400; 399, identifier:max_cells; 400, None; 401, comparison_operator:is; 401, 402; 401, 403; 402, identifier:max_counts; 403, None; 404, block; 404, 405; 405, expression_statement; 405, 406; 406, call; 406, 407; 406, 410; 407, attribute; 407, 408; 407, 409; 408, identifier:logg; 409, identifier:info; 410, argument_list; 410, 411; 410, 412; 410, 442; 411, string:'in more than '; 412, conditional_expression:if; 412, 413; 412, 426; 412, 429; 413, binary_operator:+; 413, 414; 413, 425; 414, binary_operator:+; 414, 415; 414, 421; 415, binary_operator:+; 415, 416; 415, 420; 416, call; 416, 417; 416, 418; 417, identifier:str; 418, argument_list; 418, 419; 419, identifier:_max_cells; 420, string:' cells('; 421, call; 421, 422; 421, 423; 422, identifier:str; 423, argument_list; 423, 424; 424, identifier:layer; 425, string:').'; 426, comparison_operator:is; 426, 427; 426, 428; 427, identifier:_max_counts; 428, None; 429, binary_operator:+; 429, 430; 429, 441; 430, binary_operator:+; 430, 431; 430, 437; 431, binary_operator:+; 431, 432; 431, 436; 432, call; 432, 433; 432, 434; 433, identifier:str; 434, argument_list; 434, 435; 435, identifier:_max_counts; 436, string:' counts ('; 437, call; 437, 438; 437, 439; 438, identifier:str; 439, argument_list; 439, 440; 440, identifier:layer; 441, string:').'; 442, keyword_argument; 442, 443; 442, 444; 443, identifier:no_indent; 444, True; 445, return_statement; 445, 446; 446, conditional_expression:if; 446, 447; 446, 448; 446, 449; 447, identifier:adata; 448, identifier:copy; 449, None
def filter_genes(data, min_counts=None, min_cells=None, max_counts=None, max_cells=None, min_counts_u=None, min_cells_u=None, max_counts_u=None, max_cells_u=None, min_shared_counts=None, min_shared_cells=None, copy=False): """Filter genes based on number of cells or counts. Keep genes that have at least `min_counts` counts or are expressed in at least `min_cells` cells or have at most `max_counts` counts or are expressed in at most `max_cells` cells. Only provide one of the optional parameters `min_counts`, `min_cells`, `max_counts`, `max_cells` per call. Parameters ---------- data : :class:`~anndata.AnnData`, `np.ndarray`, `sp.spmatrix` The (annotated) data matrix of shape `n_obs` × `n_vars`. Rows correspond to cells and columns to genes. min_counts : `int`, optional (default: `None`) Minimum number of counts required for a gene to pass filtering. min_cells : `int`, optional (default: `None`) Minimum number of cells expressed required for a gene to pass filtering. max_counts : `int`, optional (default: `None`) Maximum number of counts required for a gene to pass filtering. max_cells : `int`, optional (default: `None`) Maximum number of cells expressed required for a gene to pass filtering. min_counts_u : `int`, optional (default: `None`) Minimum number of unspliced counts required for a gene to pass filtering. min_cells_u : `int`, optional (default: `None`) Minimum number of unspliced cells expressed required for a gene to pass filtering. max_counts_u : `int`, optional (default: `None`) Maximum number of unspliced counts required for a gene to pass filtering. max_cells_u : `int`, optional (default: `None`) Maximum number of unspliced cells expressed required for a gene to pass filtering. min_shared_counts: `int`, optional (default: `None`) Minimum number of counts (in cells expressed simultaneously in unspliced and spliced) required for a gene. min_shared_cells: `int`, optional (default: `None`) Minimum number of cells required for a gene to be expressed simultaneously in unspliced and spliced. copy : `bool`, optional (default: `False`) Determines whether a copy is returned. Returns ------- Filters the object and adds `n_counts` to `adata.var`. """ adata = data.copy() if copy else data # set initial cell sizes before filtering set_initial_size(adata) layers = [layer for layer in ['spliced', 'unspliced'] if layer in adata.layers.keys()] if min_shared_counts is not None or min_shared_cells is not None: layers.extend(['shared']) for layer in layers: if layer is 'spliced': _min_counts, _min_cells, _max_counts, _max_cells = min_counts, min_cells, max_counts, max_cells elif layer is 'unspliced': _min_counts, _min_cells, _max_counts, _max_cells = min_counts_u, min_cells_u, max_counts_u, max_cells_u else: # shared counts/cells _min_counts, _min_cells, _max_counts, _max_cells = min_shared_counts, min_shared_cells, None, None if layer in adata.layers.keys(): X = adata.layers[layer] else: # shared counts/cells Xs, Xu = adata.layers['spliced'], adata.layers['unspliced'] nonzeros = (Xs > 0).multiply(Xu > 0) if issparse(Xs) else (Xs > 0) * (Xu > 0) X = nonzeros.multiply(Xs) + nonzeros.multiply(Xu) if issparse(nonzeros) else nonzeros * (Xs + Xu) gene_subset = np.ones(adata.n_vars, dtype=bool) if _min_counts is not None or _max_counts is not None: gene_subset, _ = filter(X, min_counts=_min_counts, max_counts=_max_counts) adata._inplace_subset_var(gene_subset) if _min_cells is not None or _max_cells is not None: gene_subset, _ = filter(X, min_cells=_min_cells, max_cells=_max_cells) adata._inplace_subset_var(gene_subset) s = np.sum(~gene_subset) if s > 0: logg.info('Filtered out {} genes that are detected'.format(s), end=' ') if _min_cells is not None or _min_counts is not None: logg.info('in less than', str(_min_cells) + ' cells (' + str(layer) + ').' if _min_counts is None else str(_min_counts) + ' counts (' + str(layer) + ').', no_indent=True) if max_cells is not None or max_counts is not None: logg.info('in more than ', str(_max_cells) + ' cells(' + str(layer) + ').' if _max_counts is None else str(_max_counts) + ' counts (' + str(layer) + ').', no_indent=True) return adata if copy else None
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 32; 2, function_name:filter_genes_dispersion; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 3, 29; 4, identifier:data; 5, default_parameter; 5, 6; 5, 7; 6, identifier:flavor; 7, string:'seurat'; 8, default_parameter; 8, 9; 8, 10; 9, identifier:min_disp; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:max_disp; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:min_mean; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:max_mean; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:n_bins; 22, integer:20; 23, default_parameter; 23, 24; 23, 25; 24, identifier:n_top_genes; 25, None; 26, default_parameter; 26, 27; 26, 28; 27, identifier:log; 28, True; 29, default_parameter; 29, 30; 29, 31; 30, identifier:copy; 31, False; 32, block; 32, 33; 32, 35; 32, 46; 32, 51; 32, 279; 33, expression_statement; 33, 34; 34, comment; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:adata; 38, conditional_expression:if; 38, 39; 38, 44; 38, 45; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:data; 42, identifier:copy; 43, argument_list; 44, identifier:copy; 45, identifier:data; 46, expression_statement; 46, 47; 47, call; 47, 48; 47, 49; 48, identifier:set_initial_size; 49, argument_list; 49, 50; 50, identifier:adata; 51, if_statement; 51, 52; 51, 61; 51, 69; 52, boolean_operator:and; 52, 53; 52, 56; 53, comparison_operator:is; 53, 54; 53, 55; 54, identifier:n_top_genes; 55, None; 56, comparison_operator:<; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:adata; 59, identifier:n_vars; 60, identifier:n_top_genes; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:logg; 66, identifier:info; 67, argument_list; 67, 68; 68, string:'Skip filtering by dispersion since number of variables are less than `n_top_genes`'; 69, else_clause; 69, 70; 70, block; 70, 71; 71, if_statement; 71, 72; 71, 75; 71, 241; 72, comparison_operator:is; 72, 73; 72, 74; 73, identifier:flavor; 74, string:'svr'; 75, block; 75, 76; 75, 104; 75, 148; 75, 157; 75, 168; 75, 174; 75, 188; 75, 200; 75, 215; 75, 232; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:mu; 79, conditional_expression:if; 79, 80; 79, 90; 79, 96; 80, attribute; 80, 81; 80, 89; 81, call; 81, 82; 81, 87; 82, attribute; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:adata; 85, identifier:X; 86, identifier:mean; 87, argument_list; 87, 88; 88, integer:0; 89, identifier:A1; 90, call; 90, 91; 90, 92; 91, identifier:issparse; 92, argument_list; 92, 93; 93, attribute; 93, 94; 93, 95; 94, identifier:adata; 95, identifier:X; 96, call; 96, 97; 96, 102; 97, attribute; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:adata; 100, identifier:X; 101, identifier:mean; 102, argument_list; 102, 103; 103, integer:0; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:sigma; 107, conditional_expression:if; 107, 108; 107, 134; 107, 140; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:np; 111, identifier:sqrt; 112, argument_list; 112, 113; 113, binary_operator:-; 113, 114; 113, 131; 114, attribute; 114, 115; 114, 130; 115, call; 115, 116; 115, 128; 116, attribute; 116, 117; 116, 127; 117, call; 117, 118; 117, 123; 118, attribute; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:adata; 121, identifier:X; 122, identifier:multiply; 123, argument_list; 123, 124; 124, attribute; 124, 125; 124, 126; 125, identifier:adata; 126, identifier:X; 127, identifier:mean; 128, argument_list; 128, 129; 129, integer:0; 130, identifier:A1; 131, binary_operator:**; 131, 132; 131, 133; 132, identifier:mu; 133, integer:2; 134, call; 134, 135; 134, 136; 135, identifier:issparse; 136, argument_list; 136, 137; 137, attribute; 137, 138; 137, 139; 138, identifier:adata; 139, identifier:X; 140, call; 140, 141; 140, 146; 141, attribute; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:adata; 144, identifier:X; 145, identifier:std; 146, argument_list; 146, 147; 147, integer:0; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:log_mu; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:np; 154, identifier:log2; 155, argument_list; 155, 156; 156, identifier:mu; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:log_cv; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:np; 163, identifier:log2; 164, argument_list; 164, 165; 165, binary_operator:/; 165, 166; 165, 167; 166, identifier:sigma; 167, identifier:mu; 168, import_from_statement; 168, 169; 168, 172; 169, dotted_name; 169, 170; 169, 171; 170, identifier:sklearn; 171, identifier:svm; 172, dotted_name; 172, 173; 173, identifier:SVR; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:clf; 177, call; 177, 178; 177, 179; 178, identifier:SVR; 179, argument_list; 179, 180; 180, keyword_argument; 180, 181; 180, 182; 181, identifier:gamma; 182, binary_operator:/; 182, 183; 182, 184; 183, float:150.; 184, call; 184, 185; 184, 186; 185, identifier:len; 186, argument_list; 186, 187; 187, identifier:mu; 188, expression_statement; 188, 189; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:clf; 192, identifier:fit; 193, argument_list; 193, 194; 193, 199; 194, subscript; 194, 195; 194, 196; 194, 198; 195, identifier:log_mu; 196, slice; 196, 197; 197, colon; 198, None; 199, identifier:log_cv; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 203; 202, identifier:score; 203, binary_operator:-; 203, 204; 203, 205; 204, identifier:log_cv; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:clf; 208, identifier:predict; 209, argument_list; 209, 210; 210, subscript; 210, 211; 210, 212; 210, 214; 211, identifier:log_mu; 212, slice; 212, 213; 213, colon; 214, None; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:nth_score; 218, subscript; 218, 219; 218, 231; 219, subscript; 219, 220; 219, 226; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:np; 223, identifier:sort; 224, argument_list; 224, 225; 225, identifier:score; 226, slice; 226, 227; 226, 228; 226, 229; 227, colon; 228, colon; 229, unary_operator:-; 229, 230; 230, integer:1; 231, identifier:n_top_genes; 232, expression_statement; 232, 233; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:adata; 236, identifier:_inplace_subset_var; 237, argument_list; 237, 238; 238, comparison_operator:>=; 238, 239; 238, 240; 239, identifier:score; 240, identifier:nth_score; 241, else_clause; 241, 242; 242, block; 242, 243; 242, 250; 243, import_from_statement; 243, 244; 243, 248; 244, dotted_name; 244, 245; 244, 246; 244, 247; 245, identifier:scanpy; 246, identifier:api; 247, identifier:pp; 248, dotted_name; 248, 249; 249, identifier:filter_genes_dispersion; 250, expression_statement; 250, 251; 251, call; 251, 252; 251, 253; 252, identifier:filter_genes_dispersion; 253, argument_list; 253, 254; 253, 255; 253, 258; 253, 261; 253, 264; 253, 267; 253, 270; 253, 273; 253, 276; 254, identifier:adata; 255, keyword_argument; 255, 256; 255, 257; 256, identifier:flavor; 257, identifier:flavor; 258, keyword_argument; 258, 259; 258, 260; 259, identifier:min_disp; 260, identifier:min_disp; 261, keyword_argument; 261, 262; 261, 263; 262, identifier:max_disp; 263, identifier:max_disp; 264, keyword_argument; 264, 265; 264, 266; 265, identifier:min_mean; 266, identifier:min_mean; 267, keyword_argument; 267, 268; 267, 269; 268, identifier:max_mean; 269, identifier:max_mean; 270, keyword_argument; 270, 271; 270, 272; 271, identifier:n_bins; 272, identifier:n_bins; 273, keyword_argument; 273, 274; 273, 275; 274, identifier:n_top_genes; 275, identifier:n_top_genes; 276, keyword_argument; 276, 277; 276, 278; 277, identifier:log; 278, identifier:log; 279, return_statement; 279, 280; 280, conditional_expression:if; 280, 281; 280, 282; 280, 283; 281, identifier:adata; 282, identifier:copy; 283, None
def filter_genes_dispersion(data, flavor='seurat', min_disp=None, max_disp=None, min_mean=None, max_mean=None, n_bins=20, n_top_genes=None, log=True, copy=False): """Extract highly variable genes. The normalized dispersion is obtained by scaling with the mean and standard deviation of the dispersions for genes falling into a given bin for mean expression of genes. This means that for each bin of mean expression, highly variable genes are selected. Parameters ---------- data : :class:`~anndata.AnnData`, `np.ndarray`, `sp.sparse` The (annotated) data matrix of shape `n_obs` × `n_vars`. Rows correspond to cells and columns to genes. flavor : {'seurat', 'cell_ranger', 'svr'}, optional (default: 'seurat') Choose the flavor for computing normalized dispersion. If choosing 'seurat', this expects non-logarithmized data - the logarithm of mean and dispersion is taken internally when `log` is at its default value `True`. For 'cell_ranger', this is usually called for logarithmized data - in this case you should set `log` to `False`. In their default workflows, Seurat passes the cutoffs whereas Cell Ranger passes `n_top_genes`. min_mean=0.0125, max_mean=3, min_disp=0.5, max_disp=`None` : `float`, optional If `n_top_genes` unequals `None`, these cutoffs for the means and the normalized dispersions are ignored. n_bins : `int` (default: 20) Number of bins for binning the mean gene expression. Normalization is done with respect to each bin. If just a single gene falls into a bin, the normalized dispersion is artificially set to 1. You'll be informed about this if you set `settings.verbosity = 4`. n_top_genes : `int` or `None` (default: `None`) Number of highly-variable genes to keep. log : `bool`, optional (default: `True`) Use the logarithm of the mean to variance ratio. copy : `bool`, optional (default: `False`) If an :class:`~anndata.AnnData` is passed, determines whether a copy is returned. Returns ------- If an AnnData `adata` is passed, returns or updates `adata` depending on \ `copy`. It filters the `adata` and adds the annotations """ adata = data.copy() if copy else data set_initial_size(adata) if n_top_genes is not None and adata.n_vars < n_top_genes: logg.info('Skip filtering by dispersion since number of variables are less than `n_top_genes`') else: if flavor is 'svr': mu = adata.X.mean(0).A1 if issparse(adata.X) else adata.X.mean(0) sigma = np.sqrt(adata.X.multiply(adata.X).mean(0).A1 - mu ** 2) if issparse(adata.X) else adata.X.std(0) log_mu = np.log2(mu) log_cv = np.log2(sigma / mu) from sklearn.svm import SVR clf = SVR(gamma=150. / len(mu)) clf.fit(log_mu[:, None], log_cv) score = log_cv - clf.predict(log_mu[:, None]) nth_score = np.sort(score)[::-1][n_top_genes] adata._inplace_subset_var(score >= nth_score) else: from scanpy.api.pp import filter_genes_dispersion filter_genes_dispersion(adata, flavor=flavor, min_disp=min_disp, max_disp=max_disp, min_mean=min_mean, max_mean=max_mean, n_bins=n_bins, n_top_genes=n_top_genes, log=log) return adata if copy else None
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 31; 2, function_name:normalize_per_cell; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 25; 3, 28; 4, identifier:data; 5, default_parameter; 5, 6; 5, 7; 6, identifier:counts_per_cell_after; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:counts_per_cell; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:key_n_counts; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:max_proportion_per_cell; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:use_initial_size; 19, True; 20, default_parameter; 20, 21; 20, 22; 21, identifier:layers; 22, list:['spliced', 'unspliced']; 22, 23; 22, 24; 23, string:'spliced'; 24, string:'unspliced'; 25, default_parameter; 25, 26; 25, 27; 26, identifier:enforce; 27, False; 28, default_parameter; 28, 29; 28, 30; 29, identifier:copy; 30, False; 31, block; 31, 32; 31, 34; 31, 45; 31, 83; 31, 90; 31, 94; 31, 231; 31, 247; 31, 270; 32, expression_statement; 32, 33; 33, comment; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:adata; 37, conditional_expression:if; 37, 38; 37, 43; 37, 44; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:data; 41, identifier:copy; 42, argument_list; 43, identifier:copy; 44, identifier:data; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:layers; 48, conditional_expression:if; 48, 49; 48, 56; 48, 59; 49, call; 49, 50; 49, 55; 50, attribute; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:adata; 53, identifier:layers; 54, identifier:keys; 55, argument_list; 56, comparison_operator:is; 56, 57; 56, 58; 57, identifier:layers; 58, string:'all'; 59, conditional_expression:if; 59, 60; 59, 62; 59, 67; 59, 68; 60, list:[layers]; 60, 61; 61, identifier:layers; 62, call; 62, 63; 62, 64; 63, identifier:isinstance; 64, argument_list; 64, 65; 64, 66; 65, identifier:layers; 66, identifier:str; 67, line_continuation:\; 68, list_comprehension; 68, 69; 68, 70; 68, 73; 69, identifier:layer; 70, for_in_clause; 70, 71; 70, 72; 71, identifier:layer; 72, identifier:layers; 73, if_clause; 73, 74; 74, comparison_operator:in; 74, 75; 74, 76; 75, identifier:layer; 76, call; 76, 77; 76, 82; 77, attribute; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:adata; 80, identifier:layers; 81, identifier:keys; 82, argument_list; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:layers; 86, binary_operator:+; 86, 87; 86, 89; 87, list:['X']; 87, 88; 88, string:'X'; 89, identifier:layers; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:modified_layers; 93, list:[]; 94, for_statement; 94, 95; 94, 96; 94, 97; 95, identifier:layer; 96, identifier:layers; 97, block; 97, 98; 97, 113; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:X; 101, conditional_expression:if; 101, 102; 101, 105; 101, 108; 102, attribute; 102, 103; 102, 104; 103, identifier:adata; 104, identifier:X; 105, comparison_operator:is; 105, 106; 105, 107; 106, identifier:layer; 107, string:'X'; 108, subscript; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:adata; 111, identifier:layers; 112, identifier:layer; 113, if_statement; 113, 114; 113, 120; 114, boolean_operator:or; 114, 115; 114, 119; 115, call; 115, 116; 115, 117; 116, identifier:not_yet_normalized; 117, argument_list; 117, 118; 118, identifier:X; 119, identifier:enforce; 120, block; 120, 121; 120, 142; 120, 162; 120, 163; 120, 177; 120, 186; 120, 192; 120, 193; 120, 224; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:counts; 124, conditional_expression:if; 124, 125; 124, 126; 124, 129; 124, 130; 125, identifier:counts_per_cell; 126, comparison_operator:is; 126, 127; 126, 128; 127, identifier:counts_per_cell; 128, None; 129, line_continuation:\; 130, conditional_expression:if; 130, 131; 130, 136; 130, 137; 131, call; 131, 132; 131, 133; 132, identifier:get_initial_size; 133, argument_list; 133, 134; 133, 135; 134, identifier:adata; 135, identifier:layer; 136, identifier:use_initial_size; 137, call; 137, 138; 137, 139; 138, identifier:get_size; 139, argument_list; 139, 140; 139, 141; 140, identifier:adata; 141, identifier:layer; 142, if_statement; 142, 143; 142, 152; 143, boolean_operator:and; 143, 144; 143, 147; 144, comparison_operator:is; 144, 145; 144, 146; 145, identifier:max_proportion_per_cell; 146, None; 147, parenthesized_expression; 147, 148; 148, comparison_operator:<; 148, 149; 148, 150; 148, 151; 149, integer:0; 150, identifier:max_proportion_per_cell; 151, integer:1; 152, block; 152, 153; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:counts; 156, call; 156, 157; 156, 158; 157, identifier:counts_per_cell_quantile; 158, argument_list; 158, 159; 158, 160; 158, 161; 159, identifier:X; 160, identifier:max_proportion_per_cell; 161, identifier:counts; 162, comment; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:counts_after; 166, conditional_expression:if; 166, 167; 166, 173; 166, 176; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:np; 170, identifier:median; 171, argument_list; 171, 172; 172, identifier:counts; 173, comparison_operator:is; 173, 174; 173, 175; 174, identifier:counts_per_cell_after; 175, None; 176, identifier:counts_per_cell_after; 177, expression_statement; 177, 178; 178, augmented_assignment:/=; 178, 179; 178, 180; 179, identifier:counts; 180, binary_operator:+; 180, 181; 180, 182; 181, identifier:counts_after; 182, parenthesized_expression; 182, 183; 183, comparison_operator:==; 183, 184; 183, 185; 184, identifier:counts_after; 185, integer:0; 186, expression_statement; 186, 187; 187, augmented_assignment:+=; 187, 188; 187, 189; 188, identifier:counts; 189, comparison_operator:==; 189, 190; 189, 191; 190, identifier:counts; 191, integer:0; 192, comment; 193, if_statement; 193, 194; 193, 198; 193, 209; 194, call; 194, 195; 194, 196; 195, identifier:issparse; 196, argument_list; 196, 197; 197, identifier:X; 198, block; 198, 199; 199, expression_statement; 199, 200; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:sparsefuncs; 203, identifier:inplace_row_scale; 204, argument_list; 204, 205; 204, 206; 205, identifier:X; 206, binary_operator:/; 206, 207; 206, 208; 207, integer:1; 208, identifier:counts; 209, else_clause; 209, 210; 210, block; 210, 211; 211, expression_statement; 211, 212; 212, augmented_assignment:/=; 212, 213; 212, 214; 213, identifier:X; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:np; 217, identifier:array; 218, argument_list; 218, 219; 219, subscript; 219, 220; 219, 221; 219, 223; 220, identifier:counts; 221, slice; 221, 222; 222, colon; 223, None; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:modified_layers; 228, identifier:append; 229, argument_list; 229, 230; 230, identifier:layer; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 243; 233, subscript; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:adata; 236, identifier:obs; 237, conditional_expression:if; 237, 238; 237, 239; 237, 242; 238, string:'n_counts'; 239, comparison_operator:is; 239, 240; 239, 241; 240, identifier:key_n_counts; 241, None; 242, identifier:key_n_counts; 243, call; 243, 244; 243, 245; 244, identifier:get_size; 245, argument_list; 245, 246; 246, identifier:adata; 247, if_statement; 247, 248; 247, 254; 248, comparison_operator:>; 248, 249; 248, 253; 249, call; 249, 250; 249, 251; 250, identifier:len; 251, argument_list; 251, 252; 252, identifier:modified_layers; 253, integer:0; 254, block; 254, 255; 255, expression_statement; 255, 256; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:logg; 259, identifier:info; 260, argument_list; 260, 261; 260, 262; 261, string:'Normalized count data:'; 262, binary_operator:+; 262, 263; 262, 269; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, string:', '; 266, identifier:join; 267, argument_list; 267, 268; 268, identifier:modified_layers; 269, string:'.'; 270, return_statement; 270, 271; 271, conditional_expression:if; 271, 272; 271, 273; 271, 274; 272, identifier:adata; 273, identifier:copy; 274, None
def normalize_per_cell(data, counts_per_cell_after=None, counts_per_cell=None, key_n_counts=None, max_proportion_per_cell=None, use_initial_size=True, layers=['spliced', 'unspliced'], enforce=False, copy=False): """Normalize each cell by total counts over all genes. Parameters ---------- data : :class:`~anndata.AnnData`, `np.ndarray`, `sp.sparse` The (annotated) data matrix of shape `n_obs` × `n_vars`. Rows correspond to cells and columns to genes. counts_per_cell_after : `float` or `None`, optional (default: `None`) If `None`, after normalization, each cell has a total count equal to the median of the *counts_per_cell* before normalization. counts_per_cell : `np.array`, optional (default: `None`) Precomputed counts per cell. key_n_counts : `str`, optional (default: `'n_counts'`) Name of the field in `adata.obs` where the total counts per cell are stored. max_proportion_per_cell : `int` (default: `None`) Exclude genes counts that account for more than a specific proportion of cell size, e.g. 0.05. use_initial_size : `bool` (default: `True`) Whether to use initial cell sizes oder actual cell sizes. layers : `str` or `list` (default: `{'spliced', 'unspliced'}`) Keys for layers to be also considered for normalization. copy : `bool`, optional (default: `False`) If an :class:`~anndata.AnnData` is passed, determines whether a copy is returned. Returns ------- Returns or updates `adata` with normalized version of the original `adata.X`, depending on `copy`. """ adata = data.copy() if copy else data layers = adata.layers.keys() if layers is 'all' else [layers] if isinstance(layers, str) \ else [layer for layer in layers if layer in adata.layers.keys()] layers = ['X'] + layers modified_layers = [] for layer in layers: X = adata.X if layer is 'X' else adata.layers[layer] if not_yet_normalized(X) or enforce: counts = counts_per_cell if counts_per_cell is not None \ else get_initial_size(adata, layer) if use_initial_size else get_size(adata, layer) if max_proportion_per_cell is not None and (0 < max_proportion_per_cell < 1): counts = counts_per_cell_quantile(X, max_proportion_per_cell, counts) # equivalent to scanpy.pp.normalize_per_cell(X, counts_per_cell_after, counts) counts_after = np.median(counts) if counts_per_cell_after is None else counts_per_cell_after counts /= counts_after + (counts_after == 0) counts += counts == 0 # to avoid division by zero if issparse(X): sparsefuncs.inplace_row_scale(X, 1 / counts) else: X /= np.array(counts[:, None]) modified_layers.append(layer) adata.obs['n_counts' if key_n_counts is None else key_n_counts] = get_size(adata) if len(modified_layers) > 0: logg.info('Normalized count data:', ', '.join(modified_layers) + '.') return adata if copy else None
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 35; 2, function_name:filter_and_normalize; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 3, 29; 3, 32; 4, identifier:data; 5, default_parameter; 5, 6; 5, 7; 6, identifier:min_counts; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:min_counts_u; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:min_cells; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:min_cells_u; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:min_shared_counts; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:min_shared_cells; 22, None; 23, default_parameter; 23, 24; 23, 25; 24, identifier:n_top_genes; 25, None; 26, default_parameter; 26, 27; 26, 28; 27, identifier:flavor; 28, string:'seurat'; 29, default_parameter; 29, 30; 29, 31; 30, identifier:log; 31, True; 32, default_parameter; 32, 33; 32, 34; 33, identifier:copy; 34, False; 35, block; 35, 36; 35, 38; 35, 49; 35, 75; 35, 98; 35, 103; 35, 119; 35, 151; 35, 161; 35, 179; 36, expression_statement; 36, 37; 37, comment; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:adata; 41, conditional_expression:if; 41, 42; 41, 47; 41, 48; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:data; 45, identifier:copy; 46, argument_list; 47, identifier:copy; 48, identifier:data; 49, if_statement; 49, 50; 49, 69; 50, boolean_operator:or; 50, 51; 50, 60; 51, comparison_operator:not; 51, 52; 51, 53; 52, string:'spliced'; 53, call; 53, 54; 53, 59; 54, attribute; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:adata; 57, identifier:layers; 58, identifier:keys; 59, argument_list; 60, comparison_operator:not; 60, 61; 60, 62; 61, string:'unspliced'; 62, call; 62, 63; 62, 68; 63, attribute; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:adata; 66, identifier:layers; 67, identifier:keys; 68, argument_list; 69, block; 69, 70; 70, raise_statement; 70, 71; 71, call; 71, 72; 71, 73; 72, identifier:ValueError; 73, argument_list; 73, 74; 74, string:'Could not find spliced / unspliced counts.'; 75, expression_statement; 75, 76; 76, call; 76, 77; 76, 78; 77, identifier:filter_genes; 78, argument_list; 78, 79; 78, 80; 78, 83; 78, 86; 78, 89; 78, 92; 78, 95; 79, identifier:adata; 80, keyword_argument; 80, 81; 80, 82; 81, identifier:min_counts; 82, identifier:min_counts; 83, keyword_argument; 83, 84; 83, 85; 84, identifier:min_counts_u; 85, identifier:min_counts_u; 86, keyword_argument; 86, 87; 86, 88; 87, identifier:min_cells; 88, identifier:min_cells; 89, keyword_argument; 89, 90; 89, 91; 90, identifier:min_cells_u; 91, identifier:min_cells_u; 92, keyword_argument; 92, 93; 92, 94; 93, identifier:min_shared_counts; 94, identifier:min_shared_counts; 95, keyword_argument; 95, 96; 95, 97; 96, identifier:min_shared_cells; 97, identifier:min_shared_cells; 98, expression_statement; 98, 99; 99, call; 99, 100; 99, 101; 100, identifier:normalize_per_cell; 101, argument_list; 101, 102; 102, identifier:adata; 103, if_statement; 103, 104; 103, 107; 104, comparison_operator:is; 104, 105; 104, 106; 105, identifier:n_top_genes; 106, None; 107, block; 107, 108; 108, expression_statement; 108, 109; 109, call; 109, 110; 109, 111; 110, identifier:filter_genes_dispersion; 111, argument_list; 111, 112; 111, 113; 111, 116; 112, identifier:adata; 113, keyword_argument; 113, 114; 113, 115; 114, identifier:n_top_genes; 115, identifier:n_top_genes; 116, keyword_argument; 116, 117; 116, 118; 117, identifier:flavor; 118, identifier:flavor; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:log_advised; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:np; 125, identifier:allclose; 126, argument_list; 126, 127; 126, 138; 127, call; 127, 128; 127, 137; 128, attribute; 128, 129; 128, 136; 129, subscript; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:adata; 132, identifier:X; 133, slice; 133, 134; 133, 135; 134, colon; 135, integer:10; 136, identifier:sum; 137, argument_list; 138, call; 138, 139; 138, 150; 139, attribute; 139, 140; 139, 149; 140, subscript; 140, 141; 140, 146; 141, subscript; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:adata; 144, identifier:layers; 145, string:'spliced'; 146, slice; 146, 147; 146, 148; 147, colon; 148, integer:10; 149, identifier:sum; 150, argument_list; 151, if_statement; 151, 152; 151, 155; 152, boolean_operator:and; 152, 153; 152, 154; 153, identifier:log; 154, identifier:log_advised; 155, block; 155, 156; 156, expression_statement; 156, 157; 157, call; 157, 158; 157, 159; 158, identifier:log1p; 159, argument_list; 159, 160; 160, identifier:adata; 161, expression_statement; 161, 162; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:logg; 165, identifier:info; 166, argument_list; 166, 167; 167, conditional_expression:if; 167, 168; 167, 169; 167, 172; 168, string:'Logarithmized X.'; 169, boolean_operator:and; 169, 170; 169, 171; 170, identifier:log; 171, identifier:log_advised; 172, conditional_expression:if; 172, 173; 172, 174; 172, 175; 173, string:'Did not modify X as it looks preprocessed already.'; 174, identifier:log; 175, conditional_expression:if; 175, 176; 175, 177; 175, 178; 176, string:'Consider logarithmizing X with `scv.pp.log1p` for better results.'; 177, identifier:log_advised; 178, string:''; 179, return_statement; 179, 180; 180, conditional_expression:if; 180, 181; 180, 182; 180, 183; 181, identifier:adata; 182, identifier:copy; 183, None
def filter_and_normalize(data, min_counts=None, min_counts_u=None, min_cells=None, min_cells_u=None, min_shared_counts=None, min_shared_cells=None, n_top_genes=None, flavor='seurat', log=True, copy=False): """Filtering, normalization and log transform Expects non-logarithmized data. If using logarithmized data, pass `log=False`. Runs the following steps .. code:: python scv.pp.filter_genes(adata) scv.pp.normalize_per_cell(adata) if n_top_genes is not None: scv.pp.filter_genes_dispersion(adata) if log: scv.pp.log1p(adata) Arguments --------- data: :class:`~anndata.AnnData` Annotated data matrix. min_counts: `int` (default: `None`) Minimum number of counts required for a gene to pass filtering (spliced). min_counts_u: `int` (default: `None`) Minimum number of counts required for a gene to pass filtering (unspliced). min_cells: `int` (default: `None`) Minimum number of cells expressed required for a gene to pass filtering (spliced). min_cells_u: `int` (default: `None`) Minimum number of cells expressed required for a gene to pass filtering (unspliced). min_shared_counts: `int`, optional (default: `None`) Minimum number of counts (in cells expressed simultaneously in unspliced and spliced) required for a gene. min_shared_cells: `int`, optional (default: `None`) Minimum number of cells required for a gene to be expressed simultaneously in unspliced and spliced. n_top_genes: `int` (default: `None`) Number of genes to keep. flavor: {'seurat', 'cell_ranger', 'svr'}, optional (default: 'seurat') Choose the flavor for computing normalized dispersion. If choosing 'seurat', this expects non-logarithmized data. log: `bool` (default: `True`) Take logarithm. copy: `bool` (default: `False`) Return a copy of `adata` instead of updating it. Returns ------- Returns or updates `adata` depending on `copy`. """ adata = data.copy() if copy else data if 'spliced' not in adata.layers.keys() or 'unspliced' not in adata.layers.keys(): raise ValueError('Could not find spliced / unspliced counts.') filter_genes(adata, min_counts=min_counts, min_counts_u=min_counts_u, min_cells=min_cells, min_cells_u=min_cells_u, min_shared_counts=min_shared_counts, min_shared_cells=min_shared_cells,) normalize_per_cell(adata) if n_top_genes is not None: filter_genes_dispersion(adata, n_top_genes=n_top_genes, flavor=flavor) log_advised = np.allclose(adata.X[:10].sum(), adata.layers['spliced'][:10].sum()) if log and log_advised: log1p(adata) logg.info('Logarithmized X.' if log and log_advised else 'Did not modify X as it looks preprocessed already.' if log else 'Consider logarithmizing X with `scv.pp.log1p` for better results.' if log_advised else '') return adata if copy else None
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:merge; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:adata; 5, identifier:ldata; 6, default_parameter; 6, 7; 6, 8; 7, identifier:copy; 8, True; 9, block; 9, 10; 9, 12; 9, 64; 9, 77; 9, 90; 9, 121; 9, 201; 9, 231; 9, 256; 9, 278; 9, 300; 9, 322; 9, 344; 9, 441; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 32; 12, 38; 13, boolean_operator:and; 13, 14; 13, 23; 14, comparison_operator:in; 14, 15; 14, 16; 15, string:'spliced'; 16, call; 16, 17; 16, 22; 17, attribute; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:ldata; 20, identifier:layers; 21, identifier:keys; 22, argument_list; 23, comparison_operator:not; 23, 24; 23, 25; 24, string:'initial_size_spliced'; 25, call; 25, 26; 25, 31; 26, attribute; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:ldata; 29, identifier:obs; 30, identifier:keys; 31, argument_list; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, call; 34, 35; 34, 36; 35, identifier:set_initial_size; 36, argument_list; 36, 37; 37, identifier:ldata; 38, elif_clause; 38, 39; 38, 58; 39, boolean_operator:and; 39, 40; 39, 49; 40, comparison_operator:in; 40, 41; 40, 42; 41, string:'spliced'; 42, call; 42, 43; 42, 48; 43, attribute; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:adata; 46, identifier:layers; 47, identifier:keys; 48, argument_list; 49, comparison_operator:not; 49, 50; 49, 51; 50, string:'initial_size_spliced'; 51, call; 51, 52; 51, 57; 52, attribute; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:adata; 55, identifier:obs; 56, identifier:keys; 57, argument_list; 58, block; 58, 59; 59, expression_statement; 59, 60; 60, call; 60, 61; 60, 62; 61, identifier:set_initial_size; 62, argument_list; 62, 63; 63, identifier:adata; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:common_obs; 67, call; 67, 68; 67, 73; 68, attribute; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:adata; 71, identifier:obs_names; 72, identifier:intersection; 73, argument_list; 73, 74; 74, attribute; 74, 75; 74, 76; 75, identifier:ldata; 76, identifier:obs_names; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:common_vars; 80, call; 80, 81; 80, 86; 81, attribute; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:adata; 84, identifier:var_names; 85, identifier:intersection; 86, argument_list; 86, 87; 87, attribute; 87, 88; 87, 89; 88, identifier:ldata; 89, identifier:var_names; 90, if_statement; 90, 91; 90, 97; 91, comparison_operator:==; 91, 92; 91, 96; 92, call; 92, 93; 92, 94; 93, identifier:len; 94, argument_list; 94, 95; 95, identifier:common_obs; 96, integer:0; 97, block; 97, 98; 97, 103; 97, 108; 98, expression_statement; 98, 99; 99, call; 99, 100; 99, 101; 100, identifier:clean_obs_names; 101, argument_list; 101, 102; 102, identifier:adata; 103, expression_statement; 103, 104; 104, call; 104, 105; 104, 106; 105, identifier:clean_obs_names; 106, argument_list; 106, 107; 107, identifier:ldata; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:common_obs; 111, call; 111, 112; 111, 117; 112, attribute; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:adata; 115, identifier:obs_names; 116, identifier:intersection; 117, argument_list; 117, 118; 118, attribute; 118, 119; 118, 120; 119, identifier:ldata; 120, identifier:obs_names; 121, if_statement; 121, 122; 121, 123; 121, 182; 122, identifier:copy; 123, block; 123, 124; 123, 153; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:_adata; 127, conditional_expression:if; 127, 128; 127, 135; 127, 146; 128, call; 128, 129; 128, 134; 129, attribute; 129, 130; 129, 133; 130, subscript; 130, 131; 130, 132; 131, identifier:adata; 132, identifier:common_obs; 133, identifier:copy; 134, argument_list; 135, comparison_operator:>=; 135, 136; 135, 141; 136, subscript; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:adata; 139, identifier:shape; 140, integer:1; 141, subscript; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:ldata; 144, identifier:shape; 145, integer:1; 146, call; 146, 147; 146, 152; 147, attribute; 147, 148; 147, 151; 148, subscript; 148, 149; 148, 150; 149, identifier:ldata; 150, identifier:common_obs; 151, identifier:copy; 152, argument_list; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:_ldata; 156, conditional_expression:if; 156, 157; 156, 164; 156, 175; 157, call; 157, 158; 157, 163; 158, attribute; 158, 159; 158, 162; 159, subscript; 159, 160; 159, 161; 160, identifier:ldata; 161, identifier:common_obs; 162, identifier:copy; 163, argument_list; 164, comparison_operator:>=; 164, 165; 164, 170; 165, subscript; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:adata; 168, identifier:shape; 169, integer:1; 170, subscript; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:ldata; 173, identifier:shape; 174, integer:1; 175, call; 175, 176; 175, 181; 176, attribute; 176, 177; 176, 180; 177, subscript; 177, 178; 177, 179; 178, identifier:adata; 179, identifier:common_obs; 180, identifier:copy; 181, argument_list; 182, else_clause; 182, 183; 183, block; 183, 184; 183, 191; 184, expression_statement; 184, 185; 185, call; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:adata; 188, identifier:_inplace_subset_obs; 189, argument_list; 189, 190; 190, identifier:common_obs; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 196; 193, pattern_list; 193, 194; 193, 195; 194, identifier:_adata; 195, identifier:_ldata; 196, expression_list; 196, 197; 196, 198; 197, identifier:adata; 198, subscript; 198, 199; 198, 200; 199, identifier:ldata; 200, identifier:common_obs; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 204; 203, identifier:same_vars; 204, parenthesized_expression; 204, 205; 205, boolean_operator:and; 205, 206; 205, 219; 206, comparison_operator:==; 206, 207; 206, 213; 207, call; 207, 208; 207, 209; 208, identifier:len; 209, argument_list; 209, 210; 210, attribute; 210, 211; 210, 212; 211, identifier:_adata; 212, identifier:var_names; 213, call; 213, 214; 213, 215; 214, identifier:len; 215, argument_list; 215, 216; 216, attribute; 216, 217; 216, 218; 217, identifier:_ldata; 218, identifier:var_names; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:np; 222, identifier:all; 223, argument_list; 223, 224; 224, comparison_operator:==; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:_adata; 227, identifier:var_names; 228, attribute; 228, 229; 228, 230; 229, identifier:_ldata; 230, identifier:var_names; 231, if_statement; 231, 232; 231, 241; 232, boolean_operator:and; 232, 233; 232, 239; 233, comparison_operator:>; 233, 234; 233, 238; 234, call; 234, 235; 234, 236; 235, identifier:len; 236, argument_list; 236, 237; 237, identifier:common_vars; 238, integer:0; 239, not_operator; 239, 240; 240, identifier:same_vars; 241, block; 241, 242; 241, 249; 242, expression_statement; 242, 243; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:_adata; 246, identifier:_inplace_subset_var; 247, argument_list; 247, 248; 248, identifier:common_vars; 249, expression_statement; 249, 250; 250, call; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:_ldata; 253, identifier:_inplace_subset_var; 254, argument_list; 254, 255; 255, identifier:common_vars; 256, for_statement; 256, 257; 256, 258; 256, 265; 257, identifier:attr; 258, call; 258, 259; 258, 264; 259, attribute; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:_ldata; 262, identifier:obs; 263, identifier:keys; 264, argument_list; 265, block; 265, 266; 266, expression_statement; 266, 267; 267, assignment; 267, 268; 267, 273; 268, subscript; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:_adata; 271, identifier:obs; 272, identifier:attr; 273, subscript; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:_ldata; 276, identifier:obs; 277, identifier:attr; 278, for_statement; 278, 279; 278, 280; 278, 287; 279, identifier:attr; 280, call; 280, 281; 280, 286; 281, attribute; 281, 282; 281, 285; 282, attribute; 282, 283; 282, 284; 283, identifier:_ldata; 284, identifier:obsm; 285, identifier:keys; 286, argument_list; 287, block; 287, 288; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 295; 290, subscript; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:_adata; 293, identifier:obsm; 294, identifier:attr; 295, subscript; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:_ldata; 298, identifier:obsm; 299, identifier:attr; 300, for_statement; 300, 301; 300, 302; 300, 309; 301, identifier:attr; 302, call; 302, 303; 302, 308; 303, attribute; 303, 304; 303, 307; 304, attribute; 304, 305; 304, 306; 305, identifier:_ldata; 306, identifier:uns; 307, identifier:keys; 308, argument_list; 309, block; 309, 310; 310, expression_statement; 310, 311; 311, assignment; 311, 312; 311, 317; 312, subscript; 312, 313; 312, 316; 313, attribute; 313, 314; 313, 315; 314, identifier:_adata; 315, identifier:uns; 316, identifier:attr; 317, subscript; 317, 318; 317, 321; 318, attribute; 318, 319; 318, 320; 319, identifier:_ldata; 320, identifier:uns; 321, identifier:attr; 322, for_statement; 322, 323; 322, 324; 322, 331; 323, identifier:attr; 324, call; 324, 325; 324, 330; 325, attribute; 325, 326; 325, 329; 326, attribute; 326, 327; 326, 328; 327, identifier:_ldata; 328, identifier:layers; 329, identifier:keys; 330, argument_list; 331, block; 331, 332; 332, expression_statement; 332, 333; 333, assignment; 333, 334; 333, 339; 334, subscript; 334, 335; 334, 338; 335, attribute; 335, 336; 335, 337; 336, identifier:_adata; 337, identifier:layers; 338, identifier:attr; 339, subscript; 339, 340; 339, 343; 340, attribute; 340, 341; 340, 342; 341, identifier:_ldata; 342, identifier:layers; 343, identifier:attr; 344, if_statement; 344, 345; 344, 356; 345, comparison_operator:==; 345, 346; 345, 351; 346, subscript; 346, 347; 346, 350; 347, attribute; 347, 348; 347, 349; 348, identifier:_adata; 349, identifier:shape; 350, integer:1; 351, subscript; 351, 352; 351, 355; 352, attribute; 352, 353; 352, 354; 353, identifier:_ldata; 354, identifier:shape; 355, integer:1; 356, block; 356, 357; 356, 387; 357, expression_statement; 357, 358; 358, assignment; 358, 359; 358, 360; 359, identifier:same_vars; 360, parenthesized_expression; 360, 361; 361, boolean_operator:and; 361, 362; 361, 375; 362, comparison_operator:==; 362, 363; 362, 369; 363, call; 363, 364; 363, 365; 364, identifier:len; 365, argument_list; 365, 366; 366, attribute; 366, 367; 366, 368; 367, identifier:_adata; 368, identifier:var_names; 369, call; 369, 370; 369, 371; 370, identifier:len; 371, argument_list; 371, 372; 372, attribute; 372, 373; 372, 374; 373, identifier:_ldata; 374, identifier:var_names; 375, call; 375, 376; 375, 379; 376, attribute; 376, 377; 376, 378; 377, identifier:np; 378, identifier:all; 379, argument_list; 379, 380; 380, comparison_operator:==; 380, 381; 380, 384; 381, attribute; 381, 382; 381, 383; 382, identifier:_adata; 383, identifier:var_names; 384, attribute; 384, 385; 384, 386; 385, identifier:_ldata; 386, identifier:var_names; 387, if_statement; 387, 388; 387, 389; 387, 434; 388, identifier:same_vars; 389, block; 389, 390; 389, 412; 390, for_statement; 390, 391; 390, 392; 390, 399; 391, identifier:attr; 392, call; 392, 393; 392, 398; 393, attribute; 393, 394; 393, 397; 394, attribute; 394, 395; 394, 396; 395, identifier:_ldata; 396, identifier:var; 397, identifier:keys; 398, argument_list; 399, block; 399, 400; 400, expression_statement; 400, 401; 401, assignment; 401, 402; 401, 407; 402, subscript; 402, 403; 402, 406; 403, attribute; 403, 404; 403, 405; 404, identifier:_adata; 405, identifier:var; 406, identifier:attr; 407, subscript; 407, 408; 407, 411; 408, attribute; 408, 409; 408, 410; 409, identifier:_ldata; 410, identifier:var; 411, identifier:attr; 412, for_statement; 412, 413; 412, 414; 412, 421; 413, identifier:attr; 414, call; 414, 415; 414, 420; 415, attribute; 415, 416; 415, 419; 416, attribute; 416, 417; 416, 418; 417, identifier:_ldata; 418, identifier:varm; 419, identifier:keys; 420, argument_list; 421, block; 421, 422; 422, expression_statement; 422, 423; 423, assignment; 423, 424; 423, 429; 424, subscript; 424, 425; 424, 428; 425, attribute; 425, 426; 425, 427; 426, identifier:_adata; 427, identifier:varm; 428, identifier:attr; 429, subscript; 429, 430; 429, 433; 430, attribute; 430, 431; 430, 432; 431, identifier:_ldata; 432, identifier:varm; 433, identifier:attr; 434, else_clause; 434, 435; 435, block; 435, 436; 436, raise_statement; 436, 437; 437, call; 437, 438; 437, 439; 438, identifier:ValueError; 439, argument_list; 439, 440; 440, string:'Variable names are not identical.'; 441, return_statement; 441, 442; 442, conditional_expression:if; 442, 443; 442, 444; 442, 445; 443, identifier:_adata; 444, identifier:copy; 445, None
def merge(adata, ldata, copy=True): """Merges two annotated data matrices. Arguments --------- adata: :class:`~anndata.AnnData` Annotated data matrix (reference data set). ldata: :class:`~anndata.AnnData` Annotated data matrix (to be merged into adata). Returns ------- Returns a :class:`~anndata.AnnData` object """ if 'spliced' in ldata.layers.keys() and 'initial_size_spliced' not in ldata.obs.keys(): set_initial_size(ldata) elif 'spliced' in adata.layers.keys() and 'initial_size_spliced' not in adata.obs.keys(): set_initial_size(adata) common_obs = adata.obs_names.intersection(ldata.obs_names) common_vars = adata.var_names.intersection(ldata.var_names) if len(common_obs) == 0: clean_obs_names(adata) clean_obs_names(ldata) common_obs = adata.obs_names.intersection(ldata.obs_names) if copy: _adata = adata[common_obs].copy() if adata.shape[1] >= ldata.shape[1] else ldata[common_obs].copy() _ldata = ldata[common_obs].copy() if adata.shape[1] >= ldata.shape[1] else adata[common_obs].copy() else: adata._inplace_subset_obs(common_obs) _adata, _ldata = adata, ldata[common_obs] same_vars = (len(_adata.var_names) == len(_ldata.var_names) and np.all(_adata.var_names == _ldata.var_names)) if len(common_vars) > 0 and not same_vars: _adata._inplace_subset_var(common_vars) _ldata._inplace_subset_var(common_vars) for attr in _ldata.obs.keys(): _adata.obs[attr] = _ldata.obs[attr] for attr in _ldata.obsm.keys(): _adata.obsm[attr] = _ldata.obsm[attr] for attr in _ldata.uns.keys(): _adata.uns[attr] = _ldata.uns[attr] for attr in _ldata.layers.keys(): _adata.layers[attr] = _ldata.layers[attr] if _adata.shape[1] == _ldata.shape[1]: same_vars = (len(_adata.var_names) == len(_ldata.var_names) and np.all(_adata.var_names == _ldata.var_names)) if same_vars: for attr in _ldata.var.keys(): _adata.var[attr] = _ldata.var[attr] for attr in _ldata.varm.keys(): _adata.varm[attr] = _ldata.varm[attr] else: raise ValueError('Variable names are not identical.') return _adata if copy else None
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:cell_fate; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:data; 5, default_parameter; 5, 6; 5, 7; 6, identifier:groupby; 7, string:'clusters'; 8, default_parameter; 8, 9; 8, 10; 9, identifier:disconnected_groups; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:self_transitions; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:n_neighbors; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:copy; 19, False; 20, block; 20, 21; 20, 23; 20, 34; 20, 44; 20, 53; 20, 61; 20, 77; 20, 83; 20, 93; 20, 103; 20, 113; 20, 124; 20, 137; 20, 149; 20, 169; 20, 199; 20, 207; 20, 227; 20, 232; 20, 252; 20, 262; 21, expression_statement; 21, 22; 22, comment; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:adata; 26, conditional_expression:if; 26, 27; 26, 32; 26, 33; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:data; 30, identifier:copy; 31, argument_list; 32, identifier:copy; 33, identifier:data; 34, expression_statement; 34, 35; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:logg; 38, identifier:info; 39, argument_list; 39, 40; 39, 41; 40, string:'computing cell fates'; 41, keyword_argument; 41, 42; 41, 43; 42, identifier:r; 43, True; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:n_neighbors; 47, conditional_expression:if; 47, 48; 47, 49; 47, 52; 48, integer:10; 49, comparison_operator:is; 49, 50; 49, 51; 50, identifier:n_neighbors; 51, None; 52, identifier:n_neighbors; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:_adata; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:adata; 59, identifier:copy; 60, argument_list; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:vgraph; 64, call; 64, 65; 64, 66; 65, identifier:VelocityGraph; 66, argument_list; 66, 67; 66, 68; 66, 71; 66, 74; 67, identifier:_adata; 68, keyword_argument; 68, 69; 68, 70; 69, identifier:n_neighbors; 70, identifier:n_neighbors; 71, keyword_argument; 71, 72; 71, 73; 72, identifier:approx; 73, True; 74, keyword_argument; 74, 75; 74, 76; 75, identifier:n_recurse_neighbors; 76, integer:1; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:vgraph; 81, identifier:compute_cosines; 82, argument_list; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 90; 85, subscript; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:_adata; 88, identifier:uns; 89, string:'velocity_graph'; 90, attribute; 90, 91; 90, 92; 91, identifier:vgraph; 92, identifier:graph; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 100; 95, subscript; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:_adata; 98, identifier:uns; 99, string:'velocity_graph_neg'; 100, attribute; 100, 101; 100, 102; 101, identifier:vgraph; 102, identifier:graph_neg; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:T; 106, call; 106, 107; 106, 108; 107, identifier:transition_matrix; 108, argument_list; 108, 109; 108, 110; 109, identifier:_adata; 110, keyword_argument; 110, 111; 110, 112; 111, identifier:self_transitions; 112, identifier:self_transitions; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:I; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:np; 119, identifier:eye; 120, argument_list; 120, 121; 121, attribute; 121, 122; 121, 123; 122, identifier:_adata; 123, identifier:n_obs; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:fate; 127, call; 127, 128; 127, 133; 128, attribute; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:np; 131, identifier:linalg; 132, identifier:inv; 133, argument_list; 133, 134; 134, binary_operator:-; 134, 135; 134, 136; 135, identifier:I; 136, identifier:T; 137, if_statement; 137, 138; 137, 142; 138, call; 138, 139; 138, 140; 139, identifier:issparse; 140, argument_list; 140, 141; 141, identifier:T; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:fate; 146, attribute; 146, 147; 146, 148; 147, identifier:fate; 148, identifier:A; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:cell_fates; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:np; 155, identifier:array; 156, argument_list; 156, 157; 157, subscript; 157, 158; 157, 163; 158, subscript; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:_adata; 161, identifier:obs; 162, identifier:groupby; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:fate; 166, identifier:argmax; 167, argument_list; 167, 168; 168, integer:1; 169, if_statement; 169, 170; 169, 173; 170, comparison_operator:is; 170, 171; 170, 172; 171, identifier:disconnected_groups; 172, None; 173, block; 173, 174; 173, 187; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:idx; 177, call; 177, 178; 177, 185; 178, attribute; 178, 179; 178, 184; 179, subscript; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:_adata; 182, identifier:obs; 183, identifier:groupby; 184, identifier:isin; 185, argument_list; 185, 186; 186, identifier:disconnected_groups; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 192; 189, subscript; 189, 190; 189, 191; 190, identifier:cell_fates; 191, identifier:idx; 192, subscript; 192, 193; 192, 198; 193, subscript; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:_adata; 196, identifier:obs; 197, identifier:groupby; 198, identifier:idx; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 206; 201, subscript; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:adata; 204, identifier:obs; 205, string:'cell_fate'; 206, identifier:cell_fates; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 214; 209, subscript; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:adata; 212, identifier:obs; 213, string:'cell_fate_confidence'; 214, binary_operator:/; 214, 215; 214, 221; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:fate; 218, identifier:max; 219, argument_list; 219, 220; 220, integer:1; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:fate; 224, identifier:sum; 225, argument_list; 225, 226; 226, integer:1; 227, expression_statement; 227, 228; 228, call; 228, 229; 228, 230; 229, identifier:strings_to_categoricals; 230, argument_list; 230, 231; 231, identifier:adata; 232, expression_statement; 232, 233; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:logg; 236, identifier:info; 237, argument_list; 237, 238; 237, 239; 237, 242; 238, string:' finished'; 239, keyword_argument; 239, 240; 239, 241; 240, identifier:time; 241, True; 242, keyword_argument; 242, 243; 242, 244; 243, identifier:end; 244, conditional_expression:if; 244, 245; 244, 246; 244, 251; 245, string:' '; 246, comparison_operator:>; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:settings; 249, identifier:verbosity; 250, integer:2; 251, string:'\n'; 252, expression_statement; 252, 253; 253, call; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:logg; 256, identifier:hint; 257, argument_list; 257, 258; 258, concatenated_string; 258, 259; 258, 260; 258, 261; 259, string:'added\n'; 260, string:' \'cell_fate\', most likely cell fate (adata.obs)\n'; 261, string:' \'cell_fate_confidence\', confidence of transitioning to the assigned fate (adata.obs)'; 262, return_statement; 262, 263; 263, conditional_expression:if; 263, 264; 263, 265; 263, 266; 264, identifier:adata; 265, identifier:copy; 266, None
def cell_fate(data, groupby='clusters', disconnected_groups=None, self_transitions=False, n_neighbors=None, copy=False): """Computes individual cell endpoints Arguments --------- data: :class:`~anndata.AnnData` Annotated data matrix. groupby: `str` (default: `'clusters'`) Key to which to assign the fates. disconnected_groups: list of `str` (default: `None`) Which groups to treat as disconnected for fate assignment. n_neighbors: `int` (default: `None`) Number of neighbors to restrict transitions to. copy: `bool` (default: `False`) Return a copy instead of writing to `adata`. Returns ------- Returns or updates `adata` with the attributes cell_fate: `.obs` most likely cell fate for each individual cell cell_fate_confidence: `.obs` confidence of transitioning to the assigned fate """ adata = data.copy() if copy else data logg.info('computing cell fates', r=True) n_neighbors = 10 if n_neighbors is None else n_neighbors _adata = adata.copy() vgraph = VelocityGraph(_adata, n_neighbors=n_neighbors, approx=True, n_recurse_neighbors=1) vgraph.compute_cosines() _adata.uns['velocity_graph'] = vgraph.graph _adata.uns['velocity_graph_neg'] = vgraph.graph_neg T = transition_matrix(_adata, self_transitions=self_transitions) I = np.eye(_adata.n_obs) fate = np.linalg.inv(I - T) if issparse(T): fate = fate.A cell_fates = np.array(_adata.obs[groupby][fate.argmax(1)]) if disconnected_groups is not None: idx = _adata.obs[groupby].isin(disconnected_groups) cell_fates[idx] = _adata.obs[groupby][idx] adata.obs['cell_fate'] = cell_fates adata.obs['cell_fate_confidence'] = fate.max(1) / fate.sum(1) strings_to_categoricals(adata) logg.info(' finished', time=True, end=' ' if settings.verbosity > 2 else '\n') logg.hint( 'added\n' ' \'cell_fate\', most likely cell fate (adata.obs)\n' ' \'cell_fate_confidence\', confidence of transitioning to the assigned fate (adata.obs)') return adata if copy else None
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 32; 2, function_name:moments; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 3, 29; 4, identifier:data; 5, default_parameter; 5, 6; 5, 7; 6, identifier:n_neighbors; 7, integer:30; 8, default_parameter; 8, 9; 8, 10; 9, identifier:n_pcs; 10, integer:30; 11, default_parameter; 11, 12; 11, 13; 12, identifier:mode; 13, string:'connectivities'; 14, default_parameter; 14, 15; 14, 16; 15, identifier:method; 16, string:'umap'; 17, default_parameter; 17, 18; 17, 19; 18, identifier:metric; 19, string:'euclidean'; 20, default_parameter; 20, 21; 20, 22; 21, identifier:use_rep; 22, None; 23, default_parameter; 23, 24; 23, 25; 24, identifier:recurse_neighbors; 25, False; 26, default_parameter; 26, 27; 26, 28; 27, identifier:renormalize; 28, False; 29, default_parameter; 29, 30; 29, 31; 30, identifier:copy; 31, False; 32, block; 32, 33; 32, 35; 32, 46; 32, 72; 32, 96; 32, 144; 32, 158; 32, 173; 32, 187; 32, 217; 32, 247; 32, 263; 32, 283; 32, 292; 33, expression_statement; 33, 34; 34, comment; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:adata; 38, conditional_expression:if; 38, 39; 38, 44; 38, 45; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:data; 42, identifier:copy; 43, argument_list; 44, identifier:copy; 45, identifier:data; 46, if_statement; 46, 47; 46, 66; 47, boolean_operator:or; 47, 48; 47, 57; 48, comparison_operator:not; 48, 49; 48, 50; 49, string:'spliced'; 50, call; 50, 51; 50, 56; 51, attribute; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:adata; 54, identifier:layers; 55, identifier:keys; 56, argument_list; 57, comparison_operator:not; 57, 58; 57, 59; 58, string:'unspliced'; 59, call; 59, 60; 59, 65; 60, attribute; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:adata; 63, identifier:layers; 64, identifier:keys; 65, argument_list; 66, block; 66, 67; 67, raise_statement; 67, 68; 68, call; 68, 69; 68, 70; 69, identifier:ValueError; 70, argument_list; 70, 71; 71, string:'Could not find spliced / unspliced counts.'; 72, if_statement; 72, 73; 72, 90; 73, call; 73, 74; 73, 75; 74, identifier:any; 75, argument_list; 75, 76; 76, list_comprehension; 76, 77; 76, 85; 77, call; 77, 78; 77, 79; 78, identifier:not_yet_normalized; 79, argument_list; 79, 80; 80, subscript; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:adata; 83, identifier:layers; 84, identifier:layer; 85, for_in_clause; 85, 86; 85, 87; 86, identifier:layer; 87, set; 87, 88; 87, 89; 88, string:'spliced'; 89, string:'unspliced'; 90, block; 90, 91; 91, expression_statement; 91, 92; 92, call; 92, 93; 92, 94; 93, identifier:normalize_per_cell; 94, argument_list; 94, 95; 95, identifier:adata; 96, if_statement; 96, 97; 96, 114; 97, boolean_operator:or; 97, 98; 97, 107; 98, comparison_operator:not; 98, 99; 98, 100; 99, string:'neighbors'; 100, call; 100, 101; 100, 106; 101, attribute; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:adata; 104, identifier:uns; 105, identifier:keys; 106, argument_list; 107, call; 107, 108; 107, 109; 108, identifier:neighbors_to_be_recomputed; 109, argument_list; 109, 110; 109, 111; 110, identifier:adata; 111, keyword_argument; 111, 112; 111, 113; 112, identifier:n_neighbors; 113, identifier:n_neighbors; 114, block; 114, 115; 114, 124; 115, if_statement; 115, 116; 115, 119; 116, comparison_operator:is; 116, 117; 116, 118; 117, identifier:use_rep; 118, None; 119, block; 119, 120; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:use_rep; 123, string:'X_pca'; 124, expression_statement; 124, 125; 125, call; 125, 126; 125, 127; 126, identifier:neighbors; 127, argument_list; 127, 128; 127, 129; 127, 132; 127, 135; 127, 138; 127, 141; 128, identifier:adata; 129, keyword_argument; 129, 130; 129, 131; 130, identifier:n_neighbors; 131, identifier:n_neighbors; 132, keyword_argument; 132, 133; 132, 134; 133, identifier:use_rep; 134, identifier:use_rep; 135, keyword_argument; 135, 136; 135, 137; 136, identifier:n_pcs; 137, identifier:n_pcs; 138, keyword_argument; 138, 139; 138, 140; 139, identifier:method; 140, identifier:method; 141, keyword_argument; 141, 142; 141, 143; 142, identifier:metric; 143, identifier:metric; 144, if_statement; 144, 145; 144, 152; 145, comparison_operator:not; 145, 146; 145, 147; 146, identifier:mode; 147, subscript; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:adata; 150, identifier:uns; 151, string:'neighbors'; 152, block; 152, 153; 153, raise_statement; 153, 154; 154, call; 154, 155; 154, 156; 155, identifier:ValueError; 156, argument_list; 156, 157; 157, string:'mode can only be \'connectivities\' or \'distances\''; 158, expression_statement; 158, 159; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:logg; 162, identifier:info; 163, argument_list; 163, 164; 163, 170; 164, binary_operator:+; 164, 165; 164, 166; 165, string:'computing moments based on '; 166, call; 166, 167; 166, 168; 167, identifier:str; 168, argument_list; 168, 169; 169, identifier:mode; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:r; 172, True; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:connectivities; 176, call; 176, 177; 176, 178; 177, identifier:get_connectivities; 178, argument_list; 178, 179; 178, 180; 178, 181; 178, 184; 179, identifier:adata; 180, identifier:mode; 181, keyword_argument; 181, 182; 181, 183; 182, identifier:n_neighbors; 183, identifier:n_neighbors; 184, keyword_argument; 184, 185; 184, 186; 185, identifier:recurse_neighbors; 186, identifier:recurse_neighbors; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 194; 189, subscript; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:adata; 192, identifier:layers; 193, string:'Ms'; 194, attribute; 194, 195; 194, 216; 195, call; 195, 196; 195, 212; 196, attribute; 196, 197; 196, 211; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:csr_matrix; 200, identifier:dot; 201, argument_list; 201, 202; 201, 203; 202, identifier:connectivities; 203, call; 203, 204; 203, 205; 204, identifier:csr_matrix; 205, argument_list; 205, 206; 206, subscript; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:adata; 209, identifier:layers; 210, string:'spliced'; 211, identifier:astype; 212, argument_list; 212, 213; 213, attribute; 213, 214; 213, 215; 214, identifier:np; 215, identifier:float32; 216, identifier:A; 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:adata; 222, identifier:layers; 223, string:'Mu'; 224, attribute; 224, 225; 224, 246; 225, call; 225, 226; 225, 242; 226, attribute; 226, 227; 226, 241; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:csr_matrix; 230, identifier:dot; 231, argument_list; 231, 232; 231, 233; 232, identifier:connectivities; 233, call; 233, 234; 233, 235; 234, identifier:csr_matrix; 235, argument_list; 235, 236; 236, subscript; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:adata; 239, identifier:layers; 240, string:'unspliced'; 241, identifier:astype; 242, argument_list; 242, 243; 243, attribute; 243, 244; 243, 245; 244, identifier:np; 245, identifier:float32; 246, identifier:A; 247, if_statement; 247, 248; 247, 249; 248, identifier:renormalize; 249, block; 249, 250; 250, expression_statement; 250, 251; 251, call; 251, 252; 251, 253; 252, identifier:normalize_per_cell; 253, argument_list; 253, 254; 253, 255; 253, 260; 254, identifier:adata; 255, keyword_argument; 255, 256; 255, 257; 256, identifier:layers; 257, set; 257, 258; 257, 259; 258, string:'Ms'; 259, string:'Mu'; 260, keyword_argument; 260, 261; 260, 262; 261, identifier:enforce; 262, True; 263, expression_statement; 263, 264; 264, call; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:logg; 267, identifier:info; 268, argument_list; 268, 269; 268, 270; 268, 273; 269, string:' finished'; 270, keyword_argument; 270, 271; 270, 272; 271, identifier:time; 272, True; 273, keyword_argument; 273, 274; 273, 275; 274, identifier:end; 275, conditional_expression:if; 275, 276; 275, 277; 275, 282; 276, string:' '; 277, comparison_operator:>; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:settings; 280, identifier:verbosity; 281, integer:2; 282, string:'\n'; 283, expression_statement; 283, 284; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:logg; 287, identifier:hint; 288, argument_list; 288, 289; 289, concatenated_string; 289, 290; 289, 291; 290, string:'added \n'; 291, string:' \'Ms\' and \'Mu\', moments of spliced/unspliced abundances (adata.layers)'; 292, return_statement; 292, 293; 293, conditional_expression:if; 293, 294; 293, 295; 293, 296; 294, identifier:adata; 295, identifier:copy; 296, None
def moments(data, n_neighbors=30, n_pcs=30, mode='connectivities', method='umap', metric='euclidean', use_rep=None, recurse_neighbors=False, renormalize=False, copy=False): """Computes moments for velocity estimation. Arguments --------- data: :class:`~anndata.AnnData` Annotated data matrix. n_neighbors: `int` (default: 30) Number of neighbors to use. n_pcs: `int` (default: 30) Number of principal components to use. mode: `'connectivities'` or `'distances'` (default: `'connectivities'`) Distance metric to use for moment computation. renormalize: `bool` (default: `False`) Renormalize the moments by total counts per cell to its median. copy: `bool` (default: `False`) Return a copy instead of writing to adata. Returns ------- Returns or updates `adata` with the attributes Ms: `.layers` dense matrix with first order moments of spliced counts. Mu: `.layers` dense matrix with first order moments of unspliced counts. """ adata = data.copy() if copy else data if 'spliced' not in adata.layers.keys() or 'unspliced' not in adata.layers.keys(): raise ValueError('Could not find spliced / unspliced counts.') if any([not_yet_normalized(adata.layers[layer]) for layer in {'spliced', 'unspliced'}]): normalize_per_cell(adata) if 'neighbors' not in adata.uns.keys() or neighbors_to_be_recomputed(adata, n_neighbors=n_neighbors): if use_rep is None: use_rep = 'X_pca' neighbors(adata, n_neighbors=n_neighbors, use_rep=use_rep, n_pcs=n_pcs, method=method, metric=metric) if mode not in adata.uns['neighbors']: raise ValueError('mode can only be \'connectivities\' or \'distances\'') logg.info('computing moments based on ' + str(mode), r=True) connectivities = get_connectivities(adata, mode, n_neighbors=n_neighbors, recurse_neighbors=recurse_neighbors) adata.layers['Ms'] = csr_matrix.dot(connectivities, csr_matrix(adata.layers['spliced'])).astype(np.float32).A adata.layers['Mu'] = csr_matrix.dot(connectivities, csr_matrix(adata.layers['unspliced'])).astype(np.float32).A if renormalize: normalize_per_cell(adata, layers={'Ms', 'Mu'}, enforce=True) logg.info(' finished', time=True, end=' ' if settings.verbosity > 2 else '\n') logg.hint( 'added \n' ' \'Ms\' and \'Mu\', moments of spliced/unspliced abundances (adata.layers)') return adata if copy else None
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 41; 2, function_name:transition_matrix; 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:adata; 5, default_parameter; 5, 6; 5, 7; 6, identifier:vkey; 7, string:'velocity'; 8, default_parameter; 8, 9; 8, 10; 9, identifier:basis; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:backward; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:self_transitions; 16, True; 17, default_parameter; 17, 18; 17, 19; 18, identifier:scale; 19, integer:10; 20, default_parameter; 20, 21; 20, 22; 21, identifier:perc; 22, None; 23, default_parameter; 23, 24; 23, 25; 24, identifier:use_negative_cosines; 25, False; 26, default_parameter; 26, 27; 26, 28; 27, identifier:weight_diffusion; 28, integer:0; 29, default_parameter; 29, 30; 29, 31; 30, identifier:scale_diffusion; 31, integer:1; 32, default_parameter; 32, 33; 32, 34; 33, identifier:weight_indirect_neighbors; 34, None; 35, default_parameter; 35, 36; 35, 37; 36, identifier:n_neighbors; 37, None; 38, default_parameter; 38, 39; 38, 40; 39, identifier:vgraph; 40, None; 41, block; 41, 42; 41, 44; 41, 58; 41, 84; 41, 132; 41, 143; 41, 144; 41, 201; 41, 202; 41, 262; 41, 271; 41, 278; 41, 304; 41, 339; 41, 481; 42, expression_statement; 42, 43; 43, comment; 44, if_statement; 44, 45; 44, 52; 45, comparison_operator:not; 45, 46; 45, 49; 46, binary_operator:+; 46, 47; 46, 48; 47, identifier:vkey; 48, string:'_graph'; 49, attribute; 49, 50; 49, 51; 50, identifier:adata; 51, identifier:uns; 52, block; 52, 53; 53, raise_statement; 53, 54; 54, call; 54, 55; 54, 56; 55, identifier:ValueError; 56, argument_list; 56, 57; 57, string:'You need to run `tl.velocity_graph` first to compute cosine correlations.'; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:graph; 61, conditional_expression:if; 61, 62; 61, 76; 61, 79; 62, call; 62, 63; 62, 75; 63, attribute; 63, 64; 63, 74; 64, call; 64, 65; 64, 66; 65, identifier:csr_matrix; 66, argument_list; 66, 67; 67, subscript; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:adata; 70, identifier:uns; 71, binary_operator:+; 71, 72; 71, 73; 72, identifier:vkey; 73, string:'_graph'; 74, identifier:copy; 75, argument_list; 76, comparison_operator:is; 76, 77; 76, 78; 77, identifier:vgraph; 78, None; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:vgraph; 82, identifier:copy; 83, argument_list; 84, if_statement; 84, 85; 84, 86; 85, identifier:self_transitions; 86, block; 86, 87; 86, 102; 86, 112; 86, 125; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:confidence; 90, call; 90, 91; 90, 101; 91, attribute; 91, 92; 91, 100; 92, attribute; 92, 93; 92, 99; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:graph; 96, identifier:max; 97, argument_list; 97, 98; 98, integer:1; 99, identifier:A; 100, identifier:flatten; 101, argument_list; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:ub; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:np; 108, identifier:percentile; 109, argument_list; 109, 110; 109, 111; 110, identifier:confidence; 111, integer:98; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:self_prob; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:np; 118, identifier:clip; 119, argument_list; 119, 120; 119, 123; 119, 124; 120, binary_operator:-; 120, 121; 120, 122; 121, identifier:ub; 122, identifier:confidence; 123, integer:0; 124, integer:1; 125, expression_statement; 125, 126; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:graph; 129, identifier:setdiag; 130, argument_list; 130, 131; 131, identifier:self_prob; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:T; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:np; 138, identifier:expm1; 139, argument_list; 139, 140; 140, binary_operator:*; 140, 141; 140, 142; 141, identifier:graph; 142, identifier:scale; 143, comment; 144, if_statement; 144, 145; 144, 156; 145, comparison_operator:in; 145, 146; 145, 149; 146, binary_operator:+; 146, 147; 146, 148; 147, identifier:vkey; 148, string:'_graph_neg'; 149, call; 149, 150; 149, 155; 150, attribute; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:adata; 153, identifier:uns; 154, identifier:keys; 155, argument_list; 156, block; 156, 157; 156, 167; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:graph_neg; 160, subscript; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:adata; 163, identifier:uns; 164, binary_operator:+; 164, 165; 164, 166; 165, identifier:vkey; 166, string:'_graph_neg'; 167, if_statement; 167, 168; 167, 169; 167, 182; 168, identifier:use_negative_cosines; 169, block; 169, 170; 170, expression_statement; 170, 171; 171, augmented_assignment:-=; 171, 172; 171, 173; 172, identifier:T; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:np; 176, identifier:expm1; 177, argument_list; 177, 178; 178, binary_operator:*; 178, 179; 178, 181; 179, unary_operator:-; 179, 180; 180, identifier:graph_neg; 181, identifier:scale; 182, else_clause; 182, 183; 183, block; 183, 184; 183, 195; 184, expression_statement; 184, 185; 185, augmented_assignment:+=; 185, 186; 185, 187; 186, identifier:T; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:np; 190, identifier:expm1; 191, argument_list; 191, 192; 192, binary_operator:*; 192, 193; 192, 194; 193, identifier:graph_neg; 194, identifier:scale; 195, expression_statement; 195, 196; 196, augmented_assignment:+=; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:T; 199, identifier:data; 200, integer:1; 201, comment; 202, if_statement; 202, 203; 202, 220; 203, boolean_operator:and; 203, 204; 203, 217; 204, boolean_operator:and; 204, 205; 204, 214; 205, comparison_operator:in; 205, 206; 205, 207; 206, string:'neighbors'; 207, call; 207, 208; 207, 213; 208, attribute; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:adata; 211, identifier:uns; 212, identifier:keys; 213, argument_list; 214, comparison_operator:is; 214, 215; 214, 216; 215, identifier:weight_indirect_neighbors; 216, None; 217, comparison_operator:<; 217, 218; 217, 219; 218, identifier:weight_indirect_neighbors; 219, integer:1; 220, block; 220, 221; 220, 233; 220, 240; 220, 244; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:direct_neighbors; 224, comparison_operator:>; 224, 225; 224, 232; 225, subscript; 225, 226; 225, 231; 226, subscript; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:adata; 229, identifier:uns; 230, string:'neighbors'; 231, string:'distances'; 232, integer:0; 233, expression_statement; 233, 234; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:direct_neighbors; 237, identifier:setdiag; 238, argument_list; 238, 239; 239, integer:1; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:w; 243, identifier:weight_indirect_neighbors; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 247; 246, identifier:T; 247, binary_operator:+; 247, 248; 247, 251; 248, binary_operator:*; 248, 249; 248, 250; 249, identifier:w; 250, identifier:T; 251, binary_operator:*; 251, 252; 251, 256; 252, parenthesized_expression; 252, 253; 253, binary_operator:-; 253, 254; 253, 255; 254, integer:1; 255, identifier:w; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:direct_neighbors; 259, identifier:multiply; 260, argument_list; 260, 261; 261, identifier:T; 262, if_statement; 262, 263; 262, 264; 263, identifier:backward; 264, block; 264, 265; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 268; 267, identifier:T; 268, attribute; 268, 269; 268, 270; 269, identifier:T; 270, identifier:T; 271, expression_statement; 271, 272; 272, assignment; 272, 273; 272, 274; 273, identifier:T; 274, call; 274, 275; 274, 276; 275, identifier:normalize; 276, argument_list; 276, 277; 277, identifier:T; 278, if_statement; 278, 279; 278, 282; 279, comparison_operator:is; 279, 280; 279, 281; 280, identifier:n_neighbors; 281, None; 282, block; 282, 283; 283, expression_statement; 283, 284; 284, assignment; 284, 285; 284, 286; 285, identifier:T; 286, call; 286, 287; 286, 290; 287, attribute; 287, 288; 287, 289; 288, identifier:T; 289, identifier:multiply; 290, argument_list; 290, 291; 291, call; 291, 292; 291, 293; 292, identifier:get_connectivities; 293, argument_list; 293, 294; 293, 295; 293, 298; 293, 301; 294, identifier:adata; 295, keyword_argument; 295, 296; 295, 297; 296, identifier:mode; 297, string:'distances'; 298, keyword_argument; 298, 299; 298, 300; 299, identifier:n_neighbors; 300, identifier:n_neighbors; 301, keyword_argument; 301, 302; 301, 303; 302, identifier:recurse_neighbors; 303, True; 304, if_statement; 304, 305; 304, 308; 305, comparison_operator:is; 305, 306; 305, 307; 306, identifier:perc; 307, None; 308, block; 308, 309; 308, 321; 308, 333; 309, expression_statement; 309, 310; 310, assignment; 310, 311; 310, 312; 311, identifier:threshold; 312, call; 312, 313; 312, 316; 313, attribute; 313, 314; 313, 315; 314, identifier:np; 315, identifier:percentile; 316, argument_list; 316, 317; 316, 320; 317, attribute; 317, 318; 317, 319; 318, identifier:T; 319, identifier:data; 320, identifier:perc; 321, expression_statement; 321, 322; 322, assignment; 322, 323; 322, 332; 323, subscript; 323, 324; 323, 327; 324, attribute; 324, 325; 324, 326; 325, identifier:T; 326, identifier:data; 327, comparison_operator:<; 327, 328; 327, 331; 328, attribute; 328, 329; 328, 330; 329, identifier:T; 330, identifier:data; 331, identifier:threshold; 332, integer:0; 333, expression_statement; 333, 334; 334, call; 334, 335; 334, 338; 335, attribute; 335, 336; 335, 337; 336, identifier:T; 337, identifier:eliminate_zeros; 338, argument_list; 339, if_statement; 339, 340; 339, 354; 340, comparison_operator:in; 340, 341; 340, 347; 341, binary_operator:+; 341, 342; 341, 343; 342, string:'X_'; 343, call; 343, 344; 343, 345; 344, identifier:str; 345, argument_list; 345, 346; 346, identifier:basis; 347, call; 347, 348; 347, 353; 348, attribute; 348, 349; 348, 352; 349, attribute; 349, 350; 349, 351; 350, identifier:adata; 351, identifier:obsm; 352, identifier:keys; 353, argument_list; 354, block; 354, 355; 354, 379; 354, 389; 354, 397; 354, 419; 354, 428; 354, 429; 354, 474; 355, expression_statement; 355, 356; 356, assignment; 356, 357; 356, 358; 357, identifier:dists_emb; 358, call; 358, 359; 358, 365; 359, attribute; 359, 360; 359, 364; 360, parenthesized_expression; 360, 361; 361, comparison_operator:>; 361, 362; 361, 363; 362, identifier:T; 363, integer:0; 364, identifier:multiply; 365, argument_list; 365, 366; 366, call; 366, 367; 366, 368; 367, identifier:squareform; 368, argument_list; 368, 369; 369, call; 369, 370; 369, 371; 370, identifier:pdist; 371, argument_list; 371, 372; 372, subscript; 372, 373; 372, 376; 373, attribute; 373, 374; 373, 375; 374, identifier:adata; 375, identifier:obsm; 376, binary_operator:+; 376, 377; 376, 378; 377, string:'X_'; 378, identifier:basis; 379, expression_statement; 379, 380; 380, augmented_assignment:*=; 380, 381; 380, 382; 381, identifier:scale_diffusion; 382, call; 382, 383; 382, 388; 383, attribute; 383, 384; 383, 387; 384, attribute; 384, 385; 384, 386; 385, identifier:dists_emb; 386, identifier:data; 387, identifier:mean; 388, argument_list; 389, expression_statement; 389, 390; 390, assignment; 390, 391; 390, 392; 391, identifier:diffusion_kernel; 392, call; 392, 393; 392, 396; 393, attribute; 393, 394; 393, 395; 394, identifier:dists_emb; 395, identifier:copy; 396, argument_list; 397, expression_statement; 397, 398; 398, assignment; 398, 399; 398, 402; 399, attribute; 399, 400; 399, 401; 400, identifier:diffusion_kernel; 401, identifier:data; 402, call; 402, 403; 402, 406; 403, attribute; 403, 404; 403, 405; 404, identifier:np; 405, identifier:exp; 406, argument_list; 406, 407; 407, binary_operator:/; 407, 408; 407, 416; 408, binary_operator:*; 408, 409; 408, 411; 409, unary_operator:-; 409, 410; 410, float:.5; 411, binary_operator:**; 411, 412; 411, 415; 412, attribute; 412, 413; 412, 414; 413, identifier:dists_emb; 414, identifier:data; 415, integer:2; 416, binary_operator:**; 416, 417; 416, 418; 417, identifier:scale_diffusion; 418, integer:2; 419, expression_statement; 419, 420; 420, assignment; 420, 421; 420, 422; 421, identifier:T; 422, call; 422, 423; 422, 426; 423, attribute; 423, 424; 423, 425; 424, identifier:T; 425, identifier:multiply; 426, argument_list; 426, 427; 427, identifier:diffusion_kernel; 428, comment; 429, if_statement; 429, 430; 429, 434; 429, 435; 430, comparison_operator:<; 430, 431; 430, 432; 430, 433; 431, integer:0; 432, identifier:weight_diffusion; 433, integer:1; 434, comment; 435, block; 435, 436; 435, 461; 436, expression_statement; 436, 437; 437, assignment; 437, 438; 437, 441; 438, attribute; 438, 439; 438, 440; 439, identifier:diffusion_kernel; 440, identifier:data; 441, call; 441, 442; 441, 445; 442, attribute; 442, 443; 442, 444; 443, identifier:np; 444, identifier:exp; 445, argument_list; 445, 446; 446, binary_operator:/; 446, 447; 446, 455; 447, binary_operator:*; 447, 448; 447, 450; 448, unary_operator:-; 448, 449; 449, float:.5; 450, binary_operator:**; 450, 451; 450, 454; 451, attribute; 451, 452; 451, 453; 452, identifier:dists_emb; 453, identifier:data; 454, integer:2; 455, binary_operator:**; 455, 456; 455, 460; 456, parenthesized_expression; 456, 457; 457, binary_operator:/; 457, 458; 457, 459; 458, identifier:scale_diffusion; 459, integer:2; 460, integer:2; 461, expression_statement; 461, 462; 462, assignment; 462, 463; 462, 464; 463, identifier:T; 464, binary_operator:+; 464, 465; 464, 471; 465, binary_operator:*; 465, 466; 465, 470; 466, parenthesized_expression; 466, 467; 467, binary_operator:-; 467, 468; 467, 469; 468, integer:1; 469, identifier:weight_diffusion; 470, identifier:T; 471, binary_operator:*; 471, 472; 471, 473; 472, identifier:weight_diffusion; 473, identifier:diffusion_kernel; 474, expression_statement; 474, 475; 475, assignment; 475, 476; 475, 477; 476, identifier:T; 477, call; 477, 478; 477, 479; 478, identifier:normalize; 479, argument_list; 479, 480; 480, identifier:T; 481, return_statement; 481, 482; 482, identifier:T
def transition_matrix(adata, vkey='velocity', basis=None, backward=False, self_transitions=True, scale=10, perc=None, use_negative_cosines=False, weight_diffusion=0, scale_diffusion=1, weight_indirect_neighbors=None, n_neighbors=None, vgraph=None): """Computes transition probabilities from velocity graph Arguments --------- adata: :class:`~anndata.AnnData` Annotated data matrix. vkey: `str` (default: `'velocity'`) Name of velocity estimates to be used. basis: `str` or `None` (default: `None`) Restrict transition to embedding if specified backward: `bool` (default: `False`) Whether to use the transition matrix to push forward (`False`) or to pull backward (`True`) scale: `float` (default: 10) Scale parameter of gaussian kernel. weight_diffusion: `float` (default: 0) Relative weight to be given to diffusion kernel (Brownian motion) scale_diffusion: `float` (default: 1) Scale of diffusion kernel. Returns ------- Returns sparse matrix with transition probabilities. """ if vkey+'_graph' not in adata.uns: raise ValueError('You need to run `tl.velocity_graph` first to compute cosine correlations.') graph = csr_matrix(adata.uns[vkey + '_graph']).copy() if vgraph is None else vgraph.copy() if self_transitions: confidence = graph.max(1).A.flatten() ub = np.percentile(confidence, 98) self_prob = np.clip(ub - confidence, 0, 1) graph.setdiag(self_prob) T = np.expm1(graph * scale) # equivalent to np.exp(graph.A * scale) - 1 if vkey + '_graph_neg' in adata.uns.keys(): graph_neg = adata.uns[vkey + '_graph_neg'] if use_negative_cosines: T -= np.expm1(-graph_neg * scale) else: T += np.expm1(graph_neg * scale) T.data += 1 # weight direct and indirect (recursed) neighbors if 'neighbors' in adata.uns.keys() and weight_indirect_neighbors is not None and weight_indirect_neighbors < 1: direct_neighbors = adata.uns['neighbors']['distances'] > 0 direct_neighbors.setdiag(1) w = weight_indirect_neighbors T = w * T + (1-w) * direct_neighbors.multiply(T) if backward: T = T.T T = normalize(T) if n_neighbors is not None: T = T.multiply(get_connectivities(adata, mode='distances', n_neighbors=n_neighbors, recurse_neighbors=True)) if perc is not None: threshold = np.percentile(T.data, perc) T.data[T.data < threshold] = 0 T.eliminate_zeros() if 'X_' + str(basis) in adata.obsm.keys(): dists_emb = (T > 0).multiply(squareform(pdist(adata.obsm['X_' + basis]))) scale_diffusion *= dists_emb.data.mean() diffusion_kernel = dists_emb.copy() diffusion_kernel.data = np.exp(-.5 * dists_emb.data ** 2 / scale_diffusion ** 2) T = T.multiply(diffusion_kernel) # combine velocity based kernel with diffusion based kernel if 0 < weight_diffusion < 1: # add another diffusion kernel (Brownian motion - like) diffusion_kernel.data = np.exp(-.5 * dists_emb.data ** 2 / (scale_diffusion/2) ** 2) T = (1-weight_diffusion) * T + weight_diffusion * diffusion_kernel T = normalize(T) return T
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:update_holder; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:holder; 6, block; 6, 7; 6, 9; 6, 17; 6, 18; 6, 19; 6, 197; 6, 198; 6, 199; 6, 200; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:subject_symbol; 12, attribute; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:subject; 16, identifier:symbol; 17, comment; 18, comment; 19, if_statement; 19, 20; 19, 25; 19, 26; 19, 27; 19, 170; 19, 171; 19, 172; 19, 173; 20, comparison_operator:in; 20, 21; 20, 22; 21, identifier:subject_symbol; 22, attribute; 22, 23; 22, 24; 23, identifier:holder; 24, identifier:state; 25, comment; 26, comment; 27, block; 27, 28; 27, 155; 27, 156; 28, if_statement; 28, 29; 28, 37; 28, 52; 28, 53; 28, 54; 28, 55; 28, 56; 28, 57; 28, 110; 28, 111; 28, 112; 28, 113; 28, 114; 28, 115; 28, 116; 29, not_operator; 29, 30; 30, subscript; 30, 31; 30, 36; 31, subscript; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:holder; 34, identifier:state; 35, identifier:subject_symbol; 36, string:'quantity'; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 47; 40, subscript; 40, 41; 40, 46; 41, subscript; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:holder; 44, identifier:state; 45, identifier:subject_symbol; 46, string:'value'; 47, subscript; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:details; 51, string:'value'; 52, comment; 53, comment; 54, comment; 55, comment; 56, comment; 57, elif_clause; 57, 58; 57, 73; 58, call; 58, 59; 58, 60; 59, identifier:same_sign; 60, argument_list; 60, 61; 60, 68; 61, subscript; 61, 62; 61, 67; 62, subscript; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:holder; 65, identifier:state; 66, identifier:subject_symbol; 67, string:'quantity'; 68, subscript; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:details; 72, string:'quantity'; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 83; 76, subscript; 76, 77; 76, 82; 77, subscript; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:holder; 80, identifier:state; 81, identifier:subject_symbol; 82, string:'value'; 83, call; 83, 84; 83, 85; 84, identifier:average_price; 85, argument_list; 85, 86; 85, 93; 85, 100; 85, 105; 86, subscript; 86, 87; 86, 92; 87, subscript; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:holder; 90, identifier:state; 91, identifier:subject_symbol; 92, string:'quantity'; 93, subscript; 93, 94; 93, 99; 94, subscript; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:holder; 97, identifier:state; 98, identifier:subject_symbol; 99, string:'value'; 100, subscript; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:self; 103, identifier:details; 104, string:'quantity'; 105, subscript; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:self; 108, identifier:details; 109, string:'value'; 110, comment; 111, comment; 112, comment; 113, comment; 114, comment; 115, comment; 116, else_clause; 116, 117; 117, block; 117, 118; 118, if_statement; 118, 119; 118, 140; 119, call; 119, 120; 119, 121; 120, identifier:same_sign; 121, argument_list; 121, 122; 121, 127; 122, subscript; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:details; 126, string:'quantity'; 127, binary_operator:+; 127, 128; 127, 135; 128, subscript; 128, 129; 128, 134; 129, subscript; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:holder; 132, identifier:state; 133, identifier:subject_symbol; 134, string:'quantity'; 135, subscript; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:self; 138, identifier:details; 139, string:'quantity'; 140, block; 140, 141; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 150; 143, subscript; 143, 144; 143, 149; 144, subscript; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:holder; 147, identifier:state; 148, identifier:subject_symbol; 149, string:'value'; 150, subscript; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:self; 153, identifier:details; 154, string:'value'; 155, comment; 156, expression_statement; 156, 157; 157, augmented_assignment:+=; 157, 158; 157, 165; 158, subscript; 158, 159; 158, 164; 159, subscript; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:holder; 162, identifier:state; 163, identifier:subject_symbol; 164, string:'quantity'; 165, subscript; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:self; 168, identifier:details; 169, string:'quantity'; 170, comment; 171, comment; 172, comment; 173, else_clause; 173, 174; 174, block; 174, 175; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 182; 177, subscript; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:holder; 180, identifier:state; 181, identifier:subject_symbol; 182, dictionary; 182, 183; 182, 190; 183, pair; 183, 184; 183, 185; 184, string:'quantity'; 185, subscript; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:self; 188, identifier:details; 189, string:'quantity'; 190, pair; 190, 191; 190, 192; 191, string:'value'; 192, subscript; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:self; 195, identifier:details; 196, string:'value'; 197, comment; 198, comment; 199, comment; 200, if_statement; 200, 201; 200, 209; 201, not_operator; 201, 202; 202, subscript; 202, 203; 202, 208; 203, subscript; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:holder; 206, identifier:state; 207, identifier:subject_symbol; 208, string:'quantity'; 209, block; 209, 210; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 219; 212, subscript; 212, 213; 212, 218; 213, subscript; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:holder; 216, identifier:state; 217, identifier:subject_symbol; 218, string:'value'; 219, integer:0
def update_holder(self, holder): """Udpate the Holder state according to the occurrence. This implementation is a example of how a Occurrence object can update the Holder state; this method should be overriden by classes that inherit from the Occurrence class. This sample implementation simply update the quantity and the average price of the Subject in the Holder's possession every time objects from this class are passed to Holder.trade(). This sample implementation considers the following signature for the Holder.state dict: .. code:: python { "SUBJECT SYMBOL": { "quantity": 0, "value": 0 } } And the following signature for the Occurrance.details dict: .. code:: python { "quantity": 0, "value": 0 } """ subject_symbol = self.subject.symbol # If the Holder already have a state regarding this Subject, # update that state if subject_symbol in holder.state: # If the Holder have zero units of this subject, the average # value paid/received for the subject is the value of the trade itself if not holder.state[subject_symbol]['quantity']: holder.state[subject_symbol]['value'] = self.details['value'] # If the Holder owns units of this subject then the average value # paid/received for the subject may need to be updated with # this occurrence details # If the occurrence have the same sign as the quantity in the Holder # state, a new average value needs to be calculated for the subject elif same_sign( holder.state[subject_symbol]['quantity'], self.details['quantity']): holder.state[subject_symbol]['value'] = average_price( holder.state[subject_symbol]['quantity'], holder.state[subject_symbol]['value'], self.details['quantity'], self.details['value'] ) # If the occurrence does not have the same sign of the quantity in the # Holder state, then do other stuff. # A trade app would normally implement some sort of profit/loss logic # here. # This sample implementation only checks if the average value # of the subject needs to be updated and then update it as needed. else: if same_sign( self.details['quantity'], holder.state[subject_symbol]['quantity'] + self.details['quantity']): holder.state[subject_symbol]['value'] = self.details['value'] # Update the quantity of the subject in the Holder's posession holder.state[subject_symbol]['quantity'] += self.details['quantity'] # If the Holder don't have a state with this occurrence's Subject, # then register this occurrence as the first state of the Subject # in the Holder's possession else: holder.state[subject_symbol] = { 'quantity': self.details['quantity'], 'value': self.details['value'] } # If the Holder knows about this Subject but don't have any unit # of it, the paid value of the subject in the Holder state should # be zero. if not holder.state[subject_symbol]['quantity']: holder.state[subject_symbol]['value'] = 0
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:addImagingColumns; 3, parameters; 3, 4; 3, 5; 4, identifier:msname; 5, default_parameter; 5, 6; 5, 7; 6, identifier:ack; 7, True; 8, block; 8, 9; 8, 11; 8, 12; 8, 17; 8, 18; 8, 31; 8, 39; 8, 40; 8, 58; 8, 59; 8, 63; 8, 95; 8, 96; 8, 116; 8, 117; 8, 168; 8, 219; 8, 343; 8, 344; 8, 362; 8, 363; 8, 364; 8, 379; 8, 388; 8, 398; 8, 412; 8, 422; 8, 423; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, import_statement; 12, 13; 13, aliased_import; 13, 14; 13, 16; 14, dotted_name; 14, 15; 15, identifier:numpy; 16, identifier:np; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:t; 21, call; 21, 22; 21, 23; 22, identifier:table; 23, argument_list; 23, 24; 23, 25; 23, 28; 24, identifier:msname; 25, keyword_argument; 25, 26; 25, 27; 26, identifier:readonly; 27, False; 28, keyword_argument; 28, 29; 28, 30; 29, identifier:ack; 30, False; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:cnames; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:t; 37, identifier:colnames; 38, argument_list; 39, comment; 40, try_statement; 40, 41; 40, 51; 41, block; 41, 42; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:cdesc; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:t; 48, identifier:getcoldesc; 49, argument_list; 49, 50; 50, string:'DATA'; 51, except_clause; 51, 52; 52, block; 52, 53; 53, raise_statement; 53, 54; 54, call; 54, 55; 54, 56; 55, identifier:ValueError; 56, argument_list; 56, 57; 57, string:'Column DATA does not exist'; 58, comment; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:hasTiled; 62, False; 63, try_statement; 63, 64; 63, 89; 64, block; 64, 65; 64, 74; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:dminfo; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:t; 71, identifier:getdminfo; 72, argument_list; 72, 73; 73, string:"DATA"; 74, if_statement; 74, 75; 74, 84; 75, comparison_operator:==; 75, 76; 75, 83; 76, subscript; 76, 77; 76, 80; 77, subscript; 77, 78; 77, 79; 78, identifier:dminfo; 79, string:'TYPE'; 80, slice; 80, 81; 80, 82; 81, colon; 82, integer:5; 83, string:'Tiled'; 84, block; 84, 85; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:hasTiled; 88, True; 89, except_clause; 89, 90; 90, block; 90, 91; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:hasTiled; 94, False; 95, comment; 96, if_statement; 96, 97; 96, 99; 97, not_operator; 97, 98; 98, identifier:hasTiled; 99, block; 99, 100; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:dminfo; 103, dictionary; 103, 104; 103, 107; 104, pair; 104, 105; 104, 106; 105, string:'TYPE'; 106, string:'TiledShapeStMan'; 107, pair; 107, 108; 107, 109; 108, string:'SPEC'; 109, dictionary; 109, 110; 110, pair; 110, 111; 110, 112; 111, string:'DEFAULTTILESHAPE'; 112, list:[4, 32, 128]; 112, 113; 112, 114; 112, 115; 113, integer:4; 114, integer:32; 115, integer:128; 116, comment; 117, if_statement; 117, 118; 117, 121; 117, 129; 118, comparison_operator:in; 118, 119; 118, 120; 119, string:'MODEL_DATA'; 120, identifier:cnames; 121, block; 121, 122; 122, expression_statement; 122, 123; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:six; 126, identifier:print_; 127, argument_list; 127, 128; 128, string:"Column MODEL_DATA not added; it already exists"; 129, else_clause; 129, 130; 130, block; 130, 131; 130, 137; 130, 143; 130, 158; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 136; 133, subscript; 133, 134; 133, 135; 134, identifier:dminfo; 135, string:'NAME'; 136, string:'modeldata'; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 142; 139, subscript; 139, 140; 139, 141; 140, identifier:cdesc; 141, string:'comment'; 142, string:'The model data column'; 143, expression_statement; 143, 144; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:t; 147, identifier:addcols; 148, argument_list; 148, 149; 148, 157; 149, call; 149, 150; 149, 151; 150, identifier:maketabdesc; 151, argument_list; 151, 152; 152, call; 152, 153; 152, 154; 153, identifier:makecoldesc; 154, argument_list; 154, 155; 154, 156; 155, string:'MODEL_DATA'; 156, identifier:cdesc; 157, identifier:dminfo; 158, if_statement; 158, 159; 158, 160; 159, identifier:ack; 160, block; 160, 161; 161, expression_statement; 161, 162; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:six; 165, identifier:print_; 166, argument_list; 166, 167; 167, string:"added column MODEL_DATA"; 168, if_statement; 168, 169; 168, 172; 168, 180; 169, comparison_operator:in; 169, 170; 169, 171; 170, string:'CORRECTED_DATA'; 171, identifier:cnames; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:six; 177, identifier:print_; 178, argument_list; 178, 179; 179, string:"Column CORRECTED_DATA not added; it already exists"; 180, else_clause; 180, 181; 181, block; 181, 182; 181, 188; 181, 194; 181, 209; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 187; 184, subscript; 184, 185; 184, 186; 185, identifier:dminfo; 186, string:'NAME'; 187, string:'correcteddata'; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 193; 190, subscript; 190, 191; 190, 192; 191, identifier:cdesc; 192, string:'comment'; 193, string:'The corrected data column'; 194, expression_statement; 194, 195; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:t; 198, identifier:addcols; 199, argument_list; 199, 200; 199, 208; 200, call; 200, 201; 200, 202; 201, identifier:maketabdesc; 202, argument_list; 202, 203; 203, call; 203, 204; 203, 205; 204, identifier:makecoldesc; 205, argument_list; 205, 206; 205, 207; 206, string:'CORRECTED_DATA'; 207, identifier:cdesc; 208, identifier:dminfo; 209, if_statement; 209, 210; 209, 211; 210, identifier:ack; 211, block; 211, 212; 212, expression_statement; 212, 213; 213, call; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:six; 216, identifier:print_; 217, argument_list; 217, 218; 218, string:"'added column CORRECTED_DATA"; 219, if_statement; 219, 220; 219, 223; 219, 231; 220, comparison_operator:in; 220, 221; 220, 222; 221, string:'IMAGING_WEIGHT'; 222, identifier:cnames; 223, block; 223, 224; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:six; 228, identifier:print_; 229, argument_list; 229, 230; 230, string:"Column IMAGING_WEIGHT not added; it already exists"; 231, else_clause; 231, 232; 231, 233; 231, 234; 232, comment; 233, comment; 234, block; 234, 235; 234, 239; 234, 250; 234, 284; 234, 301; 234, 316; 234, 322; 234, 333; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 238; 237, identifier:shp; 238, list:[]; 239, if_statement; 239, 240; 239, 243; 240, comparison_operator:in; 240, 241; 240, 242; 241, string:'shape'; 242, identifier:cdesc; 243, block; 243, 244; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 247; 246, identifier:shp; 247, subscript; 247, 248; 247, 249; 248, identifier:cdesc; 249, string:'shape'; 250, if_statement; 250, 251; 250, 257; 250, 266; 251, comparison_operator:>; 251, 252; 251, 256; 252, call; 252, 253; 252, 254; 253, identifier:len; 254, argument_list; 254, 255; 255, identifier:shp; 256, integer:0; 257, block; 257, 258; 257, 265; 258, expression_statement; 258, 259; 259, assignment; 259, 260; 259, 261; 260, identifier:shp; 261, list:[shp[0]]; 261, 262; 262, subscript; 262, 263; 262, 264; 263, identifier:shp; 264, integer:0; 265, comment; 266, else_clause; 266, 267; 267, block; 267, 268; 267, 283; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 271; 270, identifier:shp; 271, list:[t.getcell('DATA', 0).shape[0]]; 271, 272; 272, subscript; 272, 273; 272, 282; 273, attribute; 273, 274; 273, 281; 274, call; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:t; 277, identifier:getcell; 278, argument_list; 278, 279; 278, 280; 279, string:'DATA'; 280, integer:0; 281, identifier:shape; 282, integer:0; 283, comment; 284, expression_statement; 284, 285; 285, assignment; 285, 286; 285, 287; 286, identifier:cd; 287, call; 287, 288; 287, 289; 288, identifier:makearrcoldesc; 289, argument_list; 289, 290; 289, 291; 289, 292; 289, 295; 289, 298; 290, string:'IMAGING_WEIGHT'; 291, integer:0; 292, keyword_argument; 292, 293; 292, 294; 293, identifier:ndim; 294, integer:1; 295, keyword_argument; 295, 296; 295, 297; 296, identifier:shape; 297, identifier:shp; 298, keyword_argument; 298, 299; 298, 300; 299, identifier:valuetype; 300, string:'float'; 301, expression_statement; 301, 302; 302, assignment; 302, 303; 302, 304; 303, identifier:dminfo; 304, dictionary; 304, 305; 304, 308; 305, pair; 305, 306; 305, 307; 306, string:'TYPE'; 307, string:'TiledShapeStMan'; 308, pair; 308, 309; 308, 310; 309, string:'SPEC'; 310, dictionary; 310, 311; 311, pair; 311, 312; 311, 313; 312, string:'DEFAULTTILESHAPE'; 313, list:[32, 128]; 313, 314; 313, 315; 314, integer:32; 315, integer:128; 316, expression_statement; 316, 317; 317, assignment; 317, 318; 317, 321; 318, subscript; 318, 319; 318, 320; 319, identifier:dminfo; 320, string:'NAME'; 321, string:'imagingweight'; 322, expression_statement; 322, 323; 323, call; 323, 324; 323, 327; 324, attribute; 324, 325; 324, 326; 325, identifier:t; 326, identifier:addcols; 327, argument_list; 327, 328; 327, 332; 328, call; 328, 329; 328, 330; 329, identifier:maketabdesc; 330, argument_list; 330, 331; 331, identifier:cd; 332, identifier:dminfo; 333, if_statement; 333, 334; 333, 335; 334, identifier:ack; 335, block; 335, 336; 336, expression_statement; 336, 337; 337, call; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, identifier:six; 340, identifier:print_; 341, argument_list; 341, 342; 342, string:"added column IMAGING_WEIGHT"; 343, comment; 344, if_statement; 344, 345; 344, 353; 345, comparison_operator:in; 345, 346; 345, 347; 346, string:'CHANNEL_SELECTION'; 347, call; 347, 348; 347, 351; 348, attribute; 348, 349; 348, 350; 349, identifier:t; 350, identifier:colkeywordnames; 351, argument_list; 351, 352; 352, string:'MODEL_DATA'; 353, block; 353, 354; 354, expression_statement; 354, 355; 355, call; 355, 356; 355, 359; 356, attribute; 356, 357; 356, 358; 357, identifier:t; 358, identifier:removecolkeyword; 359, argument_list; 359, 360; 359, 361; 360, string:'MODEL_DATA'; 361, string:'CHANNEL_SELECTION'; 362, comment; 363, comment; 364, expression_statement; 364, 365; 365, assignment; 365, 366; 365, 367; 366, identifier:tspw; 367, call; 367, 368; 367, 369; 368, identifier:table; 369, argument_list; 369, 370; 369, 376; 370, call; 370, 371; 370, 374; 371, attribute; 371, 372; 371, 373; 372, identifier:t; 373, identifier:getkeyword; 374, argument_list; 374, 375; 375, string:'SPECTRAL_WINDOW'; 376, keyword_argument; 376, 377; 376, 378; 377, identifier:ack; 378, False; 379, expression_statement; 379, 380; 380, assignment; 380, 381; 380, 382; 381, identifier:nchans; 382, call; 382, 383; 382, 386; 383, attribute; 383, 384; 383, 385; 384, identifier:tspw; 385, identifier:getcol; 386, argument_list; 386, 387; 387, string:'NUM_CHAN'; 388, expression_statement; 388, 389; 389, assignment; 389, 390; 389, 391; 390, identifier:chans; 391, list_comprehension; 391, 392; 391, 395; 392, list:[0, nch]; 392, 393; 392, 394; 393, integer:0; 394, identifier:nch; 395, for_in_clause; 395, 396; 395, 397; 396, identifier:nch; 397, identifier:nchans; 398, expression_statement; 398, 399; 399, call; 399, 400; 399, 403; 400, attribute; 400, 401; 400, 402; 401, identifier:t; 402, identifier:putcolkeyword; 403, argument_list; 403, 404; 403, 405; 403, 406; 404, string:'MODEL_DATA'; 405, string:'CHANNEL_SELECTION'; 406, call; 406, 407; 406, 410; 407, attribute; 407, 408; 407, 409; 408, identifier:np; 409, identifier:int32; 410, argument_list; 410, 411; 411, identifier:chans; 412, if_statement; 412, 413; 412, 414; 413, identifier:ack; 414, block; 414, 415; 415, expression_statement; 415, 416; 416, call; 416, 417; 416, 420; 417, attribute; 417, 418; 417, 419; 418, identifier:six; 419, identifier:print_; 420, argument_list; 420, 421; 421, string:"defined keyword CHANNEL_SELECTION in column MODEL_DATA"; 422, comment; 423, expression_statement; 423, 424; 424, call; 424, 425; 424, 428; 425, attribute; 425, 426; 425, 427; 426, identifier:t; 427, identifier:flush; 428, argument_list
def addImagingColumns(msname, ack=True): """ Add the columns to an MS needed for the casa imager. It adds the columns MODEL_DATA, CORRECTED_DATA, and IMAGING_WEIGHT. It also sets the CHANNEL_SELECTION keyword needed for the older casa imagers. A column is not added if already existing. """ # numpy is needed import numpy as np # Open the MS t = table(msname, readonly=False, ack=False) cnames = t.colnames() # Get the description of the DATA column. try: cdesc = t.getcoldesc('DATA') except: raise ValueError('Column DATA does not exist') # Determine if the DATA storage specification is tiled. hasTiled = False try: dminfo = t.getdminfo("DATA") if dminfo['TYPE'][:5] == 'Tiled': hasTiled = True except: hasTiled = False # Use TiledShapeStMan if needed. if not hasTiled: dminfo = {'TYPE': 'TiledShapeStMan', 'SPEC': {'DEFAULTTILESHAPE': [4, 32, 128]}} # Add the columns(if not existing). Use the description of the DATA column. if 'MODEL_DATA' in cnames: six.print_("Column MODEL_DATA not added; it already exists") else: dminfo['NAME'] = 'modeldata' cdesc['comment'] = 'The model data column' t.addcols(maketabdesc(makecoldesc('MODEL_DATA', cdesc)), dminfo) if ack: six.print_("added column MODEL_DATA") if 'CORRECTED_DATA' in cnames: six.print_("Column CORRECTED_DATA not added; it already exists") else: dminfo['NAME'] = 'correcteddata' cdesc['comment'] = 'The corrected data column' t.addcols(maketabdesc(makecoldesc('CORRECTED_DATA', cdesc)), dminfo) if ack: six.print_("'added column CORRECTED_DATA") if 'IMAGING_WEIGHT' in cnames: six.print_("Column IMAGING_WEIGHT not added; it already exists") else: # Add IMAGING_WEIGHT which is 1-dim and has type float. # It needs a shape, otherwise the CASA imager complains. shp = [] if 'shape' in cdesc: shp = cdesc['shape'] if len(shp) > 0: shp = [shp[0]] # use nchan from shape else: shp = [t.getcell('DATA', 0).shape[0]] # use nchan from actual data cd = makearrcoldesc('IMAGING_WEIGHT', 0, ndim=1, shape=shp, valuetype='float') dminfo = {'TYPE': 'TiledShapeStMan', 'SPEC': {'DEFAULTTILESHAPE': [32, 128]}} dminfo['NAME'] = 'imagingweight' t.addcols(maketabdesc(cd), dminfo) if ack: six.print_("added column IMAGING_WEIGHT") # Add or overwrite keyword CHANNEL_SELECTION. if 'CHANNEL_SELECTION' in t.colkeywordnames('MODEL_DATA'): t.removecolkeyword('MODEL_DATA', 'CHANNEL_SELECTION') # Define the CHANNEL_SELECTION keyword containing the channels of # all spectral windows. tspw = table(t.getkeyword('SPECTRAL_WINDOW'), ack=False) nchans = tspw.getcol('NUM_CHAN') chans = [[0, nch] for nch in nchans] t.putcolkeyword('MODEL_DATA', 'CHANNEL_SELECTION', np.int32(chans)) if ack: six.print_("defined keyword CHANNEL_SELECTION in column MODEL_DATA") # Flush the table to make sure it is written. t.flush()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:msregularize; 3, parameters; 3, 4; 3, 5; 4, identifier:msname; 5, identifier:newname; 6, block; 6, 7; 6, 9; 6, 10; 6, 17; 6, 26; 6, 30; 6, 31; 6, 261; 6, 262; 6, 318; 6, 325; 6, 333; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:t; 13, call; 13, 14; 13, 15; 14, identifier:table; 15, argument_list; 15, 16; 16, identifier:msname; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:t1; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:t; 23, identifier:sort; 24, argument_list; 24, 25; 25, string:'unique ANTENNA1,ANTENNA2'; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:nadded; 29, integer:0; 30, comment; 31, for_statement; 31, 32; 31, 33; 31, 41; 32, identifier:tsub; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:t; 36, identifier:iter; 37, argument_list; 37, 38; 38, list:['TIME', 'DATA_DESC_ID']; 38, 39; 38, 40; 39, string:'TIME'; 40, string:'DATA_DESC_ID'; 41, block; 41, 42; 41, 56; 41, 66; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:nmissing; 45, binary_operator:-; 45, 46; 45, 51; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:t1; 49, identifier:nrows; 50, argument_list; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:tsub; 54, identifier:nrows; 55, argument_list; 56, if_statement; 56, 57; 56, 60; 57, comparison_operator:<; 57, 58; 57, 59; 58, identifier:nmissing; 59, integer:0; 60, block; 60, 61; 61, raise_statement; 61, 62; 62, call; 62, 63; 62, 64; 63, identifier:ValueError; 64, argument_list; 64, 65; 65, string:"A time/band chunk has too many rows"; 66, if_statement; 66, 67; 66, 70; 66, 71; 67, comparison_operator:>; 67, 68; 67, 69; 68, identifier:nmissing; 69, integer:0; 70, comment; 71, block; 71, 72; 71, 90; 71, 108; 71, 117; 71, 126; 71, 135; 71, 157; 71, 171; 71, 172; 71, 173; 71, 174; 71, 214; 71, 215; 71, 236; 71, 257; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:ant1; 75, call; 75, 76; 75, 87; 76, attribute; 76, 77; 76, 86; 77, call; 77, 78; 77, 79; 78, identifier:str; 79, argument_list; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:t1; 83, identifier:getcol; 84, argument_list; 84, 85; 85, string:'ANTENNA1'; 86, identifier:replace; 87, argument_list; 87, 88; 87, 89; 88, string:' '; 89, string:','; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:ant2; 93, call; 93, 94; 93, 105; 94, attribute; 94, 95; 94, 104; 95, call; 95, 96; 95, 97; 96, identifier:str; 97, argument_list; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:t1; 101, identifier:getcol; 102, argument_list; 102, 103; 103, string:'ANTENNA2'; 104, identifier:replace; 105, argument_list; 105, 106; 105, 107; 106, string:' '; 107, string:','; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:ant1; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:tsub; 114, identifier:getcol; 115, argument_list; 115, 116; 116, string:'ANTENNA1'; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:ant2; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:tsub; 123, identifier:getcol; 124, argument_list; 124, 125; 125, string:'ANTENNA2'; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:t2; 129, call; 129, 130; 129, 131; 130, identifier:taql; 131, argument_list; 131, 132; 132, binary_operator:+; 132, 133; 132, 134; 133, string:'select from $t1 where !any(ANTENNA1 == $ant1 &&'; 134, string:' ANTENNA2 == $ant2)'; 135, expression_statement; 135, 136; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:six; 139, identifier:print_; 140, argument_list; 140, 141; 140, 142; 140, 147; 140, 152; 141, identifier:nmissing; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:t1; 145, identifier:nrows; 146, argument_list; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:tsub; 150, identifier:nrows; 151, argument_list; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:t2; 155, identifier:nrows; 156, argument_list; 157, if_statement; 157, 158; 157, 165; 158, comparison_operator:!=; 158, 159; 158, 164; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:t2; 162, identifier:nrows; 163, argument_list; 164, identifier:nmissing; 165, block; 165, 166; 166, raise_statement; 166, 167; 167, call; 167, 168; 167, 169; 168, identifier:ValueError; 169, argument_list; 169, 170; 170, string:"A time/band chunk behaves strangely"; 171, comment; 172, comment; 173, comment; 174, if_statement; 174, 175; 174, 178; 174, 205; 175, comparison_operator:==; 175, 176; 175, 177; 176, identifier:nadded; 177, integer:0; 178, block; 178, 179; 178, 193; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:tnew; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:t2; 185, identifier:copy; 186, argument_list; 186, 187; 186, 190; 187, binary_operator:+; 187, 188; 187, 189; 188, identifier:newname; 189, string:"_add"; 190, keyword_argument; 190, 191; 190, 192; 191, identifier:deep; 192, True; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:tnew; 196, call; 196, 197; 196, 198; 197, identifier:table; 198, argument_list; 198, 199; 198, 202; 199, binary_operator:+; 199, 200; 199, 201; 200, identifier:newname; 201, string:"_add"; 202, keyword_argument; 202, 203; 202, 204; 203, identifier:readonly; 204, False; 205, else_clause; 205, 206; 206, block; 206, 207; 207, expression_statement; 207, 208; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:t2; 211, identifier:copyrows; 212, argument_list; 212, 213; 213, identifier:tnew; 214, comment; 215, expression_statement; 215, 216; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:tnew; 219, identifier:putcell; 220, argument_list; 220, 221; 220, 222; 220, 229; 221, string:'TIME'; 222, call; 222, 223; 222, 224; 223, identifier:range; 224, argument_list; 224, 225; 224, 226; 225, identifier:nadded; 226, binary_operator:+; 226, 227; 226, 228; 227, identifier:nadded; 228, identifier:nmissing; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:tsub; 232, identifier:getcell; 233, argument_list; 233, 234; 233, 235; 234, string:'TIME'; 235, integer:0; 236, expression_statement; 236, 237; 237, call; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:tnew; 240, identifier:putcell; 241, argument_list; 241, 242; 241, 243; 241, 250; 242, string:'DATA_DESC_ID'; 243, call; 243, 244; 243, 245; 244, identifier:range; 245, argument_list; 245, 246; 245, 247; 246, identifier:nadded; 247, binary_operator:+; 247, 248; 247, 249; 248, identifier:nadded; 249, identifier:nmissing; 250, call; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:tsub; 253, identifier:getcell; 254, argument_list; 254, 255; 254, 256; 255, string:'DATA_DESC_ID'; 256, integer:0; 257, expression_statement; 257, 258; 258, augmented_assignment:+=; 258, 259; 258, 260; 259, identifier:nadded; 260, identifier:nmissing; 261, comment; 262, if_statement; 262, 263; 262, 266; 262, 267; 262, 305; 263, comparison_operator:>; 263, 264; 263, 265; 264, identifier:nadded; 265, integer:0; 266, comment; 267, block; 267, 268; 267, 273; 267, 278; 267, 287; 267, 296; 268, expression_statement; 268, 269; 269, call; 269, 270; 269, 271; 270, identifier:taql; 271, argument_list; 271, 272; 272, string:'update $tnew set DATA=0+0i'; 273, expression_statement; 273, 274; 274, call; 274, 275; 274, 276; 275, identifier:taql; 276, argument_list; 276, 277; 277, string:'update $tnew set FLAG=True'; 278, expression_statement; 278, 279; 279, assignment; 279, 280; 279, 281; 280, identifier:tcomb; 281, call; 281, 282; 281, 283; 282, identifier:table; 283, argument_list; 283, 284; 284, list:[t, tnew]; 284, 285; 284, 286; 285, identifier:t; 286, identifier:tnew; 287, expression_statement; 287, 288; 288, call; 288, 289; 288, 292; 289, attribute; 289, 290; 289, 291; 290, identifier:tcomb; 291, identifier:rename; 292, argument_list; 292, 293; 293, binary_operator:+; 293, 294; 293, 295; 294, identifier:newname; 295, string:'_adds'; 296, expression_statement; 296, 297; 297, assignment; 297, 298; 297, 299; 298, identifier:tcombs; 299, call; 299, 300; 299, 303; 300, attribute; 300, 301; 300, 302; 301, identifier:tcomb; 302, identifier:sort; 303, argument_list; 303, 304; 304, string:'TIME,DATA_DESC_ID,ANTENNA1,ANTENNA2'; 305, else_clause; 305, 306; 306, block; 306, 307; 307, expression_statement; 307, 308; 308, assignment; 308, 309; 308, 310; 309, identifier:tcombs; 310, call; 310, 311; 310, 314; 311, attribute; 311, 312; 311, 313; 312, identifier:t; 313, identifier:query; 314, argument_list; 314, 315; 315, keyword_argument; 315, 316; 315, 317; 316, identifier:offset; 317, integer:0; 318, expression_statement; 318, 319; 319, call; 319, 320; 319, 323; 320, attribute; 320, 321; 320, 322; 321, identifier:tcombs; 322, identifier:rename; 323, argument_list; 323, 324; 324, identifier:newname; 325, expression_statement; 325, 326; 326, call; 326, 327; 326, 330; 327, attribute; 327, 328; 327, 329; 328, identifier:six; 329, identifier:print_; 330, argument_list; 330, 331; 330, 332; 331, identifier:newname; 332, string:'has been created; it references the original MS'; 333, if_statement; 333, 334; 333, 337; 333, 351; 334, comparison_operator:>; 334, 335; 334, 336; 335, identifier:nadded; 336, integer:0; 337, block; 337, 338; 338, expression_statement; 338, 339; 339, call; 339, 340; 339, 343; 340, attribute; 340, 341; 340, 342; 341, identifier:six; 342, identifier:print_; 343, argument_list; 343, 344; 343, 345; 343, 348; 343, 349; 343, 350; 344, string:' and'; 345, binary_operator:+; 345, 346; 345, 347; 346, identifier:newname; 347, string:'_adds'; 348, string:'containing'; 349, identifier:nadded; 350, string:'new rows'; 351, else_clause; 351, 352; 352, block; 352, 353; 353, expression_statement; 353, 354; 354, call; 354, 355; 354, 358; 355, attribute; 355, 356; 355, 357; 356, identifier:six; 357, identifier:print_; 358, argument_list; 358, 359; 359, string:' no rows needed to be added'
def msregularize(msname, newname): """ Regularize an MS The output MS will be such that it has the same number of baselines for each time stamp. Where needed fully flagged rows are added. Possibly missing rows are written into a separate MS <newname>-add. It is concatenated with the original MS and sorted in order of TIME, DATADESC_ID, ANTENNA1,ANTENNA2 to form a new regular MS. Note that the new MS references the input MS (it does not copy the data). It means that changes made in the new MS are also made in the input MS. If no rows were missing, the new MS is still created referencing the input MS. """ # Find out all baselines. t = table(msname) t1 = t.sort('unique ANTENNA1,ANTENNA2') nadded = 0 # Now iterate in time,band over the MS. for tsub in t.iter(['TIME', 'DATA_DESC_ID']): nmissing = t1.nrows() - tsub.nrows() if nmissing < 0: raise ValueError("A time/band chunk has too many rows") if nmissing > 0: # Rows needs to be added for the missing baselines. ant1 = str(t1.getcol('ANTENNA1')).replace(' ', ',') ant2 = str(t1.getcol('ANTENNA2')).replace(' ', ',') ant1 = tsub.getcol('ANTENNA1') ant2 = tsub.getcol('ANTENNA2') t2 = taql('select from $t1 where !any(ANTENNA1 == $ant1 &&' + ' ANTENNA2 == $ant2)') six.print_(nmissing, t1.nrows(), tsub.nrows(), t2.nrows()) if t2.nrows() != nmissing: raise ValueError("A time/band chunk behaves strangely") # If nothing added yet, create a new table. # (which has to be reopened for read/write). # Otherwise append to that new table. if nadded == 0: tnew = t2.copy(newname + "_add", deep=True) tnew = table(newname + "_add", readonly=False) else: t2.copyrows(tnew) # Set the correct time and band in the new rows. tnew.putcell('TIME', range(nadded, nadded + nmissing), tsub.getcell('TIME', 0)) tnew.putcell('DATA_DESC_ID', range(nadded, nadded + nmissing), tsub.getcell('DATA_DESC_ID', 0)) nadded += nmissing # Combine the existing table and new table. if nadded > 0: # First initialize data and flags in the added rows. taql('update $tnew set DATA=0+0i') taql('update $tnew set FLAG=True') tcomb = table([t, tnew]) tcomb.rename(newname + '_adds') tcombs = tcomb.sort('TIME,DATA_DESC_ID,ANTENNA1,ANTENNA2') else: tcombs = t.query(offset=0) tcombs.rename(newname) six.print_(newname, 'has been created; it references the original MS') if nadded > 0: six.print_(' and', newname + '_adds', 'containing', nadded, 'new rows') else: six.print_(' no rows needed to be added')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 43; 2, function_name:tablefromascii; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 3, 24; 3, 27; 3, 30; 3, 34; 3, 37; 3, 40; 4, identifier:tablename; 5, identifier:asciifile; 6, default_parameter; 6, 7; 6, 8; 7, identifier:headerfile; 8, string:''; 9, default_parameter; 9, 10; 9, 11; 10, identifier:autoheader; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:autoshape; 14, list:[]; 15, default_parameter; 15, 16; 15, 17; 16, identifier:columnnames; 17, list:[]; 18, default_parameter; 18, 19; 18, 20; 19, identifier:datatypes; 20, list:[]; 21, default_parameter; 21, 22; 21, 23; 22, identifier:sep; 23, string:' '; 24, default_parameter; 24, 25; 24, 26; 25, identifier:commentmarker; 26, string:''; 27, default_parameter; 27, 28; 27, 29; 28, identifier:firstline; 29, integer:1; 30, default_parameter; 30, 31; 30, 32; 31, identifier:lastline; 32, unary_operator:-; 32, 33; 33, integer:1; 34, default_parameter; 34, 35; 34, 36; 35, identifier:readonly; 36, True; 37, default_parameter; 37, 38; 37, 39; 38, identifier:lockoptions; 39, string:'default'; 40, default_parameter; 40, 41; 40, 42; 41, identifier:ack; 42, True; 43, block; 43, 44; 43, 46; 43, 50; 43, 61; 43, 72; 43, 95; 43, 145; 43, 169; 43, 184; 43, 185; 43, 189; 44, expression_statement; 44, 45; 45, comment; 46, import_statement; 46, 47; 47, dotted_name; 47, 48; 47, 49; 48, identifier:os; 49, identifier:path; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:filename; 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:expandvars; 59, argument_list; 59, 60; 60, identifier:asciifile; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:filename; 64, call; 64, 65; 64, 70; 65, attribute; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:os; 68, identifier:path; 69, identifier:expanduser; 70, argument_list; 70, 71; 71, identifier:filename; 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:filename; 82, block; 82, 83; 82, 90; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:s; 86, binary_operator:%; 86, 87; 86, 88; 87, string:"File '%s' not found"; 88, parenthesized_expression; 88, 89; 89, identifier:filename; 90, raise_statement; 90, 91; 91, call; 91, 92; 91, 93; 92, identifier:IOError; 93, argument_list; 93, 94; 94, identifier:s; 95, if_statement; 95, 96; 95, 99; 96, comparison_operator:!=; 96, 97; 96, 98; 97, identifier:headerfile; 98, string:''; 99, block; 99, 100; 99, 111; 99, 122; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:filename; 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:expandvars; 109, argument_list; 109, 110; 110, identifier:headerfile; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:filename; 114, call; 114, 115; 114, 120; 115, attribute; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:os; 118, identifier:path; 119, identifier:expanduser; 120, argument_list; 120, 121; 121, identifier:filename; 122, if_statement; 122, 123; 122, 132; 123, not_operator; 123, 124; 124, call; 124, 125; 124, 130; 125, attribute; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:os; 128, identifier:path; 129, identifier:exists; 130, argument_list; 130, 131; 131, identifier:filename; 132, block; 132, 133; 132, 140; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:s; 136, binary_operator:%; 136, 137; 136, 138; 137, string:"File '%s' not found"; 138, parenthesized_expression; 138, 139; 139, identifier:filename; 140, raise_statement; 140, 141; 141, call; 141, 142; 141, 143; 142, identifier:IOError; 143, argument_list; 143, 144; 144, identifier:s; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:tab; 148, call; 148, 149; 148, 150; 149, identifier:table; 150, argument_list; 150, 151; 150, 152; 150, 153; 150, 154; 150, 155; 150, 156; 150, 157; 150, 158; 150, 159; 150, 160; 150, 163; 150, 166; 151, identifier:asciifile; 152, identifier:headerfile; 153, identifier:tablename; 154, identifier:autoheader; 155, identifier:autoshape; 156, identifier:sep; 157, identifier:commentmarker; 158, identifier:firstline; 159, identifier:lastline; 160, keyword_argument; 160, 161; 160, 162; 161, identifier:_columnnames; 162, identifier:columnnames; 163, keyword_argument; 163, 164; 163, 165; 164, identifier:_datatypes; 165, identifier:datatypes; 166, keyword_argument; 166, 167; 166, 168; 167, identifier:_oper; 168, integer:1; 169, expression_statement; 169, 170; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:six; 173, identifier:print_; 174, argument_list; 174, 175; 175, binary_operator:+; 175, 176; 175, 183; 176, binary_operator:+; 176, 177; 176, 178; 177, string:'Input format: ['; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:tab; 181, identifier:_getasciiformat; 182, argument_list; 183, string:']'; 184, comment; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:tab; 188, integer:0; 189, return_statement; 189, 190; 190, call; 190, 191; 190, 192; 191, identifier:table; 192, argument_list; 192, 193; 192, 194; 192, 197; 192, 200; 193, identifier:tablename; 194, keyword_argument; 194, 195; 194, 196; 195, identifier:readonly; 196, identifier:readonly; 197, keyword_argument; 197, 198; 197, 199; 198, identifier:lockoptions; 199, identifier:lockoptions; 200, keyword_argument; 200, 201; 200, 202; 201, identifier:ack; 202, identifier:ack
def tablefromascii(tablename, asciifile, headerfile='', autoheader=False, autoshape=[], columnnames=[], datatypes=[], sep=' ', commentmarker='', firstline=1, lastline=-1, readonly=True, lockoptions='default', ack=True): """Create a table from an ASCII file. Create a table from a file in ASCII format. Columnar data as well as table and column keywords may be specified. Once the table is created from the ASCII data, it is opened in the specified mode and a table object is returned. The table columns are filled from a file containing the data values separated by a separator (one line per table row). The default separator is a blank. Blanks before and after the separator are ignored. If a non-blank separator is used, values can be empty. Such values default to 0, empty string, or F depending on the data type. E.g. 1,,2, has 4 values of which the 2nd and 4th are empty and default to 0. Similarly if fewer values are given than needed, the missing values get the default value. Either the data format can be explicitly specified or it can be found automatically. The former gives more control in ambiguous situations. Both scalar and array columns can be generated from the ASCII input. The format string determines the type and optional shape. It is possible to give the column names and their data types in various ways: - Using 2 header lines (as described below) as the first two lines in the data file or in a separate header file. This is the default way. - Derive them automatically from the data (`autoheader=True`). - Using the arguments `columnnames` and `datatypes` (as non-empty vectors of strings). It implies (`autoheader=False`). The data types should be given in the same way as done in headers. In automatic mode (`autoheader=True`) the first line of the ASCII data is analyzed to deduce the data types. Only the types I, D, and A can be recognized. A number without decimal point or exponent is I (integer), otherwise it is D (double). Any other string is A (string). Note that a number may contain a leading sign (+ or -). The `autoshape` argument can be used to specify if the input should be stored as multiple scalars (the default) or as a single array. In the latter case one axis in the shape can be defined as variable length by giving it the value 0. It means that the actual array shape in a row is determined by the number of values in the corresponding input line. Columns get the names `Column1`, `Column2`, etc.. For example: 1. `autoshape=[]` (which is the default) means that all values are to be stored as scalar columns. 2. `autoshape=0` means that all values in a row are to be stored as a variable length vector. 3. `autoshape=10` defines a fixed length vector. If an input line contains less than 10 values, the vector is filled with default values. If more than 10 values, the latter values are ignored. 4. `autoshape=[5,0]` defines a 2-dim array of which the 2nd axis is variable. Note that if an input line does not contain a multiple of 5 values, the array is filled with default values. If the format of the table is explicitly specified, it has to be done either in the first two lines of the data file (named by the argument filename), or in a separate header file (named by the argument headerfile). In both forms, table keywords may also be specified before the column definitions. The column names and types can be described by two lines: 1. The first line contains the names of the columns. These names may be enclosed in quotes (either single or double). 2. The second line contains the data type and optionally the shape of each column. Valid types are: - S for Short data - I for Integer data - R for Real data - D for Double Precision data - X for Complex data (Real followed by Imaginary) - Z for Complex data (Amplitude then Phase) - DX for Double Precision Complex data (Real followed by Imaginary) - DZ for Double Precision Complex data (Amplitude then Phase) - A for ASCII data (a value must be enclosed in single or double quotes if it contains whitespace) - B for Boolean data (False are empty string, 0, or any string starting with F, f, N, or n). If a column is an array, the shape has to be given after the data type without any whitespace. E.g. `I10` defines an integer vector of length 10. `A2,5` defines a 2-dim string array with shape [2,5]. Note that `I` is not the same as `I1` as the first one defines a scalar and the other one a vector with length 1. The last column can have one variable length axis denoted by the value 0. It "consumes" the remainder of the input line. If the argument headerfile is set then the header information is read from that file instead of the first lines of the data file. To give a simple example of the form where the header information is located at the top of the data file:: COLI COLF COLD COLX COLZ COLS I R D X Z A 1 1.1 1.11 1.12 1.13 1.14 1.15 Str1 10 11 12 13 14 15 16 "" Note that a complex number consists of 2 numbers. Also note that an empty string can be given. Let us now give an example of a separate header file that one might use to get interferometer data into casacore:: U V W TIME ANT1 ANT2 DATA R R R D I I X1,0 The data file would then look like:: 124.011 54560.0 3477.1 43456789.0990 1 2 4.327 -0.1132 34561.0 45629.3 3900.5 43456789.0990 1 3 5.398 0.4521 Note that the DATA column is defined as a 2-dim array of 1 correlation and a variable number of channels, so the actual number of channels is determined by the input. In this example both rows will have 1 channel (note that a complex value contains 2 values). Tables may have keywords in addition to the columns. The keywords are useful for holding information that is global to the entire table (such as author, revision, history, etc.). The keywords in the header definitions must preceed the column descriptions. They must be enclosed between a line that starts with ".key..." and a line that starts with ".endkey..." (where ... can be anything). A table keywordset and column keywordsets can be specified. The latter can be specified by specifying the column name after the .keywords string. Between these two lines each line should contain the following: - The keyword name, e.g., ANYKEY - The datatype and optional shape of the keyword (cf. list of valid types above) - The value or values for the keyword (the keyword may contain a scalar or an array of values). e.g., 3.14159 21.78945 Thus to continue the example above, one might wish to add keywords as follows:: .keywords DATE A "97/1/16" REVISION D 2.01 AUTHOR A "Tim Cornwell" INSTRUMENT A "VLA" .endkeywords .keywords TIME UNIT A "s" .endkeywords U V W TIME ANT1 ANT2 DATA R R R D I I X1,0 Similarly to the column format string, the keyword formats can also contain shape information. The only difference is that if no shape is given, a keyword can have multiple values (making it a vector). It is possible to ignore comment lines in the header and data file by giving the `commentmarker`. It indicates that lines starting with the given marker are ignored. Note that the marker can be a regular expression (e.g. `' *//'` tells that lines starting with // and optionally preceeded by blanks have to be ignored). With the arguments `firstline` and `lastline` one can specify which lines have to be taken from the input file. A negative value means 1 for `firstline` or end-of-file for `lastline`. Note that if the headers and data are combined in one file, these line arguments apply to the whole file. If headers and data are in separate files, these line arguments apply to the data file only. Also note that ignored comment lines are counted, thus are used to determine which lines are in the line range. The number of rows is determined by the number of lines read from the data file. """ import os.path filename = os.path.expandvars(asciifile) filename = os.path.expanduser(filename) if not os.path.exists(filename): s = "File '%s' not found" % (filename) raise IOError(s) if headerfile != '': filename = os.path.expandvars(headerfile) filename = os.path.expanduser(filename) if not os.path.exists(filename): s = "File '%s' not found" % (filename) raise IOError(s) tab = table(asciifile, headerfile, tablename, autoheader, autoshape, sep, commentmarker, firstline, lastline, _columnnames=columnnames, _datatypes=datatypes, _oper=1) six.print_('Input format: [' + tab._getasciiformat() + ']') # Close table and reopen it in correct way. tab = 0 return table(tablename, readonly=readonly, lockoptions=lockoptions, ack=ack)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 27; 2, function_name:makescacoldesc; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 3, 24; 4, identifier:columnname; 5, identifier:value; 6, default_parameter; 6, 7; 6, 8; 7, identifier:datamanagertype; 8, string:''; 9, default_parameter; 9, 10; 9, 11; 10, identifier:datamanagergroup; 11, string:''; 12, default_parameter; 12, 13; 12, 14; 13, identifier:options; 14, integer:0; 15, default_parameter; 15, 16; 15, 17; 16, identifier:maxlen; 17, integer:0; 18, default_parameter; 18, 19; 18, 20; 19, identifier:comment; 20, string:''; 21, default_parameter; 21, 22; 21, 23; 22, identifier:valuetype; 23, string:''; 24, default_parameter; 24, 25; 24, 26; 25, identifier:keywords; 26, dictionary; 27, block; 27, 28; 27, 30; 27, 34; 27, 46; 27, 71; 28, expression_statement; 28, 29; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:vtype; 33, identifier:valuetype; 34, if_statement; 34, 35; 34, 38; 35, comparison_operator:==; 35, 36; 35, 37; 36, identifier:vtype; 37, string:''; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:vtype; 42, call; 42, 43; 42, 44; 43, identifier:_value_type_name; 44, argument_list; 44, 45; 45, identifier:value; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:rec2; 49, dictionary; 49, 50; 49, 53; 49, 56; 49, 59; 49, 62; 49, 65; 49, 68; 50, pair; 50, 51; 50, 52; 51, string:'valueType'; 52, identifier:vtype; 53, pair; 53, 54; 53, 55; 54, string:'dataManagerType'; 55, identifier:datamanagertype; 56, pair; 56, 57; 56, 58; 57, string:'dataManagerGroup'; 58, identifier:datamanagergroup; 59, pair; 59, 60; 59, 61; 60, string:'option'; 61, identifier:options; 62, pair; 62, 63; 62, 64; 63, string:'maxlen'; 64, identifier:maxlen; 65, pair; 65, 66; 65, 67; 66, string:'comment'; 67, identifier:comment; 68, pair; 68, 69; 68, 70; 69, string:'keywords'; 70, identifier:keywords; 71, return_statement; 71, 72; 72, dictionary; 72, 73; 72, 76; 73, pair; 73, 74; 73, 75; 74, string:'name'; 75, identifier:columnname; 76, pair; 76, 77; 76, 78; 77, string:'desc'; 78, identifier:rec2
def makescacoldesc(columnname, value, datamanagertype='', datamanagergroup='', options=0, maxlen=0, comment='', valuetype='', keywords={}): """Create description of a scalar column. A description for a scalar column can be created from a name for the column and a data value, which is used only to determine the type of the column. Note that a dict value is also possible. It is possible to create the column description in more detail by giving the data manager name, group, option, and comment as well. The data manager type tells which data manager (storage manager) is used to store the columns. The data manager type and group are explained in more detail in the `casacore Tables <../../casacore/doc/html/group__Tables__module.html>`_ documentation. It returns a dict with fields `name` and `desc` which can thereafter be used to build a table description using function :func:`maketabdesc`. `columname` Name of column `value` Example data value used to determine the column's data type. It is only used if argument `valuetype` is not given. `datamanagertype` Type of data manager which can be one of StandardStMan (default) or IncrementalStMan. The latter one can save disk space if many subsequent cells in the column will have the same value. `datamanagergroup` Data manager group. Only for the expert user. `options` Options. Need not be filled in. `maxlen` Maximum length of string values in a column. Default 0 means unlimited. `comment` Comment: informational for user. `valuetype` A string giving the column's data type. Possible data types are bool (or boolean), uchar (or byte), short, int (or integer), uint, float, double, complex, dcomplex, and string. 'keywords' A dict defining initial keywords for the column. For example:: scd1 = makescacoldesc("col2", "")) scd2 = makescacoldesc("col1", 1, "IncrementalStMan") td = maketabdesc([scd1, scd2]) This creates a table description consisting of an integer column `col1`, and a string column `col2`. `col1` uses the IncrementalStMan storage manager, while `col2` uses the default storage manager StandardStMan. """ vtype = valuetype if vtype == '': vtype = _value_type_name(value) rec2 = {'valueType': vtype, 'dataManagerType': datamanagertype, 'dataManagerGroup': datamanagergroup, 'option': options, 'maxlen': maxlen, 'comment': comment, 'keywords': keywords} return {'name': columnname, 'desc': rec2}
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 33; 2, function_name:makearrcoldesc; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 3, 24; 3, 27; 3, 30; 4, identifier:columnname; 5, identifier:value; 6, default_parameter; 6, 7; 6, 8; 7, identifier:ndim; 8, integer:0; 9, default_parameter; 9, 10; 9, 11; 10, identifier:shape; 11, list:[]; 12, default_parameter; 12, 13; 12, 14; 13, identifier:datamanagertype; 14, string:''; 15, default_parameter; 15, 16; 15, 17; 16, identifier:datamanagergroup; 17, string:''; 18, default_parameter; 18, 19; 18, 20; 19, identifier:options; 20, integer:0; 21, default_parameter; 21, 22; 21, 23; 22, identifier:maxlen; 23, integer:0; 24, default_parameter; 24, 25; 24, 26; 25, identifier:comment; 26, string:''; 27, default_parameter; 27, 28; 27, 29; 28, identifier:valuetype; 29, string:''; 30, default_parameter; 30, 31; 30, 32; 31, identifier:keywords; 32, dictionary; 33, block; 33, 34; 33, 36; 33, 40; 33, 52; 33, 72; 33, 106; 34, expression_statement; 34, 35; 35, comment; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:vtype; 39, identifier:valuetype; 40, if_statement; 40, 41; 40, 44; 41, comparison_operator:==; 41, 42; 41, 43; 42, identifier:vtype; 43, string:''; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:vtype; 48, call; 48, 49; 48, 50; 49, identifier:_value_type_name; 50, argument_list; 50, 51; 51, identifier:value; 52, if_statement; 52, 53; 52, 59; 53, comparison_operator:>; 53, 54; 53, 58; 54, call; 54, 55; 54, 56; 55, identifier:len; 56, argument_list; 56, 57; 57, identifier:shape; 58, integer:0; 59, block; 59, 60; 60, if_statement; 60, 61; 60, 64; 61, comparison_operator:<=; 61, 62; 61, 63; 62, identifier:ndim; 63, integer:0; 64, block; 64, 65; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:ndim; 68, call; 68, 69; 68, 70; 69, identifier:len; 70, argument_list; 70, 71; 71, identifier:shape; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:rec2; 75, dictionary; 75, 76; 75, 79; 75, 82; 75, 85; 75, 88; 75, 91; 75, 94; 75, 97; 75, 100; 75, 103; 76, pair; 76, 77; 76, 78; 77, string:'valueType'; 78, identifier:vtype; 79, pair; 79, 80; 79, 81; 80, string:'dataManagerType'; 81, identifier:datamanagertype; 82, pair; 82, 83; 82, 84; 83, string:'dataManagerGroup'; 84, identifier:datamanagergroup; 85, pair; 85, 86; 85, 87; 86, string:'ndim'; 87, identifier:ndim; 88, pair; 88, 89; 88, 90; 89, string:'shape'; 90, identifier:shape; 91, pair; 91, 92; 91, 93; 92, string:'_c_order'; 93, True; 94, pair; 94, 95; 94, 96; 95, string:'option'; 96, identifier:options; 97, pair; 97, 98; 97, 99; 98, string:'maxlen'; 99, identifier:maxlen; 100, pair; 100, 101; 100, 102; 101, string:'comment'; 102, identifier:comment; 103, pair; 103, 104; 103, 105; 104, string:'keywords'; 105, identifier:keywords; 106, return_statement; 106, 107; 107, dictionary; 107, 108; 107, 111; 108, pair; 108, 109; 108, 110; 109, string:'name'; 110, identifier:columnname; 111, pair; 111, 112; 111, 113; 112, string:'desc'; 113, identifier:rec2
def makearrcoldesc(columnname, value, ndim=0, shape=[], datamanagertype='', datamanagergroup='', options=0, maxlen=0, comment='', valuetype='', keywords={}): """Create description of an array column. A description for a scalar column can be created from a name for the column and a data value, which is used only to determine the type of the column. Note that a dict value is also possible. It is possible to create the column description in more detail by giving the dimensionality, shape, data manager name, group, option, and comment as well. The data manager type tells which data manager (storage manager) is used to store the columns. The data manager type and group are explained in more detail in the `casacore Tables <../../casacore/doc/html/group__Tables__module.html>`_ documentation. It returns a dict with fields `name` and `desc` which can thereafter be used to build a table description using function :func:`maketabdesc`. `name` The name of the column. `value` A data value, which is only used to determine the data type of the column. It is only used if argument `valuetype` is not given. `ndim` Optionally the number of dimensions. A value > 0 means that all arrays in the column must have that dimensionality. Note that the arrays can still differ in shape unless the shape vector is also given. `shape` An optional sequence of integers giving the shape of the array in each cell. If given, it forces option FixedShape (see below) and sets the number of dimensions (if not given). All arrays in the column get the given shape and the array is created as soon as a row is added. Note that the shape vector gives the shape in each table cell; the number of rows in the table should NOT be part of it. `datamanagertype` Type of data manager which can be one of StandardStMan (default), IncrementalStMan, TiledColumnStMan, TiledCellStMan, or TiledShapeStMan. The tiled storage managers are usually used for bigger data arrays. `datamanagergroup` Data manager group. Only for the expert user. `options` Optionally numeric array options which can be added to combine them. `1` means Direct. It tells that the data are directly stored in the table. Direct forces option FixedShape. If not given, the array is indirect, which means that the data will be stored in a separate file. `4` means FixedShape. This option does not need to be given, because it is enforced if the shape is given. FixedShape means that the shape of the array must be the same in each cell of the column. Otherwise the array shapes may be different in each column cell and is it possible that a cell does not contain an array at all. Note that when given (or implicitly by option Direct), the shape argument must be given as well. Default is 0, thus indirect and variable shaped. `maxlen` Maximum length of string values in a column. Default 0 means unlimited. `comment` Comment: informational for user. `valuetype` A string giving the column's data type. Possible data types are bool (or boolean), uchar (or byte), short, int (or integer), uint, float, double, complex, dcomplex, and string. 'keywords' A dict defining initial keywords for the column. For example:: acd1= makescacoldesc("arr1", 1., 0, [2,3,4]) td = maketabdesc(acd1) This creates a table description consisting of an array column `arr1` containing 3-dim arrays of doubles with shape [2,3,4]. """ vtype = valuetype if vtype == '': vtype = _value_type_name(value) if len(shape) > 0: if ndim <= 0: ndim = len(shape) rec2 = {'valueType': vtype, 'dataManagerType': datamanagertype, 'dataManagerGroup': datamanagergroup, 'ndim': ndim, 'shape': shape, '_c_order': True, 'option': options, 'maxlen': maxlen, 'comment': comment, 'keywords': keywords} return {'name': columnname, 'desc': rec2}
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:makedminfo; 3, parameters; 3, 4; 3, 5; 4, identifier:tabdesc; 5, default_parameter; 5, 6; 5, 7; 6, identifier:group_spec; 7, None; 8, block; 8, 9; 8, 11; 8, 20; 8, 50; 8, 57; 8, 58; 8, 59; 8, 191; 8, 192; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 15; 12, comparison_operator:is; 12, 13; 12, 14; 13, identifier:group_spec; 14, None; 15, block; 15, 16; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:group_spec; 19, dictionary; 20, class_definition; 20, 21; 20, 22; 20, 24; 21, identifier:DMGroup; 22, argument_list; 22, 23; 23, identifier:object; 24, block; 24, 25; 24, 27; 25, expression_statement; 25, 26; 26, comment; 27, function_definition; 27, 28; 27, 29; 27, 31; 28, function_name:__init__; 29, parameters; 29, 30; 30, identifier:self; 31, block; 31, 32; 31, 38; 31, 44; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:columns; 37, list:[]; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:type; 43, None; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:spec; 49, None; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:dm_groups; 53, call; 53, 54; 53, 55; 54, identifier:defaultdict; 55, argument_list; 55, 56; 56, identifier:DMGroup; 57, comment; 58, comment; 59, for_statement; 59, 60; 59, 63; 59, 69; 60, pattern_list; 60, 61; 60, 62; 61, identifier:c; 62, identifier:d; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:six; 66, identifier:iteritems; 67, argument_list; 67, 68; 68, identifier:tabdesc; 69, block; 69, 70; 69, 79; 69, 80; 69, 90; 69, 100; 69, 101; 69, 109; 69, 117; 69, 118; 69, 124; 69, 125; 69, 134; 69, 135; 69, 154; 69, 155; 70, if_statement; 70, 71; 70, 77; 71, comparison_operator:in; 71, 72; 71, 73; 72, identifier:c; 73, tuple; 73, 74; 73, 75; 73, 76; 74, string:'_define_hypercolumn_'; 75, string:'_keywords_'; 76, string:'_private_keywords_'; 77, block; 77, 78; 78, continue_statement; 79, comment; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:group; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:d; 86, identifier:get; 87, argument_list; 87, 88; 87, 89; 88, string:"dataManagerGroup"; 89, string:"StandardStMan"; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:type_; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:d; 96, identifier:get; 97, argument_list; 97, 98; 97, 99; 98, string:"dataManagerType"; 99, string:"StandardStMan"; 100, comment; 101, if_statement; 101, 102; 101, 104; 102, not_operator; 102, 103; 103, identifier:group; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:group; 108, string:"StandardStMan"; 109, if_statement; 109, 110; 109, 112; 110, not_operator; 110, 111; 111, identifier:type_; 112, block; 112, 113; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:type_; 116, string:"StandardStMan"; 117, comment; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:dm_group; 121, subscript; 121, 122; 121, 123; 122, identifier:dm_groups; 123, identifier:group; 124, comment; 125, expression_statement; 125, 126; 126, call; 126, 127; 126, 132; 127, attribute; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:dm_group; 130, identifier:columns; 131, identifier:append; 132, argument_list; 132, 133; 133, identifier:c; 134, comment; 135, if_statement; 135, 136; 135, 141; 136, comparison_operator:is; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:dm_group; 139, identifier:spec; 140, None; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:dm_group; 146, identifier:spec; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:group_spec; 150, identifier:get; 151, argument_list; 151, 152; 151, 153; 152, identifier:group; 153, dictionary; 154, comment; 155, if_statement; 155, 156; 155, 161; 155, 168; 156, comparison_operator:is; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:dm_group; 159, identifier:type; 160, None; 161, block; 161, 162; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:dm_group; 166, identifier:type; 167, identifier:type_; 168, elif_clause; 168, 169; 168, 175; 169, not_operator; 169, 170; 170, comparison_operator:==; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:dm_group; 173, identifier:type; 174, identifier:type_; 175, block; 175, 176; 176, raise_statement; 176, 177; 177, call; 177, 178; 177, 179; 178, identifier:ValueError; 179, argument_list; 179, 180; 180, binary_operator:%; 180, 181; 180, 185; 181, concatenated_string; 181, 182; 181, 183; 181, 184; 182, string:"Mismatched dataManagerType '%s' "; 183, string:"for dataManagerGroup '%s' "; 184, string:"Previously, the type was '%s'"; 185, tuple; 185, 186; 185, 187; 185, 188; 186, identifier:type_; 187, identifier:group; 188, attribute; 188, 189; 188, 190; 189, identifier:dm_group; 190, identifier:type; 191, comment; 192, return_statement; 192, 193; 193, dictionary_comprehension; 193, 194; 193, 223; 194, pair; 194, 195; 194, 201; 195, binary_operator:%; 195, 196; 195, 197; 196, string:'*%d'; 197, parenthesized_expression; 197, 198; 198, binary_operator:+; 198, 199; 198, 200; 199, identifier:i; 200, integer:1; 201, dictionary; 201, 202; 201, 207; 201, 212; 201, 215; 201, 220; 202, pair; 202, 203; 202, 204; 203, string:'COLUMNS'; 204, attribute; 204, 205; 204, 206; 205, identifier:dm_group; 206, identifier:columns; 207, pair; 207, 208; 207, 209; 208, string:'TYPE'; 209, attribute; 209, 210; 209, 211; 210, identifier:dm_group; 211, identifier:type; 212, pair; 212, 213; 212, 214; 213, string:'NAME'; 214, identifier:group; 215, pair; 215, 216; 215, 217; 216, string:'SPEC'; 217, attribute; 217, 218; 217, 219; 218, identifier:dm_group; 219, identifier:spec; 220, pair; 220, 221; 220, 222; 221, string:'SEQNR'; 222, identifier:i; 223, for_in_clause; 223, 224; 223, 229; 224, pattern_list; 224, 225; 224, 226; 225, identifier:i; 226, tuple_pattern; 226, 227; 226, 228; 227, identifier:group; 228, identifier:dm_group; 229, call; 229, 230; 229, 231; 230, identifier:enumerate; 231, argument_list; 231, 232; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:six; 235, identifier:iteritems; 236, argument_list; 236, 237; 237, identifier:dm_groups
def makedminfo(tabdesc, group_spec=None): """Creates a data manager information object. Create a data manager information dictionary outline from a table description. The resulting dictionary is a bare outline and is available for the purposes of further customising the data manager via the `group_spec` argument. The resulting dictionary can be used in the :class:`table` constructor and the :meth:`default_ms` and :meth:`default_ms_subtable` functions. `tabdesc` The table description `group_spec` The SPEC for a data manager group. In practice this is useful for setting the Default Tile Size and Maximum Cache Size for the Data Manager { 'WeightColumnGroup' : { 'DEFAULTTILESHAPE': np.int32([4,4,4]), 'MAXIMUMCACHESIZE': 1000, } } This should be used with care. """ if group_spec is None: group_spec = {} class DMGroup(object): """ Keep track of the columns, type and spec of each data manager group """ def __init__(self): self.columns = [] self.type = None self.spec = None dm_groups = defaultdict(DMGroup) # Iterate through the table columns, grouping them # by their dataManagerGroup for c, d in six.iteritems(tabdesc): if c in ('_define_hypercolumn_', '_keywords_', '_private_keywords_'): continue # Extract group and data manager type group = d.get("dataManagerGroup", "StandardStMan") type_ = d.get("dataManagerType", "StandardStMan") # Set defaults if necessary if not group: group = "StandardStMan" if not type_: type_ = "StandardStMan" # Obtain the (possibly empty) data manager group dm_group = dm_groups[group] # Add the column dm_group.columns.append(c) # Set the spec if dm_group.spec is None: dm_group.spec = group_spec.get(group, {}) # Check that the data manager type is consistent across columns if dm_group.type is None: dm_group.type = type_ elif not dm_group.type == type_: raise ValueError("Mismatched dataManagerType '%s' " "for dataManagerGroup '%s' " "Previously, the type was '%s'" % (type_, group, dm_group.type)) # Output a data manager entry return { '*%d'%(i+1): { 'COLUMNS': dm_group.columns, 'TYPE': dm_group.type, 'NAME': group, 'SPEC' : dm_group.spec, 'SEQNR': i } for i, (group, dm_group) in enumerate(six.iteritems(dm_groups)) }
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:attrgetrow; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:groupname; 6, identifier:key; 7, default_parameter; 7, 8; 7, 9; 8, identifier:value; 9, None; 10, block; 10, 11; 10, 13; 10, 29; 10, 30; 10, 41; 10, 67; 10, 93; 11, expression_statement; 11, 12; 12, comment; 13, if_statement; 13, 14; 13, 20; 14, not_operator; 14, 15; 15, call; 15, 16; 15, 17; 16, identifier:isinstance; 17, argument_list; 17, 18; 17, 19; 18, identifier:key; 19, identifier:str; 20, block; 20, 21; 21, return_statement; 21, 22; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:_attrgetrow; 26, argument_list; 26, 27; 26, 28; 27, identifier:groupname; 28, identifier:key; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:rownrs; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:attrfindrows; 37, argument_list; 37, 38; 37, 39; 37, 40; 38, identifier:groupname; 39, identifier:key; 40, identifier:value; 41, if_statement; 41, 42; 41, 48; 42, comparison_operator:==; 42, 43; 42, 47; 43, call; 43, 44; 43, 45; 44, identifier:len; 45, argument_list; 45, 46; 46, identifier:rownrs; 47, integer:0; 48, block; 48, 49; 49, raise_statement; 49, 50; 50, call; 50, 51; 50, 52; 51, identifier:IndexError; 52, argument_list; 52, 53; 53, binary_operator:+; 53, 54; 53, 63; 54, binary_operator:+; 54, 55; 54, 62; 55, binary_operator:+; 55, 56; 55, 61; 56, binary_operator:+; 56, 57; 56, 60; 57, binary_operator:+; 57, 58; 57, 59; 58, string:"Image attribute "; 59, identifier:key; 60, string:" in group "; 61, identifier:groupname; 62, string:" has no matches for value "; 63, call; 63, 64; 63, 65; 64, identifier:str; 65, argument_list; 65, 66; 66, identifier:value; 67, if_statement; 67, 68; 67, 74; 68, comparison_operator:>; 68, 69; 68, 73; 69, call; 69, 70; 69, 71; 70, identifier:len; 71, argument_list; 71, 72; 72, identifier:rownrs; 73, integer:1; 74, block; 74, 75; 75, raise_statement; 75, 76; 76, call; 76, 77; 76, 78; 77, identifier:IndexError; 78, argument_list; 78, 79; 79, binary_operator:+; 79, 80; 79, 89; 80, binary_operator:+; 80, 81; 80, 88; 81, binary_operator:+; 81, 82; 81, 87; 82, binary_operator:+; 82, 83; 82, 86; 83, binary_operator:+; 83, 84; 83, 85; 84, string:"Image attribute "; 85, identifier:key; 86, string:" in group "; 87, identifier:groupname; 88, string:" has multiple matches for value "; 89, call; 89, 90; 89, 91; 90, identifier:str; 91, argument_list; 91, 92; 92, identifier:value; 93, return_statement; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:_attrgetrow; 98, argument_list; 98, 99; 98, 100; 99, identifier:groupname; 100, subscript; 100, 101; 100, 102; 101, identifier:rownrs; 102, integer:0
def attrgetrow(self, groupname, key, value=None): """Get the values of all attributes of a row in a group. If the key is an integer, the key is the row number for which the attribute values have to be returned. Otherwise the key has to be a string and it defines the name of an attribute. The attribute values of the row for which the key matches the given value is returned. It can only be used for unique attribute keys. An IndexError exception is raised if no or multiple matches are found. """ if not isinstance(key, str): return self._attrgetrow(groupname, key) # The key is an attribute name whose value has to be found. rownrs = self.attrfindrows(groupname, key, value) if len(rownrs) == 0: raise IndexError("Image attribute " + key + " in group " + groupname + " has no matches for value " + str(value)) if len(rownrs) > 1: raise IndexError("Image attribute " + key + " in group " + groupname + " has multiple matches for value " + str(value)) return self._attrgetrow(groupname, rownrs[0])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:substitute; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:s; 5, default_parameter; 5, 6; 5, 7; 6, identifier:objlist; 7, tuple; 8, default_parameter; 8, 9; 8, 10; 9, identifier:globals; 10, dictionary; 11, default_parameter; 11, 12; 11, 13; 12, identifier:locals; 13, dictionary; 14, block; 14, 15; 14, 17; 14, 18; 14, 29; 14, 30; 14, 34; 14, 38; 14, 42; 14, 46; 14, 50; 14, 54; 14, 58; 14, 62; 14, 63; 14, 290; 14, 316; 15, expression_statement; 15, 16; 16, comment; 17, comment; 18, if_statement; 18, 19; 18, 21; 19, not_operator; 19, 20; 20, identifier:locals; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:locals; 25, call; 25, 26; 25, 27; 26, identifier:getlocals; 27, argument_list; 27, 28; 28, integer:3; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:backslash; 33, False; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:dollar; 37, False; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:nparen; 41, integer:0; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:name; 45, string:''; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:evalstr; 49, string:''; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:squote; 53, False; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:dquote; 57, False; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:out; 61, string:''; 62, comment; 63, for_statement; 63, 64; 63, 65; 63, 66; 64, identifier:tmp; 65, identifier:s; 66, block; 66, 67; 66, 79; 66, 80; 66, 81; 66, 160; 66, 161; 66, 187; 66, 261; 66, 262; 66, 263; 66, 278; 66, 287; 66, 288; 66, 289; 67, if_statement; 67, 68; 67, 69; 68, identifier:backslash; 69, block; 69, 70; 69, 74; 69, 78; 70, expression_statement; 70, 71; 71, augmented_assignment:+=; 71, 72; 71, 73; 72, identifier:out; 73, identifier:tmp; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:backslash; 77, False; 78, continue_statement; 79, comment; 80, comment; 81, if_statement; 81, 82; 81, 87; 82, boolean_operator:and; 82, 83; 82, 84; 83, identifier:dollar; 84, comparison_operator:==; 84, 85; 84, 86; 85, identifier:nparen; 86, integer:0; 87, block; 87, 88; 87, 110; 87, 111; 87, 126; 87, 127; 87, 145; 87, 146; 87, 156; 88, if_statement; 88, 89; 88, 104; 89, boolean_operator:or; 89, 90; 89, 99; 90, boolean_operator:or; 90, 91; 90, 94; 91, comparison_operator:==; 91, 92; 91, 93; 92, identifier:tmp; 93, string:'_'; 94, parenthesized_expression; 94, 95; 95, comparison_operator:<=; 95, 96; 95, 97; 95, 98; 96, string:'a'; 97, identifier:tmp; 98, string:'z'; 99, parenthesized_expression; 99, 100; 100, comparison_operator:<=; 100, 101; 100, 102; 100, 103; 101, string:'A'; 102, identifier:tmp; 103, string:'Z'; 104, block; 104, 105; 104, 109; 105, expression_statement; 105, 106; 106, augmented_assignment:+=; 106, 107; 106, 108; 107, identifier:name; 108, identifier:tmp; 109, continue_statement; 110, comment; 111, if_statement; 111, 112; 111, 120; 112, boolean_operator:and; 112, 113; 112, 117; 113, comparison_operator:<=; 113, 114; 113, 115; 113, 116; 114, string:'0'; 115, identifier:tmp; 116, string:'9'; 117, comparison_operator:!=; 117, 118; 117, 119; 118, identifier:name; 119, string:''; 120, block; 120, 121; 120, 125; 121, expression_statement; 121, 122; 122, augmented_assignment:+=; 122, 123; 122, 124; 123, identifier:name; 124, identifier:tmp; 125, continue_statement; 126, comment; 127, if_statement; 127, 128; 127, 135; 128, boolean_operator:and; 128, 129; 128, 132; 129, comparison_operator:==; 129, 130; 129, 131; 130, identifier:tmp; 131, string:'('; 132, comparison_operator:==; 132, 133; 132, 134; 133, identifier:name; 134, string:''; 135, block; 135, 136; 135, 140; 135, 144; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:nparen; 139, integer:1; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:evalstr; 143, string:''; 144, continue_statement; 145, comment; 146, expression_statement; 146, 147; 147, augmented_assignment:+=; 147, 148; 147, 149; 148, identifier:out; 149, call; 149, 150; 149, 151; 150, identifier:substitutename; 151, argument_list; 151, 152; 151, 153; 151, 154; 151, 155; 152, identifier:name; 153, identifier:objlist; 154, identifier:globals; 155, identifier:locals; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:dollar; 159, False; 160, comment; 161, if_statement; 161, 162; 161, 168; 161, 174; 162, boolean_operator:and; 162, 163; 162, 166; 163, comparison_operator:==; 163, 164; 163, 165; 164, identifier:tmp; 165, string:'"'; 166, not_operator; 166, 167; 167, identifier:squote; 168, block; 168, 169; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:dquote; 172, not_operator; 172, 173; 173, identifier:dquote; 174, elif_clause; 174, 175; 174, 181; 175, boolean_operator:and; 175, 176; 175, 179; 176, comparison_operator:==; 176, 177; 176, 178; 177, identifier:tmp; 178, string:"'"; 179, not_operator; 179, 180; 180, identifier:dquote; 181, block; 181, 182; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:squote; 185, not_operator; 185, 186; 186, identifier:squote; 187, if_statement; 187, 188; 187, 193; 187, 194; 187, 195; 188, boolean_operator:and; 188, 189; 188, 191; 189, not_operator; 189, 190; 190, identifier:dquote; 191, not_operator; 191, 192; 192, identifier:squote; 193, comment; 194, comment; 195, block; 195, 196; 195, 245; 195, 246; 195, 247; 196, if_statement; 196, 197; 196, 200; 197, comparison_operator:>; 197, 198; 197, 199; 198, identifier:nparen; 199, integer:0; 200, block; 200, 201; 200, 240; 200, 244; 201, if_statement; 201, 202; 201, 205; 201, 210; 202, comparison_operator:==; 202, 203; 202, 204; 203, identifier:tmp; 204, string:'('; 205, block; 205, 206; 206, expression_statement; 206, 207; 207, augmented_assignment:+=; 207, 208; 207, 209; 208, identifier:nparen; 209, integer:1; 210, elif_clause; 210, 211; 210, 214; 211, comparison_operator:==; 211, 212; 211, 213; 212, identifier:tmp; 213, string:')'; 214, block; 214, 215; 214, 219; 215, expression_statement; 215, 216; 216, augmented_assignment:-=; 216, 217; 216, 218; 217, identifier:nparen; 218, integer:1; 219, if_statement; 219, 220; 219, 223; 219, 224; 219, 225; 219, 226; 220, comparison_operator:==; 220, 221; 220, 222; 221, identifier:nparen; 222, integer:0; 223, comment; 224, comment; 225, comment; 226, block; 226, 227; 226, 236; 227, expression_statement; 227, 228; 228, augmented_assignment:+=; 228, 229; 228, 230; 229, identifier:out; 230, call; 230, 231; 230, 232; 231, identifier:substituteexpr; 232, argument_list; 232, 233; 232, 234; 232, 235; 233, identifier:evalstr; 234, identifier:globals; 235, identifier:locals; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 239; 238, identifier:dollar; 239, False; 240, expression_statement; 240, 241; 241, augmented_assignment:+=; 241, 242; 241, 243; 242, identifier:evalstr; 243, identifier:tmp; 244, continue_statement; 245, comment; 246, comment; 247, if_statement; 247, 248; 247, 251; 248, comparison_operator:==; 248, 249; 248, 250; 249, identifier:tmp; 250, string:'$'; 251, block; 251, 252; 251, 256; 251, 260; 252, expression_statement; 252, 253; 253, assignment; 253, 254; 253, 255; 254, identifier:dollar; 255, True; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 259; 258, identifier:name; 259, string:''; 260, continue_statement; 261, comment; 262, comment; 263, if_statement; 263, 264; 263, 267; 263, 272; 264, comparison_operator:==; 264, 265; 264, 266; 265, identifier:nparen; 266, integer:0; 267, block; 267, 268; 268, expression_statement; 268, 269; 269, augmented_assignment:+=; 269, 270; 269, 271; 270, identifier:out; 271, identifier:tmp; 272, else_clause; 272, 273; 273, block; 273, 274; 274, expression_statement; 274, 275; 275, augmented_assignment:+=; 275, 276; 275, 277; 276, identifier:evalstr; 277, identifier:tmp; 278, if_statement; 278, 279; 278, 282; 279, comparison_operator:==; 279, 280; 279, 281; 280, identifier:tmp; 281, string:'\\'; 282, block; 282, 283; 283, expression_statement; 283, 284; 284, assignment; 284, 285; 284, 286; 285, identifier:backslash; 286, True; 287, comment; 288, comment; 289, comment; 290, if_statement; 290, 291; 290, 292; 290, 303; 291, identifier:dollar; 292, block; 292, 293; 293, expression_statement; 293, 294; 294, augmented_assignment:+=; 294, 295; 294, 296; 295, identifier:out; 296, call; 296, 297; 296, 298; 297, identifier:substitutename; 298, argument_list; 298, 299; 298, 300; 298, 301; 298, 302; 299, identifier:name; 300, identifier:objlist; 301, identifier:globals; 302, identifier:locals; 303, else_clause; 303, 304; 304, block; 304, 305; 305, if_statement; 305, 306; 305, 309; 306, comparison_operator:>; 306, 307; 306, 308; 307, identifier:nparen; 308, integer:0; 309, block; 309, 310; 310, expression_statement; 310, 311; 311, augmented_assignment:+=; 311, 312; 311, 313; 312, identifier:out; 313, binary_operator:+; 313, 314; 313, 315; 314, string:'$('; 315, identifier:evalstr; 316, return_statement; 316, 317; 317, identifier:out
def substitute(s, objlist=(), globals={}, locals={}): """Substitute global python variables in a command string. This function parses a string and tries to substitute parts like `$name` by their value. It is uses by :mod:`image` and :mod:`table` to handle image and table objects in a command, but also other variables (integers, strings, etc.) can be substituted. The following rules apply: 1. A name must start with an underscore or alphabetic, followed by zero or more alphanumerics and underscores. 2. String parts enclosed in single or double quotes are literals and are left untouched. Furthermore a $ can be escaped by a backslash, which is useful if an environment variable is used. Note that an extra backslash is required in Python to escape the backslash. The output contains the quotes and backslashes. 3. A variable is looked up in the given local and global namespaces. 4. If the variable `name` has a vector value, its substitution is enclosed in square brackets and separated by commas. 5. A string value is enclosed in double quotes. If the value contains a double quote, that quote is enclosed in single quotes. 6. If the name's value has a type mentioned in the argument `objlist`, it is substituted by `$n` (where n is a sequence number) and its value is added to the objects of that type in `objlist`. 7. If the name is unknown or has an unknown type, it is left untouched. The `objlist` argument is a list of tuples or lists where each tuple or list has three fields: 1. The first field is the object type (e.g. `table`) 2. The second field is a prefix for the sequence number (usually empty). E.g. regions could have prefix 'r' resulting in a substitution like `$r1`. 3. The third field is a list of objects to be substituted. New objects get appended to it. Usually the list is initially empty. Apart from substituting variables, it also substitutes `$(expression)` by the expression result. It correctly handles parentheses and quotes in the expression. For example:: >>> a = 2 >>> b = 3 >>> substitute('$(a+b)+$a') '5+2' >>> substitute('$(a+b+a)') '7' >>> substitute('$((a+b)+$a)') '$((a+b)+$a)' >>> substitute('$((a+b)*(a+b))') '25' >>> substitute('$(len("ab cd( de"))') '9' Substitution is NOT recursive. E.g. if a=1 and b="$a", the result of substitute("$b") is "$a" and not 1. """ # Get the local variables at the caller level if not given. if not locals: locals = getlocals(3) # Initialize some variables. backslash = False dollar = False nparen = 0 name = '' evalstr = '' squote = False dquote = False out = '' # Loop through the entire string. for tmp in s: if backslash: out += tmp backslash = False continue # If a dollar is found, we might have a name or expression. # Alphabetics and underscore are always part of name. if dollar and nparen == 0: if tmp == '_' or ('a' <= tmp <= 'z') or ('A' <= tmp <= 'Z'): name += tmp continue # Numerics are only part if not first character. if '0' <= tmp <= '9' and name != '': name += tmp continue # $( indicates the start of an expression to evaluate. if tmp == '(' and name == '': nparen = 1 evalstr = '' continue # End of name found. Try to substitute. out += substitutename(name, objlist, globals, locals) dollar = False # Handle possible single or double quotes. if tmp == '"' and not squote: dquote = not dquote elif tmp == "'" and not dquote: squote = not squote if not dquote and not squote: # Count the number of balanced parentheses # (outside quoted strings) in the subexpression. if nparen > 0: if tmp == '(': nparen += 1 elif tmp == ')': nparen -= 1 if nparen == 0: # The last closing parenthese is found. # Evaluate the subexpression. # Add the result to the output. out += substituteexpr(evalstr, globals, locals) dollar = False evalstr += tmp continue # Set a switch if we have a dollar (outside quoted # and eval strings). if tmp == '$': dollar = True name = '' continue # No special character; add it to output or evalstr. # Set a switch if we have a backslash. if nparen == 0: out += tmp else: evalstr += tmp if tmp == '\\': backslash = True # The entire string has been handled. # Substitute a possible last name. # Insert a possible incomplete eval string as such. if dollar: out += substitutename(name, objlist, globals, locals) else: if nparen > 0: out += '$(' + evalstr return out
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:iter; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:columnnames; 6, default_parameter; 6, 7; 6, 8; 7, identifier:order; 8, string:''; 9, default_parameter; 9, 10; 9, 11; 10, identifier:sort; 11, True; 12, block; 12, 13; 12, 15; 12, 22; 13, expression_statement; 13, 14; 14, comment; 15, import_from_statement; 15, 16; 15, 20; 16, relative_import; 16, 17; 16, 18; 17, import_prefix; 18, dotted_name; 18, 19; 19, identifier:tableiter; 20, dotted_name; 20, 21; 21, identifier:tableiter; 22, return_statement; 22, 23; 23, call; 23, 24; 23, 25; 24, identifier:tableiter; 25, argument_list; 25, 26; 25, 27; 25, 28; 25, 29; 26, identifier:self; 27, identifier:columnnames; 28, identifier:order; 29, identifier:sort
def iter(self, columnnames, order='', sort=True): """Return a tableiter object. :class:`tableiter` lets one iterate over a table by returning in each iteration step a reference table containing equal values for the given columns. By default a sort is done on the given columns to get the correct iteration order. `order` | 'ascending' is iterate in ascending order (is the default). | 'descending' is iterate in descending order. `sort=False` do not sort (because table is already in correct order). For example, iterate by time through a measurementset table:: t = table('3c343.MS') for ts in t.iter('TIME'): print ts.nrows() """ from .tableiter import tableiter return tableiter(self, columnnames, order, sort)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:index; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:columnnames; 6, default_parameter; 6, 7; 6, 8; 7, identifier:sort; 8, True; 9, block; 9, 10; 9, 12; 9, 19; 10, expression_statement; 10, 11; 11, comment; 12, import_from_statement; 12, 13; 12, 17; 13, relative_import; 13, 14; 13, 15; 14, import_prefix; 15, dotted_name; 15, 16; 16, identifier:tableindex; 17, dotted_name; 17, 18; 18, identifier:tableindex; 19, return_statement; 19, 20; 20, call; 20, 21; 20, 22; 21, identifier:tableindex; 22, argument_list; 22, 23; 22, 24; 22, 25; 23, identifier:self; 24, identifier:columnnames; 25, identifier:sort
def index(self, columnnames, sort=True): """Return a tableindex object. :class:`tableindex` lets one get the row numbers of the rows holding given values for the columns for which the index is created. It uses an in-memory index on which a binary search is done. By default the table is sorted on the given columns to get the correct index order. For example:: t = table('3c343.MS') tinx = t.index('ANTENNA1') print tinx.rownumbers(0) # print rownrs containing ANTENNA1=0 """ from .tableindex import tableindex return tableindex(self, columnnames, sort)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:rownumbers; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:table; 7, None; 8, block; 8, 9; 8, 11; 8, 25; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 15; 12, comparison_operator:is; 12, 13; 12, 14; 13, identifier:table; 14, None; 15, block; 15, 16; 16, return_statement; 16, 17; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:_rownumbers; 21, argument_list; 21, 22; 22, call; 22, 23; 22, 24; 23, identifier:Table; 24, argument_list; 25, return_statement; 25, 26; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:_rownumbers; 30, argument_list; 30, 31; 31, identifier:table
def rownumbers(self, table=None): """Return a list containing the row numbers of this table. This method can be useful after a selection or a sort. It returns the row numbers of the rows in this table with respect to the given table. If no table is given, the original table is used. For example:: t = table('W53.MS') t1 = t.selectrows([1,3,5,7,9]) # select a few rows t1.rownumbers(t) # [1 3 5 7 9] t2 = t1.selectrows([2,5]) # select rows from the selection t2.rownumbers(t1) # [2 5] # rownrs of t2 in table t1 t2.rownumbers(t) # [3 9] # rownrs of t2 in t t2.rownumbers() # [3 9] The last statements show that the method returns the row numbers referring to the given table. Table t2 contains rows 2 and 5 in table t1, which are rows 3 and 9 in table t. """ if table is None: return self._rownumbers(Table()) return self._rownumbers(table)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:showstructure; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:dataman; 7, True; 8, default_parameter; 8, 9; 8, 10; 9, identifier:column; 10, True; 11, default_parameter; 11, 12; 11, 13; 12, identifier:subtable; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:sort; 16, False; 17, block; 17, 18; 17, 20; 18, expression_statement; 18, 19; 19, comment; 20, return_statement; 20, 21; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:_showstructure; 25, argument_list; 25, 26; 25, 27; 25, 28; 25, 29; 26, identifier:dataman; 27, identifier:column; 28, identifier:subtable; 29, identifier:sort
def showstructure(self, dataman=True, column=True, subtable=False, sort=False): """Show table structure in a formatted string. The structure of this table and optionally its subtables is shown. It shows the data manager info and column descriptions. Optionally the columns are sorted in alphabetical order. `dataman` Show data manager info? If False, only column info is shown. If True, data manager info and columns per data manager are shown. `column` Show column description per data manager? Only takes effect if dataman=True. `subtable` Show the structure of all subtables (recursively). The names of subtables are always shown. 'sort' Sort the columns in alphabetical order? """ return self._showstructure(dataman, column, subtable, sort)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 26; 2, function_name:query; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:query; 7, string:''; 8, default_parameter; 8, 9; 8, 10; 9, identifier:name; 10, string:''; 11, default_parameter; 11, 12; 11, 13; 12, identifier:sortlist; 13, string:''; 14, default_parameter; 14, 15; 14, 16; 15, identifier:columns; 16, string:''; 17, default_parameter; 17, 18; 17, 19; 18, identifier:limit; 19, integer:0; 20, default_parameter; 20, 21; 20, 22; 21, identifier:offset; 22, integer:0; 23, default_parameter; 23, 24; 23, 25; 24, identifier:style; 25, string:'Python'; 26, block; 26, 27; 26, 29; 26, 55; 26, 59; 26, 66; 26, 70; 26, 79; 26, 88; 26, 99; 26, 110; 26, 119; 27, expression_statement; 27, 28; 28, comment; 29, if_statement; 29, 30; 29, 47; 30, boolean_operator:and; 30, 31; 30, 44; 31, boolean_operator:and; 31, 32; 31, 40; 31, 41; 32, boolean_operator:and; 32, 33; 32, 38; 33, boolean_operator:and; 33, 34; 33, 36; 34, not_operator; 34, 35; 35, identifier:query; 36, not_operator; 36, 37; 37, identifier:sortlist; 38, not_operator; 38, 39; 39, identifier:columns; 40, line_continuation:\; 41, comparison_operator:<=; 41, 42; 41, 43; 42, identifier:limit; 43, integer:0; 44, comparison_operator:<=; 44, 45; 44, 46; 45, identifier:offset; 46, integer:0; 47, block; 47, 48; 48, raise_statement; 48, 49; 49, call; 49, 50; 49, 51; 50, identifier:ValueError; 51, argument_list; 51, 52; 52, binary_operator:+; 52, 53; 52, 54; 53, string:'No selection done (arguments query, '; 54, string:'sortlist, columns, limit, and offset are empty)'; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:command; 58, string:'select '; 59, if_statement; 59, 60; 59, 61; 60, identifier:columns; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, augmented_assignment:+=; 63, 64; 63, 65; 64, identifier:command; 65, identifier:columns; 66, expression_statement; 66, 67; 67, augmented_assignment:+=; 67, 68; 67, 69; 68, identifier:command; 69, string:' from $1'; 70, if_statement; 70, 71; 70, 72; 71, identifier:query; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, augmented_assignment:+=; 74, 75; 74, 76; 75, identifier:command; 76, binary_operator:+; 76, 77; 76, 78; 77, string:' where '; 78, identifier:query; 79, if_statement; 79, 80; 79, 81; 80, identifier:sortlist; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, augmented_assignment:+=; 83, 84; 83, 85; 84, identifier:command; 85, binary_operator:+; 85, 86; 85, 87; 86, string:' orderby '; 87, identifier:sortlist; 88, if_statement; 88, 89; 88, 92; 89, comparison_operator:>; 89, 90; 89, 91; 90, identifier:limit; 91, integer:0; 92, block; 92, 93; 93, expression_statement; 93, 94; 94, augmented_assignment:+=; 94, 95; 94, 96; 95, identifier:command; 96, binary_operator:%; 96, 97; 96, 98; 97, string:' limit %d'; 98, identifier:limit; 99, if_statement; 99, 100; 99, 103; 100, comparison_operator:>; 100, 101; 100, 102; 101, identifier:offset; 102, integer:0; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, augmented_assignment:+=; 105, 106; 105, 107; 106, identifier:command; 107, binary_operator:%; 107, 108; 107, 109; 108, string:' offset %d'; 109, identifier:offset; 110, if_statement; 110, 111; 110, 112; 111, identifier:name; 112, block; 112, 113; 113, expression_statement; 113, 114; 114, augmented_assignment:+=; 114, 115; 114, 116; 115, identifier:command; 116, binary_operator:+; 116, 117; 116, 118; 117, string:' giving '; 118, identifier:name; 119, return_statement; 119, 120; 120, call; 120, 121; 120, 122; 121, identifier:tablecommand; 122, argument_list; 122, 123; 122, 124; 122, 125; 123, identifier:command; 124, identifier:style; 125, list:[self]; 125, 126; 126, identifier:self
def query(self, query='', name='', sortlist='', columns='', limit=0, offset=0, style='Python'): """Query the table and return the result as a reference table. This method queries the table. It forms a `TaQL <../../doc/199.html>`_ command from the given arguments and executes it using the :func:`taql` function. The result is returned in a so-called reference table which references the selected columns and rows in the original table. Usually a reference table is temporary, but it can be made persistent by giving it a name. Note that a reference table is handled as any table, thus can be queried again. All arguments are optional, but at least one of `query`, `name`, `sortlist`, and `columns` should be used. See the `TaQL note <../../doc/199.html>`_ for the detailed description of the the arguments representing the various parts of a TaQL command. `query` The WHERE part of a TaQL command. `name` The name of the reference table if it is to be made persistent. `sortlist` The ORDERBY part of a TaQL command. It is a single string in which commas have to be used to separate sort keys. `columns` The columns to be selected (projection in data base terms). It is a single string in which commas have to be used to separate column names. Apart from column names, expressions can be given as well. `limit` If > 0, maximum number of rows to be selected. `offset` If > 0, ignore the first N matches. `style` The TaQL syntax style to be used (defaults to Python). """ if not query and not sortlist and not columns and \ limit <= 0 and offset <= 0: raise ValueError('No selection done (arguments query, ' + 'sortlist, columns, limit, and offset are empty)') command = 'select ' if columns: command += columns command += ' from $1' if query: command += ' where ' + query if sortlist: command += ' orderby ' + sortlist if limit > 0: command += ' limit %d' % limit if offset > 0: command += ' offset %d' % offset if name: command += ' giving ' + name return tablecommand(command, style, [self])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 2, function_name:sort; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 4, identifier:self; 5, identifier:sortlist; 6, default_parameter; 6, 7; 6, 8; 7, identifier:name; 8, string:''; 9, default_parameter; 9, 10; 9, 11; 10, identifier:limit; 11, integer:0; 12, default_parameter; 12, 13; 12, 14; 13, identifier:offset; 14, integer:0; 15, default_parameter; 15, 16; 15, 17; 16, identifier:style; 17, string:'Python'; 18, block; 18, 19; 18, 21; 18, 27; 18, 38; 18, 49; 18, 58; 19, expression_statement; 19, 20; 20, comment; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:command; 24, binary_operator:+; 24, 25; 24, 26; 25, string:'select from $1 orderby '; 26, identifier:sortlist; 27, if_statement; 27, 28; 27, 31; 28, comparison_operator:>; 28, 29; 28, 30; 29, identifier:limit; 30, integer:0; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, augmented_assignment:+=; 33, 34; 33, 35; 34, identifier:command; 35, binary_operator:%; 35, 36; 35, 37; 36, string:' limit %d'; 37, identifier:limit; 38, if_statement; 38, 39; 38, 42; 39, comparison_operator:>; 39, 40; 39, 41; 40, identifier:offset; 41, integer:0; 42, block; 42, 43; 43, expression_statement; 43, 44; 44, augmented_assignment:+=; 44, 45; 44, 46; 45, identifier:command; 46, binary_operator:%; 46, 47; 46, 48; 47, string:' offset %d'; 48, identifier:offset; 49, if_statement; 49, 50; 49, 51; 50, identifier:name; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, augmented_assignment:+=; 53, 54; 53, 55; 54, identifier:command; 55, binary_operator:+; 55, 56; 55, 57; 56, string:' giving '; 57, identifier:name; 58, return_statement; 58, 59; 59, call; 59, 60; 59, 61; 60, identifier:tablecommand; 61, argument_list; 61, 62; 61, 63; 61, 64; 62, identifier:command; 63, identifier:style; 64, list:[self]; 64, 65; 65, identifier:self
def sort(self, sortlist, name='', limit=0, offset=0, style='Python'): """Sort the table and return the result as a reference table. This method sorts the table. It forms a `TaQL <../../doc/199.html>`_ command from the given arguments and executes it using the :func:`taql` function. The result is returned in a so-called reference table which references the columns and rows in the original table. Usually a reference table is temporary, but it can be made persistent by giving it a name. Note that a reference table is handled as any table, thus can be queried again. `sortlist` The ORDERBY part of a TaQL command. It is a single string in which commas have to be used to separate sort keys. A sort key can be the name of a column, but it can be an expression as well. `name` The name of the reference table if it is to be made persistent. `limit` If > 0, maximum number of rows to be selected after the sort step. It can, for instance, be used to select the N highest values. `offset` If > 0, ignore the first `offset` matches after the sort step. `style` The TaQL syntax style to be used (defaults to Python). """ command = 'select from $1 orderby ' + sortlist if limit > 0: command += ' limit %d' % limit if offset > 0: command += ' offset %d' % offset if name: command += ' giving ' + name return tablecommand(command, style, [self])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:browse; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:wait; 7, True; 8, default_parameter; 8, 9; 8, 10; 9, identifier:tempname; 10, string:"/tmp/seltable"; 11, block; 11, 12; 11, 14; 11, 17; 11, 18; 11, 19; 11, 20; 12, expression_statement; 12, 13; 13, comment; 14, import_statement; 14, 15; 15, dotted_name; 15, 16; 16, identifier:os; 17, comment; 18, comment; 19, comment; 20, if_statement; 20, 21; 20, 29; 20, 216; 21, comparison_operator:==; 21, 22; 21, 28; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:os; 25, identifier:system; 26, argument_list; 26, 27; 27, string:'test `which casabrowser`x != x'; 28, integer:0; 29, block; 29, 30; 29, 34; 29, 38; 29, 50; 29, 77; 29, 83; 29, 89; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:waitstr1; 33, string:""; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:waitstr2; 37, string:"foreground ..."; 38, if_statement; 38, 39; 38, 41; 39, not_operator; 39, 40; 40, identifier:wait; 41, block; 41, 42; 41, 46; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:waitstr1; 45, string:" &"; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:waitstr2; 49, string:"background ..."; 50, if_statement; 50, 51; 50, 56; 50, 66; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:self; 54, identifier:iswritable; 55, argument_list; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:six; 61, identifier:print_; 62, argument_list; 62, 63; 63, binary_operator:+; 63, 64; 63, 65; 64, string:"Flushing data and starting casabrowser in the "; 65, identifier:waitstr2; 66, else_clause; 66, 67; 67, block; 67, 68; 68, expression_statement; 68, 69; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:six; 72, identifier:print_; 73, argument_list; 73, 74; 74, binary_operator:+; 74, 75; 74, 76; 75, string:"Starting casabrowser in the "; 76, identifier:waitstr2; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:self; 81, identifier:flush; 82, argument_list; 83, expression_statement; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:self; 87, identifier:unlock; 88, argument_list; 89, if_statement; 89, 90; 89, 106; 89, 122; 89, 191; 90, comparison_operator:==; 90, 91; 90, 105; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:os; 94, identifier:system; 95, argument_list; 95, 96; 96, binary_operator:+; 96, 97; 96, 104; 97, binary_operator:+; 97, 98; 97, 99; 98, string:'test -e '; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:self; 102, identifier:name; 103, argument_list; 104, string:'/table.dat'; 105, integer:0; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:os; 111, identifier:system; 112, argument_list; 112, 113; 113, binary_operator:+; 113, 114; 113, 121; 114, binary_operator:+; 114, 115; 114, 116; 115, string:'casabrowser '; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:name; 120, argument_list; 121, identifier:waitstr1; 122, elif_clause; 122, 123; 122, 129; 123, comparison_operator:>; 123, 124; 123, 128; 124, call; 124, 125; 124, 126; 125, identifier:len; 126, argument_list; 126, 127; 127, identifier:tempname; 128, integer:0; 129, block; 129, 130; 129, 139; 129, 146; 129, 157; 130, expression_statement; 130, 131; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:six; 134, identifier:print_; 135, argument_list; 135, 136; 136, binary_operator:+; 136, 137; 136, 138; 137, string:" making a persistent copy in table "; 138, identifier:tempname; 139, expression_statement; 139, 140; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:self; 143, identifier:copy; 144, argument_list; 144, 145; 145, identifier:tempname; 146, expression_statement; 146, 147; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:os; 150, identifier:system; 151, argument_list; 151, 152; 152, binary_operator:+; 152, 153; 152, 156; 153, binary_operator:+; 153, 154; 153, 155; 154, string:'casabrowser '; 155, identifier:tempname; 156, identifier:waitstr1; 157, if_statement; 157, 158; 157, 159; 157, 178; 158, identifier:wait; 159, block; 159, 160; 159, 166; 159, 173; 160, import_from_statement; 160, 161; 160, 164; 161, dotted_name; 161, 162; 161, 163; 162, identifier:casacore; 163, identifier:tables; 164, dotted_name; 164, 165; 165, identifier:tabledelete; 166, expression_statement; 166, 167; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:six; 170, identifier:print_; 171, argument_list; 171, 172; 172, string:" finished browsing"; 173, expression_statement; 173, 174; 174, call; 174, 175; 174, 176; 175, identifier:tabledelete; 176, argument_list; 176, 177; 177, identifier:tempname; 178, else_clause; 178, 179; 179, block; 179, 180; 180, expression_statement; 180, 181; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:six; 184, identifier:print_; 185, argument_list; 185, 186; 186, binary_operator:+; 186, 187; 186, 190; 187, binary_operator:+; 187, 188; 187, 189; 188, string:" after browsing use tabledelete('"; 189, identifier:tempname; 190, string:"') to delete the copy"; 191, else_clause; 191, 192; 192, block; 192, 193; 192, 200; 192, 209; 193, expression_statement; 193, 194; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:six; 197, identifier:print_; 198, argument_list; 198, 199; 199, string:"Cannot browse because the table is in memory only"; 200, expression_statement; 200, 201; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:six; 204, identifier:print_; 205, argument_list; 205, 206; 206, binary_operator:+; 206, 207; 206, 208; 207, string:"You can browse a (shallow) persistent copy "; 208, string:"of the table like: "; 209, expression_statement; 209, 210; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:six; 213, identifier:print_; 214, argument_list; 214, 215; 215, string:" t.browse(True, '/tmp/tab1')"; 216, else_clause; 216, 217; 217, block; 217, 218; 217, 234; 217, 240; 217, 243; 217, 249; 217, 254; 217, 265; 217, 272; 218, try_statement; 218, 219; 218, 223; 219, block; 219, 220; 220, import_statement; 220, 221; 221, dotted_name; 221, 222; 222, identifier:wxPython; 223, except_clause; 223, 224; 223, 225; 224, identifier:ImportError; 225, block; 225, 226; 225, 233; 226, expression_statement; 226, 227; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:six; 230, identifier:print_; 231, argument_list; 231, 232; 232, string:'casabrowser nor wxPython can be found'; 233, return_statement; 234, import_from_statement; 234, 235; 234, 238; 235, dotted_name; 235, 236; 235, 237; 236, identifier:wxPython; 237, identifier:wx; 238, dotted_name; 238, 239; 239, identifier:wxPySimpleApp; 240, import_statement; 240, 241; 241, dotted_name; 241, 242; 242, identifier:sys; 243, expression_statement; 243, 244; 244, assignment; 244, 245; 244, 246; 245, identifier:app; 246, call; 246, 247; 246, 248; 247, identifier:wxPySimpleApp; 248, argument_list; 249, import_from_statement; 249, 250; 249, 252; 250, dotted_name; 250, 251; 251, identifier:wxtablebrowser; 252, dotted_name; 252, 253; 253, identifier:CasaTestFrame; 254, expression_statement; 254, 255; 255, assignment; 255, 256; 255, 257; 256, identifier:frame; 257, call; 257, 258; 257, 259; 258, identifier:CasaTestFrame; 259, argument_list; 259, 260; 259, 261; 259, 264; 260, None; 261, attribute; 261, 262; 261, 263; 262, identifier:sys; 263, identifier:stdout; 264, identifier:self; 265, expression_statement; 265, 266; 266, call; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:frame; 269, identifier:Show; 270, argument_list; 270, 271; 271, True; 272, expression_statement; 272, 273; 273, call; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:app; 276, identifier:MainLoop; 277, argument_list
def browse(self, wait=True, tempname="/tmp/seltable"): """ Browse a table using casabrowser or a simple wxwidget based browser. By default the casabrowser is used if it can be found (in your PATH). Otherwise the wxwidget one is used if wx can be loaded. The casabrowser can only browse tables that are persistent on disk. This gives problems for tables resulting from a query because they are held in memory only (unless an output table name was given). To make browsing of such tables possible, the argument `tempname` can be used to specify a table name that will be used to form a persistent table that can be browsed. Note that such a table is very small as it does not contain data, but only references to rows in the original table. The default for `tempname` is '/tmp/seltable'. If needed, the table can be deleted using the :func:`tabledelete` function. If `wait=False`, the casabrowser is started in the background. In that case the user should delete a possibly created copy of a temporary table. """ import os # Test if casabrowser can be found. # On OS-X 'which' always returns 0, so use test on top of it. # Nothing is written on stdout if not found. if os.system('test `which casabrowser`x != x') == 0: waitstr1 = "" waitstr2 = "foreground ..." if not wait: waitstr1 = " &" waitstr2 = "background ..." if self.iswritable(): six.print_("Flushing data and starting casabrowser in the " + waitstr2) else: six.print_("Starting casabrowser in the " + waitstr2) self.flush() self.unlock() if os.system('test -e ' + self.name() + '/table.dat') == 0: os.system('casabrowser ' + self.name() + waitstr1) elif len(tempname) > 0: six.print_(" making a persistent copy in table " + tempname) self.copy(tempname) os.system('casabrowser ' + tempname + waitstr1) if wait: from casacore.tables import tabledelete six.print_(" finished browsing") tabledelete(tempname) else: six.print_(" after browsing use tabledelete('" + tempname + "') to delete the copy") else: six.print_("Cannot browse because the table is in memory only") six.print_("You can browse a (shallow) persistent copy " + "of the table like: ") six.print_(" t.browse(True, '/tmp/tab1')") else: try: import wxPython except ImportError: six.print_('casabrowser nor wxPython can be found') return from wxPython.wx import wxPySimpleApp import sys app = wxPySimpleApp() from wxtablebrowser import CasaTestFrame frame = CasaTestFrame(None, sys.stdout, self) frame.Show(True) app.MainLoop()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:view; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:wait; 7, True; 8, default_parameter; 8, 9; 8, 10; 9, identifier:tempname; 10, string:"/tmp/seltable"; 11, block; 11, 12; 11, 14; 11, 17; 11, 18; 11, 19; 11, 20; 11, 24; 11, 34; 11, 251; 11, 252; 12, expression_statement; 12, 13; 13, comment; 14, import_statement; 14, 15; 15, dotted_name; 15, 16; 16, identifier:os; 17, comment; 18, comment; 19, comment; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:viewed; 23, False; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:type; 27, subscript; 27, 28; 27, 33; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:info; 32, argument_list; 33, string:"type"; 34, if_statement; 34, 35; 34, 42; 35, boolean_operator:or; 35, 36; 35, 39; 36, comparison_operator:==; 36, 37; 36, 38; 37, identifier:type; 38, string:"Measurement Set"; 39, comparison_operator:==; 39, 40; 39, 41; 40, identifier:type; 41, string:"Image"; 42, block; 42, 43; 43, if_statement; 43, 44; 43, 52; 44, comparison_operator:==; 44, 45; 44, 51; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:os; 48, identifier:system; 49, argument_list; 49, 50; 50, string:'test -x `which casaviewer` > /dev/null 2>&1'; 51, integer:0; 52, block; 52, 53; 52, 57; 52, 61; 52, 73; 52, 102; 52, 108; 52, 114; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:waitstr1; 56, string:""; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:waitstr2; 60, string:"foreground ..."; 61, if_statement; 61, 62; 61, 64; 62, not_operator; 62, 63; 63, identifier:wait; 64, block; 64, 65; 64, 69; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:waitstr1; 68, string:" &"; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:waitstr2; 72, string:"background ..."; 73, if_statement; 73, 74; 73, 79; 73, 91; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:iswritable; 78, argument_list; 79, block; 79, 80; 80, expression_statement; 80, 81; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:six; 84, identifier:print_; 85, argument_list; 85, 86; 86, binary_operator:+; 86, 87; 86, 90; 87, binary_operator:+; 87, 88; 87, 89; 88, string:"Flushing data and starting casaviewer "; 89, string:"in the "; 90, identifier:waitstr2; 91, else_clause; 91, 92; 92, block; 92, 93; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:six; 97, identifier:print_; 98, argument_list; 98, 99; 99, binary_operator:+; 99, 100; 99, 101; 100, string:"Starting casaviewer in the "; 101, identifier:waitstr2; 102, expression_statement; 102, 103; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:self; 106, identifier:flush; 107, argument_list; 108, expression_statement; 108, 109; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:self; 112, identifier:unlock; 113, argument_list; 114, if_statement; 114, 115; 114, 131; 114, 151; 114, 224; 115, comparison_operator:==; 115, 116; 115, 130; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:os; 119, identifier:system; 120, argument_list; 120, 121; 121, binary_operator:+; 121, 122; 121, 129; 122, binary_operator:+; 122, 123; 122, 124; 123, string:'test -e '; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:self; 127, identifier:name; 128, argument_list; 129, string:'/table.dat'; 130, integer:0; 131, block; 131, 132; 131, 147; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:os; 136, identifier:system; 137, argument_list; 137, 138; 138, binary_operator:+; 138, 139; 138, 146; 139, binary_operator:+; 139, 140; 139, 141; 140, string:'casaviewer '; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:self; 144, identifier:name; 145, argument_list; 146, identifier:waitstr1; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:viewed; 150, True; 151, elif_clause; 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:tempname; 157, integer:0; 158, block; 158, 159; 158, 168; 158, 175; 158, 186; 158, 190; 159, expression_statement; 159, 160; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:six; 163, identifier:print_; 164, argument_list; 164, 165; 165, binary_operator:+; 165, 166; 165, 167; 166, string:" making a persistent copy in table "; 167, identifier:tempname; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:self; 172, identifier:copy; 173, argument_list; 173, 174; 174, identifier:tempname; 175, expression_statement; 175, 176; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:os; 179, identifier:system; 180, argument_list; 180, 181; 181, binary_operator:+; 181, 182; 181, 185; 182, binary_operator:+; 182, 183; 182, 184; 183, string:'casaviewer '; 184, identifier:tempname; 185, identifier:waitstr1; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:viewed; 189, True; 190, if_statement; 190, 191; 190, 192; 190, 211; 191, identifier:wait; 192, block; 192, 193; 192, 199; 192, 206; 193, import_from_statement; 193, 194; 193, 197; 194, dotted_name; 194, 195; 194, 196; 195, identifier:casacore; 196, identifier:tables; 197, dotted_name; 197, 198; 198, identifier:tabledelete; 199, expression_statement; 199, 200; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:six; 203, identifier:print_; 204, argument_list; 204, 205; 205, string:" finished viewing"; 206, expression_statement; 206, 207; 207, call; 207, 208; 207, 209; 208, identifier:tabledelete; 209, argument_list; 209, 210; 210, identifier:tempname; 211, else_clause; 211, 212; 212, block; 212, 213; 213, expression_statement; 213, 214; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:six; 217, identifier:print_; 218, argument_list; 218, 219; 219, binary_operator:+; 219, 220; 219, 223; 220, binary_operator:+; 220, 221; 220, 222; 221, string:" after viewing use tabledelete('"; 222, identifier:tempname; 223, string:"') to delete the copy"; 224, else_clause; 224, 225; 225, block; 225, 226; 225, 235; 225, 244; 226, expression_statement; 226, 227; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:six; 230, identifier:print_; 231, argument_list; 231, 232; 232, binary_operator:+; 232, 233; 232, 234; 233, string:"Cannot browse because the table is "; 234, string:"in memory only."; 235, expression_statement; 235, 236; 236, call; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:six; 239, identifier:print_; 240, argument_list; 240, 241; 241, binary_operator:+; 241, 242; 241, 243; 242, string:"You can browse a (shallow) persistent "; 243, string:"copy of the table like:"; 244, expression_statement; 244, 245; 245, call; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:six; 248, identifier:print_; 249, argument_list; 249, 250; 250, string:" t.view(True, '/tmp/tab1')"; 251, comment; 252, if_statement; 252, 253; 252, 255; 253, not_operator; 253, 254; 254, identifier:viewed; 255, block; 255, 256; 256, expression_statement; 256, 257; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:self; 260, identifier:browse; 261, argument_list; 261, 262; 261, 263; 262, identifier:wait; 263, identifier:tempname
def view(self, wait=True, tempname="/tmp/seltable"): """ View a table using casaviewer, casabrowser, or wxwidget based browser. The table is viewed depending on the type: MeasurementSet is viewed using casaviewer. Image is viewed using casaviewer. other are browsed using the :func:`browse` function. If the casaviewer cannot be found, all tables are browsed. The casaviewer can only display tables that are persistent on disk. This gives problems for tables resulting from a query because they are held in memory only (unless an output table name was given). To make viewing of such tables possible, the argument `tempname` can be used to specify a table name that will be used to form a persistent table that can be browsed. Note that such a table is very small as it does not contain data, but only references to rows in the original table. The default for `tempname` is '/tmp/seltable'. If needed, the table can be deleted using the :func:`tabledelete` function. If `wait=False`, the casaviewer is started in the background. In that case the user should delete a possibly created copy of a temporary table. """ import os # Determine the table type. # Test if casaviewer can be found. # On OS-X 'which' always returns 0, so use test on top of it. viewed = False type = self.info()["type"] if type == "Measurement Set" or type == "Image": if os.system('test -x `which casaviewer` > /dev/null 2>&1') == 0: waitstr1 = "" waitstr2 = "foreground ..." if not wait: waitstr1 = " &" waitstr2 = "background ..." if self.iswritable(): six.print_("Flushing data and starting casaviewer " + "in the " + waitstr2) else: six.print_("Starting casaviewer in the " + waitstr2) self.flush() self.unlock() if os.system('test -e ' + self.name() + '/table.dat') == 0: os.system('casaviewer ' + self.name() + waitstr1) viewed = True elif len(tempname) > 0: six.print_(" making a persistent copy in table " + tempname) self.copy(tempname) os.system('casaviewer ' + tempname + waitstr1) viewed = True if wait: from casacore.tables import tabledelete six.print_(" finished viewing") tabledelete(tempname) else: six.print_(" after viewing use tabledelete('" + tempname + "') to delete the copy") else: six.print_("Cannot browse because the table is " + "in memory only.") six.print_("You can browse a (shallow) persistent " + "copy of the table like:") six.print_(" t.view(True, '/tmp/tab1')") # Could not view the table, so browse it. if not viewed: self.browse(wait, tempname)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_repr_html_; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 12; 5, 13; 5, 59; 5, 63; 5, 67; 5, 116; 5, 135; 5, 139; 5, 160; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:out; 11, string:"<table class='taqltable' style='overflow-x:auto'>\n"; 12, comment; 13, if_statement; 13, 14; 13, 34; 14, not_operator; 14, 15; 15, parenthesized_expression; 15, 16; 16, call; 16, 17; 16, 18; 17, identifier:all; 18, argument_list; 18, 19; 19, list_comprehension; 19, 20; 19, 27; 20, comparison_operator:==; 20, 21; 20, 26; 21, subscript; 21, 22; 21, 23; 22, identifier:colname; 23, slice; 23, 24; 23, 25; 24, colon; 25, integer:4; 26, string:"Col_"; 27, for_in_clause; 27, 28; 27, 29; 28, identifier:colname; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:colnames; 33, argument_list; 34, block; 34, 35; 34, 39; 34, 55; 35, expression_statement; 35, 36; 36, augmented_assignment:+=; 36, 37; 36, 38; 37, identifier:out; 38, string:"<tr>"; 39, for_statement; 39, 40; 39, 41; 39, 46; 40, identifier:colname; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:self; 44, identifier:colnames; 45, argument_list; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, augmented_assignment:+=; 48, 49; 48, 50; 49, identifier:out; 50, binary_operator:+; 50, 51; 50, 54; 51, binary_operator:+; 51, 52; 51, 53; 52, string:"<th><b>"; 53, identifier:colname; 54, string:"</b></th>"; 55, expression_statement; 55, 56; 56, augmented_assignment:+=; 56, 57; 56, 58; 57, identifier:out; 58, string:"</tr>"; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:cropped; 62, False; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:rowcount; 66, integer:0; 67, for_statement; 67, 68; 67, 69; 67, 70; 68, identifier:row; 69, identifier:self; 70, block; 70, 71; 70, 84; 70, 88; 70, 92; 70, 102; 70, 106; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:rowout; 74, call; 74, 75; 74, 76; 75, identifier:_format_row; 76, argument_list; 76, 77; 76, 78; 76, 83; 77, identifier:row; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:self; 81, identifier:colnames; 82, argument_list; 83, identifier:self; 84, expression_statement; 84, 85; 85, augmented_assignment:+=; 85, 86; 85, 87; 86, identifier:rowcount; 87, integer:1; 88, expression_statement; 88, 89; 89, augmented_assignment:+=; 89, 90; 89, 91; 90, identifier:out; 91, identifier:rowout; 92, if_statement; 92, 93; 92, 96; 92, 97; 93, comparison_operator:in; 93, 94; 93, 95; 94, string:"\n"; 95, identifier:rowout; 96, comment; 97, block; 97, 98; 98, expression_statement; 98, 99; 99, augmented_assignment:+=; 99, 100; 99, 101; 100, identifier:out; 101, string:"\n"; 102, expression_statement; 102, 103; 103, augmented_assignment:+=; 103, 104; 103, 105; 104, identifier:out; 105, string:"\n"; 106, if_statement; 106, 107; 106, 110; 107, comparison_operator:>=; 107, 108; 107, 109; 108, identifier:rowcount; 109, integer:20; 110, block; 110, 111; 110, 115; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:cropped; 114, True; 115, break_statement; 116, if_statement; 116, 117; 116, 125; 117, comparison_operator:==; 117, 118; 117, 124; 118, subscript; 118, 119; 118, 120; 119, identifier:out; 120, slice; 120, 121; 120, 123; 121, unary_operator:-; 121, 122; 122, integer:2; 123, colon; 124, string:"\n\n"; 125, block; 125, 126; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:out; 129, subscript; 129, 130; 129, 131; 130, identifier:out; 131, slice; 131, 132; 131, 133; 132, colon; 133, unary_operator:-; 133, 134; 134, integer:1; 135, expression_statement; 135, 136; 136, augmented_assignment:+=; 136, 137; 136, 138; 137, identifier:out; 138, string:"</table>"; 139, if_statement; 139, 140; 139, 141; 140, identifier:cropped; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, augmented_assignment:+=; 143, 144; 143, 145; 144, identifier:out; 145, parenthesized_expression; 145, 146; 146, binary_operator:+; 146, 147; 146, 159; 147, binary_operator:+; 147, 148; 147, 149; 148, string:"<p style='text-align:center'>("; 149, call; 149, 150; 149, 151; 150, identifier:str; 151, argument_list; 151, 152; 152, binary_operator:-; 152, 153; 152, 158; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:nrows; 157, argument_list; 158, integer:20; 159, string:" more rows)</p>\n"; 160, return_statement; 160, 161; 161, identifier:out
def _repr_html_(self): """Give a nice representation of tables in notebooks.""" out = "<table class='taqltable' style='overflow-x:auto'>\n" # Print column names (not if they are all auto-generated) if not(all([colname[:4] == "Col_" for colname in self.colnames()])): out += "<tr>" for colname in self.colnames(): out += "<th><b>"+colname+"</b></th>" out += "</tr>" cropped = False rowcount = 0 for row in self: rowout = _format_row(row, self.colnames(), self) rowcount += 1 out += rowout if "\n" in rowout: # Double space after multiline rows out += "\n" out += "\n" if rowcount >= 20: cropped = True break if out[-2:] == "\n\n": out = out[:-1] out += "</table>" if cropped: out += ("<p style='text-align:center'>(" + str(self.nrows()-20)+" more rows)</p>\n") return out
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:handle_import; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:name; 6, identifier:compilation; 7, identifier:rule; 8, block; 8, 9; 8, 11; 8, 12; 8, 13; 8, 14; 8, 15; 8, 16; 8, 17; 8, 18; 8, 25; 8, 36; 8, 61; 8, 67; 8, 71; 8, 72; 8, 123; 8, 139; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, comment; 13, comment; 14, comment; 15, comment; 16, comment; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:path; 21, call; 21, 22; 21, 23; 22, identifier:PurePosixPath; 23, argument_list; 23, 24; 24, identifier:name; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:search_exts; 28, call; 28, 29; 28, 30; 29, identifier:list; 30, argument_list; 30, 31; 31, attribute; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:compilation; 34, identifier:compiler; 35, identifier:dynamic_extensions; 36, if_statement; 36, 37; 36, 46; 36, 53; 37, boolean_operator:and; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:path; 40, identifier:suffix; 41, comparison_operator:in; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:path; 44, identifier:suffix; 45, identifier:search_exts; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:basename; 50, attribute; 50, 51; 50, 52; 51, identifier:path; 52, identifier:stem; 53, else_clause; 53, 54; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:basename; 58, attribute; 58, 59; 58, 60; 59, identifier:path; 60, identifier:name; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:relative_to; 64, attribute; 64, 65; 64, 66; 65, identifier:path; 66, identifier:parent; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:search_path; 70, list:[]; 71, comment; 72, if_statement; 72, 73; 72, 78; 72, 93; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:relative_to; 76, identifier:is_absolute; 77, argument_list; 78, block; 78, 79; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:relative_to; 82, call; 82, 83; 82, 84; 83, identifier:PurePosixPath; 84, argument_list; 84, 85; 85, list_splat; 85, 86; 86, subscript; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:relative_to; 89, identifier:parts; 90, slice; 90, 91; 90, 92; 91, integer:1; 92, colon; 93, elif_clause; 93, 94; 93, 99; 93, 100; 93, 101; 94, attribute; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:rule; 97, identifier:source_file; 98, identifier:origin; 99, comment; 100, comment; 101, block; 101, 102; 102, expression_statement; 102, 103; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:search_path; 106, identifier:append; 107, argument_list; 107, 108; 108, tuple; 108, 109; 108, 114; 109, attribute; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:rule; 112, identifier:source_file; 113, identifier:origin; 114, binary_operator:/; 114, 115; 114, 122; 115, attribute; 115, 116; 115, 121; 116, attribute; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:rule; 119, identifier:source_file; 120, identifier:relpath; 121, identifier:parent; 122, identifier:relative_to; 123, expression_statement; 123, 124; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:search_path; 127, identifier:extend; 128, generator_expression; 128, 129; 128, 132; 129, tuple; 129, 130; 129, 131; 130, identifier:origin; 131, identifier:relative_to; 132, for_in_clause; 132, 133; 132, 134; 133, identifier:origin; 134, attribute; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:compilation; 137, identifier:compiler; 138, identifier:search_path; 139, for_statement; 139, 140; 139, 143; 139, 150; 140, pattern_list; 140, 141; 140, 142; 141, identifier:prefix; 142, identifier:suffix; 143, call; 143, 144; 143, 145; 144, identifier:product; 145, argument_list; 145, 146; 145, 149; 146, tuple; 146, 147; 146, 148; 147, string:'_'; 148, string:''; 149, identifier:search_exts; 150, block; 150, 151; 150, 159; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:filename; 154, binary_operator:+; 154, 155; 154, 158; 155, binary_operator:+; 155, 156; 155, 157; 156, identifier:prefix; 157, identifier:basename; 158, identifier:suffix; 159, for_statement; 159, 160; 159, 163; 159, 164; 160, pattern_list; 160, 161; 160, 162; 161, identifier:origin; 162, identifier:relative_to; 163, identifier:search_path; 164, block; 164, 165; 164, 171; 164, 172; 164, 173; 164, 174; 164, 191; 164, 205; 164, 211; 164, 220; 164, 221; 164, 222; 164, 223; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:relpath; 168, binary_operator:/; 168, 169; 168, 170; 169, identifier:relative_to; 170, identifier:filename; 171, comment; 172, comment; 173, comment; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:relpath; 177, call; 177, 178; 177, 179; 178, identifier:PurePosixPath; 179, argument_list; 179, 180; 180, call; 180, 181; 180, 186; 181, attribute; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:os; 184, identifier:path; 185, identifier:normpath; 186, argument_list; 186, 187; 187, call; 187, 188; 187, 189; 188, identifier:str; 189, argument_list; 189, 190; 190, identifier:relpath; 191, if_statement; 191, 192; 191, 201; 191, 202; 191, 203; 192, comparison_operator:==; 192, 193; 192, 198; 193, attribute; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:rule; 196, identifier:source_file; 197, identifier:key; 198, tuple; 198, 199; 198, 200; 199, identifier:origin; 200, identifier:relpath; 201, comment; 202, comment; 203, block; 203, 204; 204, continue_statement; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:path; 208, binary_operator:/; 208, 209; 208, 210; 209, identifier:origin; 210, identifier:relpath; 211, if_statement; 211, 212; 211, 218; 212, not_operator; 212, 213; 213, call; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:path; 216, identifier:exists; 217, argument_list; 218, block; 218, 219; 219, continue_statement; 220, comment; 221, comment; 222, comment; 223, return_statement; 223, 224; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:SourceFile; 227, identifier:read; 228, argument_list; 228, 229; 228, 230; 229, identifier:origin; 230, identifier:relpath
def handle_import(self, name, compilation, rule): """Implementation of the core Sass import mechanism, which just looks for files on disk. """ # TODO this is all not terribly well-specified by Sass. at worst, # it's unclear how far "upwards" we should be allowed to go. but i'm # also a little fuzzy on e.g. how relative imports work from within a # file that's not actually in the search path. # TODO i think with the new origin semantics, i've made it possible to # import relative to the current file even if the current file isn't # anywhere in the search path. is that right? path = PurePosixPath(name) search_exts = list(compilation.compiler.dynamic_extensions) if path.suffix and path.suffix in search_exts: basename = path.stem else: basename = path.name relative_to = path.parent search_path = [] # tuple of (origin, start_from) if relative_to.is_absolute(): relative_to = PurePosixPath(*relative_to.parts[1:]) elif rule.source_file.origin: # Search relative to the current file first, only if not doing an # absolute import search_path.append(( rule.source_file.origin, rule.source_file.relpath.parent / relative_to, )) search_path.extend( (origin, relative_to) for origin in compilation.compiler.search_path ) for prefix, suffix in product(('_', ''), search_exts): filename = prefix + basename + suffix for origin, relative_to in search_path: relpath = relative_to / filename # Lexically (ignoring symlinks!) eliminate .. from the part # of the path that exists within Sass-space. pathlib # deliberately doesn't do this, but os.path does. relpath = PurePosixPath(os.path.normpath(str(relpath))) if rule.source_file.key == (origin, relpath): # Avoid self-import # TODO is this what ruby does? continue path = origin / relpath if not path.exists(): continue # All good! # TODO if this file has already been imported, we'll do the # source preparation twice. make it lazy. return SourceFile.read(origin, relpath)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_get_properties; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:rule; 6, identifier:scope; 7, identifier:block; 8, block; 8, 9; 8, 11; 8, 33; 8, 46; 8, 69; 8, 78; 8, 89; 8, 97; 8, 106; 8, 111; 8, 120; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 16; 13, pattern_list; 13, 14; 13, 15; 14, identifier:prop; 15, identifier:raw_value; 16, subscript; 16, 17; 16, 30; 17, parenthesized_expression; 17, 18; 18, binary_operator:+; 18, 19; 18, 28; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:_prop_split_re; 22, identifier:split; 23, argument_list; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:block; 26, identifier:prop; 27, integer:1; 28, list:[None]; 28, 29; 29, None; 30, slice; 30, 31; 30, 32; 31, colon; 32, integer:2; 33, if_statement; 33, 34; 33, 37; 34, comparison_operator:is; 34, 35; 34, 36; 35, identifier:raw_value; 36, None; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:raw_value; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:raw_value; 44, identifier:strip; 45, argument_list; 46, try_statement; 46, 47; 46, 62; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:is_var; 51, parenthesized_expression; 51, 52; 52, comparison_operator:==; 52, 53; 52, 61; 53, subscript; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:block; 56, identifier:prop; 57, call; 57, 58; 57, 59; 58, identifier:len; 59, argument_list; 59, 60; 60, identifier:prop; 61, string:'='; 62, except_clause; 62, 63; 62, 64; 63, identifier:IndexError; 64, block; 64, 65; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:is_var; 68, False; 69, if_statement; 69, 70; 69, 71; 70, identifier:is_var; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:warn_deprecated; 75, argument_list; 75, 76; 75, 77; 76, identifier:rule; 77, string:"Assignment with = is deprecated; use : instead."; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:calculator; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:_make_calculator; 85, argument_list; 85, 86; 86, attribute; 86, 87; 86, 88; 87, identifier:rule; 88, identifier:namespace; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:prop; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:prop; 95, identifier:strip; 96, argument_list; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:prop; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:calculator; 103, identifier:do_glob_math; 104, argument_list; 104, 105; 105, identifier:prop; 106, if_statement; 106, 107; 106, 109; 107, not_operator; 107, 108; 108, identifier:prop; 109, block; 109, 110; 110, return_statement; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:_prop; 114, binary_operator:+; 114, 115; 114, 119; 115, parenthesized_expression; 115, 116; 116, boolean_operator:or; 116, 117; 116, 118; 117, identifier:scope; 118, string:''; 119, identifier:prop; 120, if_statement; 120, 121; 120, 133; 120, 134; 120, 313; 121, boolean_operator:or; 121, 122; 121, 123; 122, identifier:is_var; 123, boolean_operator:and; 123, 124; 123, 130; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:prop; 127, identifier:startswith; 128, argument_list; 128, 129; 129, string:'$'; 130, comparison_operator:is; 130, 131; 130, 132; 131, identifier:raw_value; 132, None; 133, comment; 134, block; 134, 135; 134, 139; 134, 143; 134, 144; 134, 212; 134, 213; 134, 220; 134, 240; 134, 251; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:is_default; 138, False; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:is_global; 142, True; 143, comment; 144, while_statement; 144, 145; 144, 146; 145, True; 146, block; 146, 147; 146, 157; 146, 176; 146, 182; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:splits; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:raw_value; 153, identifier:rsplit; 154, argument_list; 154, 155; 154, 156; 155, None; 156, integer:1; 157, if_statement; 157, 158; 157, 174; 158, boolean_operator:or; 158, 159; 158, 165; 159, comparison_operator:<; 159, 160; 159, 164; 160, call; 160, 161; 160, 162; 161, identifier:len; 162, argument_list; 162, 163; 163, identifier:splits; 164, integer:2; 165, not_operator; 165, 166; 166, call; 166, 167; 166, 172; 167, attribute; 167, 168; 167, 171; 168, subscript; 168, 169; 168, 170; 169, identifier:splits; 170, integer:1; 171, identifier:startswith; 172, argument_list; 172, 173; 173, string:'!'; 174, block; 174, 175; 175, break_statement; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 181; 178, pattern_list; 178, 179; 178, 180; 179, identifier:raw_value; 180, identifier:flag; 181, identifier:splits; 182, if_statement; 182, 183; 182, 186; 182, 191; 182, 200; 183, comparison_operator:==; 183, 184; 183, 185; 184, identifier:flag; 185, string:'!default'; 186, block; 186, 187; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:is_default; 190, True; 191, elif_clause; 191, 192; 191, 195; 192, comparison_operator:==; 192, 193; 192, 194; 193, identifier:flag; 194, string:'!global'; 195, block; 195, 196; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 199; 198, identifier:is_global; 199, True; 200, else_clause; 200, 201; 201, block; 201, 202; 202, raise_statement; 202, 203; 203, call; 203, 204; 203, 205; 204, identifier:ValueError; 205, argument_list; 205, 206; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, string:"Unrecognized flag: {0}"; 209, identifier:format; 210, argument_list; 210, 211; 211, identifier:flag; 212, comment; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 216; 215, identifier:_prop; 216, call; 216, 217; 216, 218; 217, identifier:normalize_var; 218, argument_list; 218, 219; 219, identifier:_prop; 220, try_statement; 220, 221; 220, 233; 221, block; 221, 222; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:existing_value; 225, call; 225, 226; 225, 231; 226, attribute; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:rule; 229, identifier:namespace; 230, identifier:variable; 231, argument_list; 231, 232; 232, identifier:_prop; 233, except_clause; 233, 234; 233, 235; 234, identifier:KeyError; 235, block; 235, 236; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 239; 238, identifier:existing_value; 239, None; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:is_defined; 243, boolean_operator:and; 243, 244; 243, 247; 244, comparison_operator:is; 244, 245; 244, 246; 245, identifier:existing_value; 246, None; 247, not_operator; 247, 248; 248, attribute; 248, 249; 248, 250; 249, identifier:existing_value; 250, identifier:is_null; 251, if_statement; 251, 252; 251, 255; 251, 257; 252, boolean_operator:and; 252, 253; 252, 254; 253, identifier:is_default; 254, identifier:is_defined; 255, block; 255, 256; 256, pass_statement; 257, else_clause; 257, 258; 258, block; 258, 259; 258, 285; 258, 286; 258, 287; 258, 299; 259, if_statement; 259, 260; 259, 276; 260, boolean_operator:and; 260, 261; 260, 269; 261, boolean_operator:and; 261, 262; 261, 263; 262, identifier:is_defined; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:prop; 266, identifier:startswith; 267, argument_list; 267, 268; 268, string:'$'; 269, call; 269, 270; 269, 275; 270, attribute; 270, 271; 270, 274; 271, subscript; 271, 272; 271, 273; 272, identifier:prop; 273, integer:1; 274, identifier:isupper; 275, argument_list; 276, block; 276, 277; 277, expression_statement; 277, 278; 278, call; 278, 279; 278, 282; 279, attribute; 279, 280; 279, 281; 280, identifier:log; 281, identifier:warn; 282, argument_list; 282, 283; 282, 284; 283, string:"Constant %r redefined"; 284, identifier:prop; 285, comment; 286, comment; 287, expression_statement; 287, 288; 288, assignment; 288, 289; 288, 290; 289, identifier:value; 290, call; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:calculator; 293, identifier:calculate; 294, argument_list; 294, 295; 294, 296; 295, identifier:raw_value; 296, keyword_argument; 296, 297; 296, 298; 297, identifier:divide; 298, True; 299, expression_statement; 299, 300; 300, call; 300, 301; 300, 306; 301, attribute; 301, 302; 301, 305; 302, attribute; 302, 303; 302, 304; 303, identifier:rule; 304, identifier:namespace; 305, identifier:set_variable; 306, argument_list; 306, 307; 306, 308; 306, 309; 307, identifier:_prop; 308, identifier:value; 309, keyword_argument; 309, 310; 309, 311; 310, identifier:local_only; 311, not_operator; 311, 312; 312, identifier:is_global; 313, else_clause; 313, 314; 313, 315; 314, comment; 315, block; 315, 316; 315, 325; 315, 345; 315, 403; 316, expression_statement; 316, 317; 317, assignment; 317, 318; 317, 319; 318, identifier:_prop; 319, call; 319, 320; 319, 323; 320, attribute; 320, 321; 320, 322; 321, identifier:calculator; 322, identifier:apply_vars; 323, argument_list; 323, 324; 324, identifier:_prop; 325, if_statement; 325, 326; 325, 329; 325, 334; 326, comparison_operator:is; 326, 327; 326, 328; 327, identifier:raw_value; 328, None; 329, block; 329, 330; 330, expression_statement; 330, 331; 331, assignment; 331, 332; 331, 333; 332, identifier:value; 333, None; 334, else_clause; 334, 335; 335, block; 335, 336; 336, expression_statement; 336, 337; 337, assignment; 337, 338; 337, 339; 338, identifier:value; 339, call; 339, 340; 339, 343; 340, attribute; 340, 341; 340, 342; 341, identifier:calculator; 342, identifier:calculate; 343, argument_list; 343, 344; 344, identifier:raw_value; 345, if_statement; 345, 346; 345, 349; 345, 351; 345, 362; 346, comparison_operator:is; 346, 347; 346, 348; 347, identifier:value; 348, None; 349, block; 349, 350; 350, pass_statement; 351, elif_clause; 351, 352; 351, 359; 351, 360; 352, call; 352, 353; 352, 354; 353, identifier:isinstance; 354, argument_list; 354, 355; 354, 356; 355, identifier:value; 356, attribute; 356, 357; 356, 358; 357, identifier:six; 358, identifier:string_types; 359, comment; 360, block; 360, 361; 361, pass_statement; 362, else_clause; 362, 363; 363, block; 363, 364; 363, 370; 363, 386; 363, 392; 364, if_statement; 364, 365; 364, 368; 365, attribute; 365, 366; 365, 367; 366, identifier:value; 367, identifier:is_null; 368, block; 368, 369; 369, return_statement; 370, expression_statement; 370, 371; 371, assignment; 371, 372; 371, 373; 372, identifier:style; 373, call; 373, 374; 373, 379; 374, attribute; 374, 375; 374, 378; 375, attribute; 375, 376; 375, 377; 376, identifier:rule; 377, identifier:legacy_compiler_options; 378, identifier:get; 379, argument_list; 379, 380; 379, 381; 380, string:'style'; 381, attribute; 381, 382; 381, 385; 382, attribute; 382, 383; 382, 384; 383, identifier:self; 384, identifier:compiler; 385, identifier:output_style; 386, expression_statement; 386, 387; 387, assignment; 387, 388; 387, 389; 388, identifier:compress; 389, comparison_operator:==; 389, 390; 389, 391; 390, identifier:style; 391, string:'compressed'; 392, expression_statement; 392, 393; 393, assignment; 393, 394; 393, 395; 394, identifier:value; 395, call; 395, 396; 395, 399; 396, attribute; 396, 397; 396, 398; 397, identifier:value; 398, identifier:render; 399, argument_list; 399, 400; 400, keyword_argument; 400, 401; 400, 402; 401, identifier:compress; 402, identifier:compress; 403, expression_statement; 403, 404; 404, call; 404, 405; 404, 410; 405, attribute; 405, 406; 405, 409; 406, attribute; 406, 407; 406, 408; 407, identifier:rule; 408, identifier:properties; 409, identifier:append; 410, argument_list; 410, 411; 411, tuple; 411, 412; 411, 413; 412, identifier:_prop; 413, identifier:value
def _get_properties(self, rule, scope, block): """ Implements properties and variables extraction and assignment """ prop, raw_value = (_prop_split_re.split(block.prop, 1) + [None])[:2] if raw_value is not None: raw_value = raw_value.strip() try: is_var = (block.prop[len(prop)] == '=') except IndexError: is_var = False if is_var: warn_deprecated(rule, "Assignment with = is deprecated; use : instead.") calculator = self._make_calculator(rule.namespace) prop = prop.strip() prop = calculator.do_glob_math(prop) if not prop: return _prop = (scope or '') + prop if is_var or prop.startswith('$') and raw_value is not None: # Pop off any flags: !default, !global is_default = False is_global = True # eventually sass will default this to false while True: splits = raw_value.rsplit(None, 1) if len(splits) < 2 or not splits[1].startswith('!'): break raw_value, flag = splits if flag == '!default': is_default = True elif flag == '!global': is_global = True else: raise ValueError("Unrecognized flag: {0}".format(flag)) # Variable assignment _prop = normalize_var(_prop) try: existing_value = rule.namespace.variable(_prop) except KeyError: existing_value = None is_defined = existing_value is not None and not existing_value.is_null if is_default and is_defined: pass else: if is_defined and prop.startswith('$') and prop[1].isupper(): log.warn("Constant %r redefined", prop) # Variable assignment is an expression, so it always performs # real division value = calculator.calculate(raw_value, divide=True) rule.namespace.set_variable( _prop, value, local_only=not is_global) else: # Regular property destined for output _prop = calculator.apply_vars(_prop) if raw_value is None: value = None else: value = calculator.calculate(raw_value) if value is None: pass elif isinstance(value, six.string_types): # TODO kill this branch pass else: if value.is_null: return style = rule.legacy_compiler_options.get( 'style', self.compiler.output_style) compress = style == 'compressed' value = value.render(compress=compress) rule.properties.append((_prop, value))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:longest_common_subsequence; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:a; 5, identifier:b; 6, default_parameter; 6, 7; 6, 8; 7, identifier:mergefunc; 8, None; 9, block; 9, 10; 9, 12; 9, 33; 9, 34; 9, 35; 9, 39; 9, 68; 9, 69; 9, 70; 9, 74; 9, 159; 9, 160; 9, 161; 9, 162; 9, 163; 9, 164; 9, 165; 9, 166; 9, 175; 9, 184; 9, 188; 9, 252; 9, 253; 9, 259; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 16; 12, 17; 13, comparison_operator:is; 13, 14; 13, 15; 14, identifier:mergefunc; 15, None; 16, comment; 17, block; 17, 18; 18, function_definition; 18, 19; 18, 20; 18, 23; 19, function_name:mergefunc; 20, parameters; 20, 21; 20, 22; 21, identifier:a; 22, identifier:b; 23, block; 23, 24; 23, 31; 24, if_statement; 24, 25; 24, 28; 25, comparison_operator:==; 25, 26; 25, 27; 26, identifier:a; 27, identifier:b; 28, block; 28, 29; 29, return_statement; 29, 30; 30, identifier:a; 31, return_statement; 31, 32; 32, None; 33, comment; 34, comment; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:eq; 38, dictionary; 39, for_statement; 39, 40; 39, 43; 39, 47; 40, pattern_list; 40, 41; 40, 42; 41, identifier:ai; 42, identifier:aval; 43, call; 43, 44; 43, 45; 44, identifier:enumerate; 45, argument_list; 45, 46; 46, identifier:a; 47, block; 47, 48; 48, for_statement; 48, 49; 48, 52; 48, 56; 49, pattern_list; 49, 50; 49, 51; 50, identifier:bi; 51, identifier:bval; 52, call; 52, 53; 52, 54; 53, identifier:enumerate; 54, argument_list; 54, 55; 55, identifier:b; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 63; 59, subscript; 59, 60; 59, 61; 59, 62; 60, identifier:eq; 61, identifier:ai; 62, identifier:bi; 63, call; 63, 64; 63, 65; 64, identifier:mergefunc; 65, argument_list; 65, 66; 65, 67; 66, identifier:aval; 67, identifier:bval; 68, comment; 69, comment; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:prefix_lcs_length; 73, dictionary; 74, for_statement; 74, 75; 74, 76; 74, 85; 75, identifier:ai; 76, call; 76, 77; 76, 78; 77, identifier:range; 78, argument_list; 78, 79; 78, 81; 79, unary_operator:-; 79, 80; 80, integer:1; 81, call; 81, 82; 81, 83; 82, identifier:len; 83, argument_list; 83, 84; 84, identifier:a; 85, block; 85, 86; 86, for_statement; 86, 87; 86, 88; 86, 97; 87, identifier:bi; 88, call; 88, 89; 88, 90; 89, identifier:range; 90, argument_list; 90, 91; 90, 93; 91, unary_operator:-; 91, 92; 92, integer:1; 93, call; 93, 94; 93, 95; 94, identifier:len; 95, argument_list; 95, 96; 96, identifier:b; 97, block; 97, 98; 97, 152; 98, if_statement; 98, 99; 98, 108; 98, 113; 98, 132; 99, boolean_operator:or; 99, 100; 99, 104; 100, comparison_operator:==; 100, 101; 100, 102; 101, identifier:ai; 102, unary_operator:-; 102, 103; 103, integer:1; 104, comparison_operator:==; 104, 105; 104, 106; 105, identifier:bi; 106, unary_operator:-; 106, 107; 107, integer:1; 108, block; 108, 109; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:l; 112, integer:0; 113, elif_clause; 113, 114; 113, 118; 114, subscript; 114, 115; 114, 116; 114, 117; 115, identifier:eq; 116, identifier:ai; 117, identifier:bi; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:l; 122, binary_operator:+; 122, 123; 122, 131; 123, subscript; 123, 124; 123, 125; 123, 128; 124, identifier:prefix_lcs_length; 125, binary_operator:-; 125, 126; 125, 127; 126, identifier:ai; 127, integer:1; 128, binary_operator:-; 128, 129; 128, 130; 129, identifier:bi; 130, integer:1; 131, integer:1; 132, else_clause; 132, 133; 133, block; 133, 134; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:l; 137, call; 137, 138; 137, 139; 138, identifier:max; 139, argument_list; 139, 140; 139, 146; 140, subscript; 140, 141; 140, 142; 140, 143; 141, identifier:prefix_lcs_length; 142, identifier:ai; 143, binary_operator:-; 143, 144; 143, 145; 144, identifier:bi; 145, integer:1; 146, subscript; 146, 147; 146, 148; 146, 151; 147, identifier:prefix_lcs_length; 148, binary_operator:-; 148, 149; 148, 150; 149, identifier:ai; 150, integer:1; 151, identifier:bi; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 158; 154, subscript; 154, 155; 154, 156; 154, 157; 155, identifier:prefix_lcs_length; 156, identifier:ai; 157, identifier:bi; 158, identifier:l; 159, comment; 160, comment; 161, comment; 162, comment; 163, comment; 164, comment; 165, comment; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:ai; 169, binary_operator:-; 169, 170; 169, 174; 170, call; 170, 171; 170, 172; 171, identifier:len; 172, argument_list; 172, 173; 173, identifier:a; 174, integer:1; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:bi; 178, binary_operator:-; 178, 179; 178, 183; 179, call; 179, 180; 179, 181; 180, identifier:len; 181, argument_list; 181, 182; 182, identifier:b; 183, integer:1; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:ret; 187, list:[]; 188, while_statement; 188, 189; 188, 196; 189, boolean_operator:and; 189, 190; 189, 193; 190, comparison_operator:>=; 190, 191; 190, 192; 191, identifier:ai; 192, integer:0; 193, comparison_operator:>=; 193, 194; 193, 195; 194, identifier:bi; 195, integer:0; 196, block; 196, 197; 196, 204; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:merged; 200, subscript; 200, 201; 200, 202; 200, 203; 201, identifier:eq; 202, identifier:ai; 203, identifier:bi; 204, if_statement; 204, 205; 204, 208; 204, 227; 204, 246; 205, comparison_operator:is; 205, 206; 205, 207; 206, identifier:merged; 207, None; 208, block; 208, 209; 208, 219; 208, 223; 209, expression_statement; 209, 210; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:ret; 213, identifier:append; 214, argument_list; 214, 215; 215, tuple; 215, 216; 215, 217; 215, 218; 216, identifier:ai; 217, identifier:bi; 218, identifier:merged; 219, expression_statement; 219, 220; 220, augmented_assignment:-=; 220, 221; 220, 222; 221, identifier:ai; 222, integer:1; 223, expression_statement; 223, 224; 224, augmented_assignment:-=; 224, 225; 224, 226; 225, identifier:bi; 226, integer:1; 227, elif_clause; 227, 228; 227, 241; 228, comparison_operator:>; 228, 229; 228, 235; 229, subscript; 229, 230; 229, 231; 229, 232; 230, identifier:prefix_lcs_length; 231, identifier:ai; 232, binary_operator:-; 232, 233; 232, 234; 233, identifier:bi; 234, integer:1; 235, subscript; 235, 236; 235, 237; 235, 240; 236, identifier:prefix_lcs_length; 237, binary_operator:-; 237, 238; 237, 239; 238, identifier:ai; 239, integer:1; 240, identifier:bi; 241, block; 241, 242; 242, expression_statement; 242, 243; 243, augmented_assignment:-=; 243, 244; 243, 245; 244, identifier:bi; 245, integer:1; 246, else_clause; 246, 247; 247, block; 247, 248; 248, expression_statement; 248, 249; 249, augmented_assignment:-=; 249, 250; 249, 251; 250, identifier:ai; 251, integer:1; 252, comment; 253, expression_statement; 253, 254; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:ret; 257, identifier:reverse; 258, argument_list; 259, return_statement; 259, 260; 260, identifier:ret
def longest_common_subsequence(a, b, mergefunc=None): """Find the longest common subsequence between two iterables. The longest common subsequence is the core of any diff algorithm: it's the longest sequence of elements that appears in both parent sequences in the same order, but NOT necessarily consecutively. Original algorithm borrowed from Wikipedia: http://en.wikipedia.org/wiki/Longest_common_subsequence_problem#Code_for_the_dynamic_programming_solution This function is used only to implement @extend, largely because that's what the Ruby implementation does. Thus it's been extended slightly from the simple diff-friendly algorithm given above. What @extend wants to know is whether two simple selectors are compatible, not just equal. To that end, you must pass in a "merge" function to compare a pair of elements manually. It should return `None` if they are incompatible, and a MERGED element if they are compatible -- in the case of selectors, this is whichever one is more specific. Because of this fuzzier notion of equality, the return value is a list of ``(a_index, b_index, value)`` tuples rather than items alone. """ if mergefunc is None: # Stupid default, just in case def mergefunc(a, b): if a == b: return a return None # Precalculate equality, since it can be a tad expensive and every pair is # compared at least once eq = {} for ai, aval in enumerate(a): for bi, bval in enumerate(b): eq[ai, bi] = mergefunc(aval, bval) # Build the "length" matrix, which provides the length of the LCS for # arbitrary-length prefixes. -1 exists only to support the base case prefix_lcs_length = {} for ai in range(-1, len(a)): for bi in range(-1, len(b)): if ai == -1 or bi == -1: l = 0 elif eq[ai, bi]: l = prefix_lcs_length[ai - 1, bi - 1] + 1 else: l = max( prefix_lcs_length[ai, bi - 1], prefix_lcs_length[ai - 1, bi]) prefix_lcs_length[ai, bi] = l # The interesting part. The key insight is that the bottom-right value in # the length matrix must be the length of the LCS because of how the matrix # is defined, so all that's left to do is backtrack from the ends of both # sequences in whatever way keeps the LCS as long as possible, and keep # track of the equal pairs of elements we see along the way. # Wikipedia does this with recursion, but the algorithm is trivial to # rewrite as a loop, as below. ai = len(a) - 1 bi = len(b) - 1 ret = [] while ai >= 0 and bi >= 0: merged = eq[ai, bi] if merged is not None: ret.append((ai, bi, merged)) ai -= 1 bi -= 1 elif prefix_lcs_length[ai, bi - 1] > prefix_lcs_length[ai - 1, bi]: bi -= 1 else: ai -= 1 # ret has the latest items first, which is backwards ret.reverse() return ret
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:determine_encoding; 3, parameters; 3, 4; 4, identifier:buf; 5, block; 5, 6; 5, 8; 5, 9; 5, 13; 5, 20; 5, 93; 5, 94; 5, 115; 5, 157; 5, 227; 5, 228; 5, 229; 5, 238; 5, 247; 5, 339; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:bom_encoding; 12, string:'UTF-8'; 13, if_statement; 13, 14; 13, 16; 13, 17; 14, not_operator; 14, 15; 15, identifier:buf; 16, comment; 17, block; 17, 18; 18, return_statement; 18, 19; 19, identifier:bom_encoding; 20, if_statement; 20, 21; 20, 28; 20, 29; 20, 30; 20, 31; 21, call; 21, 22; 21, 23; 22, identifier:isinstance; 23, argument_list; 23, 24; 23, 25; 24, identifier:buf; 25, attribute; 25, 26; 25, 27; 26, identifier:six; 27, identifier:text_type; 28, comment; 29, comment; 30, comment; 31, block; 31, 32; 31, 47; 31, 48; 31, 49; 31, 53; 31, 57; 32, if_statement; 32, 33; 32, 38; 33, comparison_operator:==; 33, 34; 33, 37; 34, subscript; 34, 35; 34, 36; 35, identifier:buf; 36, integer:0; 37, string:'\ufeff'; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:buf; 42, subscript; 42, 43; 42, 44; 43, identifier:buf; 44, slice; 44, 45; 44, 46; 45, integer:0; 46, colon; 47, comment; 48, comment; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:charset_start; 52, string:'@charset "'; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:charset_end; 56, string:'";'; 57, if_statement; 57, 58; 57, 64; 57, 89; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:buf; 61, identifier:startswith; 62, argument_list; 62, 63; 63, identifier:charset_start; 64, block; 64, 65; 64, 72; 64, 82; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:start; 68, call; 68, 69; 68, 70; 69, identifier:len; 70, argument_list; 70, 71; 71, identifier:charset_start; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:end; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:buf; 78, identifier:index; 79, argument_list; 79, 80; 79, 81; 80, identifier:charset_end; 81, identifier:start; 82, return_statement; 82, 83; 83, subscript; 83, 84; 83, 85; 84, identifier:buf; 85, slice; 85, 86; 85, 87; 85, 88; 86, identifier:start; 87, colon; 88, identifier:end; 89, else_clause; 89, 90; 90, block; 90, 91; 91, return_statement; 91, 92; 92, identifier:bom_encoding; 93, comment; 94, if_statement; 94, 95; 94, 102; 95, comparison_operator:==; 95, 96; 95, 101; 96, subscript; 96, 97; 96, 98; 97, identifier:buf; 98, slice; 98, 99; 98, 100; 99, colon; 100, integer:3; 101, string:b'\xef\xbb\xbf'; 102, block; 102, 103; 102, 107; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:bom_encoding; 106, string:'UTF-8'; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:buf; 110, subscript; 110, 111; 110, 112; 111, identifier:buf; 112, slice; 112, 113; 112, 114; 113, integer:3; 114, colon; 115, if_statement; 115, 116; 115, 123; 115, 136; 116, comparison_operator:==; 116, 117; 116, 122; 117, subscript; 117, 118; 117, 119; 118, identifier:buf; 119, slice; 119, 120; 119, 121; 120, colon; 121, integer:4; 122, string:b'\x00\x00\xfe\xff'; 123, block; 123, 124; 123, 128; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:bom_encoding; 127, string:'UTF-32BE'; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:buf; 131, subscript; 131, 132; 131, 133; 132, identifier:buf; 133, slice; 133, 134; 133, 135; 134, integer:4; 135, colon; 136, elif_clause; 136, 137; 136, 144; 137, comparison_operator:==; 137, 138; 137, 143; 138, subscript; 138, 139; 138, 140; 139, identifier:buf; 140, slice; 140, 141; 140, 142; 141, colon; 142, integer:4; 143, string:b'\xff\xfe\x00\x00'; 144, block; 144, 145; 144, 149; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:bom_encoding; 148, string:'UTF-32LE'; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:buf; 152, subscript; 152, 153; 152, 154; 153, identifier:buf; 154, slice; 154, 155; 154, 156; 155, integer:4; 156, colon; 157, if_statement; 157, 158; 157, 165; 157, 171; 157, 185; 157, 206; 158, comparison_operator:==; 158, 159; 158, 164; 159, subscript; 159, 160; 159, 161; 160, identifier:buf; 161, slice; 161, 162; 161, 163; 162, colon; 163, integer:4; 164, string:b'\x00\x00\xff\xfe'; 165, block; 165, 166; 166, raise_statement; 166, 167; 167, call; 167, 168; 167, 169; 168, identifier:UnicodeError; 169, argument_list; 169, 170; 170, string:"UTF-32-2143 is not supported"; 171, elif_clause; 171, 172; 171, 179; 172, comparison_operator:==; 172, 173; 172, 178; 173, subscript; 173, 174; 173, 175; 174, identifier:buf; 175, slice; 175, 176; 175, 177; 176, colon; 177, integer:4; 178, string:b'\xfe\xff\x00\x00'; 179, block; 179, 180; 180, raise_statement; 180, 181; 181, call; 181, 182; 181, 183; 182, identifier:UnicodeError; 183, argument_list; 183, 184; 184, string:"UTF-32-2143 is not supported"; 185, elif_clause; 185, 186; 185, 193; 186, comparison_operator:==; 186, 187; 186, 192; 187, subscript; 187, 188; 187, 189; 188, identifier:buf; 189, slice; 189, 190; 189, 191; 190, colon; 191, integer:2; 192, string:b'\xfe\xff'; 193, block; 193, 194; 193, 198; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:bom_encoding; 197, string:'UTF-16BE'; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:buf; 201, subscript; 201, 202; 201, 203; 202, identifier:buf; 203, slice; 203, 204; 203, 205; 204, integer:2; 205, colon; 206, elif_clause; 206, 207; 206, 214; 207, comparison_operator:==; 207, 208; 207, 213; 208, subscript; 208, 209; 208, 210; 209, identifier:buf; 210, slice; 210, 211; 210, 212; 211, colon; 212, integer:2; 213, string:b'\xff\xfe'; 214, block; 214, 215; 214, 219; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:bom_encoding; 218, string:'UTF-16LE'; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 222; 221, identifier:buf; 222, subscript; 222, 223; 222, 224; 223, identifier:buf; 224, slice; 224, 225; 224, 226; 225, integer:2; 226, colon; 227, comment; 228, comment; 229, expression_statement; 229, 230; 230, assignment; 230, 231; 230, 232; 231, identifier:charset_start; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, string:'@charset "'; 235, identifier:encode; 236, argument_list; 236, 237; 237, identifier:bom_encoding; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 241; 240, identifier:charset_end; 241, call; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, string:'";'; 244, identifier:encode; 245, argument_list; 245, 246; 246, identifier:bom_encoding; 247, if_statement; 247, 248; 247, 254; 247, 332; 248, call; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:buf; 251, identifier:startswith; 252, argument_list; 252, 253; 253, identifier:charset_start; 254, block; 254, 255; 254, 262; 254, 272; 254, 281; 254, 290; 254, 291; 254, 292; 254, 305; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 258; 257, identifier:start; 258, call; 258, 259; 258, 260; 259, identifier:len; 260, argument_list; 260, 261; 261, identifier:charset_start; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 265; 264, identifier:end; 265, call; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, identifier:buf; 268, identifier:index; 269, argument_list; 269, 270; 269, 271; 270, identifier:charset_end; 271, identifier:start; 272, expression_statement; 272, 273; 273, assignment; 273, 274; 273, 275; 274, identifier:encoded_encoding; 275, subscript; 275, 276; 275, 277; 276, identifier:buf; 277, slice; 277, 278; 277, 279; 277, 280; 278, identifier:start; 279, colon; 280, identifier:end; 281, expression_statement; 281, 282; 282, assignment; 282, 283; 282, 284; 283, identifier:encoding; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:encoded_encoding; 287, identifier:decode; 288, argument_list; 288, 289; 289, identifier:bom_encoding; 290, comment; 291, comment; 292, expression_statement; 292, 293; 293, assignment; 293, 294; 293, 295; 294, identifier:encoded_charset; 295, subscript; 295, 296; 295, 297; 296, identifier:buf; 297, slice; 297, 298; 297, 299; 298, colon; 299, binary_operator:+; 299, 300; 299, 301; 300, identifier:end; 301, call; 301, 302; 301, 303; 302, identifier:len; 303, argument_list; 303, 304; 304, identifier:charset_end; 305, if_statement; 305, 306; 305, 320; 306, parenthesized_expression; 306, 307; 307, comparison_operator:!=; 307, 308; 307, 314; 308, call; 308, 309; 308, 312; 309, attribute; 309, 310; 309, 311; 310, identifier:encoded_charset; 311, identifier:decode; 312, argument_list; 312, 313; 313, identifier:encoding; 314, call; 314, 315; 314, 318; 315, attribute; 315, 316; 315, 317; 316, identifier:encoded_charset; 317, identifier:decode; 318, argument_list; 318, 319; 319, identifier:bom_encoding; 320, block; 320, 321; 321, raise_statement; 321, 322; 322, call; 322, 323; 322, 324; 323, identifier:UnicodeError; 324, argument_list; 324, 325; 325, call; 325, 326; 325, 329; 326, attribute; 326, 327; 326, 328; 327, string:"@charset {0} is incompatible with detected encoding {1}"; 328, identifier:format; 329, argument_list; 329, 330; 329, 331; 330, identifier:bom_encoding; 331, identifier:encoding; 332, else_clause; 332, 333; 332, 334; 333, comment; 334, block; 334, 335; 335, expression_statement; 335, 336; 336, assignment; 336, 337; 336, 338; 337, identifier:encoding; 338, identifier:bom_encoding; 339, return_statement; 339, 340; 340, identifier:encoding
def determine_encoding(buf): """Return the appropriate encoding for the given CSS source, according to the CSS charset rules. `buf` may be either a string or bytes. """ # The ultimate default is utf8; bravo, W3C bom_encoding = 'UTF-8' if not buf: # What return bom_encoding if isinstance(buf, six.text_type): # We got a file that, for whatever reason, produces already-decoded # text. Check for the BOM (which is useless now) and believe # whatever's in the @charset. if buf[0] == '\ufeff': buf = buf[0:] # This is pretty similar to the code below, but without any encoding # double-checking. charset_start = '@charset "' charset_end = '";' if buf.startswith(charset_start): start = len(charset_start) end = buf.index(charset_end, start) return buf[start:end] else: return bom_encoding # BOMs if buf[:3] == b'\xef\xbb\xbf': bom_encoding = 'UTF-8' buf = buf[3:] if buf[:4] == b'\x00\x00\xfe\xff': bom_encoding = 'UTF-32BE' buf = buf[4:] elif buf[:4] == b'\xff\xfe\x00\x00': bom_encoding = 'UTF-32LE' buf = buf[4:] if buf[:4] == b'\x00\x00\xff\xfe': raise UnicodeError("UTF-32-2143 is not supported") elif buf[:4] == b'\xfe\xff\x00\x00': raise UnicodeError("UTF-32-2143 is not supported") elif buf[:2] == b'\xfe\xff': bom_encoding = 'UTF-16BE' buf = buf[2:] elif buf[:2] == b'\xff\xfe': bom_encoding = 'UTF-16LE' buf = buf[2:] # The spec requires exactly this syntax; no escapes or extra spaces or # other shenanigans, thank goodness. charset_start = '@charset "'.encode(bom_encoding) charset_end = '";'.encode(bom_encoding) if buf.startswith(charset_start): start = len(charset_start) end = buf.index(charset_end, start) encoded_encoding = buf[start:end] encoding = encoded_encoding.decode(bom_encoding) # Ensure that decoding with the specified encoding actually produces # the same @charset rule encoded_charset = buf[:end + len(charset_end)] if (encoded_charset.decode(encoding) != encoded_charset.decode(bom_encoding)): raise UnicodeError( "@charset {0} is incompatible with detected encoding {1}" .format(bom_encoding, encoding)) else: # With no @charset, believe the BOM encoding = bom_encoding return encoding
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_draw_surfaces; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:surface; 6, identifier:offset; 7, identifier:surfaces; 8, block; 8, 9; 8, 11; 8, 17; 8, 23; 8, 33; 8, 41; 8, 49; 8, 60; 8, 66; 8, 72; 8, 73; 8, 74; 8, 97; 8, 106; 8, 113; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:surface_blit; 14, attribute; 14, 15; 14, 16; 15, identifier:surface; 16, identifier:blit; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 22; 19, pattern_list; 19, 20; 19, 21; 20, identifier:ox; 21, identifier:oy; 22, identifier:offset; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 28; 25, pattern_list; 25, 26; 25, 27; 26, identifier:left; 27, identifier:top; 28, attribute; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:_tile_view; 32, identifier:topleft; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:hit; 36, attribute; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:_layer_quadtree; 40, identifier:hit; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:get_tile; 44, attribute; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:data; 48, identifier:get_tile_image; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:tile_layers; 52, call; 52, 53; 52, 54; 53, identifier:tuple; 54, argument_list; 54, 55; 55, attribute; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:data; 59, identifier:visible_tile_layers; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:dirty; 63, call; 63, 64; 63, 65; 64, identifier:list; 65, argument_list; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:dirty_append; 69, attribute; 69, 70; 69, 71; 70, identifier:dirty; 71, identifier:append; 72, comment; 73, comment; 74, function_definition; 74, 75; 74, 76; 74, 78; 75, function_name:sprite_sort; 76, parameters; 76, 77; 77, identifier:i; 78, block; 78, 79; 79, return_statement; 79, 80; 80, expression_list; 80, 81; 80, 84; 81, subscript; 81, 82; 81, 83; 82, identifier:i; 83, integer:2; 84, binary_operator:+; 84, 85; 84, 90; 85, subscript; 85, 86; 85, 89; 86, subscript; 86, 87; 86, 88; 87, identifier:i; 88, integer:1; 89, integer:1; 90, call; 90, 91; 90, 96; 91, attribute; 91, 92; 91, 95; 92, subscript; 92, 93; 92, 94; 93, identifier:i; 94, integer:0; 95, identifier:get_height; 96, argument_list; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:surfaces; 101, identifier:sort; 102, argument_list; 102, 103; 103, keyword_argument; 103, 104; 103, 105; 104, identifier:key; 105, identifier:sprite_sort; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:layer_getter; 109, call; 109, 110; 109, 111; 110, identifier:itemgetter; 111, argument_list; 111, 112; 112, integer:2; 113, for_statement; 113, 114; 113, 117; 113, 122; 114, pattern_list; 114, 115; 114, 116; 115, identifier:layer; 116, identifier:group; 117, call; 117, 118; 117, 119; 118, identifier:groupby; 119, argument_list; 119, 120; 119, 121; 120, identifier:surfaces; 121, identifier:layer_getter; 122, block; 122, 123; 122, 128; 122, 173; 122, 174; 122, 175; 123, delete_statement; 123, 124; 124, subscript; 124, 125; 124, 126; 125, identifier:dirty; 126, slice; 126, 127; 127, colon; 128, for_statement; 128, 129; 128, 130; 128, 131; 129, identifier:i; 130, identifier:group; 131, block; 131, 132; 132, try_statement; 132, 133; 132, 140; 132, 156; 133, block; 133, 134; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:flags; 137, subscript; 137, 138; 137, 139; 138, identifier:i; 139, integer:3; 140, except_clause; 140, 141; 140, 142; 141, identifier:IndexError; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, call; 144, 145; 144, 146; 145, identifier:dirty_append; 146, argument_list; 146, 147; 147, call; 147, 148; 147, 149; 148, identifier:surface_blit; 149, argument_list; 149, 150; 149, 153; 150, subscript; 150, 151; 150, 152; 151, identifier:i; 152, integer:0; 153, subscript; 153, 154; 153, 155; 154, identifier:i; 155, integer:1; 156, else_clause; 156, 157; 157, block; 157, 158; 158, expression_statement; 158, 159; 159, call; 159, 160; 159, 161; 160, identifier:dirty_append; 161, argument_list; 161, 162; 162, call; 162, 163; 162, 164; 163, identifier:surface_blit; 164, argument_list; 164, 165; 164, 168; 164, 171; 164, 172; 165, subscript; 165, 166; 165, 167; 166, identifier:i; 167, integer:0; 168, subscript; 168, 169; 168, 170; 169, identifier:i; 170, integer:1; 171, None; 172, identifier:flags; 173, comment; 174, comment; 175, for_statement; 175, 176; 175, 177; 175, 178; 176, identifier:dirty_rect; 177, identifier:dirty; 178, block; 178, 179; 179, for_statement; 179, 180; 179, 181; 179, 191; 180, identifier:r; 181, call; 181, 182; 181, 183; 182, identifier:hit; 183, argument_list; 183, 184; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:dirty_rect; 187, identifier:move; 188, argument_list; 188, 189; 188, 190; 189, identifier:ox; 190, identifier:oy; 191, block; 191, 192; 191, 200; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 199; 194, pattern_list; 194, 195; 194, 196; 194, 197; 194, 198; 195, identifier:x; 196, identifier:y; 197, identifier:tw; 198, identifier:th; 199, identifier:r; 200, for_statement; 200, 201; 200, 202; 200, 213; 201, identifier:l; 202, list_comprehension; 202, 203; 202, 204; 202, 207; 203, identifier:i; 204, for_in_clause; 204, 205; 204, 206; 205, identifier:i; 206, identifier:tile_layers; 207, if_clause; 207, 208; 208, call; 208, 209; 208, 210; 209, identifier:gt; 210, argument_list; 210, 211; 210, 212; 211, identifier:i; 212, identifier:layer; 213, block; 213, 214; 213, 241; 213, 258; 214, if_statement; 214, 215; 214, 224; 215, boolean_operator:and; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:self; 218, identifier:tall_sprites; 219, comparison_operator:==; 219, 220; 219, 221; 220, identifier:l; 221, binary_operator:+; 221, 222; 221, 223; 222, identifier:layer; 223, integer:1; 224, block; 224, 225; 225, if_statement; 225, 226; 225, 239; 226, comparison_operator:<=; 226, 227; 226, 232; 227, binary_operator:+; 227, 228; 227, 231; 228, binary_operator:-; 228, 229; 228, 230; 229, identifier:y; 230, identifier:oy; 231, identifier:th; 232, binary_operator:-; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:dirty_rect; 235, identifier:bottom; 236, attribute; 236, 237; 236, 238; 237, identifier:self; 238, identifier:tall_sprites; 239, block; 239, 240; 240, continue_statement; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 244; 243, identifier:tile; 244, call; 244, 245; 244, 246; 245, identifier:get_tile; 246, argument_list; 246, 247; 246, 252; 246, 257; 247, binary_operator:+; 247, 248; 247, 251; 248, binary_operator://; 248, 249; 248, 250; 249, identifier:x; 250, identifier:tw; 251, identifier:left; 252, binary_operator:+; 252, 253; 252, 256; 253, binary_operator://; 253, 254; 253, 255; 254, identifier:y; 255, identifier:th; 256, identifier:top; 257, identifier:l; 258, expression_statement; 258, 259; 259, boolean_operator:and; 259, 260; 259, 261; 260, identifier:tile; 261, call; 261, 262; 261, 263; 262, identifier:surface_blit; 263, argument_list; 263, 264; 263, 265; 264, identifier:tile; 265, tuple; 265, 266; 265, 269; 266, binary_operator:-; 266, 267; 266, 268; 267, identifier:x; 268, identifier:ox; 269, binary_operator:-; 269, 270; 269, 271; 270, identifier:y; 271, identifier:oy
def _draw_surfaces(self, surface, offset, surfaces): """ Draw surfaces onto buffer, then redraw tiles that cover them :param surface: destination :param offset: offset to compensate for buffer alignment :param surfaces: sequence of surfaces to blit """ surface_blit = surface.blit ox, oy = offset left, top = self._tile_view.topleft hit = self._layer_quadtree.hit get_tile = self.data.get_tile_image tile_layers = tuple(self.data.visible_tile_layers) dirty = list() dirty_append = dirty.append # TODO: check to avoid sorting overhead # sort layers, then the y value def sprite_sort(i): return i[2], i[1][1] + i[0].get_height() surfaces.sort(key=sprite_sort) layer_getter = itemgetter(2) for layer, group in groupby(surfaces, layer_getter): del dirty[:] for i in group: try: flags = i[3] except IndexError: dirty_append(surface_blit(i[0], i[1])) else: dirty_append(surface_blit(i[0], i[1], None, flags)) # TODO: make set of covered tiles, in the case where a cluster # of sprite surfaces causes excessive over tile overdrawing for dirty_rect in dirty: for r in hit(dirty_rect.move(ox, oy)): x, y, tw, th = r for l in [i for i in tile_layers if gt(i, layer)]: if self.tall_sprites and l == layer + 1: if y - oy + th <= dirty_rect.bottom - self.tall_sprites: continue tile = get_tile(x // tw + left, y // th + top, l) tile and surface_blit(tile, (x - ox, y - oy))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:get_tile_images_by_rect; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:rect; 6, block; 6, 7; 6, 9; 6, 38; 6, 49; 6, 57; 6, 65; 6, 71; 6, 77; 6, 83; 6, 92; 7, expression_statement; 7, 8; 8, comment; 9, function_definition; 9, 10; 9, 11; 9, 15; 10, function_name:rev; 11, parameters; 11, 12; 11, 13; 11, 14; 12, identifier:seq; 13, identifier:start; 14, identifier:stop; 15, block; 15, 16; 15, 25; 16, if_statement; 16, 17; 16, 20; 17, comparison_operator:<; 17, 18; 17, 19; 18, identifier:start; 19, integer:0; 20, block; 20, 21; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:start; 24, integer:0; 25, return_statement; 25, 26; 26, call; 26, 27; 26, 28; 27, identifier:enumerate; 28, argument_list; 28, 29; 28, 37; 29, subscript; 29, 30; 29, 31; 30, identifier:seq; 31, slice; 31, 32; 31, 33; 31, 34; 32, identifier:start; 33, colon; 34, binary_operator:+; 34, 35; 34, 36; 35, identifier:stop; 36, integer:1; 37, identifier:start; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 45; 40, pattern_list; 40, 41; 40, 42; 40, 43; 40, 44; 41, identifier:x1; 42, identifier:y1; 43, identifier:x2; 44, identifier:y2; 45, call; 45, 46; 45, 47; 46, identifier:rect_to_bb; 47, argument_list; 47, 48; 48, identifier:rect; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:images; 52, attribute; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:self; 55, identifier:tmx; 56, identifier:images; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:layers; 60, attribute; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:self; 63, identifier:tmx; 64, identifier:layers; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:at; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:_animated_tile; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:tracked_gids; 74, attribute; 74, 75; 74, 76; 75, identifier:self; 76, identifier:_tracked_gids; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:anim_map; 80, attribute; 80, 81; 80, 82; 81, identifier:self; 82, identifier:_animation_map; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:track; 86, call; 86, 87; 86, 88; 87, identifier:bool; 88, argument_list; 88, 89; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:_animation_queue; 92, for_statement; 92, 93; 92, 94; 92, 99; 93, identifier:l; 94, attribute; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:tmx; 98, identifier:visible_tile_layers; 99, block; 99, 100; 100, for_statement; 100, 101; 100, 104; 100, 114; 101, pattern_list; 101, 102; 101, 103; 102, identifier:y; 103, identifier:row; 104, call; 104, 105; 104, 106; 105, identifier:rev; 106, argument_list; 106, 107; 106, 112; 106, 113; 107, attribute; 107, 108; 107, 111; 108, subscript; 108, 109; 108, 110; 109, identifier:layers; 110, identifier:l; 111, identifier:data; 112, identifier:y1; 113, identifier:y2; 114, block; 114, 115; 115, for_statement; 115, 116; 115, 119; 115, 133; 115, 134; 115, 135; 116, pattern_list; 116, 117; 116, 118; 117, identifier:x; 118, identifier:gid; 119, list_comprehension; 119, 120; 119, 121; 119, 129; 120, identifier:i; 121, for_in_clause; 121, 122; 121, 123; 122, identifier:i; 123, call; 123, 124; 123, 125; 124, identifier:rev; 125, argument_list; 125, 126; 125, 127; 125, 128; 126, identifier:row; 127, identifier:x1; 128, identifier:x2; 129, if_clause; 129, 130; 130, subscript; 130, 131; 130, 132; 131, identifier:i; 132, integer:1; 133, comment; 134, comment; 135, block; 135, 136; 135, 157; 136, if_statement; 136, 137; 136, 142; 137, boolean_operator:and; 137, 138; 137, 139; 138, identifier:track; 139, comparison_operator:in; 139, 140; 139, 141; 140, identifier:gid; 141, identifier:tracked_gids; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, call; 144, 145; 144, 152; 145, attribute; 145, 146; 145, 151; 146, attribute; 146, 147; 146, 150; 147, subscript; 147, 148; 147, 149; 148, identifier:anim_map; 149, identifier:gid; 150, identifier:positions; 151, identifier:add; 152, argument_list; 152, 153; 153, tuple; 153, 154; 153, 155; 153, 156; 154, identifier:x; 155, identifier:y; 156, identifier:l; 157, try_statement; 157, 158; 157, 159; 157, 172; 158, comment; 159, block; 159, 160; 160, expression_statement; 160, 161; 161, yield; 161, 162; 162, expression_list; 162, 163; 162, 164; 162, 165; 162, 166; 163, identifier:x; 164, identifier:y; 165, identifier:l; 166, subscript; 166, 167; 166, 168; 167, identifier:at; 168, tuple; 168, 169; 168, 170; 168, 171; 169, identifier:x; 170, identifier:y; 171, identifier:l; 172, except_clause; 172, 173; 172, 174; 172, 175; 173, identifier:KeyError; 174, comment; 175, block; 175, 176; 176, expression_statement; 176, 177; 177, yield; 177, 178; 178, expression_list; 178, 179; 178, 180; 178, 181; 178, 182; 179, identifier:x; 180, identifier:y; 181, identifier:l; 182, subscript; 182, 183; 182, 184; 183, identifier:images; 184, identifier:gid
def get_tile_images_by_rect(self, rect): """ Speed up data access More efficient because data is accessed and cached locally """ def rev(seq, start, stop): if start < 0: start = 0 return enumerate(seq[start:stop + 1], start) x1, y1, x2, y2 = rect_to_bb(rect) images = self.tmx.images layers = self.tmx.layers at = self._animated_tile tracked_gids = self._tracked_gids anim_map = self._animation_map track = bool(self._animation_queue) for l in self.tmx.visible_tile_layers: for y, row in rev(layers[l].data, y1, y2): for x, gid in [i for i in rev(row, x1, x2) if i[1]]: # since the tile has been queried, assume it wants to be checked # for animations sometime in the future if track and gid in tracked_gids: anim_map[gid].positions.add((x, y, l)) try: # animated, so return the correct frame yield x, y, l, at[(x, y, l)] except KeyError: # not animated, so return surface from data, if any yield x, y, l, images[gid]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:describe_pdb; 3, parameters; 3, 4; 4, identifier:pdb_id; 5, block; 5, 6; 5, 8; 5, 18; 5, 25; 5, 36; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:out; 11, call; 11, 12; 11, 13; 12, identifier:get_info; 13, argument_list; 13, 14; 13, 15; 14, identifier:pdb_id; 15, keyword_argument; 15, 16; 15, 17; 16, identifier:url_root; 17, string:'http://www.rcsb.org/pdb/rest/describePDB?structureId='; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:out; 21, call; 21, 22; 21, 23; 22, identifier:to_dict; 23, argument_list; 23, 24; 24, identifier:out; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:out; 28, call; 28, 29; 28, 30; 29, identifier:remove_at_sign; 30, argument_list; 30, 31; 31, subscript; 31, 32; 31, 35; 32, subscript; 32, 33; 32, 34; 33, identifier:out; 34, string:'PDBdescription'; 35, string:'PDB'; 36, return_statement; 36, 37; 37, identifier:out
def describe_pdb(pdb_id): """Get description and metadata of a PDB entry Parameters ---------- pdb_id : string A 4 character string giving a pdb entry of interest Returns ------- out : string A text pdb description from PDB Examples -------- >>> describe_pdb('4lza') {'citation_authors': 'Malashkevich, V.N., Bhosle, R., Toro, R., Hillerich, B., Gizzi, A., Garforth, S., Kar, A., Chan, M.K., Lafluer, J., Patel, H., Matikainen, B., Chamala, S., Lim, S., Celikgil, A., Villegas, G., Evans, B., Love, J., Fiser, A., Khafizov, K., Seidel, R., Bonanno, J.B., Almo, S.C.', 'deposition_date': '2013-07-31', 'expMethod': 'X-RAY DIFFRACTION', 'keywords': 'TRANSFERASE', 'last_modification_date': '2013-08-14', 'nr_atoms': '0', 'nr_entities': '1', 'nr_residues': '390', 'release_date': '2013-08-14', 'resolution': '1.84', 'status': 'CURRENT', 'structureId': '4LZA', 'structure_authors': 'Malashkevich, V.N., Bhosle, R., Toro, R., Hillerich, B., Gizzi, A., Garforth, S., Kar, A., Chan, M.K., Lafluer, J., Patel, H., Matikainen, B., Chamala, S., Lim, S., Celikgil, A., Villegas, G., Evans, B., Love, J., Fiser, A., Khafizov, K., Seidel, R., Bonanno, J.B., Almo, S.C., New York Structural Genomics Research Consortium (NYSGRC)', 'title': 'Crystal structure of adenine phosphoribosyltransferase from Thermoanaerobacter pseudethanolicus ATCC 33223, NYSGRC Target 029700.'} """ out = get_info(pdb_id, url_root = 'http://www.rcsb.org/pdb/rest/describePDB?structureId=') out = to_dict(out) out = remove_at_sign(out['PDBdescription']['PDB']) return out
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:download_file_from_google_drive; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:file_id; 5, identifier:dest_path; 6, default_parameter; 6, 7; 6, 8; 7, identifier:overwrite; 8, False; 9, default_parameter; 9, 10; 9, 11; 10, identifier:unzip; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:showsize; 14, False; 15, block; 15, 16; 15, 18; 15, 25; 15, 37; 16, expression_statement; 16, 17; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:destination_directory; 21, call; 21, 22; 21, 23; 22, identifier:dirname; 23, argument_list; 23, 24; 24, identifier:dest_path; 25, if_statement; 25, 26; 25, 31; 26, not_operator; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:exists; 29, argument_list; 29, 30; 30, identifier:destination_directory; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, call; 33, 34; 33, 35; 34, identifier:makedirs; 35, argument_list; 35, 36; 36, identifier:destination_directory; 37, if_statement; 37, 38; 37, 45; 38, boolean_operator:or; 38, 39; 38, 44; 39, not_operator; 39, 40; 40, call; 40, 41; 40, 42; 41, identifier:exists; 42, argument_list; 42, 43; 43, identifier:dest_path; 44, identifier:overwrite; 45, block; 45, 46; 45, 54; 45, 68; 45, 74; 45, 94; 45, 103; 45, 133; 45, 141; 45, 146; 45, 156; 45, 161; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:session; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:requests; 52, identifier:Session; 53, argument_list; 54, expression_statement; 54, 55; 55, call; 55, 56; 55, 57; 56, identifier:print; 57, argument_list; 57, 58; 57, 65; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, string:'Downloading {} into {}... '; 61, identifier:format; 62, argument_list; 62, 63; 62, 64; 63, identifier:file_id; 64, identifier:dest_path; 65, keyword_argument; 65, 66; 65, 67; 66, identifier:end; 67, string:''; 68, expression_statement; 68, 69; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:stdout; 72, identifier:flush; 73, argument_list; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:response; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:session; 80, identifier:get; 81, argument_list; 81, 82; 81, 85; 81, 91; 82, attribute; 82, 83; 82, 84; 83, identifier:GoogleDriveDownloader; 84, identifier:DOWNLOAD_URL; 85, keyword_argument; 85, 86; 85, 87; 86, identifier:params; 87, dictionary; 87, 88; 88, pair; 88, 89; 88, 90; 89, string:'id'; 90, identifier:file_id; 91, keyword_argument; 91, 92; 91, 93; 92, identifier:stream; 93, True; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:token; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:GoogleDriveDownloader; 100, identifier:_get_confirm_token; 101, argument_list; 101, 102; 102, identifier:response; 103, if_statement; 103, 104; 103, 105; 104, identifier:token; 105, block; 105, 106; 105, 116; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:params; 109, dictionary; 109, 110; 109, 113; 110, pair; 110, 111; 110, 112; 111, string:'id'; 112, identifier:file_id; 113, pair; 113, 114; 113, 115; 114, string:'confirm'; 115, identifier:token; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:response; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:session; 122, identifier:get; 123, argument_list; 123, 124; 123, 127; 123, 130; 124, attribute; 124, 125; 124, 126; 125, identifier:GoogleDriveDownloader; 126, identifier:DOWNLOAD_URL; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:params; 129, identifier:params; 130, keyword_argument; 130, 131; 130, 132; 131, identifier:stream; 132, True; 133, if_statement; 133, 134; 133, 135; 134, identifier:showsize; 135, block; 135, 136; 135, 140; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 139; 138, identifier:print; 139, argument_list; 140, comment; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:current_download_size; 144, list:[0]; 144, 145; 145, integer:0; 146, expression_statement; 146, 147; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:GoogleDriveDownloader; 150, identifier:_save_response_content; 151, argument_list; 151, 152; 151, 153; 151, 154; 151, 155; 152, identifier:response; 153, identifier:dest_path; 154, identifier:showsize; 155, identifier:current_download_size; 156, expression_statement; 156, 157; 157, call; 157, 158; 157, 159; 158, identifier:print; 159, argument_list; 159, 160; 160, string:'Done.'; 161, if_statement; 161, 162; 161, 163; 162, identifier:unzip; 163, block; 163, 164; 164, try_statement; 164, 165; 164, 206; 165, block; 165, 166; 165, 174; 165, 180; 165, 201; 166, expression_statement; 166, 167; 167, call; 167, 168; 167, 169; 168, identifier:print; 169, argument_list; 169, 170; 169, 171; 170, string:'Unzipping...'; 171, keyword_argument; 171, 172; 171, 173; 172, identifier:end; 173, string:''; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:stdout; 178, identifier:flush; 179, argument_list; 180, with_statement; 180, 181; 180, 193; 181, with_clause; 181, 182; 182, with_item; 182, 183; 183, as_pattern; 183, 184; 183, 191; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:zipfile; 187, identifier:ZipFile; 188, argument_list; 188, 189; 188, 190; 189, identifier:dest_path; 190, string:'r'; 191, as_pattern_target; 191, 192; 192, identifier:z; 193, block; 193, 194; 194, expression_statement; 194, 195; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:z; 198, identifier:extractall; 199, argument_list; 199, 200; 200, identifier:destination_directory; 201, expression_statement; 201, 202; 202, call; 202, 203; 202, 204; 203, identifier:print; 204, argument_list; 204, 205; 205, string:'Done.'; 206, except_clause; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:zipfile; 209, identifier:BadZipfile; 210, block; 210, 211; 211, expression_statement; 211, 212; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:warnings; 215, identifier:warn; 216, argument_list; 216, 217; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, string:'Ignoring `unzip` since "{}" does not look like a valid zip file'; 220, identifier:format; 221, argument_list; 221, 222; 222, identifier:file_id
def download_file_from_google_drive(file_id, dest_path, overwrite=False, unzip=False, showsize=False): """ Downloads a shared file from google drive into a given folder. Optionally unzips it. Parameters ---------- file_id: str the file identifier. You can obtain it from the sharable link. dest_path: str the destination where to save the downloaded file. Must be a path (for example: './downloaded_file.txt') overwrite: bool optional, if True forces re-download and overwrite. unzip: bool optional, if True unzips a file. If the file is not a zip file, ignores it. showsize: bool optional, if True print the current download size. Returns ------- None """ destination_directory = dirname(dest_path) if not exists(destination_directory): makedirs(destination_directory) if not exists(dest_path) or overwrite: session = requests.Session() print('Downloading {} into {}... '.format(file_id, dest_path), end='') stdout.flush() response = session.get(GoogleDriveDownloader.DOWNLOAD_URL, params={'id': file_id}, stream=True) token = GoogleDriveDownloader._get_confirm_token(response) if token: params = {'id': file_id, 'confirm': token} response = session.get(GoogleDriveDownloader.DOWNLOAD_URL, params=params, stream=True) if showsize: print() # Skip to the next line current_download_size = [0] GoogleDriveDownloader._save_response_content(response, dest_path, showsize, current_download_size) print('Done.') if unzip: try: print('Unzipping...', end='') stdout.flush() with zipfile.ZipFile(dest_path, 'r') as z: z.extractall(destination_directory) print('Done.') except zipfile.BadZipfile: warnings.warn('Ignoring `unzip` since "{}" does not look like a valid zip file'.format(file_id))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 1, 7; 2, function_name:server_extensions_handshake; 3, parameters; 3, 4; 3, 5; 4, identifier:requested; 5, identifier:supported; 6, comment; 7, block; 7, 8; 7, 10; 7, 14; 7, 89; 7, 182; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:accepts; 13, dictionary; 14, for_statement; 14, 15; 14, 16; 14, 17; 15, identifier:offer; 16, identifier:requested; 17, block; 17, 18; 17, 34; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:name; 21, call; 21, 22; 21, 33; 22, attribute; 22, 23; 22, 32; 23, subscript; 23, 24; 23, 31; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:offer; 27, identifier:split; 28, argument_list; 28, 29; 28, 30; 29, string:";"; 30, integer:1; 31, integer:0; 32, identifier:strip; 33, argument_list; 34, for_statement; 34, 35; 34, 36; 34, 37; 35, identifier:extension; 36, identifier:supported; 37, block; 37, 38; 38, if_statement; 38, 39; 38, 44; 39, comparison_operator:==; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:extension; 42, identifier:name; 43, identifier:name; 44, block; 44, 45; 44, 54; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:accept; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:extension; 51, identifier:accept; 52, argument_list; 52, 53; 53, identifier:offer; 54, if_statement; 54, 55; 54, 58; 54, 67; 55, comparison_operator:is; 55, 56; 55, 57; 56, identifier:accept; 57, True; 58, block; 58, 59; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 66; 61, subscript; 61, 62; 61, 63; 62, identifier:accepts; 63, attribute; 63, 64; 63, 65; 64, identifier:extension; 65, identifier:name; 66, True; 67, elif_clause; 67, 68; 67, 75; 68, boolean_operator:and; 68, 69; 68, 72; 69, comparison_operator:is; 69, 70; 69, 71; 70, identifier:accept; 71, False; 72, comparison_operator:is; 72, 73; 72, 74; 73, identifier:accept; 74, None; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 83; 78, subscript; 78, 79; 78, 80; 79, identifier:accepts; 80, attribute; 80, 81; 80, 82; 81, identifier:extension; 82, identifier:name; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:accept; 86, identifier:encode; 87, argument_list; 87, 88; 88, string:"ascii"; 89, if_statement; 89, 90; 89, 91; 90, identifier:accepts; 91, block; 91, 92; 91, 96; 91, 175; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:extensions; 95, list:[]; 96, for_statement; 96, 97; 96, 100; 96, 105; 97, pattern_list; 97, 98; 97, 99; 98, identifier:name; 99, identifier:params; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:accepts; 103, identifier:items; 104, argument_list; 105, block; 105, 106; 106, if_statement; 106, 107; 106, 110; 106, 123; 107, comparison_operator:is; 107, 108; 107, 109; 108, identifier:params; 109, True; 110, block; 110, 111; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:extensions; 115, identifier:append; 116, argument_list; 116, 117; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:name; 120, identifier:encode; 121, argument_list; 121, 122; 122, string:"ascii"; 123, else_clause; 123, 124; 123, 125; 124, comment; 125, block; 125, 126; 125, 135; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:params; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:params; 132, identifier:decode; 133, argument_list; 133, 134; 134, string:"ascii"; 135, if_statement; 135, 136; 135, 139; 135, 156; 136, comparison_operator:==; 136, 137; 136, 138; 137, identifier:params; 138, string:""; 139, block; 139, 140; 140, expression_statement; 140, 141; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:extensions; 144, identifier:append; 145, argument_list; 145, 146; 146, call; 146, 147; 146, 154; 147, attribute; 147, 148; 147, 153; 148, parenthesized_expression; 148, 149; 149, binary_operator:%; 149, 150; 149, 151; 150, string:"%s"; 151, parenthesized_expression; 151, 152; 152, identifier:name; 153, identifier:encode; 154, argument_list; 154, 155; 155, string:"ascii"; 156, else_clause; 156, 157; 157, block; 157, 158; 158, expression_statement; 158, 159; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:extensions; 162, identifier:append; 163, argument_list; 163, 164; 164, call; 164, 165; 164, 173; 165, attribute; 165, 166; 165, 172; 166, parenthesized_expression; 166, 167; 167, binary_operator:%; 167, 168; 167, 169; 168, string:"%s; %s"; 169, tuple; 169, 170; 169, 171; 170, identifier:name; 171, identifier:params; 172, identifier:encode; 173, argument_list; 173, 174; 174, string:"ascii"; 175, return_statement; 175, 176; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, string:b", "; 179, identifier:join; 180, argument_list; 180, 181; 181, identifier:extensions; 182, return_statement; 182, 183; 183, None
def server_extensions_handshake(requested, supported): # type: (List[str], List[Extension]) -> Optional[bytes] """Agree on the extensions to use returning an appropriate header value. This returns None if there are no agreed extensions """ accepts = {} for offer in requested: name = offer.split(";", 1)[0].strip() for extension in supported: if extension.name == name: accept = extension.accept(offer) if accept is True: accepts[extension.name] = True elif accept is not False and accept is not None: accepts[extension.name] = accept.encode("ascii") if accepts: extensions = [] for name, params in accepts.items(): if params is True: extensions.append(name.encode("ascii")) else: # py34 annoyance: doesn't support bytestring formatting params = params.decode("ascii") if params == "": extensions.append(("%s" % (name)).encode("ascii")) else: extensions.append(("%s; %s" % (name, params)).encode("ascii")) return b", ".join(extensions) return None
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:listfiles; 3, parameters; 3, 4; 4, identifier:data_name; 5, block; 5, 6; 5, 8; 5, 18; 5, 40; 5, 41; 5, 46; 5, 50; 5, 192; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:data_source; 11, call; 11, 12; 11, 13; 12, identifier:get_data_object; 13, argument_list; 13, 14; 13, 15; 14, identifier:data_name; 15, keyword_argument; 15, 16; 15, 17; 16, identifier:use_data_config; 17, False; 18, if_statement; 18, 19; 18, 21; 19, not_operator; 19, 20; 20, identifier:data_source; 21, block; 21, 22; 21, 34; 22, if_statement; 22, 23; 22, 26; 23, comparison_operator:in; 23, 24; 23, 25; 24, string:'output'; 25, identifier:data_name; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:floyd_logger; 31, identifier:info; 32, argument_list; 32, 33; 33, string:"Note: You cannot clone the output of a running job. You need to wait for it to finish."; 34, expression_statement; 34, 35; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:sys; 38, identifier:exit; 39, argument_list; 40, comment; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:dirs; 44, list:['']; 44, 45; 45, string:''; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:paths; 49, list:[]; 50, while_statement; 50, 51; 50, 52; 51, identifier:dirs; 52, block; 52, 53; 52, 61; 52, 73; 52, 89; 52, 110; 52, 116; 52, 130; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:cur_dir; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:dirs; 59, identifier:pop; 60, argument_list; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:url; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, string:"/resources/{}/{}?content=true"; 67, identifier:format; 68, argument_list; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:data_source; 71, identifier:resource_id; 72, identifier:cur_dir; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:response; 76, call; 76, 77; 76, 88; 77, attribute; 77, 78; 77, 87; 78, call; 78, 79; 78, 84; 79, attribute; 79, 80; 79, 83; 80, call; 80, 81; 80, 82; 81, identifier:DataClient; 82, argument_list; 83, identifier:request; 84, argument_list; 84, 85; 84, 86; 85, string:"GET"; 86, identifier:url; 87, identifier:json; 88, argument_list; 89, if_statement; 89, 90; 89, 95; 90, comparison_operator:>; 90, 91; 90, 94; 91, subscript; 91, 92; 91, 93; 92, identifier:response; 93, string:'skipped_files'; 94, integer:0; 95, block; 95, 96; 96, expression_statement; 96, 97; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:floyd_logger; 100, identifier:info; 101, argument_list; 101, 102; 101, 103; 101, 104; 101, 107; 102, string:"Warning: in directory '%s', %s/%s files skipped (too many files)"; 103, identifier:cur_dir; 104, subscript; 104, 105; 104, 106; 105, identifier:response; 106, string:'skipped_files'; 107, subscript; 107, 108; 107, 109; 108, identifier:response; 109, string:'total_files'; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:files; 113, subscript; 113, 114; 113, 115; 114, identifier:response; 115, string:'files'; 116, expression_statement; 116, 117; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:files; 120, identifier:sort; 121, argument_list; 121, 122; 122, keyword_argument; 122, 123; 122, 124; 123, identifier:key; 124, lambda; 124, 125; 124, 127; 125, lambda_parameters; 125, 126; 126, identifier:f; 127, subscript; 127, 128; 127, 129; 128, identifier:f; 129, string:'name'; 130, for_statement; 130, 131; 130, 132; 130, 133; 131, identifier:f; 132, identifier:files; 133, block; 133, 134; 133, 148; 133, 161; 133, 168; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:path; 137, call; 137, 138; 137, 143; 138, attribute; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:os; 141, identifier:path; 142, identifier:join; 143, argument_list; 143, 144; 143, 145; 144, identifier:cur_dir; 145, subscript; 145, 146; 145, 147; 146, identifier:f; 147, string:'name'; 148, if_statement; 148, 149; 148, 154; 149, comparison_operator:==; 149, 150; 149, 153; 150, subscript; 150, 151; 150, 152; 151, identifier:f; 152, string:'type'; 153, string:'directory'; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, augmented_assignment:+=; 156, 157; 156, 158; 157, identifier:path; 158, attribute; 158, 159; 158, 160; 159, identifier:os; 160, identifier:sep; 161, expression_statement; 161, 162; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:paths; 165, identifier:append; 166, argument_list; 166, 167; 167, identifier:path; 168, if_statement; 168, 169; 168, 174; 169, comparison_operator:==; 169, 170; 169, 173; 170, subscript; 170, 171; 170, 172; 171, identifier:f; 172, string:'type'; 173, string:'directory'; 174, block; 174, 175; 175, expression_statement; 175, 176; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:dirs; 179, identifier:append; 180, argument_list; 180, 181; 181, call; 181, 182; 181, 187; 182, attribute; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:os; 185, identifier:path; 186, identifier:join; 187, argument_list; 187, 188; 187, 189; 188, identifier:cur_dir; 189, subscript; 189, 190; 189, 191; 190, identifier:f; 191, string:'name'; 192, for_statement; 192, 193; 192, 194; 192, 195; 193, identifier:path; 194, identifier:paths; 195, block; 195, 196; 196, expression_statement; 196, 197; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:floyd_logger; 200, identifier:info; 201, argument_list; 201, 202; 202, identifier:path
def listfiles(data_name): """ List files in a dataset. """ data_source = get_data_object(data_name, use_data_config=False) if not data_source: if 'output' in data_name: floyd_logger.info("Note: You cannot clone the output of a running job. You need to wait for it to finish.") sys.exit() # Depth-first search dirs = [''] paths = [] while dirs: cur_dir = dirs.pop() url = "/resources/{}/{}?content=true".format(data_source.resource_id, cur_dir) response = DataClient().request("GET", url).json() if response['skipped_files'] > 0: floyd_logger.info("Warning: in directory '%s', %s/%s files skipped (too many files)", cur_dir, response['skipped_files'], response['total_files']) files = response['files'] files.sort(key=lambda f: f['name']) for f in files: path = os.path.join(cur_dir, f['name']) if f['type'] == 'directory': path += os.sep paths.append(path) if f['type'] == 'directory': dirs.append(os.path.join(cur_dir, f['name'])) for path in paths: floyd_logger.info(path)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:download; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 4, identifier:self; 5, identifier:url; 6, identifier:filename; 7, default_parameter; 7, 8; 7, 9; 8, identifier:relative; 9, False; 10, default_parameter; 10, 11; 10, 12; 11, identifier:headers; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:timeout; 15, integer:5; 16, block; 16, 17; 16, 19; 16, 30; 16, 42; 16, 43; 16, 47; 16, 60; 16, 61; 16, 71; 17, expression_statement; 17, 18; 18, comment; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:request_url; 22, conditional_expression:if; 22, 23; 22, 28; 22, 29; 23, binary_operator:+; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:base_url; 27, identifier:url; 28, identifier:relative; 29, identifier:url; 30, expression_statement; 30, 31; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:floyd_logger; 34, identifier:debug; 35, argument_list; 35, 36; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, string:"Downloading file from url: {}"; 39, identifier:format; 40, argument_list; 40, 41; 41, identifier:request_url; 42, comment; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:request_headers; 46, dictionary; 47, if_statement; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:auth_header; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 57; 54, subscript; 54, 55; 54, 56; 55, identifier:request_headers; 56, string:"Authorization"; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:auth_header; 60, comment; 61, if_statement; 61, 62; 61, 63; 62, identifier:headers; 63, block; 63, 64; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:request_headers; 68, identifier:update; 69, argument_list; 69, 70; 70, identifier:headers; 71, try_statement; 71, 72; 71, 203; 72, block; 72, 73; 72, 91; 72, 98; 72, 201; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:response; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:requests; 79, identifier:get; 80, argument_list; 80, 81; 80, 82; 80, 85; 80, 88; 81, identifier:request_url; 82, keyword_argument; 82, 83; 82, 84; 83, identifier:headers; 84, identifier:request_headers; 85, keyword_argument; 85, 86; 85, 87; 86, identifier:timeout; 87, identifier:timeout; 88, keyword_argument; 88, 89; 88, 90; 89, identifier:stream; 90, True; 91, expression_statement; 91, 92; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:self; 95, identifier:check_response_status; 96, argument_list; 96, 97; 97, identifier:response; 98, with_statement; 98, 99; 98, 109; 98, 110; 98, 111; 99, with_clause; 99, 100; 100, with_item; 100, 101; 101, as_pattern; 101, 102; 101, 107; 102, call; 102, 103; 102, 104; 103, identifier:open; 104, argument_list; 104, 105; 104, 106; 105, identifier:filename; 106, string:'wb'; 107, as_pattern_target; 107, 108; 108, identifier:f; 109, comment; 110, comment; 111, block; 111, 112; 111, 123; 111, 138; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:content_length; 115, call; 115, 116; 115, 121; 116, attribute; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:response; 119, identifier:headers; 120, identifier:get; 121, argument_list; 121, 122; 122, string:'x-floydhub-content-length'; 123, if_statement; 123, 124; 123, 126; 124, not_operator; 124, 125; 125, identifier:content_length; 126, block; 126, 127; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:content_length; 130, call; 130, 131; 130, 136; 131, attribute; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:response; 134, identifier:headers; 135, identifier:get; 136, argument_list; 136, 137; 137, string:'content-length'; 138, if_statement; 138, 139; 138, 140; 138, 178; 139, identifier:content_length; 140, block; 140, 141; 141, for_statement; 141, 142; 141, 143; 141, 167; 142, identifier:chunk; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:progress; 146, identifier:bar; 147, argument_list; 147, 148; 147, 156; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:response; 151, identifier:iter_content; 152, argument_list; 152, 153; 153, keyword_argument; 153, 154; 153, 155; 154, identifier:chunk_size; 155, integer:1024; 156, keyword_argument; 156, 157; 156, 158; 157, identifier:expected_size; 158, binary_operator:+; 158, 159; 158, 166; 159, parenthesized_expression; 159, 160; 160, binary_operator:/; 160, 161; 160, 165; 161, call; 161, 162; 161, 163; 162, identifier:int; 163, argument_list; 163, 164; 164, identifier:content_length; 165, integer:1024; 166, integer:1; 167, block; 167, 168; 168, if_statement; 168, 169; 168, 170; 169, identifier:chunk; 170, block; 170, 171; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:f; 175, identifier:write; 176, argument_list; 176, 177; 177, identifier:chunk; 178, else_clause; 178, 179; 179, block; 179, 180; 180, for_statement; 180, 181; 180, 182; 180, 190; 181, identifier:chunk; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:response; 185, identifier:iter_content; 186, argument_list; 186, 187; 187, keyword_argument; 187, 188; 187, 189; 188, identifier:chunk_size; 189, integer:1024; 190, block; 190, 191; 191, if_statement; 191, 192; 191, 193; 192, identifier:chunk; 193, block; 193, 194; 194, expression_statement; 194, 195; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:f; 198, identifier:write; 199, argument_list; 199, 200; 200, identifier:chunk; 201, return_statement; 201, 202; 202, identifier:filename; 203, except_clause; 203, 204; 203, 212; 204, as_pattern; 204, 205; 204, 210; 205, attribute; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:requests; 208, identifier:exceptions; 209, identifier:ConnectionError; 210, as_pattern_target; 210, 211; 211, identifier:exception; 212, block; 212, 213; 212, 225; 213, expression_statement; 213, 214; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:floyd_logger; 217, identifier:debug; 218, argument_list; 218, 219; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, string:"Exception: {}"; 222, identifier:format; 223, argument_list; 223, 224; 224, identifier:exception; 225, expression_statement; 225, 226; 226, call; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:sys; 229, identifier:exit; 230, argument_list; 230, 231; 231, string:"Cannot connect to the Floyd server. Check your internet connection."
def download(self, url, filename, relative=False, headers=None, timeout=5): """ Download the file from the given url at the current path """ request_url = self.base_url + url if relative else url floyd_logger.debug("Downloading file from url: {}".format(request_url)) # Auth headers if present request_headers = {} if self.auth_header: request_headers["Authorization"] = self.auth_header # Add any additional headers if headers: request_headers.update(headers) try: response = requests.get(request_url, headers=request_headers, timeout=timeout, stream=True) self.check_response_status(response) with open(filename, 'wb') as f: # chunk mode response doesn't have content-length so we are # using a custom header here content_length = response.headers.get('x-floydhub-content-length') if not content_length: content_length = response.headers.get('content-length') if content_length: for chunk in progress.bar(response.iter_content(chunk_size=1024), expected_size=(int(content_length) / 1024) + 1): if chunk: f.write(chunk) else: for chunk in response.iter_content(chunk_size=1024): if chunk: f.write(chunk) return filename except requests.exceptions.ConnectionError as exception: floyd_logger.debug("Exception: {}".format(exception)) sys.exit("Cannot connect to the Floyd server. Check your internet connection.")
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:get_command_line; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 4, identifier:instance_type; 5, identifier:env; 6, identifier:message; 7, identifier:data; 8, identifier:mode; 9, identifier:open_notebook; 10, identifier:command_str; 11, block; 11, 12; 11, 14; 11, 20; 11, 34; 11, 48; 11, 60; 11, 108; 11, 152; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:floyd_command; 17, list:["floyd", "run"]; 17, 18; 17, 19; 18, string:"floyd"; 19, string:"run"; 20, if_statement; 20, 21; 20, 22; 21, identifier:instance_type; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:floyd_command; 27, identifier:append; 28, argument_list; 28, 29; 29, binary_operator:+; 29, 30; 29, 31; 30, string:'--'; 31, subscript; 31, 32; 31, 33; 32, identifier:INSTANCE_NAME_MAP; 33, identifier:instance_type; 34, if_statement; 34, 35; 34, 41; 35, boolean_operator:and; 35, 36; 35, 37; 36, identifier:env; 37, not_operator; 37, 38; 38, comparison_operator:==; 38, 39; 38, 40; 39, identifier:env; 40, identifier:DEFAULT_ENV; 41, block; 41, 42; 42, expression_statement; 42, 43; 43, augmented_assignment:+=; 43, 44; 43, 45; 44, identifier:floyd_command; 45, list:["--env", env]; 45, 46; 45, 47; 46, string:"--env"; 47, identifier:env; 48, if_statement; 48, 49; 48, 50; 49, identifier:message; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, augmented_assignment:+=; 52, 53; 52, 54; 53, identifier:floyd_command; 54, list:["--message", shell_quote(message)]; 54, 55; 54, 56; 55, string:"--message"; 56, call; 56, 57; 56, 58; 57, identifier:shell_quote; 58, argument_list; 58, 59; 59, identifier:message; 60, if_statement; 60, 61; 60, 62; 61, identifier:data; 62, block; 62, 63; 63, for_statement; 63, 64; 63, 65; 63, 66; 64, identifier:data_item; 65, identifier:data; 66, block; 66, 67; 66, 76; 66, 102; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:parts; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:data_item; 73, identifier:split; 74, argument_list; 74, 75; 75, string:':'; 76, if_statement; 76, 77; 76, 83; 77, comparison_operator:>; 77, 78; 77, 82; 78, call; 78, 79; 78, 80; 79, identifier:len; 80, argument_list; 80, 81; 81, identifier:parts; 82, integer:1; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:data_item; 87, binary_operator:+; 87, 88; 87, 99; 88, binary_operator:+; 88, 89; 88, 98; 89, call; 89, 90; 89, 91; 90, identifier:normalize_data_name; 91, argument_list; 91, 92; 91, 95; 92, subscript; 92, 93; 92, 94; 93, identifier:parts; 94, integer:0; 95, keyword_argument; 95, 96; 95, 97; 96, identifier:use_data_config; 97, False; 98, string:':'; 99, subscript; 99, 100; 99, 101; 100, identifier:parts; 101, integer:1; 102, expression_statement; 102, 103; 103, augmented_assignment:+=; 103, 104; 103, 105; 104, identifier:floyd_command; 105, list:["--data", data_item]; 105, 106; 105, 107; 106, string:"--data"; 107, identifier:data_item; 108, if_statement; 108, 109; 108, 114; 108, 137; 109, boolean_operator:and; 109, 110; 109, 111; 110, identifier:mode; 111, comparison_operator:!=; 111, 112; 111, 113; 112, identifier:mode; 113, string:"job"; 114, block; 114, 115; 114, 121; 115, expression_statement; 115, 116; 116, augmented_assignment:+=; 116, 117; 116, 118; 117, identifier:floyd_command; 118, list:["--mode", mode]; 118, 119; 118, 120; 119, string:"--mode"; 120, identifier:mode; 121, if_statement; 121, 122; 121, 125; 122, comparison_operator:==; 122, 123; 122, 124; 123, identifier:mode; 124, string:'jupyter'; 125, block; 125, 126; 126, if_statement; 126, 127; 126, 129; 127, not_operator; 127, 128; 128, identifier:open_notebook; 129, block; 129, 130; 130, expression_statement; 130, 131; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:floyd_command; 134, identifier:append; 135, argument_list; 135, 136; 136, string:"--no-open"; 137, else_clause; 137, 138; 138, block; 138, 139; 139, if_statement; 139, 140; 139, 141; 140, identifier:command_str; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:floyd_command; 146, identifier:append; 147, argument_list; 147, 148; 148, call; 148, 149; 148, 150; 149, identifier:shell_quote; 150, argument_list; 150, 151; 151, identifier:command_str; 152, return_statement; 152, 153; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, string:' '; 156, identifier:join; 157, argument_list; 157, 158; 158, identifier:floyd_command
def get_command_line(instance_type, env, message, data, mode, open_notebook, command_str): """ Return a string representing the full floyd command entered in the command line """ floyd_command = ["floyd", "run"] if instance_type: floyd_command.append('--' + INSTANCE_NAME_MAP[instance_type]) if env and not env == DEFAULT_ENV: floyd_command += ["--env", env] if message: floyd_command += ["--message", shell_quote(message)] if data: for data_item in data: parts = data_item.split(':') if len(parts) > 1: data_item = normalize_data_name(parts[0], use_data_config=False) + ':' + parts[1] floyd_command += ["--data", data_item] if mode and mode != "job": floyd_command += ["--mode", mode] if mode == 'jupyter': if not open_notebook: floyd_command.append("--no-open") else: if command_str: floyd_command.append(shell_quote(command_str)) return ' '.join(floyd_command)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:restart; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 3, 12; 3, 13; 3, 14; 4, identifier:ctx; 5, identifier:job_name; 6, identifier:data; 7, identifier:open_notebook; 8, identifier:env; 9, identifier:message; 10, identifier:gpu; 11, identifier:cpu; 12, identifier:gpup; 13, identifier:cpup; 14, identifier:command; 15, block; 15, 16; 15, 18; 15, 19; 15, 20; 15, 21; 15, 22; 15, 49; 15, 55; 15, 59; 15, 65; 15, 91; 15, 113; 15, 132; 15, 164; 15, 174; 15, 185; 15, 194; 15, 203; 15, 217; 15, 225; 15, 239; 15, 257; 16, expression_statement; 16, 17; 17, comment; 18, comment; 19, comment; 20, comment; 21, comment; 22, if_statement; 22, 23; 22, 29; 23, comparison_operator:>; 23, 24; 23, 28; 24, call; 24, 25; 24, 26; 25, identifier:len; 26, argument_list; 26, 27; 27, identifier:env; 28, integer:1; 29, block; 29, 30; 29, 42; 30, expression_statement; 30, 31; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:floyd_logger; 34, identifier:error; 35, argument_list; 35, 36; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, string:"You passed more than one environment: {}. Please specify a single environment."; 39, identifier:format; 40, argument_list; 40, 41; 41, identifier:env; 42, expression_statement; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:sys; 46, identifier:exit; 47, argument_list; 47, 48; 48, integer:1; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:env; 52, subscript; 52, 53; 52, 54; 53, identifier:env; 54, integer:0; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:parameters; 58, dictionary; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:expt_client; 62, call; 62, 63; 62, 64; 63, identifier:ExperimentClient; 64, argument_list; 65, try_statement; 65, 66; 65, 79; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:job; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:expt_client; 73, identifier:get; 74, argument_list; 74, 75; 75, call; 75, 76; 75, 77; 76, identifier:normalize_job_name; 77, argument_list; 77, 78; 78, identifier:job_name; 79, except_clause; 79, 80; 79, 81; 80, identifier:FloydException; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:job; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:expt_client; 88, identifier:get; 89, argument_list; 89, 90; 90, identifier:job_name; 91, if_statement; 91, 92; 91, 93; 91, 98; 91, 105; 92, identifier:gpu; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:instance_type; 97, identifier:G1_INSTANCE_TYPE; 98, elif_clause; 98, 99; 98, 100; 99, identifier:cpu; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:instance_type; 104, identifier:C1_INSTANCE_TYPE; 105, else_clause; 105, 106; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:instance_type; 110, attribute; 110, 111; 110, 112; 111, identifier:job; 112, identifier:instance_type; 113, if_statement; 113, 114; 113, 117; 113, 124; 114, comparison_operator:is; 114, 115; 114, 116; 115, identifier:instance_type; 116, None; 117, block; 117, 118; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 123; 120, subscript; 120, 121; 120, 122; 121, identifier:parameters; 122, string:'instance_type'; 123, identifier:instance_type; 124, else_clause; 124, 125; 125, block; 125, 126; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:instance_type; 129, attribute; 129, 130; 129, 131; 130, identifier:job; 131, identifier:instance_type; 132, if_statement; 132, 133; 132, 136; 133, comparison_operator:is; 133, 134; 133, 135; 134, identifier:env; 135, None; 136, block; 136, 137; 136, 143; 136, 158; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:arch; 140, subscript; 140, 141; 140, 142; 141, identifier:INSTANCE_ARCH_MAP; 142, identifier:instance_type; 143, if_statement; 143, 144; 143, 150; 144, not_operator; 144, 145; 145, call; 145, 146; 145, 147; 146, identifier:validate_env; 147, argument_list; 147, 148; 147, 149; 148, identifier:env; 149, identifier:arch; 150, block; 150, 151; 151, expression_statement; 151, 152; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:sys; 155, identifier:exit; 156, argument_list; 156, 157; 157, integer:1; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 163; 160, subscript; 160, 161; 160, 162; 161, identifier:parameters; 162, string:'env'; 163, identifier:env; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 170; 166, pattern_list; 166, 167; 166, 168; 166, 169; 167, identifier:success; 168, identifier:data_ids; 169, identifier:show_data_info; 170, call; 170, 171; 170, 172; 171, identifier:process_data_ids; 172, argument_list; 172, 173; 173, identifier:data; 174, if_statement; 174, 175; 174, 177; 175, not_operator; 175, 176; 176, identifier:success; 177, block; 177, 178; 178, expression_statement; 178, 179; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:sys; 182, identifier:exit; 183, argument_list; 183, 184; 184, integer:1; 185, if_statement; 185, 186; 185, 187; 186, identifier:data_ids; 187, block; 187, 188; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 193; 190, subscript; 190, 191; 190, 192; 191, identifier:parameters; 192, string:'data_ids'; 193, identifier:data_ids; 194, if_statement; 194, 195; 194, 196; 195, identifier:message; 196, block; 196, 197; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 202; 199, subscript; 199, 200; 199, 201; 200, identifier:parameters; 201, string:'description'; 202, identifier:message; 203, if_statement; 203, 204; 203, 205; 204, identifier:command; 205, block; 205, 206; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 211; 208, subscript; 208, 209; 208, 210; 209, identifier:parameters; 210, string:'command'; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, string:' '; 214, identifier:join; 215, argument_list; 215, 216; 216, identifier:command; 217, expression_statement; 217, 218; 218, call; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:floyd_logger; 221, identifier:info; 222, argument_list; 222, 223; 222, 224; 223, string:'Restarting job %s...'; 224, identifier:job_name; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:new_job_info; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:expt_client; 231, identifier:restart; 232, argument_list; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:job; 235, identifier:id; 236, keyword_argument; 236, 237; 236, 238; 237, identifier:parameters; 238, identifier:parameters; 239, if_statement; 239, 240; 239, 242; 240, not_operator; 240, 241; 241, identifier:new_job_info; 242, block; 242, 243; 242, 250; 243, expression_statement; 243, 244; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:floyd_logger; 247, identifier:error; 248, argument_list; 248, 249; 249, string:"Failed to restart job"; 250, expression_statement; 250, 251; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:sys; 254, identifier:exit; 255, argument_list; 255, 256; 256, integer:1; 257, expression_statement; 257, 258; 258, call; 258, 259; 258, 260; 259, identifier:show_new_job_info; 260, argument_list; 260, 261; 260, 262; 260, 265; 260, 266; 260, 269; 260, 270; 261, identifier:expt_client; 262, subscript; 262, 263; 262, 264; 263, identifier:new_job_info; 264, string:'name'; 265, identifier:new_job_info; 266, attribute; 266, 267; 266, 268; 267, identifier:job; 268, identifier:mode; 269, identifier:open_notebook; 270, identifier:show_data_info
def restart(ctx, job_name, data, open_notebook, env, message, gpu, cpu, gpup, cpup, command): """ Restart a finished job as a new job. """ # Error early if more than one --env is passed. Then get the first/only # --env out of the list so all other operations work normally (they don't # expect an iterable). For details on this approach, see the comment above # the --env click option if len(env) > 1: floyd_logger.error( "You passed more than one environment: {}. Please specify a single environment.".format(env) ) sys.exit(1) env = env[0] parameters = {} expt_client = ExperimentClient() try: job = expt_client.get(normalize_job_name(job_name)) except FloydException: job = expt_client.get(job_name) if gpu: instance_type = G1_INSTANCE_TYPE elif cpu: instance_type = C1_INSTANCE_TYPE else: instance_type = job.instance_type if instance_type is not None: parameters['instance_type'] = instance_type else: instance_type = job.instance_type if env is not None: arch = INSTANCE_ARCH_MAP[instance_type] if not validate_env(env, arch): sys.exit(1) parameters['env'] = env success, data_ids, show_data_info = process_data_ids(data) if not success: sys.exit(1) if data_ids: parameters['data_ids'] = data_ids if message: parameters['description'] = message if command: parameters['command'] = ' '.join(command) floyd_logger.info('Restarting job %s...', job_name) new_job_info = expt_client.restart(job.id, parameters=parameters) if not new_job_info: floyd_logger.error("Failed to restart job") sys.exit(1) show_new_job_info(expt_client, new_job_info['name'], new_job_info, job.mode, open_notebook, show_data_info)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:filter_user; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:user; 5, default_parameter; 5, 6; 5, 7; 6, identifier:using; 7, string:'records'; 8, default_parameter; 8, 9; 8, 10; 9, identifier:interaction; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:part_of_week; 13, string:'allweek'; 14, default_parameter; 14, 15; 14, 16; 15, identifier:part_of_day; 16, string:'allday'; 17, block; 17, 18; 17, 20; 17, 81; 17, 150; 17, 203; 17, 252; 18, expression_statement; 18, 19; 19, comment; 20, if_statement; 20, 21; 20, 24; 20, 31; 21, comparison_operator:==; 21, 22; 21, 23; 22, identifier:using; 23, string:'recharges'; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:records; 28, attribute; 28, 29; 28, 30; 29, identifier:user; 30, identifier:recharges; 31, else_clause; 31, 32; 32, block; 32, 33; 32, 39; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:records; 36, attribute; 36, 37; 36, 38; 37, identifier:user; 38, identifier:records; 39, if_statement; 39, 40; 39, 43; 39, 61; 40, comparison_operator:==; 40, 41; 40, 42; 41, identifier:interaction; 42, string:'callandtext'; 43, block; 43, 44; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:records; 47, call; 47, 48; 47, 49; 48, identifier:filter; 49, argument_list; 49, 50; 49, 60; 50, lambda; 50, 51; 50, 53; 51, lambda_parameters; 51, 52; 52, identifier:r; 53, comparison_operator:in; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:r; 56, identifier:interaction; 57, list:['call', 'text']; 57, 58; 57, 59; 58, string:'call'; 59, string:'text'; 60, identifier:records; 61, elif_clause; 61, 62; 61, 65; 62, comparison_operator:is; 62, 63; 62, 64; 63, identifier:interaction; 64, None; 65, block; 65, 66; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:records; 69, call; 69, 70; 69, 71; 70, identifier:filter; 71, argument_list; 71, 72; 71, 80; 72, lambda; 72, 73; 72, 75; 73, lambda_parameters; 73, 74; 74, identifier:r; 75, comparison_operator:==; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:r; 78, identifier:interaction; 79, identifier:interaction; 80, identifier:records; 81, if_statement; 81, 82; 81, 85; 81, 107; 81, 133; 82, comparison_operator:==; 82, 83; 82, 84; 83, identifier:part_of_week; 84, string:'weekday'; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:records; 89, call; 89, 90; 89, 91; 90, identifier:filter; 91, argument_list; 91, 92; 91, 106; 92, lambda; 92, 93; 92, 95; 93, lambda_parameters; 93, 94; 94, identifier:r; 95, comparison_operator:not; 95, 96; 95, 103; 96, call; 96, 97; 96, 102; 97, attribute; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:r; 100, identifier:datetime; 101, identifier:isoweekday; 102, argument_list; 103, attribute; 103, 104; 103, 105; 104, identifier:user; 105, identifier:weekend; 106, identifier:records; 107, elif_clause; 107, 108; 107, 111; 108, comparison_operator:==; 108, 109; 108, 110; 109, identifier:part_of_week; 110, string:'weekend'; 111, block; 111, 112; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:records; 115, call; 115, 116; 115, 117; 116, identifier:filter; 117, argument_list; 117, 118; 117, 132; 118, lambda; 118, 119; 118, 121; 119, lambda_parameters; 119, 120; 120, identifier:r; 121, comparison_operator:in; 121, 122; 121, 129; 122, call; 122, 123; 122, 128; 123, attribute; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:r; 126, identifier:datetime; 127, identifier:isoweekday; 128, argument_list; 129, attribute; 129, 130; 129, 131; 130, identifier:user; 131, identifier:weekend; 132, identifier:records; 133, elif_clause; 133, 134; 133, 137; 134, comparison_operator:!=; 134, 135; 134, 136; 135, identifier:part_of_week; 136, string:'allweek'; 137, block; 137, 138; 138, raise_statement; 138, 139; 139, call; 139, 140; 139, 141; 140, identifier:KeyError; 141, argument_list; 141, 142; 142, call; 142, 143; 142, 148; 143, attribute; 143, 144; 143, 147; 144, concatenated_string; 144, 145; 144, 146; 145, string:"{} is not a valid value for part_of_week. it should be 'weekday', "; 146, string:"'weekend' or 'allweek'."; 147, identifier:format; 148, argument_list; 148, 149; 149, identifier:part_of_week; 150, if_statement; 150, 151; 150, 158; 150, 179; 151, comparison_operator:<; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:user; 154, identifier:night_start; 155, attribute; 155, 156; 155, 157; 156, identifier:user; 157, identifier:night_end; 158, block; 158, 159; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:night_filter; 162, lambda; 162, 163; 162, 165; 163, lambda_parameters; 163, 164; 164, identifier:r; 165, comparison_operator:>; 165, 166; 165, 169; 165, 176; 166, attribute; 166, 167; 166, 168; 167, identifier:user; 168, identifier:night_end; 169, call; 169, 170; 169, 175; 170, attribute; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:r; 173, identifier:datetime; 174, identifier:time; 175, argument_list; 176, attribute; 176, 177; 176, 178; 177, identifier:user; 178, identifier:night_start; 179, else_clause; 179, 180; 180, block; 180, 181; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:night_filter; 184, lambda; 184, 185; 184, 187; 185, lambda_parameters; 185, 186; 186, identifier:r; 187, not_operator; 187, 188; 188, parenthesized_expression; 188, 189; 189, comparison_operator:<; 189, 190; 189, 193; 189, 200; 190, attribute; 190, 191; 190, 192; 191, identifier:user; 192, identifier:night_end; 193, call; 193, 194; 193, 199; 194, attribute; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:r; 197, identifier:datetime; 198, identifier:time; 199, argument_list; 200, attribute; 200, 201; 200, 202; 201, identifier:user; 202, identifier:night_start; 203, if_statement; 203, 204; 203, 207; 203, 224; 203, 237; 204, comparison_operator:==; 204, 205; 204, 206; 205, identifier:part_of_day; 206, string:'day'; 207, block; 207, 208; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 211; 210, identifier:records; 211, call; 211, 212; 211, 213; 212, identifier:filter; 213, argument_list; 213, 214; 213, 223; 214, lambda; 214, 215; 214, 217; 215, lambda_parameters; 215, 216; 216, identifier:r; 217, not_operator; 217, 218; 218, parenthesized_expression; 218, 219; 219, call; 219, 220; 219, 221; 220, identifier:night_filter; 221, argument_list; 221, 222; 222, identifier:r; 223, identifier:records; 224, elif_clause; 224, 225; 224, 228; 225, comparison_operator:==; 225, 226; 225, 227; 226, identifier:part_of_day; 227, string:'night'; 228, block; 228, 229; 229, expression_statement; 229, 230; 230, assignment; 230, 231; 230, 232; 231, identifier:records; 232, call; 232, 233; 232, 234; 233, identifier:filter; 234, argument_list; 234, 235; 234, 236; 235, identifier:night_filter; 236, identifier:records; 237, elif_clause; 237, 238; 237, 241; 238, comparison_operator:!=; 238, 239; 238, 240; 239, identifier:part_of_day; 240, string:'allday'; 241, block; 241, 242; 242, raise_statement; 242, 243; 243, call; 243, 244; 243, 245; 244, identifier:KeyError; 245, argument_list; 245, 246; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, string:"{} is not a valid value for part_of_day. It should be 'day', 'night' or 'allday'."; 249, identifier:format; 250, argument_list; 250, 251; 251, identifier:part_of_day; 252, return_statement; 252, 253; 253, call; 253, 254; 253, 255; 254, identifier:list; 255, argument_list; 255, 256; 256, identifier:records
def filter_user(user, using='records', interaction=None, part_of_week='allweek', part_of_day='allday'): """ Filter records of a User objects by interaction, part of week and day. Parameters ---------- user : User a bandicoot User object type : str, default 'records' 'records' or 'recharges' part_of_week : {'allweek', 'weekday', 'weekend'}, default 'allweek' * 'weekend': keep only the weekend records * 'weekday': keep only the weekdays records * 'allweek': use all the records part_of_day : {'allday', 'day', 'night'}, default 'allday' * 'day': keep only the records during the day * 'night': keep only the records during the night * 'allday': use all the records interaction : object The interaction to filter records: * "callandtext", for only callandtext; * a string, to filter for one type; * None, to use all records. """ if using == 'recharges': records = user.recharges else: records = user.records if interaction == 'callandtext': records = filter( lambda r: r.interaction in ['call', 'text'], records) elif interaction is not None: records = filter(lambda r: r.interaction == interaction, records) if part_of_week == 'weekday': records = filter( lambda r: r.datetime.isoweekday() not in user.weekend, records) elif part_of_week == 'weekend': records = filter( lambda r: r.datetime.isoweekday() in user.weekend, records) elif part_of_week != 'allweek': raise KeyError( "{} is not a valid value for part_of_week. it should be 'weekday', " "'weekend' or 'allweek'.".format(part_of_week)) if user.night_start < user.night_end: night_filter = lambda r: user.night_end > r.datetime.time( ) > user.night_start else: night_filter = lambda r: not( user.night_end < r.datetime.time() < user.night_start) if part_of_day == 'day': records = filter(lambda r: not(night_filter(r)), records) elif part_of_day == 'night': records = filter(night_filter, records) elif part_of_day != 'allday': raise KeyError( "{} is not a valid value for part_of_day. It should be 'day', 'night' or 'allday'.".format(part_of_day)) return list(records)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:infer_type; 3, parameters; 3, 4; 4, identifier:data; 5, block; 5, 6; 5, 8; 5, 24; 5, 33; 5, 98; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 21; 9, call; 9, 10; 9, 11; 10, identifier:isinstance; 11, argument_list; 11, 12; 11, 13; 12, identifier:data; 13, tuple; 13, 14; 13, 18; 14, call; 14, 15; 14, 16; 15, identifier:type; 16, argument_list; 16, 17; 17, None; 18, attribute; 18, 19; 18, 20; 19, identifier:numbers; 20, identifier:Number; 21, block; 21, 22; 22, return_statement; 22, 23; 23, string:'scalar'; 24, if_statement; 24, 25; 24, 30; 25, call; 25, 26; 25, 27; 26, identifier:isinstance; 27, argument_list; 27, 28; 27, 29; 28, identifier:data; 29, identifier:SummaryStats; 30, block; 30, 31; 31, return_statement; 31, 32; 32, string:'summarystats'; 33, if_statement; 33, 34; 33, 39; 33, 40; 34, call; 34, 35; 34, 36; 35, identifier:hasattr; 36, argument_list; 36, 37; 36, 38; 37, identifier:data; 38, string:"__len__"; 39, comment; 40, block; 40, 41; 40, 53; 40, 73; 40, 84; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:data; 44, list_comprehension; 44, 45; 44, 46; 44, 49; 45, identifier:x; 46, for_in_clause; 46, 47; 46, 48; 47, identifier:x; 48, identifier:data; 49, if_clause; 49, 50; 50, comparison_operator:is; 50, 51; 50, 52; 51, identifier:x; 52, None; 53, if_statement; 53, 54; 53, 70; 54, boolean_operator:or; 54, 55; 54, 61; 55, comparison_operator:==; 55, 56; 55, 60; 56, call; 56, 57; 56, 58; 57, identifier:len; 58, argument_list; 58, 59; 59, identifier:data; 60, integer:0; 61, call; 61, 62; 61, 63; 62, identifier:isinstance; 63, argument_list; 63, 64; 63, 67; 64, subscript; 64, 65; 64, 66; 65, identifier:data; 66, integer:0; 67, attribute; 67, 68; 67, 69; 68, identifier:numbers; 69, identifier:Number; 70, block; 70, 71; 71, return_statement; 71, 72; 72, string:'distribution_scalar'; 73, if_statement; 73, 74; 73, 81; 74, call; 74, 75; 74, 76; 75, identifier:isinstance; 76, argument_list; 76, 77; 76, 80; 77, subscript; 77, 78; 77, 79; 78, identifier:data; 79, integer:0; 80, identifier:SummaryStats; 81, block; 81, 82; 82, return_statement; 82, 83; 83, string:'distribution_summarystats'; 84, raise_statement; 84, 85; 85, call; 85, 86; 85, 87; 86, identifier:TypeError; 87, argument_list; 87, 88; 88, call; 88, 89; 88, 94; 89, attribute; 89, 90; 89, 93; 90, concatenated_string; 90, 91; 90, 92; 91, string:"{} is not a valid input. It should be a number, a SummaryStats "; 92, string:"object, or None"; 93, identifier:format; 94, argument_list; 94, 95; 95, subscript; 95, 96; 95, 97; 96, identifier:data; 97, integer:0; 98, raise_statement; 98, 99; 99, call; 99, 100; 99, 101; 100, identifier:TypeError; 101, argument_list; 101, 102; 102, call; 102, 103; 102, 108; 103, attribute; 103, 104; 103, 107; 104, concatenated_string; 104, 105; 104, 106; 105, string:"{} is not a valid input. It should be a number, a SummaryStats "; 106, string:"object, or a list"; 107, identifier:format; 108, argument_list; 108, 109; 109, identifier:data
def infer_type(data): """ Infer the type of objects returned by indicators. infer_type returns: - 'scalar' for a number or None, - 'summarystats' for a SummaryStats object, - 'distribution_scalar' for a list of scalars, - 'distribution_summarystats' for a list of SummaryStats objects """ if isinstance(data, (type(None), numbers.Number)): return 'scalar' if isinstance(data, SummaryStats): return 'summarystats' if hasattr(data, "__len__"): # list or numpy array data = [x for x in data if x is not None] if len(data) == 0 or isinstance(data[0], numbers.Number): return 'distribution_scalar' if isinstance(data[0], SummaryStats): return 'distribution_summarystats' raise TypeError( "{} is not a valid input. It should be a number, a SummaryStats " "object, or None".format(data[0])) raise TypeError( "{} is not a valid input. It should be a number, a SummaryStats " "object, or a list".format(data))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:churn_rate; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:user; 5, default_parameter; 5, 6; 5, 7; 6, identifier:summary; 7, string:'default'; 8, dictionary_splat_pattern; 8, 9; 9, identifier:kwargs; 10, block; 10, 11; 10, 13; 10, 31; 10, 61; 10, 69; 10, 77; 10, 93; 10, 97; 10, 101; 10, 146; 10, 156; 10, 230; 11, expression_statement; 11, 12; 12, comment; 13, if_statement; 13, 14; 13, 22; 14, comparison_operator:==; 14, 15; 14, 21; 15, call; 15, 16; 15, 17; 16, identifier:len; 17, argument_list; 17, 18; 18, attribute; 18, 19; 18, 20; 19, identifier:user; 20, identifier:records; 21, integer:0; 22, block; 22, 23; 23, return_statement; 23, 24; 24, call; 24, 25; 24, 26; 25, identifier:statistics; 26, argument_list; 26, 27; 26, 28; 27, list:[]; 28, keyword_argument; 28, 29; 28, 30; 29, identifier:summary; 30, identifier:summary; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:query; 34, dictionary; 34, 35; 34, 38; 34, 52; 34, 55; 34, 58; 35, pair; 35, 36; 35, 37; 36, string:'groupby'; 37, string:'week'; 38, pair; 38, 39; 38, 40; 39, string:'divide_by'; 40, call; 40, 41; 40, 42; 41, identifier:OrderedDict; 42, argument_list; 42, 43; 43, list:[ ('part_of_week', ['allweek']), ('part_of_day', ['allday']) ]; 43, 44; 43, 48; 44, tuple; 44, 45; 44, 46; 45, string:'part_of_week'; 46, list:['allweek']; 46, 47; 47, string:'allweek'; 48, tuple; 48, 49; 48, 50; 49, string:'part_of_day'; 50, list:['allday']; 50, 51; 51, string:'allday'; 52, pair; 52, 53; 52, 54; 53, string:'using'; 54, string:'records'; 55, pair; 55, 56; 55, 57; 56, string:'filter_empty'; 57, True; 58, pair; 58, 59; 58, 60; 59, string:'binning'; 60, True; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:rv; 64, call; 64, 65; 64, 66; 65, identifier:grouping_query; 66, argument_list; 66, 67; 66, 68; 67, identifier:user; 68, identifier:query; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:weekly_positions; 72, subscript; 72, 73; 72, 76; 73, subscript; 73, 74; 73, 75; 74, identifier:rv; 75, integer:0; 76, integer:1; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:all_positions; 80, call; 80, 81; 80, 82; 81, identifier:list; 82, argument_list; 82, 83; 83, call; 83, 84; 83, 85; 84, identifier:set; 85, generator_expression; 85, 86; 85, 87; 85, 90; 86, identifier:p; 87, for_in_clause; 87, 88; 87, 89; 88, identifier:l; 89, identifier:weekly_positions; 90, for_in_clause; 90, 91; 90, 92; 91, identifier:p; 92, identifier:l; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:frequencies; 96, dictionary; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:cos_dist; 100, list:[]; 101, for_statement; 101, 102; 101, 105; 101, 109; 102, pattern_list; 102, 103; 102, 104; 103, identifier:week; 104, identifier:week_positions; 105, call; 105, 106; 105, 107; 106, identifier:enumerate; 107, argument_list; 107, 108; 108, identifier:weekly_positions; 109, block; 109, 110; 109, 117; 109, 128; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:count; 113, call; 113, 114; 113, 115; 114, identifier:Counter; 115, argument_list; 115, 116; 116, identifier:week_positions; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:total; 120, call; 120, 121; 120, 122; 121, identifier:sum; 122, argument_list; 122, 123; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:count; 126, identifier:values; 127, argument_list; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 133; 130, subscript; 130, 131; 130, 132; 131, identifier:frequencies; 132, identifier:week; 133, list_comprehension; 133, 134; 133, 143; 134, binary_operator:/; 134, 135; 134, 142; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:count; 138, identifier:get; 139, argument_list; 139, 140; 139, 141; 140, identifier:p; 141, integer:0; 142, identifier:total; 143, for_in_clause; 143, 144; 143, 145; 144, identifier:p; 145, identifier:all_positions; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:all_indexes; 149, call; 149, 150; 149, 151; 150, identifier:range; 151, argument_list; 151, 152; 152, call; 152, 153; 152, 154; 153, identifier:len; 154, argument_list; 154, 155; 155, identifier:all_positions; 156, for_statement; 156, 157; 156, 160; 156, 171; 157, pattern_list; 157, 158; 157, 159; 158, identifier:f_1; 159, identifier:f_2; 160, call; 160, 161; 160, 162; 161, identifier:pairwise; 162, argument_list; 162, 163; 163, call; 163, 164; 163, 165; 164, identifier:list; 165, argument_list; 165, 166; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:frequencies; 169, identifier:values; 170, argument_list; 171, block; 171, 172; 171, 188; 171, 200; 171, 212; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:num; 175, call; 175, 176; 175, 177; 176, identifier:sum; 177, generator_expression; 177, 178; 177, 185; 178, binary_operator:*; 178, 179; 178, 182; 179, subscript; 179, 180; 179, 181; 180, identifier:f_1; 181, identifier:a; 182, subscript; 182, 183; 182, 184; 183, identifier:f_2; 184, identifier:a; 185, for_in_clause; 185, 186; 185, 187; 186, identifier:a; 187, identifier:all_indexes; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:denom_1; 191, call; 191, 192; 191, 193; 192, identifier:sum; 193, generator_expression; 193, 194; 193, 197; 194, binary_operator:**; 194, 195; 194, 196; 195, identifier:f; 196, integer:2; 197, for_in_clause; 197, 198; 197, 199; 198, identifier:f; 199, identifier:f_1; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 203; 202, identifier:denom_2; 203, call; 203, 204; 203, 205; 204, identifier:sum; 205, generator_expression; 205, 206; 205, 209; 206, binary_operator:**; 206, 207; 206, 208; 207, identifier:f; 208, integer:2; 209, for_in_clause; 209, 210; 209, 211; 210, identifier:f; 211, identifier:f_2; 212, expression_statement; 212, 213; 213, call; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:cos_dist; 216, identifier:append; 217, argument_list; 217, 218; 218, binary_operator:-; 218, 219; 218, 220; 219, integer:1; 220, binary_operator:/; 220, 221; 220, 222; 221, identifier:num; 222, parenthesized_expression; 222, 223; 223, binary_operator:*; 223, 224; 223, 227; 224, binary_operator:**; 224, 225; 224, 226; 225, identifier:denom_1; 226, float:.5; 227, binary_operator:**; 227, 228; 227, 229; 228, identifier:denom_2; 229, float:.5; 230, return_statement; 230, 231; 231, call; 231, 232; 231, 233; 232, identifier:statistics; 233, argument_list; 233, 234; 233, 235; 234, identifier:cos_dist; 235, keyword_argument; 235, 236; 235, 237; 236, identifier:summary; 237, identifier:summary
def churn_rate(user, summary='default', **kwargs): """ Computes the frequency spent at every towers each week, and returns the distribution of the cosine similarity between two consecutives week. .. note:: The churn rate is always computed between pairs of weeks. """ if len(user.records) == 0: return statistics([], summary=summary) query = { 'groupby': 'week', 'divide_by': OrderedDict([ ('part_of_week', ['allweek']), ('part_of_day', ['allday']) ]), 'using': 'records', 'filter_empty': True, 'binning': True } rv = grouping_query(user, query) weekly_positions = rv[0][1] all_positions = list(set(p for l in weekly_positions for p in l)) frequencies = {} cos_dist = [] for week, week_positions in enumerate(weekly_positions): count = Counter(week_positions) total = sum(count.values()) frequencies[week] = [count.get(p, 0) / total for p in all_positions] all_indexes = range(len(all_positions)) for f_1, f_2 in pairwise(list(frequencies.values())): num = sum(f_1[a] * f_2[a] for a in all_indexes) denom_1 = sum(f ** 2 for f in f_1) denom_2 = sum(f ** 2 for f in f_2) cos_dist.append(1 - num / (denom_1 ** .5 * denom_2 ** .5)) return statistics(cos_dist, summary=summary)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:describe; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 42; 5, 56; 5, 70; 5, 113; 5, 130; 5, 140; 5, 163; 5, 193; 5, 228; 5, 258; 5, 279; 5, 300; 5, 321; 6, expression_statement; 6, 7; 7, comment; 8, function_definition; 8, 9; 8, 10; 8, 13; 9, function_name:format_int; 10, parameters; 10, 11; 10, 12; 11, identifier:name; 12, identifier:n; 13, block; 13, 14; 14, if_statement; 14, 15; 14, 22; 14, 34; 15, boolean_operator:or; 15, 16; 15, 19; 16, comparison_operator:==; 16, 17; 16, 18; 17, identifier:n; 18, integer:0; 19, comparison_operator:==; 19, 20; 19, 21; 20, identifier:n; 21, integer:1; 22, block; 22, 23; 23, return_statement; 23, 24; 24, binary_operator:%; 24, 25; 24, 26; 25, string:"%i %s"; 26, tuple; 26, 27; 26, 28; 27, identifier:n; 28, subscript; 28, 29; 28, 30; 29, identifier:name; 30, slice; 30, 31; 30, 32; 31, colon; 32, unary_operator:-; 32, 33; 33, integer:1; 34, else_clause; 34, 35; 35, block; 35, 36; 36, return_statement; 36, 37; 37, binary_operator:%; 37, 38; 37, 39; 38, string:"%i %s"; 39, tuple; 39, 40; 39, 41; 40, identifier:n; 41, identifier:name; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:empty_box; 45, binary_operator:+; 45, 46; 45, 55; 46, binary_operator:+; 46, 47; 46, 52; 47, binary_operator:+; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:Colors; 50, identifier:OKGREEN; 51, string:'[ ]'; 52, attribute; 52, 53; 52, 54; 53, identifier:Colors; 54, identifier:ENDC; 55, string:' '; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:filled_box; 59, binary_operator:+; 59, 60; 59, 69; 60, binary_operator:+; 60, 61; 60, 66; 61, binary_operator:+; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:Colors; 64, identifier:OKGREEN; 65, string:'[x]'; 66, attribute; 66, 67; 66, 68; 67, identifier:Colors; 68, identifier:ENDC; 69, string:' '; 70, if_statement; 70, 71; 70, 76; 70, 84; 71, comparison_operator:is; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:self; 74, identifier:start_time; 75, None; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 80; 79, identifier:print; 80, argument_list; 80, 81; 81, binary_operator:+; 81, 82; 81, 83; 82, identifier:empty_box; 83, string:"No records stored"; 84, else_clause; 84, 85; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 89; 88, identifier:print; 89, argument_list; 89, 90; 90, parenthesized_expression; 90, 91; 91, binary_operator:+; 91, 92; 91, 104; 92, binary_operator:+; 92, 93; 92, 94; 93, identifier:filled_box; 94, call; 94, 95; 94, 96; 95, identifier:format_int; 96, argument_list; 96, 97; 96, 98; 97, string:"records"; 98, call; 98, 99; 98, 100; 99, identifier:len; 100, argument_list; 100, 101; 101, attribute; 101, 102; 101, 103; 102, identifier:self; 103, identifier:records; 104, binary_operator:%; 104, 105; 104, 106; 105, string:" from %s to %s"; 106, tuple; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:self; 109, identifier:start_time; 110, attribute; 110, 111; 110, 112; 111, identifier:self; 112, identifier:end_time; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:nb_contacts; 116, call; 116, 117; 116, 122; 117, attribute; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:bc; 120, identifier:individual; 121, identifier:number_of_contacts; 122, argument_list; 122, 123; 122, 124; 122, 127; 123, identifier:self; 124, keyword_argument; 124, 125; 124, 126; 125, identifier:interaction; 126, string:'callandtext'; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:groupby; 129, None; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:nb_contacts; 133, subscript; 133, 134; 133, 139; 134, subscript; 134, 135; 134, 138; 135, subscript; 135, 136; 135, 137; 136, identifier:nb_contacts; 137, string:'allweek'; 138, string:'allday'; 139, string:'callandtext'; 140, if_statement; 140, 141; 140, 142; 140, 154; 141, identifier:nb_contacts; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, call; 144, 145; 144, 146; 145, identifier:print; 146, argument_list; 146, 147; 147, binary_operator:+; 147, 148; 147, 149; 148, identifier:filled_box; 149, call; 149, 150; 149, 151; 150, identifier:format_int; 151, argument_list; 151, 152; 151, 153; 152, string:"contacts"; 153, identifier:nb_contacts; 154, else_clause; 154, 155; 155, block; 155, 156; 156, expression_statement; 156, 157; 157, call; 157, 158; 157, 159; 158, identifier:print; 159, argument_list; 159, 160; 160, binary_operator:+; 160, 161; 160, 162; 161, identifier:empty_box; 162, string:"No contacts"; 163, if_statement; 163, 164; 163, 167; 163, 184; 164, attribute; 164, 165; 164, 166; 165, identifier:self; 166, identifier:has_attributes; 167, block; 167, 168; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 171; 170, identifier:print; 171, argument_list; 171, 172; 172, binary_operator:+; 172, 173; 172, 174; 173, identifier:filled_box; 174, call; 174, 175; 174, 176; 175, identifier:format_int; 176, argument_list; 176, 177; 176, 178; 177, string:"attributes"; 178, call; 178, 179; 178, 180; 179, identifier:len; 180, argument_list; 180, 181; 181, attribute; 181, 182; 181, 183; 182, identifier:self; 183, identifier:attributes; 184, else_clause; 184, 185; 185, block; 185, 186; 186, expression_statement; 186, 187; 187, call; 187, 188; 187, 189; 188, identifier:print; 189, argument_list; 189, 190; 190, binary_operator:+; 190, 191; 190, 192; 191, identifier:empty_box; 192, string:"No attribute stored"; 193, if_statement; 193, 194; 193, 202; 193, 210; 194, comparison_operator:==; 194, 195; 194, 201; 195, call; 195, 196; 195, 197; 196, identifier:len; 197, argument_list; 197, 198; 198, attribute; 198, 199; 198, 200; 199, identifier:self; 200, identifier:antennas; 201, integer:0; 202, block; 202, 203; 203, expression_statement; 203, 204; 204, call; 204, 205; 204, 206; 205, identifier:print; 206, argument_list; 206, 207; 207, binary_operator:+; 207, 208; 207, 209; 208, identifier:empty_box; 209, string:"No antenna stored"; 210, else_clause; 210, 211; 211, block; 211, 212; 212, expression_statement; 212, 213; 213, call; 213, 214; 213, 215; 214, identifier:print; 215, argument_list; 215, 216; 216, binary_operator:+; 216, 217; 216, 218; 217, identifier:filled_box; 218, call; 218, 219; 218, 220; 219, identifier:format_int; 220, argument_list; 220, 221; 220, 222; 221, string:"antennas"; 222, call; 222, 223; 222, 224; 223, identifier:len; 224, argument_list; 224, 225; 225, attribute; 225, 226; 225, 227; 226, identifier:self; 227, identifier:antennas; 228, if_statement; 228, 229; 228, 232; 228, 249; 229, attribute; 229, 230; 229, 231; 230, identifier:self; 231, identifier:has_recharges; 232, block; 232, 233; 233, expression_statement; 233, 234; 234, call; 234, 235; 234, 236; 235, identifier:print; 236, argument_list; 236, 237; 237, binary_operator:+; 237, 238; 237, 239; 238, identifier:filled_box; 239, call; 239, 240; 239, 241; 240, identifier:format_int; 241, argument_list; 241, 242; 241, 243; 242, string:"recharges"; 243, call; 243, 244; 243, 245; 244, identifier:len; 245, argument_list; 245, 246; 246, attribute; 246, 247; 246, 248; 247, identifier:self; 248, identifier:recharges; 249, else_clause; 249, 250; 250, block; 250, 251; 251, expression_statement; 251, 252; 252, call; 252, 253; 252, 254; 253, identifier:print; 254, argument_list; 254, 255; 255, binary_operator:+; 255, 256; 255, 257; 256, identifier:empty_box; 257, string:"No recharges"; 258, if_statement; 258, 259; 258, 262; 258, 270; 259, attribute; 259, 260; 259, 261; 260, identifier:self; 261, identifier:has_home; 262, block; 262, 263; 263, expression_statement; 263, 264; 264, call; 264, 265; 264, 266; 265, identifier:print; 266, argument_list; 266, 267; 267, binary_operator:+; 267, 268; 267, 269; 268, identifier:filled_box; 269, string:"Has home"; 270, else_clause; 270, 271; 271, block; 271, 272; 272, expression_statement; 272, 273; 273, call; 273, 274; 273, 275; 274, identifier:print; 275, argument_list; 275, 276; 276, binary_operator:+; 276, 277; 276, 278; 277, identifier:empty_box; 278, string:"No home"; 279, if_statement; 279, 280; 279, 283; 279, 291; 280, attribute; 280, 281; 280, 282; 281, identifier:self; 282, identifier:has_text; 283, block; 283, 284; 284, expression_statement; 284, 285; 285, call; 285, 286; 285, 287; 286, identifier:print; 287, argument_list; 287, 288; 288, binary_operator:+; 288, 289; 288, 290; 289, identifier:filled_box; 290, string:"Has texts"; 291, else_clause; 291, 292; 292, block; 292, 293; 293, expression_statement; 293, 294; 294, call; 294, 295; 294, 296; 295, identifier:print; 296, argument_list; 296, 297; 297, binary_operator:+; 297, 298; 297, 299; 298, identifier:empty_box; 299, string:"No texts"; 300, if_statement; 300, 301; 300, 304; 300, 312; 301, attribute; 301, 302; 301, 303; 302, identifier:self; 303, identifier:has_call; 304, block; 304, 305; 305, expression_statement; 305, 306; 306, call; 306, 307; 306, 308; 307, identifier:print; 308, argument_list; 308, 309; 309, binary_operator:+; 309, 310; 309, 311; 310, identifier:filled_box; 311, string:"Has calls"; 312, else_clause; 312, 313; 313, block; 313, 314; 314, expression_statement; 314, 315; 315, call; 315, 316; 315, 317; 316, identifier:print; 317, argument_list; 317, 318; 318, binary_operator:+; 318, 319; 318, 320; 319, identifier:empty_box; 320, string:"No calls"; 321, if_statement; 321, 322; 321, 325; 321, 333; 322, attribute; 322, 323; 322, 324; 323, identifier:self; 324, identifier:has_network; 325, block; 325, 326; 326, expression_statement; 326, 327; 327, call; 327, 328; 327, 329; 328, identifier:print; 329, argument_list; 329, 330; 330, binary_operator:+; 330, 331; 330, 332; 331, identifier:filled_box; 332, string:"Has network"; 333, else_clause; 333, 334; 334, block; 334, 335; 335, expression_statement; 335, 336; 336, call; 336, 337; 336, 338; 337, identifier:print; 338, argument_list; 338, 339; 339, binary_operator:+; 339, 340; 339, 341; 340, identifier:empty_box; 341, string:"No network"
def describe(self): """ Generates a short description of the object, and writes it to the standard output. Examples -------- >>> import bandicoot as bc >>> user = bc.User() >>> user.records = bc.tests.generate_user.random_burst(5) >>> user.describe() [x] 5 records from 2014-01-01 10:41:00 to 2014-01-01 11:21:00 5 contacts [x] 1 attribute """ def format_int(name, n): if n == 0 or n == 1: return "%i %s" % (n, name[:-1]) else: return "%i %s" % (n, name) empty_box = Colors.OKGREEN + '[ ]' + Colors.ENDC + ' ' filled_box = Colors.OKGREEN + '[x]' + Colors.ENDC + ' ' if self.start_time is None: print(empty_box + "No records stored") else: print((filled_box + format_int("records", len(self.records)) + " from %s to %s" % (self.start_time, self.end_time))) nb_contacts = bc.individual.number_of_contacts( self, interaction='callandtext', groupby=None) nb_contacts = nb_contacts['allweek']['allday']['callandtext'] if nb_contacts: print(filled_box + format_int("contacts", nb_contacts)) else: print(empty_box + "No contacts") if self.has_attributes: print(filled_box + format_int("attributes", len(self.attributes))) else: print(empty_box + "No attribute stored") if len(self.antennas) == 0: print(empty_box + "No antenna stored") else: print(filled_box + format_int("antennas", len(self.antennas))) if self.has_recharges: print(filled_box + format_int("recharges", len(self.recharges))) else: print(empty_box + "No recharges") if self.has_home: print(filled_box + "Has home") else: print(empty_box + "No home") if self.has_text: print(filled_box + "Has texts") else: print(empty_box + "No texts") if self.has_call: print(filled_box + "Has calls") else: print(empty_box + "No calls") if self.has_network: print(filled_box + "Has network") else: print(empty_box + "No network")
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:matrix_index; 3, parameters; 3, 4; 4, identifier:user; 5, block; 5, 6; 5, 8; 5, 31; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:other_keys; 11, call; 11, 12; 11, 13; 12, identifier:sorted; 13, argument_list; 13, 14; 14, list_comprehension; 14, 15; 14, 16; 14, 25; 15, identifier:k; 16, for_in_clause; 16, 17; 16, 18; 17, identifier:k; 18, call; 18, 19; 18, 24; 19, attribute; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:user; 22, identifier:network; 23, identifier:keys; 24, argument_list; 25, if_clause; 25, 26; 26, comparison_operator:!=; 26, 27; 26, 28; 27, identifier:k; 28, attribute; 28, 29; 28, 30; 29, identifier:user; 30, identifier:name; 31, return_statement; 31, 32; 32, binary_operator:+; 32, 33; 32, 37; 33, list:[user.name]; 33, 34; 34, attribute; 34, 35; 34, 36; 35, identifier:user; 36, identifier:name; 37, identifier:other_keys
def matrix_index(user): """ Returns the keys associated with each axis of the matrices. The first key is always the name of the current user, followed by the sorted names of all the correspondants. """ other_keys = sorted([k for k in user.network.keys() if k != user.name]) return [user.name] + other_keys
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:clustering_coefficient_weighted; 3, parameters; 3, 4; 3, 5; 4, identifier:user; 5, default_parameter; 5, 6; 5, 7; 6, identifier:interaction; 7, None; 8, block; 8, 9; 8, 11; 8, 21; 8, 36; 8, 46; 8, 53; 8, 57; 8, 132; 8, 148; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:matrix; 14, call; 14, 15; 14, 16; 15, identifier:matrix_undirected_weighted; 16, argument_list; 16, 17; 16, 18; 17, identifier:user; 18, keyword_argument; 18, 19; 18, 20; 19, identifier:interaction; 20, identifier:interaction; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:weights; 24, list_comprehension; 24, 25; 24, 26; 24, 29; 24, 32; 25, identifier:weight; 26, for_in_clause; 26, 27; 26, 28; 27, identifier:g; 28, identifier:matrix; 29, for_in_clause; 29, 30; 29, 31; 30, identifier:weight; 31, identifier:g; 32, if_clause; 32, 33; 33, comparison_operator:is; 33, 34; 33, 35; 34, identifier:weight; 35, None; 36, if_statement; 36, 37; 36, 43; 37, comparison_operator:==; 37, 38; 37, 42; 38, call; 38, 39; 38, 40; 39, identifier:len; 40, argument_list; 40, 41; 41, identifier:weights; 42, integer:0; 43, block; 43, 44; 44, return_statement; 44, 45; 45, None; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:max_weight; 49, call; 49, 50; 49, 51; 50, identifier:max; 51, argument_list; 51, 52; 52, identifier:weights; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:triplet_weight; 56, integer:0; 57, for_statement; 57, 58; 57, 61; 57, 72; 58, pattern_list; 58, 59; 58, 60; 59, identifier:a; 60, identifier:b; 61, call; 61, 62; 61, 63; 62, identifier:combinations; 63, argument_list; 63, 64; 63, 71; 64, call; 64, 65; 64, 66; 65, identifier:range; 66, argument_list; 66, 67; 67, call; 67, 68; 67, 69; 68, identifier:len; 69, argument_list; 69, 70; 70, identifier:matrix; 71, integer:2; 72, block; 72, 73; 72, 95; 72, 109; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 79; 75, pattern_list; 75, 76; 75, 77; 75, 78; 76, identifier:a_b; 77, identifier:a_c; 78, identifier:b_c; 79, expression_list; 79, 80; 79, 85; 79, 90; 80, subscript; 80, 81; 80, 84; 81, subscript; 81, 82; 81, 83; 82, identifier:matrix; 83, identifier:a; 84, identifier:b; 85, subscript; 85, 86; 85, 89; 86, subscript; 86, 87; 86, 88; 87, identifier:matrix; 88, identifier:a; 89, integer:0; 90, subscript; 90, 91; 90, 94; 91, subscript; 91, 92; 91, 93; 92, identifier:matrix; 93, identifier:b; 94, integer:0; 95, if_statement; 95, 96; 95, 107; 96, boolean_operator:or; 96, 97; 96, 104; 97, boolean_operator:or; 97, 98; 97, 101; 98, comparison_operator:is; 98, 99; 98, 100; 99, identifier:a_b; 100, None; 101, comparison_operator:is; 101, 102; 101, 103; 102, identifier:a_c; 103, None; 104, comparison_operator:is; 104, 105; 104, 106; 105, identifier:b_c; 106, None; 107, block; 107, 108; 108, continue_statement; 109, if_statement; 109, 110; 109, 115; 110, boolean_operator:and; 110, 111; 110, 114; 111, boolean_operator:and; 111, 112; 111, 113; 112, identifier:a_b; 113, identifier:a_c; 114, identifier:b_c; 115, block; 115, 116; 116, expression_statement; 116, 117; 117, augmented_assignment:+=; 117, 118; 117, 119; 118, identifier:triplet_weight; 119, binary_operator:/; 119, 120; 119, 131; 120, binary_operator:**; 120, 121; 120, 127; 121, parenthesized_expression; 121, 122; 122, binary_operator:*; 122, 123; 122, 126; 123, binary_operator:*; 123, 124; 123, 125; 124, identifier:a_b; 125, identifier:a_c; 126, identifier:b_c; 127, parenthesized_expression; 127, 128; 128, binary_operator:/; 128, 129; 128, 130; 129, integer:1; 130, integer:3; 131, identifier:max_weight; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:d_ego; 135, call; 135, 136; 135, 137; 136, identifier:sum; 137, generator_expression; 137, 138; 137, 139; 137, 144; 138, integer:1; 139, for_in_clause; 139, 140; 139, 141; 140, identifier:i; 141, subscript; 141, 142; 141, 143; 142, identifier:matrix; 143, integer:0; 144, if_clause; 144, 145; 145, comparison_operator:>; 145, 146; 145, 147; 146, identifier:i; 147, integer:0; 148, return_statement; 148, 149; 149, conditional_expression:if; 149, 150; 149, 161; 149, 164; 150, binary_operator:/; 150, 151; 150, 154; 151, binary_operator:*; 151, 152; 151, 153; 152, integer:2; 153, identifier:triplet_weight; 154, parenthesized_expression; 154, 155; 155, binary_operator:*; 155, 156; 155, 157; 156, identifier:d_ego; 157, parenthesized_expression; 157, 158; 158, binary_operator:-; 158, 159; 158, 160; 159, identifier:d_ego; 160, integer:1; 161, comparison_operator:>; 161, 162; 161, 163; 162, identifier:d_ego; 163, integer:1; 164, integer:0
def clustering_coefficient_weighted(user, interaction=None): """ The clustering coefficient of the user's weighted, undirected network. It is defined the same way as :meth`~bandicoot.network.clustering_coefficient_unweighted`, except that closed triplets are weighted by the number of interactions. For each triplet (A, B, C), we compute the geometric mean of the number of interactions, using the undirected weighted matrix: .. math:: weight_{abc} = (m_{ab} \; m_{bc} \; m_{ac})^{1/3} The weight is normalized, between 0 and 1, by the maximum value in the matrix. """ matrix = matrix_undirected_weighted(user, interaction=interaction) weights = [weight for g in matrix for weight in g if weight is not None] if len(weights) == 0: return None max_weight = max(weights) triplet_weight = 0 for a, b in combinations(range(len(matrix)), 2): a_b, a_c, b_c = matrix[a][b], matrix[a][0], matrix[b][0] if a_b is None or a_c is None or b_c is None: continue if a_b and a_c and b_c: triplet_weight += (a_b * a_c * b_c) ** (1 / 3) / max_weight d_ego = sum(1 for i in matrix[0] if i > 0) return 2 * triplet_weight / (d_ego * (d_ego - 1)) if d_ego > 1 else 0
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:assortativity_indicators; 3, parameters; 3, 4; 4, identifier:user; 5, block; 5, 6; 5, 8; 5, 15; 5, 22; 5, 29; 5, 30; 5, 40; 5, 76; 5, 169; 5, 173; 5, 189; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:matrix; 11, call; 11, 12; 11, 13; 12, identifier:matrix_undirected_unweighted; 13, argument_list; 13, 14; 14, identifier:user; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:count_indicator; 18, call; 18, 19; 18, 20; 19, identifier:defaultdict; 20, argument_list; 20, 21; 21, identifier:int; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:total_indicator; 25, call; 25, 26; 25, 27; 26, identifier:defaultdict; 27, argument_list; 27, 28; 28, identifier:int; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:ego_indics; 33, call; 33, 34; 33, 35; 34, identifier:all; 35, argument_list; 35, 36; 35, 37; 36, identifier:user; 37, keyword_argument; 37, 38; 37, 39; 38, identifier:flatten; 39, True; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:ego_indics; 43, dictionary_comprehension; 43, 44; 43, 47; 43, 56; 44, pair; 44, 45; 44, 46; 45, identifier:a; 46, identifier:value; 47, for_in_clause; 47, 48; 47, 51; 48, pattern_list; 48, 49; 48, 50; 49, identifier:a; 50, identifier:value; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:ego_indics; 54, identifier:items; 55, argument_list; 56, if_clause; 56, 57; 57, boolean_operator:and; 57, 58; 57, 69; 58, boolean_operator:and; 58, 59; 58, 62; 59, comparison_operator:!=; 59, 60; 59, 61; 60, identifier:a; 61, string:"name"; 62, comparison_operator:!=; 62, 63; 62, 68; 63, subscript; 63, 64; 63, 65; 64, identifier:a; 65, slice; 65, 66; 65, 67; 66, colon; 67, integer:11; 68, string:"reporting__"; 69, comparison_operator:!=; 69, 70; 69, 75; 70, subscript; 70, 71; 70, 72; 71, identifier:a; 72, slice; 72, 73; 72, 74; 73, colon; 74, integer:10; 75, string:"attributes"; 76, for_statement; 76, 77; 76, 80; 76, 87; 77, pattern_list; 77, 78; 77, 79; 78, identifier:i; 79, identifier:u_name; 80, call; 80, 81; 80, 82; 81, identifier:enumerate; 82, argument_list; 82, 83; 83, call; 83, 84; 83, 85; 84, identifier:matrix_index; 85, argument_list; 85, 86; 86, identifier:user; 87, block; 87, 88; 87, 100; 87, 101; 87, 121; 87, 131; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:correspondent; 91, call; 91, 92; 91, 97; 92, attribute; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:user; 95, identifier:network; 96, identifier:get; 97, argument_list; 97, 98; 97, 99; 98, identifier:u_name; 99, None; 100, comment; 101, if_statement; 101, 102; 101, 119; 102, boolean_operator:or; 102, 103; 102, 112; 103, boolean_operator:or; 103, 104; 103, 107; 104, comparison_operator:is; 104, 105; 104, 106; 105, identifier:correspondent; 106, None; 107, comparison_operator:==; 107, 108; 107, 109; 108, identifier:u_name; 109, attribute; 109, 110; 109, 111; 110, identifier:user; 111, identifier:name; 112, comparison_operator:==; 112, 113; 112, 118; 113, subscript; 113, 114; 113, 117; 114, subscript; 114, 115; 114, 116; 115, identifier:matrix; 116, integer:0; 117, identifier:i; 118, integer:0; 119, block; 119, 120; 120, continue_statement; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:neighbor_indics; 124, call; 124, 125; 124, 126; 125, identifier:all; 126, argument_list; 126, 127; 126, 128; 127, identifier:correspondent; 128, keyword_argument; 128, 129; 128, 130; 129, identifier:flatten; 130, True; 131, for_statement; 131, 132; 131, 133; 131, 134; 132, identifier:a; 133, identifier:ego_indics; 134, block; 134, 135; 135, if_statement; 135, 136; 135, 147; 136, boolean_operator:and; 136, 137; 136, 142; 137, comparison_operator:is; 137, 138; 137, 141; 138, subscript; 138, 139; 138, 140; 139, identifier:ego_indics; 140, identifier:a; 141, None; 142, comparison_operator:is; 142, 143; 142, 146; 143, subscript; 143, 144; 143, 145; 144, identifier:neighbor_indics; 145, identifier:a; 146, None; 147, block; 147, 148; 147, 154; 148, expression_statement; 148, 149; 149, augmented_assignment:+=; 149, 150; 149, 153; 150, subscript; 150, 151; 150, 152; 151, identifier:total_indicator; 152, identifier:a; 153, integer:1; 154, expression_statement; 154, 155; 155, augmented_assignment:+=; 155, 156; 155, 159; 156, subscript; 156, 157; 156, 158; 157, identifier:count_indicator; 158, identifier:a; 159, binary_operator:**; 159, 160; 159, 168; 160, parenthesized_expression; 160, 161; 161, binary_operator:-; 161, 162; 161, 165; 162, subscript; 162, 163; 162, 164; 163, identifier:ego_indics; 164, identifier:a; 165, subscript; 165, 166; 165, 167; 166, identifier:neighbor_indics; 167, identifier:a; 168, integer:2; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:assortativity; 172, dictionary; 173, for_statement; 173, 174; 173, 175; 173, 176; 174, identifier:i; 175, identifier:count_indicator; 176, block; 176, 177; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 182; 179, subscript; 179, 180; 179, 181; 180, identifier:assortativity; 181, identifier:i; 182, binary_operator:/; 182, 183; 182, 186; 183, subscript; 183, 184; 183, 185; 184, identifier:count_indicator; 185, identifier:i; 186, subscript; 186, 187; 186, 188; 187, identifier:total_indicator; 188, identifier:i; 189, return_statement; 189, 190; 190, identifier:assortativity
def assortativity_indicators(user): """ Computes the assortativity of indicators. This indicator measures the similarity of the current user with his correspondants, for all bandicoot indicators. For each one, it calculates the variance of the current user's value with the values for all his correspondants: .. math:: \\text{assortativity}(J) = \\frac{1}{n} \\sum_i^n (J_{\\text{user}} - J_{\\text{i}})^2 for the indicator :math:`J`, and all the :math:`n` correspondents. """ matrix = matrix_undirected_unweighted(user) count_indicator = defaultdict(int) total_indicator = defaultdict(int) # Use all indicator except reporting variables and attributes ego_indics = all(user, flatten=True) ego_indics = {a: value for a, value in ego_indics.items() if a != "name" and a[:11] != "reporting__" and a[:10] != "attributes"} for i, u_name in enumerate(matrix_index(user)): correspondent = user.network.get(u_name, None) # Non reciprocated edge if correspondent is None or u_name == user.name or matrix[0][i] == 0: continue neighbor_indics = all(correspondent, flatten=True) for a in ego_indics: if ego_indics[a] is not None and neighbor_indics[a] is not None: total_indicator[a] += 1 count_indicator[a] += (ego_indics[a] - neighbor_indics[a]) ** 2 assortativity = {} for i in count_indicator: assortativity[i] = count_indicator[i] / total_indicator[i] return assortativity
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:assortativity_attributes; 3, parameters; 3, 4; 4, identifier:user; 5, block; 5, 6; 5, 8; 5, 15; 5, 35; 5, 39; 5, 98; 5, 102; 5, 161; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:matrix; 11, call; 11, 12; 11, 13; 12, identifier:matrix_undirected_unweighted; 13, argument_list; 13, 14; 14, identifier:user; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:neighbors; 18, list_comprehension; 18, 19; 18, 20; 18, 29; 19, identifier:k; 20, for_in_clause; 20, 21; 20, 22; 21, identifier:k; 22, call; 22, 23; 22, 28; 23, attribute; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:user; 26, identifier:network; 27, identifier:keys; 28, argument_list; 29, if_clause; 29, 30; 30, comparison_operator:!=; 30, 31; 30, 32; 31, identifier:k; 32, attribute; 32, 33; 32, 34; 33, identifier:user; 34, identifier:name; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:neighbors_attrbs; 38, dictionary; 39, for_statement; 39, 40; 39, 43; 39, 50; 40, pattern_list; 40, 41; 40, 42; 41, identifier:i; 42, identifier:u_name; 43, call; 43, 44; 43, 45; 44, identifier:enumerate; 45, argument_list; 45, 46; 46, call; 46, 47; 46, 48; 47, identifier:matrix_index; 48, argument_list; 48, 49; 49, identifier:user; 50, block; 50, 51; 50, 63; 50, 83; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:correspondent; 54, call; 54, 55; 54, 60; 55, attribute; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:user; 58, identifier:network; 59, identifier:get; 60, argument_list; 60, 61; 60, 62; 61, identifier:u_name; 62, None; 63, if_statement; 63, 64; 63, 81; 64, boolean_operator:or; 64, 65; 64, 74; 65, boolean_operator:or; 65, 66; 65, 69; 66, comparison_operator:is; 66, 67; 66, 68; 67, identifier:correspondent; 68, None; 69, comparison_operator:==; 69, 70; 69, 71; 70, identifier:u_name; 71, attribute; 71, 72; 71, 73; 72, identifier:user; 73, identifier:name; 74, comparison_operator:==; 74, 75; 74, 80; 75, subscript; 75, 76; 75, 79; 76, subscript; 76, 77; 76, 78; 77, identifier:matrix; 78, integer:0; 79, identifier:i; 80, integer:0; 81, block; 81, 82; 82, continue_statement; 83, if_statement; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:correspondent; 86, identifier:has_attributes; 87, block; 87, 88; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 95; 90, subscript; 90, 91; 90, 92; 91, identifier:neighbors_attrbs; 92, attribute; 92, 93; 92, 94; 93, identifier:correspondent; 94, identifier:name; 95, attribute; 95, 96; 95, 97; 96, identifier:correspondent; 97, identifier:attributes; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:assortativity; 101, dictionary; 102, for_statement; 102, 103; 102, 104; 102, 107; 103, identifier:a; 104, attribute; 104, 105; 104, 106; 105, identifier:user; 106, identifier:attributes; 107, block; 107, 108; 107, 134; 107, 148; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:total; 111, call; 111, 112; 111, 113; 112, identifier:sum; 113, generator_expression; 113, 114; 113, 115; 113, 118; 114, integer:1; 115, for_in_clause; 115, 116; 115, 117; 116, identifier:n; 117, identifier:neighbors; 118, if_clause; 118, 119; 119, boolean_operator:and; 119, 120; 119, 123; 120, comparison_operator:in; 120, 121; 120, 122; 121, identifier:n; 122, identifier:neighbors_attrbs; 123, comparison_operator:==; 123, 124; 123, 129; 124, subscript; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:user; 127, identifier:attributes; 128, identifier:a; 129, subscript; 129, 130; 129, 133; 130, subscript; 130, 131; 130, 132; 131, identifier:neighbors_attrbs; 132, identifier:n; 133, identifier:a; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:den; 137, call; 137, 138; 137, 139; 138, identifier:sum; 139, generator_expression; 139, 140; 139, 141; 139, 144; 140, integer:1; 141, for_in_clause; 141, 142; 141, 143; 142, identifier:n; 143, identifier:neighbors; 144, if_clause; 144, 145; 145, comparison_operator:in; 145, 146; 145, 147; 146, identifier:n; 147, identifier:neighbors_attrbs; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 153; 150, subscript; 150, 151; 150, 152; 151, identifier:assortativity; 152, identifier:a; 153, conditional_expression:if; 153, 154; 153, 157; 153, 160; 154, binary_operator:/; 154, 155; 154, 156; 155, identifier:total; 156, identifier:den; 157, comparison_operator:!=; 157, 158; 157, 159; 158, identifier:den; 159, integer:0; 160, None; 161, return_statement; 161, 162; 162, identifier:assortativity
def assortativity_attributes(user): """ Computes the assortativity of the nominal attributes. This indicator measures the homophily of the current user with his correspondants, for each attributes. It returns a value between 0 (no assortativity) and 1 (all the contacts share the same value): the percentage of contacts sharing the same value. """ matrix = matrix_undirected_unweighted(user) neighbors = [k for k in user.network.keys() if k != user.name] neighbors_attrbs = {} for i, u_name in enumerate(matrix_index(user)): correspondent = user.network.get(u_name, None) if correspondent is None or u_name == user.name or matrix[0][i] == 0: continue if correspondent.has_attributes: neighbors_attrbs[correspondent.name] = correspondent.attributes assortativity = {} for a in user.attributes: total = sum(1 for n in neighbors if n in neighbors_attrbs and user.attributes[a] == neighbors_attrbs[n][a]) den = sum(1 for n in neighbors if n in neighbors_attrbs) assortativity[a] = total / den if den != 0 else None return assortativity
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:network_sampling; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:n; 5, identifier:filename; 6, default_parameter; 6, 7; 6, 8; 7, identifier:directory; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:snowball; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:user; 14, None; 15, block; 15, 16; 15, 18; 15, 207; 15, 228; 16, expression_statement; 16, 17; 17, comment; 18, if_statement; 18, 19; 18, 20; 18, 131; 19, identifier:snowball; 20, block; 20, 21; 21, if_statement; 21, 22; 21, 25; 21, 31; 22, comparison_operator:is; 22, 23; 22, 24; 23, identifier:user; 24, None; 25, block; 25, 26; 26, raise_statement; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:ValueError; 29, argument_list; 29, 30; 30, string:"Must specify a starting user from whom to initiate the snowball"; 31, else_clause; 31, 32; 32, block; 32, 33; 32, 43; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 38; 35, pattern_list; 35, 36; 35, 37; 36, identifier:users; 37, identifier:agenda; 38, expression_list; 38, 39; 38, 41; 39, list:[user]; 39, 40; 40, identifier:user; 41, list:[user]; 41, 42; 42, identifier:user; 43, while_statement; 43, 44; 43, 50; 44, comparison_operator:>; 44, 45; 44, 49; 45, call; 45, 46; 45, 47; 46, identifier:len; 47, argument_list; 47, 48; 48, identifier:agenda; 49, integer:0; 50, block; 50, 51; 50, 59; 50, 82; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:parent; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:agenda; 57, identifier:pop; 58, argument_list; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:dealphebetized_network; 62, call; 62, 63; 62, 64; 63, identifier:sorted; 64, argument_list; 64, 65; 64, 72; 65, call; 65, 66; 65, 71; 66, attribute; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:parent; 69, identifier:network; 70, identifier:items; 71, argument_list; 72, keyword_argument; 72, 73; 72, 74; 73, identifier:key; 74, lambda; 74, 75; 74, 77; 75, lambda_parameters; 75, 76; 76, identifier:k; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:random; 80, identifier:random; 81, argument_list; 82, for_statement; 82, 83; 82, 84; 82, 85; 83, identifier:neighbor; 84, identifier:dealphebetized_network; 85, block; 85, 86; 86, if_statement; 86, 87; 86, 105; 87, boolean_operator:and; 87, 88; 87, 99; 88, boolean_operator:and; 88, 89; 88, 94; 89, comparison_operator:not; 89, 90; 89, 93; 90, subscript; 90, 91; 90, 92; 91, identifier:neighbor; 92, integer:1; 93, identifier:users; 94, comparison_operator:is; 94, 95; 94, 98; 95, subscript; 95, 96; 95, 97; 96, identifier:neighbor; 97, integer:1; 98, None; 99, comparison_operator:<; 99, 100; 99, 104; 100, call; 100, 101; 100, 102; 101, identifier:len; 102, argument_list; 102, 103; 103, identifier:users; 104, identifier:n; 105, block; 105, 106; 105, 115; 106, expression_statement; 106, 107; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:users; 110, identifier:append; 111, argument_list; 111, 112; 112, subscript; 112, 113; 112, 114; 113, identifier:neighbor; 114, integer:1; 115, if_statement; 115, 116; 115, 121; 116, attribute; 116, 117; 116, 120; 117, subscript; 117, 118; 117, 119; 118, identifier:neighbor; 119, integer:1; 120, identifier:network; 121, block; 121, 122; 122, expression_statement; 122, 123; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:agenda; 126, identifier:push; 127, argument_list; 127, 128; 128, subscript; 128, 129; 128, 130; 129, identifier:neighbor; 130, integer:1; 131, else_clause; 131, 132; 132, block; 132, 133; 132, 163; 132, 180; 132, 188; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:files; 136, list_comprehension; 136, 137; 136, 138; 136, 146; 137, identifier:x; 138, for_in_clause; 138, 139; 138, 140; 139, identifier:x; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:os; 143, identifier:listdir; 144, argument_list; 144, 145; 145, identifier:directory; 146, if_clause; 146, 147; 147, call; 147, 148; 147, 153; 148, attribute; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:os; 151, identifier:path; 152, identifier:isfile; 153, argument_list; 153, 154; 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:join; 160, argument_list; 160, 161; 160, 162; 161, identifier:directory; 162, identifier:x; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:shuffled_files; 166, call; 166, 167; 166, 168; 167, identifier:sorted; 168, argument_list; 168, 169; 168, 170; 169, identifier:files; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:key; 172, lambda; 172, 173; 172, 175; 173, lambda_parameters; 173, 174; 174, identifier:k; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:random; 178, identifier:random; 179, argument_list; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:user_names; 183, subscript; 183, 184; 183, 185; 184, identifier:shuffled_files; 185, slice; 185, 186; 185, 187; 186, colon; 187, identifier:n; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:users; 191, list_comprehension; 191, 192; 191, 204; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:bc; 195, identifier:read_csv; 196, argument_list; 196, 197; 196, 203; 197, subscript; 197, 198; 197, 199; 198, identifier:u; 199, slice; 199, 200; 199, 201; 200, colon; 201, unary_operator:-; 201, 202; 202, integer:4; 203, identifier:directory; 204, for_in_clause; 204, 205; 204, 206; 205, identifier:u; 206, identifier:user_names; 207, if_statement; 207, 208; 207, 214; 208, comparison_operator:<; 208, 209; 208, 213; 209, call; 209, 210; 209, 211; 210, identifier:len; 211, argument_list; 211, 212; 212, identifier:users; 213, identifier:n; 214, block; 214, 215; 215, raise_statement; 215, 216; 216, call; 216, 217; 216, 218; 217, identifier:ValueError; 218, argument_list; 218, 219; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, string:"Specified more users than records that exist, only {} records available"; 222, identifier:format; 223, argument_list; 223, 224; 224, call; 224, 225; 224, 226; 225, identifier:len; 226, argument_list; 226, 227; 227, identifier:users; 228, expression_statement; 228, 229; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:bc; 232, identifier:to_csv; 233, argument_list; 233, 234; 233, 246; 234, list_comprehension; 234, 235; 234, 243; 235, call; 235, 236; 235, 241; 236, attribute; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:bc; 239, identifier:utils; 240, identifier:all; 241, argument_list; 241, 242; 242, identifier:u; 243, for_in_clause; 243, 244; 243, 245; 244, identifier:u; 245, identifier:users; 246, identifier:filename
def network_sampling(n, filename, directory=None, snowball=False, user=None): """ Selects a few users and exports a CSV of indicators for them. TODO: Returns the network/graph between the selected users. Parameters ---------- n : int Number of users to select. filename : string File to export to. directory: string Directory to select users from if using the default random selection. snowball: starts from a specified user, iterates over neighbors, and does a BFS until n neighbors are reached """ if snowball: if user is None: raise ValueError("Must specify a starting user from whom to initiate the snowball") else: users, agenda = [user], [user] while len(agenda) > 0: parent = agenda.pop() dealphebetized_network = sorted(parent.network.items(), key=lambda k: random.random()) for neighbor in dealphebetized_network: if neighbor[1] not in users and neighbor[1] is not None and len(users) < n: users.append(neighbor[1]) if neighbor[1].network: agenda.push(neighbor[1]) else: files = [x for x in os.listdir(directory) if os.path.isfile(os.path.join(directory, x))] shuffled_files = sorted(files, key=lambda k: random.random()) user_names = shuffled_files[:n] users = [bc.read_csv(u[:-4], directory) for u in user_names] if len(users) < n: raise ValueError("Specified more users than records that exist, only {} records available".format(len(users))) bc.to_csv([bc.utils.all(u) for u in users], filename)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:to_csv; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:objects; 5, identifier:filename; 6, default_parameter; 6, 7; 6, 8; 7, identifier:digits; 8, integer:5; 9, default_parameter; 9, 10; 9, 11; 10, identifier:warnings; 11, True; 12, block; 12, 13; 12, 15; 12, 28; 12, 39; 12, 54; 12, 75; 12, 183; 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:objects; 21, identifier:list; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:objects; 26, list:[objects]; 26, 27; 27, identifier:objects; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:data; 31, list_comprehension; 31, 32; 31, 36; 32, call; 32, 33; 32, 34; 33, identifier:flatten; 34, argument_list; 34, 35; 35, identifier:obj; 36, for_in_clause; 36, 37; 36, 38; 37, identifier:obj; 38, identifier:objects; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:all_keys; 42, list_comprehension; 42, 43; 42, 44; 42, 47; 43, identifier:d; 44, for_in_clause; 44, 45; 44, 46; 45, identifier:datum; 46, identifier:data; 47, for_in_clause; 47, 48; 47, 49; 48, identifier:d; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:datum; 52, identifier:keys; 53, argument_list; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:field_names; 57, call; 57, 58; 57, 59; 58, identifier:sorted; 59, argument_list; 59, 60; 59, 64; 60, call; 60, 61; 60, 62; 61, identifier:set; 62, argument_list; 62, 63; 63, identifier:all_keys; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:key; 66, lambda; 66, 67; 66, 69; 67, lambda_parameters; 67, 68; 68, identifier:x; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:all_keys; 72, identifier:index; 73, argument_list; 73, 74; 74, identifier:x; 75, with_statement; 75, 76; 75, 86; 76, with_clause; 76, 77; 77, with_item; 77, 78; 78, as_pattern; 78, 79; 78, 84; 79, call; 79, 80; 79, 81; 80, identifier:open; 81, argument_list; 81, 82; 81, 83; 82, identifier:filename; 83, string:'w'; 84, as_pattern_target; 84, 85; 85, identifier:f; 86, block; 86, 87; 86, 96; 86, 103; 86, 138; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:w; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:csv; 93, identifier:writer; 94, argument_list; 94, 95; 95, identifier:f; 96, expression_statement; 96, 97; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:w; 100, identifier:writerow; 101, argument_list; 101, 102; 102, identifier:field_names; 103, function_definition; 103, 104; 103, 105; 103, 107; 104, function_name:make_repr; 105, parameters; 105, 106; 106, identifier:item; 107, block; 107, 108; 108, if_statement; 108, 109; 108, 112; 108, 115; 108, 131; 109, comparison_operator:is; 109, 110; 109, 111; 110, identifier:item; 111, None; 112, block; 112, 113; 113, return_statement; 113, 114; 114, None; 115, elif_clause; 115, 116; 115, 121; 116, call; 116, 117; 116, 118; 117, identifier:isinstance; 118, argument_list; 118, 119; 118, 120; 119, identifier:item; 120, identifier:float; 121, block; 121, 122; 122, return_statement; 122, 123; 123, call; 123, 124; 123, 125; 124, identifier:repr; 125, argument_list; 125, 126; 126, call; 126, 127; 126, 128; 127, identifier:round; 128, argument_list; 128, 129; 128, 130; 129, identifier:item; 130, identifier:digits; 131, else_clause; 131, 132; 132, block; 132, 133; 133, return_statement; 133, 134; 134, call; 134, 135; 134, 136; 135, identifier:str; 136, argument_list; 136, 137; 137, identifier:item; 138, for_statement; 138, 139; 138, 140; 138, 141; 139, identifier:row; 140, identifier:data; 141, block; 141, 142; 141, 163; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:row; 145, call; 145, 146; 145, 147; 146, identifier:dict; 147, generator_expression; 147, 148; 147, 154; 148, tuple; 148, 149; 148, 150; 149, identifier:k; 150, call; 150, 151; 150, 152; 151, identifier:make_repr; 152, argument_list; 152, 153; 153, identifier:v; 154, for_in_clause; 154, 155; 154, 158; 155, pattern_list; 155, 156; 155, 157; 156, identifier:k; 157, identifier:v; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:row; 161, identifier:items; 162, argument_list; 163, expression_statement; 163, 164; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:w; 167, identifier:writerow; 168, argument_list; 168, 169; 169, list_comprehension; 169, 170; 169, 180; 170, call; 170, 171; 170, 172; 171, identifier:make_repr; 172, argument_list; 172, 173; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:row; 176, identifier:get; 177, argument_list; 177, 178; 177, 179; 178, identifier:k; 179, None; 180, for_in_clause; 180, 181; 180, 182; 181, identifier:k; 182, identifier:field_names; 183, if_statement; 183, 184; 183, 185; 184, identifier:warnings; 185, block; 185, 186; 186, expression_statement; 186, 187; 187, call; 187, 188; 187, 189; 188, identifier:print; 189, argument_list; 189, 190; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, string:"Successfully exported {} object(s) to {}"; 193, identifier:format; 194, argument_list; 194, 195; 194, 199; 195, call; 195, 196; 195, 197; 196, identifier:len; 197, argument_list; 197, 198; 198, identifier:objects; 199, identifier:filename
def to_csv(objects, filename, digits=5, warnings=True): """ Export the flatten indicators of one or several users to CSV. Parameters ---------- objects : list List of objects to be exported. filename : string File to export to. digits : int Precision of floats. Examples -------- This function can be used to export the results of :meth`bandicoot.utils.all`. >>> U_1 = bc.User() >>> U_2 = bc.User() >>> bc.to_csv([bc.utils.all(U_1), bc.utils.all(U_2)], 'results_1_2.csv') If you only have one object, you can simply pass it as argument: >>> bc.to_csv(bc.utils.all(U_1), 'results_1.csv') """ if not isinstance(objects, list): objects = [objects] data = [flatten(obj) for obj in objects] all_keys = [d for datum in data for d in datum.keys()] field_names = sorted(set(all_keys), key=lambda x: all_keys.index(x)) with open(filename, 'w') as f: w = csv.writer(f) w.writerow(field_names) def make_repr(item): if item is None: return None elif isinstance(item, float): return repr(round(item, digits)) else: return str(item) for row in data: row = dict((k, make_repr(v)) for k, v in row.items()) w.writerow([make_repr(row.get(k, None)) for k in field_names]) if warnings: print("Successfully exported {} object(s) to {}".format(len(objects), filename))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 33; 2, function_name:read_csv; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 3, 24; 3, 27; 3, 30; 4, identifier:user_id; 5, identifier:records_path; 6, default_parameter; 6, 7; 6, 8; 7, identifier:antennas_path; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:attributes_path; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:recharges_path; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:network; 17, False; 18, default_parameter; 18, 19; 18, 20; 19, identifier:duration_format; 20, string:'seconds'; 21, default_parameter; 21, 22; 21, 23; 22, identifier:describe; 23, True; 24, default_parameter; 24, 25; 24, 26; 25, identifier:warnings; 26, True; 27, default_parameter; 27, 28; 27, 29; 28, identifier:errors; 29, False; 30, default_parameter; 30, 31; 30, 32; 31, identifier:drop_duplicates; 32, False; 33, block; 33, 34; 33, 36; 33, 40; 33, 98; 33, 112; 33, 145; 33, 149; 33, 175; 33, 179; 33, 205; 33, 230; 33, 231; 33, 259; 33, 268; 33, 275; 34, expression_statement; 34, 35; 35, comment; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:antennas; 39, None; 40, if_statement; 40, 41; 40, 44; 41, comparison_operator:is; 41, 42; 41, 43; 42, identifier:antennas_path; 43, None; 44, block; 44, 45; 45, try_statement; 45, 46; 45, 94; 46, block; 46, 47; 47, with_statement; 47, 48; 47, 58; 48, with_clause; 48, 49; 49, with_item; 49, 50; 50, as_pattern; 50, 51; 50, 56; 51, call; 51, 52; 51, 53; 52, identifier:open; 53, argument_list; 53, 54; 53, 55; 54, identifier:antennas_path; 55, string:'r'; 56, as_pattern_target; 56, 57; 57, identifier:csv_file; 58, block; 58, 59; 58, 68; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:reader; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:csv; 65, identifier:DictReader; 66, argument_list; 66, 67; 67, identifier:csv_file; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:antennas; 71, call; 71, 72; 71, 73; 72, identifier:dict; 73, generator_expression; 73, 74; 73, 91; 74, tuple; 74, 75; 74, 78; 75, subscript; 75, 76; 75, 77; 76, identifier:d; 77, string:'antenna_id'; 78, tuple; 78, 79; 78, 85; 79, call; 79, 80; 79, 81; 80, identifier:float; 81, argument_list; 81, 82; 82, subscript; 82, 83; 82, 84; 83, identifier:d; 84, string:'latitude'; 85, call; 85, 86; 85, 87; 86, identifier:float; 87, argument_list; 87, 88; 88, subscript; 88, 89; 88, 90; 89, identifier:d; 90, string:'longitude'; 91, for_in_clause; 91, 92; 91, 93; 92, identifier:d; 93, identifier:reader; 94, except_clause; 94, 95; 94, 96; 95, identifier:IOError; 96, block; 96, 97; 97, pass_statement; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:user_records; 101, call; 101, 102; 101, 107; 102, attribute; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:os; 105, identifier:path; 106, identifier:join; 107, argument_list; 107, 108; 107, 109; 108, identifier:records_path; 109, binary_operator:+; 109, 110; 109, 111; 110, identifier:user_id; 111, string:'.csv'; 112, with_statement; 112, 113; 112, 123; 113, with_clause; 113, 114; 114, with_item; 114, 115; 115, as_pattern; 115, 116; 115, 121; 116, call; 116, 117; 116, 118; 117, identifier:open; 118, argument_list; 118, 119; 118, 120; 119, identifier:user_records; 120, string:'r'; 121, as_pattern_target; 121, 122; 122, identifier:csv_file; 123, block; 123, 124; 123, 133; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:reader; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:csv; 130, identifier:DictReader; 131, argument_list; 131, 132; 132, identifier:csv_file; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:records; 136, list_comprehension; 136, 137; 136, 142; 137, call; 137, 138; 137, 139; 138, identifier:_parse_record; 139, argument_list; 139, 140; 139, 141; 140, identifier:r; 141, identifier:duration_format; 142, for_in_clause; 142, 143; 142, 144; 143, identifier:r; 144, identifier:reader; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:attributes; 148, None; 149, if_statement; 149, 150; 149, 153; 150, comparison_operator:is; 150, 151; 150, 152; 151, identifier:attributes_path; 152, None; 153, block; 153, 154; 153, 168; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:user_attributes; 157, call; 157, 158; 157, 163; 158, attribute; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:os; 161, identifier:path; 162, identifier:join; 163, argument_list; 163, 164; 163, 165; 164, identifier:attributes_path; 165, binary_operator:+; 165, 166; 165, 167; 166, identifier:user_id; 167, string:'.csv'; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:attributes; 171, call; 171, 172; 171, 173; 172, identifier:_load_attributes; 173, argument_list; 173, 174; 174, identifier:user_attributes; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:recharges; 178, None; 179, if_statement; 179, 180; 179, 183; 180, comparison_operator:is; 180, 181; 180, 182; 181, identifier:recharges_path; 182, None; 183, block; 183, 184; 183, 198; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:user_recharges; 187, call; 187, 188; 187, 193; 188, attribute; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:os; 191, identifier:path; 192, identifier:join; 193, argument_list; 193, 194; 193, 195; 194, identifier:recharges_path; 195, binary_operator:+; 195, 196; 195, 197; 196, identifier:user_id; 197, string:'.csv'; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:recharges; 201, call; 201, 202; 201, 203; 202, identifier:_load_recharges; 203, argument_list; 203, 204; 204, identifier:user_recharges; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 210; 207, pattern_list; 207, 208; 207, 209; 208, identifier:user; 209, identifier:bad_records; 210, call; 210, 211; 210, 212; 211, identifier:load; 212, argument_list; 212, 213; 212, 214; 212, 215; 212, 216; 212, 217; 212, 218; 212, 219; 212, 220; 212, 221; 212, 224; 212, 227; 213, identifier:user_id; 214, identifier:records; 215, identifier:antennas; 216, identifier:attributes; 217, identifier:recharges; 218, identifier:antennas_path; 219, identifier:attributes_path; 220, identifier:recharges_path; 221, keyword_argument; 221, 222; 221, 223; 222, identifier:describe; 223, False; 224, keyword_argument; 224, 225; 224, 226; 225, identifier:warnings; 226, identifier:warnings; 227, keyword_argument; 227, 228; 227, 229; 228, identifier:drop_duplicates; 229, identifier:drop_duplicates; 230, comment; 231, if_statement; 231, 232; 231, 235; 232, comparison_operator:is; 232, 233; 232, 234; 233, identifier:network; 234, True; 235, block; 235, 236; 235, 253; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:user; 240, identifier:network; 241, call; 241, 242; 241, 243; 242, identifier:_read_network; 243, argument_list; 243, 244; 243, 245; 243, 246; 243, 247; 243, 248; 243, 249; 243, 250; 244, identifier:user; 245, identifier:records_path; 246, identifier:attributes_path; 247, identifier:read_csv; 248, identifier:antennas_path; 249, identifier:warnings; 250, keyword_argument; 250, 251; 250, 252; 251, identifier:drop_duplicates; 252, identifier:drop_duplicates; 253, expression_statement; 253, 254; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:user; 257, identifier:recompute_missing_neighbors; 258, argument_list; 259, if_statement; 259, 260; 259, 261; 260, identifier:describe; 261, block; 261, 262; 262, expression_statement; 262, 263; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:user; 266, identifier:describe; 267, argument_list; 268, if_statement; 268, 269; 268, 270; 269, identifier:errors; 270, block; 270, 271; 271, return_statement; 271, 272; 272, expression_list; 272, 273; 272, 274; 273, identifier:user; 274, identifier:bad_records; 275, return_statement; 275, 276; 276, identifier:user
def read_csv(user_id, records_path, antennas_path=None, attributes_path=None, recharges_path=None, network=False, duration_format='seconds', describe=True, warnings=True, errors=False, drop_duplicates=False): """ Load user records from a CSV file. Parameters ---------- user_id : str ID of the user (filename) records_path : str Path of the directory all the user files. antennas_path : str, optional Path of the CSV file containing (place_id, latitude, longitude) values. This allows antennas to be mapped to their locations. recharges_path : str, optional Path of the directory containing recharges files (``datetime, amount, balance, retailer_id`` CSV file). antennas_path : str, optional Path of the CSV file containing (place_id, latitude, longitude) values. This allows antennas to be mapped to their locations. network : bool, optional If network is True, bandicoot loads the network of the user's correspondants from the same path. Defaults to False. duration_format : str, default is 'seconds' Allows reading records with call duration specified in other formats than seconds. Options are 'seconds' or any format such as '%H:%M:%S', '%M%S', etc. describe : boolean If describe is True, it will print a description of the loaded user to the standard output. errors : boolean If errors is True, returns a tuple (user, errors), where user is the user object and errors are the records which could not be loaded. drop_duplicates : boolean If drop_duplicates, remove "duplicated records" (same correspondants, direction, date and time). Not activated by default. Examples -------- >>> user = bandicoot.read_csv('sample_records', '.') >>> print len(user.records) 10 >>> user = bandicoot.read_csv('sample_records', 'samples', 'sample_places.csv') >>> print len(user.antennas) 5 >>> user = bandicoot.read_csv('sample_records', '.', None, 'sample_attributes.csv') >>> print user.attributes['age'] 25 Notes ----- - The csv files can be single, or double quoted if needed. - Empty cells are filled with ``None``. For example, if the column ``call_duration`` is empty for one record, its value will be ``None``. Other values such as ``"N/A"``, ``"None"``, ``"null"`` will be considered as a text. """ antennas = None if antennas_path is not None: try: with open(antennas_path, 'r') as csv_file: reader = csv.DictReader(csv_file) antennas = dict((d['antenna_id'], (float(d['latitude']), float(d['longitude']))) for d in reader) except IOError: pass user_records = os.path.join(records_path, user_id + '.csv') with open(user_records, 'r') as csv_file: reader = csv.DictReader(csv_file) records = [_parse_record(r, duration_format) for r in reader] attributes = None if attributes_path is not None: user_attributes = os.path.join(attributes_path, user_id + '.csv') attributes = _load_attributes(user_attributes) recharges = None if recharges_path is not None: user_recharges = os.path.join(recharges_path, user_id + '.csv') recharges = _load_recharges(user_recharges) user, bad_records = load(user_id, records, antennas, attributes, recharges, antennas_path, attributes_path, recharges_path, describe=False, warnings=warnings, drop_duplicates=drop_duplicates) # Loads the network if network is True: user.network = _read_network(user, records_path, attributes_path, read_csv, antennas_path, warnings, drop_duplicates=drop_duplicates) user.recompute_missing_neighbors() if describe: user.describe() if errors: return user, bad_records return user
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:_conversations; 3, parameters; 3, 4; 3, 5; 4, identifier:group; 5, default_parameter; 5, 6; 5, 7; 6, identifier:delta; 7, call; 7, 8; 7, 11; 8, attribute; 8, 9; 8, 10; 9, identifier:datetime; 10, identifier:timedelta; 11, argument_list; 11, 12; 12, keyword_argument; 12, 13; 12, 14; 13, identifier:hours; 14, integer:1; 15, block; 15, 16; 15, 18; 15, 22; 15, 26; 15, 112; 16, expression_statement; 16, 17; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:last_time; 21, None; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:results; 25, list:[]; 26, for_statement; 26, 27; 26, 28; 26, 29; 27, identifier:g; 28, identifier:group; 29, block; 29, 30; 29, 106; 30, if_statement; 30, 31; 30, 42; 30, 75; 31, boolean_operator:or; 31, 32; 31, 35; 32, comparison_operator:is; 32, 33; 32, 34; 33, identifier:last_time; 34, None; 35, comparison_operator:<; 35, 36; 35, 41; 36, binary_operator:-; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:g; 39, identifier:datetime; 40, identifier:last_time; 41, identifier:delta; 42, block; 42, 43; 43, if_statement; 43, 44; 43, 49; 43, 57; 43, 58; 44, comparison_operator:==; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:g; 47, identifier:interaction; 48, string:'text'; 49, block; 49, 50; 50, expression_statement; 50, 51; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:results; 54, identifier:append; 55, argument_list; 55, 56; 56, identifier:g; 57, comment; 58, else_clause; 58, 59; 59, block; 59, 60; 60, if_statement; 60, 61; 60, 67; 61, comparison_operator:!=; 61, 62; 61, 66; 62, call; 62, 63; 62, 64; 63, identifier:len; 64, argument_list; 64, 65; 65, identifier:results; 66, integer:0; 67, block; 67, 68; 67, 71; 68, expression_statement; 68, 69; 69, yield; 69, 70; 70, identifier:results; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:results; 74, list:[]; 75, else_clause; 75, 76; 76, block; 76, 77; 76, 88; 77, if_statement; 77, 78; 77, 84; 78, comparison_operator:!=; 78, 79; 78, 83; 79, call; 79, 80; 79, 81; 80, identifier:len; 81, argument_list; 81, 82; 82, identifier:results; 83, integer:0; 84, block; 84, 85; 85, expression_statement; 85, 86; 86, yield; 86, 87; 87, identifier:results; 88, if_statement; 88, 89; 88, 94; 88, 99; 89, comparison_operator:==; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:g; 92, identifier:interaction; 93, string:'call'; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:results; 98, list:[]; 99, else_clause; 99, 100; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:results; 104, list:[g]; 104, 105; 105, identifier:g; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:last_time; 109, attribute; 109, 110; 109, 111; 110, identifier:g; 111, identifier:datetime; 112, if_statement; 112, 113; 112, 119; 113, comparison_operator:!=; 113, 114; 113, 118; 114, call; 114, 115; 114, 116; 115, identifier:len; 116, argument_list; 116, 117; 117, identifier:results; 118, integer:0; 119, block; 119, 120; 120, expression_statement; 120, 121; 121, yield; 121, 122; 122, identifier:results
def _conversations(group, delta=datetime.timedelta(hours=1)): """ Group texts into conversations. The function returns an iterator over records grouped by conversations. See :ref:`Using bandicoot <conversations-label>` for a definition of conversations. A conversation begins when one person sends a text-message to the other and ends when one of them makes a phone call or there is no activity between them for an hour. """ last_time = None results = [] for g in group: if last_time is None or g.datetime - last_time < delta: if g.interaction == 'text': results.append(g) # A call always ends a conversation else: if len(results) != 0: yield results results = [] else: if len(results) != 0: yield results if g.interaction == 'call': results = [] else: results = [g] last_time = g.datetime if len(results) != 0: yield results
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 2, function_name:fetch; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 4, identifier:cert; 5, identifier:issuer; 6, default_parameter; 6, 7; 6, 8; 7, identifier:hash_algo; 8, string:'sha1'; 9, default_parameter; 9, 10; 9, 11; 10, identifier:nonce; 11, True; 12, default_parameter; 12, 13; 12, 14; 13, identifier:user_agent; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:timeout; 17, integer:10; 18, block; 18, 19; 18, 21; 18, 41; 18, 61; 18, 81; 18, 99; 18, 128; 18, 171; 18, 183; 18, 201; 18, 253; 18, 265; 18, 269; 18, 381; 19, expression_statement; 19, 20; 20, comment; 21, if_statement; 21, 22; 21, 30; 22, not_operator; 22, 23; 23, call; 23, 24; 23, 25; 24, identifier:isinstance; 25, argument_list; 25, 26; 25, 27; 26, identifier:cert; 27, attribute; 27, 28; 27, 29; 28, identifier:x509; 29, identifier:Certificate; 30, block; 30, 31; 31, raise_statement; 31, 32; 32, call; 32, 33; 32, 34; 33, identifier:TypeError; 34, argument_list; 34, 35; 35, binary_operator:%; 35, 36; 35, 37; 36, string:'cert must be an instance of asn1crypto.x509.Certificate, not %s'; 37, call; 37, 38; 37, 39; 38, identifier:type_name; 39, argument_list; 39, 40; 40, identifier:cert; 41, if_statement; 41, 42; 41, 50; 42, not_operator; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:isinstance; 45, argument_list; 45, 46; 45, 47; 46, identifier:issuer; 47, attribute; 47, 48; 47, 49; 48, identifier:x509; 49, identifier:Certificate; 50, block; 50, 51; 51, raise_statement; 51, 52; 52, call; 52, 53; 52, 54; 53, identifier:TypeError; 54, argument_list; 54, 55; 55, binary_operator:%; 55, 56; 55, 57; 56, string:'issuer must be an instance of asn1crypto.x509.Certificate, not %s'; 57, call; 57, 58; 57, 59; 58, identifier:type_name; 59, argument_list; 59, 60; 60, identifier:issuer; 61, if_statement; 61, 62; 61, 70; 62, comparison_operator:not; 62, 63; 62, 64; 63, identifier:hash_algo; 64, call; 64, 65; 64, 66; 65, identifier:set; 66, argument_list; 66, 67; 67, list:['sha1', 'sha256']; 67, 68; 67, 69; 68, string:'sha1'; 69, string:'sha256'; 70, block; 70, 71; 71, raise_statement; 71, 72; 72, call; 72, 73; 72, 74; 73, identifier:ValueError; 74, argument_list; 74, 75; 75, binary_operator:%; 75, 76; 75, 77; 76, string:'hash_algo must be one of "sha1", "sha256", not %s'; 77, call; 77, 78; 77, 79; 78, identifier:repr; 79, argument_list; 79, 80; 80, identifier:hash_algo; 81, if_statement; 81, 82; 81, 88; 82, not_operator; 82, 83; 83, call; 83, 84; 83, 85; 84, identifier:isinstance; 85, argument_list; 85, 86; 85, 87; 86, identifier:nonce; 87, identifier:bool; 88, block; 88, 89; 89, raise_statement; 89, 90; 90, call; 90, 91; 90, 92; 91, identifier:TypeError; 92, argument_list; 92, 93; 93, binary_operator:%; 93, 94; 93, 95; 94, string:'nonce must be a bool, not %s'; 95, call; 95, 96; 95, 97; 96, identifier:type_name; 97, argument_list; 97, 98; 98, identifier:nonce; 99, if_statement; 99, 100; 99, 103; 99, 110; 100, comparison_operator:is; 100, 101; 100, 102; 101, identifier:user_agent; 102, None; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:user_agent; 107, binary_operator:%; 107, 108; 107, 109; 108, string:'certvalidator %s'; 109, identifier:__version__; 110, elif_clause; 110, 111; 110, 117; 111, not_operator; 111, 112; 112, call; 112, 113; 112, 114; 113, identifier:isinstance; 114, argument_list; 114, 115; 114, 116; 115, identifier:user_agent; 116, identifier:str_cls; 117, block; 117, 118; 118, raise_statement; 118, 119; 119, call; 119, 120; 119, 121; 120, identifier:TypeError; 121, argument_list; 121, 122; 122, binary_operator:%; 122, 123; 122, 124; 123, string:'user_agent must be a unicode string, not %s'; 124, call; 124, 125; 124, 126; 125, identifier:type_name; 126, argument_list; 126, 127; 127, identifier:user_agent; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:cert_id; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:ocsp; 134, identifier:CertId; 135, argument_list; 135, 136; 136, dictionary; 136, 137; 136, 148; 136, 157; 136, 166; 137, pair; 137, 138; 137, 139; 138, string:'hash_algorithm'; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:algos; 142, identifier:DigestAlgorithm; 143, argument_list; 143, 144; 144, dictionary; 144, 145; 145, pair; 145, 146; 145, 147; 146, string:'algorithm'; 147, identifier:hash_algo; 148, pair; 148, 149; 148, 150; 149, string:'issuer_name_hash'; 150, call; 150, 151; 150, 152; 151, identifier:getattr; 152, argument_list; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:cert; 155, identifier:issuer; 156, identifier:hash_algo; 157, pair; 157, 158; 157, 159; 158, string:'issuer_key_hash'; 159, call; 159, 160; 159, 161; 160, identifier:getattr; 161, argument_list; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:issuer; 164, identifier:public_key; 165, identifier:hash_algo; 166, pair; 166, 167; 166, 168; 167, string:'serial_number'; 168, attribute; 168, 169; 168, 170; 169, identifier:cert; 170, identifier:serial_number; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:request; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:ocsp; 177, identifier:Request; 178, argument_list; 178, 179; 179, dictionary; 179, 180; 180, pair; 180, 181; 180, 182; 181, string:'req_cert'; 182, identifier:cert_id; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:tbs_request; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:ocsp; 189, identifier:TBSRequest; 190, argument_list; 190, 191; 191, dictionary; 191, 192; 192, pair; 192, 193; 192, 194; 193, string:'request_list'; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:ocsp; 197, identifier:Requests; 198, argument_list; 198, 199; 199, list:[request]; 199, 200; 200, identifier:request; 201, if_statement; 201, 202; 201, 203; 202, identifier:nonce; 203, block; 203, 204; 203, 241; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:nonce_extension; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:ocsp; 210, identifier:TBSRequestExtension; 211, argument_list; 211, 212; 212, dictionary; 212, 213; 212, 216; 212, 219; 213, pair; 213, 214; 213, 215; 214, string:'extn_id'; 215, string:'nonce'; 216, pair; 216, 217; 216, 218; 217, string:'critical'; 218, False; 219, pair; 219, 220; 219, 221; 220, string:'extn_value'; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:core; 224, identifier:OctetString; 225, argument_list; 225, 226; 226, call; 226, 227; 226, 240; 227, attribute; 227, 228; 227, 239; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:core; 231, identifier:OctetString; 232, argument_list; 232, 233; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:os; 236, identifier:urandom; 237, argument_list; 237, 238; 238, integer:16; 239, identifier:dump; 240, argument_list; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 246; 243, subscript; 243, 244; 243, 245; 244, identifier:tbs_request; 245, string:'request_extensions'; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:ocsp; 249, identifier:TBSRequestExtensions; 250, argument_list; 250, 251; 251, list:[nonce_extension]; 251, 252; 252, identifier:nonce_extension; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 256; 255, identifier:ocsp_request; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:ocsp; 259, identifier:OCSPRequest; 260, argument_list; 260, 261; 261, dictionary; 261, 262; 262, pair; 262, 263; 262, 264; 263, string:'tbs_request'; 264, identifier:tbs_request; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 268; 267, identifier:last_e; 268, None; 269, for_statement; 269, 270; 269, 271; 269, 274; 270, identifier:ocsp_url; 271, attribute; 271, 272; 271, 273; 272, identifier:cert; 273, identifier:ocsp_urls; 274, block; 274, 275; 275, try_statement; 275, 276; 275, 370; 276, block; 276, 277; 276, 284; 276, 292; 276, 300; 276, 308; 276, 321; 276, 336; 276, 342; 276, 348; 276, 368; 277, expression_statement; 277, 278; 278, assignment; 278, 279; 278, 280; 279, identifier:request; 280, call; 280, 281; 280, 282; 281, identifier:Request; 282, argument_list; 282, 283; 283, identifier:ocsp_url; 284, expression_statement; 284, 285; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:request; 288, identifier:add_header; 289, argument_list; 289, 290; 289, 291; 290, string:'Accept'; 291, string:'application/ocsp-response'; 292, expression_statement; 292, 293; 293, call; 293, 294; 293, 297; 294, attribute; 294, 295; 294, 296; 295, identifier:request; 296, identifier:add_header; 297, argument_list; 297, 298; 297, 299; 298, string:'Content-Type'; 299, string:'application/ocsp-request'; 300, expression_statement; 300, 301; 301, call; 301, 302; 301, 305; 302, attribute; 302, 303; 302, 304; 303, identifier:request; 304, identifier:add_header; 305, argument_list; 305, 306; 305, 307; 306, string:'User-Agent'; 307, identifier:user_agent; 308, expression_statement; 308, 309; 309, assignment; 309, 310; 309, 311; 310, identifier:response; 311, call; 311, 312; 311, 313; 312, identifier:urlopen; 313, argument_list; 313, 314; 313, 315; 313, 320; 314, identifier:request; 315, call; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:ocsp_request; 318, identifier:dump; 319, argument_list; 320, identifier:timeout; 321, expression_statement; 321, 322; 322, assignment; 322, 323; 322, 324; 323, identifier:ocsp_response; 324, call; 324, 325; 324, 330; 325, attribute; 325, 326; 325, 329; 326, attribute; 326, 327; 326, 328; 327, identifier:ocsp; 328, identifier:OCSPResponse; 329, identifier:load; 330, argument_list; 330, 331; 331, call; 331, 332; 331, 335; 332, attribute; 332, 333; 332, 334; 333, identifier:response; 334, identifier:read; 335, argument_list; 336, expression_statement; 336, 337; 337, assignment; 337, 338; 337, 339; 338, identifier:request_nonce; 339, attribute; 339, 340; 339, 341; 340, identifier:ocsp_request; 341, identifier:nonce_value; 342, expression_statement; 342, 343; 343, assignment; 343, 344; 343, 345; 344, identifier:response_nonce; 345, attribute; 345, 346; 345, 347; 346, identifier:ocsp_response; 347, identifier:nonce_value; 348, if_statement; 348, 349; 348, 360; 349, boolean_operator:and; 349, 350; 349, 353; 350, boolean_operator:and; 350, 351; 350, 352; 351, identifier:request_nonce; 352, identifier:response_nonce; 353, comparison_operator:!=; 353, 354; 353, 357; 354, attribute; 354, 355; 354, 356; 355, identifier:request_nonce; 356, identifier:native; 357, attribute; 357, 358; 357, 359; 358, identifier:response_nonce; 359, identifier:native; 360, block; 360, 361; 361, raise_statement; 361, 362; 362, call; 362, 363; 362, 366; 363, attribute; 363, 364; 363, 365; 364, identifier:errors; 365, identifier:OCSPValidationError; 366, argument_list; 366, 367; 367, string:'Unable to verify OCSP response since the request and response nonces do not match'; 368, return_statement; 368, 369; 369, identifier:ocsp_response; 370, except_clause; 370, 371; 370, 376; 371, as_pattern; 371, 372; 371, 374; 372, parenthesized_expression; 372, 373; 373, identifier:URLError; 374, as_pattern_target; 374, 375; 375, identifier:e; 376, block; 376, 377; 377, expression_statement; 377, 378; 378, assignment; 378, 379; 378, 380; 379, identifier:last_e; 380, identifier:e; 381, raise_statement; 381, 382; 382, identifier:last_e
def fetch(cert, issuer, hash_algo='sha1', nonce=True, user_agent=None, timeout=10): """ Fetches an OCSP response for a certificate :param cert: An asn1cyrpto.x509.Certificate object to get an OCSP reponse for :param issuer: An asn1crypto.x509.Certificate object that is the issuer of cert :param hash_algo: A unicode string of "sha1" or "sha256" :param nonce: A boolean - if the nonce extension should be used to prevent replay attacks :param user_agent: The HTTP user agent to use when requesting the OCSP response. If None, a default is used in the format "certvalidation 1.0.0". :param timeout: The number of seconds after which an HTTP request should timeout :raises: urllib.error.URLError/urllib2.URLError - when a URL/HTTP error occurs socket.error - when a socket error occurs :return: An asn1crypto.ocsp.OCSPResponse object """ if not isinstance(cert, x509.Certificate): raise TypeError('cert must be an instance of asn1crypto.x509.Certificate, not %s' % type_name(cert)) if not isinstance(issuer, x509.Certificate): raise TypeError('issuer must be an instance of asn1crypto.x509.Certificate, not %s' % type_name(issuer)) if hash_algo not in set(['sha1', 'sha256']): raise ValueError('hash_algo must be one of "sha1", "sha256", not %s' % repr(hash_algo)) if not isinstance(nonce, bool): raise TypeError('nonce must be a bool, not %s' % type_name(nonce)) if user_agent is None: user_agent = 'certvalidator %s' % __version__ elif not isinstance(user_agent, str_cls): raise TypeError('user_agent must be a unicode string, not %s' % type_name(user_agent)) cert_id = ocsp.CertId({ 'hash_algorithm': algos.DigestAlgorithm({'algorithm': hash_algo}), 'issuer_name_hash': getattr(cert.issuer, hash_algo), 'issuer_key_hash': getattr(issuer.public_key, hash_algo), 'serial_number': cert.serial_number, }) request = ocsp.Request({ 'req_cert': cert_id, }) tbs_request = ocsp.TBSRequest({ 'request_list': ocsp.Requests([request]), }) if nonce: nonce_extension = ocsp.TBSRequestExtension({ 'extn_id': 'nonce', 'critical': False, 'extn_value': core.OctetString(core.OctetString(os.urandom(16)).dump()) }) tbs_request['request_extensions'] = ocsp.TBSRequestExtensions([nonce_extension]) ocsp_request = ocsp.OCSPRequest({ 'tbs_request': tbs_request, }) last_e = None for ocsp_url in cert.ocsp_urls: try: request = Request(ocsp_url) request.add_header('Accept', 'application/ocsp-response') request.add_header('Content-Type', 'application/ocsp-request') request.add_header('User-Agent', user_agent) response = urlopen(request, ocsp_request.dump(), timeout) ocsp_response = ocsp.OCSPResponse.load(response.read()) request_nonce = ocsp_request.nonce_value response_nonce = ocsp_response.nonce_value if request_nonce and response_nonce and request_nonce.native != response_nonce.native: raise errors.OCSPValidationError( 'Unable to verify OCSP response since the request and response nonces do not match' ) return ocsp_response except (URLError) as e: last_e = e raise last_e