sequence
stringlengths
546
16.2k
code
stringlengths
108
19.3k
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_find_cert_in_list; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:cert; 5, identifier:issuer; 6, identifier:certificate_list; 7, identifier:crl_issuer; 8, block; 8, 9; 8, 11; 8, 19; 8, 25; 8, 31; 8, 42; 8, 48; 8, 126; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:revoked_certificates; 14, subscript; 14, 15; 14, 18; 15, subscript; 15, 16; 15, 17; 16, identifier:certificate_list; 17, string:'tbs_cert_list'; 18, string:'revoked_certificates'; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:cert_serial; 22, attribute; 22, 23; 22, 24; 23, identifier:cert; 24, identifier:serial_number; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:issuer_name; 28, attribute; 28, 29; 28, 30; 29, identifier:issuer; 30, identifier:subject; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:known_extensions; 34, call; 34, 35; 34, 36; 35, identifier:set; 36, argument_list; 36, 37; 37, list:[ 'crl_reason', 'hold_instruction_code', 'invalidity_date', 'certificate_issuer' ]; 37, 38; 37, 39; 37, 40; 37, 41; 38, string:'crl_reason'; 39, string:'hold_instruction_code'; 40, string:'invalidity_date'; 41, string:'certificate_issuer'; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:last_issuer_name; 45, attribute; 45, 46; 45, 47; 46, identifier:crl_issuer; 47, identifier:subject; 48, for_statement; 48, 49; 48, 50; 48, 51; 48, 52; 49, identifier:revoked_cert; 50, identifier:revoked_certificates; 51, comment; 52, block; 52, 53; 52, 64; 52, 81; 52, 87; 52, 97; 52, 120; 53, if_statement; 53, 54; 53, 59; 54, binary_operator:-; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:revoked_cert; 57, identifier:critical_extensions; 58, identifier:known_extensions; 59, block; 59, 60; 60, raise_statement; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:NotImplementedError; 63, argument_list; 64, if_statement; 64, 65; 64, 74; 65, boolean_operator:and; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:revoked_cert; 68, identifier:issuer_name; 69, comparison_operator:!=; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:revoked_cert; 72, identifier:issuer_name; 73, identifier:last_issuer_name; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:last_issuer_name; 78, attribute; 78, 79; 78, 80; 79, identifier:revoked_cert; 80, identifier:issuer_name; 81, if_statement; 81, 82; 81, 85; 82, comparison_operator:!=; 82, 83; 82, 84; 83, identifier:last_issuer_name; 84, identifier:issuer_name; 85, block; 85, 86; 86, continue_statement; 87, if_statement; 87, 88; 87, 95; 88, comparison_operator:!=; 88, 89; 88, 94; 89, attribute; 89, 90; 89, 93; 90, subscript; 90, 91; 90, 92; 91, identifier:revoked_cert; 92, string:'user_certificate'; 93, identifier:native; 94, identifier:cert_serial; 95, block; 95, 96; 96, continue_statement; 97, if_statement; 97, 98; 97, 102; 97, 112; 98, not_operator; 98, 99; 99, attribute; 99, 100; 99, 101; 100, identifier:revoked_cert; 101, identifier:crl_reason_value; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:crl_reason; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:crl; 109, identifier:CRLReason; 110, argument_list; 110, 111; 111, string:'unspecified'; 112, else_clause; 112, 113; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:crl_reason; 117, attribute; 117, 118; 117, 119; 118, identifier:revoked_cert; 119, identifier:crl_reason_value; 120, return_statement; 120, 121; 121, tuple; 121, 122; 121, 125; 122, subscript; 122, 123; 122, 124; 123, identifier:revoked_cert; 124, string:'revocation_date'; 125, identifier:crl_reason; 126, return_statement; 126, 127; 127, tuple; 127, 128; 127, 129; 128, None; 129, None
def _find_cert_in_list(cert, issuer, certificate_list, crl_issuer): """ Looks for a cert in the list of revoked certificates :param cert: An asn1crypto.x509.Certificate object of the cert being checked :param issuer: An asn1crypto.x509.Certificate object of the cert issuer :param certificate_list: An ans1crypto.crl.CertificateList object to look in for the cert :param crl_issuer: An asn1crypto.x509.Certificate object of the CRL issuer :return: A tuple of (None, None) if not present, otherwise a tuple of (asn1crypto.x509.Time object, asn1crypto.crl.CRLReason object) representing the date/time the object was revoked and why """ revoked_certificates = certificate_list['tbs_cert_list']['revoked_certificates'] cert_serial = cert.serial_number issuer_name = issuer.subject known_extensions = set([ 'crl_reason', 'hold_instruction_code', 'invalidity_date', 'certificate_issuer' ]) last_issuer_name = crl_issuer.subject for revoked_cert in revoked_certificates: # If any unknown critical extensions, the entry can not be used if revoked_cert.critical_extensions - known_extensions: raise NotImplementedError() if revoked_cert.issuer_name and revoked_cert.issuer_name != last_issuer_name: last_issuer_name = revoked_cert.issuer_name if last_issuer_name != issuer_name: continue if revoked_cert['user_certificate'].native != cert_serial: continue if not revoked_cert.crl_reason_value: crl_reason = crl.CRLReason('unspecified') else: crl_reason = revoked_cert.crl_reason_value return (revoked_cert['revocation_date'], crl_reason) return (None, None)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:derive_coordinates; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 15; 5, 72; 5, 76; 5, 80; 5, 84; 5, 85; 5, 113; 5, 295; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 12; 8, 13; 9, attribute; 9, 10; 9, 11; 10, identifier:self; 11, identifier:_coordinates_resolved; 12, comment; 13, block; 13, 14; 14, return_statement; 15, if_statement; 15, 16; 15, 21; 15, 22; 15, 64; 16, comparison_operator:is; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:seg_map; 20, None; 21, comment; 22, block; 22, 23; 22, 43; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 33; 25, pattern_list; 25, 26; 25, 29; 25, 32; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:start; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:filename; 32, identifier:include_ref; 33, call; 33, 34; 33, 39; 34, attribute; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:seg_map; 38, identifier:derive_source_offset; 39, argument_list; 39, 40; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:start; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 51; 45, pattern_list; 45, 46; 45, 49; 45, 50; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:end; 49, identifier:end_filename; 50, identifier:_; 51, call; 51, 52; 51, 57; 52, attribute; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:self; 55, identifier:seg_map; 56, identifier:derive_source_offset; 57, argument_list; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:end; 61, keyword_argument; 61, 62; 61, 63; 62, identifier:is_end; 63, True; 64, else_clause; 64, 65; 65, block; 65, 66; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:end_filename; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:filename; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:line_start; 75, integer:0; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:lineno; 79, integer:1; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:file_pos; 83, integer:0; 84, comment; 85, if_statement; 85, 86; 85, 91; 85, 96; 85, 107; 86, comparison_operator:!=; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:filename; 90, identifier:end_filename; 91, block; 91, 92; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:get_end; 95, False; 96, elif_clause; 96, 97; 96, 102; 97, comparison_operator:is; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:end; 101, None; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:get_end; 106, False; 107, else_clause; 107, 108; 108, block; 108, 109; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:get_end; 112, True; 113, if_statement; 113, 114; 113, 127; 114, boolean_operator:and; 114, 115; 114, 121; 115, parenthesized_expression; 115, 116; 116, comparison_operator:is; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:filename; 120, None; 121, parenthesized_expression; 121, 122; 122, comparison_operator:is; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:start; 126, None; 127, block; 127, 128; 127, 266; 127, 267; 128, with_statement; 128, 129; 128, 147; 129, with_clause; 129, 130; 130, with_item; 130, 131; 131, as_pattern; 131, 132; 131, 145; 132, call; 132, 133; 132, 134; 133, identifier:open; 134, argument_list; 134, 135; 134, 138; 134, 139; 134, 142; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:filename; 138, string:'r'; 139, keyword_argument; 139, 140; 139, 141; 140, identifier:newline; 141, string:''; 142, keyword_argument; 142, 143; 142, 144; 143, identifier:encoding; 144, string:'utf_8'; 145, as_pattern_target; 145, 146; 146, identifier:fp; 147, block; 147, 148; 148, while_statement; 148, 149; 148, 150; 149, True; 150, block; 150, 151; 150, 159; 150, 166; 150, 172; 150, 224; 150, 258; 150, 262; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:line_text; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:fp; 157, identifier:readline; 158, argument_list; 159, expression_statement; 159, 160; 160, augmented_assignment:+=; 160, 161; 160, 162; 161, identifier:file_pos; 162, call; 162, 163; 162, 164; 163, identifier:len; 164, argument_list; 164, 165; 165, identifier:line_text; 166, if_statement; 166, 167; 166, 170; 167, comparison_operator:==; 167, 168; 167, 169; 168, identifier:line_text; 169, string:""; 170, block; 170, 171; 171, break_statement; 172, if_statement; 172, 173; 172, 186; 173, boolean_operator:and; 173, 174; 173, 180; 174, parenthesized_expression; 174, 175; 175, comparison_operator:is; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:self; 178, identifier:start_line; 179, None; 180, parenthesized_expression; 180, 181; 181, comparison_operator:<; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:self; 184, identifier:start; 185, identifier:file_pos; 186, block; 186, 187; 186, 193; 186, 203; 186, 219; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:self; 191, identifier:start_line; 192, identifier:lineno; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:self; 197, identifier:start_col; 198, binary_operator:-; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:self; 201, identifier:start; 202, identifier:line_start; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:self; 207, identifier:start_line_text; 208, call; 208, 209; 208, 217; 209, attribute; 209, 210; 209, 216; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:line_text; 213, identifier:rstrip; 214, argument_list; 214, 215; 215, string:"\n"; 216, identifier:rstrip; 217, argument_list; 217, 218; 218, string:"\r"; 219, if_statement; 219, 220; 219, 222; 220, not_operator; 220, 221; 221, identifier:get_end; 222, block; 222, 223; 223, break_statement; 224, if_statement; 224, 225; 224, 240; 225, boolean_operator:and; 225, 226; 225, 234; 226, boolean_operator:and; 226, 227; 226, 228; 227, identifier:get_end; 228, parenthesized_expression; 228, 229; 229, comparison_operator:is; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:self; 232, identifier:end_line; 233, None; 234, parenthesized_expression; 234, 235; 235, comparison_operator:<; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:self; 238, identifier:end; 239, identifier:file_pos; 240, block; 240, 241; 240, 247; 240, 257; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:self; 245, identifier:end_line; 246, identifier:lineno; 247, expression_statement; 247, 248; 248, assignment; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:self; 251, identifier:end_col; 252, binary_operator:-; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:self; 255, identifier:end; 256, identifier:line_start; 257, break_statement; 258, expression_statement; 258, 259; 259, augmented_assignment:+=; 259, 260; 259, 261; 260, identifier:lineno; 261, integer:1; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 265; 264, identifier:line_start; 265, identifier:file_pos; 266, comment; 267, if_statement; 267, 268; 267, 270; 268, not_operator; 268, 269; 269, identifier:get_end; 270, block; 270, 271; 270, 279; 270, 287; 271, expression_statement; 271, 272; 272, assignment; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:self; 275, identifier:end_line; 276, attribute; 276, 277; 276, 278; 277, identifier:self; 278, identifier:start_line; 279, expression_statement; 279, 280; 280, assignment; 280, 281; 280, 284; 281, attribute; 281, 282; 281, 283; 282, identifier:self; 283, identifier:end_col; 284, attribute; 284, 285; 284, 286; 285, identifier:self; 286, identifier:start_col; 287, expression_statement; 287, 288; 288, assignment; 288, 289; 288, 292; 289, attribute; 289, 290; 289, 291; 290, identifier:self; 291, identifier:end; 292, attribute; 292, 293; 292, 294; 293, identifier:self; 294, identifier:start; 295, expression_statement; 295, 296; 296, assignment; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:self; 299, identifier:_coordinates_resolved; 300, True
def derive_coordinates(self): """ Depending on the compilation source, some members of the SourceRef object may be incomplete. Calling this function performs the necessary derivations to complete the object. """ if self._coordinates_resolved: # Coordinates were already resolved. Skip return if self.seg_map is not None: # Translate coordinates self.start, self.filename, include_ref = self.seg_map.derive_source_offset(self.start) self.end, end_filename, _ = self.seg_map.derive_source_offset(self.end, is_end=True) else: end_filename = self.filename line_start = 0 lineno = 1 file_pos = 0 # Skip deriving end coordinate if selection spans multiple files if self.filename != end_filename: get_end = False elif self.end is None: get_end = False else: get_end = True if (self.filename is not None) and (self.start is not None): with open(self.filename, 'r', newline='', encoding='utf_8') as fp: while True: line_text = fp.readline() file_pos += len(line_text) if line_text == "": break if (self.start_line is None) and (self.start < file_pos): self.start_line = lineno self.start_col = self.start - line_start self.start_line_text = line_text.rstrip("\n").rstrip("\r") if not get_end: break if get_end and (self.end_line is None) and (self.end < file_pos): self.end_line = lineno self.end_col = self.end - line_start break lineno += 1 line_start = file_pos # If no end coordinate was derived, just do a single char selection if not get_end: self.end_line = self.start_line self.end_col = self.start_col self.end = self.start self._coordinates_resolved = True
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:children; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:unroll; 7, False; 8, default_parameter; 8, 9; 8, 10; 9, identifier:skip_not_present; 10, True; 11, block; 11, 12; 11, 14; 12, expression_statement; 12, 13; 13, comment; 14, for_statement; 14, 15; 14, 16; 14, 21; 15, identifier:child_inst; 16, attribute; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:inst; 20, identifier:children; 21, block; 21, 22; 21, 40; 22, if_statement; 22, 23; 22, 24; 22, 25; 23, identifier:skip_not_present; 24, comment; 25, block; 25, 26; 26, if_statement; 26, 27; 26, 37; 26, 38; 27, not_operator; 27, 28; 28, call; 28, 29; 28, 34; 29, attribute; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:child_inst; 32, identifier:properties; 33, identifier:get; 34, argument_list; 34, 35; 34, 36; 35, string:'ispresent'; 36, True; 37, comment; 38, block; 38, 39; 39, continue_statement; 40, if_statement; 40, 41; 40, 54; 40, 55; 40, 102; 41, boolean_operator:and; 41, 42; 41, 51; 42, boolean_operator:and; 42, 43; 42, 44; 43, identifier:unroll; 44, call; 44, 45; 44, 46; 45, identifier:isinstance; 46, argument_list; 46, 47; 46, 48; 47, identifier:child_inst; 48, attribute; 48, 49; 48, 50; 49, identifier:comp; 50, identifier:AddressableComponent; 51, attribute; 51, 52; 51, 53; 52, identifier:child_inst; 53, identifier:is_array; 54, comment; 55, block; 55, 56; 55, 69; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:range_list; 59, list_comprehension; 59, 60; 59, 64; 60, call; 60, 61; 60, 62; 61, identifier:range; 62, argument_list; 62, 63; 63, identifier:n; 64, for_in_clause; 64, 65; 64, 66; 65, identifier:n; 66, attribute; 66, 67; 66, 68; 67, identifier:child_inst; 68, identifier:array_dimensions; 69, for_statement; 69, 70; 69, 71; 69, 78; 70, identifier:idxs; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:itertools; 74, identifier:product; 75, argument_list; 75, 76; 76, list_splat; 76, 77; 77, identifier:range_list; 78, block; 78, 79; 78, 92; 78, 98; 78, 99; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:N; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:Node; 85, identifier:_factory; 86, argument_list; 86, 87; 86, 88; 86, 91; 87, identifier:child_inst; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:env; 91, identifier:self; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:N; 96, identifier:current_idx; 97, identifier:idxs; 98, comment; 99, expression_statement; 99, 100; 100, yield; 100, 101; 101, identifier:N; 102, else_clause; 102, 103; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, yield; 105, 106; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:Node; 109, identifier:_factory; 110, argument_list; 110, 111; 110, 112; 110, 115; 111, identifier:child_inst; 112, attribute; 112, 113; 112, 114; 113, identifier:self; 114, identifier:env; 115, identifier:self
def children(self, unroll=False, skip_not_present=True): """ Returns an iterator that provides nodes for all immediate children of this component. Parameters ---------- unroll : bool If True, any children that are arrays are unrolled. skip_not_present : bool If True, skips children whose 'ispresent' property is set to False Yields ------ :class:`~Node` All immediate children """ for child_inst in self.inst.children: if skip_not_present: # Check if property ispresent == False if not child_inst.properties.get('ispresent', True): # ispresent was explicitly set to False. Skip it continue if unroll and isinstance(child_inst, comp.AddressableComponent) and child_inst.is_array: # Unroll the array range_list = [range(n) for n in child_inst.array_dimensions] for idxs in itertools.product(*range_list): N = Node._factory(child_inst, self.env, self) N.current_idx = idxs # pylint: disable=attribute-defined-outside-init yield N else: yield Node._factory(child_inst, self.env, self)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:find_by_path; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:path; 6, block; 6, 7; 6, 9; 6, 18; 6, 22; 6, 177; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:pathparts; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:path; 15, identifier:split; 16, argument_list; 16, 17; 17, string:'.'; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:current_node; 21, identifier:self; 22, for_statement; 22, 23; 22, 24; 22, 25; 23, identifier:pathpart; 24, identifier:pathparts; 25, block; 25, 26; 25, 36; 25, 45; 25, 57; 25, 75; 25, 84; 25, 91; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:m; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:re; 32, identifier:fullmatch; 33, argument_list; 33, 34; 33, 35; 34, string:r'^(\w+)((?:\[(?:\d+|0[xX][\da-fA-F]+)\])*)$'; 35, identifier:pathpart; 36, if_statement; 36, 37; 36, 39; 37, not_operator; 37, 38; 38, identifier:m; 39, block; 39, 40; 40, raise_statement; 40, 41; 41, call; 41, 42; 41, 43; 42, identifier:ValueError; 43, argument_list; 43, 44; 44, string:"Invalid path"; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 50; 47, pattern_list; 47, 48; 47, 49; 48, identifier:inst_name; 49, identifier:array_suffix; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:m; 53, identifier:group; 54, argument_list; 54, 55; 54, 56; 55, integer:1; 56, integer:2; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:idx_list; 60, list_comprehension; 60, 61; 60, 66; 61, call; 61, 62; 61, 63; 62, identifier:int; 63, argument_list; 63, 64; 63, 65; 64, identifier:s; 65, integer:0; 66, for_in_clause; 66, 67; 66, 68; 67, identifier:s; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:re; 71, identifier:findall; 72, argument_list; 72, 73; 72, 74; 73, string:r'\[(\d+|0[xX][\da-fA-F]+)\]'; 74, identifier:array_suffix; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:current_node; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:current_node; 81, identifier:get_child_by_name; 82, argument_list; 82, 83; 83, identifier:inst_name; 84, if_statement; 84, 85; 84, 88; 85, comparison_operator:is; 85, 86; 85, 87; 86, identifier:current_node; 87, None; 88, block; 88, 89; 89, return_statement; 89, 90; 90, None; 91, if_statement; 91, 92; 91, 93; 92, identifier:idx_list; 93, block; 93, 94; 94, if_statement; 94, 95; 94, 107; 94, 108; 94, 170; 95, boolean_operator:and; 95, 96; 95, 102; 96, parenthesized_expression; 96, 97; 97, call; 97, 98; 97, 99; 98, identifier:isinstance; 99, argument_list; 99, 100; 99, 101; 100, identifier:current_node; 101, identifier:AddressableNode; 102, attribute; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:current_node; 105, identifier:inst; 106, identifier:is_array; 107, comment; 108, block; 108, 109; 108, 129; 108, 135; 108, 136; 109, if_statement; 109, 110; 109, 123; 110, comparison_operator:!=; 110, 111; 110, 115; 111, call; 111, 112; 111, 113; 112, identifier:len; 113, argument_list; 113, 114; 114, identifier:idx_list; 115, call; 115, 116; 115, 117; 116, identifier:len; 117, argument_list; 117, 118; 118, attribute; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:current_node; 121, identifier:inst; 122, identifier:array_dimensions; 123, block; 123, 124; 124, raise_statement; 124, 125; 125, call; 125, 126; 125, 127; 126, identifier:IndexError; 127, argument_list; 127, 128; 128, string:"Wrong number of array dimensions"; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:current_node; 133, identifier:current_idx; 134, list:[]; 135, comment; 136, for_statement; 136, 137; 136, 140; 136, 144; 137, pattern_list; 137, 138; 137, 139; 138, identifier:i; 139, identifier:idx; 140, call; 140, 141; 140, 142; 141, identifier:enumerate; 142, argument_list; 142, 143; 143, identifier:idx_list; 144, block; 144, 145; 144, 161; 145, if_statement; 145, 146; 145, 155; 146, comparison_operator:>=; 146, 147; 146, 148; 147, identifier:idx; 148, subscript; 148, 149; 148, 154; 149, attribute; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:current_node; 152, identifier:inst; 153, identifier:array_dimensions; 154, identifier:i; 155, block; 155, 156; 156, raise_statement; 156, 157; 157, call; 157, 158; 157, 159; 158, identifier:IndexError; 159, argument_list; 159, 160; 160, string:"Array index out of range"; 161, expression_statement; 161, 162; 162, call; 162, 163; 162, 168; 163, attribute; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:current_node; 166, identifier:current_idx; 167, identifier:append; 168, argument_list; 168, 169; 169, identifier:idx; 170, else_clause; 170, 171; 171, block; 171, 172; 172, raise_statement; 172, 173; 173, call; 173, 174; 173, 175; 174, identifier:IndexError; 175, argument_list; 175, 176; 176, string:"Index attempted on non-array component"; 177, return_statement; 177, 178; 178, identifier:current_node
def find_by_path(self, path): """ Finds the descendant node that is located at the relative path Returns ``None`` if not found Raises exception if path is malformed, or array index is out of range Parameters ---------- path: str Path to target relative to current node Returns ------- :class:`~Node` or None Descendant Node. None if not found. Raises ------ ValueError If path syntax is invalid IndexError If an array index in the path is invalid """ pathparts = path.split('.') current_node = self for pathpart in pathparts: m = re.fullmatch(r'^(\w+)((?:\[(?:\d+|0[xX][\da-fA-F]+)\])*)$', pathpart) if not m: raise ValueError("Invalid path") inst_name, array_suffix = m.group(1,2) idx_list = [ int(s,0) for s in re.findall(r'\[(\d+|0[xX][\da-fA-F]+)\]', array_suffix) ] current_node = current_node.get_child_by_name(inst_name) if current_node is None: return None if idx_list: if (isinstance(current_node, AddressableNode)) and current_node.inst.is_array: # is an array if len(idx_list) != len(current_node.inst.array_dimensions): raise IndexError("Wrong number of array dimensions") current_node.current_idx = [] # pylint: disable=attribute-defined-outside-init for i,idx in enumerate(idx_list): if idx >= current_node.inst.array_dimensions[i]: raise IndexError("Array index out of range") current_node.current_idx.append(idx) else: raise IndexError("Index attempted on non-array component") return current_node
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:get_property; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:prop_name; 6, dictionary_splat_pattern; 6, 7; 7, identifier:kwargs; 8, block; 8, 9; 8, 11; 8, 15; 8, 19; 8, 37; 8, 38; 8, 57; 8, 58; 8, 117; 8, 123; 8, 124; 8, 137; 8, 138; 8, 150; 8, 169; 8, 170; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:ovr_default; 14, False; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:default; 18, None; 19, if_statement; 19, 20; 19, 23; 20, comparison_operator:in; 20, 21; 20, 22; 21, string:'default'; 22, identifier:kwargs; 23, block; 23, 24; 23, 28; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:ovr_default; 27, True; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:default; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:kwargs; 34, identifier:pop; 35, argument_list; 35, 36; 36, string:'default'; 37, comment; 38, if_statement; 38, 39; 38, 40; 39, identifier:kwargs; 40, block; 40, 41; 41, raise_statement; 41, 42; 42, call; 42, 43; 42, 44; 43, identifier:TypeError; 44, argument_list; 44, 45; 45, binary_operator:%; 45, 46; 45, 47; 46, string:"got an unexpected keyword argument '%s'"; 47, subscript; 47, 48; 47, 56; 48, call; 48, 49; 48, 50; 49, identifier:list; 50, argument_list; 50, 51; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:kwargs; 54, identifier:keys; 55, argument_list; 56, integer:0; 57, comment; 58, if_statement; 58, 59; 58, 66; 59, comparison_operator:in; 59, 60; 59, 61; 60, identifier:prop_name; 61, attribute; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:self; 64, identifier:inst; 65, identifier:properties; 66, block; 66, 67; 66, 77; 66, 99; 66, 115; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:prop_value; 70, subscript; 70, 71; 70, 76; 71, attribute; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:self; 74, identifier:inst; 75, identifier:properties; 76, identifier:prop_name; 77, if_statement; 77, 78; 77, 85; 77, 86; 78, call; 78, 79; 78, 80; 79, identifier:isinstance; 80, argument_list; 80, 81; 80, 82; 81, identifier:prop_value; 82, attribute; 82, 83; 82, 84; 83, identifier:rdltypes; 84, identifier:ComponentRef; 85, comment; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:prop_value; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:prop_value; 93, identifier:build_node_ref; 94, argument_list; 94, 95; 94, 96; 95, identifier:self; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:env; 99, if_statement; 99, 100; 99, 107; 100, call; 100, 101; 100, 102; 101, identifier:isinstance; 102, argument_list; 102, 103; 102, 104; 103, identifier:prop_value; 104, attribute; 104, 105; 104, 106; 105, identifier:rdltypes; 106, identifier:PropertyReference; 107, block; 107, 108; 108, expression_statement; 108, 109; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:prop_value; 112, identifier:_resolve_node; 113, argument_list; 113, 114; 114, identifier:self; 115, return_statement; 115, 116; 116, identifier:prop_value; 117, if_statement; 117, 118; 117, 119; 117, 120; 118, identifier:ovr_default; 119, comment; 120, block; 120, 121; 121, return_statement; 121, 122; 122, identifier:default; 123, comment; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:rule; 127, call; 127, 128; 127, 135; 128, attribute; 128, 129; 128, 134; 129, attribute; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:self; 132, identifier:env; 133, identifier:property_rules; 134, identifier:lookup_property; 135, argument_list; 135, 136; 136, identifier:prop_name; 137, comment; 138, if_statement; 138, 139; 138, 142; 139, comparison_operator:is; 139, 140; 139, 141; 140, identifier:rule; 141, None; 142, block; 142, 143; 143, raise_statement; 143, 144; 144, call; 144, 145; 144, 146; 145, identifier:LookupError; 146, argument_list; 146, 147; 147, binary_operator:%; 147, 148; 147, 149; 148, string:"Unknown property '%s'"; 149, identifier:prop_name; 150, if_statement; 150, 151; 150, 161; 151, comparison_operator:not; 151, 152; 151, 158; 152, call; 152, 153; 152, 154; 153, identifier:type; 154, argument_list; 154, 155; 155, attribute; 155, 156; 155, 157; 156, identifier:self; 157, identifier:inst; 158, attribute; 158, 159; 158, 160; 159, identifier:rule; 160, identifier:bindable_to; 161, block; 161, 162; 162, raise_statement; 162, 163; 163, call; 163, 164; 163, 165; 164, identifier:LookupError; 165, argument_list; 165, 166; 166, binary_operator:%; 166, 167; 166, 168; 167, string:"Unknown property '%s'"; 168, identifier:prop_name; 169, comment; 170, return_statement; 170, 171; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:rule; 174, identifier:get_default; 175, argument_list; 175, 176; 176, identifier:self
def get_property(self, prop_name, **kwargs): """ Gets the SystemRDL component property If a property was not explicitly set in the RDL source, its default value is derived. In some cases, a default value is implied according to other property values. Properties values that are a reference to a component instance are converted to a :class:`~Node` overlay object. Parameters ---------- prop_name: str SystemRDL property name default: Override built-in default value of property. If the property was not explicitly set, return this value rather than the property's intrinsic default value. Raises ------ LookupError If prop_name is invalid """ ovr_default = False default = None if 'default' in kwargs: ovr_default = True default = kwargs.pop('default') # Check for stray kwargs if kwargs: raise TypeError("got an unexpected keyword argument '%s'" % list(kwargs.keys())[0]) # If its already in the component, then safe to bypass checks if prop_name in self.inst.properties: prop_value = self.inst.properties[prop_name] if isinstance(prop_value, rdltypes.ComponentRef): # If this is a hierarchical component reference, convert it to a Node reference prop_value = prop_value.build_node_ref(self, self.env) if isinstance(prop_value, rdltypes.PropertyReference): prop_value._resolve_node(self) return prop_value if ovr_default: # Default value is being overridden by user. Return their value return default # Otherwise, return its default value based on the property's rules rule = self.env.property_rules.lookup_property(prop_name) # Is it even a valid property or allowed for this component type? if rule is None: raise LookupError("Unknown property '%s'" % prop_name) if type(self.inst) not in rule.bindable_to: raise LookupError("Unknown property '%s'" % prop_name) # Return the default value as specified by the rulebook return rule.get_default(self)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:elaborate; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:top_def_name; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:inst_name; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:parameters; 13, None; 14, block; 14, 15; 14, 17; 14, 26; 14, 27; 14, 136; 14, 137; 14, 146; 14, 152; 14, 160; 14, 166; 14, 167; 14, 174; 14, 180; 14, 186; 14, 192; 14, 198; 14, 199; 14, 218; 14, 219; 14, 313; 14, 314; 14, 323; 14, 334; 14, 335; 14, 355; 14, 356; 14, 388; 14, 389; 14, 390; 14, 410; 14, 426; 15, expression_statement; 15, 16; 16, comment; 17, if_statement; 17, 18; 17, 21; 18, comparison_operator:is; 18, 19; 18, 20; 19, identifier:parameters; 20, None; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:parameters; 25, dictionary; 26, comment; 27, if_statement; 27, 28; 27, 31; 27, 32; 27, 84; 28, comparison_operator:is; 28, 29; 28, 30; 29, identifier:top_def_name; 30, None; 31, comment; 32, block; 32, 33; 32, 53; 32, 63; 33, if_statement; 33, 34; 33, 41; 34, comparison_operator:not; 34, 35; 34, 36; 35, identifier:top_def_name; 36, attribute; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:root; 40, identifier:comp_defs; 41, block; 41, 42; 42, expression_statement; 42, 43; 43, call; 43, 44; 43, 49; 44, attribute; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:msg; 48, identifier:fatal; 49, argument_list; 49, 50; 50, binary_operator:%; 50, 51; 50, 52; 51, string:"Elaboration target '%s' not found"; 52, identifier:top_def_name; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:top_def; 56, subscript; 56, 57; 56, 62; 57, attribute; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:root; 61, identifier:comp_defs; 62, identifier:top_def_name; 63, if_statement; 63, 64; 63, 72; 64, not_operator; 64, 65; 65, call; 65, 66; 65, 67; 66, identifier:isinstance; 67, argument_list; 67, 68; 67, 69; 68, identifier:top_def; 69, attribute; 69, 70; 69, 71; 70, identifier:comp; 71, identifier:Addrmap; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 80; 75, attribute; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:self; 78, identifier:msg; 79, identifier:fatal; 80, argument_list; 80, 81; 81, binary_operator:%; 81, 82; 81, 83; 82, string:"Elaboration target '%s' is not an 'addrmap' component"; 83, identifier:top_def_name; 84, else_clause; 84, 85; 84, 86; 85, comment; 86, block; 86, 87; 87, for_statement; 87, 88; 87, 89; 87, 104; 87, 125; 88, identifier:comp_def; 89, call; 89, 90; 89, 91; 90, identifier:reversed; 91, argument_list; 91, 92; 92, call; 92, 93; 92, 94; 93, identifier:list; 94, argument_list; 94, 95; 95, call; 95, 96; 95, 103; 96, attribute; 96, 97; 96, 102; 97, attribute; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:root; 101, identifier:comp_defs; 102, identifier:values; 103, argument_list; 104, block; 104, 105; 105, if_statement; 105, 106; 105, 113; 106, call; 106, 107; 106, 108; 107, identifier:isinstance; 108, argument_list; 108, 109; 108, 110; 109, identifier:comp_def; 110, attribute; 110, 111; 110, 112; 111, identifier:comp; 112, identifier:Addrmap; 113, block; 113, 114; 113, 118; 113, 124; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:top_def; 117, identifier:comp_def; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:top_def_name; 121, attribute; 121, 122; 121, 123; 122, identifier:comp_def; 123, identifier:type_name; 124, break_statement; 125, else_clause; 125, 126; 126, block; 126, 127; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 134; 129, attribute; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:self; 132, identifier:msg; 133, identifier:fatal; 134, argument_list; 134, 135; 135, string:"Could not find any 'addrmap' components to elaborate"; 136, comment; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:root_inst; 140, call; 140, 141; 140, 142; 141, identifier:deepcopy; 142, argument_list; 142, 143; 143, attribute; 143, 144; 143, 145; 144, identifier:self; 145, identifier:root; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:root_inst; 150, identifier:is_instance; 151, True; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:root_inst; 156, identifier:original_def; 157, attribute; 157, 158; 157, 159; 158, identifier:self; 159, identifier:root; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:root_inst; 164, identifier:inst_name; 165, string:"$root"; 166, comment; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:top_inst; 170, call; 170, 171; 170, 172; 171, identifier:deepcopy; 172, argument_list; 172, 173; 173, identifier:top_def; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:top_inst; 178, identifier:is_instance; 179, True; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:top_inst; 184, identifier:original_def; 185, identifier:top_def; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:top_inst; 190, identifier:addr_offset; 191, integer:0; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:top_inst; 196, identifier:external; 197, True; 198, comment; 199, if_statement; 199, 200; 199, 203; 199, 210; 200, comparison_operator:is; 200, 201; 200, 202; 201, identifier:inst_name; 202, None; 203, block; 203, 204; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:top_inst; 208, identifier:inst_name; 209, identifier:inst_name; 210, else_clause; 210, 211; 211, block; 211, 212; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:top_inst; 216, identifier:inst_name; 217, identifier:top_def_name; 218, comment; 219, for_statement; 219, 220; 219, 223; 219, 228; 219, 229; 220, pattern_list; 220, 221; 220, 222; 221, identifier:param_name; 222, identifier:value; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:parameters; 226, identifier:items; 227, argument_list; 228, comment; 229, block; 229, 230; 229, 234; 229, 261; 229, 273; 229, 281; 229, 293; 229, 307; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:parameter; 233, None; 234, for_statement; 234, 235; 234, 236; 234, 239; 234, 252; 235, identifier:p; 236, attribute; 236, 237; 236, 238; 237, identifier:top_inst; 238, identifier:parameters; 239, block; 239, 240; 240, if_statement; 240, 241; 240, 246; 241, comparison_operator:==; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:p; 244, identifier:name; 245, identifier:param_name; 246, block; 246, 247; 246, 251; 247, expression_statement; 247, 248; 248, assignment; 248, 249; 248, 250; 249, identifier:parameter; 250, identifier:p; 251, break_statement; 252, else_clause; 252, 253; 253, block; 253, 254; 254, raise_statement; 254, 255; 255, call; 255, 256; 255, 257; 256, identifier:ValueError; 257, argument_list; 257, 258; 258, binary_operator:%; 258, 259; 258, 260; 259, string:"Parameter '%s' is not available for override"; 260, identifier:param_name; 261, expression_statement; 261, 262; 262, assignment; 262, 263; 262, 264; 263, identifier:value_expr; 264, call; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:expr; 267, identifier:ExternalLiteral; 268, argument_list; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:self; 271, identifier:env; 272, identifier:value; 273, expression_statement; 273, 274; 274, assignment; 274, 275; 274, 276; 275, identifier:value_type; 276, call; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:value_expr; 279, identifier:predict_type; 280, argument_list; 281, if_statement; 281, 282; 281, 285; 282, comparison_operator:is; 282, 283; 282, 284; 283, identifier:value_type; 284, None; 285, block; 285, 286; 286, raise_statement; 286, 287; 287, call; 287, 288; 287, 289; 288, identifier:TypeError; 289, argument_list; 289, 290; 290, binary_operator:%; 290, 291; 290, 292; 291, string:"Override value for parameter '%s' is an unrecognized type"; 292, identifier:param_name; 293, if_statement; 293, 294; 293, 299; 294, comparison_operator:!=; 294, 295; 294, 296; 295, identifier:value_type; 296, attribute; 296, 297; 296, 298; 297, identifier:parameter; 298, identifier:param_type; 299, block; 299, 300; 300, raise_statement; 300, 301; 301, call; 301, 302; 301, 303; 302, identifier:TypeError; 303, argument_list; 303, 304; 304, binary_operator:%; 304, 305; 304, 306; 305, string:"Incorrect type for parameter '%s'"; 306, identifier:param_name; 307, expression_statement; 307, 308; 308, assignment; 308, 309; 308, 312; 309, attribute; 309, 310; 309, 311; 310, identifier:parameter; 311, identifier:expr; 312, identifier:value_expr; 313, comment; 314, expression_statement; 314, 315; 315, call; 315, 316; 315, 321; 316, attribute; 316, 317; 316, 320; 317, attribute; 317, 318; 317, 319; 318, identifier:root_inst; 319, identifier:children; 320, identifier:append; 321, argument_list; 321, 322; 322, identifier:top_inst; 323, expression_statement; 323, 324; 324, assignment; 324, 325; 324, 326; 325, identifier:root_node; 326, call; 326, 327; 326, 328; 327, identifier:RootNode; 328, argument_list; 328, 329; 328, 330; 328, 333; 329, identifier:root_inst; 330, attribute; 330, 331; 330, 332; 331, identifier:self; 332, identifier:env; 333, None; 334, comment; 335, expression_statement; 335, 336; 336, call; 336, 337; 336, 347; 337, attribute; 337, 338; 337, 346; 338, call; 338, 339; 338, 342; 339, attribute; 339, 340; 339, 341; 340, identifier:walker; 341, identifier:RDLWalker; 342, argument_list; 342, 343; 343, keyword_argument; 343, 344; 343, 345; 344, identifier:skip_not_present; 345, False; 346, identifier:walk; 347, argument_list; 347, 348; 347, 349; 348, identifier:root_node; 349, call; 349, 350; 349, 351; 350, identifier:ElabExpressionsListener; 351, argument_list; 351, 352; 352, attribute; 352, 353; 352, 354; 353, identifier:self; 354, identifier:msg; 355, comment; 356, expression_statement; 356, 357; 357, call; 357, 358; 357, 368; 358, attribute; 358, 359; 358, 367; 359, call; 359, 360; 359, 363; 360, attribute; 360, 361; 360, 362; 361, identifier:walker; 362, identifier:RDLWalker; 363, argument_list; 363, 364; 364, keyword_argument; 364, 365; 364, 366; 365, identifier:skip_not_present; 366, False; 367, identifier:walk; 368, argument_list; 368, 369; 368, 370; 368, 376; 368, 382; 369, identifier:root_node; 370, call; 370, 371; 370, 372; 371, identifier:PrePlacementValidateListener; 372, argument_list; 372, 373; 373, attribute; 373, 374; 373, 375; 374, identifier:self; 375, identifier:msg; 376, call; 376, 377; 376, 378; 377, identifier:StructuralPlacementListener; 378, argument_list; 378, 379; 379, attribute; 379, 380; 379, 381; 380, identifier:self; 381, identifier:msg; 382, call; 382, 383; 382, 384; 383, identifier:LateElabListener; 384, argument_list; 384, 385; 385, attribute; 385, 386; 385, 387; 386, identifier:self; 387, identifier:msg; 388, comment; 389, comment; 390, expression_statement; 390, 391; 391, call; 391, 392; 391, 402; 392, attribute; 392, 393; 392, 401; 393, call; 393, 394; 393, 397; 394, attribute; 394, 395; 394, 396; 395, identifier:walker; 396, identifier:RDLWalker; 397, argument_list; 397, 398; 398, keyword_argument; 398, 399; 398, 400; 399, identifier:skip_not_present; 400, True; 401, identifier:walk; 402, argument_list; 402, 403; 402, 404; 403, identifier:root_node; 404, call; 404, 405; 404, 406; 405, identifier:ValidateListener; 406, argument_list; 406, 407; 407, attribute; 407, 408; 407, 409; 408, identifier:self; 409, identifier:env; 410, if_statement; 410, 411; 410, 416; 411, attribute; 411, 412; 411, 415; 412, attribute; 412, 413; 412, 414; 413, identifier:self; 414, identifier:msg; 415, identifier:had_error; 416, block; 416, 417; 417, expression_statement; 417, 418; 418, call; 418, 419; 418, 424; 419, attribute; 419, 420; 419, 423; 420, attribute; 420, 421; 420, 422; 421, identifier:self; 422, identifier:msg; 423, identifier:fatal; 424, argument_list; 424, 425; 425, string:"Elaborate aborted due to previous errors"; 426, return_statement; 426, 427; 427, identifier:root_node
def elaborate(self, top_def_name=None, inst_name=None, parameters=None): """ Elaborates the design for the given top-level addrmap component. During elaboration, the following occurs: - An instance of the ``$root`` meta-component is created. - The addrmap component specified by ``top_def_name`` is instantiated as a child of ``$root``. - Expressions, parameters, and inferred address/field placements are elaborated. - Validation checks are performed. If a design contains multiple root-level addrmaps, ``elaborate()`` can be called multiple times in order to elaborate each individually. If any exceptions (:class:`~systemrdl.RDLCompileError` or other) occur during elaboration, then the RDLCompiler object should be discarded. Parameters ---------- top_def_name: str Explicitly choose which addrmap in the root namespace will be the top-level component. If unset, The last addrmap defined will be chosen. inst_name: str Overrides the top-component's instantiated name. By default, instantiated name is the same as ``top_def_name`` parameters: dict Dictionary of parameter overrides for the top component instance. Raises ------ :class:`~systemrdl.RDLCompileError` If any fatal elaboration error is encountered Returns ------- :class:`~systemrdl.node.RootNode` Elaborated root meta-component's Node object. """ if parameters is None: parameters = {} # Get top-level component definition to elaborate if top_def_name is not None: # Lookup top_def_name if top_def_name not in self.root.comp_defs: self.msg.fatal("Elaboration target '%s' not found" % top_def_name) top_def = self.root.comp_defs[top_def_name] if not isinstance(top_def, comp.Addrmap): self.msg.fatal("Elaboration target '%s' is not an 'addrmap' component" % top_def_name) else: # Not specified. Find the last addrmap defined for comp_def in reversed(list(self.root.comp_defs.values())): if isinstance(comp_def, comp.Addrmap): top_def = comp_def top_def_name = comp_def.type_name break else: self.msg.fatal("Could not find any 'addrmap' components to elaborate") # Create an instance of the root component root_inst = deepcopy(self.root) root_inst.is_instance = True root_inst.original_def = self.root root_inst.inst_name = "$root" # Create a top-level instance top_inst = deepcopy(top_def) top_inst.is_instance = True top_inst.original_def = top_def top_inst.addr_offset = 0 top_inst.external = True # addrmap is always implied as external if inst_name is not None: top_inst.inst_name = inst_name else: top_inst.inst_name = top_def_name # Override parameters as needed for param_name, value in parameters.items(): # Find the parameter to override parameter = None for p in top_inst.parameters: if p.name == param_name: parameter = p break else: raise ValueError("Parameter '%s' is not available for override" % param_name) value_expr = expr.ExternalLiteral(self.env, value) value_type = value_expr.predict_type() if value_type is None: raise TypeError("Override value for parameter '%s' is an unrecognized type" % param_name) if value_type != parameter.param_type: raise TypeError("Incorrect type for parameter '%s'" % param_name) parameter.expr = value_expr # instantiate top_inst into the root component instance root_inst.children.append(top_inst) root_node = RootNode(root_inst, self.env, None) # Resolve all expressions walker.RDLWalker(skip_not_present=False).walk( root_node, ElabExpressionsListener(self.msg) ) # Resolve address and field placement walker.RDLWalker(skip_not_present=False).walk( root_node, PrePlacementValidateListener(self.msg), StructuralPlacementListener(self.msg), LateElabListener(self.msg) ) # Validate design # Only need to validate nodes that are present walker.RDLWalker(skip_not_present=True).walk(root_node, ValidateListener(self.env)) if self.msg.had_error: self.msg.fatal("Elaborate aborted due to previous errors") return root_node
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:resolve_addresses; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:node; 6, block; 6, 7; 6, 9; 6, 10; 6, 11; 6, 20; 6, 30; 6, 34; 6, 277; 6, 278; 6, 279; 6, 303; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:prop_alignment; 14, subscript; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:alignment_stack; 18, unary_operator:-; 18, 19; 19, integer:1; 20, if_statement; 20, 21; 20, 24; 20, 25; 21, comparison_operator:is; 21, 22; 21, 23; 22, identifier:prop_alignment; 23, None; 24, comment; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:prop_alignment; 29, integer:1; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:prev_node; 33, None; 34, for_statement; 34, 35; 34, 36; 34, 44; 35, identifier:child_node; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:node; 39, identifier:children; 40, argument_list; 40, 41; 41, keyword_argument; 41, 42; 41, 43; 42, identifier:skip_not_present; 43, False; 44, block; 44, 45; 44, 54; 44, 69; 44, 103; 44, 104; 44, 112; 44, 122; 44, 123; 44, 227; 44, 228; 44, 237; 44, 260; 44, 261; 44, 273; 45, if_statement; 45, 46; 45, 52; 46, not_operator; 46, 47; 47, call; 47, 48; 47, 49; 48, identifier:isinstance; 49, argument_list; 49, 50; 49, 51; 50, identifier:child_node; 51, identifier:AddressableNode; 52, block; 52, 53; 53, continue_statement; 54, if_statement; 54, 55; 54, 62; 54, 63; 55, comparison_operator:is; 55, 56; 55, 61; 56, attribute; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:child_node; 59, identifier:inst; 60, identifier:addr_offset; 61, None; 62, comment; 63, block; 63, 64; 63, 68; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:prev_node; 67, identifier:child_node; 68, continue_statement; 69, if_statement; 69, 70; 69, 75; 70, attribute; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:node; 73, identifier:env; 74, identifier:chk_implicit_addr; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, call; 77, 78; 77, 85; 78, attribute; 78, 79; 78, 84; 79, attribute; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:node; 82, identifier:env; 83, identifier:msg; 84, identifier:message; 85, argument_list; 85, 86; 85, 91; 85, 98; 86, attribute; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:node; 89, identifier:env; 90, identifier:chk_implicit_addr; 91, binary_operator:%; 91, 92; 91, 93; 92, string:"Address offset of component '%s' is not explicitly set"; 93, attribute; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:child_node; 96, identifier:inst; 97, identifier:inst_name; 98, attribute; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:child_node; 101, identifier:inst; 102, identifier:inst_src_ref; 103, comment; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:alloc_alignment; 107, attribute; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:child_node; 110, identifier:inst; 111, identifier:addr_align; 112, if_statement; 112, 113; 112, 116; 112, 117; 113, comparison_operator:is; 113, 114; 113, 115; 114, identifier:alloc_alignment; 115, None; 116, comment; 117, block; 117, 118; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:alloc_alignment; 121, integer:1; 122, comment; 123, if_statement; 123, 124; 123, 136; 123, 164; 123, 193; 123, 223; 124, comparison_operator:==; 124, 125; 124, 131; 125, subscript; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:self; 128, identifier:addressing_mode_stack; 129, unary_operator:-; 129, 130; 130, integer:1; 131, attribute; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:rdltypes; 134, identifier:AddressingType; 135, identifier:compact; 136, block; 136, 137; 137, if_statement; 137, 138; 137, 143; 137, 144; 137, 156; 138, call; 138, 139; 138, 140; 139, identifier:isinstance; 140, argument_list; 140, 141; 140, 142; 141, identifier:child_node; 142, identifier:RegNode; 143, comment; 144, block; 144, 145; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:mode_alignment; 148, binary_operator://; 148, 149; 148, 155; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:child_node; 152, identifier:get_property; 153, argument_list; 153, 154; 154, string:'accesswidth'; 155, integer:8; 156, else_clause; 156, 157; 156, 158; 156, 159; 157, comment; 158, comment; 159, block; 159, 160; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:mode_alignment; 163, integer:1; 164, elif_clause; 164, 165; 164, 177; 164, 178; 164, 179; 165, comparison_operator:==; 165, 166; 165, 172; 166, subscript; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:self; 169, identifier:addressing_mode_stack; 170, unary_operator:-; 170, 171; 171, integer:1; 172, attribute; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:rdltypes; 175, identifier:AddressingType; 176, identifier:regalign; 177, comment; 178, comment; 179, block; 179, 180; 179, 186; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:mode_alignment; 183, attribute; 183, 184; 183, 185; 184, identifier:child_node; 185, identifier:size; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:mode_alignment; 189, call; 189, 190; 189, 191; 190, identifier:roundup_pow2; 191, argument_list; 191, 192; 192, identifier:mode_alignment; 193, elif_clause; 193, 194; 193, 206; 193, 207; 193, 208; 193, 209; 194, comparison_operator:==; 194, 195; 194, 201; 195, subscript; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:self; 198, identifier:addressing_mode_stack; 199, unary_operator:-; 199, 200; 200, integer:1; 201, attribute; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:rdltypes; 204, identifier:AddressingType; 205, identifier:fullalign; 206, comment; 207, comment; 208, comment; 209, block; 209, 210; 209, 216; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 213; 212, identifier:mode_alignment; 213, attribute; 213, 214; 213, 215; 214, identifier:child_node; 215, identifier:total_size; 216, expression_statement; 216, 217; 217, assignment; 217, 218; 217, 219; 218, identifier:mode_alignment; 219, call; 219, 220; 219, 221; 220, identifier:roundup_pow2; 221, argument_list; 221, 222; 222, identifier:mode_alignment; 223, else_clause; 223, 224; 224, block; 224, 225; 225, raise_statement; 225, 226; 226, identifier:RuntimeError; 227, comment; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:alignment; 231, call; 231, 232; 231, 233; 232, identifier:max; 233, argument_list; 233, 234; 233, 235; 233, 236; 234, identifier:prop_alignment; 235, identifier:alloc_alignment; 236, identifier:mode_alignment; 237, if_statement; 237, 238; 237, 241; 237, 246; 238, comparison_operator:is; 238, 239; 238, 240; 239, identifier:prev_node; 240, None; 241, block; 241, 242; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 245; 244, identifier:next_offset; 245, integer:0; 246, else_clause; 246, 247; 247, block; 247, 248; 248, expression_statement; 248, 249; 249, assignment; 249, 250; 249, 251; 250, identifier:next_offset; 251, binary_operator:+; 251, 252; 251, 257; 252, attribute; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:prev_node; 255, identifier:inst; 256, identifier:addr_offset; 257, attribute; 257, 258; 257, 259; 258, identifier:prev_node; 259, identifier:total_size; 260, comment; 261, expression_statement; 261, 262; 262, assignment; 262, 263; 262, 268; 263, attribute; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:child_node; 266, identifier:inst; 267, identifier:addr_offset; 268, call; 268, 269; 268, 270; 269, identifier:roundup_to; 270, argument_list; 270, 271; 270, 272; 271, identifier:next_offset; 272, identifier:alignment; 273, expression_statement; 273, 274; 274, assignment; 274, 275; 274, 276; 275, identifier:prev_node; 276, identifier:child_node; 277, comment; 278, comment; 279, function_definition; 279, 280; 279, 281; 279, 283; 280, function_name:get_child_sort_key; 281, parameters; 281, 282; 282, identifier:inst; 283, block; 283, 284; 284, if_statement; 284, 285; 284, 293; 284, 297; 285, not_operator; 285, 286; 286, call; 286, 287; 286, 288; 287, identifier:isinstance; 288, argument_list; 288, 289; 288, 290; 289, identifier:inst; 290, attribute; 290, 291; 290, 292; 291, identifier:comp; 292, identifier:AddressableComponent; 293, block; 293, 294; 294, return_statement; 294, 295; 295, unary_operator:-; 295, 296; 296, integer:1; 297, else_clause; 297, 298; 298, block; 298, 299; 299, return_statement; 299, 300; 300, attribute; 300, 301; 300, 302; 301, identifier:inst; 302, identifier:addr_offset; 303, expression_statement; 303, 304; 304, call; 304, 305; 304, 312; 305, attribute; 305, 306; 305, 311; 306, attribute; 306, 307; 306, 310; 307, attribute; 307, 308; 307, 309; 308, identifier:node; 309, identifier:inst; 310, identifier:children; 311, identifier:sort; 312, argument_list; 312, 313; 313, keyword_argument; 313, 314; 313, 315; 314, identifier:key; 315, identifier:get_child_sort_key
def resolve_addresses(self, node): """ Resolve addresses of children of Addrmap and Regfile components """ # Get alignment based on 'alignment' property # This remains constant for all children prop_alignment = self.alignment_stack[-1] if prop_alignment is None: # was not specified. Does not contribute to alignment prop_alignment = 1 prev_node = None for child_node in node.children(skip_not_present=False): if not isinstance(child_node, AddressableNode): continue if child_node.inst.addr_offset is not None: # Address is already known. Do not need to infer prev_node = child_node continue if node.env.chk_implicit_addr: node.env.msg.message( node.env.chk_implicit_addr, "Address offset of component '%s' is not explicitly set" % child_node.inst.inst_name, child_node.inst.inst_src_ref ) # Get alignment specified by '%=' allocator, if any alloc_alignment = child_node.inst.addr_align if alloc_alignment is None: # was not specified. Does not contribute to alignment alloc_alignment = 1 # Calculate alignment based on current addressing mode if self.addressing_mode_stack[-1] == rdltypes.AddressingType.compact: if isinstance(child_node, RegNode): # Regs are aligned based on their accesswidth mode_alignment = child_node.get_property('accesswidth') // 8 else: # Spec does not specify for other components # Assuming absolutely compact packing mode_alignment = 1 elif self.addressing_mode_stack[-1] == rdltypes.AddressingType.regalign: # Components are aligned to a multiple of their size # Spec vaguely suggests that alignment is also a power of 2 mode_alignment = child_node.size mode_alignment = roundup_pow2(mode_alignment) elif self.addressing_mode_stack[-1] == rdltypes.AddressingType.fullalign: # Same as regalign except for arrays # Arrays are aligned to their total size # Both are rounded to power of 2 mode_alignment = child_node.total_size mode_alignment = roundup_pow2(mode_alignment) else: raise RuntimeError # Calculate resulting address offset alignment = max(prop_alignment, alloc_alignment, mode_alignment) if prev_node is None: next_offset = 0 else: next_offset = prev_node.inst.addr_offset + prev_node.total_size # round next_offset up to alignment child_node.inst.addr_offset = roundup_to(next_offset, alignment) prev_node = child_node # Sort children by address offset # Non-addressable child components are sorted to be first (signals) def get_child_sort_key(inst): if not isinstance(inst, comp.AddressableComponent): return -1 else: return inst.addr_offset node.inst.children.sort(key=get_child_sort_key)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:parse_include; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:start; 6, block; 6, 7; 6, 9; 6, 10; 6, 14; 6, 35; 6, 39; 6, 40; 6, 92; 6, 93; 6, 102; 6, 114; 6, 144; 6, 160; 6, 171; 6, 180; 6, 181; 6, 182; 6, 191; 6, 260; 6, 261; 6, 332; 6, 368; 6, 369; 6, 375; 6, 422; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:i; 13, identifier:start; 14, while_statement; 14, 15; 14, 16; 15, identifier:i; 16, block; 16, 17; 16, 31; 17, if_statement; 17, 18; 17, 25; 18, comparison_operator:==; 18, 19; 18, 24; 19, subscript; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:text; 23, identifier:i; 24, string:'\n'; 25, block; 25, 26; 25, 30; 26, expression_statement; 26, 27; 27, augmented_assignment:+=; 27, 28; 27, 29; 28, identifier:i; 29, integer:1; 30, break_statement; 31, expression_statement; 31, 32; 32, augmented_assignment:-=; 32, 33; 32, 34; 33, identifier:i; 34, integer:1; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:line_start; 38, identifier:i; 39, comment; 40, if_statement; 40, 41; 40, 66; 41, not_operator; 41, 42; 42, parenthesized_expression; 42, 43; 43, boolean_operator:or; 43, 44; 43, 54; 44, comparison_operator:==; 44, 45; 44, 53; 45, subscript; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:text; 49, slice; 49, 50; 49, 51; 49, 52; 50, identifier:line_start; 51, colon; 52, identifier:start; 53, string:""; 54, call; 54, 55; 54, 65; 55, attribute; 55, 56; 55, 64; 56, subscript; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:text; 60, slice; 60, 61; 60, 62; 60, 63; 61, identifier:line_start; 62, colon; 63, identifier:start; 64, identifier:isspace; 65, argument_list; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, call; 68, 69; 68, 76; 69, attribute; 69, 70; 69, 75; 70, attribute; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:env; 74, identifier:msg; 75, identifier:fatal; 76, argument_list; 76, 77; 76, 78; 77, string:"Unexpected text before include"; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:messages; 81, identifier:SourceRef; 82, argument_list; 82, 83; 82, 84; 82, 87; 83, identifier:line_start; 84, binary_operator:-; 84, 85; 84, 86; 85, identifier:start; 86, integer:1; 87, keyword_argument; 87, 88; 87, 89; 88, identifier:filename; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:path; 92, comment; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:inc_regex; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:re; 99, identifier:compile; 100, argument_list; 100, 101; 101, string:r'`include\s+("([^\r\n]+)"|<([^\r\n]+)>)'; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:m_inc; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:inc_regex; 108, identifier:match; 109, argument_list; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:self; 112, identifier:text; 113, identifier:start; 114, if_statement; 114, 115; 114, 118; 115, comparison_operator:is; 115, 116; 115, 117; 116, identifier:m_inc; 117, None; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, call; 120, 121; 120, 128; 121, attribute; 121, 122; 121, 127; 122, attribute; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:env; 126, identifier:msg; 127, identifier:fatal; 128, argument_list; 128, 129; 128, 130; 129, string:"Invalid usage of include directive"; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:messages; 133, identifier:SourceRef; 134, argument_list; 134, 135; 134, 136; 134, 139; 135, identifier:start; 136, binary_operator:+; 136, 137; 136, 138; 137, identifier:start; 138, integer:7; 139, keyword_argument; 139, 140; 139, 141; 140, identifier:filename; 141, attribute; 141, 142; 141, 143; 142, identifier:self; 143, identifier:path; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:incl_path_raw; 147, boolean_operator:or; 147, 148; 147, 154; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:m_inc; 151, identifier:group; 152, argument_list; 152, 153; 153, integer:2; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:m_inc; 157, identifier:group; 158, argument_list; 158, 159; 159, integer:3; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:end; 163, binary_operator:-; 163, 164; 163, 170; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:m_inc; 167, identifier:end; 168, argument_list; 168, 169; 169, integer:0; 170, integer:1; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:path_start; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:m_inc; 177, identifier:start; 178, argument_list; 178, 179; 179, integer:1; 180, comment; 181, comment; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:tail_regex; 185, call; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:re; 188, identifier:compile; 189, argument_list; 189, 190; 190, string:r'(?:[ \t]*/\*[^\r\n]*?\*/)*[ \t]*(?://[^\r\n]*?|/\*[^\r\n]*?)?\r?\n'; 191, if_statement; 191, 192; 191, 204; 192, not_operator; 192, 193; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:tail_regex; 196, identifier:match; 197, argument_list; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:self; 200, identifier:text; 201, binary_operator:+; 201, 202; 201, 203; 202, identifier:end; 203, integer:1; 204, block; 204, 205; 204, 214; 204, 228; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:tail_capture_regex; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:re; 211, identifier:compile; 212, argument_list; 212, 213; 213, string:r'[^\r\n]*?\r?\n'; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:m; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:tail_capture_regex; 220, identifier:match; 221, argument_list; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:self; 224, identifier:text; 225, binary_operator:+; 225, 226; 225, 227; 226, identifier:end; 227, integer:1; 228, expression_statement; 228, 229; 229, call; 229, 230; 229, 237; 230, attribute; 230, 231; 230, 236; 231, attribute; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:self; 234, identifier:env; 235, identifier:msg; 236, identifier:fatal; 237, argument_list; 237, 238; 237, 239; 238, string:"Unexpected text after include"; 239, call; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:messages; 242, identifier:SourceRef; 243, argument_list; 243, 244; 243, 247; 243, 255; 244, binary_operator:+; 244, 245; 244, 246; 245, identifier:end; 246, integer:1; 247, binary_operator:-; 247, 248; 247, 254; 248, call; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:m; 251, identifier:end; 252, argument_list; 252, 253; 253, integer:0; 254, integer:1; 255, keyword_argument; 255, 256; 255, 257; 256, identifier:filename; 257, attribute; 257, 258; 257, 259; 258, identifier:self; 259, identifier:path; 260, comment; 261, if_statement; 261, 262; 261, 270; 261, 275; 262, call; 262, 263; 262, 268; 263, attribute; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:os; 266, identifier:path; 267, identifier:isabs; 268, argument_list; 268, 269; 269, identifier:incl_path_raw; 270, block; 270, 271; 271, expression_statement; 271, 272; 272, assignment; 272, 273; 272, 274; 273, identifier:incl_path; 274, identifier:incl_path_raw; 275, else_clause; 275, 276; 275, 277; 276, comment; 277, block; 277, 278; 278, for_statement; 278, 279; 278, 280; 278, 283; 278, 308; 279, identifier:search_path; 280, attribute; 280, 281; 280, 282; 281, identifier:self; 282, identifier:search_paths; 283, block; 283, 284; 283, 296; 284, expression_statement; 284, 285; 285, assignment; 285, 286; 285, 287; 286, identifier:incl_path; 287, call; 287, 288; 287, 293; 288, attribute; 288, 289; 288, 292; 289, attribute; 289, 290; 289, 291; 290, identifier:os; 291, identifier:path; 292, identifier:join; 293, argument_list; 293, 294; 293, 295; 294, identifier:search_path; 295, identifier:incl_path_raw; 296, if_statement; 296, 297; 296, 305; 296, 306; 297, call; 297, 298; 297, 303; 298, attribute; 298, 299; 298, 302; 299, attribute; 299, 300; 299, 301; 300, identifier:os; 301, identifier:path; 302, identifier:isfile; 303, argument_list; 303, 304; 304, identifier:incl_path; 305, comment; 306, block; 306, 307; 307, break_statement; 308, else_clause; 308, 309; 308, 310; 309, comment; 310, block; 310, 311; 311, expression_statement; 311, 312; 312, assignment; 312, 313; 312, 314; 313, identifier:incl_path; 314, call; 314, 315; 314, 320; 315, attribute; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:os; 318, identifier:path; 319, identifier:join; 320, argument_list; 320, 321; 320, 331; 321, call; 321, 322; 321, 327; 322, attribute; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:os; 325, identifier:path; 326, identifier:dirname; 327, argument_list; 327, 328; 328, attribute; 328, 329; 328, 330; 329, identifier:self; 330, identifier:path; 331, identifier:incl_path_raw; 332, if_statement; 332, 333; 332, 342; 333, not_operator; 333, 334; 334, call; 334, 335; 334, 340; 335, attribute; 335, 336; 335, 339; 336, attribute; 336, 337; 336, 338; 337, identifier:os; 338, identifier:path; 339, identifier:isfile; 340, argument_list; 340, 341; 341, identifier:incl_path; 342, block; 342, 343; 343, expression_statement; 343, 344; 344, call; 344, 345; 344, 352; 345, attribute; 345, 346; 345, 351; 346, attribute; 346, 347; 346, 350; 347, attribute; 347, 348; 347, 349; 348, identifier:self; 349, identifier:env; 350, identifier:msg; 351, identifier:fatal; 352, argument_list; 352, 353; 352, 356; 353, binary_operator:%; 353, 354; 353, 355; 354, string:"Could not find '%s' in include search paths"; 355, identifier:incl_path_raw; 356, call; 356, 357; 356, 360; 357, attribute; 357, 358; 357, 359; 358, identifier:messages; 359, identifier:SourceRef; 360, argument_list; 360, 361; 360, 362; 360, 363; 361, identifier:path_start; 362, identifier:end; 363, keyword_argument; 363, 364; 363, 365; 364, identifier:filename; 365, attribute; 365, 366; 365, 367; 366, identifier:self; 367, identifier:path; 368, comment; 369, expression_statement; 369, 370; 370, assignment; 370, 371; 370, 372; 371, identifier:incl_ref; 372, attribute; 372, 373; 372, 374; 373, identifier:self; 374, identifier:incl_ref; 375, while_statement; 375, 376; 375, 377; 376, identifier:incl_ref; 377, block; 377, 378; 377, 416; 378, if_statement; 378, 379; 378, 390; 379, call; 379, 380; 379, 385; 380, attribute; 380, 381; 380, 384; 381, attribute; 381, 382; 381, 383; 382, identifier:os; 383, identifier:path; 384, identifier:samefile; 385, argument_list; 385, 386; 385, 387; 386, identifier:incl_path; 387, attribute; 387, 388; 387, 389; 388, identifier:incl_ref; 389, identifier:path; 390, block; 390, 391; 391, expression_statement; 391, 392; 392, call; 392, 393; 392, 400; 393, attribute; 393, 394; 393, 399; 394, attribute; 394, 395; 394, 398; 395, attribute; 395, 396; 395, 397; 396, identifier:self; 397, identifier:env; 398, identifier:msg; 399, identifier:fatal; 400, argument_list; 400, 401; 400, 404; 401, binary_operator:%; 401, 402; 401, 403; 402, string:"Include of '%s' results in a circular reference"; 403, identifier:incl_path_raw; 404, call; 404, 405; 404, 408; 405, attribute; 405, 406; 405, 407; 406, identifier:messages; 407, identifier:SourceRef; 408, argument_list; 408, 409; 408, 410; 408, 411; 409, identifier:path_start; 410, identifier:end; 411, keyword_argument; 411, 412; 411, 413; 412, identifier:filename; 413, attribute; 413, 414; 413, 415; 414, identifier:self; 415, identifier:path; 416, expression_statement; 416, 417; 417, assignment; 417, 418; 417, 419; 418, identifier:incl_ref; 419, attribute; 419, 420; 419, 421; 420, identifier:incl_ref; 421, identifier:parent; 422, return_statement; 422, 423; 423, tuple; 423, 424; 423, 425; 424, identifier:end; 425, identifier:incl_path
def parse_include(self, start): """ Extract include from text based on start position of token Returns ------- (end, incl_path) - end: last char in include - incl_path: Resolved path to include """ # Seek back to start of line i = start while i: if self.text[i] == '\n': i += 1 break i -= 1 line_start = i # check that there is no unexpected text before the include if not (self.text[line_start:start] == "" or self.text[line_start:start].isspace()): self.env.msg.fatal( "Unexpected text before include", messages.SourceRef(line_start, start-1, filename=self.path) ) # Capture include contents inc_regex = re.compile(r'`include\s+("([^\r\n]+)"|<([^\r\n]+)>)') m_inc = inc_regex.match(self.text, start) if m_inc is None: self.env.msg.fatal( "Invalid usage of include directive", messages.SourceRef(start, start+7, filename=self.path) ) incl_path_raw = m_inc.group(2) or m_inc.group(3) end = m_inc.end(0)-1 path_start = m_inc.start(1) #[^\r\n]*?\r?\n # Check that only comments follow tail_regex = re.compile(r'(?:[ \t]*/\*[^\r\n]*?\*/)*[ \t]*(?://[^\r\n]*?|/\*[^\r\n]*?)?\r?\n') if not tail_regex.match(self.text, end+1): tail_capture_regex = re.compile(r'[^\r\n]*?\r?\n') m = tail_capture_regex.match(self.text, end+1) self.env.msg.fatal( "Unexpected text after include", messages.SourceRef(end+1, m.end(0)-1, filename=self.path) ) # Resolve include path. if os.path.isabs(incl_path_raw): incl_path = incl_path_raw else: # Search include paths first. for search_path in self.search_paths: incl_path = os.path.join(search_path, incl_path_raw) if os.path.isfile(incl_path): # found match! break else: # Otherwise, assume it is relative to the current file incl_path = os.path.join(os.path.dirname(self.path), incl_path_raw) if not os.path.isfile(incl_path): self.env.msg.fatal( "Could not find '%s' in include search paths" % incl_path_raw, messages.SourceRef(path_start, end, filename=self.path) ) # Check if path has already been referenced before incl_ref = self.incl_ref while incl_ref: if os.path.samefile(incl_path, incl_ref.path): self.env.msg.fatal( "Include of '%s' results in a circular reference" % incl_path_raw, messages.SourceRef(path_start, end, filename=self.path) ) incl_ref = incl_ref.parent return(end, incl_path)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:get_paths; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:patterns; 5, default_parameter; 5, 6; 5, 7; 6, identifier:start_dirs; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:max_depth; 10, integer:1; 11, block; 11, 12; 11, 14; 11, 15; 11, 66; 11, 82; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, if_statement; 15, 16; 15, 25; 16, boolean_operator:and; 16, 17; 16, 23; 17, comparison_operator:==; 17, 18; 17, 22; 18, call; 18, 19; 18, 20; 19, identifier:len; 20, argument_list; 20, 21; 21, identifier:patterns; 22, integer:1; 23, not_operator; 23, 24; 24, identifier:start_dirs; 25, block; 25, 26; 25, 32; 25, 43; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:pattern; 29, subscript; 29, 30; 29, 31; 30, identifier:patterns; 31, integer:0; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:directory; 35, call; 35, 36; 35, 41; 36, attribute; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:os; 39, identifier:path; 40, identifier:dirname; 41, argument_list; 41, 42; 42, identifier:pattern; 43, if_statement; 43, 44; 43, 45; 44, identifier:directory; 45, block; 45, 46; 45, 58; 45, 62; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:patterns; 49, list:[os.path.basename(pattern)]; 49, 50; 50, call; 50, 51; 50, 56; 51, attribute; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:os; 54, identifier:path; 55, identifier:basename; 56, argument_list; 56, 57; 57, identifier:pattern; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:start_dirs; 61, identifier:directory; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:max_depth; 65, integer:1; 66, if_statement; 66, 67; 66, 73; 67, boolean_operator:or; 67, 68; 67, 70; 68, not_operator; 68, 69; 69, identifier:start_dirs; 70, comparison_operator:==; 70, 71; 70, 72; 71, identifier:start_dirs; 72, string:'.'; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:start_dirs; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:os; 80, identifier:getcwd; 81, argument_list; 82, for_statement; 82, 83; 82, 84; 82, 90; 83, identifier:start_dir; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:start_dirs; 87, identifier:split; 88, argument_list; 88, 89; 89, string:','; 90, block; 90, 91; 91, for_statement; 91, 92; 91, 96; 91, 102; 91, 103; 92, pattern_list; 92, 93; 92, 94; 92, 95; 93, identifier:root; 94, identifier:dirs; 95, identifier:files; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:os; 99, identifier:walk; 100, argument_list; 100, 101; 101, identifier:start_dir; 102, comment; 103, block; 103, 104; 103, 143; 103, 147; 103, 161; 104, if_statement; 104, 105; 104, 108; 105, comparison_operator:is; 105, 106; 105, 107; 106, identifier:max_depth; 107, None; 108, block; 108, 109; 108, 123; 108, 137; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:relpath; 112, call; 112, 113; 112, 118; 113, attribute; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:os; 116, identifier:path; 117, identifier:relpath; 118, argument_list; 118, 119; 118, 120; 119, identifier:root; 120, keyword_argument; 120, 121; 120, 122; 121, identifier:start; 122, identifier:start_dir; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:depth; 126, call; 126, 127; 126, 128; 127, identifier:len; 128, argument_list; 128, 129; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:relpath; 132, identifier:split; 133, argument_list; 133, 134; 134, attribute; 134, 135; 134, 136; 135, identifier:os; 136, identifier:sep; 137, if_statement; 137, 138; 137, 141; 138, comparison_operator:>; 138, 139; 138, 140; 139, identifier:depth; 140, identifier:max_depth; 141, block; 141, 142; 142, continue_statement; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:names; 146, list:[]; 147, for_statement; 147, 148; 147, 149; 147, 150; 148, identifier:pattern; 149, identifier:patterns; 150, block; 150, 151; 151, expression_statement; 151, 152; 152, augmented_assignment:+=; 152, 153; 152, 154; 153, identifier:names; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:fnmatch; 157, identifier:filter; 158, argument_list; 158, 159; 158, 160; 159, identifier:files; 160, identifier:pattern; 161, for_statement; 161, 162; 161, 163; 161, 164; 162, identifier:name; 163, identifier:names; 164, block; 164, 165; 164, 177; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:path; 168, call; 168, 169; 168, 174; 169, attribute; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:os; 172, identifier:path; 173, identifier:join; 174, argument_list; 174, 175; 174, 176; 175, identifier:root; 176, identifier:name; 177, expression_statement; 177, 178; 178, yield; 178, 179; 179, identifier:path
def get_paths(patterns, start_dirs=None, max_depth=1): """Retrieve files that match any of the patterns.""" # Shortcut: if there is only one pattern, make sure we process just that. if len(patterns) == 1 and not start_dirs: pattern = patterns[0] directory = os.path.dirname(pattern) if directory: patterns = [os.path.basename(pattern)] start_dirs = directory max_depth = 1 if not start_dirs or start_dirs == '.': start_dirs = os.getcwd() for start_dir in start_dirs.split(','): for root, dirs, files in os.walk(start_dir): # pylint: disable=W0612 if max_depth is not None: relpath = os.path.relpath(root, start=start_dir) depth = len(relpath.split(os.sep)) if depth > max_depth: continue names = [] for pattern in patterns: names += fnmatch.filter(files, pattern) for name in names: path = os.path.join(root, name) yield path
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 34; 2, function_name:edit_files; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 28; 3, 31; 4, identifier:patterns; 5, default_parameter; 5, 6; 5, 7; 6, identifier:expressions; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:functions; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:executables; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:start_dirs; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:max_depth; 19, integer:1; 20, default_parameter; 20, 21; 20, 22; 21, identifier:dry_run; 22, True; 23, default_parameter; 23, 24; 23, 25; 24, identifier:output; 25, attribute; 25, 26; 25, 27; 26, identifier:sys; 27, identifier:stdout; 28, default_parameter; 28, 29; 28, 30; 29, identifier:encoding; 30, None; 31, default_parameter; 31, 32; 31, 33; 32, identifier:newline; 33, None; 34, block; 34, 35; 34, 37; 34, 49; 34, 63; 34, 77; 34, 91; 34, 106; 34, 116; 34, 126; 34, 136; 34, 140; 34, 261; 35, expression_statement; 35, 36; 36, comment; 37, if_statement; 37, 38; 37, 43; 38, not_operator; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:is_list; 41, argument_list; 41, 42; 42, identifier:patterns; 43, block; 43, 44; 44, raise_statement; 44, 45; 45, call; 45, 46; 45, 47; 46, identifier:TypeError; 47, argument_list; 47, 48; 48, string:"patterns should be a list"; 49, if_statement; 49, 50; 49, 57; 50, boolean_operator:and; 50, 51; 50, 52; 51, identifier:expressions; 52, not_operator; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:is_list; 55, argument_list; 55, 56; 56, identifier:expressions; 57, block; 57, 58; 58, raise_statement; 58, 59; 59, call; 59, 60; 59, 61; 60, identifier:TypeError; 61, argument_list; 61, 62; 62, string:"expressions should be a list of exec expressions"; 63, if_statement; 63, 64; 63, 71; 64, boolean_operator:and; 64, 65; 64, 66; 65, identifier:functions; 66, not_operator; 66, 67; 67, call; 67, 68; 67, 69; 68, identifier:is_list; 69, argument_list; 69, 70; 70, identifier:functions; 71, block; 71, 72; 72, raise_statement; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:TypeError; 75, argument_list; 75, 76; 76, string:"functions should be a list of functions"; 77, if_statement; 77, 78; 77, 85; 78, boolean_operator:and; 78, 79; 78, 80; 79, identifier:executables; 80, not_operator; 80, 81; 81, call; 81, 82; 81, 83; 82, identifier:is_list; 83, argument_list; 83, 84; 84, identifier:executables; 85, block; 85, 86; 86, raise_statement; 86, 87; 87, call; 87, 88; 87, 89; 88, identifier:TypeError; 89, argument_list; 89, 90; 90, string:"executables should be a list of program names"; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:editor; 94, call; 94, 95; 94, 96; 95, identifier:MassEdit; 96, argument_list; 96, 97; 96, 100; 96, 103; 97, keyword_argument; 97, 98; 97, 99; 98, identifier:dry_run; 99, identifier:dry_run; 100, keyword_argument; 100, 101; 100, 102; 101, identifier:encoding; 102, identifier:encoding; 103, keyword_argument; 103, 104; 103, 105; 104, identifier:newline; 105, identifier:newline; 106, if_statement; 106, 107; 106, 108; 107, identifier:expressions; 108, block; 108, 109; 109, expression_statement; 109, 110; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:editor; 113, identifier:set_code_exprs; 114, argument_list; 114, 115; 115, identifier:expressions; 116, if_statement; 116, 117; 116, 118; 117, identifier:functions; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:editor; 123, identifier:set_functions; 124, argument_list; 124, 125; 125, identifier:functions; 126, if_statement; 126, 127; 126, 128; 127, identifier:executables; 128, block; 128, 129; 129, expression_statement; 129, 130; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:editor; 133, identifier:set_executables; 134, argument_list; 134, 135; 135, identifier:executables; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:processed_paths; 139, list:[]; 140, for_statement; 140, 141; 140, 142; 140, 152; 141, identifier:path; 142, call; 142, 143; 142, 144; 143, identifier:get_paths; 144, argument_list; 144, 145; 144, 146; 144, 149; 145, identifier:patterns; 146, keyword_argument; 146, 147; 146, 148; 147, identifier:start_dirs; 148, identifier:start_dirs; 149, keyword_argument; 149, 150; 149, 151; 150, identifier:max_depth; 151, identifier:max_depth; 152, block; 152, 153; 152, 247; 153, try_statement; 153, 154; 153, 231; 154, block; 154, 155; 154, 167; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:diffs; 158, call; 158, 159; 158, 160; 159, identifier:list; 160, argument_list; 160, 161; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:editor; 164, identifier:edit_file; 165, argument_list; 165, 166; 166, identifier:path; 167, if_statement; 167, 168; 167, 169; 167, 170; 168, identifier:dry_run; 169, comment; 170, block; 170, 171; 170, 180; 170, 185; 170, 186; 170, 187; 170, 188; 170, 194; 170, 224; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:diff; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, string:""; 177, identifier:join; 178, argument_list; 178, 179; 179, identifier:diffs; 180, if_statement; 180, 181; 180, 183; 181, not_operator; 181, 182; 182, identifier:diff; 183, block; 183, 184; 184, continue_statement; 185, comment; 186, comment; 187, comment; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:encoding; 191, attribute; 191, 192; 191, 193; 192, identifier:output; 193, identifier:encoding; 194, if_statement; 194, 195; 194, 196; 195, identifier:encoding; 196, block; 196, 197; 196, 211; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:bytes_diff; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:diff; 203, identifier:encode; 204, argument_list; 204, 205; 204, 208; 205, keyword_argument; 205, 206; 205, 207; 206, identifier:encoding; 207, identifier:encoding; 208, keyword_argument; 208, 209; 208, 210; 209, identifier:errors; 210, string:'ignore'; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:diff; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:bytes_diff; 217, identifier:decode; 218, argument_list; 218, 219; 219, keyword_argument; 219, 220; 219, 221; 220, identifier:encoding; 221, attribute; 221, 222; 221, 223; 222, identifier:output; 223, identifier:encoding; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:output; 228, identifier:write; 229, argument_list; 229, 230; 230, identifier:diff; 231, except_clause; 231, 232; 231, 236; 232, as_pattern; 232, 233; 232, 234; 233, identifier:UnicodeDecodeError; 234, as_pattern_target; 234, 235; 235, identifier:err; 236, block; 236, 237; 236, 246; 237, expression_statement; 237, 238; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:log; 241, identifier:error; 242, argument_list; 242, 243; 242, 244; 242, 245; 243, string:"failed to process %s: %s"; 244, identifier:path; 245, identifier:err; 246, continue_statement; 247, expression_statement; 247, 248; 248, call; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:processed_paths; 251, identifier:append; 252, argument_list; 252, 253; 253, call; 253, 254; 253, 259; 254, attribute; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:os; 257, identifier:path; 258, identifier:abspath; 259, argument_list; 259, 260; 260, identifier:path; 261, return_statement; 261, 262; 262, identifier:processed_paths
def edit_files(patterns, expressions=None, functions=None, executables=None, start_dirs=None, max_depth=1, dry_run=True, output=sys.stdout, encoding=None, newline=None): """Process patterns with MassEdit. Arguments: patterns: file pattern to identify the files to be processed. expressions: single python expression to be applied line by line. functions: functions to process files contents. executables: os executables to execute on the argument files. Keyword arguments: max_depth: maximum recursion level when looking for file matches. start_dirs: workspace(ies) where to start the file search. dry_run: only display differences if True. Save modified file otherwise. output: handle where the output should be redirected. Return: list of files processed. """ if not is_list(patterns): raise TypeError("patterns should be a list") if expressions and not is_list(expressions): raise TypeError("expressions should be a list of exec expressions") if functions and not is_list(functions): raise TypeError("functions should be a list of functions") if executables and not is_list(executables): raise TypeError("executables should be a list of program names") editor = MassEdit(dry_run=dry_run, encoding=encoding, newline=newline) if expressions: editor.set_code_exprs(expressions) if functions: editor.set_functions(functions) if executables: editor.set_executables(executables) processed_paths = [] for path in get_paths(patterns, start_dirs=start_dirs, max_depth=max_depth): try: diffs = list(editor.edit_file(path)) if dry_run: # At this point, encoding is the input encoding. diff = "".join(diffs) if not diff: continue # The encoding of the target output may not match the input # encoding. If it's defined, we round trip the diff text # to bytes and back to silence any conversion errors. encoding = output.encoding if encoding: bytes_diff = diff.encode(encoding=encoding, errors='ignore') diff = bytes_diff.decode(encoding=output.encoding) output.write(diff) except UnicodeDecodeError as err: log.error("failed to process %s: %s", path, err) continue processed_paths.append(os.path.abspath(path)) return processed_paths
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:detect_events; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:dat; 5, identifier:method; 6, default_parameter; 6, 7; 6, 8; 7, identifier:value; 8, None; 9, block; 9, 10; 9, 12; 9, 204; 9, 247; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 20; 13, boolean_operator:or; 13, 14; 13, 17; 14, comparison_operator:in; 14, 15; 14, 16; 15, string:'thresh'; 16, identifier:method; 17, comparison_operator:==; 17, 18; 17, 19; 18, string:'custom'; 19, identifier:method; 20, block; 20, 21; 20, 39; 20, 57; 20, 93; 20, 105; 20, 112; 20, 158; 21, if_statement; 21, 22; 21, 25; 22, comparison_operator:==; 22, 23; 22, 24; 23, identifier:method; 24, string:'above_thresh'; 25, block; 25, 26; 25, 32; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:above_det; 29, comparison_operator:>=; 29, 30; 29, 31; 30, identifier:dat; 31, identifier:value; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:detected; 35, call; 35, 36; 35, 37; 36, identifier:_detect_start_end; 37, argument_list; 37, 38; 38, identifier:above_det; 39, if_statement; 39, 40; 39, 43; 40, comparison_operator:==; 40, 41; 40, 42; 41, identifier:method; 42, string:'below_thresh'; 43, block; 43, 44; 43, 50; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:below_det; 47, comparison_operator:<; 47, 48; 47, 49; 48, identifier:dat; 49, identifier:value; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:detected; 53, call; 53, 54; 53, 55; 54, identifier:_detect_start_end; 55, argument_list; 55, 56; 56, identifier:below_det; 57, if_statement; 57, 58; 57, 61; 58, comparison_operator:==; 58, 59; 58, 60; 59, identifier:method; 60, string:'between_thresh'; 61, block; 61, 62; 61, 70; 61, 78; 61, 86; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:above_det; 65, comparison_operator:>=; 65, 66; 65, 67; 66, identifier:dat; 67, subscript; 67, 68; 67, 69; 68, identifier:value; 69, integer:0; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:below_det; 73, comparison_operator:<; 73, 74; 73, 75; 74, identifier:dat; 75, subscript; 75, 76; 75, 77; 76, identifier:value; 77, integer:1; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:between_det; 81, call; 81, 82; 81, 83; 82, identifier:logical_and; 83, argument_list; 83, 84; 83, 85; 84, identifier:above_det; 85, identifier:below_det; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:detected; 89, call; 89, 90; 89, 91; 90, identifier:_detect_start_end; 91, argument_list; 91, 92; 92, identifier:between_det; 93, if_statement; 93, 94; 93, 97; 94, comparison_operator:==; 94, 95; 94, 96; 95, identifier:method; 96, string:'custom'; 97, block; 97, 98; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:detected; 101, call; 101, 102; 101, 103; 102, identifier:_detect_start_end; 103, argument_list; 103, 104; 104, identifier:dat; 105, if_statement; 105, 106; 105, 109; 106, comparison_operator:is; 106, 107; 106, 108; 107, identifier:detected; 108, None; 109, block; 109, 110; 110, return_statement; 110, 111; 111, None; 112, if_statement; 112, 113; 112, 118; 112, 119; 113, comparison_operator:in; 113, 114; 113, 115; 114, identifier:method; 115, list:['above_thresh', 'custom']; 115, 116; 115, 117; 116, string:'above_thresh'; 117, string:'custom'; 118, comment; 119, block; 119, 120; 119, 132; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:detected; 123, call; 123, 124; 123, 125; 124, identifier:insert; 125, argument_list; 125, 126; 125, 127; 125, 128; 125, 129; 126, identifier:detected; 127, integer:1; 128, integer:0; 129, keyword_argument; 129, 130; 129, 131; 130, identifier:axis; 131, integer:1; 132, for_statement; 132, 133; 132, 134; 132, 135; 133, identifier:i; 134, identifier:detected; 135, block; 135, 136; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 141; 138, subscript; 138, 139; 138, 140; 139, identifier:i; 140, integer:1; 141, binary_operator:+; 141, 142; 141, 145; 142, subscript; 142, 143; 142, 144; 143, identifier:i; 144, integer:0; 145, call; 145, 146; 145, 147; 146, identifier:argmax; 147, argument_list; 147, 148; 148, subscript; 148, 149; 148, 150; 149, identifier:dat; 150, slice; 150, 151; 150, 154; 150, 155; 151, subscript; 151, 152; 151, 153; 152, identifier:i; 153, integer:0; 154, colon; 155, subscript; 155, 156; 155, 157; 156, identifier:i; 157, integer:2; 158, if_statement; 158, 159; 158, 164; 158, 165; 159, comparison_operator:in; 159, 160; 159, 161; 160, identifier:method; 161, list:['below_thresh', 'between_thresh']; 161, 162; 161, 163; 162, string:'below_thresh'; 163, string:'between_thresh'; 164, comment; 165, block; 165, 166; 165, 178; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:detected; 169, call; 169, 170; 169, 171; 170, identifier:insert; 171, argument_list; 171, 172; 171, 173; 171, 174; 171, 175; 172, identifier:detected; 173, integer:1; 174, integer:0; 175, keyword_argument; 175, 176; 175, 177; 176, identifier:axis; 177, integer:1; 178, for_statement; 178, 179; 178, 180; 178, 181; 179, identifier:i; 180, identifier:detected; 181, block; 181, 182; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 187; 184, subscript; 184, 185; 184, 186; 185, identifier:i; 186, integer:1; 187, binary_operator:+; 187, 188; 187, 191; 188, subscript; 188, 189; 188, 190; 189, identifier:i; 190, integer:0; 191, call; 191, 192; 191, 193; 192, identifier:argmin; 193, argument_list; 193, 194; 194, subscript; 194, 195; 194, 196; 195, identifier:dat; 196, slice; 196, 197; 196, 200; 196, 201; 197, subscript; 197, 198; 197, 199; 198, identifier:i; 199, integer:0; 200, colon; 201, subscript; 201, 202; 201, 203; 202, identifier:i; 203, integer:2; 204, if_statement; 204, 205; 204, 208; 205, comparison_operator:==; 205, 206; 205, 207; 206, identifier:method; 207, string:'maxima'; 208, block; 208, 209; 208, 218; 208, 230; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:peaks; 212, subscript; 212, 213; 212, 217; 213, call; 213, 214; 213, 215; 214, identifier:argrelmax; 215, argument_list; 215, 216; 216, identifier:dat; 217, integer:0; 218, expression_statement; 218, 219; 219, assignment; 219, 220; 219, 221; 220, identifier:detected; 221, attribute; 221, 222; 221, 229; 222, call; 222, 223; 222, 224; 223, identifier:vstack; 224, argument_list; 224, 225; 225, tuple; 225, 226; 225, 227; 225, 228; 226, identifier:peaks; 227, identifier:peaks; 228, identifier:peaks; 229, identifier:T; 230, if_statement; 230, 231; 230, 234; 231, comparison_operator:is; 231, 232; 231, 233; 232, identifier:value; 233, None; 234, block; 234, 235; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 238; 237, identifier:detected; 238, subscript; 238, 239; 238, 240; 238, 245; 239, identifier:detected; 240, comparison_operator:>; 240, 241; 240, 244; 241, subscript; 241, 242; 241, 243; 242, identifier:dat; 243, identifier:peaks; 244, identifier:value; 245, slice; 245, 246; 246, colon; 247, return_statement; 247, 248; 248, identifier:detected
def detect_events(dat, method, value=None): """Detect events using 'above_thresh', 'below_thresh' or 'maxima' method. Parameters ---------- dat : ndarray (dtype='float') vector with the data after transformation method : str 'above_thresh', 'below_thresh' or 'maxima' value : float or tuple of float for 'above_thresh' or 'below_thresh', it's the value of threshold for the event detection for 'between_thresh', it's the lower and upper threshold as tuple for 'maxima', it's the distance in s from the peak to find a minimum Returns ------- ndarray (dtype='int') N x 3 matrix with start, peak, end samples """ if 'thresh' in method or 'custom' == method: if method == 'above_thresh': above_det = dat >= value detected = _detect_start_end(above_det) if method == 'below_thresh': below_det = dat < value detected = _detect_start_end(below_det) if method == 'between_thresh': above_det = dat >= value[0] below_det = dat < value[1] between_det = logical_and(above_det, below_det) detected = _detect_start_end(between_det) if method == 'custom': detected = _detect_start_end(dat) if detected is None: return None if method in ['above_thresh', 'custom']: # add the location of the peak in the middle detected = insert(detected, 1, 0, axis=1) for i in detected: i[1] = i[0] + argmax(dat[i[0]:i[2]]) if method in ['below_thresh', 'between_thresh']: # add the location of the trough in the middle detected = insert(detected, 1, 0, axis=1) for i in detected: i[1] = i[0] + argmin(dat[i[0]:i[2]]) if method == 'maxima': peaks = argrelmax(dat)[0] detected = vstack((peaks, peaks, peaks)).T if value is not None: detected = detected[dat[peaks] > value, :] return detected
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:merge_close; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:events; 5, identifier:min_interval; 6, default_parameter; 6, 7; 6, 8; 7, identifier:merge_to_longer; 8, False; 9, block; 9, 10; 9, 12; 9, 18; 9, 22; 9, 144; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:half_iv; 15, binary_operator:/; 15, 16; 15, 17; 16, identifier:min_interval; 17, integer:2; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:merged; 21, list:[]; 22, for_statement; 22, 23; 22, 24; 22, 25; 23, identifier:higher; 24, identifier:events; 25, block; 25, 26; 26, if_statement; 26, 27; 26, 29; 26, 37; 27, not_operator; 27, 28; 28, identifier:merged; 29, block; 29, 30; 30, expression_statement; 30, 31; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:merged; 34, identifier:append; 35, argument_list; 35, 36; 36, identifier:higher; 37, else_clause; 37, 38; 38, block; 38, 39; 38, 46; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:lower; 42, subscript; 42, 43; 42, 44; 43, identifier:merged; 44, unary_operator:-; 44, 45; 45, integer:1; 46, if_statement; 46, 47; 46, 58; 46, 135; 47, comparison_operator:<=; 47, 48; 47, 53; 48, binary_operator:-; 48, 49; 48, 52; 49, subscript; 49, 50; 49, 51; 50, identifier:higher; 51, string:'start'; 52, identifier:half_iv; 53, binary_operator:+; 53, 54; 53, 57; 54, subscript; 54, 55; 54, 56; 55, identifier:lower; 56, string:'end'; 57, identifier:half_iv; 58, block; 58, 59; 59, if_statement; 59, 60; 59, 78; 59, 108; 60, boolean_operator:and; 60, 61; 60, 62; 61, identifier:merge_to_longer; 62, parenthesized_expression; 62, 63; 63, comparison_operator:>; 63, 64; 63, 71; 64, binary_operator:-; 64, 65; 64, 68; 65, subscript; 65, 66; 65, 67; 66, identifier:higher; 67, string:'end'; 68, subscript; 68, 69; 68, 70; 69, identifier:higher; 70, string:'start'; 71, binary_operator:-; 71, 72; 71, 75; 72, subscript; 72, 73; 72, 74; 73, identifier:lower; 74, string:'end'; 75, subscript; 75, 76; 75, 77; 76, identifier:lower; 77, string:'start'; 78, block; 78, 79; 78, 91; 78, 101; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:start; 82, call; 82, 83; 82, 84; 83, identifier:min; 84, argument_list; 84, 85; 84, 88; 85, subscript; 85, 86; 85, 87; 86, identifier:lower; 87, string:'start'; 88, subscript; 88, 89; 88, 90; 89, identifier:higher; 90, string:'start'; 91, expression_statement; 91, 92; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:higher; 95, identifier:update; 96, argument_list; 96, 97; 97, dictionary; 97, 98; 98, pair; 98, 99; 98, 100; 99, string:'start'; 100, identifier:start; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 107; 103, subscript; 103, 104; 103, 105; 104, identifier:merged; 105, unary_operator:-; 105, 106; 106, integer:1; 107, identifier:higher; 108, else_clause; 108, 109; 109, block; 109, 110; 109, 122; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:end; 113, call; 113, 114; 113, 115; 114, identifier:max; 115, argument_list; 115, 116; 115, 119; 116, subscript; 116, 117; 116, 118; 117, identifier:lower; 118, string:'end'; 119, subscript; 119, 120; 119, 121; 120, identifier:higher; 121, string:'end'; 122, expression_statement; 122, 123; 123, call; 123, 124; 123, 130; 124, attribute; 124, 125; 124, 129; 125, subscript; 125, 126; 125, 127; 126, identifier:merged; 127, unary_operator:-; 127, 128; 128, integer:1; 129, identifier:update; 130, argument_list; 130, 131; 131, dictionary; 131, 132; 132, pair; 132, 133; 132, 134; 133, string:'end'; 134, identifier:end; 135, else_clause; 135, 136; 136, block; 136, 137; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:merged; 141, identifier:append; 142, argument_list; 142, 143; 143, identifier:higher; 144, return_statement; 144, 145; 145, identifier:merged
def merge_close(events, min_interval, merge_to_longer=False): """Merge events that are separated by a less than a minimum interval. Parameters ---------- events : list of dict events with 'start' and 'end' times, from one or several channels. **Events must be sorted by their start time.** min_interval : float minimum delay between consecutive events, in seconds merge_to_longer : bool (default: False) If True, info (chan, peak, etc.) from the longer of the 2 events is kept. Otherwise, info from the earlier onset spindle is kept. Returns ------- list of dict original events list with close events merged. """ half_iv = min_interval / 2 merged = [] for higher in events: if not merged: merged.append(higher) else: lower = merged[-1] if higher['start'] - half_iv <= lower['end'] + half_iv: if merge_to_longer and (higher['end'] - higher['start'] > lower['end'] - lower['start']): start = min(lower['start'], higher['start']) higher.update({'start': start}) merged[-1] = higher else: end = max(lower['end'], higher['end']) merged[-1].update({'end': end}) else: merged.append(higher) return merged
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_remove_duplicate; 3, parameters; 3, 4; 3, 5; 4, identifier:old_events; 5, identifier:dat; 6, block; 6, 7; 6, 9; 6, 19; 6, 44; 6, 48; 6, 49; 6, 62; 6, 78; 6, 101; 6, 105; 6, 109; 6, 188; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:diff_events; 12, call; 12, 13; 12, 14; 13, identifier:diff; 14, argument_list; 14, 15; 14, 16; 15, identifier:old_events; 16, keyword_argument; 16, 17; 16, 18; 17, identifier:axis; 18, integer:0; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:dupl; 22, subscript; 22, 23; 22, 43; 23, call; 23, 24; 23, 25; 24, identifier:where; 25, argument_list; 25, 26; 26, binary_operator:&; 26, 27; 26, 35; 27, parenthesized_expression; 27, 28; 28, comparison_operator:==; 28, 29; 28, 34; 29, subscript; 29, 30; 29, 31; 29, 33; 30, identifier:diff_events; 31, slice; 31, 32; 32, colon; 33, integer:0; 34, integer:0; 35, parenthesized_expression; 35, 36; 36, comparison_operator:==; 36, 37; 36, 42; 37, subscript; 37, 38; 37, 39; 37, 41; 38, identifier:diff_events; 39, slice; 39, 40; 40, colon; 41, integer:2; 42, integer:0; 43, integer:0; 44, expression_statement; 44, 45; 45, augmented_assignment:+=; 45, 46; 45, 47; 46, identifier:dupl; 47, integer:1; 48, comment; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:n_nondupl_events; 52, binary_operator:-; 52, 53; 52, 58; 53, subscript; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:old_events; 56, identifier:shape; 57, integer:0; 58, call; 58, 59; 58, 60; 59, identifier:len; 60, argument_list; 60, 61; 61, identifier:dupl; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:new_events; 65, call; 65, 66; 65, 67; 66, identifier:zeros; 67, argument_list; 67, 68; 67, 75; 68, tuple; 68, 69; 68, 70; 69, identifier:n_nondupl_events; 70, subscript; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:old_events; 73, identifier:shape; 74, integer:1; 75, keyword_argument; 75, 76; 75, 77; 76, identifier:dtype; 77, string:'int'; 78, if_statement; 78, 79; 78, 83; 79, call; 79, 80; 79, 81; 80, identifier:len; 81, argument_list; 81, 82; 82, identifier:dupl; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:lg; 88, identifier:debug; 89, argument_list; 89, 90; 90, binary_operator:+; 90, 91; 90, 100; 91, binary_operator:+; 91, 92; 91, 93; 92, string:'Removing '; 93, call; 93, 94; 93, 95; 94, identifier:str; 95, argument_list; 95, 96; 96, call; 96, 97; 96, 98; 97, identifier:len; 98, argument_list; 98, 99; 99, identifier:dupl; 100, string:' duplicate events'; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:i; 104, integer:0; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:indices; 108, list:[]; 109, for_statement; 109, 110; 109, 113; 109, 117; 110, pattern_list; 110, 111; 110, 112; 111, identifier:i_old; 112, identifier:one_old_event; 113, call; 113, 114; 113, 115; 114, identifier:enumerate; 115, argument_list; 115, 116; 116, identifier:old_events; 117, block; 117, 118; 118, if_statement; 118, 119; 118, 122; 118, 142; 119, comparison_operator:not; 119, 120; 119, 121; 120, identifier:i_old; 121, identifier:dupl; 122, block; 122, 123; 122, 131; 122, 135; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 130; 125, subscript; 125, 126; 125, 127; 125, 128; 126, identifier:new_events; 127, identifier:i; 128, slice; 128, 129; 129, colon; 130, identifier:one_old_event; 131, expression_statement; 131, 132; 132, augmented_assignment:+=; 132, 133; 132, 134; 133, identifier:i; 134, integer:1; 135, expression_statement; 135, 136; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:indices; 139, identifier:append; 140, argument_list; 140, 141; 141, identifier:i_old; 142, else_clause; 142, 143; 143, block; 143, 144; 143, 153; 143, 159; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:peak_0; 147, subscript; 147, 148; 147, 149; 147, 152; 148, identifier:new_events; 149, binary_operator:-; 149, 150; 149, 151; 150, identifier:i; 151, integer:1; 152, integer:1; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:peak_1; 156, subscript; 156, 157; 156, 158; 157, identifier:one_old_event; 158, integer:1; 159, if_statement; 159, 160; 159, 167; 159, 177; 160, comparison_operator:>=; 160, 161; 160, 164; 161, subscript; 161, 162; 161, 163; 162, identifier:dat; 163, identifier:peak_0; 164, subscript; 164, 165; 164, 166; 165, identifier:dat; 166, identifier:peak_1; 167, block; 167, 168; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 176; 170, subscript; 170, 171; 170, 172; 170, 175; 171, identifier:new_events; 172, binary_operator:-; 172, 173; 172, 174; 173, identifier:i; 174, integer:1; 175, integer:1; 176, identifier:peak_0; 177, else_clause; 177, 178; 178, block; 178, 179; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 187; 181, subscript; 181, 182; 181, 183; 181, 186; 182, identifier:new_events; 183, binary_operator:-; 183, 184; 183, 185; 184, identifier:i; 185, integer:1; 186, integer:1; 187, identifier:peak_1; 188, return_statement; 188, 189; 189, expression_list; 189, 190; 189, 191; 190, identifier:indices; 191, identifier:new_events
def _remove_duplicate(old_events, dat): """Remove duplicates from the events. Parameters ---------- old_events : ndarray (dtype='int') N x 3 matrix with start, peak, end samples dat : ndarray (dtype='float') vector with the data after detection-transformation (to compute peak) Returns ------- ndarray (dtype='int') vector of indices of the events to keep ndarray (dtype='int') N x 3 matrix with start, peak, end samples Notes ----- old_events is assumed to be sorted. It only checks for the start time and end time. When two (or more) events have the same start time and the same end time, then it takes the largest peak. There is no tolerance, indices need to be identical. """ diff_events = diff(old_events, axis=0) dupl = where((diff_events[:, 0] == 0) & (diff_events[:, 2] == 0))[0] dupl += 1 # more convenient, it copies old_event first and then compares n_nondupl_events = old_events.shape[0] - len(dupl) new_events = zeros((n_nondupl_events, old_events.shape[1]), dtype='int') if len(dupl): lg.debug('Removing ' + str(len(dupl)) + ' duplicate events') i = 0 indices = [] for i_old, one_old_event in enumerate(old_events): if i_old not in dupl: new_events[i, :] = one_old_event i += 1 indices.append(i_old) else: peak_0 = new_events[i - 1, 1] peak_1 = one_old_event[1] if dat[peak_0] >= dat[peak_1]: new_events[i - 1, 1] = peak_0 else: new_events[i - 1, 1] = peak_1 return indices, new_events
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:math; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:data; 5, default_parameter; 5, 6; 5, 7; 6, identifier:operator; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:operator_name; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:axis; 13, None; 14, block; 14, 15; 14, 17; 14, 33; 14, 34; 14, 76; 14, 77; 14, 88; 14, 92; 14, 197; 14, 205; 14, 219; 14, 223; 14, 428; 15, expression_statement; 15, 16; 16, comment; 17, if_statement; 17, 18; 17, 25; 18, boolean_operator:and; 18, 19; 18, 22; 19, comparison_operator:is; 19, 20; 19, 21; 20, identifier:operator; 21, None; 22, comparison_operator:is; 22, 23; 22, 24; 23, identifier:operator_name; 24, None; 25, block; 25, 26; 26, raise_statement; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:TypeError; 29, argument_list; 29, 30; 30, concatenated_string; 30, 31; 30, 32; 31, string:'Parameters "operator" and "operator_name" are '; 32, string:'mutually exclusive'; 33, comment; 34, if_statement; 34, 35; 34, 38; 35, comparison_operator:is; 35, 36; 35, 37; 36, identifier:operator_name; 37, None; 38, block; 38, 39; 38, 51; 38, 55; 38, 69; 39, if_statement; 39, 40; 39, 45; 40, call; 40, 41; 40, 42; 41, identifier:isinstance; 42, argument_list; 42, 43; 42, 44; 43, identifier:operator_name; 44, identifier:str; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:operator_name; 49, tuple; 49, 50; 50, identifier:operator_name; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:operators; 54, list:[]; 55, for_statement; 55, 56; 55, 57; 55, 58; 56, identifier:one_operator_name; 57, identifier:operator_name; 58, block; 58, 59; 59, expression_statement; 59, 60; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:operators; 63, identifier:append; 64, argument_list; 64, 65; 65, call; 65, 66; 65, 67; 66, identifier:eval; 67, argument_list; 67, 68; 68, identifier:one_operator_name; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:operator; 72, call; 72, 73; 72, 74; 73, identifier:tuple; 74, argument_list; 74, 75; 75, identifier:operators; 76, comment; 77, if_statement; 77, 78; 77, 82; 78, call; 78, 79; 78, 80; 79, identifier:callable; 80, argument_list; 80, 81; 81, identifier:operator; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:operator; 86, tuple; 86, 87; 87, identifier:operator; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:operations; 91, list:[]; 92, for_statement; 92, 93; 92, 94; 92, 95; 93, identifier:one_operator; 94, identifier:operator; 95, block; 95, 96; 95, 100; 95, 104; 95, 176; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:on_axis; 99, False; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:keepdims; 103, True; 104, try_statement; 104, 105; 104, 115; 104, 134; 105, block; 105, 106; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:args; 109, attribute; 109, 110; 109, 114; 110, call; 110, 111; 110, 112; 111, identifier:getfullargspec; 112, argument_list; 112, 113; 113, identifier:one_operator; 114, identifier:args; 115, except_clause; 115, 116; 115, 117; 116, identifier:TypeError; 117, block; 117, 118; 118, expression_statement; 118, 119; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:lg; 122, identifier:debug; 123, argument_list; 123, 124; 124, binary_operator:+; 124, 125; 124, 131; 125, binary_operator:+; 125, 126; 125, 127; 126, string:'func '; 127, call; 127, 128; 127, 129; 128, identifier:str; 129, argument_list; 129, 130; 130, identifier:one_operator; 131, concatenated_string; 131, 132; 131, 133; 132, string:' is not a Python '; 133, string:'function'; 134, else_clause; 134, 135; 135, block; 135, 136; 135, 163; 136, if_statement; 136, 137; 136, 140; 137, comparison_operator:in; 137, 138; 137, 139; 138, string:'axis'; 139, identifier:args; 140, block; 140, 141; 140, 145; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:on_axis; 144, True; 145, if_statement; 145, 146; 145, 149; 146, comparison_operator:is; 146, 147; 146, 148; 147, identifier:axis; 148, None; 149, block; 149, 150; 150, raise_statement; 150, 151; 151, call; 151, 152; 151, 153; 152, identifier:TypeError; 153, argument_list; 153, 154; 154, binary_operator:+; 154, 155; 154, 162; 155, binary_operator:+; 155, 156; 155, 159; 156, concatenated_string; 156, 157; 156, 158; 157, string:'You need to specify an axis if you '; 158, string:'use '; 159, attribute; 159, 160; 159, 161; 160, identifier:one_operator; 161, identifier:__name__; 162, string:' (which applies to an axis)'; 163, if_statement; 163, 164; 163, 171; 164, boolean_operator:or; 164, 165; 164, 168; 165, comparison_operator:in; 165, 166; 165, 167; 166, string:'keepdims'; 167, identifier:args; 168, comparison_operator:in; 168, 169; 168, 170; 169, identifier:one_operator; 170, identifier:NOKEEPDIM; 171, block; 171, 172; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:keepdims; 175, False; 176, expression_statement; 176, 177; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:operations; 180, identifier:append; 181, argument_list; 181, 182; 182, dictionary; 182, 183; 182, 188; 182, 191; 182, 194; 183, pair; 183, 184; 183, 185; 184, string:'name'; 185, attribute; 185, 186; 185, 187; 186, identifier:one_operator; 187, identifier:__name__; 188, pair; 188, 189; 188, 190; 189, string:'func'; 190, identifier:one_operator; 191, pair; 191, 192; 191, 193; 192, string:'on_axis'; 193, identifier:on_axis; 194, pair; 194, 195; 194, 196; 195, string:'keepdims'; 196, identifier:keepdims; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:output; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:data; 203, identifier:_copy; 204, argument_list; 205, if_statement; 205, 206; 205, 209; 206, comparison_operator:is; 206, 207; 206, 208; 207, identifier:axis; 208, None; 209, block; 209, 210; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 213; 212, identifier:idx_axis; 213, call; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:data; 216, identifier:index_of; 217, argument_list; 217, 218; 218, identifier:axis; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 222; 221, identifier:first_op; 222, True; 223, for_statement; 223, 224; 223, 225; 223, 226; 223, 227; 224, identifier:op; 225, identifier:operations; 226, comment; 227, block; 227, 228; 227, 234; 227, 255; 227, 408; 227, 412; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:func; 231, subscript; 231, 232; 231, 233; 232, identifier:op; 233, string:'func'; 234, if_statement; 234, 235; 234, 238; 235, comparison_operator:==; 235, 236; 235, 237; 236, identifier:func; 237, identifier:mode; 238, block; 238, 239; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 242; 241, identifier:func; 242, lambda; 242, 243; 242, 246; 243, lambda_parameters; 243, 244; 243, 245; 244, identifier:x; 245, identifier:axis; 246, subscript; 246, 247; 246, 254; 247, call; 247, 248; 247, 249; 248, identifier:mode; 249, argument_list; 249, 250; 249, 251; 250, identifier:x; 251, keyword_argument; 251, 252; 251, 253; 252, identifier:axis; 253, identifier:axis; 254, integer:0; 255, for_statement; 255, 256; 255, 257; 255, 266; 255, 267; 256, identifier:i; 257, call; 257, 258; 257, 259; 258, identifier:range; 259, argument_list; 259, 260; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:output; 263, identifier:number_of; 264, argument_list; 264, 265; 265, string:'trial'; 266, comment; 267, block; 267, 268; 267, 291; 268, if_statement; 268, 269; 268, 270; 268, 280; 269, identifier:first_op; 270, block; 270, 271; 271, expression_statement; 271, 272; 272, assignment; 272, 273; 272, 274; 273, identifier:x; 274, call; 274, 275; 274, 276; 275, identifier:data; 276, argument_list; 276, 277; 277, keyword_argument; 277, 278; 277, 279; 278, identifier:trial; 279, identifier:i; 280, else_clause; 280, 281; 281, block; 281, 282; 282, expression_statement; 282, 283; 283, assignment; 283, 284; 283, 285; 284, identifier:x; 285, call; 285, 286; 285, 287; 286, identifier:output; 287, argument_list; 287, 288; 288, keyword_argument; 288, 289; 288, 290; 289, identifier:trial; 290, identifier:i; 291, if_statement; 291, 292; 291, 295; 291, 382; 292, subscript; 292, 293; 292, 294; 293, identifier:op; 294, string:'on_axis'; 295, block; 295, 296; 295, 314; 296, expression_statement; 296, 297; 297, call; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:lg; 300, identifier:debug; 301, argument_list; 301, 302; 302, binary_operator:+; 302, 303; 302, 310; 303, binary_operator:+; 303, 304; 303, 309; 304, binary_operator:+; 304, 305; 304, 306; 305, string:'running '; 306, subscript; 306, 307; 306, 308; 307, identifier:op; 308, string:'name'; 309, string:' on '; 310, call; 310, 311; 310, 312; 311, identifier:str; 312, argument_list; 312, 313; 313, identifier:idx_axis; 314, try_statement; 314, 315; 314, 350; 315, block; 315, 316; 315, 336; 316, if_statement; 316, 317; 316, 320; 317, comparison_operator:==; 317, 318; 317, 319; 318, identifier:func; 319, identifier:diff; 320, block; 320, 321; 320, 328; 321, expression_statement; 321, 322; 322, call; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:lg; 325, identifier:debug; 326, argument_list; 326, 327; 327, string:'Diff has one-point of zero padding'; 328, expression_statement; 328, 329; 329, assignment; 329, 330; 329, 331; 330, identifier:x; 331, call; 331, 332; 331, 333; 332, identifier:_pad_one_axis_one_value; 333, argument_list; 333, 334; 333, 335; 334, identifier:x; 335, identifier:idx_axis; 336, expression_statement; 336, 337; 337, assignment; 337, 338; 337, 343; 338, subscript; 338, 339; 338, 342; 339, attribute; 339, 340; 339, 341; 340, identifier:output; 341, identifier:data; 342, identifier:i; 343, call; 343, 344; 343, 345; 344, identifier:func; 345, argument_list; 345, 346; 345, 347; 346, identifier:x; 347, keyword_argument; 347, 348; 347, 349; 348, identifier:axis; 349, identifier:idx_axis; 350, except_clause; 350, 351; 350, 352; 351, identifier:IndexError; 352, block; 352, 353; 353, raise_statement; 353, 354; 354, call; 354, 355; 354, 356; 355, identifier:ValueError; 356, argument_list; 356, 357; 357, binary_operator:+; 357, 358; 357, 381; 358, binary_operator:+; 358, 359; 358, 366; 359, binary_operator:+; 359, 360; 359, 363; 360, binary_operator:+; 360, 361; 360, 362; 361, string:'The axis '; 362, identifier:axis; 363, concatenated_string; 363, 364; 363, 365; 364, string:' does not '; 365, string:'exist in ['; 366, call; 366, 367; 366, 370; 367, attribute; 367, 368; 367, 369; 368, string:', '; 369, identifier:join; 370, argument_list; 370, 371; 371, call; 371, 372; 371, 373; 372, identifier:list; 373, argument_list; 373, 374; 374, call; 374, 375; 374, 380; 375, attribute; 375, 376; 375, 379; 376, attribute; 376, 377; 376, 378; 377, identifier:data; 378, identifier:axis; 379, identifier:keys; 380, argument_list; 381, string:']'; 382, else_clause; 382, 383; 383, block; 383, 384; 383, 397; 384, expression_statement; 384, 385; 385, call; 385, 386; 385, 389; 386, attribute; 386, 387; 386, 388; 387, identifier:lg; 388, identifier:debug; 389, argument_list; 389, 390; 390, binary_operator:+; 390, 391; 390, 396; 391, binary_operator:+; 391, 392; 391, 393; 392, string:'running '; 393, subscript; 393, 394; 393, 395; 394, identifier:op; 395, string:'name'; 396, string:' on each datapoint'; 397, expression_statement; 397, 398; 398, assignment; 398, 399; 398, 404; 399, subscript; 399, 400; 399, 403; 400, attribute; 400, 401; 400, 402; 401, identifier:output; 402, identifier:data; 403, identifier:i; 404, call; 404, 405; 404, 406; 405, identifier:func; 406, argument_list; 406, 407; 407, identifier:x; 408, expression_statement; 408, 409; 409, assignment; 409, 410; 409, 411; 410, identifier:first_op; 411, False; 412, if_statement; 412, 413; 412, 421; 413, boolean_operator:and; 413, 414; 413, 417; 414, subscript; 414, 415; 414, 416; 415, identifier:op; 416, string:'on_axis'; 417, not_operator; 417, 418; 418, subscript; 418, 419; 418, 420; 419, identifier:op; 420, string:'keepdims'; 421, block; 421, 422; 422, delete_statement; 422, 423; 423, subscript; 423, 424; 423, 427; 424, attribute; 424, 425; 424, 426; 425, identifier:output; 426, identifier:axis; 427, identifier:axis; 428, return_statement; 428, 429; 429, identifier:output
def math(data, operator=None, operator_name=None, axis=None): """Apply mathematical operation to each trial and channel individually. Parameters ---------- data : instance of DataTime, DataFreq, or DataTimeFreq operator : function or tuple of functions, optional function(s) to run on the data. operator_name : str or tuple of str, optional name of the function(s) to run on the data. axis : str, optional for functions that accept it, which axis you should run it on. Returns ------- instance of Data data where the trials underwent operator. Raises ------ TypeError If you pass both operator and operator_name. ValueError When you try to operate on an axis that has already been removed. Notes ----- operator and operator_name are mutually exclusive. operator_name is given as shortcut for most common operations. If a function accepts an 'axis' argument, you need to pass 'axis' to the constructor. In this way, it'll apply the function to the correct dimension. The possible point-wise operator_name are: 'absolute', 'angle', 'dB' (=10 * log10), 'exp', 'log', 'sqrt', 'square', 'unwrap' The operator_name's that need an axis, but do not remove it: 'hilbert', 'diff', 'detrend' The operator_name's that need an axis and remove it: 'mean', 'median', 'mode', 'std' Examples -------- You can pass a single value or a tuple. The order starts from left to right, so abs of the hilbert transform, should be: >>> rms = math(data, operator_name=('hilbert', 'abs'), axis='time') If you want to pass the power of three, use lambda (or partial): >>> p3 = lambda x: power(x, 3) >>> data_p3 = math(data, operator=p3) Note that lambdas are fine with point-wise operation, but if you want them to operate on axis, you need to pass ''axis'' as well, so that: >>> std_ddof = lambda x, axis: std(x, axis, ddof=1) >>> data_std = math(data, operator=std_ddof) If you don't pass 'axis' in lambda, it'll never know on which axis the function should be applied and you'll get unpredictable results. If you want to pass a function that operates on an axis and removes it (for example, if you want the max value over time), you need to add an argument in your function called ''keepdims'' (the values won't be used): >>> def func(x, axis, keepdims=None): >>> return nanmax(x, axis=axis) """ if operator is not None and operator_name is not None: raise TypeError('Parameters "operator" and "operator_name" are ' 'mutually exclusive') # turn input into a tuple of functions in operators if operator_name is not None: if isinstance(operator_name, str): operator_name = (operator_name, ) operators = [] for one_operator_name in operator_name: operators.append(eval(one_operator_name)) operator = tuple(operators) # make it an iterable if callable(operator): operator = (operator, ) operations = [] for one_operator in operator: on_axis = False keepdims = True try: args = getfullargspec(one_operator).args except TypeError: lg.debug('func ' + str(one_operator) + ' is not a Python ' 'function') else: if 'axis' in args: on_axis = True if axis is None: raise TypeError('You need to specify an axis if you ' 'use ' + one_operator.__name__ + ' (which applies to an axis)') if 'keepdims' in args or one_operator in NOKEEPDIM: keepdims = False operations.append({'name': one_operator.__name__, 'func': one_operator, 'on_axis': on_axis, 'keepdims': keepdims, }) output = data._copy() if axis is not None: idx_axis = data.index_of(axis) first_op = True for op in operations: #lg.info('running operator: ' + op['name']) func = op['func'] if func == mode: func = lambda x, axis: mode(x, axis=axis)[0] for i in range(output.number_of('trial')): # don't copy original data, but use data if it's the first operation if first_op: x = data(trial=i) else: x = output(trial=i) if op['on_axis']: lg.debug('running ' + op['name'] + ' on ' + str(idx_axis)) try: if func == diff: lg.debug('Diff has one-point of zero padding') x = _pad_one_axis_one_value(x, idx_axis) output.data[i] = func(x, axis=idx_axis) except IndexError: raise ValueError('The axis ' + axis + ' does not ' 'exist in [' + ', '.join(list(data.axis.keys())) + ']') else: lg.debug('running ' + op['name'] + ' on each datapoint') output.data[i] = func(x) first_op = False if op['on_axis'] and not op['keepdims']: del output.axis[axis] return output
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:detect_Massimini2004; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:dat_orig; 5, identifier:s_freq; 6, identifier:time; 7, identifier:opts; 8, block; 8, 9; 8, 11; 8, 21; 8, 33; 8, 44; 8, 48; 8, 135; 8, 150; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:opts; 14, identifier:invert; 15, block; 15, 16; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:dat_orig; 19, unary_operator:-; 19, 20; 20, identifier:dat_orig; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:dat_det; 24, call; 24, 25; 24, 26; 25, identifier:transform_signal; 26, argument_list; 26, 27; 26, 28; 26, 29; 26, 30; 27, identifier:dat_orig; 28, identifier:s_freq; 29, string:'double_butter'; 30, attribute; 30, 31; 30, 32; 31, identifier:opts; 32, identifier:det_filt; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:above_zero; 36, call; 36, 37; 36, 38; 37, identifier:detect_events; 38, argument_list; 38, 39; 38, 40; 38, 41; 39, identifier:dat_det; 40, string:'above_thresh'; 41, keyword_argument; 41, 42; 41, 43; 42, identifier:value; 43, float:0.; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:sw_in_chan; 47, list:[]; 48, if_statement; 48, 49; 48, 52; 49, comparison_operator:is; 49, 50; 49, 51; 50, identifier:above_zero; 51, None; 52, block; 52, 53; 52, 64; 52, 65; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:troughs; 56, call; 56, 57; 56, 58; 57, identifier:within_duration; 58, argument_list; 58, 59; 58, 60; 58, 61; 59, identifier:above_zero; 60, identifier:time; 61, attribute; 61, 62; 61, 63; 62, identifier:opts; 63, identifier:trough_duration; 64, comment; 65, if_statement; 65, 66; 65, 69; 66, comparison_operator:is; 66, 67; 66, 68; 67, identifier:troughs; 68, None; 69, block; 69, 70; 69, 81; 69, 82; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:troughs; 73, call; 73, 74; 73, 75; 74, identifier:select_peaks; 75, argument_list; 75, 76; 75, 77; 75, 78; 76, identifier:dat_det; 77, identifier:troughs; 78, attribute; 78, 79; 78, 80; 79, identifier:opts; 80, identifier:max_trough_amp; 81, comment; 82, if_statement; 82, 83; 82, 86; 83, comparison_operator:is; 83, 84; 83, 85; 84, identifier:troughs; 85, None; 86, block; 86, 87; 86, 97; 86, 98; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:events; 90, call; 90, 91; 90, 92; 91, identifier:_add_halfwave; 92, argument_list; 92, 93; 92, 94; 92, 95; 92, 96; 93, identifier:dat_det; 94, identifier:troughs; 95, identifier:s_freq; 96, identifier:opts; 97, comment; 98, if_statement; 98, 99; 98, 103; 99, call; 99, 100; 99, 101; 100, identifier:len; 101, argument_list; 101, 102; 102, identifier:events; 103, block; 103, 104; 103, 115; 103, 124; 103, 125; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:events; 107, call; 107, 108; 107, 109; 108, identifier:within_duration; 109, argument_list; 109, 110; 109, 111; 109, 112; 110, identifier:events; 111, identifier:time; 112, attribute; 112, 113; 112, 114; 113, identifier:opts; 114, identifier:duration; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:events; 118, call; 118, 119; 118, 120; 119, identifier:remove_straddlers; 120, argument_list; 120, 121; 120, 122; 120, 123; 121, identifier:events; 122, identifier:time; 123, identifier:s_freq; 124, comment; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:sw_in_chan; 128, call; 128, 129; 128, 130; 129, identifier:make_slow_waves; 130, argument_list; 130, 131; 130, 132; 130, 133; 130, 134; 131, identifier:events; 132, identifier:dat_det; 133, identifier:time; 134, identifier:s_freq; 135, if_statement; 135, 136; 135, 142; 136, comparison_operator:==; 136, 137; 136, 141; 137, call; 137, 138; 137, 139; 138, identifier:len; 139, argument_list; 139, 140; 140, identifier:sw_in_chan; 141, integer:0; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:lg; 147, identifier:info; 148, argument_list; 148, 149; 149, string:'No slow wave found'; 150, return_statement; 150, 151; 151, identifier:sw_in_chan
def detect_Massimini2004(dat_orig, s_freq, time, opts): """Slow wave detection based on Massimini et al., 2004. Parameters ---------- dat_orig : ndarray (dtype='float') vector with the data for one channel s_freq : float sampling frequency time : ndarray (dtype='float') vector with the time points for each sample opts : instance of 'DetectSlowWave' 'det_filt' : dict parameters for 'butter', 'duration' : tuple of float min and max duration of SW 'min_ptp' : float min peak-to-peak amplitude 'trough_duration' : tuple of float min and max duration of first half-wave (trough) Returns ------- list of dict list of detected SWs float SW density, per 30-s epoch References ---------- Massimini, M. et al. J Neurosci 24(31) 6862-70 (2004). """ if opts.invert: dat_orig = -dat_orig dat_det = transform_signal(dat_orig, s_freq, 'double_butter', opts.det_filt) above_zero = detect_events(dat_det, 'above_thresh', value=0.) sw_in_chan = [] if above_zero is not None: troughs = within_duration(above_zero, time, opts.trough_duration) #lg.info('troughs within duration: ' + str(troughs.shape)) if troughs is not None: troughs = select_peaks(dat_det, troughs, opts.max_trough_amp) #lg.info('troughs deep enough: ' + str(troughs.shape)) if troughs is not None: events = _add_halfwave(dat_det, troughs, s_freq, opts) #lg.info('SWs high enough: ' + str(events.shape)) if len(events): events = within_duration(events, time, opts.duration) events = remove_straddlers(events, time, s_freq) #lg.info('SWs within duration: ' + str(events.shape)) sw_in_chan = make_slow_waves(events, dat_det, time, s_freq) if len(sw_in_chan) == 0: lg.info('No slow wave found') return sw_in_chan
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 29; 2, function_name:filter_; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 4, identifier:data; 5, default_parameter; 5, 6; 5, 7; 6, identifier:axis; 7, string:'time'; 8, default_parameter; 8, 9; 8, 10; 9, identifier:low_cut; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:high_cut; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:order; 16, integer:4; 17, default_parameter; 17, 18; 17, 19; 18, identifier:ftype; 19, string:'butter'; 20, default_parameter; 20, 21; 20, 22; 21, identifier:Rs; 22, None; 23, default_parameter; 23, 24; 23, 25; 24, identifier:notchfreq; 25, integer:50; 26, default_parameter; 26, 27; 26, 28; 27, identifier:notchquality; 28, integer:25; 29, block; 29, 30; 29, 32; 29, 40; 29, 44; 29, 137; 29, 151; 29, 160; 29, 224; 29, 232; 29, 283; 30, expression_statement; 30, 31; 31, comment; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:nyquist; 35, binary_operator:/; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:data; 38, identifier:s_freq; 39, float:2.; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:btype; 43, None; 44, if_statement; 44, 45; 44, 52; 44, 83; 44, 110; 45, boolean_operator:and; 45, 46; 45, 49; 46, comparison_operator:is; 46, 47; 46, 48; 47, identifier:low_cut; 48, None; 49, comparison_operator:is; 49, 50; 49, 51; 50, identifier:high_cut; 51, None; 52, block; 52, 53; 52, 69; 52, 73; 53, if_statement; 53, 54; 53, 61; 54, boolean_operator:or; 54, 55; 54, 58; 55, comparison_operator:>; 55, 56; 55, 57; 56, identifier:low_cut; 57, identifier:nyquist; 58, comparison_operator:>; 58, 59; 58, 60; 59, identifier:high_cut; 60, identifier:nyquist; 61, block; 61, 62; 62, raise_statement; 62, 63; 63, call; 63, 64; 63, 65; 64, identifier:ValueError; 65, argument_list; 65, 66; 66, concatenated_string; 66, 67; 66, 68; 67, string:'cutoff has to be less than Nyquist '; 68, string:'frequency'; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:btype; 72, string:'bandpass'; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:Wn; 76, tuple; 76, 77; 76, 80; 77, binary_operator:/; 77, 78; 77, 79; 78, identifier:low_cut; 79, identifier:nyquist; 80, binary_operator:/; 80, 81; 80, 82; 81, identifier:high_cut; 82, identifier:nyquist; 83, elif_clause; 83, 84; 83, 87; 84, comparison_operator:is; 84, 85; 84, 86; 85, identifier:low_cut; 86, None; 87, block; 87, 88; 87, 100; 87, 104; 88, if_statement; 88, 89; 88, 92; 89, comparison_operator:>; 89, 90; 89, 91; 90, identifier:low_cut; 91, identifier:nyquist; 92, block; 92, 93; 93, raise_statement; 93, 94; 94, call; 94, 95; 94, 96; 95, identifier:ValueError; 96, argument_list; 96, 97; 97, concatenated_string; 97, 98; 97, 99; 98, string:'cutoff has to be less than Nyquist '; 99, string:'frequency'; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:btype; 103, string:'highpass'; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:Wn; 107, binary_operator:/; 107, 108; 107, 109; 108, identifier:low_cut; 109, identifier:nyquist; 110, elif_clause; 110, 111; 110, 114; 111, comparison_operator:is; 111, 112; 111, 113; 112, identifier:high_cut; 113, None; 114, block; 114, 115; 114, 127; 114, 131; 115, if_statement; 115, 116; 115, 119; 116, comparison_operator:>; 116, 117; 116, 118; 117, identifier:high_cut; 118, identifier:nyquist; 119, block; 119, 120; 120, raise_statement; 120, 121; 121, call; 121, 122; 121, 123; 122, identifier:ValueError; 123, argument_list; 123, 124; 124, concatenated_string; 124, 125; 124, 126; 125, string:'cutoff has to be less than Nyquist '; 126, string:'frequency'; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:btype; 130, string:'lowpass'; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:Wn; 134, binary_operator:/; 134, 135; 134, 136; 135, identifier:high_cut; 136, identifier:nyquist; 137, if_statement; 137, 138; 137, 145; 138, boolean_operator:and; 138, 139; 138, 142; 139, comparison_operator:is; 139, 140; 139, 141; 140, identifier:btype; 141, None; 142, comparison_operator:!=; 142, 143; 142, 144; 143, identifier:ftype; 144, string:'notch'; 145, block; 145, 146; 146, raise_statement; 146, 147; 147, call; 147, 148; 147, 149; 148, identifier:TypeError; 149, argument_list; 149, 150; 150, string:'You should specify at least low_cut or high_cut'; 151, if_statement; 151, 152; 151, 155; 152, comparison_operator:is; 152, 153; 152, 154; 153, identifier:Rs; 154, None; 155, block; 155, 156; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:Rs; 159, integer:40; 160, if_statement; 160, 161; 160, 164; 160, 184; 161, comparison_operator:==; 161, 162; 161, 163; 162, identifier:ftype; 163, string:'notch'; 164, block; 164, 165; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:b_a; 168, list_comprehension; 168, 169; 168, 176; 169, call; 169, 170; 169, 171; 170, identifier:iirnotch; 171, argument_list; 171, 172; 171, 175; 172, binary_operator:/; 172, 173; 172, 174; 173, identifier:w0; 174, identifier:nyquist; 175, identifier:notchquality; 176, for_in_clause; 176, 177; 176, 178; 177, identifier:w0; 178, call; 178, 179; 178, 180; 179, identifier:arange; 180, argument_list; 180, 181; 180, 182; 180, 183; 181, identifier:notchfreq; 182, identifier:nyquist; 183, identifier:notchfreq; 184, else_clause; 184, 185; 185, block; 185, 186; 185, 206; 186, expression_statement; 186, 187; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:lg; 190, identifier:debug; 191, argument_list; 191, 192; 192, call; 192, 193; 192, 198; 193, attribute; 193, 194; 193, 197; 194, concatenated_string; 194, 195; 194, 196; 195, string:'order {0: 2}, Wn {1}, btype {2}, ftype {3}'; 196, string:''; 197, identifier:format; 198, argument_list; 198, 199; 198, 200; 198, 204; 198, 205; 199, identifier:order; 200, call; 200, 201; 200, 202; 201, identifier:str; 202, argument_list; 202, 203; 203, identifier:Wn; 204, identifier:btype; 205, identifier:ftype; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:b_a; 209, list:[iirfilter(order, Wn, btype=btype, ftype=ftype, rs=Rs), ]; 209, 210; 210, call; 210, 211; 210, 212; 211, identifier:iirfilter; 212, argument_list; 212, 213; 212, 214; 212, 215; 212, 218; 212, 221; 213, identifier:order; 214, identifier:Wn; 215, keyword_argument; 215, 216; 215, 217; 216, identifier:btype; 217, identifier:btype; 218, keyword_argument; 218, 219; 218, 220; 219, identifier:ftype; 220, identifier:ftype; 221, keyword_argument; 221, 222; 221, 223; 222, identifier:rs; 223, identifier:Rs; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:fdata; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:data; 230, identifier:_copy; 231, argument_list; 232, for_statement; 232, 233; 232, 234; 232, 243; 233, identifier:i; 234, call; 234, 235; 234, 236; 235, identifier:range; 236, argument_list; 236, 237; 237, call; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:data; 240, identifier:number_of; 241, argument_list; 241, 242; 242, string:'trial'; 243, block; 243, 244; 243, 252; 243, 275; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 247; 246, identifier:x; 247, subscript; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:data; 250, identifier:data; 251, identifier:i; 252, for_statement; 252, 253; 252, 256; 252, 257; 253, pattern_list; 253, 254; 253, 255; 254, identifier:b; 255, identifier:a; 256, identifier:b_a; 257, block; 257, 258; 258, expression_statement; 258, 259; 259, assignment; 259, 260; 259, 261; 260, identifier:x; 261, call; 261, 262; 261, 263; 262, identifier:filtfilt; 263, argument_list; 263, 264; 263, 265; 263, 266; 263, 267; 264, identifier:b; 265, identifier:a; 266, identifier:x; 267, keyword_argument; 267, 268; 267, 269; 268, identifier:axis; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:data; 272, identifier:index_of; 273, argument_list; 273, 274; 274, identifier:axis; 275, expression_statement; 275, 276; 276, assignment; 276, 277; 276, 282; 277, subscript; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:fdata; 280, identifier:data; 281, identifier:i; 282, identifier:x; 283, return_statement; 283, 284; 284, identifier:fdata
def filter_(data, axis='time', low_cut=None, high_cut=None, order=4, ftype='butter', Rs=None, notchfreq=50, notchquality=25): """Design filter and apply it. Parameters ---------- ftype : str 'butter', 'cheby1', 'cheby2', 'ellip', 'bessel', 'diff', or 'notch' axis : str, optional axis to apply the filter on. low_cut : float, optional (not for notch) low cutoff for high-pass filter high_cut : float, optional (not for notch) high cutoff for low-pass filter order : int, optional (not for notch) filter order data : instance of Data (not for notch) the data to filter. notchfreq : float (only for notch) frequency to apply notch filter to (+ harmonics) notchquality : int (only for notch) Quality factor (see scipy.signal.iirnotch) Returns ------- filtered_data : instance of DataRaw filtered data Notes ----- You can specify any filter type as defined by iirfilter. If you specify low_cut only, it generates a high-pass filter. If you specify high_cut only, it generates a low-pass filter. If you specify both, it generates a band-pass filter. low_cut and high_cut should be given as ratio of the Nyquist. But if you specify s_freq, then the ratio will be computed automatically. Raises ------ ValueError if the cutoff frequency is larger than the Nyquist frequency. """ nyquist = data.s_freq / 2. btype = None if low_cut is not None and high_cut is not None: if low_cut > nyquist or high_cut > nyquist: raise ValueError('cutoff has to be less than Nyquist ' 'frequency') btype = 'bandpass' Wn = (low_cut / nyquist, high_cut / nyquist) elif low_cut is not None: if low_cut > nyquist: raise ValueError('cutoff has to be less than Nyquist ' 'frequency') btype = 'highpass' Wn = low_cut / nyquist elif high_cut is not None: if high_cut > nyquist: raise ValueError('cutoff has to be less than Nyquist ' 'frequency') btype = 'lowpass' Wn = high_cut / nyquist if btype is None and ftype != 'notch': raise TypeError('You should specify at least low_cut or high_cut') if Rs is None: Rs = 40 if ftype == 'notch': b_a = [iirnotch(w0 / nyquist, notchquality) for w0 in arange(notchfreq, nyquist, notchfreq)] else: lg.debug('order {0: 2}, Wn {1}, btype {2}, ftype {3}' ''.format(order, str(Wn), btype, ftype)) b_a = [iirfilter(order, Wn, btype=btype, ftype=ftype, rs=Rs), ] fdata = data._copy() for i in range(data.number_of('trial')): x = data.data[i] for b, a in b_a: x = filtfilt(b, a, x, axis=data.index_of(axis)) fdata.data[i] = x return fdata
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:select; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:data; 5, default_parameter; 5, 6; 5, 7; 6, identifier:trial; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:invert; 10, False; 11, dictionary_splat_pattern; 11, 12; 12, identifier:axes_to_select; 13, block; 13, 14; 13, 16; 13, 33; 13, 65; 13, 107; 13, 108; 13, 119; 13, 142; 13, 157; 13, 161; 13, 413; 14, expression_statement; 14, 15; 15, comment; 16, if_statement; 16, 17; 16, 27; 17, boolean_operator:and; 17, 18; 17, 21; 18, comparison_operator:is; 18, 19; 18, 20; 19, identifier:trial; 20, None; 21, not_operator; 21, 22; 22, call; 22, 23; 22, 24; 23, identifier:isinstance; 24, argument_list; 24, 25; 24, 26; 25, identifier:trial; 26, identifier:Iterable; 27, block; 27, 28; 28, raise_statement; 28, 29; 29, call; 29, 30; 29, 31; 30, identifier:TypeError; 31, argument_list; 31, 32; 32, string:'Trial needs to be iterable.'; 33, for_statement; 33, 34; 33, 37; 33, 42; 34, pattern_list; 34, 35; 34, 36; 35, identifier:axis_to_select; 36, identifier:values_to_select; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:axes_to_select; 40, identifier:items; 41, argument_list; 42, block; 42, 43; 43, if_statement; 43, 44; 43, 57; 44, parenthesized_expression; 44, 45; 45, boolean_operator:or; 45, 46; 45, 52; 46, not_operator; 46, 47; 47, call; 47, 48; 47, 49; 48, identifier:isinstance; 49, argument_list; 49, 50; 49, 51; 50, identifier:values_to_select; 51, identifier:Iterable; 52, call; 52, 53; 52, 54; 53, identifier:isinstance; 54, argument_list; 54, 55; 54, 56; 55, identifier:values_to_select; 56, identifier:str; 57, block; 57, 58; 58, raise_statement; 58, 59; 59, call; 59, 60; 59, 61; 60, identifier:TypeError; 61, argument_list; 61, 62; 62, binary_operator:+; 62, 63; 62, 64; 63, identifier:axis_to_select; 64, string:' needs to be iterable.'; 65, if_statement; 65, 66; 65, 69; 65, 82; 66, comparison_operator:is; 66, 67; 66, 68; 67, identifier:trial; 68, None; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:trial; 73, call; 73, 74; 73, 75; 74, identifier:range; 75, argument_list; 75, 76; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:data; 79, identifier:number_of; 80, argument_list; 80, 81; 81, string:'trial'; 82, else_clause; 82, 83; 83, block; 83, 84; 83, 88; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:trial; 87, identifier:trial; 88, if_statement; 88, 89; 88, 90; 89, identifier:invert; 90, block; 90, 91; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:trial; 94, call; 94, 95; 94, 96; 95, identifier:setdiff1d; 96, argument_list; 96, 97; 96, 106; 97, call; 97, 98; 97, 99; 98, identifier:range; 99, argument_list; 99, 100; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:data; 103, identifier:number_of; 104, argument_list; 104, 105; 105, string:'trial'; 106, identifier:trial; 107, comment; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:output; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:data; 114, identifier:_copy; 115, argument_list; 115, 116; 116, keyword_argument; 116, 117; 116, 118; 117, identifier:axis; 118, False; 119, for_statement; 119, 120; 119, 121; 119, 124; 120, identifier:one_axis; 121, attribute; 121, 122; 121, 123; 122, identifier:output; 123, identifier:axis; 124, block; 124, 125; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 132; 127, subscript; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:output; 130, identifier:axis; 131, identifier:one_axis; 132, call; 132, 133; 132, 134; 133, identifier:empty; 134, argument_list; 134, 135; 134, 139; 135, call; 135, 136; 135, 137; 136, identifier:len; 137, argument_list; 137, 138; 138, identifier:trial; 139, keyword_argument; 139, 140; 139, 141; 140, identifier:dtype; 141, string:'O'; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:output; 146, identifier:data; 147, call; 147, 148; 147, 149; 148, identifier:empty; 149, argument_list; 149, 150; 149, 154; 150, call; 150, 151; 150, 152; 151, identifier:len; 152, argument_list; 152, 153; 153, identifier:trial; 154, keyword_argument; 154, 155; 154, 156; 155, identifier:dtype; 156, string:'O'; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:to_select; 160, dictionary; 161, for_statement; 161, 162; 161, 165; 161, 169; 162, pattern_list; 162, 163; 162, 164; 163, identifier:cnt; 164, identifier:i; 165, call; 165, 166; 165, 167; 166, identifier:enumerate; 167, argument_list; 167, 168; 168, identifier:trial; 169, block; 169, 170; 169, 182; 169, 398; 170, expression_statement; 170, 171; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:lg; 174, identifier:debug; 175, argument_list; 175, 176; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, string:'Selection on trial {0: 6}'; 179, identifier:format; 180, argument_list; 180, 181; 181, identifier:i; 182, for_statement; 182, 183; 182, 184; 182, 187; 183, identifier:one_axis; 184, attribute; 184, 185; 184, 186; 185, identifier:output; 186, identifier:axis; 187, block; 187, 188; 187, 198; 187, 388; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:values; 191, subscript; 191, 192; 191, 197; 192, subscript; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:data; 195, identifier:axis; 196, identifier:one_axis; 197, identifier:i; 198, if_statement; 198, 199; 198, 206; 198, 363; 199, comparison_operator:in; 199, 200; 199, 201; 200, identifier:one_axis; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:axes_to_select; 204, identifier:keys; 205, argument_list; 206, block; 206, 207; 206, 213; 206, 328; 206, 339; 206, 357; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:values_to_select; 210, subscript; 210, 211; 210, 212; 211, identifier:axes_to_select; 212, identifier:one_axis; 213, if_statement; 213, 214; 213, 220; 213, 225; 213, 244; 214, comparison_operator:==; 214, 215; 214, 219; 215, call; 215, 216; 215, 217; 216, identifier:len; 217, argument_list; 217, 218; 218, identifier:values_to_select; 219, integer:0; 220, block; 220, 221; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:selected_values; 224, tuple; 225, elif_clause; 225, 226; 225, 233; 226, call; 226, 227; 226, 228; 227, identifier:isinstance; 228, argument_list; 228, 229; 228, 232; 229, subscript; 229, 230; 229, 231; 230, identifier:values_to_select; 231, integer:0; 232, identifier:str; 233, block; 233, 234; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 237; 236, identifier:selected_values; 237, call; 237, 238; 237, 239; 238, identifier:asarray; 239, argument_list; 239, 240; 239, 241; 240, identifier:values_to_select; 241, keyword_argument; 241, 242; 241, 243; 242, identifier:dtype; 243, string:'U'; 244, else_clause; 244, 245; 245, block; 245, 246; 245, 322; 246, if_statement; 246, 247; 246, 259; 246, 273; 246, 288; 246, 303; 247, parenthesized_expression; 247, 248; 248, boolean_operator:and; 248, 249; 248, 254; 249, comparison_operator:is; 249, 250; 249, 253; 250, subscript; 250, 251; 250, 252; 251, identifier:values_to_select; 252, integer:0; 253, None; 254, comparison_operator:is; 254, 255; 254, 258; 255, subscript; 255, 256; 255, 257; 256, identifier:values_to_select; 257, integer:1; 258, None; 259, block; 259, 260; 260, expression_statement; 260, 261; 261, assignment; 261, 262; 261, 263; 262, identifier:bool_values; 263, call; 263, 264; 263, 265; 264, identifier:ones; 265, argument_list; 265, 266; 265, 270; 266, call; 266, 267; 266, 268; 267, identifier:len; 268, argument_list; 268, 269; 269, identifier:values; 270, keyword_argument; 270, 271; 270, 272; 271, identifier:dtype; 272, identifier:bool; 273, elif_clause; 273, 274; 273, 279; 274, comparison_operator:is; 274, 275; 274, 278; 275, subscript; 275, 276; 275, 277; 276, identifier:values_to_select; 277, integer:0; 278, None; 279, block; 279, 280; 280, expression_statement; 280, 281; 281, assignment; 281, 282; 281, 283; 282, identifier:bool_values; 283, comparison_operator:<; 283, 284; 283, 285; 284, identifier:values; 285, subscript; 285, 286; 285, 287; 286, identifier:values_to_select; 287, integer:1; 288, elif_clause; 288, 289; 288, 294; 289, comparison_operator:is; 289, 290; 289, 293; 290, subscript; 290, 291; 290, 292; 291, identifier:values_to_select; 292, integer:1; 293, None; 294, block; 294, 295; 295, expression_statement; 295, 296; 296, assignment; 296, 297; 296, 298; 297, identifier:bool_values; 298, comparison_operator:<=; 298, 299; 298, 302; 299, subscript; 299, 300; 299, 301; 300, identifier:values_to_select; 301, integer:0; 302, identifier:values; 303, else_clause; 303, 304; 304, block; 304, 305; 305, expression_statement; 305, 306; 306, assignment; 306, 307; 306, 308; 307, identifier:bool_values; 308, parenthesized_expression; 308, 309; 309, binary_operator:&; 309, 310; 309, 316; 310, parenthesized_expression; 310, 311; 311, comparison_operator:<=; 311, 312; 311, 315; 312, subscript; 312, 313; 312, 314; 313, identifier:values_to_select; 314, integer:0; 315, identifier:values; 316, parenthesized_expression; 316, 317; 317, comparison_operator:<; 317, 318; 317, 319; 318, identifier:values; 319, subscript; 319, 320; 319, 321; 320, identifier:values_to_select; 321, integer:1; 322, expression_statement; 322, 323; 323, assignment; 323, 324; 323, 325; 324, identifier:selected_values; 325, subscript; 325, 326; 325, 327; 326, identifier:values; 327, identifier:bool_values; 328, if_statement; 328, 329; 328, 330; 329, identifier:invert; 330, block; 330, 331; 331, expression_statement; 331, 332; 332, assignment; 332, 333; 332, 334; 333, identifier:selected_values; 334, call; 334, 335; 334, 336; 335, identifier:setdiff1d; 336, argument_list; 336, 337; 336, 338; 337, identifier:values; 338, identifier:selected_values; 339, expression_statement; 339, 340; 340, call; 340, 341; 340, 344; 341, attribute; 341, 342; 341, 343; 342, identifier:lg; 343, identifier:debug; 344, argument_list; 344, 345; 345, call; 345, 346; 345, 351; 346, attribute; 346, 347; 346, 350; 347, concatenated_string; 347, 348; 347, 349; 348, string:'In axis {0}, selecting {1: 6} '; 349, string:'values'; 350, identifier:format; 351, argument_list; 351, 352; 351, 353; 352, identifier:one_axis; 353, call; 353, 354; 353, 355; 354, identifier:len; 355, argument_list; 355, 356; 356, identifier:selected_values; 357, expression_statement; 357, 358; 358, assignment; 358, 359; 358, 362; 359, subscript; 359, 360; 359, 361; 360, identifier:to_select; 361, identifier:one_axis; 362, identifier:selected_values; 363, else_clause; 363, 364; 364, block; 364, 365; 364, 378; 365, expression_statement; 365, 366; 366, call; 366, 367; 366, 370; 367, attribute; 367, 368; 367, 369; 368, identifier:lg; 369, identifier:debug; 370, argument_list; 370, 371; 371, binary_operator:+; 371, 372; 371, 375; 372, binary_operator:+; 372, 373; 372, 374; 373, string:'In axis '; 374, identifier:one_axis; 375, concatenated_string; 375, 376; 375, 377; 376, string:', selecting all the '; 377, string:'values'; 378, expression_statement; 378, 379; 379, assignment; 379, 380; 379, 381; 380, identifier:selected_values; 381, subscript; 381, 382; 381, 387; 382, subscript; 382, 383; 382, 386; 383, attribute; 383, 384; 383, 385; 384, identifier:data; 385, identifier:axis; 386, identifier:one_axis; 387, identifier:i; 388, expression_statement; 388, 389; 389, assignment; 389, 390; 389, 397; 390, subscript; 390, 391; 390, 396; 391, subscript; 391, 392; 391, 395; 392, attribute; 392, 393; 392, 394; 393, identifier:output; 394, identifier:axis; 395, identifier:one_axis; 396, identifier:cnt; 397, identifier:selected_values; 398, expression_statement; 398, 399; 399, assignment; 399, 400; 399, 405; 400, subscript; 400, 401; 400, 404; 401, attribute; 401, 402; 401, 403; 402, identifier:output; 403, identifier:data; 404, identifier:cnt; 405, call; 405, 406; 405, 407; 406, identifier:data; 407, argument_list; 407, 408; 407, 411; 408, keyword_argument; 408, 409; 408, 410; 409, identifier:trial; 410, identifier:i; 411, dictionary_splat; 411, 412; 412, identifier:to_select; 413, return_statement; 413, 414; 414, identifier:output
def select(data, trial=None, invert=False, **axes_to_select): """Define the selection of trials, using ranges or actual values. Parameters ---------- data : instance of Data data to select from. trial : list of int or ndarray (dtype='i'), optional index of trials of interest **axes_to_select, optional Values need to be tuple or list. If the values in one axis are string, then you need to specify all the strings that you want. If the values are numeric, then you should specify the range (you cannot specify single values, nor multiple values). To select only up to one point, you can use (None, value_of_interest) invert : bool take the opposite selection Returns ------- instance, same class as input data where selection has been applied. """ if trial is not None and not isinstance(trial, Iterable): raise TypeError('Trial needs to be iterable.') for axis_to_select, values_to_select in axes_to_select.items(): if (not isinstance(values_to_select, Iterable) or isinstance(values_to_select, str)): raise TypeError(axis_to_select + ' needs to be iterable.') if trial is None: trial = range(data.number_of('trial')) else: trial = trial if invert: trial = setdiff1d(range(data.number_of('trial')), trial) # create empty axis output = data._copy(axis=False) for one_axis in output.axis: output.axis[one_axis] = empty(len(trial), dtype='O') output.data = empty(len(trial), dtype='O') to_select = {} for cnt, i in enumerate(trial): lg.debug('Selection on trial {0: 6}'.format(i)) for one_axis in output.axis: values = data.axis[one_axis][i] if one_axis in axes_to_select.keys(): values_to_select = axes_to_select[one_axis] if len(values_to_select) == 0: selected_values = () elif isinstance(values_to_select[0], str): selected_values = asarray(values_to_select, dtype='U') else: if (values_to_select[0] is None and values_to_select[1] is None): bool_values = ones(len(values), dtype=bool) elif values_to_select[0] is None: bool_values = values < values_to_select[1] elif values_to_select[1] is None: bool_values = values_to_select[0] <= values else: bool_values = ((values_to_select[0] <= values) & (values < values_to_select[1])) selected_values = values[bool_values] if invert: selected_values = setdiff1d(values, selected_values) lg.debug('In axis {0}, selecting {1: 6} ' 'values'.format(one_axis, len(selected_values))) to_select[one_axis] = selected_values else: lg.debug('In axis ' + one_axis + ', selecting all the ' 'values') selected_values = data.axis[one_axis][i] output.axis[one_axis][cnt] = selected_values output.data[cnt] = data(trial=i, **to_select) return output
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 49; 2, function_name:fetch; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 13; 3, 16; 3, 19; 3, 22; 3, 25; 3, 28; 3, 31; 3, 34; 3, 37; 3, 40; 3, 43; 3, 46; 4, identifier:dataset; 5, identifier:annot; 6, default_parameter; 6, 7; 6, 8; 7, identifier:cat; 8, tuple; 8, 9; 8, 10; 8, 11; 8, 12; 9, integer:0; 10, integer:0; 11, integer:0; 12, integer:0; 13, default_parameter; 13, 14; 13, 15; 14, identifier:evt_type; 15, None; 16, default_parameter; 16, 17; 16, 18; 17, identifier:stage; 18, None; 19, default_parameter; 19, 20; 19, 21; 20, identifier:cycle; 21, None; 22, default_parameter; 22, 23; 22, 24; 23, identifier:chan_full; 24, None; 25, default_parameter; 25, 26; 25, 27; 26, identifier:epoch; 27, None; 28, default_parameter; 28, 29; 28, 30; 29, identifier:epoch_dur; 30, integer:30; 31, default_parameter; 31, 32; 31, 33; 32, identifier:epoch_overlap; 33, integer:0; 34, default_parameter; 34, 35; 34, 36; 35, identifier:epoch_step; 36, None; 37, default_parameter; 37, 38; 37, 39; 38, identifier:reject_epoch; 39, False; 40, default_parameter; 40, 41; 40, 42; 41, identifier:reject_artf; 42, False; 43, default_parameter; 43, 44; 43, 45; 44, identifier:min_dur; 45, integer:0; 46, default_parameter; 46, 47; 46, 48; 47, identifier:buffer; 48, integer:0; 49, block; 49, 50; 49, 52; 49, 77; 49, 78; 49, 105; 49, 106; 49, 184; 49, 191; 49, 197; 50, expression_statement; 50, 51; 51, comment; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:bundles; 55, call; 55, 56; 55, 57; 56, identifier:get_times; 57, argument_list; 57, 58; 57, 59; 57, 62; 57, 65; 57, 68; 57, 71; 57, 74; 58, identifier:annot; 59, keyword_argument; 59, 60; 59, 61; 60, identifier:evt_type; 61, identifier:evt_type; 62, keyword_argument; 62, 63; 62, 64; 63, identifier:stage; 64, identifier:stage; 65, keyword_argument; 65, 66; 65, 67; 66, identifier:cycle; 67, identifier:cycle; 68, keyword_argument; 68, 69; 68, 70; 69, identifier:chan; 70, identifier:chan_full; 71, keyword_argument; 71, 72; 71, 73; 72, identifier:exclude; 73, identifier:reject_epoch; 74, keyword_argument; 74, 75; 74, 76; 75, identifier:buffer; 76, identifier:buffer; 77, comment; 78, if_statement; 78, 79; 78, 82; 79, boolean_operator:and; 79, 80; 79, 81; 80, identifier:reject_artf; 81, identifier:bundles; 82, block; 82, 83; 83, for_statement; 83, 84; 83, 85; 83, 86; 84, identifier:bund; 85, identifier:bundles; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 92; 89, subscript; 89, 90; 89, 91; 90, identifier:bund; 91, string:'times'; 92, call; 92, 93; 92, 94; 93, identifier:remove_artf_evts; 94, argument_list; 94, 95; 94, 98; 94, 99; 94, 102; 95, subscript; 95, 96; 95, 97; 96, identifier:bund; 97, string:'times'; 98, identifier:annot; 99, subscript; 99, 100; 99, 101; 100, identifier:bund; 101, string:'chan'; 102, keyword_argument; 102, 103; 102, 104; 103, identifier:min_dur; 104, integer:0; 105, comment; 106, if_statement; 106, 107; 106, 108; 107, identifier:bundles; 108, block; 108, 109; 108, 175; 108, 176; 109, if_statement; 109, 110; 109, 113; 109, 121; 109, 163; 110, comparison_operator:==; 110, 111; 110, 112; 111, string:'locked'; 112, identifier:epoch; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:bundles; 117, call; 117, 118; 117, 119; 118, identifier:_divide_bundles; 119, argument_list; 119, 120; 120, identifier:bundles; 121, elif_clause; 121, 122; 121, 125; 122, comparison_operator:==; 122, 123; 122, 124; 123, string:'unlocked'; 124, identifier:epoch; 125, block; 125, 126; 125, 146; 125, 154; 126, if_statement; 126, 127; 126, 130; 126, 135; 127, comparison_operator:is; 127, 128; 127, 129; 128, identifier:epoch_step; 129, None; 130, block; 130, 131; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:step; 134, identifier:epoch_step; 135, else_clause; 135, 136; 136, block; 136, 137; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:step; 140, binary_operator:-; 140, 141; 140, 142; 141, identifier:epoch_dur; 142, parenthesized_expression; 142, 143; 143, binary_operator:*; 143, 144; 143, 145; 144, identifier:epoch_dur; 145, identifier:epoch_overlap; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:bundles; 149, call; 149, 150; 149, 151; 150, identifier:_concat; 151, argument_list; 151, 152; 151, 153; 152, identifier:bundles; 153, identifier:cat; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:bundles; 157, call; 157, 158; 157, 159; 158, identifier:_find_intervals; 159, argument_list; 159, 160; 159, 161; 159, 162; 160, identifier:bundles; 161, identifier:epoch_dur; 162, identifier:step; 163, elif_clause; 163, 164; 163, 166; 164, not_operator; 164, 165; 165, identifier:epoch; 166, block; 166, 167; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:bundles; 170, call; 170, 171; 170, 172; 171, identifier:_concat; 172, argument_list; 172, 173; 172, 174; 173, identifier:bundles; 174, identifier:cat; 175, comment; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:bundles; 179, call; 179, 180; 179, 181; 180, identifier:_longer_than; 181, argument_list; 181, 182; 181, 183; 182, identifier:bundles; 183, identifier:min_dur; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:segments; 187, call; 187, 188; 187, 189; 188, identifier:Segments; 189, argument_list; 189, 190; 190, identifier:dataset; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:segments; 195, identifier:segments; 196, identifier:bundles; 197, return_statement; 197, 198; 198, identifier:segments
def fetch(dataset, annot, cat=(0, 0, 0, 0), evt_type=None, stage=None, cycle=None, chan_full=None, epoch=None, epoch_dur=30, epoch_overlap=0, epoch_step=None, reject_epoch=False, reject_artf=False, min_dur=0, buffer=0): """Create instance of Segments for analysis, complete with info about stage, cycle, channel, event type. Segments contains only metadata until .read_data is called. Parameters ---------- dataset : instance of Dataset info about record annot : instance of Annotations scoring info cat : tuple of int Determines where the signal is concatenated. If cat[0] is 1, cycles will be concatenated. If cat[1] is 1, different stages will be concatenated. If cat[2] is 1, discontinuous signal within a same condition (stage, cycle, event type) will be concatenated. If cat[3] is 1, events of different types will be concatenated. 0 in any position indicates no concatenation. evt_type: list of str, optional Enter a list of event types to get events; otherwise, epochs will be returned. stage: list of str, optional Stage(s) of interest. If None, stage is ignored. cycle: list of tuple of two float, optional Cycle(s) of interest, as start and end times in seconds from record start. If None, cycles are ignored. chan_full: list of str or None Channel(s) of interest, only used for events (epochs have no channel). Channel format is 'chan_name (group_name)'. If used for epochs, separate segments will be returned for each channel; this is necessary for channel-specific artefact removal (see reject_artf below). If None, channel is ignored. epoch : str, optional If 'locked', returns epochs locked to staging. If 'unlocked', divides signal (with specified concatenation) into epochs of duration epoch_dur starting at first sample of every segment and discarding any remainder. If None, longest run of signal is returned. epoch_dur : float only for epoch='unlocked'. Duration of epochs returned, in seconds. epoch_overlap : float only for epoch='unlocked'. Ratio of overlap between two consecutive segments. Value between 0 and 1. Overriden by step. epoch_step : float only for epoch='unlocked'. Time between consecutive epoch starts, in seconds. Overrides epoch_overlap/ reject_epoch: bool If True, epochs marked as 'Poor' quality or staged as 'Artefact' will be rejected (and the signal segmented in consequence). Has no effect on event selection. reject_artf : bool If True, excludes events marked as 'Artefact'. If chan_full is specified, only artefacts marked on a given channel are removed from that channel. Signal is segmented in consequence. If None, Artefact events are ignored. min_dur : float Minimum duration of segments returned, in seconds. buffer : float adds this many seconds of signal before and after each segment Returns ------- instance of Segments metadata for all analysis segments """ bundles = get_times(annot, evt_type=evt_type, stage=stage, cycle=cycle, chan=chan_full, exclude=reject_epoch, buffer=buffer) # Remove artefacts if reject_artf and bundles: for bund in bundles: bund['times'] = remove_artf_evts(bund['times'], annot, bund['chan'], min_dur=0) # Divide bundles into segments to be concatenated if bundles: if 'locked' == epoch: bundles = _divide_bundles(bundles) elif 'unlocked' == epoch: if epoch_step is not None: step = epoch_step else: step = epoch_dur - (epoch_dur * epoch_overlap) bundles = _concat(bundles, cat) bundles = _find_intervals(bundles, epoch_dur, step) elif not epoch: bundles = _concat(bundles, cat) # Minimum duration bundles = _longer_than(bundles, min_dur) segments = Segments(dataset) segments.segments = bundles return segments
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 23; 2, function_name:get_times; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 4, identifier:annot; 5, default_parameter; 5, 6; 5, 7; 6, identifier:evt_type; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:stage; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:cycle; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:chan; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:exclude; 19, False; 20, default_parameter; 20, 21; 20, 22; 21, identifier:buffer; 22, integer:0; 23, block; 23, 24; 23, 26; 23, 32; 23, 38; 23, 48; 23, 58; 23, 68; 23, 116; 23, 120; 23, 127; 23, 131; 23, 253; 24, expression_statement; 24, 25; 25, comment; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:getter; 29, attribute; 29, 30; 29, 31; 30, identifier:annot; 31, identifier:get_epochs; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:last; 35, attribute; 35, 36; 35, 37; 36, identifier:annot; 37, identifier:last_second; 38, if_statement; 38, 39; 38, 42; 39, comparison_operator:is; 39, 40; 39, 41; 40, identifier:stage; 41, None; 42, block; 42, 43; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:stage; 46, tuple; 46, 47; 47, None; 48, if_statement; 48, 49; 48, 52; 49, comparison_operator:is; 49, 50; 49, 51; 50, identifier:cycle; 51, None; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:cycle; 56, tuple; 56, 57; 57, None; 58, if_statement; 58, 59; 58, 62; 59, comparison_operator:is; 59, 60; 59, 61; 60, identifier:chan; 61, None; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:chan; 66, tuple; 66, 67; 67, None; 68, if_statement; 68, 69; 68, 72; 68, 78; 68, 107; 69, comparison_operator:is; 69, 70; 69, 71; 70, identifier:evt_type; 71, None; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:evt_type; 76, tuple; 76, 77; 77, None; 78, elif_clause; 78, 79; 78, 86; 79, call; 79, 80; 79, 81; 80, identifier:isinstance; 81, argument_list; 81, 82; 81, 85; 82, subscript; 82, 83; 82, 84; 83, identifier:evt_type; 84, integer:0; 85, identifier:str; 86, block; 86, 87; 86, 93; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:getter; 90, attribute; 90, 91; 90, 92; 91, identifier:annot; 92, identifier:get_events; 93, if_statement; 93, 94; 93, 98; 94, comparison_operator:!=; 94, 95; 94, 96; 95, identifier:chan; 96, tuple; 96, 97; 97, None; 98, block; 98, 99; 98, 106; 99, expression_statement; 99, 100; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:chan; 103, identifier:append; 104, argument_list; 104, 105; 105, string:''; 106, comment; 107, else_clause; 107, 108; 108, block; 108, 109; 109, expression_statement; 109, 110; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:lg; 113, identifier:error; 114, argument_list; 114, 115; 115, string:'Event type must be list/tuple of str or None'; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:qual; 119, None; 120, if_statement; 120, 121; 120, 122; 121, identifier:exclude; 122, block; 122, 123; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:qual; 126, string:'Good'; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:bundles; 130, list:[]; 131, for_statement; 131, 132; 131, 133; 131, 134; 132, identifier:et; 133, identifier:evt_type; 134, block; 134, 135; 135, for_statement; 135, 136; 135, 137; 135, 138; 136, identifier:ch; 137, identifier:chan; 138, block; 138, 139; 139, for_statement; 139, 140; 139, 141; 139, 142; 140, identifier:cyc; 141, identifier:cycle; 142, block; 142, 143; 143, for_statement; 143, 144; 143, 145; 143, 146; 144, identifier:ss; 145, identifier:stage; 146, block; 146, 147; 146, 151; 146, 161; 146, 183; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:st_input; 150, identifier:ss; 151, if_statement; 151, 152; 151, 155; 152, comparison_operator:is; 152, 153; 152, 154; 153, identifier:ss; 154, None; 155, block; 155, 156; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:st_input; 159, tuple; 159, 160; 160, identifier:ss; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:evochs; 164, call; 164, 165; 164, 166; 165, identifier:getter; 166, argument_list; 166, 167; 166, 170; 166, 173; 166, 177; 166, 180; 167, keyword_argument; 167, 168; 167, 169; 168, identifier:name; 169, identifier:et; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:time; 172, identifier:cyc; 173, keyword_argument; 173, 174; 173, 175; 174, identifier:chan; 175, tuple; 175, 176; 176, identifier:ch; 177, keyword_argument; 177, 178; 177, 179; 178, identifier:stage; 179, identifier:st_input; 180, keyword_argument; 180, 181; 180, 182; 181, identifier:qual; 182, identifier:qual; 183, if_statement; 183, 184; 183, 185; 184, identifier:evochs; 185, block; 185, 186; 185, 212; 185, 227; 185, 246; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:times; 189, list_comprehension; 189, 190; 189, 209; 190, tuple; 190, 191; 190, 200; 191, call; 191, 192; 191, 193; 192, identifier:max; 193, argument_list; 193, 194; 193, 199; 194, binary_operator:-; 194, 195; 194, 198; 195, subscript; 195, 196; 195, 197; 196, identifier:e; 197, string:'start'; 198, identifier:buffer; 199, integer:0; 200, call; 200, 201; 200, 202; 201, identifier:min; 202, argument_list; 202, 203; 202, 208; 203, binary_operator:+; 203, 204; 203, 207; 204, subscript; 204, 205; 204, 206; 205, identifier:e; 206, string:'end'; 207, identifier:buffer; 208, identifier:last; 209, for_in_clause; 209, 210; 209, 211; 210, identifier:e; 211, identifier:evochs; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:times; 215, call; 215, 216; 215, 217; 216, identifier:sorted; 217, argument_list; 217, 218; 217, 219; 218, identifier:times; 219, keyword_argument; 219, 220; 219, 221; 220, identifier:key; 221, lambda; 221, 222; 221, 224; 222, lambda_parameters; 222, 223; 223, identifier:x; 224, subscript; 224, 225; 224, 226; 225, identifier:x; 226, integer:0; 227, expression_statement; 227, 228; 228, assignment; 228, 229; 228, 230; 229, identifier:one_bundle; 230, dictionary; 230, 231; 230, 234; 230, 237; 230, 240; 230, 243; 231, pair; 231, 232; 231, 233; 232, string:'times'; 233, identifier:times; 234, pair; 234, 235; 234, 236; 235, string:'stage'; 236, identifier:ss; 237, pair; 237, 238; 237, 239; 238, string:'cycle'; 239, identifier:cyc; 240, pair; 240, 241; 240, 242; 241, string:'chan'; 242, identifier:ch; 243, pair; 243, 244; 243, 245; 244, string:'name'; 245, identifier:et; 246, expression_statement; 246, 247; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:bundles; 250, identifier:append; 251, argument_list; 251, 252; 252, identifier:one_bundle; 253, return_statement; 253, 254; 254, identifier:bundles
def get_times(annot, evt_type=None, stage=None, cycle=None, chan=None, exclude=False, buffer=0): """Get start and end times for selected segments of data, bundled together with info. Parameters ---------- annot: instance of Annotations The annotation file containing events and epochs evt_type: list of str, optional Enter a list of event types to get events; otherwise, epochs will be returned. stage: list of str, optional Stage(s) of interest. If None, stage is ignored. cycle: list of tuple of two float, optional Cycle(s) of interest, as start and end times in seconds from record start. If None, cycles are ignored. chan: list of str or tuple of None Channel(s) of interest. Channel format is 'chan_name (group_name)'. If None, channel is ignored. exclude: bool Exclude epochs by quality. If True, epochs marked as 'Poor' quality or staged as 'Artefact' will be rejected (and the signal segmented in consequence). Has no effect on event selection. buffer : float adds this many seconds of signal before and after each segment Returns ------- list of dict Each dict has times (the start and end times of each segment, as list of tuple of float), stage, cycle, chan, name (event type, if applicable) Notes ----- This function returns epoch or event start and end times, bundled together according to the specified parameters. Presently, setting exclude to True does not exclude events found in Poor signal epochs. The rationale is that events would never be marked in Poor signal epochs. If they were automatically detected, these epochs would have been left out during detection. If they were manually marked, then it must have been Good signal. At the moment, in the GUI, the exclude epoch option is disabled when analyzing events, but we could fix the code if we find a use case for rejecting events based on the quality of the epoch signal. """ getter = annot.get_epochs last = annot.last_second if stage is None: stage = (None,) if cycle is None: cycle = (None,) if chan is None: chan = (None,) if evt_type is None: evt_type = (None,) elif isinstance(evt_type[0], str): getter = annot.get_events if chan != (None,): chan.append('') # also retrieve events marked on all channels else: lg.error('Event type must be list/tuple of str or None') qual = None if exclude: qual = 'Good' bundles = [] for et in evt_type: for ch in chan: for cyc in cycle: for ss in stage: st_input = ss if ss is not None: st_input = (ss,) evochs = getter(name=et, time=cyc, chan=(ch,), stage=st_input, qual=qual) if evochs: times = [( max(e['start'] - buffer, 0), min(e['end'] + buffer, last)) for e in evochs] times = sorted(times, key=lambda x: x[0]) one_bundle = {'times': times, 'stage': ss, 'cycle': cyc, 'chan': ch, 'name': et} bundles.append(one_bundle) return bundles
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:_concat; 3, parameters; 3, 4; 3, 5; 4, identifier:bundles; 5, default_parameter; 5, 6; 5, 7; 6, identifier:cat; 7, tuple; 7, 8; 7, 9; 7, 10; 7, 11; 8, integer:0; 9, integer:0; 10, integer:0; 11, integer:0; 12, block; 12, 13; 12, 15; 12, 31; 12, 47; 12, 63; 12, 79; 12, 83; 12, 87; 12, 91; 12, 114; 12, 130; 12, 146; 12, 156; 12, 166; 12, 176; 12, 180; 12, 301; 12, 409; 12, 421; 13, expression_statement; 13, 14; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:chan; 18, call; 18, 19; 18, 20; 19, identifier:sorted; 20, argument_list; 20, 21; 21, call; 21, 22; 21, 23; 22, identifier:set; 23, argument_list; 23, 24; 24, list_comprehension; 24, 25; 24, 28; 25, subscript; 25, 26; 25, 27; 26, identifier:x; 27, string:'chan'; 28, for_in_clause; 28, 29; 28, 30; 29, identifier:x; 30, identifier:bundles; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:cycle; 34, call; 34, 35; 34, 36; 35, identifier:sorted; 36, argument_list; 36, 37; 37, call; 37, 38; 37, 39; 38, identifier:set; 39, argument_list; 39, 40; 40, list_comprehension; 40, 41; 40, 44; 41, subscript; 41, 42; 41, 43; 42, identifier:x; 43, string:'cycle'; 44, for_in_clause; 44, 45; 44, 46; 45, identifier:x; 46, identifier:bundles; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:stage; 50, call; 50, 51; 50, 52; 51, identifier:sorted; 52, argument_list; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:set; 55, argument_list; 55, 56; 56, list_comprehension; 56, 57; 56, 60; 57, subscript; 57, 58; 57, 59; 58, identifier:x; 59, string:'stage'; 60, for_in_clause; 60, 61; 60, 62; 61, identifier:x; 62, identifier:bundles; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:evt_type; 66, call; 66, 67; 66, 68; 67, identifier:sorted; 68, argument_list; 68, 69; 69, call; 69, 70; 69, 71; 70, identifier:set; 71, argument_list; 71, 72; 72, list_comprehension; 72, 73; 72, 76; 73, subscript; 73, 74; 73, 75; 74, identifier:x; 75, string:'name'; 76, for_in_clause; 76, 77; 76, 78; 77, identifier:x; 78, identifier:bundles; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:all_cycle; 82, None; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:all_stage; 86, None; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:all_evt_type; 90, None; 91, if_statement; 91, 92; 91, 97; 92, comparison_operator:is; 92, 93; 92, 96; 93, subscript; 93, 94; 93, 95; 94, identifier:cycle; 95, integer:0; 96, None; 97, block; 97, 98; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:all_cycle; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, string:', '; 104, identifier:join; 105, argument_list; 105, 106; 106, list_comprehension; 106, 107; 106, 111; 107, call; 107, 108; 107, 109; 108, identifier:str; 109, argument_list; 109, 110; 110, identifier:c; 111, for_in_clause; 111, 112; 111, 113; 112, identifier:c; 113, identifier:cycle; 114, if_statement; 114, 115; 114, 120; 115, comparison_operator:is; 115, 116; 115, 119; 116, subscript; 116, 117; 116, 118; 117, identifier:stage; 118, integer:0; 119, None; 120, block; 120, 121; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:all_stage; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, string:', '; 127, identifier:join; 128, argument_list; 128, 129; 129, identifier:stage; 130, if_statement; 130, 131; 130, 136; 131, comparison_operator:is; 131, 132; 131, 135; 132, subscript; 132, 133; 132, 134; 133, identifier:evt_type; 134, integer:0; 135, None; 136, block; 136, 137; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:all_evt_type; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, string:', '; 143, identifier:join; 144, argument_list; 144, 145; 145, identifier:evt_type; 146, if_statement; 146, 147; 146, 150; 147, subscript; 147, 148; 147, 149; 148, identifier:cat; 149, integer:0; 150, block; 150, 151; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:cycle; 154, list:[all_cycle]; 154, 155; 155, identifier:all_cycle; 156, if_statement; 156, 157; 156, 160; 157, subscript; 157, 158; 157, 159; 158, identifier:cat; 159, integer:1; 160, block; 160, 161; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:stage; 164, list:[all_stage]; 164, 165; 165, identifier:all_stage; 166, if_statement; 166, 167; 166, 170; 167, subscript; 167, 168; 167, 169; 168, identifier:cat; 169, integer:3; 170, block; 170, 171; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:evt_type; 174, list:[all_evt_type]; 174, 175; 175, identifier:all_evt_type; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:to_concat; 179, list:[]; 180, for_statement; 180, 181; 180, 182; 180, 183; 181, identifier:ch; 182, identifier:chan; 183, block; 183, 184; 184, for_statement; 184, 185; 184, 186; 184, 187; 185, identifier:cyc; 186, identifier:cycle; 187, block; 187, 188; 188, for_statement; 188, 189; 188, 190; 188, 191; 189, identifier:st; 190, identifier:stage; 191, block; 191, 192; 192, for_statement; 192, 193; 192, 194; 192, 195; 193, identifier:et; 194, identifier:evt_type; 195, block; 195, 196; 195, 200; 195, 260; 195, 275; 195, 294; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 199; 198, identifier:new_times; 199, list:[]; 200, for_statement; 200, 201; 200, 202; 200, 203; 201, identifier:bund; 202, identifier:bundles; 203, block; 203, 204; 203, 212; 203, 222; 203, 232; 203, 242; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:chan_cond; 207, comparison_operator:==; 207, 208; 207, 209; 208, identifier:ch; 209, subscript; 209, 210; 209, 211; 210, identifier:bund; 211, string:'chan'; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:cyc_cond; 215, comparison_operator:in; 215, 216; 215, 217; 216, identifier:cyc; 217, tuple; 217, 218; 217, 221; 218, subscript; 218, 219; 218, 220; 219, identifier:bund; 220, string:'cycle'; 221, identifier:all_cycle; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:st_cond; 225, comparison_operator:in; 225, 226; 225, 227; 226, identifier:st; 227, tuple; 227, 228; 227, 231; 228, subscript; 228, 229; 228, 230; 229, identifier:bund; 230, string:'stage'; 231, identifier:all_stage; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 235; 234, identifier:et_cond; 235, comparison_operator:in; 235, 236; 235, 237; 236, identifier:et; 237, tuple; 237, 238; 237, 241; 238, subscript; 238, 239; 238, 240; 239, identifier:bund; 240, string:'name'; 241, identifier:all_evt_type; 242, if_statement; 242, 243; 242, 250; 243, boolean_operator:and; 243, 244; 243, 249; 244, boolean_operator:and; 244, 245; 244, 248; 245, boolean_operator:and; 245, 246; 245, 247; 246, identifier:chan_cond; 247, identifier:cyc_cond; 248, identifier:st_cond; 249, identifier:et_cond; 250, block; 250, 251; 251, expression_statement; 251, 252; 252, call; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:new_times; 255, identifier:extend; 256, argument_list; 256, 257; 257, subscript; 257, 258; 257, 259; 258, identifier:bund; 259, string:'times'; 260, expression_statement; 260, 261; 261, assignment; 261, 262; 261, 263; 262, identifier:new_times; 263, call; 263, 264; 263, 265; 264, identifier:sorted; 265, argument_list; 265, 266; 265, 267; 266, identifier:new_times; 267, keyword_argument; 267, 268; 267, 269; 268, identifier:key; 269, lambda; 269, 270; 269, 272; 270, lambda_parameters; 270, 271; 271, identifier:x; 272, subscript; 272, 273; 272, 274; 273, identifier:x; 274, integer:0; 275, expression_statement; 275, 276; 276, assignment; 276, 277; 276, 278; 277, identifier:new_bund; 278, dictionary; 278, 279; 278, 282; 278, 285; 278, 288; 278, 291; 279, pair; 279, 280; 279, 281; 280, string:'times'; 281, identifier:new_times; 282, pair; 282, 283; 282, 284; 283, string:'chan'; 284, identifier:ch; 285, pair; 285, 286; 285, 287; 286, string:'cycle'; 287, identifier:cyc; 288, pair; 288, 289; 288, 290; 289, string:'stage'; 290, identifier:st; 291, pair; 291, 292; 291, 293; 292, string:'name'; 293, identifier:et; 294, expression_statement; 294, 295; 295, call; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:to_concat; 298, identifier:append; 299, argument_list; 299, 300; 300, identifier:new_bund; 301, if_statement; 301, 302; 301, 306; 302, not_operator; 302, 303; 303, subscript; 303, 304; 303, 305; 304, identifier:cat; 305, integer:2; 306, block; 306, 307; 306, 311; 306, 405; 307, expression_statement; 307, 308; 308, assignment; 308, 309; 308, 310; 309, identifier:to_concat_new; 310, list:[]; 311, for_statement; 311, 312; 311, 313; 311, 314; 312, identifier:bund; 313, identifier:to_concat; 314, block; 314, 315; 314, 319; 314, 330; 314, 334; 315, expression_statement; 315, 316; 316, assignment; 316, 317; 316, 318; 317, identifier:last; 318, None; 319, expression_statement; 319, 320; 320, call; 320, 321; 320, 326; 321, attribute; 321, 322; 321, 325; 322, subscript; 322, 323; 322, 324; 323, identifier:bund; 324, string:'times'; 325, identifier:append; 326, argument_list; 326, 327; 327, tuple; 327, 328; 327, 329; 328, identifier:inf; 329, identifier:inf; 330, expression_statement; 330, 331; 331, assignment; 331, 332; 331, 333; 332, identifier:start; 333, integer:0; 334, for_statement; 334, 335; 334, 338; 334, 344; 335, pattern_list; 335, 336; 335, 337; 336, identifier:i; 337, identifier:j; 338, call; 338, 339; 338, 340; 339, identifier:enumerate; 340, argument_list; 340, 341; 341, subscript; 341, 342; 341, 343; 342, identifier:bund; 343, string:'times'; 344, block; 344, 345; 344, 399; 345, if_statement; 345, 346; 345, 349; 346, comparison_operator:is; 346, 347; 346, 348; 347, identifier:last; 348, None; 349, block; 349, 350; 350, if_statement; 350, 351; 350, 362; 351, not_operator; 351, 352; 352, call; 352, 353; 352, 354; 353, identifier:isclose; 354, argument_list; 354, 355; 354, 358; 354, 359; 355, subscript; 355, 356; 355, 357; 356, identifier:j; 357, integer:0; 358, identifier:last; 359, keyword_argument; 359, 360; 359, 361; 360, identifier:abs_tol; 361, float:0.01; 362, block; 362, 363; 362, 374; 362, 382; 362, 388; 362, 395; 363, expression_statement; 363, 364; 364, assignment; 364, 365; 364, 366; 365, identifier:new_times; 366, subscript; 366, 367; 366, 370; 367, subscript; 367, 368; 367, 369; 368, identifier:bund; 369, string:'times'; 370, slice; 370, 371; 370, 372; 370, 373; 371, identifier:start; 372, colon; 373, identifier:i; 374, expression_statement; 374, 375; 375, assignment; 375, 376; 375, 377; 376, identifier:new_bund; 377, call; 377, 378; 377, 381; 378, attribute; 378, 379; 378, 380; 379, identifier:bund; 380, identifier:copy; 381, argument_list; 382, expression_statement; 382, 383; 383, assignment; 383, 384; 383, 387; 384, subscript; 384, 385; 384, 386; 385, identifier:new_bund; 386, string:'times'; 387, identifier:new_times; 388, expression_statement; 388, 389; 389, call; 389, 390; 389, 393; 390, attribute; 390, 391; 390, 392; 391, identifier:to_concat_new; 392, identifier:append; 393, argument_list; 393, 394; 394, identifier:new_bund; 395, expression_statement; 395, 396; 396, assignment; 396, 397; 396, 398; 397, identifier:start; 398, identifier:i; 399, expression_statement; 399, 400; 400, assignment; 400, 401; 400, 402; 401, identifier:last; 402, subscript; 402, 403; 402, 404; 403, identifier:j; 404, integer:1; 405, expression_statement; 405, 406; 406, assignment; 406, 407; 406, 408; 407, identifier:to_concat; 408, identifier:to_concat_new; 409, expression_statement; 409, 410; 410, assignment; 410, 411; 410, 412; 411, identifier:to_concat; 412, list_comprehension; 412, 413; 412, 414; 412, 417; 413, identifier:x; 414, for_in_clause; 414, 415; 414, 416; 415, identifier:x; 416, identifier:to_concat; 417, if_clause; 417, 418; 418, subscript; 418, 419; 418, 420; 419, identifier:x; 420, string:'times'; 421, return_statement; 421, 422; 422, identifier:to_concat
def _concat(bundles, cat=(0, 0, 0, 0)): """Prepare event or epoch start and end times for concatenation.""" chan = sorted(set([x['chan'] for x in bundles])) cycle = sorted(set([x['cycle'] for x in bundles])) stage = sorted(set([x['stage'] for x in bundles])) evt_type = sorted(set([x['name'] for x in bundles])) all_cycle = None all_stage = None all_evt_type = None if cycle[0] is not None: all_cycle = ', '.join([str(c) for c in cycle]) if stage[0] is not None: all_stage = ', '.join(stage) if evt_type[0] is not None: all_evt_type = ', '.join(evt_type) if cat[0]: cycle = [all_cycle] if cat[1]: stage = [all_stage] if cat[3]: evt_type = [all_evt_type] to_concat = [] for ch in chan: for cyc in cycle: for st in stage: for et in evt_type: new_times = [] for bund in bundles: chan_cond = ch == bund['chan'] cyc_cond = cyc in (bund['cycle'], all_cycle) st_cond = st in (bund['stage'], all_stage) et_cond = et in (bund['name'], all_evt_type) if chan_cond and cyc_cond and st_cond and et_cond: new_times.extend(bund['times']) new_times = sorted(new_times, key=lambda x: x[0]) new_bund = {'times': new_times, 'chan': ch, 'cycle': cyc, 'stage': st, 'name': et } to_concat.append(new_bund) if not cat[2]: to_concat_new = [] for bund in to_concat: last = None bund['times'].append((inf,inf)) start = 0 for i, j in enumerate(bund['times']): if last is not None: if not isclose(j[0], last, abs_tol=0.01): new_times = bund['times'][start:i] new_bund = bund.copy() new_bund['times'] = new_times to_concat_new.append(new_bund) start = i last = j[1] to_concat = to_concat_new to_concat = [x for x in to_concat if x['times']] return to_concat
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:toggle_pac; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 95; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 12; 9, comparison_operator:is; 9, 10; 9, 11; 10, identifier:Pac; 11, None; 12, block; 12, 13; 12, 25; 12, 36; 12, 47; 12, 58; 12, 69; 12, 80; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:pac_on; 16, call; 16, 17; 16, 24; 17, attribute; 17, 18; 17, 23; 18, subscript; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:pac; 22, string:'pac_on'; 23, identifier:get_value; 24, argument_list; 25, expression_statement; 25, 26; 26, call; 26, 27; 26, 34; 27, attribute; 27, 28; 27, 33; 28, subscript; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:pac; 32, string:'prep'; 33, identifier:setEnabled; 34, argument_list; 34, 35; 35, identifier:pac_on; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 45; 38, attribute; 38, 39; 38, 44; 39, subscript; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:pac; 43, string:'box_metric'; 44, identifier:setEnabled; 45, argument_list; 45, 46; 46, identifier:pac_on; 47, expression_statement; 47, 48; 48, call; 48, 49; 48, 56; 49, attribute; 49, 50; 49, 55; 50, subscript; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:pac; 54, string:'box_complex'; 55, identifier:setEnabled; 56, argument_list; 56, 57; 57, identifier:pac_on; 58, expression_statement; 58, 59; 59, call; 59, 60; 59, 67; 60, attribute; 60, 61; 60, 66; 61, subscript; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:self; 64, identifier:pac; 65, string:'box_surro'; 66, identifier:setEnabled; 67, argument_list; 67, 68; 68, identifier:pac_on; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 78; 71, attribute; 71, 72; 71, 77; 72, subscript; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:self; 75, identifier:pac; 76, string:'box_opts'; 77, identifier:setEnabled; 78, argument_list; 78, 79; 79, identifier:pac_on; 80, if_statement; 80, 81; 80, 83; 81, not_operator; 81, 82; 82, identifier:pac_on; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, call; 85, 86; 85, 93; 86, attribute; 86, 87; 86, 92; 87, subscript; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:pac; 91, string:'prep'; 92, identifier:set_value; 93, argument_list; 93, 94; 94, False; 95, if_statement; 95, 96; 95, 101; 96, boolean_operator:and; 96, 97; 96, 100; 97, comparison_operator:is; 97, 98; 97, 99; 98, identifier:Pac; 99, None; 100, identifier:pac_on; 101, block; 101, 102; 101, 108; 101, 118; 101, 128; 101, 163; 101, 174; 101, 185; 101, 244; 101, 299; 101, 311; 101, 330; 101, 342; 101, 354; 101, 364; 101, 403; 101, 416; 101, 429; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:pac; 105, attribute; 105, 106; 105, 107; 106, identifier:self; 107, identifier:pac; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:hilb_on; 111, call; 111, 112; 111, 117; 112, attribute; 112, 113; 112, 116; 113, subscript; 113, 114; 113, 115; 114, identifier:pac; 115, string:'hilbert_on'; 116, identifier:isChecked; 117, argument_list; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:wav_on; 121, call; 121, 122; 121, 127; 122, attribute; 122, 123; 122, 126; 123, subscript; 123, 124; 123, 125; 124, identifier:pac; 125, string:'wavelet_on'; 126, identifier:isChecked; 127, argument_list; 128, for_statement; 128, 129; 128, 130; 128, 137; 129, identifier:button; 130, call; 130, 131; 130, 136; 131, attribute; 131, 132; 131, 135; 132, subscript; 132, 133; 132, 134; 133, identifier:pac; 134, string:'hilbert'; 135, identifier:values; 136, argument_list; 137, block; 137, 138; 137, 147; 138, expression_statement; 138, 139; 139, call; 139, 140; 139, 145; 140, attribute; 140, 141; 140, 144; 141, subscript; 141, 142; 141, 143; 142, identifier:button; 143, integer:0; 144, identifier:setEnabled; 145, argument_list; 145, 146; 146, identifier:hilb_on; 147, if_statement; 147, 148; 147, 153; 148, comparison_operator:is; 148, 149; 148, 152; 149, subscript; 149, 150; 149, 151; 150, identifier:button; 151, integer:1; 152, None; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, call; 155, 156; 155, 161; 156, attribute; 156, 157; 156, 160; 157, subscript; 157, 158; 157, 159; 158, identifier:button; 159, integer:1; 160, identifier:setEnabled; 161, argument_list; 161, 162; 162, identifier:hilb_on; 163, expression_statement; 163, 164; 164, call; 164, 165; 164, 172; 165, attribute; 165, 166; 165, 171; 166, subscript; 166, 167; 166, 170; 167, subscript; 167, 168; 167, 169; 168, identifier:pac; 169, string:'wav_width'; 170, integer:0; 171, identifier:setEnabled; 172, argument_list; 172, 173; 173, identifier:wav_on; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 183; 176, attribute; 176, 177; 176, 182; 177, subscript; 177, 178; 177, 181; 178, subscript; 178, 179; 178, 180; 179, identifier:pac; 180, string:'wav_width'; 181, integer:1; 182, identifier:setEnabled; 183, argument_list; 183, 184; 184, identifier:wav_on; 185, if_statement; 185, 186; 185, 197; 185, 220; 186, comparison_operator:in; 186, 187; 186, 194; 187, call; 187, 188; 187, 193; 188, attribute; 188, 189; 188, 192; 189, subscript; 189, 190; 189, 191; 190, identifier:pac; 191, string:'metric'; 192, identifier:get_value; 193, argument_list; 194, list:[ 'Kullback-Leibler Distance', 'Heights ratio']; 194, 195; 194, 196; 195, string:'Kullback-Leibler Distance'; 196, string:'Heights ratio'; 197, block; 197, 198; 197, 209; 198, expression_statement; 198, 199; 199, call; 199, 200; 199, 207; 200, attribute; 200, 201; 200, 206; 201, subscript; 201, 202; 201, 205; 202, subscript; 202, 203; 202, 204; 203, identifier:pac; 204, string:'nbin'; 205, integer:0; 206, identifier:setEnabled; 207, argument_list; 207, 208; 208, True; 209, expression_statement; 209, 210; 210, call; 210, 211; 210, 218; 211, attribute; 211, 212; 211, 217; 212, subscript; 212, 213; 212, 216; 213, subscript; 213, 214; 213, 215; 214, identifier:pac; 215, string:'nbin'; 216, integer:1; 217, identifier:setEnabled; 218, argument_list; 218, 219; 219, True; 220, else_clause; 220, 221; 221, block; 221, 222; 221, 233; 222, expression_statement; 222, 223; 223, call; 223, 224; 223, 231; 224, attribute; 224, 225; 224, 230; 225, subscript; 225, 226; 225, 229; 226, subscript; 226, 227; 226, 228; 227, identifier:pac; 228, string:'nbin'; 229, integer:0; 230, identifier:setEnabled; 231, argument_list; 231, 232; 232, False; 233, expression_statement; 233, 234; 234, call; 234, 235; 234, 242; 235, attribute; 235, 236; 235, 241; 236, subscript; 236, 237; 236, 240; 237, subscript; 237, 238; 237, 239; 238, identifier:pac; 239, string:'nbin'; 240, integer:1; 241, identifier:setEnabled; 242, argument_list; 242, 243; 243, False; 244, if_statement; 244, 245; 244, 250; 245, comparison_operator:==; 245, 246; 245, 249; 246, subscript; 246, 247; 246, 248; 247, identifier:pac; 248, string:'metric'; 249, string:'ndPac'; 250, block; 250, 251; 250, 286; 251, for_statement; 251, 252; 251, 253; 251, 260; 252, identifier:button; 253, call; 253, 254; 253, 259; 254, attribute; 254, 255; 254, 258; 255, subscript; 255, 256; 255, 257; 256, identifier:pac; 257, string:'surro'; 258, identifier:values; 259, argument_list; 260, block; 260, 261; 260, 270; 261, expression_statement; 261, 262; 262, call; 262, 263; 262, 268; 263, attribute; 263, 264; 263, 267; 264, subscript; 264, 265; 264, 266; 265, identifier:button; 266, integer:0; 267, identifier:setEnabled; 268, argument_list; 268, 269; 269, False; 270, if_statement; 270, 271; 270, 276; 271, comparison_operator:is; 271, 272; 271, 275; 272, subscript; 272, 273; 272, 274; 273, identifier:button; 274, integer:1; 275, None; 276, block; 276, 277; 277, expression_statement; 277, 278; 278, call; 278, 279; 278, 284; 279, attribute; 279, 280; 279, 283; 280, subscript; 280, 281; 280, 282; 281, identifier:button; 282, integer:1; 283, identifier:setEnabled; 284, argument_list; 284, 285; 285, False; 286, expression_statement; 286, 287; 287, call; 287, 288; 287, 297; 288, attribute; 288, 289; 288, 296; 289, subscript; 289, 290; 289, 295; 290, subscript; 290, 291; 290, 294; 291, subscript; 291, 292; 291, 293; 292, identifier:pac; 293, string:'surro'; 294, string:'pval'; 295, integer:0; 296, identifier:setEnabled; 297, argument_list; 297, 298; 298, True; 299, expression_statement; 299, 300; 300, assignment; 300, 301; 300, 302; 301, identifier:ndpac_on; 302, comparison_operator:==; 302, 303; 302, 310; 303, call; 303, 304; 303, 309; 304, attribute; 304, 305; 304, 308; 305, subscript; 305, 306; 305, 307; 306, identifier:pac; 307, string:'metric'; 308, identifier:get_value; 309, argument_list; 310, string:'ndPac'; 311, expression_statement; 311, 312; 312, assignment; 312, 313; 312, 314; 313, identifier:surro_on; 314, call; 314, 315; 314, 316; 315, identifier:logical_and; 316, argument_list; 316, 317; 316, 328; 317, comparison_operator:!=; 317, 318; 317, 325; 318, call; 318, 319; 318, 324; 319, attribute; 319, 320; 319, 323; 320, subscript; 320, 321; 320, 322; 321, identifier:pac; 322, string:'surro_method'; 323, identifier:get_value; 324, argument_list; 325, concatenated_string; 325, 326; 325, 327; 326, string:''; 327, string:'No surrogates'; 328, not_operator; 328, 329; 329, identifier:ndpac_on; 330, expression_statement; 330, 331; 331, assignment; 331, 332; 331, 333; 332, identifier:norm_on; 333, comparison_operator:!=; 333, 334; 333, 341; 334, call; 334, 335; 334, 340; 335, attribute; 335, 336; 335, 339; 336, subscript; 336, 337; 336, 338; 337, identifier:pac; 338, string:'surro_norm'; 339, identifier:get_value; 340, argument_list; 341, string:'No normalization'; 342, expression_statement; 342, 343; 343, assignment; 343, 344; 343, 345; 344, identifier:blocks_on; 345, comparison_operator:in; 345, 346; 345, 347; 346, string:'across time'; 347, call; 347, 348; 347, 353; 348, attribute; 348, 349; 348, 352; 349, subscript; 349, 350; 349, 351; 350, identifier:pac; 351, string:'surro_method'; 352, identifier:get_value; 353, argument_list; 354, expression_statement; 354, 355; 355, call; 355, 356; 355, 361; 356, attribute; 356, 357; 356, 360; 357, subscript; 357, 358; 357, 359; 358, identifier:pac; 359, string:'surro_method'; 360, identifier:setEnabled; 361, argument_list; 361, 362; 362, not_operator; 362, 363; 363, identifier:ndpac_on; 364, for_statement; 364, 365; 364, 366; 364, 373; 365, identifier:button; 366, call; 366, 367; 366, 372; 367, attribute; 367, 368; 367, 371; 368, subscript; 368, 369; 368, 370; 369, identifier:pac; 370, string:'surro'; 371, identifier:values; 372, argument_list; 373, block; 373, 374; 373, 385; 374, expression_statement; 374, 375; 375, call; 375, 376; 375, 381; 376, attribute; 376, 377; 376, 380; 377, subscript; 377, 378; 377, 379; 378, identifier:button; 379, integer:0; 380, identifier:setEnabled; 381, argument_list; 381, 382; 382, boolean_operator:and; 382, 383; 382, 384; 383, identifier:surro_on; 384, identifier:norm_on; 385, if_statement; 385, 386; 385, 391; 386, comparison_operator:is; 386, 387; 386, 390; 387, subscript; 387, 388; 387, 389; 388, identifier:button; 389, integer:1; 390, None; 391, block; 391, 392; 392, expression_statement; 392, 393; 393, call; 393, 394; 393, 399; 394, attribute; 394, 395; 394, 398; 395, subscript; 395, 396; 395, 397; 396, identifier:button; 397, integer:1; 398, identifier:setEnabled; 399, argument_list; 399, 400; 400, boolean_operator:and; 400, 401; 400, 402; 401, identifier:surro_on; 402, identifier:norm_on; 403, expression_statement; 403, 404; 404, call; 404, 405; 404, 414; 405, attribute; 405, 406; 405, 413; 406, subscript; 406, 407; 406, 412; 407, subscript; 407, 408; 407, 411; 408, subscript; 408, 409; 408, 410; 409, identifier:pac; 410, string:'surro'; 411, string:'nblocks'; 412, integer:0; 413, identifier:setEnabled; 414, argument_list; 414, 415; 415, identifier:blocks_on; 416, expression_statement; 416, 417; 417, call; 417, 418; 417, 427; 418, attribute; 418, 419; 418, 426; 419, subscript; 419, 420; 419, 425; 420, subscript; 420, 421; 420, 424; 421, subscript; 421, 422; 421, 423; 422, identifier:pac; 423, string:'surro'; 424, string:'nblocks'; 425, integer:1; 426, identifier:setEnabled; 427, argument_list; 427, 428; 428, identifier:blocks_on; 429, if_statement; 429, 430; 429, 431; 430, identifier:ndpac_on; 431, block; 431, 432; 431, 441; 432, expression_statement; 432, 433; 433, call; 433, 434; 433, 439; 434, attribute; 434, 435; 434, 438; 435, subscript; 435, 436; 435, 437; 436, identifier:pac; 437, string:'surro_method'; 438, identifier:set_value; 439, argument_list; 439, 440; 440, string:'No surrogates'; 441, expression_statement; 441, 442; 442, call; 442, 443; 442, 452; 443, attribute; 443, 444; 443, 451; 444, subscript; 444, 445; 444, 450; 445, subscript; 445, 446; 445, 449; 446, subscript; 446, 447; 446, 448; 447, identifier:pac; 448, string:'surro'; 449, string:'pval'; 450, integer:0; 451, identifier:setEnabled; 452, argument_list; 452, 453; 453, True
def toggle_pac(self): """Enable and disable PAC options.""" if Pac is not None: pac_on = self.pac['pac_on'].get_value() self.pac['prep'].setEnabled(pac_on) self.pac['box_metric'].setEnabled(pac_on) self.pac['box_complex'].setEnabled(pac_on) self.pac['box_surro'].setEnabled(pac_on) self.pac['box_opts'].setEnabled(pac_on) if not pac_on: self.pac['prep'].set_value(False) if Pac is not None and pac_on: pac = self.pac hilb_on = pac['hilbert_on'].isChecked() wav_on = pac['wavelet_on'].isChecked() for button in pac['hilbert'].values(): button[0].setEnabled(hilb_on) if button[1] is not None: button[1].setEnabled(hilb_on) pac['wav_width'][0].setEnabled(wav_on) pac['wav_width'][1].setEnabled(wav_on) if pac['metric'].get_value() in [ 'Kullback-Leibler Distance', 'Heights ratio']: pac['nbin'][0].setEnabled(True) pac['nbin'][1].setEnabled(True) else: pac['nbin'][0].setEnabled(False) pac['nbin'][1].setEnabled(False) if pac['metric'] == 'ndPac': for button in pac['surro'].values(): button[0].setEnabled(False) if button[1] is not None: button[1].setEnabled(False) pac['surro']['pval'][0].setEnabled(True) ndpac_on = pac['metric'].get_value() == 'ndPac' surro_on = logical_and(pac['surro_method'].get_value() != '' 'No surrogates', not ndpac_on) norm_on = pac['surro_norm'].get_value() != 'No normalization' blocks_on = 'across time' in pac['surro_method'].get_value() pac['surro_method'].setEnabled(not ndpac_on) for button in pac['surro'].values(): button[0].setEnabled(surro_on and norm_on) if button[1] is not None: button[1].setEnabled(surro_on and norm_on) pac['surro']['nblocks'][0].setEnabled(blocks_on) pac['surro']['nblocks'][1].setEnabled(blocks_on) if ndpac_on: pac['surro_method'].set_value('No surrogates') pac['surro']['pval'][0].setEnabled(True)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_segments; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 31; 5, 41; 5, 53; 5, 65; 5, 69; 5, 73; 5, 123; 5, 124; 5, 134; 5, 135; 5, 142; 5, 143; 5, 147; 5, 151; 5, 218; 5, 219; 5, 231; 5, 232; 5, 242; 5, 278; 5, 279; 5, 301; 5, 329; 5, 330; 5, 340; 5, 388; 5, 389; 5, 399; 5, 409; 5, 410; 5, 424; 5, 480; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:chunk; 12, dictionary_comprehension; 12, 13; 12, 20; 13, pair; 13, 14; 13, 15; 14, identifier:k; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:v; 18, identifier:isChecked; 19, argument_list; 20, for_in_clause; 20, 21; 20, 24; 21, pattern_list; 21, 22; 21, 23; 22, identifier:k; 23, identifier:v; 24, call; 24, 25; 24, 30; 25, attribute; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:chunk; 29, identifier:items; 30, argument_list; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:lock_to_staging; 34, call; 34, 35; 34, 40; 35, attribute; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:lock_to_staging; 39, identifier:get_value; 40, argument_list; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:epoch_dur; 44, call; 44, 45; 44, 52; 45, attribute; 45, 46; 45, 51; 46, subscript; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:epoch_param; 50, string:'dur'; 51, identifier:get_value; 52, argument_list; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:epoch_overlap; 56, call; 56, 57; 56, 64; 57, attribute; 57, 58; 57, 63; 58, subscript; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:epoch_param; 62, string:'overlap_val'; 63, identifier:value; 64, argument_list; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:epoch_step; 68, None; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:epoch; 72, None; 73, if_statement; 73, 74; 73, 77; 74, subscript; 74, 75; 74, 76; 75, identifier:chunk; 76, string:'epoch'; 77, block; 77, 78; 78, if_statement; 78, 79; 78, 80; 78, 85; 79, identifier:lock_to_staging; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:epoch; 84, string:'locked'; 85, else_clause; 85, 86; 86, block; 86, 87; 86, 91; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:epoch; 90, string:'unlocked'; 91, if_statement; 91, 92; 91, 101; 92, call; 92, 93; 92, 100; 93, attribute; 93, 94; 93, 99; 94, subscript; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:epoch_param; 98, string:'step'; 99, identifier:isChecked; 100, argument_list; 101, block; 101, 102; 101, 114; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:epoch_step; 105, call; 105, 106; 105, 113; 106, attribute; 106, 107; 106, 112; 107, subscript; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:self; 110, identifier:epoch_param; 111, string:'step_val'; 112, identifier:get_value; 113, argument_list; 114, if_statement; 114, 115; 114, 118; 115, comparison_operator:<=; 115, 116; 115, 117; 116, identifier:epoch_step; 117, integer:0; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:epoch_step; 122, float:0.1; 123, comment; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:self; 128, identifier:chan; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:self; 132, identifier:get_channels; 133, argument_list; 134, comment; 135, if_statement; 135, 136; 135, 140; 136, not_operator; 136, 137; 137, attribute; 137, 138; 137, 139; 138, identifier:self; 139, identifier:chan; 140, block; 140, 141; 141, return_statement; 142, comment; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:chan_full; 146, None; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:evt_type; 150, None; 151, if_statement; 151, 152; 151, 155; 152, subscript; 152, 153; 152, 154; 153, identifier:chunk; 154, string:'event'; 155, block; 155, 156; 155, 189; 155, 199; 156, if_statement; 156, 157; 156, 164; 157, call; 157, 158; 157, 163; 158, attribute; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:self; 161, identifier:evt_chan_only; 162, identifier:get_value; 163, argument_list; 164, block; 164, 165; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:chan_full; 168, list_comprehension; 168, 169; 168, 184; 169, binary_operator:+; 169, 170; 169, 181; 170, binary_operator:+; 170, 171; 170, 174; 171, binary_operator:+; 171, 172; 171, 173; 172, identifier:i; 173, string:' ('; 174, call; 174, 175; 174, 180; 175, attribute; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:self; 178, identifier:idx_group; 179, identifier:currentText; 180, argument_list; 181, concatenated_string; 181, 182; 181, 183; 182, string:''; 183, string:')'; 184, for_in_clause; 184, 185; 184, 186; 185, identifier:i; 186, attribute; 186, 187; 186, 188; 187, identifier:self; 188, identifier:chan; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:evt_type; 192, call; 192, 193; 192, 198; 193, attribute; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:self; 196, identifier:idx_evt_type; 197, identifier:selectedItems; 198, argument_list; 199, if_statement; 199, 200; 199, 202; 199, 204; 200, not_operator; 200, 201; 201, identifier:evt_type; 202, block; 202, 203; 203, return_statement; 204, else_clause; 204, 205; 205, block; 205, 206; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:evt_type; 209, list_comprehension; 209, 210; 209, 215; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:x; 213, identifier:text; 214, argument_list; 215, for_in_clause; 215, 216; 215, 217; 216, identifier:x; 217, identifier:evt_type; 218, comment; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 222; 221, identifier:cycle; 222, assignment; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:self; 225, identifier:cycle; 226, call; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:self; 229, identifier:get_cycles; 230, argument_list; 231, comment; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 235; 234, identifier:stage; 235, call; 235, 236; 235, 241; 236, attribute; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:self; 239, identifier:idx_stage; 240, identifier:selectedItems; 241, argument_list; 242, if_statement; 242, 243; 242, 245; 242, 254; 243, not_operator; 243, 244; 244, identifier:stage; 245, block; 245, 246; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:stage; 249, assignment; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:self; 252, identifier:stage; 253, None; 254, else_clause; 254, 255; 255, block; 255, 256; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 259; 258, identifier:stage; 259, assignment; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:self; 262, identifier:stage; 263, list_comprehension; 263, 264; 263, 269; 264, call; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:x; 267, identifier:text; 268, argument_list; 269, for_in_clause; 269, 270; 269, 271; 270, identifier:x; 271, call; 271, 272; 271, 277; 272, attribute; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:self; 275, identifier:idx_stage; 276, identifier:selectedItems; 277, argument_list; 278, comment; 279, expression_statement; 279, 280; 280, assignment; 280, 281; 280, 282; 281, identifier:cat; 282, dictionary_comprehension; 282, 283; 282, 290; 283, pair; 283, 284; 283, 285; 284, identifier:k; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:v; 288, identifier:get_value; 289, argument_list; 290, for_in_clause; 290, 291; 290, 294; 291, pattern_list; 291, 292; 291, 293; 292, identifier:k; 293, identifier:v; 294, call; 294, 295; 294, 300; 295, attribute; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:self; 298, identifier:cat; 299, identifier:items; 300, argument_list; 301, expression_statement; 301, 302; 302, assignment; 302, 303; 302, 304; 303, identifier:cat; 304, tuple; 304, 305; 304, 311; 304, 317; 304, 323; 305, call; 305, 306; 305, 307; 306, identifier:int; 307, argument_list; 307, 308; 308, subscript; 308, 309; 308, 310; 309, identifier:cat; 310, string:'cycle'; 311, call; 311, 312; 311, 313; 312, identifier:int; 313, argument_list; 313, 314; 314, subscript; 314, 315; 314, 316; 315, identifier:cat; 316, string:'stage'; 317, call; 317, 318; 317, 319; 318, identifier:int; 319, argument_list; 319, 320; 320, subscript; 320, 321; 320, 322; 321, identifier:cat; 322, string:'discontinuous'; 323, call; 323, 324; 323, 325; 324, identifier:int; 325, argument_list; 325, 326; 326, subscript; 326, 327; 326, 328; 327, identifier:cat; 328, string:'evt_type'; 329, comment; 330, expression_statement; 330, 331; 331, assignment; 331, 332; 331, 333; 332, identifier:reject_event; 333, call; 333, 334; 333, 339; 334, attribute; 334, 335; 334, 338; 335, attribute; 335, 336; 335, 337; 336, identifier:self; 337, identifier:reject_event; 338, identifier:get_value; 339, argument_list; 340, if_statement; 340, 341; 340, 344; 340, 373; 340, 382; 341, comparison_operator:==; 341, 342; 341, 343; 342, identifier:reject_event; 343, string:'channel-specific'; 344, block; 344, 345; 344, 369; 345, expression_statement; 345, 346; 346, assignment; 346, 347; 346, 348; 347, identifier:chan_full; 348, list_comprehension; 348, 349; 348, 364; 349, binary_operator:+; 349, 350; 349, 361; 350, binary_operator:+; 350, 351; 350, 354; 351, binary_operator:+; 351, 352; 351, 353; 352, identifier:i; 353, string:' ('; 354, call; 354, 355; 354, 360; 355, attribute; 355, 356; 355, 359; 356, attribute; 356, 357; 356, 358; 357, identifier:self; 358, identifier:idx_group; 359, identifier:currentText; 360, argument_list; 361, concatenated_string; 361, 362; 361, 363; 362, string:''; 363, string:')'; 364, for_in_clause; 364, 365; 364, 366; 365, identifier:i; 366, attribute; 366, 367; 366, 368; 367, identifier:self; 368, identifier:chan; 369, expression_statement; 369, 370; 370, assignment; 370, 371; 370, 372; 371, identifier:reject_artf; 372, True; 373, elif_clause; 373, 374; 373, 377; 374, comparison_operator:==; 374, 375; 374, 376; 375, identifier:reject_event; 376, string:'from any channel'; 377, block; 377, 378; 378, expression_statement; 378, 379; 379, assignment; 379, 380; 379, 381; 380, identifier:reject_artf; 381, True; 382, else_clause; 382, 383; 383, block; 383, 384; 384, expression_statement; 384, 385; 385, assignment; 385, 386; 385, 387; 386, identifier:reject_artf; 387, False; 388, comment; 389, expression_statement; 389, 390; 390, assignment; 390, 391; 390, 392; 391, identifier:min_dur; 392, call; 392, 393; 392, 398; 393, attribute; 393, 394; 393, 397; 394, attribute; 394, 395; 394, 396; 395, identifier:self; 396, identifier:min_dur; 397, identifier:get_value; 398, argument_list; 399, expression_statement; 399, 400; 400, assignment; 400, 401; 400, 402; 401, identifier:reject_epoch; 402, call; 402, 403; 402, 408; 403, attribute; 403, 404; 403, 407; 404, attribute; 404, 405; 404, 406; 405, identifier:self; 406, identifier:reject_epoch; 407, identifier:get_value; 408, argument_list; 409, comment; 410, expression_statement; 410, 411; 411, assignment; 411, 412; 411, 415; 412, attribute; 412, 413; 412, 414; 413, identifier:self; 414, identifier:title; 415, call; 415, 416; 415, 419; 416, attribute; 416, 417; 416, 418; 417, identifier:self; 418, identifier:make_title; 419, argument_list; 419, 420; 419, 421; 419, 422; 419, 423; 420, identifier:chan_full; 421, identifier:cycle; 422, identifier:stage; 423, identifier:evt_type; 424, expression_statement; 424, 425; 425, assignment; 425, 426; 425, 427; 426, identifier:segments; 427, call; 427, 428; 427, 429; 428, identifier:fetch; 429, argument_list; 429, 430; 429, 437; 429, 444; 429, 447; 429, 450; 429, 453; 429, 456; 429, 459; 429, 462; 429, 465; 429, 468; 429, 471; 429, 474; 429, 477; 430, attribute; 430, 431; 430, 436; 431, attribute; 431, 432; 431, 435; 432, attribute; 432, 433; 432, 434; 433, identifier:self; 434, identifier:parent; 435, identifier:info; 436, identifier:dataset; 437, attribute; 437, 438; 437, 443; 438, attribute; 438, 439; 438, 442; 439, attribute; 439, 440; 439, 441; 440, identifier:self; 441, identifier:parent; 442, identifier:notes; 443, identifier:annot; 444, keyword_argument; 444, 445; 444, 446; 445, identifier:cat; 446, identifier:cat; 447, keyword_argument; 447, 448; 447, 449; 448, identifier:evt_type; 449, identifier:evt_type; 450, keyword_argument; 450, 451; 450, 452; 451, identifier:stage; 452, identifier:stage; 453, keyword_argument; 453, 454; 453, 455; 454, identifier:cycle; 455, identifier:cycle; 456, keyword_argument; 456, 457; 456, 458; 457, identifier:chan_full; 458, identifier:chan_full; 459, keyword_argument; 459, 460; 459, 461; 460, identifier:epoch; 461, identifier:epoch; 462, keyword_argument; 462, 463; 462, 464; 463, identifier:epoch_dur; 464, identifier:epoch_dur; 465, keyword_argument; 465, 466; 465, 467; 466, identifier:epoch_overlap; 467, identifier:epoch_overlap; 468, keyword_argument; 468, 469; 468, 470; 469, identifier:epoch_step; 470, identifier:epoch_step; 471, keyword_argument; 471, 472; 471, 473; 472, identifier:reject_epoch; 473, identifier:reject_epoch; 474, keyword_argument; 474, 475; 474, 476; 475, identifier:reject_artf; 476, identifier:reject_artf; 477, keyword_argument; 477, 478; 477, 479; 478, identifier:min_dur; 479, identifier:min_dur; 480, return_statement; 480, 481; 481, identifier:segments
def get_segments(self): """Get segments for analysis. Creates instance of trans.Segments.""" # Chunking chunk = {k: v.isChecked() for k, v in self.chunk.items()} lock_to_staging = self.lock_to_staging.get_value() epoch_dur = self.epoch_param['dur'].get_value() epoch_overlap = self.epoch_param['overlap_val'].value() epoch_step = None epoch = None if chunk['epoch']: if lock_to_staging: epoch = 'locked' else: epoch = 'unlocked' if self.epoch_param['step'].isChecked(): epoch_step = self.epoch_param['step_val'].get_value() if epoch_step <= 0: epoch_step = 0.1 # Which channel(s) self.chan = self.get_channels() # chan name without group if not self.chan: return # Which event type(s) chan_full = None evt_type = None if chunk['event']: if self.evt_chan_only.get_value(): chan_full = [i + ' (' + self.idx_group.currentText() + '' ')' for i in self.chan] evt_type = self.idx_evt_type.selectedItems() if not evt_type: return else: evt_type = [x.text() for x in evt_type] # Which cycle(s) cycle = self.cycle = self.get_cycles() # Which stage(s) stage = self.idx_stage.selectedItems() if not stage: stage = self.stage = None else: stage = self.stage = [ x.text() for x in self.idx_stage.selectedItems()] # Concatenation cat = {k: v.get_value() for k, v in self.cat.items()} cat = (int(cat['cycle']), int(cat['stage']), int(cat['discontinuous']), int(cat['evt_type'])) # Artefact event rejection reject_event = self.reject_event.get_value() if reject_event == 'channel-specific': chan_full = [i + ' (' + self.idx_group.currentText() + '' ')' for i in self.chan] reject_artf = True elif reject_event == 'from any channel': reject_artf = True else: reject_artf = False # Other options min_dur = self.min_dur.get_value() reject_epoch = self.reject_epoch.get_value() # Generate title for summary plot self.title = self.make_title(chan_full, cycle, stage, evt_type) segments = fetch(self.parent.info.dataset, self.parent.notes.annot, cat=cat, evt_type=evt_type, stage=stage, cycle=cycle, chan_full=chan_full, epoch=epoch, epoch_dur=epoch_dur, epoch_overlap=epoch_overlap, epoch_step=epoch_step, reject_epoch=reject_epoch, reject_artf=reject_artf, min_dur=min_dur) return segments
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:compute_evt_params; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 14; 5, 36; 5, 60; 5, 84; 5, 106; 5, 116; 5, 126; 5, 134; 5, 140; 5, 155; 5, 177; 5, 181; 5, 185; 5, 199; 5, 273; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:ev; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:event; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:glob; 17, dictionary_comprehension; 17, 18; 17, 25; 18, pair; 18, 19; 18, 20; 19, identifier:k; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:v; 23, identifier:get_value; 24, argument_list; 25, for_in_clause; 25, 26; 25, 29; 26, pattern_list; 26, 27; 26, 28; 27, identifier:k; 28, identifier:v; 29, call; 29, 30; 29, 35; 30, attribute; 30, 31; 30, 34; 31, subscript; 31, 32; 31, 33; 32, identifier:ev; 33, string:'global'; 34, identifier:items; 35, argument_list; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:params; 39, dictionary_comprehension; 39, 40; 39, 49; 40, pair; 40, 41; 40, 42; 41, identifier:k; 42, call; 42, 43; 42, 48; 43, attribute; 43, 44; 43, 47; 44, subscript; 44, 45; 44, 46; 45, identifier:v; 46, integer:0; 47, identifier:get_value; 48, argument_list; 49, for_in_clause; 49, 50; 49, 53; 50, pattern_list; 50, 51; 50, 52; 51, identifier:k; 52, identifier:v; 53, call; 53, 54; 53, 59; 54, attribute; 54, 55; 54, 58; 55, subscript; 55, 56; 55, 57; 56, identifier:ev; 57, string:'local'; 58, identifier:items; 59, argument_list; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:prep; 63, dictionary_comprehension; 63, 64; 63, 73; 64, pair; 64, 65; 64, 66; 65, identifier:k; 66, call; 66, 67; 66, 72; 67, attribute; 67, 68; 67, 71; 68, subscript; 68, 69; 68, 70; 69, identifier:v; 70, integer:1; 71, identifier:get_value; 72, argument_list; 73, for_in_clause; 73, 74; 73, 77; 74, pattern_list; 74, 75; 74, 76; 75, identifier:k; 76, identifier:v; 77, call; 77, 78; 77, 83; 78, attribute; 78, 79; 78, 82; 79, subscript; 79, 80; 79, 81; 80, identifier:ev; 81, string:'local'; 82, identifier:items; 83, argument_list; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:slopes; 87, dictionary_comprehension; 87, 88; 87, 95; 88, pair; 88, 89; 88, 90; 89, identifier:k; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:v; 93, identifier:get_value; 94, argument_list; 95, for_in_clause; 95, 96; 95, 99; 96, pattern_list; 96, 97; 96, 98; 97, identifier:k; 98, identifier:v; 99, call; 99, 100; 99, 105; 100, attribute; 100, 101; 100, 104; 101, subscript; 101, 102; 101, 103; 102, identifier:ev; 103, string:'sw'; 104, identifier:items; 105, argument_list; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:f1; 109, call; 109, 110; 109, 115; 110, attribute; 110, 111; 110, 114; 111, subscript; 111, 112; 111, 113; 112, identifier:ev; 113, string:'f1'; 114, identifier:get_value; 115, argument_list; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:f2; 119, call; 119, 120; 119, 125; 120, attribute; 120, 121; 120, 124; 121, subscript; 121, 122; 121, 123; 122, identifier:ev; 123, string:'f2'; 124, identifier:get_value; 125, argument_list; 126, if_statement; 126, 127; 126, 129; 127, not_operator; 127, 128; 128, identifier:f2; 129, block; 129, 130; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:f2; 133, None; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:band; 137, tuple; 137, 138; 137, 139; 138, identifier:f1; 139, identifier:f2; 140, if_statement; 140, 141; 140, 150; 141, not_operator; 141, 142; 142, parenthesized_expression; 142, 143; 143, boolean_operator:or; 143, 144; 143, 147; 144, subscript; 144, 145; 144, 146; 145, identifier:slopes; 146, string:'avg_slope'; 147, subscript; 147, 148; 147, 149; 148, identifier:slopes; 149, string:'max_slope'; 150, block; 150, 151; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:slopes; 154, None; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:evt_dat; 158, call; 158, 159; 158, 160; 159, identifier:event_params; 160, argument_list; 160, 161; 160, 164; 160, 165; 160, 168; 160, 171; 160, 174; 161, attribute; 161, 162; 161, 163; 162, identifier:self; 163, identifier:data; 164, identifier:params; 165, keyword_argument; 165, 166; 165, 167; 166, identifier:band; 167, identifier:band; 168, keyword_argument; 168, 169; 168, 170; 169, identifier:slopes; 170, identifier:slopes; 171, keyword_argument; 171, 172; 171, 173; 172, identifier:prep; 173, identifier:prep; 174, keyword_argument; 174, 175; 174, 176; 175, identifier:parent; 176, identifier:self; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:count; 180, None; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:density; 184, None; 185, if_statement; 185, 186; 185, 189; 186, subscript; 186, 187; 186, 188; 187, identifier:glob; 188, string:'count'; 189, block; 189, 190; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:count; 193, call; 193, 194; 193, 195; 194, identifier:len; 195, argument_list; 195, 196; 196, attribute; 196, 197; 196, 198; 197, identifier:self; 198, identifier:data; 199, if_statement; 199, 200; 199, 203; 200, subscript; 200, 201; 200, 202; 201, identifier:glob; 202, string:'density'; 203, block; 203, 204; 203, 210; 203, 211; 203, 237; 203, 259; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:epoch_dur; 207, subscript; 207, 208; 207, 209; 208, identifier:glob; 209, string:'density_per'; 210, comment; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:poi; 214, call; 214, 215; 214, 216; 215, identifier:get_times; 216, argument_list; 216, 217; 216, 224; 216, 229; 216, 234; 217, attribute; 217, 218; 217, 223; 218, attribute; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:self; 221, identifier:parent; 222, identifier:notes; 223, identifier:annot; 224, keyword_argument; 224, 225; 224, 226; 225, identifier:stage; 226, attribute; 226, 227; 226, 228; 227, identifier:self; 228, identifier:stage; 229, keyword_argument; 229, 230; 229, 231; 230, identifier:cycle; 231, attribute; 231, 232; 231, 233; 232, identifier:self; 233, identifier:cycle; 234, keyword_argument; 234, 235; 234, 236; 235, identifier:exclude; 236, True; 237, expression_statement; 237, 238; 238, assignment; 238, 239; 238, 240; 239, identifier:total_dur; 240, call; 240, 241; 240, 242; 241, identifier:sum; 242, argument_list; 242, 243; 243, list_comprehension; 243, 244; 243, 251; 243, 254; 244, binary_operator:-; 244, 245; 244, 248; 245, subscript; 245, 246; 245, 247; 246, identifier:x; 247, integer:1; 248, subscript; 248, 249; 248, 250; 249, identifier:x; 250, integer:0; 251, for_in_clause; 251, 252; 251, 253; 252, identifier:y; 253, identifier:poi; 254, for_in_clause; 254, 255; 254, 256; 255, identifier:x; 256, subscript; 256, 257; 256, 258; 257, identifier:y; 258, string:'times'; 259, expression_statement; 259, 260; 260, assignment; 260, 261; 260, 262; 261, identifier:density; 262, binary_operator:/; 262, 263; 262, 269; 263, call; 263, 264; 263, 265; 264, identifier:len; 265, argument_list; 265, 266; 266, attribute; 266, 267; 266, 268; 267, identifier:self; 268, identifier:data; 269, parenthesized_expression; 269, 270; 270, binary_operator:/; 270, 271; 270, 272; 271, identifier:total_dur; 272, identifier:epoch_dur; 273, return_statement; 273, 274; 274, expression_list; 274, 275; 274, 276; 274, 277; 275, identifier:evt_dat; 276, identifier:count; 277, identifier:density
def compute_evt_params(self): """Compute event parameters.""" ev = self.event glob = {k: v.get_value() for k, v in ev['global'].items()} params = {k: v[0].get_value() for k, v in ev['local'].items()} prep = {k: v[1].get_value() for k, v in ev['local'].items()} slopes = {k: v.get_value() for k, v in ev['sw'].items()} f1 = ev['f1'].get_value() f2 = ev['f2'].get_value() if not f2: f2 = None band = (f1, f2) if not (slopes['avg_slope'] or slopes['max_slope']): slopes = None evt_dat = event_params(self.data, params, band=band, slopes=slopes, prep=prep, parent=self) count = None density = None if glob['count']: count = len(self.data) if glob['density']: epoch_dur = glob['density_per'] # get period of interest based on stage and cycle selection poi = get_times(self.parent.notes.annot, stage=self.stage, cycle=self.cycle, exclude=True) total_dur = sum([x[1] - x[0] for y in poi for x in y['times']]) density = len(self.data) / (total_dur / epoch_dur) return evt_dat, count, density
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:detect_format; 3, parameters; 3, 4; 4, identifier:filename; 5, block; 5, 6; 5, 8; 5, 15; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:filename; 11, call; 11, 12; 11, 13; 12, identifier:Path; 13, argument_list; 13, 14; 14, identifier:filename; 15, if_statement; 15, 16; 15, 21; 15, 107; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:filename; 19, identifier:is_dir; 20, argument_list; 21, block; 21, 22; 22, if_statement; 22, 23; 22, 42; 22, 45; 22, 57; 22, 69; 22, 82; 22, 95; 23, boolean_operator:and; 23, 24; 23, 33; 24, call; 24, 25; 24, 26; 25, identifier:list; 26, argument_list; 26, 27; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:filename; 30, identifier:glob; 31, argument_list; 31, 32; 32, string:'*.stc'; 33, call; 33, 34; 33, 35; 34, identifier:list; 35, argument_list; 35, 36; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:filename; 39, identifier:glob; 40, argument_list; 40, 41; 41, string:'*.erd'; 42, block; 42, 43; 43, return_statement; 43, 44; 44, identifier:Ktlx; 45, elif_clause; 45, 46; 45, 54; 46, call; 46, 47; 46, 53; 47, attribute; 47, 48; 47, 52; 48, parenthesized_expression; 48, 49; 49, binary_operator:/; 49, 50; 49, 51; 50, identifier:filename; 51, string:'patient.info'; 52, identifier:exists; 53, argument_list; 54, block; 54, 55; 55, return_statement; 55, 56; 56, identifier:Moberg; 57, elif_clause; 57, 58; 57, 66; 58, call; 58, 59; 58, 65; 59, attribute; 59, 60; 59, 64; 60, parenthesized_expression; 60, 61; 61, binary_operator:/; 61, 62; 61, 63; 62, identifier:filename; 63, string:'info.xml'; 64, identifier:exists; 65, argument_list; 66, block; 66, 67; 67, return_statement; 67, 68; 68, identifier:EgiMff; 69, elif_clause; 69, 70; 69, 79; 70, call; 70, 71; 70, 72; 71, identifier:list; 72, argument_list; 72, 73; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:filename; 76, identifier:glob; 77, argument_list; 77, 78; 78, string:'*.openephys'; 79, block; 79, 80; 80, return_statement; 80, 81; 81, identifier:OpenEphys; 82, elif_clause; 82, 83; 82, 92; 83, call; 83, 84; 83, 85; 84, identifier:list; 85, argument_list; 85, 86; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:filename; 89, identifier:glob; 90, argument_list; 90, 91; 91, string:'*.txt'; 92, block; 92, 93; 93, return_statement; 93, 94; 94, identifier:Text; 95, else_clause; 95, 96; 96, block; 96, 97; 97, raise_statement; 97, 98; 98, call; 98, 99; 98, 100; 99, identifier:UnrecognizedFormat; 100, argument_list; 100, 101; 101, binary_operator:+; 101, 102; 101, 103; 102, string:'Unrecognized format for directory '; 103, call; 103, 104; 103, 105; 104, identifier:str; 105, argument_list; 105, 106; 106, identifier:filename; 107, else_clause; 107, 108; 108, block; 108, 109; 108, 118; 108, 131; 108, 140; 108, 149; 108, 158; 108, 173; 108, 199; 108, 243; 109, if_statement; 109, 110; 109, 115; 110, comparison_operator:==; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:filename; 113, identifier:suffix; 114, string:'.won'; 115, block; 115, 116; 116, return_statement; 116, 117; 117, identifier:Wonambi; 118, if_statement; 118, 119; 118, 128; 119, comparison_operator:==; 119, 120; 119, 127; 120, call; 120, 121; 120, 126; 121, attribute; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:filename; 124, identifier:suffix; 125, identifier:lower; 126, argument_list; 127, string:'.trc'; 128, block; 128, 129; 129, return_statement; 129, 130; 130, identifier:Micromed; 131, if_statement; 131, 132; 131, 137; 132, comparison_operator:==; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:filename; 135, identifier:suffix; 136, string:'.set'; 137, block; 137, 138; 138, return_statement; 138, 139; 139, identifier:EEGLAB; 140, if_statement; 140, 141; 140, 146; 141, comparison_operator:==; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:filename; 144, identifier:suffix; 145, string:'.edf'; 146, block; 146, 147; 147, return_statement; 147, 148; 148, identifier:Edf; 149, if_statement; 149, 150; 149, 155; 150, comparison_operator:==; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:filename; 153, identifier:suffix; 154, string:'.abf'; 155, block; 155, 156; 156, return_statement; 156, 157; 157, identifier:Abf; 158, if_statement; 158, 159; 158, 170; 159, boolean_operator:or; 159, 160; 159, 165; 160, comparison_operator:==; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:filename; 163, identifier:suffix; 164, string:'.vhdr'; 165, comparison_operator:==; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:filename; 168, identifier:suffix; 169, string:'.eeg'; 170, block; 170, 171; 171, return_statement; 171, 172; 172, identifier:BrainVision; 173, if_statement; 173, 174; 173, 179; 173, 180; 174, comparison_operator:==; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:filename; 177, identifier:suffix; 178, string:'.dat'; 179, comment; 180, block; 180, 181; 181, try_statement; 181, 182; 181, 188; 181, 195; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, call; 184, 185; 184, 186; 185, identifier:_read_header_length; 186, argument_list; 186, 187; 187, identifier:filename; 188, except_clause; 188, 189; 188, 192; 188, 193; 189, tuple; 189, 190; 189, 191; 190, identifier:AttributeError; 191, identifier:ValueError; 192, comment; 193, block; 193, 194; 194, pass_statement; 195, else_clause; 195, 196; 196, block; 196, 197; 197, return_statement; 197, 198; 198, identifier:BCI2000; 199, with_statement; 199, 200; 199, 211; 200, with_clause; 200, 201; 201, with_item; 201, 202; 202, as_pattern; 202, 203; 202, 209; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:filename; 206, identifier:open; 207, argument_list; 207, 208; 208, string:'rb'; 209, as_pattern_target; 209, 210; 210, identifier:f; 211, block; 211, 212; 211, 221; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:file_header; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:f; 218, identifier:read; 219, argument_list; 219, 220; 220, integer:8; 221, if_statement; 221, 222; 221, 228; 221, 231; 222, comparison_operator:in; 222, 223; 222, 224; 223, identifier:file_header; 224, tuple; 224, 225; 224, 226; 224, 227; 225, string:b'NEURALCD'; 226, string:b'NEURALSG'; 227, string:b'NEURALEV'; 228, block; 228, 229; 229, return_statement; 229, 230; 230, identifier:BlackRock; 231, elif_clause; 231, 232; 231, 239; 231, 240; 232, comparison_operator:==; 232, 233; 232, 238; 233, subscript; 233, 234; 233, 235; 234, identifier:file_header; 235, slice; 235, 236; 235, 237; 236, colon; 237, integer:6; 238, string:b'MATLAB'; 239, comment; 240, block; 240, 241; 241, return_statement; 241, 242; 242, identifier:FieldTrip; 243, if_statement; 243, 244; 243, 253; 243, 287; 244, comparison_operator:==; 244, 245; 244, 252; 245, call; 245, 246; 245, 251; 246, attribute; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:filename; 249, identifier:suffix; 250, identifier:lower; 251, argument_list; 252, string:'.txt'; 253, block; 253, 254; 254, with_statement; 254, 255; 254, 266; 255, with_clause; 255, 256; 256, with_item; 256, 257; 257, as_pattern; 257, 258; 257, 264; 258, call; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:filename; 261, identifier:open; 262, argument_list; 262, 263; 263, string:'rt'; 264, as_pattern_target; 264, 265; 265, identifier:f; 266, block; 266, 267; 266, 275; 267, expression_statement; 267, 268; 268, assignment; 268, 269; 268, 270; 269, identifier:first_line; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:f; 273, identifier:readline; 274, argument_list; 275, if_statement; 275, 276; 275, 284; 276, comparison_operator:in; 276, 277; 276, 278; 277, string:'.rr'; 278, subscript; 278, 279; 278, 280; 279, identifier:first_line; 280, slice; 280, 281; 280, 283; 281, unary_operator:-; 281, 282; 282, integer:4; 283, colon; 284, block; 284, 285; 285, return_statement; 285, 286; 286, identifier:LyonRRI; 287, else_clause; 287, 288; 288, block; 288, 289; 289, raise_statement; 289, 290; 290, call; 290, 291; 290, 292; 291, identifier:UnrecognizedFormat; 292, argument_list; 292, 293; 293, binary_operator:+; 293, 294; 293, 295; 294, string:'Unrecognized format for file '; 295, call; 295, 296; 295, 297; 296, identifier:str; 297, argument_list; 297, 298; 298, identifier:filename
def detect_format(filename): """Detect file format. Parameters ---------- filename : str or Path name of the filename or directory. Returns ------- class used to read the data. """ filename = Path(filename) if filename.is_dir(): if list(filename.glob('*.stc')) and list(filename.glob('*.erd')): return Ktlx elif (filename / 'patient.info').exists(): return Moberg elif (filename / 'info.xml').exists(): return EgiMff elif list(filename.glob('*.openephys')): return OpenEphys elif list(filename.glob('*.txt')): return Text else: raise UnrecognizedFormat('Unrecognized format for directory ' + str(filename)) else: if filename.suffix == '.won': return Wonambi if filename.suffix.lower() == '.trc': return Micromed if filename.suffix == '.set': return EEGLAB if filename.suffix == '.edf': return Edf if filename.suffix == '.abf': return Abf if filename.suffix == '.vhdr' or filename.suffix == '.eeg': return BrainVision if filename.suffix == '.dat': # very general try: _read_header_length(filename) except (AttributeError, ValueError): # there is no HeaderLen pass else: return BCI2000 with filename.open('rb') as f: file_header = f.read(8) if file_header in (b'NEURALCD', b'NEURALSG', b'NEURALEV'): return BlackRock elif file_header[:6] == b'MATLAB': # we might need to read more return FieldTrip if filename.suffix.lower() == '.txt': with filename.open('rt') as f: first_line = f.readline() if '.rr' in first_line[-4:]: return LyonRRI else: raise UnrecognizedFormat('Unrecognized format for file ' + str(filename))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:read_videos; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:begtime; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:endtime; 10, None; 11, block; 11, 12; 11, 14; 11, 31; 11, 46; 11, 63; 11, 78; 11, 90; 11, 92; 12, expression_statement; 12, 13; 13, comment; 14, if_statement; 14, 15; 14, 20; 15, call; 15, 16; 15, 17; 16, identifier:isinstance; 17, argument_list; 17, 18; 17, 19; 18, identifier:begtime; 19, identifier:datetime; 20, block; 20, 21; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:begtime; 24, binary_operator:-; 24, 25; 24, 26; 25, identifier:begtime; 26, subscript; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:header; 30, string:'start_time'; 31, if_statement; 31, 32; 31, 37; 32, call; 32, 33; 32, 34; 33, identifier:isinstance; 34, argument_list; 34, 35; 34, 36; 35, identifier:begtime; 36, identifier:timedelta; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:begtime; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:begtime; 44, identifier:total_seconds; 45, argument_list; 46, if_statement; 46, 47; 46, 52; 47, call; 47, 48; 47, 49; 48, identifier:isinstance; 49, argument_list; 49, 50; 49, 51; 50, identifier:endtime; 51, identifier:datetime; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:endtime; 56, binary_operator:-; 56, 57; 56, 58; 57, identifier:endtime; 58, subscript; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:header; 62, string:'start_time'; 63, if_statement; 63, 64; 63, 69; 64, call; 64, 65; 64, 66; 65, identifier:isinstance; 66, argument_list; 66, 67; 66, 68; 67, identifier:endtime; 68, identifier:timedelta; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:endtime; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:endtime; 76, identifier:total_seconds; 77, argument_list; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:videos; 81, call; 81, 82; 81, 87; 82, attribute; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:self; 85, identifier:dataset; 86, identifier:return_videos; 87, argument_list; 87, 88; 87, 89; 88, identifier:begtime; 89, identifier:endtime; 90, expression_statement; 90, 91; 91, comment; 92, return_statement; 92, 93; 93, identifier:videos
def read_videos(self, begtime=None, endtime=None): """Return list of videos with start and end times for a period. Parameters ---------- begtime : int or datedelta or datetime or list start of the data to read; if it's int, it's assumed it's s; if it's datedelta, it's assumed from the start of the recording; if it's datetime, it's assumed it's absolute time. It can also be a list of any of the above type. endtime : int or datedelta or datetime end of the data to read; if it's int, it's assumed it's s; if it's datedelta, it's assumed from the start of the recording; if it's datetime, it's assumed it's absolute time. It can also be a list of any of the above type. Returns ------- list of path list of absolute paths (as str) to the movie files float time in s from the beginning of the first movie when the part of interest starts float time in s from the beginning of the last movie when the part of interest ends Raises ------ OSError when there are no video files at all IndexError when there are video files, but the interval of interest is not in the list of files. """ if isinstance(begtime, datetime): begtime = begtime - self.header['start_time'] if isinstance(begtime, timedelta): begtime = begtime.total_seconds() if isinstance(endtime, datetime): endtime = endtime - self.header['start_time'] if isinstance(endtime, timedelta): endtime = endtime.total_seconds() videos = self.dataset.return_videos(begtime, endtime) """ try except AttributeError: lg.debug('This format does not have video') videos = None """ return videos
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 23; 2, function_name:read_data; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:chan; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:begtime; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:endtime; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:begsam; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:endsam; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:s_freq; 22, None; 23, block; 23, 24; 23, 26; 23, 32; 23, 42; 23, 57; 23, 70; 23, 90; 23, 94; 23, 118; 23, 135; 23, 148; 23, 165; 23, 202; 23, 239; 23, 252; 23, 265; 23, 283; 23, 290; 23, 304; 23, 318; 23, 330; 23, 453; 24, expression_statement; 24, 25; 25, comment; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:data; 29, call; 29, 30; 29, 31; 30, identifier:ChanTime; 31, argument_list; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:data; 36, identifier:start_time; 37, subscript; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:header; 41, string:'start_time'; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:data; 46, identifier:s_freq; 47, assignment; 47, 48; 47, 49; 48, identifier:s_freq; 49, conditional_expression:if; 49, 50; 49, 51; 49, 52; 50, identifier:s_freq; 51, identifier:s_freq; 52, subscript; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:self; 55, identifier:header; 56, string:'s_freq'; 57, if_statement; 57, 58; 57, 61; 58, comparison_operator:is; 58, 59; 58, 60; 59, identifier:chan; 60, None; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:chan; 65, subscript; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:header; 69, string:'chan_name'; 70, if_statement; 70, 71; 70, 84; 71, not_operator; 71, 72; 72, parenthesized_expression; 72, 73; 73, boolean_operator:or; 73, 74; 73, 79; 74, call; 74, 75; 74, 76; 75, identifier:isinstance; 76, argument_list; 76, 77; 76, 78; 77, identifier:chan; 78, identifier:list; 79, call; 79, 80; 79, 81; 80, identifier:isinstance; 81, argument_list; 81, 82; 81, 83; 82, identifier:chan; 83, identifier:tuple; 84, block; 84, 85; 85, raise_statement; 85, 86; 86, call; 86, 87; 86, 88; 87, identifier:TypeError; 88, argument_list; 88, 89; 89, string:'Parameter "chan" should be a list'; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:add_ref; 93, False; 94, if_statement; 94, 95; 94, 98; 95, comparison_operator:in; 95, 96; 95, 97; 96, string:'_REF'; 97, identifier:chan; 98, block; 98, 99; 98, 103; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:add_ref; 102, True; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 109; 105, subscript; 105, 106; 105, 107; 106, identifier:chan; 107, slice; 107, 108; 108, colon; 109, list_comprehension; 109, 110; 109, 111; 109, 114; 110, identifier:x; 111, for_in_clause; 111, 112; 111, 113; 112, identifier:x; 113, identifier:chan; 114, if_clause; 114, 115; 115, comparison_operator:!=; 115, 116; 115, 117; 116, identifier:x; 117, string:'_REF'; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:idx_chan; 121, list_comprehension; 121, 122; 121, 132; 122, call; 122, 123; 122, 130; 123, attribute; 123, 124; 123, 129; 124, subscript; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:self; 127, identifier:header; 128, string:'chan_name'; 129, identifier:index; 130, argument_list; 130, 131; 131, identifier:x; 132, for_in_clause; 132, 133; 132, 134; 133, identifier:x; 134, identifier:chan; 135, if_statement; 135, 136; 135, 143; 136, boolean_operator:and; 136, 137; 136, 140; 137, comparison_operator:is; 137, 138; 137, 139; 138, identifier:begtime; 139, None; 140, comparison_operator:is; 140, 141; 140, 142; 141, identifier:begsam; 142, None; 143, block; 143, 144; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:begsam; 147, integer:0; 148, if_statement; 148, 149; 148, 156; 149, boolean_operator:and; 149, 150; 149, 153; 150, comparison_operator:is; 150, 151; 150, 152; 151, identifier:endtime; 152, None; 153, comparison_operator:is; 153, 154; 153, 155; 154, identifier:endsam; 155, None; 156, block; 156, 157; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:endsam; 160, subscript; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:self; 163, identifier:header; 164, string:'n_samples'; 165, if_statement; 165, 166; 165, 169; 166, comparison_operator:is; 166, 167; 166, 168; 167, identifier:begtime; 168, None; 169, block; 169, 170; 169, 183; 169, 187; 170, if_statement; 170, 171; 170, 177; 171, not_operator; 171, 172; 172, call; 172, 173; 172, 174; 173, identifier:isinstance; 174, argument_list; 174, 175; 174, 176; 175, identifier:begtime; 176, identifier:list; 177, block; 177, 178; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:begtime; 181, list:[begtime]; 181, 182; 182, identifier:begtime; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:begsam; 186, list:[]; 187, for_statement; 187, 188; 187, 189; 187, 190; 188, identifier:one_begtime; 189, identifier:begtime; 190, block; 190, 191; 191, expression_statement; 191, 192; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:begsam; 195, identifier:append; 196, argument_list; 196, 197; 197, call; 197, 198; 197, 199; 198, identifier:_convert_time_to_sample; 199, argument_list; 199, 200; 199, 201; 200, identifier:one_begtime; 201, identifier:self; 202, if_statement; 202, 203; 202, 206; 203, comparison_operator:is; 203, 204; 203, 205; 204, identifier:endtime; 205, None; 206, block; 206, 207; 206, 220; 206, 224; 207, if_statement; 207, 208; 207, 214; 208, not_operator; 208, 209; 209, call; 209, 210; 209, 211; 210, identifier:isinstance; 211, argument_list; 211, 212; 211, 213; 212, identifier:endtime; 213, identifier:list; 214, block; 214, 215; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:endtime; 218, list:[endtime]; 218, 219; 219, identifier:endtime; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 223; 222, identifier:endsam; 223, list:[]; 224, for_statement; 224, 225; 224, 226; 224, 227; 225, identifier:one_endtime; 226, identifier:endtime; 227, block; 227, 228; 228, expression_statement; 228, 229; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:endsam; 232, identifier:append; 233, argument_list; 233, 234; 234, call; 234, 235; 234, 236; 235, identifier:_convert_time_to_sample; 236, argument_list; 236, 237; 236, 238; 237, identifier:one_endtime; 238, identifier:self; 239, if_statement; 239, 240; 239, 246; 240, not_operator; 240, 241; 241, call; 241, 242; 241, 243; 242, identifier:isinstance; 243, argument_list; 243, 244; 243, 245; 244, identifier:begsam; 245, identifier:list; 246, block; 246, 247; 247, expression_statement; 247, 248; 248, assignment; 248, 249; 248, 250; 249, identifier:begsam; 250, list:[begsam]; 250, 251; 251, identifier:begsam; 252, if_statement; 252, 253; 252, 259; 253, not_operator; 253, 254; 254, call; 254, 255; 254, 256; 255, identifier:isinstance; 256, argument_list; 256, 257; 256, 258; 257, identifier:endsam; 258, identifier:list; 259, block; 259, 260; 260, expression_statement; 260, 261; 261, assignment; 261, 262; 261, 263; 262, identifier:endsam; 263, list:[endsam]; 263, 264; 264, identifier:endsam; 265, if_statement; 265, 266; 265, 275; 266, comparison_operator:!=; 266, 267; 266, 271; 267, call; 267, 268; 267, 269; 268, identifier:len; 269, argument_list; 269, 270; 270, identifier:begsam; 271, call; 271, 272; 271, 273; 272, identifier:len; 273, argument_list; 273, 274; 274, identifier:endsam; 275, block; 275, 276; 276, raise_statement; 276, 277; 277, call; 277, 278; 277, 279; 278, identifier:ValueError; 279, argument_list; 279, 280; 280, binary_operator:+; 280, 281; 280, 282; 281, string:'There should be the same number of start and '; 282, string:'end point'; 283, expression_statement; 283, 284; 284, assignment; 284, 285; 284, 286; 285, identifier:n_trl; 286, call; 286, 287; 286, 288; 287, identifier:len; 288, argument_list; 288, 289; 289, identifier:begsam; 290, expression_statement; 290, 291; 291, assignment; 291, 292; 291, 297; 292, subscript; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:data; 295, identifier:axis; 296, string:'chan'; 297, call; 297, 298; 297, 299; 298, identifier:empty; 299, argument_list; 299, 300; 299, 301; 300, identifier:n_trl; 301, keyword_argument; 301, 302; 301, 303; 302, identifier:dtype; 303, string:'O'; 304, expression_statement; 304, 305; 305, assignment; 305, 306; 305, 311; 306, subscript; 306, 307; 306, 310; 307, attribute; 307, 308; 307, 309; 308, identifier:data; 309, identifier:axis; 310, string:'time'; 311, call; 311, 312; 311, 313; 312, identifier:empty; 313, argument_list; 313, 314; 313, 315; 314, identifier:n_trl; 315, keyword_argument; 315, 316; 315, 317; 316, identifier:dtype; 317, string:'O'; 318, expression_statement; 318, 319; 319, assignment; 319, 320; 319, 323; 320, attribute; 320, 321; 320, 322; 321, identifier:data; 322, identifier:data; 323, call; 323, 324; 323, 325; 324, identifier:empty; 325, argument_list; 325, 326; 325, 327; 326, identifier:n_trl; 327, keyword_argument; 327, 328; 327, 329; 328, identifier:dtype; 329, string:'O'; 330, for_statement; 330, 331; 330, 335; 330, 344; 331, pattern_list; 331, 332; 331, 333; 331, 334; 332, identifier:i; 333, identifier:one_begsam; 334, identifier:one_endsam; 335, call; 335, 336; 335, 337; 336, identifier:zip; 337, argument_list; 337, 338; 337, 342; 337, 343; 338, call; 338, 339; 338, 340; 339, identifier:range; 340, argument_list; 340, 341; 341, identifier:n_trl; 342, identifier:begsam; 343, identifier:endsam; 344, block; 344, 345; 344, 351; 344, 364; 344, 375; 344, 379; 344, 412; 344, 420; 344, 436; 345, expression_statement; 345, 346; 346, assignment; 346, 347; 346, 348; 347, identifier:dataset; 348, attribute; 348, 349; 348, 350; 349, identifier:self; 350, identifier:dataset; 351, expression_statement; 351, 352; 352, call; 352, 353; 352, 356; 353, attribute; 353, 354; 353, 355; 354, identifier:lg; 355, identifier:debug; 356, argument_list; 356, 357; 357, call; 357, 358; 357, 361; 358, attribute; 358, 359; 358, 360; 359, string:'begsam {0: 6}, endsam {1: 6}'; 360, identifier:format; 361, argument_list; 361, 362; 361, 363; 362, identifier:one_begsam; 363, identifier:one_endsam; 364, expression_statement; 364, 365; 365, assignment; 365, 366; 365, 367; 366, identifier:dat; 367, call; 367, 368; 367, 371; 368, attribute; 368, 369; 368, 370; 369, identifier:dataset; 370, identifier:return_dat; 371, argument_list; 371, 372; 371, 373; 371, 374; 372, identifier:idx_chan; 373, identifier:one_begsam; 374, identifier:one_endsam; 375, expression_statement; 375, 376; 376, assignment; 376, 377; 376, 378; 377, identifier:chan_in_dat; 378, identifier:chan; 379, if_statement; 379, 380; 379, 381; 380, identifier:add_ref; 381, block; 381, 382; 381, 393; 381, 405; 382, expression_statement; 382, 383; 383, assignment; 383, 384; 383, 385; 384, identifier:zero_ref; 385, call; 385, 386; 385, 387; 386, identifier:zeros; 387, argument_list; 387, 388; 388, tuple; 388, 389; 388, 390; 389, integer:1; 390, binary_operator:-; 390, 391; 390, 392; 391, identifier:one_endsam; 392, identifier:one_begsam; 393, expression_statement; 393, 394; 394, assignment; 394, 395; 394, 396; 395, identifier:dat; 396, call; 396, 397; 396, 398; 397, identifier:concatenate; 398, argument_list; 398, 399; 398, 402; 399, tuple; 399, 400; 399, 401; 400, identifier:dat; 401, identifier:zero_ref; 402, keyword_argument; 402, 403; 402, 404; 403, identifier:axis; 404, integer:0; 405, expression_statement; 405, 406; 406, call; 406, 407; 406, 410; 407, attribute; 407, 408; 407, 409; 408, identifier:chan_in_dat; 409, identifier:append; 410, argument_list; 410, 411; 411, string:'_REF'; 412, expression_statement; 412, 413; 413, assignment; 413, 414; 413, 419; 414, subscript; 414, 415; 414, 418; 415, attribute; 415, 416; 415, 417; 416, identifier:data; 417, identifier:data; 418, identifier:i; 419, identifier:dat; 420, expression_statement; 420, 421; 421, assignment; 421, 422; 421, 429; 422, subscript; 422, 423; 422, 428; 423, subscript; 423, 424; 423, 427; 424, attribute; 424, 425; 424, 426; 425, identifier:data; 426, identifier:axis; 427, string:'chan'; 428, identifier:i; 429, call; 429, 430; 429, 431; 430, identifier:asarray; 431, argument_list; 431, 432; 431, 433; 432, identifier:chan_in_dat; 433, keyword_argument; 433, 434; 433, 435; 434, identifier:dtype; 435, string:'U'; 436, expression_statement; 436, 437; 437, assignment; 437, 438; 437, 445; 438, subscript; 438, 439; 438, 444; 439, subscript; 439, 440; 439, 443; 440, attribute; 440, 441; 440, 442; 441, identifier:data; 442, identifier:axis; 443, string:'time'; 444, identifier:i; 445, parenthesized_expression; 445, 446; 446, binary_operator:/; 446, 447; 446, 452; 447, call; 447, 448; 447, 449; 448, identifier:arange; 449, argument_list; 449, 450; 449, 451; 450, identifier:one_begsam; 451, identifier:one_endsam; 452, identifier:s_freq; 453, return_statement; 453, 454; 454, identifier:data
def read_data(self, chan=None, begtime=None, endtime=None, begsam=None, endsam=None, s_freq=None): """Read the data and creates a ChanTime instance Parameters ---------- chan : list of strings names of the channels to read begtime : int or datedelta or datetime or list start of the data to read; if it's int or float, it's assumed it's s; if it's timedelta, it's assumed from the start of the recording; if it's datetime, it's assumed it's absolute time. It can also be a list of any of the above type. endtime : int or datedelta or datetime end of the data to read; if it's int or float, it's assumed it's s; if it's timedelta, it's assumed from the start of the recording; if it's datetime, it's assumed it's absolute time. It can also be a list of any of the above type. begsam : int first sample (this sample will be included) endsam : int last sample (this sample will NOT be included) s_freq : int sampling frequency of the data Returns ------- An instance of ChanTime Notes ----- begsam and endsam follow Python convention, which starts at zero, includes begsam but DOES NOT include endsam. If begtime and endtime are a list, they both need the exact same length and the data will be stored in trials. If neither begtime or begsam are specified, it starts from the first sample. If neither endtime or endsam are specified, it reads until the end. """ data = ChanTime() data.start_time = self.header['start_time'] data.s_freq = s_freq = s_freq if s_freq else self.header['s_freq'] if chan is None: chan = self.header['chan_name'] if not (isinstance(chan, list) or isinstance(chan, tuple)): raise TypeError('Parameter "chan" should be a list') add_ref = False if '_REF' in chan: add_ref = True chan[:] = [x for x in chan if x != '_REF'] idx_chan = [self.header['chan_name'].index(x) for x in chan] if begtime is None and begsam is None: begsam = 0 if endtime is None and endsam is None: endsam = self.header['n_samples'] if begtime is not None: if not isinstance(begtime, list): begtime = [begtime] begsam = [] for one_begtime in begtime: begsam.append(_convert_time_to_sample(one_begtime, self)) if endtime is not None: if not isinstance(endtime, list): endtime = [endtime] endsam = [] for one_endtime in endtime: endsam.append(_convert_time_to_sample(one_endtime, self)) if not isinstance(begsam, list): begsam = [begsam] if not isinstance(endsam, list): endsam = [endsam] if len(begsam) != len(endsam): raise ValueError('There should be the same number of start and ' + 'end point') n_trl = len(begsam) data.axis['chan'] = empty(n_trl, dtype='O') data.axis['time'] = empty(n_trl, dtype='O') data.data = empty(n_trl, dtype='O') for i, one_begsam, one_endsam in zip(range(n_trl), begsam, endsam): dataset = self.dataset lg.debug('begsam {0: 6}, endsam {1: 6}'.format(one_begsam, one_endsam)) dat = dataset.return_dat(idx_chan, one_begsam, one_endsam) chan_in_dat = chan if add_ref: zero_ref = zeros((1, one_endsam - one_begsam)) dat = concatenate((dat, zero_ref), axis=0) chan_in_dat.append('_REF') data.data[i] = dat data.axis['chan'][i] = asarray(chan_in_dat, dtype='U') data.axis['time'][i] = (arange(one_begsam, one_endsam) / s_freq) return data
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:export_freq_band; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:xfreq; 5, identifier:bands; 6, identifier:filename; 7, block; 7, 8; 7, 10; 7, 23; 7, 36; 7, 56; 7, 64; 7, 106; 7, 147; 7, 154; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:heading_row_1; 13, list:['Segment index', 'Start time', 'End time', 'Duration', 'Stitches', 'Stage', 'Cycle', 'Event type', 'Channel', ]; 13, 14; 13, 15; 13, 16; 13, 17; 13, 18; 13, 19; 13, 20; 13, 21; 13, 22; 14, string:'Segment index'; 15, string:'Start time'; 16, string:'End time'; 17, string:'Duration'; 18, string:'Stitches'; 19, string:'Stage'; 20, string:'Cycle'; 21, string:'Event type'; 22, string:'Channel'; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:spacer; 26, binary_operator:*; 26, 27; 26, 29; 27, list:['']; 27, 28; 28, string:''; 29, parenthesized_expression; 29, 30; 30, binary_operator:-; 30, 31; 30, 35; 31, call; 31, 32; 31, 33; 32, identifier:len; 33, argument_list; 33, 34; 34, identifier:heading_row_1; 35, integer:1; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:band_hdr; 39, list_comprehension; 39, 40; 39, 51; 40, binary_operator:+; 40, 41; 40, 47; 41, binary_operator:+; 41, 42; 41, 46; 42, call; 42, 43; 42, 44; 43, identifier:str; 44, argument_list; 44, 45; 45, identifier:b1; 46, string:'-'; 47, call; 47, 48; 47, 49; 48, identifier:str; 49, argument_list; 49, 50; 50, identifier:b2; 51, for_in_clause; 51, 52; 51, 55; 52, pattern_list; 52, 53; 52, 54; 53, identifier:b1; 54, identifier:b2; 55, identifier:bands; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:xband; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:xfreq; 62, identifier:copy; 63, argument_list; 64, for_statement; 64, 65; 64, 66; 64, 67; 65, identifier:seg; 66, identifier:xband; 67, block; 67, 68; 67, 72; 67, 100; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:bandlist; 71, list:[]; 72, for_statement; 72, 73; 72, 76; 72, 80; 73, pattern_list; 73, 74; 73, 75; 74, identifier:i; 75, identifier:b; 76, call; 76, 77; 76, 78; 77, identifier:enumerate; 78, argument_list; 78, 79; 79, identifier:bands; 80, block; 80, 81; 80, 93; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 86; 83, pattern_list; 83, 84; 83, 85; 84, identifier:pwr; 85, identifier:_; 86, call; 86, 87; 86, 88; 87, identifier:band_power; 88, argument_list; 88, 89; 88, 92; 89, subscript; 89, 90; 89, 91; 90, identifier:seg; 91, string:'data'; 92, identifier:b; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:bandlist; 97, identifier:append; 98, argument_list; 98, 99; 99, identifier:pwr; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 105; 102, subscript; 102, 103; 102, 104; 103, identifier:seg; 104, string:'band'; 105, identifier:bandlist; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:as_matrix; 109, call; 109, 110; 109, 111; 110, identifier:asarray; 111, argument_list; 111, 112; 112, list_comprehension; 112, 113; 112, 132; 112, 133; 112, 136; 113, list_comprehension; 113, 114; 113, 121; 114, subscript; 114, 115; 114, 120; 115, subscript; 115, 116; 115, 119; 116, subscript; 116, 117; 116, 118; 117, identifier:x; 118, string:'band'; 119, identifier:y; 120, identifier:chan; 121, for_in_clause; 121, 122; 121, 123; 122, identifier:y; 123, call; 123, 124; 123, 125; 124, identifier:range; 125, argument_list; 125, 126; 126, call; 126, 127; 126, 128; 127, identifier:len; 128, argument_list; 128, 129; 129, subscript; 129, 130; 129, 131; 130, identifier:x; 131, string:'band'; 132, line_continuation:\; 133, for_in_clause; 133, 134; 133, 135; 134, identifier:x; 135, identifier:xband; 136, for_in_clause; 136, 137; 136, 138; 137, identifier:chan; 138, call; 138, 139; 138, 146; 139, attribute; 139, 140; 139, 145; 140, subscript; 140, 141; 140, 144; 141, subscript; 141, 142; 141, 143; 142, identifier:x; 143, string:'band'; 144, integer:0; 145, identifier:keys; 146, argument_list; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:desc; 150, call; 150, 151; 150, 152; 151, identifier:get_descriptives; 152, argument_list; 152, 153; 153, identifier:as_matrix; 154, with_statement; 154, 155; 154, 168; 155, with_clause; 155, 156; 156, with_item; 156, 157; 157, as_pattern; 157, 158; 157, 166; 158, call; 158, 159; 158, 160; 159, identifier:open; 160, argument_list; 160, 161; 160, 162; 160, 163; 161, identifier:filename; 162, string:'w'; 163, keyword_argument; 163, 164; 163, 165; 164, identifier:newline; 165, string:''; 166, as_pattern_target; 166, 167; 167, identifier:f; 168, block; 168, 169; 168, 181; 168, 188; 168, 201; 168, 210; 168, 227; 168, 244; 168, 261; 168, 278; 168, 282; 169, expression_statement; 169, 170; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:lg; 173, identifier:info; 174, argument_list; 174, 175; 175, binary_operator:+; 175, 176; 175, 177; 176, string:'Writing to '; 177, call; 177, 178; 177, 179; 178, identifier:str; 179, argument_list; 179, 180; 180, identifier:filename; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:csv_file; 184, call; 184, 185; 184, 186; 185, identifier:writer; 186, argument_list; 186, 187; 187, identifier:f; 188, expression_statement; 188, 189; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:csv_file; 192, identifier:writerow; 193, argument_list; 193, 194; 194, list:['Wonambi v{}'.format(__version__)]; 194, 195; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, string:'Wonambi v{}'; 198, identifier:format; 199, argument_list; 199, 200; 200, identifier:__version__; 201, expression_statement; 201, 202; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:csv_file; 205, identifier:writerow; 206, argument_list; 206, 207; 207, binary_operator:+; 207, 208; 207, 209; 208, identifier:heading_row_1; 209, identifier:band_hdr; 210, expression_statement; 210, 211; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:csv_file; 214, identifier:writerow; 215, argument_list; 215, 216; 216, binary_operator:+; 216, 217; 216, 221; 217, binary_operator:+; 217, 218; 217, 220; 218, list:['Mean']; 218, 219; 219, string:'Mean'; 220, identifier:spacer; 221, call; 221, 222; 221, 223; 222, identifier:list; 223, argument_list; 223, 224; 224, subscript; 224, 225; 224, 226; 225, identifier:desc; 226, string:'mean'; 227, expression_statement; 227, 228; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:csv_file; 231, identifier:writerow; 232, argument_list; 232, 233; 233, binary_operator:+; 233, 234; 233, 238; 234, binary_operator:+; 234, 235; 234, 237; 235, list:['SD']; 235, 236; 236, string:'SD'; 237, identifier:spacer; 238, call; 238, 239; 238, 240; 239, identifier:list; 240, argument_list; 240, 241; 241, subscript; 241, 242; 241, 243; 242, identifier:desc; 243, string:'sd'; 244, expression_statement; 244, 245; 245, call; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:csv_file; 248, identifier:writerow; 249, argument_list; 249, 250; 250, binary_operator:+; 250, 251; 250, 255; 251, binary_operator:+; 251, 252; 251, 254; 252, list:['Mean of ln']; 252, 253; 253, string:'Mean of ln'; 254, identifier:spacer; 255, call; 255, 256; 255, 257; 256, identifier:list; 257, argument_list; 257, 258; 258, subscript; 258, 259; 258, 260; 259, identifier:desc; 260, string:'mean_log'; 261, expression_statement; 261, 262; 262, call; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:csv_file; 265, identifier:writerow; 266, argument_list; 266, 267; 267, binary_operator:+; 267, 268; 267, 272; 268, binary_operator:+; 268, 269; 268, 271; 269, list:['SD of ln']; 269, 270; 270, string:'SD of ln'; 271, identifier:spacer; 272, call; 272, 273; 272, 274; 273, identifier:list; 274, argument_list; 274, 275; 275, subscript; 275, 276; 275, 277; 276, identifier:desc; 277, string:'sd_log'; 278, expression_statement; 278, 279; 279, assignment; 279, 280; 279, 281; 280, identifier:idx; 281, integer:0; 282, for_statement; 282, 283; 282, 284; 282, 285; 283, identifier:seg; 284, identifier:xband; 285, block; 285, 286; 286, for_statement; 286, 287; 286, 288; 286, 297; 287, identifier:chan; 288, call; 288, 289; 288, 296; 289, attribute; 289, 290; 289, 295; 290, subscript; 290, 291; 290, 294; 291, subscript; 291, 292; 291, 293; 292, identifier:seg; 293, string:'band'; 294, integer:0; 295, identifier:keys; 296, argument_list; 297, block; 297, 298; 297, 302; 297, 306; 297, 321; 297, 346; 298, expression_statement; 298, 299; 299, augmented_assignment:+=; 299, 300; 299, 301; 300, identifier:idx; 301, integer:1; 302, expression_statement; 302, 303; 303, assignment; 303, 304; 303, 305; 304, identifier:cyc; 305, None; 306, if_statement; 306, 307; 306, 312; 307, comparison_operator:is; 307, 308; 307, 311; 308, subscript; 308, 309; 308, 310; 309, identifier:seg; 310, string:'cycle'; 311, None; 312, block; 312, 313; 313, expression_statement; 313, 314; 314, assignment; 314, 315; 314, 316; 315, identifier:cyc; 316, subscript; 316, 317; 316, 320; 317, subscript; 317, 318; 317, 319; 318, identifier:seg; 319, string:'cycle'; 320, integer:2; 321, expression_statement; 321, 322; 322, assignment; 322, 323; 322, 324; 323, identifier:data_row; 324, call; 324, 325; 324, 326; 325, identifier:list; 326, argument_list; 326, 327; 327, list_comprehension; 327, 328; 327, 335; 328, subscript; 328, 329; 328, 334; 329, subscript; 329, 330; 329, 333; 330, subscript; 330, 331; 330, 332; 331, identifier:seg; 332, string:'band'; 333, identifier:x; 334, identifier:chan; 335, for_in_clause; 335, 336; 335, 337; 336, identifier:x; 337, call; 337, 338; 337, 339; 338, identifier:range; 339, argument_list; 339, 340; 340, call; 340, 341; 340, 342; 341, identifier:len; 342, argument_list; 342, 343; 343, subscript; 343, 344; 343, 345; 344, identifier:seg; 345, string:'band'; 346, expression_statement; 346, 347; 347, call; 347, 348; 347, 351; 348, attribute; 348, 349; 348, 350; 349, identifier:csv_file; 350, identifier:writerow; 351, argument_list; 351, 352; 352, binary_operator:+; 352, 353; 352, 375; 353, list:[idx, seg['start'], seg['end'], seg['duration'], seg['n_stitch'], seg['stage'], cyc, seg['name'], chan, ]; 353, 354; 353, 355; 353, 358; 353, 361; 353, 364; 353, 367; 353, 370; 353, 371; 353, 374; 354, identifier:idx; 355, subscript; 355, 356; 355, 357; 356, identifier:seg; 357, string:'start'; 358, subscript; 358, 359; 358, 360; 359, identifier:seg; 360, string:'end'; 361, subscript; 361, 362; 361, 363; 362, identifier:seg; 363, string:'duration'; 364, subscript; 364, 365; 364, 366; 365, identifier:seg; 366, string:'n_stitch'; 367, subscript; 367, 368; 367, 369; 368, identifier:seg; 369, string:'stage'; 370, identifier:cyc; 371, subscript; 371, 372; 371, 373; 372, identifier:seg; 373, string:'name'; 374, identifier:chan; 375, identifier:data_row
def export_freq_band(xfreq, bands, filename): """Write frequency analysis data to CSV by pre-defined band.""" heading_row_1 = ['Segment index', 'Start time', 'End time', 'Duration', 'Stitches', 'Stage', 'Cycle', 'Event type', 'Channel', ] spacer = [''] * (len(heading_row_1) - 1) band_hdr = [str(b1) + '-' + str(b2) for b1, b2 in bands] xband = xfreq.copy() for seg in xband: bandlist = [] for i, b in enumerate(bands): pwr, _ = band_power(seg['data'], b) bandlist.append(pwr) seg['band'] = bandlist as_matrix = asarray([ [x['band'][y][chan] for y in range(len(x['band']))] \ for x in xband for chan in x['band'][0].keys()]) desc = get_descriptives(as_matrix) with open(filename, 'w', newline='') as f: lg.info('Writing to ' + str(filename)) csv_file = writer(f) csv_file.writerow(['Wonambi v{}'.format(__version__)]) csv_file.writerow(heading_row_1 + band_hdr) csv_file.writerow(['Mean'] + spacer + list(desc['mean'])) csv_file.writerow(['SD'] + spacer + list(desc['sd'])) csv_file.writerow(['Mean of ln'] + spacer + list(desc['mean_log'])) csv_file.writerow(['SD of ln'] + spacer + list(desc['sd_log'])) idx = 0 for seg in xband: for chan in seg['band'][0].keys(): idx += 1 cyc = None if seg['cycle'] is not None: cyc = seg['cycle'][2] data_row = list( [seg['band'][x][chan] for x in range( len(seg['band']))]) csv_file.writerow([idx, seg['start'], seg['end'], seg['duration'], seg['n_stitch'], seg['stage'], cyc, seg['name'], chan, ] + data_row)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_cycles; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 19; 5, 25; 5, 46; 5, 67; 5, 71; 5, 80; 5, 98; 5, 184; 5, 188; 5, 217; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:cycles; 11, call; 11, 12; 11, 17; 12, attribute; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:rater; 16, identifier:find; 17, argument_list; 17, 18; 18, string:'cycles'; 19, if_statement; 19, 20; 19, 22; 20, not_operator; 20, 21; 21, identifier:cycles; 22, block; 22, 23; 23, return_statement; 23, 24; 24, None; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:starts; 28, call; 28, 29; 28, 30; 29, identifier:sorted; 30, argument_list; 30, 31; 31, list_comprehension; 31, 32; 31, 38; 32, call; 32, 33; 32, 34; 33, identifier:float; 34, argument_list; 34, 35; 35, attribute; 35, 36; 35, 37; 36, identifier:mrkr; 37, identifier:text; 38, for_in_clause; 38, 39; 38, 40; 39, identifier:mrkr; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:cycles; 43, identifier:findall; 44, argument_list; 44, 45; 45, string:'cyc_start'; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:ends; 49, call; 49, 50; 49, 51; 50, identifier:sorted; 51, argument_list; 51, 52; 52, list_comprehension; 52, 53; 52, 59; 53, call; 53, 54; 53, 55; 54, identifier:float; 55, argument_list; 55, 56; 56, attribute; 56, 57; 56, 58; 57, identifier:mrkr; 58, identifier:text; 59, for_in_clause; 59, 60; 59, 61; 60, identifier:mrkr; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:cycles; 64, identifier:findall; 65, argument_list; 65, 66; 66, string:'cyc_end'; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:cyc_list; 70, list:[]; 71, if_statement; 71, 72; 71, 77; 72, boolean_operator:or; 72, 73; 72, 75; 73, not_operator; 73, 74; 74, identifier:starts; 75, not_operator; 75, 76; 76, identifier:ends; 77, block; 77, 78; 78, return_statement; 78, 79; 79, None; 80, if_statement; 80, 81; 80, 92; 81, call; 81, 82; 81, 83; 82, identifier:all; 83, generator_expression; 83, 84; 83, 89; 84, comparison_operator:<; 84, 85; 84, 86; 85, identifier:i; 86, subscript; 86, 87; 86, 88; 87, identifier:starts; 88, integer:0; 89, for_in_clause; 89, 90; 89, 91; 90, identifier:i; 91, identifier:ends; 92, block; 92, 93; 93, raise_statement; 93, 94; 94, call; 94, 95; 94, 96; 95, identifier:ValueError; 96, argument_list; 96, 97; 97, string:'First cycle has no start.'; 98, for_statement; 98, 99; 98, 102; 98, 114; 98, 115; 98, 116; 99, tuple_pattern; 99, 100; 99, 101; 100, identifier:this_start; 101, identifier:next_start; 102, call; 102, 103; 102, 104; 103, identifier:zip; 104, argument_list; 104, 105; 104, 106; 105, identifier:starts; 106, binary_operator:+; 106, 107; 106, 112; 107, subscript; 107, 108; 107, 109; 108, identifier:starts; 109, slice; 109, 110; 109, 111; 110, integer:1; 111, colon; 112, list:[inf]; 112, 113; 113, identifier:inf; 114, comment; 115, comment; 116, block; 116, 117; 116, 131; 116, 146; 116, 165; 116, 177; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:end_between_starts; 120, list_comprehension; 120, 121; 120, 122; 120, 125; 120, 126; 121, identifier:end; 122, for_in_clause; 122, 123; 122, 124; 123, identifier:end; 124, identifier:ends; 125, line_continuation:\; 126, if_clause; 126, 127; 127, comparison_operator:<; 127, 128; 127, 129; 127, 130; 128, identifier:this_start; 129, identifier:end; 130, identifier:next_start; 131, if_statement; 131, 132; 131, 138; 132, comparison_operator:>; 132, 133; 132, 137; 133, call; 133, 134; 133, 135; 134, identifier:len; 135, argument_list; 135, 136; 136, identifier:end_between_starts; 137, integer:1; 138, block; 138, 139; 139, raise_statement; 139, 140; 140, call; 140, 141; 140, 142; 141, identifier:ValueError; 142, argument_list; 142, 143; 143, concatenated_string; 143, 144; 143, 145; 144, string:'Found more than one cycle end for same '; 145, string:'cycle'; 146, if_statement; 146, 147; 146, 148; 146, 157; 147, identifier:end_between_starts; 148, block; 148, 149; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:one_cycle; 152, tuple; 152, 153; 152, 154; 153, identifier:this_start; 154, subscript; 154, 155; 154, 156; 155, identifier:end_between_starts; 156, integer:0; 157, else_clause; 157, 158; 158, block; 158, 159; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:one_cycle; 162, tuple; 162, 163; 162, 164; 163, identifier:this_start; 164, identifier:next_start; 165, if_statement; 165, 166; 165, 171; 166, comparison_operator:==; 166, 167; 166, 170; 167, subscript; 167, 168; 167, 169; 168, identifier:one_cycle; 169, integer:1; 170, identifier:inf; 171, block; 171, 172; 172, raise_statement; 172, 173; 173, call; 173, 174; 173, 175; 174, identifier:ValueError; 175, argument_list; 175, 176; 176, string:'Last cycle has no end.'; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:cyc_list; 181, identifier:append; 182, argument_list; 182, 183; 183, identifier:one_cycle; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:output; 187, list:[]; 188, for_statement; 188, 189; 188, 192; 188, 196; 189, pattern_list; 189, 190; 189, 191; 190, identifier:i; 191, identifier:j; 192, call; 192, 193; 192, 194; 193, identifier:enumerate; 194, argument_list; 194, 195; 195, identifier:cyc_list; 196, block; 196, 197; 196, 210; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:cyc; 200, expression_list; 200, 201; 200, 204; 200, 207; 201, subscript; 201, 202; 201, 203; 202, identifier:j; 203, integer:0; 204, subscript; 204, 205; 204, 206; 205, identifier:j; 206, integer:1; 207, binary_operator:+; 207, 208; 207, 209; 208, identifier:i; 209, integer:1; 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, identifier:cyc; 217, return_statement; 217, 218; 218, identifier:output
def get_cycles(self): """Return the cycle start and end times. Returns ------- list of tuple of float start and end times for each cycle, in seconds from recording start and the cycle index starting at 1 """ cycles = self.rater.find('cycles') if not cycles: return None starts = sorted( [float(mrkr.text) for mrkr in cycles.findall('cyc_start')]) ends = sorted( [float(mrkr.text) for mrkr in cycles.findall('cyc_end')]) cyc_list = [] if not starts or not ends: return None if all(i < starts[0] for i in ends): raise ValueError('First cycle has no start.') for (this_start, next_start) in zip(starts, starts[1:] + [inf]): # if an end is smaller than the next start, make it the end # otherwise, the next_start is the end end_between_starts = [end for end in ends \ if this_start < end <= next_start] if len(end_between_starts) > 1: raise ValueError('Found more than one cycle end for same ' 'cycle') if end_between_starts: one_cycle = (this_start, end_between_starts[0]) else: one_cycle = (this_start, next_start) if one_cycle[1] == inf: raise ValueError('Last cycle has no end.') cyc_list.append(one_cycle) output = [] for i, j in enumerate(cyc_list): cyc = j[0], j[1], i + 1 output.append(cyc) return output
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_read_erd; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:erd_file; 5, identifier:begsam; 6, identifier:endsam; 7, block; 7, 8; 7, 10; 7, 17; 7, 23; 7, 29; 7, 30; 7, 37; 7, 47; 7, 65; 7, 78; 7, 84; 7, 93; 7, 100; 7, 101; 7, 113; 7, 119; 7, 131; 7, 167; 7, 285; 7, 286; 7, 339; 7, 346; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:hdr; 13, call; 13, 14; 13, 15; 14, identifier:_read_hdr_file; 15, argument_list; 15, 16; 16, identifier:erd_file; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:n_allchan; 20, subscript; 20, 21; 20, 22; 21, identifier:hdr; 22, string:'num_channels'; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:shorted; 26, subscript; 26, 27; 26, 28; 27, identifier:hdr; 28, string:'shorted'; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:n_shorted; 33, call; 33, 34; 33, 35; 34, identifier:sum; 35, argument_list; 35, 36; 36, identifier:shorted; 37, if_statement; 37, 38; 37, 41; 38, comparison_operator:>; 38, 39; 38, 40; 39, identifier:n_shorted; 40, integer:0; 41, block; 41, 42; 42, raise_statement; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:NotImplementedError; 45, argument_list; 45, 46; 46, string:'shorted channels not tested yet'; 47, if_statement; 47, 48; 47, 54; 48, comparison_operator:in; 48, 49; 48, 52; 49, subscript; 49, 50; 49, 51; 50, identifier:hdr; 51, string:'file_schema'; 52, tuple; 52, 53; 53, integer:7; 54, block; 54, 55; 54, 59; 54, 60; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:abs_delta; 58, string:b'\x80'; 59, comment; 60, raise_statement; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:NotImplementedError; 63, argument_list; 63, 64; 64, string:'schema 7 not tested yet'; 65, if_statement; 65, 66; 65, 73; 66, comparison_operator:in; 66, 67; 66, 70; 67, subscript; 67, 68; 67, 69; 68, identifier:hdr; 69, string:'file_schema'; 70, tuple; 70, 71; 70, 72; 71, integer:8; 72, integer:9; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:abs_delta; 77, string:b'\xff\xff'; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:n_smp; 81, binary_operator:-; 81, 82; 81, 83; 82, identifier:endsam; 83, identifier:begsam; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:data; 87, call; 87, 88; 87, 89; 88, identifier:empty; 89, argument_list; 89, 90; 90, tuple; 90, 91; 90, 92; 91, identifier:n_allchan; 92, identifier:n_smp; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:data; 97, identifier:fill; 98, argument_list; 98, 99; 99, identifier:NaN; 100, comment; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:etc; 104, call; 104, 105; 104, 106; 105, identifier:_read_etc; 106, argument_list; 106, 107; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:erd_file; 110, identifier:with_suffix; 111, argument_list; 111, 112; 112, string:'.etc'; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:all_beg; 116, subscript; 116, 117; 116, 118; 117, identifier:etc; 118, string:'samplestamp'; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:all_end; 122, binary_operator:-; 122, 123; 122, 130; 123, binary_operator:+; 123, 124; 123, 127; 124, subscript; 124, 125; 124, 126; 125, identifier:etc; 126, string:'samplestamp'; 127, subscript; 127, 128; 127, 129; 128, identifier:etc; 129, string:'sample_span'; 130, integer:1; 131, try_statement; 131, 132; 131, 162; 132, block; 132, 133; 132, 147; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:begrec; 136, subscript; 136, 137; 136, 146; 137, subscript; 137, 138; 137, 145; 138, call; 138, 139; 138, 140; 139, identifier:where; 140, argument_list; 140, 141; 141, parenthesized_expression; 141, 142; 142, comparison_operator:>=; 142, 143; 142, 144; 143, identifier:all_end; 144, identifier:begsam; 145, integer:0; 146, integer:0; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:endrec; 150, subscript; 150, 151; 150, 160; 151, subscript; 151, 152; 151, 159; 152, call; 152, 153; 152, 154; 153, identifier:where; 154, argument_list; 154, 155; 155, parenthesized_expression; 155, 156; 156, comparison_operator:<; 156, 157; 156, 158; 157, identifier:all_beg; 158, identifier:endsam; 159, integer:0; 160, unary_operator:-; 160, 161; 161, integer:1; 162, except_clause; 162, 163; 162, 164; 163, identifier:IndexError; 164, block; 164, 165; 165, return_statement; 165, 166; 166, identifier:data; 167, with_statement; 167, 168; 167, 179; 168, with_clause; 168, 169; 169, with_item; 169, 170; 170, as_pattern; 170, 171; 170, 177; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:erd_file; 174, identifier:open; 175, argument_list; 175, 176; 176, string:'rb'; 177, as_pattern_target; 177, 178; 178, identifier:f; 179, block; 179, 180; 180, for_statement; 180, 181; 180, 182; 180, 189; 180, 190; 181, identifier:rec; 182, call; 182, 183; 182, 184; 183, identifier:range; 184, argument_list; 184, 185; 184, 186; 185, identifier:begrec; 186, binary_operator:+; 186, 187; 186, 188; 187, identifier:endrec; 188, integer:1; 189, comment; 190, block; 190, 191; 190, 199; 190, 207; 190, 215; 190, 231; 190, 232; 190, 242; 190, 252; 190, 267; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 194; 193, identifier:begpos_rec; 194, binary_operator:-; 194, 195; 194, 196; 195, identifier:begsam; 196, subscript; 196, 197; 196, 198; 197, identifier:all_beg; 198, identifier:rec; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:endpos_rec; 202, binary_operator:-; 202, 203; 202, 204; 203, identifier:endsam; 204, subscript; 204, 205; 204, 206; 205, identifier:all_beg; 206, identifier:rec; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:begpos_rec; 210, call; 210, 211; 210, 212; 211, identifier:max; 212, argument_list; 212, 213; 212, 214; 213, identifier:begpos_rec; 214, integer:0; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:endpos_rec; 218, call; 218, 219; 218, 220; 219, identifier:min; 220, argument_list; 220, 221; 220, 222; 221, identifier:endpos_rec; 222, binary_operator:+; 222, 223; 222, 230; 223, binary_operator:-; 223, 224; 223, 227; 224, subscript; 224, 225; 224, 226; 225, identifier:all_end; 226, identifier:rec; 227, subscript; 227, 228; 227, 229; 228, identifier:all_beg; 229, identifier:rec; 230, integer:1; 231, comment; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 235; 234, identifier:d1; 235, binary_operator:-; 235, 236; 235, 241; 236, binary_operator:+; 236, 237; 236, 238; 237, identifier:begpos_rec; 238, subscript; 238, 239; 238, 240; 239, identifier:all_beg; 240, identifier:rec; 241, identifier:begsam; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 245; 244, identifier:d2; 245, binary_operator:-; 245, 246; 245, 251; 246, binary_operator:+; 246, 247; 246, 248; 247, identifier:endpos_rec; 248, subscript; 248, 249; 248, 250; 249, identifier:all_beg; 250, identifier:rec; 251, identifier:begsam; 252, expression_statement; 252, 253; 253, assignment; 253, 254; 253, 255; 254, identifier:dat; 255, call; 255, 256; 255, 257; 256, identifier:_read_packet; 257, argument_list; 257, 258; 257, 259; 257, 264; 257, 265; 257, 266; 258, identifier:f; 259, subscript; 259, 260; 259, 263; 260, subscript; 260, 261; 260, 262; 261, identifier:etc; 262, string:'offset'; 263, identifier:rec; 264, identifier:endpos_rec; 265, identifier:n_allchan; 266, identifier:abs_delta; 267, expression_statement; 267, 268; 268, assignment; 268, 269; 268, 277; 269, subscript; 269, 270; 269, 271; 269, 273; 270, identifier:data; 271, slice; 271, 272; 272, colon; 273, slice; 273, 274; 273, 275; 273, 276; 274, identifier:d1; 275, colon; 276, identifier:d2; 277, subscript; 277, 278; 277, 279; 277, 281; 278, identifier:dat; 279, slice; 279, 280; 280, colon; 281, slice; 281, 282; 281, 283; 281, 284; 282, identifier:begpos_rec; 283, colon; 284, identifier:endpos_rec; 285, comment; 286, if_statement; 286, 287; 286, 290; 286, 333; 287, comparison_operator:>; 287, 288; 287, 289; 288, identifier:n_shorted; 289, integer:0; 290, block; 290, 291; 290, 309; 290, 318; 290, 325; 291, expression_statement; 291, 292; 292, assignment; 292, 293; 292, 294; 293, identifier:full_channels; 294, subscript; 294, 295; 294, 308; 295, call; 295, 296; 295, 297; 296, identifier:where; 297, argument_list; 297, 298; 298, call; 298, 299; 298, 300; 299, identifier:asarray; 300, argument_list; 300, 301; 301, list_comprehension; 301, 302; 301, 305; 302, comparison_operator:==; 302, 303; 302, 304; 303, identifier:x; 304, integer:0; 305, for_in_clause; 305, 306; 305, 307; 306, identifier:x; 307, identifier:shorted; 308, integer:0; 309, expression_statement; 309, 310; 310, assignment; 310, 311; 310, 312; 311, identifier:output; 312, call; 312, 313; 312, 314; 313, identifier:empty; 314, argument_list; 314, 315; 315, tuple; 315, 316; 315, 317; 316, identifier:n_allchan; 317, identifier:n_smp; 318, expression_statement; 318, 319; 319, call; 319, 320; 319, 323; 320, attribute; 320, 321; 320, 322; 321, identifier:output; 322, identifier:fill; 323, argument_list; 323, 324; 324, identifier:NaN; 325, expression_statement; 325, 326; 326, assignment; 326, 327; 326, 332; 327, subscript; 327, 328; 327, 329; 327, 330; 328, identifier:output; 329, identifier:full_channels; 330, slice; 330, 331; 331, colon; 332, identifier:data; 333, else_clause; 333, 334; 334, block; 334, 335; 335, expression_statement; 335, 336; 336, assignment; 336, 337; 336, 338; 337, identifier:output; 338, identifier:data; 339, expression_statement; 339, 340; 340, assignment; 340, 341; 340, 342; 341, identifier:factor; 342, call; 342, 343; 342, 344; 343, identifier:_calculate_conversion; 344, argument_list; 344, 345; 345, identifier:hdr; 346, return_statement; 346, 347; 347, binary_operator:*; 347, 348; 347, 353; 348, call; 348, 349; 348, 350; 349, identifier:expand_dims; 350, argument_list; 350, 351; 350, 352; 351, identifier:factor; 352, integer:1; 353, identifier:output
def _read_erd(erd_file, begsam, endsam): """Read the raw data and return a matrix, converted to microvolts. Parameters ---------- erd_file : str one of the .erd files to read begsam : int index of the first sample to read endsam : int index of the last sample (excluded, per python convention) Returns ------- numpy.ndarray 2d matrix with the data, as read from the file Error ----- It checks whether the event byte (the first byte) is x00 as expected. It can also be x01, meaning that an event was generated by an external trigger. According to the manual, "a photic stimulator is the only supported device which generates an external trigger." If the eventbyte is something else, it throws an error. Notes ----- Each sample point consists of these parts: - Event Byte - Frequency byte (only if file_schema >= 8 and one chan has != freq) - Delta mask (only if file_schema >= 8) - Delta Information - Absolute Channel Values Event Byte: Bit 0 of the event byte indicates the presence of the external trigger during the sample period. It's very rare. Delta Mask: Bit-mask of a size int( number_of_channels / 8 + 0.5). Each 1 in the mask indicates that corresponding channel has 2*n bit delta, 0 means that corresponding channel has n bit delta. The rest of the byte of the delta mask is filled with "1". If file_schema <= 7, it generates a "fake" delta, where everything is 0. Some channels are shorted (i.e. not recorded), however they are stored in a non-intuitive way: deltamask takes them into account, but for the rest they are never used/recorded. So, we need to keep track both of all the channels (including the non-shorted) and of the actual channels only. When we save the data as memory-mapped, we only save the real channels. However, the data in the output have both shorted and non-shorted channels. Shorted channels have NaN's only. About the actual implementation, we always follow the python convention that the first sample is included and the last sample is not. """ hdr = _read_hdr_file(erd_file) n_allchan = hdr['num_channels'] shorted = hdr['shorted'] # does this exist for Schema 7 at all? n_shorted = sum(shorted) if n_shorted > 0: raise NotImplementedError('shorted channels not tested yet') if hdr['file_schema'] in (7,): abs_delta = b'\x80' # one byte: 10000000 raise NotImplementedError('schema 7 not tested yet') if hdr['file_schema'] in (8, 9): abs_delta = b'\xff\xff' n_smp = endsam - begsam data = empty((n_allchan, n_smp)) data.fill(NaN) # it includes the sample in both cases etc = _read_etc(erd_file.with_suffix('.etc')) all_beg = etc['samplestamp'] all_end = etc['samplestamp'] + etc['sample_span'] - 1 try: begrec = where((all_end >= begsam))[0][0] endrec = where((all_beg < endsam))[0][-1] except IndexError: return data with erd_file.open('rb') as f: for rec in range(begrec, endrec + 1): # [begpos_rec, endpos_rec] begpos_rec = begsam - all_beg[rec] endpos_rec = endsam - all_beg[rec] begpos_rec = max(begpos_rec, 0) endpos_rec = min(endpos_rec, all_end[rec] - all_beg[rec] + 1) # [d1, d2) d1 = begpos_rec + all_beg[rec] - begsam d2 = endpos_rec + all_beg[rec] - begsam dat = _read_packet(f, etc['offset'][rec], endpos_rec, n_allchan, abs_delta) data[:, d1:d2] = dat[:, begpos_rec:endpos_rec] # fill up the output data, put NaN for shorted channels if n_shorted > 0: full_channels = where(asarray([x == 0 for x in shorted]))[0] output = empty((n_allchan, n_smp)) output.fill(NaN) output[full_channels, :] = data else: output = data factor = _calculate_conversion(hdr) return expand_dims(factor, 1) * output
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:return_markers; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 19; 5, 38; 5, 321; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:ent_file; 11, call; 11, 12; 11, 17; 12, attribute; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:_filename; 16, identifier:with_suffix; 17, argument_list; 17, 18; 18, string:'.ent'; 19, if_statement; 19, 20; 19, 26; 20, not_operator; 20, 21; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:ent_file; 24, identifier:exists; 25, argument_list; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:ent_file; 30, call; 30, 31; 30, 36; 31, attribute; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:_filename; 35, identifier:with_suffix; 36, argument_list; 36, 37; 37, string:'.ent.old'; 38, try_statement; 38, 39; 38, 47; 38, 56; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:ent_notes; 43, call; 43, 44; 43, 45; 44, identifier:_read_ent; 45, argument_list; 45, 46; 46, identifier:ent_file; 47, except_clause; 47, 48; 47, 51; 48, tuple; 48, 49; 48, 50; 49, identifier:FileNotFoundError; 50, identifier:PermissionError; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:markers; 55, list:[]; 56, else_clause; 56, 57; 57, block; 57, 58; 57, 62; 57, 104; 57, 114; 57, 118; 57, 122; 57, 126; 57, 130; 57, 276; 57, 280; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:allnote; 61, list:[]; 62, for_statement; 62, 63; 62, 64; 62, 65; 63, identifier:n; 64, identifier:ent_notes; 65, block; 65, 66; 66, try_statement; 66, 67; 66, 85; 67, block; 67, 68; 67, 76; 68, expression_statement; 68, 69; 69, call; 69, 70; 69, 75; 70, attribute; 70, 71; 70, 74; 71, subscript; 71, 72; 71, 73; 72, identifier:n; 73, string:'value'; 74, identifier:keys; 75, argument_list; 76, expression_statement; 76, 77; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:allnote; 80, identifier:append; 81, argument_list; 81, 82; 82, subscript; 82, 83; 82, 84; 83, identifier:n; 84, string:'value'; 85, except_clause; 85, 86; 85, 87; 86, identifier:AttributeError; 87, block; 87, 88; 88, expression_statement; 88, 89; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:lg; 92, identifier:debug; 93, argument_list; 93, 94; 94, call; 94, 95; 94, 100; 95, attribute; 95, 96; 95, 99; 96, concatenated_string; 96, 97; 96, 98; 97, string:'Note of length {} was not '; 98, string:'converted to dict'; 99, identifier:format; 100, argument_list; 100, 101; 101, subscript; 101, 102; 101, 103; 102, identifier:n; 103, string:'length'; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:s_freq; 107, subscript; 107, 108; 107, 113; 108, subscript; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:_hdr; 112, string:'erd'; 113, string:'sample_freq'; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:pcname; 117, string:'0CFEBE72-DA20-4b3a-A8AC-CDD41BFE2F0D'; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:note_time; 121, list:[]; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:note_name; 125, list:[]; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:note_note; 129, list:[]; 130, for_statement; 130, 131; 130, 132; 130, 133; 131, identifier:n; 132, identifier:allnote; 133, block; 133, 134; 133, 142; 133, 149; 133, 161; 133, 171; 133, 175; 133, 176; 133, 186; 133, 196; 133, 206; 133, 218; 133, 256; 133, 267; 134, if_statement; 134, 135; 134, 140; 135, comparison_operator:==; 135, 136; 135, 139; 136, subscript; 136, 137; 136, 138; 137, identifier:n; 138, string:'Text'; 139, string:'Analyzed Data Note'; 140, block; 140, 141; 141, continue_statement; 142, if_statement; 142, 143; 142, 147; 143, not_operator; 143, 144; 144, subscript; 144, 145; 144, 146; 145, identifier:n; 146, string:'Text'; 147, block; 147, 148; 148, continue_statement; 149, if_statement; 149, 150; 149, 159; 150, comparison_operator:not; 150, 151; 150, 152; 151, string:'User'; 152, call; 152, 153; 152, 158; 153, attribute; 153, 154; 153, 157; 154, subscript; 154, 155; 154, 156; 155, identifier:n; 156, string:'Data'; 157, identifier:keys; 158, argument_list; 159, block; 159, 160; 160, continue_statement; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:user1; 164, comparison_operator:==; 164, 165; 164, 170; 165, subscript; 165, 166; 165, 169; 166, subscript; 166, 167; 166, 168; 167, identifier:n; 168, string:'Data'; 169, string:'User'; 170, string:'Persyst'; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:user2; 174, False; 175, comment; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:user3; 179, comparison_operator:==; 179, 180; 179, 185; 180, subscript; 180, 181; 180, 184; 181, subscript; 181, 182; 181, 183; 182, identifier:n; 183, string:'Data'; 184, string:'User'; 185, identifier:pcname; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:user4; 189, comparison_operator:==; 189, 190; 189, 195; 190, subscript; 190, 191; 190, 194; 191, subscript; 191, 192; 191, 193; 192, identifier:n; 193, string:'Data'; 194, string:'User'; 195, string:'XLSpike - Intracranial'; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 199; 198, identifier:user5; 199, comparison_operator:==; 199, 200; 199, 205; 200, subscript; 200, 201; 200, 204; 201, subscript; 201, 202; 201, 203; 202, identifier:n; 203, string:'Data'; 204, string:'User'; 205, string:'XLEvent - Intracranial'; 206, if_statement; 206, 207; 206, 216; 207, boolean_operator:or; 207, 208; 207, 215; 208, boolean_operator:or; 208, 209; 208, 214; 209, boolean_operator:or; 209, 210; 209, 213; 210, boolean_operator:or; 210, 211; 210, 212; 211, identifier:user1; 212, identifier:user2; 213, identifier:user3; 214, identifier:user4; 215, identifier:user5; 216, block; 216, 217; 217, continue_statement; 218, if_statement; 218, 219; 218, 229; 218, 237; 219, comparison_operator:==; 219, 220; 219, 228; 220, call; 220, 221; 220, 222; 221, identifier:len; 222, argument_list; 222, 223; 223, subscript; 223, 224; 223, 227; 224, subscript; 224, 225; 224, 226; 225, identifier:n; 226, string:'Data'; 227, string:'User'; 228, integer:0; 229, block; 229, 230; 230, expression_statement; 230, 231; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:note_name; 234, identifier:append; 235, argument_list; 235, 236; 236, string:'-unknown-'; 237, else_clause; 237, 238; 238, block; 238, 239; 239, expression_statement; 239, 240; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:note_name; 243, identifier:append; 244, argument_list; 244, 245; 245, subscript; 245, 246; 245, 255; 246, call; 246, 247; 246, 254; 247, attribute; 247, 248; 247, 253; 248, subscript; 248, 249; 248, 252; 249, subscript; 249, 250; 249, 251; 250, identifier:n; 251, string:'Data'; 252, string:'User'; 253, identifier:split; 254, argument_list; 255, integer:0; 256, expression_statement; 256, 257; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:note_time; 260, identifier:append; 261, argument_list; 261, 262; 262, binary_operator:/; 262, 263; 262, 266; 263, subscript; 263, 264; 263, 265; 264, identifier:n; 265, string:'Stamp'; 266, identifier:s_freq; 267, expression_statement; 267, 268; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:note_note; 271, identifier:append; 272, argument_list; 272, 273; 273, subscript; 273, 274; 273, 275; 274, identifier:n; 275, string:'Text'; 276, expression_statement; 276, 277; 277, assignment; 277, 278; 277, 279; 278, identifier:markers; 279, list:[]; 280, for_statement; 280, 281; 280, 285; 280, 291; 281, pattern_list; 281, 282; 281, 283; 281, 284; 282, identifier:time; 283, identifier:name; 284, identifier:note; 285, call; 285, 286; 285, 287; 286, identifier:zip; 287, argument_list; 287, 288; 287, 289; 287, 290; 288, identifier:note_time; 289, identifier:note_name; 290, identifier:note_note; 291, block; 291, 292; 291, 314; 292, expression_statement; 292, 293; 293, assignment; 293, 294; 293, 295; 294, identifier:m; 295, dictionary; 295, 296; 295, 305; 295, 308; 295, 311; 296, pair; 296, 297; 296, 298; 297, string:'name'; 298, binary_operator:+; 298, 299; 298, 304; 299, binary_operator:+; 299, 300; 299, 303; 300, binary_operator:+; 300, 301; 300, 302; 301, identifier:note; 302, string:' ('; 303, identifier:name; 304, string:')'; 305, pair; 305, 306; 305, 307; 306, string:'start'; 307, identifier:time; 308, pair; 308, 309; 308, 310; 309, string:'end'; 310, identifier:time; 311, pair; 311, 312; 311, 313; 312, string:'chan'; 313, None; 314, expression_statement; 314, 315; 315, call; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:markers; 318, identifier:append; 319, argument_list; 319, 320; 320, identifier:m; 321, return_statement; 321, 322; 322, identifier:markers
def return_markers(self): """Reads the notes of the Ktlx recordings. """ ent_file = self._filename.with_suffix('.ent') if not ent_file.exists(): ent_file = self._filename.with_suffix('.ent.old') try: ent_notes = _read_ent(ent_file) except (FileNotFoundError, PermissionError): markers = [] else: allnote = [] for n in ent_notes: try: n['value'].keys() allnote.append(n['value']) except AttributeError: lg.debug('Note of length {} was not ' 'converted to dict'.format(n['length'])) s_freq = self._hdr['erd']['sample_freq'] pcname = '0CFEBE72-DA20-4b3a-A8AC-CDD41BFE2F0D' note_time = [] note_name = [] note_note = [] for n in allnote: if n['Text'] == 'Analyzed Data Note': continue if not n['Text']: continue if 'User' not in n['Data'].keys(): continue user1 = n['Data']['User'] == 'Persyst' user2 = False # n['Data']['User'] == 'eeg' user3 = n['Data']['User'] == pcname user4 = n['Data']['User'] == 'XLSpike - Intracranial' user5 = n['Data']['User'] == 'XLEvent - Intracranial' if user1 or user2 or user3 or user4 or user5: continue if len(n['Data']['User']) == 0: note_name.append('-unknown-') else: note_name.append(n['Data']['User'].split()[0]) note_time.append(n['Stamp'] / s_freq) note_note.append(n['Text']) markers = [] for time, name, note in zip(note_time, note_name, note_note): m = {'name': note + ' (' + name + ')', 'start': time, 'end': time, 'chan': None, } markers.append(m) return markers
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 2, function_name:band_power; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 4, identifier:data; 5, identifier:freq; 6, default_parameter; 6, 7; 6, 8; 7, identifier:scaling; 8, string:'power'; 9, default_parameter; 9, 10; 9, 11; 10, identifier:n_fft; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:detrend; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:array_out; 17, False; 18, block; 18, 19; 18, 21; 18, 67; 18, 108; 18, 131; 18, 141; 18, 151; 18, 152; 18, 187; 18, 239; 18, 334; 19, expression_statement; 19, 20; 20, comment; 21, if_statement; 21, 22; 21, 24; 21, 33; 22, not_operator; 22, 23; 23, identifier:array_out; 24, block; 24, 25; 24, 29; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:power; 28, dictionary; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:peakf; 32, dictionary; 33, else_clause; 33, 34; 34, block; 34, 35; 34, 51; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:power; 38, call; 38, 39; 38, 40; 39, identifier:zeros; 40, argument_list; 40, 41; 41, tuple; 41, 42; 41, 50; 42, subscript; 42, 43; 42, 49; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:data; 46, identifier:number_of; 47, argument_list; 47, 48; 48, string:'chan'; 49, integer:0; 50, integer:1; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:peakf; 54, call; 54, 55; 54, 56; 55, identifier:zeros; 56, argument_list; 56, 57; 57, tuple; 57, 58; 57, 66; 58, subscript; 58, 59; 58, 65; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:data; 62, identifier:number_of; 63, argument_list; 63, 64; 64, string:'chan'; 65, integer:0; 66, integer:1; 67, if_statement; 67, 68; 67, 73; 67, 78; 67, 101; 68, call; 68, 69; 68, 70; 69, identifier:isinstance; 70, argument_list; 70, 71; 70, 72; 71, identifier:data; 72, identifier:ChanFreq; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:Sxx; 77, identifier:data; 78, elif_clause; 78, 79; 78, 84; 79, call; 79, 80; 79, 81; 80, identifier:isinstance; 81, argument_list; 81, 82; 81, 83; 82, identifier:data; 83, identifier:ChanTime; 84, block; 84, 85; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:Sxx; 88, call; 88, 89; 88, 90; 89, identifier:frequency; 90, argument_list; 90, 91; 90, 92; 90, 95; 90, 98; 91, identifier:data; 92, keyword_argument; 92, 93; 92, 94; 93, identifier:scaling; 94, identifier:scaling; 95, keyword_argument; 95, 96; 95, 97; 96, identifier:n_fft; 97, identifier:n_fft; 98, keyword_argument; 98, 99; 98, 100; 99, identifier:detrend; 100, identifier:detrend; 101, else_clause; 101, 102; 102, block; 102, 103; 103, raise_statement; 103, 104; 104, call; 104, 105; 104, 106; 105, identifier:ValueError; 106, argument_list; 106, 107; 107, string:'Invalid data type'; 108, if_statement; 108, 109; 108, 112; 109, comparison_operator:is; 109, 110; 109, 111; 110, identifier:detrend; 111, None; 112, block; 112, 113; 113, if_statement; 113, 114; 113, 117; 113, 122; 114, comparison_operator:==; 114, 115; 114, 116; 115, string:'power'; 116, identifier:scaling; 117, block; 117, 118; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:detrend; 121, string:'linear'; 122, elif_clause; 122, 123; 122, 126; 123, comparison_operator:==; 123, 124; 123, 125; 124, string:'energy'; 125, identifier:scaling; 126, block; 126, 127; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:detrend; 130, None; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:sf; 134, subscript; 134, 135; 134, 140; 135, subscript; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:Sxx; 138, identifier:axis; 139, string:'freq'; 140, integer:0; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:f_res; 144, binary_operator:-; 144, 145; 144, 148; 145, subscript; 145, 146; 145, 147; 146, identifier:sf; 147, integer:1; 148, subscript; 148, 149; 148, 150; 149, identifier:sf; 150, integer:0; 151, comment; 152, if_statement; 152, 153; 152, 158; 152, 181; 153, comparison_operator:is; 153, 154; 153, 157; 154, subscript; 154, 155; 154, 156; 155, identifier:freq; 156, integer:0; 157, None; 158, block; 158, 159; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:idx_f1; 162, call; 162, 163; 162, 180; 163, attribute; 163, 164; 163, 179; 164, call; 164, 165; 164, 166; 165, identifier:asarray; 166, argument_list; 166, 167; 167, list_comprehension; 167, 168; 167, 176; 168, call; 168, 169; 168, 170; 169, identifier:abs; 170, argument_list; 170, 171; 171, binary_operator:-; 171, 172; 171, 173; 172, identifier:x; 173, subscript; 173, 174; 173, 175; 174, identifier:freq; 175, integer:0; 176, for_in_clause; 176, 177; 176, 178; 177, identifier:x; 178, identifier:sf; 179, identifier:argmin; 180, argument_list; 181, else_clause; 181, 182; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:idx_f1; 186, integer:0; 187, if_statement; 187, 188; 187, 193; 187, 228; 188, comparison_operator:is; 188, 189; 188, 192; 189, subscript; 189, 190; 189, 191; 190, identifier:freq; 191, integer:1; 192, None; 193, block; 193, 194; 193, 227; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:idx_f2; 197, call; 197, 198; 197, 199; 198, identifier:min; 199, argument_list; 199, 200; 199, 221; 200, binary_operator:+; 200, 201; 200, 220; 201, call; 201, 202; 201, 219; 202, attribute; 202, 203; 202, 218; 203, call; 203, 204; 203, 205; 204, identifier:asarray; 205, argument_list; 205, 206; 206, list_comprehension; 206, 207; 206, 215; 207, call; 207, 208; 207, 209; 208, identifier:abs; 209, argument_list; 209, 210; 210, binary_operator:-; 210, 211; 210, 212; 211, identifier:x; 212, subscript; 212, 213; 212, 214; 213, identifier:freq; 214, integer:1; 215, for_in_clause; 215, 216; 215, 217; 216, identifier:x; 217, identifier:sf; 218, identifier:argmin; 219, argument_list; 220, integer:1; 221, binary_operator:-; 221, 222; 221, 226; 222, call; 222, 223; 222, 224; 223, identifier:len; 224, argument_list; 224, 225; 225, identifier:sf; 226, integer:1; 227, comment; 228, else_clause; 228, 229; 229, block; 229, 230; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:idx_f2; 233, binary_operator:-; 233, 234; 233, 238; 234, call; 234, 235; 234, 236; 235, identifier:len; 236, argument_list; 236, 237; 237, identifier:sf; 238, integer:1; 239, for_statement; 239, 240; 239, 243; 239, 253; 240, pattern_list; 240, 241; 240, 242; 241, identifier:i; 242, identifier:chan; 243, call; 243, 244; 243, 245; 244, identifier:enumerate; 245, argument_list; 245, 246; 246, subscript; 246, 247; 246, 252; 247, subscript; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:Sxx; 250, identifier:axis; 251, string:'chan'; 252, integer:0; 253, block; 253, 254; 253, 265; 253, 279; 253, 292; 253, 303; 254, expression_statement; 254, 255; 255, assignment; 255, 256; 255, 257; 256, identifier:s; 257, subscript; 257, 258; 257, 264; 258, call; 258, 259; 258, 260; 259, identifier:Sxx; 260, argument_list; 260, 261; 261, keyword_argument; 261, 262; 261, 263; 262, identifier:chan; 263, identifier:chan; 264, integer:0; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 268; 267, identifier:pw; 268, binary_operator:*; 268, 269; 268, 278; 269, call; 269, 270; 269, 271; 270, identifier:sum; 271, argument_list; 271, 272; 272, subscript; 272, 273; 272, 274; 273, identifier:s; 274, slice; 274, 275; 274, 276; 274, 277; 275, identifier:idx_f1; 276, colon; 277, identifier:idx_f2; 278, identifier:f_res; 279, expression_statement; 279, 280; 280, assignment; 280, 281; 280, 282; 281, identifier:idx_peak; 282, call; 282, 283; 282, 291; 283, attribute; 283, 284; 283, 290; 284, subscript; 284, 285; 284, 286; 285, identifier:s; 286, slice; 286, 287; 286, 288; 286, 289; 287, identifier:idx_f1; 288, colon; 289, identifier:idx_f2; 290, identifier:argmax; 291, argument_list; 292, expression_statement; 292, 293; 293, assignment; 293, 294; 293, 295; 294, identifier:pf; 295, subscript; 295, 296; 295, 302; 296, subscript; 296, 297; 296, 298; 297, identifier:sf; 298, slice; 298, 299; 298, 300; 298, 301; 299, identifier:idx_f1; 300, colon; 301, identifier:idx_f2; 302, identifier:idx_peak; 303, if_statement; 303, 304; 303, 305; 303, 320; 304, identifier:array_out; 305, block; 305, 306; 305, 313; 306, expression_statement; 306, 307; 307, assignment; 307, 308; 307, 312; 308, subscript; 308, 309; 308, 310; 308, 311; 309, identifier:power; 310, identifier:i; 311, integer:0; 312, identifier:pw; 313, expression_statement; 313, 314; 314, assignment; 314, 315; 314, 319; 315, subscript; 315, 316; 315, 317; 315, 318; 316, identifier:peakf; 317, identifier:i; 318, integer:0; 319, identifier:pf; 320, else_clause; 320, 321; 321, block; 321, 322; 321, 328; 322, expression_statement; 322, 323; 323, assignment; 323, 324; 323, 327; 324, subscript; 324, 325; 324, 326; 325, identifier:power; 326, identifier:chan; 327, identifier:pw; 328, expression_statement; 328, 329; 329, assignment; 329, 330; 329, 333; 330, subscript; 330, 331; 330, 332; 331, identifier:peakf; 332, identifier:chan; 333, identifier:pf; 334, return_statement; 334, 335; 335, expression_list; 335, 336; 335, 337; 336, identifier:power; 337, identifier:peakf
def band_power(data, freq, scaling='power', n_fft=None, detrend=None, array_out=False): """Compute power or energy acoss a frequency band, and its peak frequency. Power is estimated using the mid-point rectangle rule. Input can be ChanTime or ChanFreq. Parameters ---------- data : instance of ChanTime or ChanFreq data to be analyzed, one trial only freq : tuple of float Frequencies for band of interest. Power will be integrated across this band, inclusively, and peak frequency determined within it. If a value is None, the band is unbounded in that direction. input_type : str 'time' or 'spectrum' scaling : str 'power' or 'energy', only used if data is ChanTime n_fft : int length of FFT. if shorter than input signal, signal is truncated; if longer, signal is zero-padded to length array_out : bool if True, will return two arrays instead of two dict. Returns ------- dict of float, or ndarray keys are channels, values are power or energy dict of float, or ndarray keys are channels, values are respective peak frequency """ if not array_out: power = {} peakf = {} else: power = zeros((data.number_of('chan')[0], 1)) peakf = zeros((data.number_of('chan')[0], 1)) if isinstance(data, ChanFreq): Sxx = data elif isinstance(data, ChanTime): Sxx = frequency(data, scaling=scaling, n_fft=n_fft, detrend=detrend) else: raise ValueError('Invalid data type') if detrend is None: if 'power' == scaling: detrend = 'linear' elif 'energy' == scaling: detrend = None sf = Sxx.axis['freq'][0] f_res = sf[1] - sf[0] # frequency resolution if freq[0] is not None: idx_f1 = asarray([abs(x - freq[0]) for x in sf]).argmin() else: idx_f1 = 0 if freq[1] is not None: idx_f2 = min(asarray([abs(x - freq[1]) for x in sf]).argmin() + 1, len(sf) - 1) # inclusive, to follow convention else: idx_f2 = len(sf) - 1 for i, chan in enumerate(Sxx.axis['chan'][0]): s = Sxx(chan=chan)[0] pw = sum(s[idx_f1:idx_f2]) * f_res idx_peak = s[idx_f1:idx_f2].argmax() pf = sf[idx_f1:idx_f2][idx_peak] if array_out: power[i, 0] = pw peakf[i, 0] = pf else: power[chan] = pw peakf[chan] = pf return power, peakf
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 24; 2, function_name:morlet; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 4, identifier:freq; 5, identifier:s_freq; 6, default_parameter; 6, 7; 6, 8; 7, identifier:ratio; 8, integer:5; 9, default_parameter; 9, 10; 9, 11; 10, identifier:sigma_f; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:dur_in_sd; 14, integer:4; 15, default_parameter; 15, 16; 15, 17; 16, identifier:dur_in_s; 17, None; 18, default_parameter; 18, 19; 18, 20; 19, identifier:normalization; 20, string:'peak'; 21, default_parameter; 21, 22; 21, 23; 22, identifier:zero_mean; 23, False; 24, block; 24, 25; 24, 27; 24, 46; 24, 57; 24, 74; 24, 87; 24, 103; 24, 118; 24, 135; 24, 152; 24, 197; 24, 214; 24, 244; 25, expression_statement; 25, 26; 26, comment; 27, if_statement; 27, 28; 27, 31; 27, 38; 28, comparison_operator:is; 28, 29; 28, 30; 29, identifier:sigma_f; 30, None; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:sigma_f; 35, binary_operator:/; 35, 36; 35, 37; 36, identifier:freq; 37, identifier:ratio; 38, else_clause; 38, 39; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:ratio; 43, binary_operator:/; 43, 44; 43, 45; 44, identifier:freq; 45, identifier:sigma_f; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:sigma_t; 49, binary_operator:/; 49, 50; 49, 51; 50, integer:1; 51, parenthesized_expression; 51, 52; 52, binary_operator:*; 52, 53; 52, 56; 53, binary_operator:*; 53, 54; 53, 55; 54, integer:2; 55, identifier:pi; 56, identifier:sigma_f; 57, if_statement; 57, 58; 57, 64; 58, boolean_operator:and; 58, 59; 58, 62; 59, comparison_operator:<; 59, 60; 59, 61; 60, identifier:ratio; 61, integer:5; 62, not_operator; 62, 63; 63, identifier:zero_mean; 64, block; 64, 65; 65, expression_statement; 65, 66; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:lg; 69, identifier:info; 70, argument_list; 70, 71; 71, concatenated_string; 71, 72; 71, 73; 72, string:'The wavelet won\'t have zero mean, set zero_mean=True to '; 73, string:'correct it'; 74, if_statement; 74, 75; 74, 78; 75, comparison_operator:is; 75, 76; 75, 77; 76, identifier:dur_in_s; 77, None; 78, block; 78, 79; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:dur_in_s; 82, binary_operator:*; 82, 83; 82, 86; 83, binary_operator:*; 83, 84; 83, 85; 84, identifier:sigma_t; 85, identifier:dur_in_sd; 86, integer:2; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:t; 90, call; 90, 91; 90, 92; 91, identifier:arange; 92, argument_list; 92, 93; 92, 97; 92, 100; 93, binary_operator:/; 93, 94; 93, 96; 94, unary_operator:-; 94, 95; 95, identifier:dur_in_s; 96, integer:2; 97, binary_operator:/; 97, 98; 97, 99; 98, identifier:dur_in_s; 99, integer:2; 100, binary_operator:/; 100, 101; 100, 102; 101, integer:1; 102, identifier:s_freq; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:w; 106, call; 106, 107; 106, 108; 107, identifier:exp; 108, argument_list; 108, 109; 109, binary_operator:*; 109, 110; 109, 117; 110, binary_operator:*; 110, 111; 110, 116; 111, binary_operator:*; 111, 112; 111, 115; 112, binary_operator:*; 112, 113; 112, 114; 113, integer:1j; 114, integer:2; 115, identifier:pi; 116, identifier:freq; 117, identifier:t; 118, if_statement; 118, 119; 118, 120; 119, identifier:zero_mean; 120, block; 120, 121; 121, expression_statement; 121, 122; 122, augmented_assignment:-=; 122, 123; 122, 124; 123, identifier:w; 124, call; 124, 125; 124, 126; 125, identifier:exp; 126, argument_list; 126, 127; 127, binary_operator:*; 127, 128; 127, 132; 128, binary_operator:/; 128, 129; 128, 131; 129, unary_operator:-; 129, 130; 130, integer:1; 131, integer:2; 132, binary_operator:**; 132, 133; 132, 134; 133, identifier:ratio; 134, integer:2; 135, expression_statement; 135, 136; 136, augmented_assignment:*=; 136, 137; 136, 138; 137, identifier:w; 138, call; 138, 139; 138, 140; 139, identifier:exp; 140, argument_list; 140, 141; 141, binary_operator:/; 141, 142; 141, 146; 142, unary_operator:-; 142, 143; 143, binary_operator:**; 143, 144; 143, 145; 144, identifier:t; 145, integer:2; 146, parenthesized_expression; 146, 147; 147, binary_operator:*; 147, 148; 147, 149; 148, integer:2; 149, binary_operator:**; 149, 150; 149, 151; 150, identifier:sigma_t; 151, integer:2; 152, if_statement; 152, 153; 152, 156; 152, 171; 152, 191; 153, comparison_operator:==; 153, 154; 153, 155; 154, identifier:normalization; 155, string:'area'; 156, block; 156, 157; 157, expression_statement; 157, 158; 158, augmented_assignment:/=; 158, 159; 158, 160; 159, identifier:w; 160, call; 160, 161; 160, 162; 161, identifier:sqrt; 162, argument_list; 162, 163; 163, binary_operator:*; 163, 164; 163, 170; 164, binary_operator:*; 164, 165; 164, 169; 165, call; 165, 166; 165, 167; 166, identifier:sqrt; 167, argument_list; 167, 168; 168, identifier:pi; 169, identifier:sigma_t; 170, identifier:s_freq; 171, elif_clause; 171, 172; 171, 175; 172, comparison_operator:==; 172, 173; 172, 174; 173, identifier:normalization; 174, string:'max'; 175, block; 175, 176; 176, expression_statement; 176, 177; 177, augmented_assignment:/=; 177, 178; 177, 179; 178, identifier:w; 179, binary_operator:/; 179, 180; 179, 190; 180, binary_operator:*; 180, 181; 180, 184; 181, binary_operator:*; 181, 182; 181, 183; 182, integer:2; 183, identifier:sigma_t; 184, call; 184, 185; 184, 186; 185, identifier:sqrt; 186, argument_list; 186, 187; 187, binary_operator:*; 187, 188; 187, 189; 188, integer:2; 189, identifier:pi; 190, identifier:s_freq; 191, elif_clause; 191, 192; 191, 195; 192, comparison_operator:==; 192, 193; 192, 194; 193, identifier:normalization; 194, string:'peak'; 195, block; 195, 196; 196, pass_statement; 197, expression_statement; 197, 198; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:lg; 201, identifier:info; 202, argument_list; 202, 203; 203, call; 203, 204; 203, 209; 204, attribute; 204, 205; 204, 208; 205, concatenated_string; 205, 206; 205, 207; 206, string:'At freq {0: 9.3f}Hz, sigma_f={1: 9.3f}Hz, sigma_t={2: 9.3f}s, '; 207, string:'total duration={3: 9.3f}s'; 208, identifier:format; 209, argument_list; 209, 210; 209, 211; 209, 212; 209, 213; 210, identifier:freq; 211, identifier:sigma_f; 212, identifier:sigma_t; 213, identifier:dur_in_s; 214, expression_statement; 214, 215; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:lg; 218, identifier:debug; 219, argument_list; 219, 220; 220, call; 220, 221; 220, 226; 221, attribute; 221, 222; 221, 225; 222, concatenated_string; 222, 223; 222, 224; 223, string:' Real peak={0: 9.3f}, Mean={1: 12.6f}, '; 224, string:'Energy={2: 9.3f}'; 225, identifier:format; 226, argument_list; 226, 227; 226, 234; 226, 238; 227, call; 227, 228; 227, 229; 228, identifier:max; 229, argument_list; 229, 230; 230, call; 230, 231; 230, 232; 231, identifier:real; 232, argument_list; 232, 233; 233, identifier:w; 234, call; 234, 235; 234, 236; 235, identifier:mean; 236, argument_list; 236, 237; 237, identifier:w; 238, binary_operator:**; 238, 239; 238, 243; 239, call; 239, 240; 239, 241; 240, identifier:norm; 241, argument_list; 241, 242; 242, identifier:w; 243, integer:2; 244, return_statement; 244, 245; 245, identifier:w
def morlet(freq, s_freq, ratio=5, sigma_f=None, dur_in_sd=4, dur_in_s=None, normalization='peak', zero_mean=False): """Create a Morlet wavelet. Parameters ---------- freq : float central frequency of the wavelet s_freq : int sampling frequency ratio : float ratio for a wavelet family ( = freq / sigma_f) sigma_f : float standard deviation of the wavelet in frequency domain dur_in_sd : float duration of the wavelet, given as number of the standard deviation in the time domain, in one side. dur_in_s : float total duration of the wavelet, two-sided (i.e. from start to finish) normalization : str 'area' means that energy is normalized to 1, 'peak' means that the peak is set at 1, 'max' is a normalization used by nitime which does not change max value of output when you change sigma_f. zero_mean : bool make sure that the wavelet has zero mean (only relevant if ratio < 5) Returns ------- ndarray vector containing the complex Morlet wavelets Notes ----- 'ratio' and 'sigma_f' are mutually exclusive. If you use 'sigma_f', the standard deviation stays the same for all the frequency. It's more common to specify a constant ratio for the wavelet family, so that the frequency resolution changes with the frequency of interest. 'dur_in_sd' and 'dur_in_s' are mutually exclusive. 'dur_in_s' specifies the total duration (from start to finish) of the window. 'dur_in_sd' calculates the total duration as the length in standard deviations in the time domain: dur_in_s = dur_in_sd * 2 * sigma_t, with sigma_t = 1 / (2 * pi * sigma_f) """ if sigma_f is None: sigma_f = freq / ratio else: ratio = freq / sigma_f sigma_t = 1 / (2 * pi * sigma_f) if ratio < 5 and not zero_mean: lg.info('The wavelet won\'t have zero mean, set zero_mean=True to ' 'correct it') if dur_in_s is None: dur_in_s = sigma_t * dur_in_sd * 2 t = arange(-dur_in_s / 2, dur_in_s / 2, 1 / s_freq) w = exp(1j * 2 * pi * freq * t) if zero_mean: w -= exp(-1 / 2 * ratio ** 2) w *= exp(-t ** 2 / (2 * sigma_t ** 2)) if normalization == 'area': w /= sqrt(sqrt(pi) * sigma_t * s_freq) elif normalization == 'max': w /= 2 * sigma_t * sqrt(2 * pi) / s_freq elif normalization == 'peak': pass lg.info('At freq {0: 9.3f}Hz, sigma_f={1: 9.3f}Hz, sigma_t={2: 9.3f}s, ' 'total duration={3: 9.3f}s'.format(freq, sigma_f, sigma_t, dur_in_s)) lg.debug(' Real peak={0: 9.3f}, Mean={1: 12.6f}, ' 'Energy={2: 9.3f}'.format(max(real(w)), mean(w), norm(w) ** 2)) return w
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_convert_timestr_to_seconds; 3, parameters; 3, 4; 3, 5; 4, identifier:time_str; 5, identifier:rec_start; 6, block; 6, 7; 6, 9; 6, 23; 6, 124; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 17; 10, not_operator; 10, 11; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:CHECK_TIME_STR; 14, identifier:match; 15, argument_list; 15, 16; 16, identifier:time_str; 17, block; 17, 18; 18, raise_statement; 18, 19; 19, call; 19, 20; 19, 21; 20, identifier:ValueError; 21, argument_list; 21, 22; 22, string:'Input can only contain digits and colons'; 23, if_statement; 23, 24; 23, 27; 23, 115; 24, comparison_operator:in; 24, 25; 24, 26; 25, string:':'; 26, identifier:time_str; 27, block; 27, 28; 27, 44; 27, 45; 27, 60; 27, 68; 27, 82; 27, 83; 27, 101; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:time_split; 31, list_comprehension; 31, 32; 31, 36; 32, call; 32, 33; 32, 34; 33, identifier:int; 34, argument_list; 34, 35; 35, identifier:x; 36, for_in_clause; 36, 37; 36, 38; 37, identifier:x; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:time_str; 41, identifier:split; 42, argument_list; 42, 43; 43, string:':'; 44, comment; 45, if_statement; 45, 46; 45, 52; 46, comparison_operator:==; 46, 47; 46, 51; 47, call; 47, 48; 47, 49; 48, identifier:len; 49, argument_list; 49, 50; 50, identifier:time_split; 51, integer:2; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:time_split; 57, identifier:append; 58, argument_list; 58, 59; 59, integer:0; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:clock_time; 63, call; 63, 64; 63, 65; 64, identifier:time; 65, argument_list; 65, 66; 66, list_splat; 66, 67; 67, identifier:time_split; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:chosen_start; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:datetime; 74, identifier:combine; 75, argument_list; 75, 76; 75, 81; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:rec_start; 79, identifier:date; 80, argument_list; 81, identifier:clock_time; 82, comment; 83, if_statement; 83, 84; 83, 91; 84, comparison_operator:<; 84, 85; 84, 86; 85, identifier:clock_time; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:rec_start; 89, identifier:time; 90, argument_list; 91, block; 91, 92; 92, expression_statement; 92, 93; 93, augmented_assignment:+=; 93, 94; 93, 95; 94, identifier:chosen_start; 95, call; 95, 96; 95, 97; 96, identifier:timedelta; 97, argument_list; 97, 98; 98, keyword_argument; 98, 99; 98, 100; 99, identifier:days; 100, integer:1; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:window_start; 104, call; 104, 105; 104, 106; 105, identifier:int; 106, argument_list; 106, 107; 107, call; 107, 108; 107, 114; 108, attribute; 108, 109; 108, 113; 109, parenthesized_expression; 109, 110; 110, binary_operator:-; 110, 111; 110, 112; 111, identifier:chosen_start; 112, identifier:rec_start; 113, identifier:total_seconds; 114, argument_list; 115, else_clause; 115, 116; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:window_start; 120, call; 120, 121; 120, 122; 121, identifier:int; 122, argument_list; 122, 123; 123, identifier:time_str; 124, return_statement; 124, 125; 125, identifier:window_start
def _convert_timestr_to_seconds(time_str, rec_start): """Convert input from user about time string to an absolute time for the recordings. Parameters ---------- time_str : str time information as '123' or '22:30' or '22:30:22' rec_start: instance of datetime absolute start time of the recordings. Returns ------- int start time of the window, in s, from the start of the recordings Raises ------ ValueError if it cannot convert the string """ if not CHECK_TIME_STR.match(time_str): raise ValueError('Input can only contain digits and colons') if ':' in time_str: time_split = [int(x) for x in time_str.split(':')] # if it's in 'HH:MM' format, add ':SS' if len(time_split) == 2: time_split.append(0) clock_time = time(*time_split) chosen_start = datetime.combine(rec_start.date(), clock_time) # if the clock time is after start of the recordings, assume it's the next day if clock_time < rec_start.time(): chosen_start += timedelta(days=1) window_start = int((chosen_start - rec_start).total_seconds()) else: window_start = int(time_str) return window_start
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:mouseReleaseEvent; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:event; 6, block; 6, 7; 6, 9; 6, 16; 6, 22; 6, 34; 6, 41; 6, 57; 6, 73; 6, 84; 6, 370; 6, 371; 6, 377; 6, 385; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 14; 10, not_operator; 10, 11; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:scene; 14, block; 14, 15; 15, return_statement; 16, if_statement; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:event_sel; 20, block; 20, 21; 21, return_statement; 22, if_statement; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:deselect; 26, block; 26, 27; 26, 33; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:deselect; 32, False; 33, return_statement; 34, if_statement; 34, 35; 34, 39; 35, not_operator; 35, 36; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:ready; 39, block; 39, 40; 40, return_statement; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:chk_marker; 44, call; 44, 45; 44, 56; 45, attribute; 45, 46; 45, 55; 46, subscript; 46, 47; 46, 54; 47, attribute; 47, 48; 47, 53; 48, attribute; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:parent; 52, identifier:notes; 53, identifier:action; 54, string:'new_bookmark'; 55, identifier:isChecked; 56, argument_list; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:chk_event; 60, call; 60, 61; 60, 72; 61, attribute; 61, 62; 61, 71; 62, subscript; 62, 63; 62, 70; 63, attribute; 63, 64; 63, 69; 64, attribute; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:self; 67, identifier:parent; 68, identifier:notes; 69, identifier:action; 70, string:'new_event'; 71, identifier:isChecked; 72, argument_list; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:y_distance; 76, call; 76, 77; 76, 82; 77, attribute; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:self; 80, identifier:parent; 81, identifier:value; 82, argument_list; 82, 83; 83, string:'y_distance'; 84, if_statement; 84, 85; 84, 88; 84, 316; 85, boolean_operator:or; 85, 86; 85, 87; 86, identifier:chk_marker; 87, identifier:chk_event; 88, block; 88, 89; 88, 106; 88, 123; 88, 124; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:x_in_scene; 92, call; 92, 93; 92, 105; 93, attribute; 93, 94; 93, 104; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:mapToScene; 98, argument_list; 98, 99; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:event; 102, identifier:pos; 103, argument_list; 104, identifier:x; 105, argument_list; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:y_in_scene; 109, call; 109, 110; 109, 122; 110, attribute; 110, 111; 110, 121; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:self; 114, identifier:mapToScene; 115, argument_list; 115, 116; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:event; 119, identifier:pos; 120, argument_list; 121, identifier:y; 122, argument_list; 123, comment; 124, if_statement; 124, 125; 124, 132; 124, 133; 124, 134; 125, comparison_operator:is; 125, 126; 125, 131; 126, subscript; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:self; 129, identifier:sel_xy; 130, integer:0; 131, None; 132, comment; 133, comment; 134, block; 134, 135; 134, 149; 134, 163; 134, 174; 134, 181; 134, 202; 134, 221; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:s_freq; 138, subscript; 138, 139; 138, 148; 139, attribute; 139, 140; 139, 147; 140, attribute; 140, 141; 140, 146; 141, attribute; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:self; 144, identifier:parent; 145, identifier:info; 146, identifier:dataset; 147, identifier:header; 148, string:'s_freq'; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:at_s_freq; 152, lambda; 152, 153; 152, 155; 153, lambda_parameters; 153, 154; 154, identifier:x; 155, binary_operator:/; 155, 156; 155, 162; 156, call; 156, 157; 156, 158; 157, identifier:round; 158, argument_list; 158, 159; 159, binary_operator:*; 159, 160; 159, 161; 160, identifier:x; 161, identifier:s_freq; 162, identifier:s_freq; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:start; 166, call; 166, 167; 166, 168; 167, identifier:at_s_freq; 168, argument_list; 168, 169; 169, subscript; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:self; 172, identifier:sel_xy; 173, integer:0; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:end; 177, call; 177, 178; 177, 179; 178, identifier:at_s_freq; 179, argument_list; 179, 180; 180, identifier:x_in_scene; 181, if_statement; 181, 182; 181, 197; 182, comparison_operator:<; 182, 183; 182, 189; 183, call; 183, 184; 183, 185; 184, identifier:abs; 185, argument_list; 185, 186; 186, binary_operator:-; 186, 187; 186, 188; 187, identifier:end; 188, identifier:start; 189, call; 189, 190; 189, 195; 190, attribute; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:self; 193, identifier:parent; 194, identifier:value; 195, argument_list; 195, 196; 196, string:'min_marker_dur'; 197, block; 197, 198; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:end; 201, identifier:start; 202, if_statement; 202, 203; 202, 206; 202, 213; 203, comparison_operator:<=; 203, 204; 203, 205; 204, identifier:start; 205, identifier:end; 206, block; 206, 207; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:time; 210, tuple; 210, 211; 210, 212; 211, identifier:start; 212, identifier:end; 213, else_clause; 213, 214; 214, block; 214, 215; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:time; 218, tuple; 218, 219; 218, 220; 219, identifier:end; 220, identifier:start; 221, if_statement; 221, 222; 221, 223; 221, 235; 222, identifier:chk_marker; 223, block; 223, 224; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 233; 226, attribute; 226, 227; 226, 232; 227, attribute; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:self; 230, identifier:parent; 231, identifier:notes; 232, identifier:add_bookmark; 233, argument_list; 233, 234; 234, identifier:time; 235, elif_clause; 235, 236; 235, 241; 236, boolean_operator:and; 236, 237; 236, 238; 237, identifier:chk_event; 238, comparison_operator:!=; 238, 239; 238, 240; 239, identifier:start; 240, identifier:end; 241, block; 241, 242; 241, 256; 241, 257; 241, 303; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 245; 244, identifier:eventtype; 245, call; 245, 246; 245, 255; 246, attribute; 246, 247; 246, 254; 247, attribute; 247, 248; 247, 253; 248, attribute; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:self; 251, identifier:parent; 252, identifier:notes; 253, identifier:idx_eventtype; 254, identifier:currentText; 255, argument_list; 256, comment; 257, if_statement; 257, 258; 257, 272; 257, 277; 258, comparison_operator:>; 258, 259; 258, 269; 259, call; 259, 260; 259, 261; 260, identifier:abs; 261, argument_list; 261, 262; 262, binary_operator:-; 262, 263; 262, 264; 263, identifier:y_in_scene; 264, subscript; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:self; 267, identifier:sel_xy; 268, integer:1; 269, binary_operator:*; 269, 270; 269, 271; 270, float:1.5; 271, identifier:y_distance; 272, block; 272, 273; 273, expression_statement; 273, 274; 274, assignment; 274, 275; 274, 276; 275, identifier:chan; 276, string:''; 277, else_clause; 277, 278; 278, block; 278, 279; 278, 295; 279, expression_statement; 279, 280; 280, assignment; 280, 281; 280, 282; 281, identifier:chan_idx; 282, call; 282, 283; 282, 284; 283, identifier:int; 284, argument_list; 284, 285; 285, call; 285, 286; 285, 287; 286, identifier:floor; 287, argument_list; 287, 288; 288, binary_operator:/; 288, 289; 288, 294; 289, subscript; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:self; 292, identifier:sel_xy; 293, integer:1; 294, identifier:y_distance; 295, expression_statement; 295, 296; 296, assignment; 296, 297; 296, 298; 297, identifier:chan; 298, subscript; 298, 299; 298, 302; 299, attribute; 299, 300; 299, 301; 300, identifier:self; 301, identifier:chan; 302, identifier:chan_idx; 303, expression_statement; 303, 304; 304, call; 304, 305; 304, 312; 305, attribute; 305, 306; 305, 311; 306, attribute; 306, 307; 306, 310; 307, attribute; 307, 308; 307, 309; 308, identifier:self; 309, identifier:parent; 310, identifier:notes; 311, identifier:add_event; 312, argument_list; 312, 313; 312, 314; 312, 315; 313, identifier:eventtype; 314, identifier:time; 315, identifier:chan; 316, else_clause; 316, 317; 316, 318; 317, comment; 318, block; 318, 319; 318, 343; 318, 349; 318, 350; 318, 360; 319, if_statement; 319, 320; 319, 331; 320, comparison_operator:in; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:self; 323, identifier:idx_info; 324, call; 324, 325; 324, 330; 325, attribute; 325, 326; 325, 329; 326, attribute; 326, 327; 326, 328; 327, identifier:self; 328, identifier:scene; 329, identifier:items; 330, argument_list; 331, block; 331, 332; 332, expression_statement; 332, 333; 333, call; 333, 334; 333, 339; 334, attribute; 334, 335; 334, 338; 335, attribute; 335, 336; 335, 337; 336, identifier:self; 337, identifier:scene; 338, identifier:removeItem; 339, argument_list; 339, 340; 340, attribute; 340, 341; 340, 342; 341, identifier:self; 342, identifier:idx_info; 343, expression_statement; 343, 344; 344, assignment; 344, 345; 344, 348; 345, attribute; 345, 346; 345, 347; 346, identifier:self; 347, identifier:idx_info; 348, None; 349, comment; 350, expression_statement; 350, 351; 351, call; 351, 352; 351, 359; 352, attribute; 352, 353; 352, 358; 353, attribute; 353, 354; 353, 357; 354, attribute; 354, 355; 354, 356; 355, identifier:self; 356, identifier:parent; 357, identifier:spectrum; 358, identifier:update; 359, argument_list; 360, expression_statement; 360, 361; 361, call; 361, 362; 361, 369; 362, attribute; 362, 363; 362, 368; 363, attribute; 363, 364; 363, 367; 364, attribute; 364, 365; 364, 366; 365, identifier:self; 366, identifier:parent; 367, identifier:spectrum; 368, identifier:display_window; 369, argument_list; 370, comment; 371, expression_statement; 371, 372; 372, assignment; 372, 373; 372, 376; 373, attribute; 373, 374; 373, 375; 374, identifier:self; 375, identifier:sel_chan; 376, None; 377, expression_statement; 377, 378; 378, assignment; 378, 379; 378, 382; 379, attribute; 379, 380; 379, 381; 380, identifier:self; 381, identifier:sel_xy; 382, tuple; 382, 383; 382, 384; 383, None; 384, None; 385, if_statement; 385, 386; 385, 397; 386, comparison_operator:in; 386, 387; 386, 390; 387, attribute; 387, 388; 387, 389; 388, identifier:self; 389, identifier:idx_sel; 390, call; 390, 391; 390, 396; 391, attribute; 391, 392; 391, 395; 392, attribute; 392, 393; 392, 394; 393, identifier:self; 394, identifier:scene; 395, identifier:items; 396, argument_list; 397, block; 397, 398; 397, 409; 398, expression_statement; 398, 399; 399, call; 399, 400; 399, 405; 400, attribute; 400, 401; 400, 404; 401, attribute; 401, 402; 401, 403; 402, identifier:self; 403, identifier:scene; 404, identifier:removeItem; 405, argument_list; 405, 406; 406, attribute; 406, 407; 406, 408; 407, identifier:self; 408, identifier:idx_sel; 409, expression_statement; 409, 410; 410, assignment; 410, 411; 410, 414; 411, attribute; 411, 412; 411, 413; 412, identifier:self; 413, identifier:idx_sel; 414, None
def mouseReleaseEvent(self, event): """Create a new event or marker, or show the previous power spectrum """ if not self.scene: return if self.event_sel: return if self.deselect: self.deselect = False return if not self.ready: return chk_marker = self.parent.notes.action['new_bookmark'].isChecked() chk_event = self.parent.notes.action['new_event'].isChecked() y_distance = self.parent.value('y_distance') if chk_marker or chk_event: x_in_scene = self.mapToScene(event.pos()).x() y_in_scene = self.mapToScene(event.pos()).y() # it can happen that selection is empty (f.e. double-click) if self.sel_xy[0] is not None: # max resolution = sampling frequency # in case there is no data s_freq = self.parent.info.dataset.header['s_freq'] at_s_freq = lambda x: round(x * s_freq) / s_freq start = at_s_freq(self.sel_xy[0]) end = at_s_freq(x_in_scene) if abs(end - start) < self.parent.value('min_marker_dur'): end = start if start <= end: time = (start, end) else: time = (end, start) if chk_marker: self.parent.notes.add_bookmark(time) elif chk_event and start != end: eventtype = self.parent.notes.idx_eventtype.currentText() # if dragged across > 1.5 chan, event is marked on all chan if abs(y_in_scene - self.sel_xy[1]) > 1.5 * y_distance: chan = '' else: chan_idx = int(floor(self.sel_xy[1] / y_distance)) chan = self.chan[chan_idx] self.parent.notes.add_event(eventtype, time, chan) else: # normal selection if self.idx_info in self.scene.items(): self.scene.removeItem(self.idx_info) self.idx_info = None # restore spectrum self.parent.spectrum.update() self.parent.spectrum.display_window() # general garbage collection self.sel_chan = None self.sel_xy = (None, None) if self.idx_sel in self.scene.items(): self.scene.removeItem(self.idx_sel) self.idx_sel = None
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:next_event; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:delete; 7, False; 8, block; 8, 9; 8, 11; 8, 67; 8, 73; 8, 79; 8, 87; 8, 131; 8, 143; 8, 162; 8, 218; 8, 232; 8, 294; 8, 300; 8, 309; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 13; 12, identifier:delete; 13, block; 13, 14; 13, 18; 13, 29; 13, 42; 13, 51; 13, 59; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:msg; 17, string:"Delete this event? This cannot be undone."; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:msgbox; 21, call; 21, 22; 21, 23; 22, identifier:QMessageBox; 23, argument_list; 23, 24; 23, 27; 23, 28; 24, attribute; 24, 25; 24, 26; 25, identifier:QMessageBox; 26, identifier:Question; 27, string:'Delete event'; 28, identifier:msg; 29, expression_statement; 29, 30; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:msgbox; 33, identifier:setStandardButtons; 34, argument_list; 34, 35; 35, binary_operator:|; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:QMessageBox; 38, identifier:Yes; 39, attribute; 39, 40; 39, 41; 40, identifier:QMessageBox; 41, identifier:No; 42, expression_statement; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:msgbox; 46, identifier:setDefaultButton; 47, argument_list; 47, 48; 48, attribute; 48, 49; 48, 50; 49, identifier:QMessageBox; 50, identifier:Yes; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:response; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:msgbox; 57, identifier:exec_; 58, argument_list; 59, if_statement; 59, 60; 59, 65; 60, comparison_operator:==; 60, 61; 60, 62; 61, identifier:response; 62, attribute; 62, 63; 62, 64; 63, identifier:QMessageBox; 64, identifier:No; 65, block; 65, 66; 66, return_statement; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:event_sel; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:event_sel; 73, if_statement; 73, 74; 73, 77; 74, comparison_operator:is; 74, 75; 74, 76; 75, identifier:event_sel; 76, None; 77, block; 77, 78; 78, return_statement; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:notes; 82, attribute; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:self; 85, identifier:parent; 86, identifier:notes; 87, if_statement; 87, 88; 87, 92; 87, 123; 88, not_operator; 88, 89; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:current_event_row; 92, block; 92, 93; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:row; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:notes; 99, identifier:find_row; 100, argument_list; 100, 101; 100, 108; 101, call; 101, 102; 101, 107; 102, attribute; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:event_sel; 105, identifier:marker; 106, identifier:x; 107, argument_list; 108, binary_operator:+; 108, 109; 108, 116; 109, call; 109, 110; 109, 115; 110, attribute; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:event_sel; 113, identifier:marker; 114, identifier:x; 115, argument_list; 116, call; 116, 117; 116, 122; 117, attribute; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:event_sel; 120, identifier:marker; 121, identifier:width; 122, argument_list; 123, else_clause; 123, 124; 124, block; 124, 125; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:row; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:current_event_row; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:same_type; 134, call; 134, 135; 134, 142; 135, attribute; 135, 136; 135, 141; 136, subscript; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:self; 139, identifier:action; 140, string:'next_of_same_type'; 141, identifier:isChecked; 142, argument_list; 143, if_statement; 143, 144; 143, 145; 144, identifier:same_type; 145, block; 145, 146; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:target; 149, call; 149, 150; 149, 161; 150, attribute; 150, 151; 150, 160; 151, call; 151, 152; 151, 157; 152, attribute; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:notes; 155, identifier:idx_annot_list; 156, identifier:item; 157, argument_list; 157, 158; 157, 159; 158, identifier:row; 159, integer:2; 160, identifier:text; 161, argument_list; 162, if_statement; 162, 163; 162, 164; 163, identifier:delete; 164, block; 164, 165; 164, 171; 164, 201; 164, 214; 165, expression_statement; 165, 166; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:notes; 169, identifier:delete_row; 170, argument_list; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:msg; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, string:'Deleted event from {} to {}.'; 177, identifier:format; 178, argument_list; 178, 179; 178, 186; 179, call; 179, 180; 179, 185; 180, attribute; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:event_sel; 183, identifier:marker; 184, identifier:x; 185, argument_list; 186, binary_operator:+; 186, 187; 186, 194; 187, call; 187, 188; 187, 193; 188, attribute; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:event_sel; 191, identifier:marker; 192, identifier:x; 193, argument_list; 194, call; 194, 195; 194, 200; 195, attribute; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:event_sel; 198, identifier:marker; 199, identifier:width; 200, argument_list; 201, expression_statement; 201, 202; 202, call; 202, 203; 202, 212; 203, attribute; 203, 204; 203, 211; 204, call; 204, 205; 204, 210; 205, attribute; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:self; 208, identifier:parent; 209, identifier:statusBar; 210, argument_list; 211, identifier:showMessage; 212, argument_list; 212, 213; 213, identifier:msg; 214, expression_statement; 214, 215; 215, augmented_assignment:-=; 215, 216; 215, 217; 216, identifier:row; 217, integer:1; 218, if_statement; 218, 219; 218, 230; 219, comparison_operator:==; 219, 220; 219, 223; 220, binary_operator:+; 220, 221; 220, 222; 221, identifier:row; 222, integer:1; 223, call; 223, 224; 223, 229; 224, attribute; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:notes; 227, identifier:idx_annot_list; 228, identifier:rowCount; 229, argument_list; 230, block; 230, 231; 231, return_statement; 232, if_statement; 232, 233; 232, 235; 232, 242; 233, not_operator; 233, 234; 234, identifier:same_type; 235, block; 235, 236; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 239; 238, identifier:next_row; 239, binary_operator:+; 239, 240; 239, 241; 240, identifier:row; 241, integer:1; 242, else_clause; 242, 243; 243, block; 243, 244; 243, 248; 243, 265; 243, 288; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 247; 246, identifier:next_row; 247, None; 248, expression_statement; 248, 249; 249, assignment; 249, 250; 249, 251; 250, identifier:types; 251, subscript; 251, 252; 251, 260; 252, call; 252, 253; 252, 258; 253, attribute; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:notes; 256, identifier:idx_annot_list; 257, identifier:property; 258, argument_list; 258, 259; 259, string:'name'; 260, slice; 260, 261; 260, 264; 261, binary_operator:+; 261, 262; 261, 263; 262, identifier:row; 263, integer:1; 264, colon; 265, for_statement; 265, 266; 265, 269; 265, 273; 266, pattern_list; 266, 267; 266, 268; 267, identifier:i; 268, identifier:ty; 269, call; 269, 270; 269, 271; 270, identifier:enumerate; 271, argument_list; 271, 272; 272, identifier:types; 273, block; 273, 274; 274, if_statement; 274, 275; 274, 278; 275, comparison_operator:==; 275, 276; 275, 277; 276, identifier:ty; 277, identifier:target; 278, block; 278, 279; 278, 287; 279, expression_statement; 279, 280; 280, assignment; 280, 281; 280, 282; 281, identifier:next_row; 282, binary_operator:+; 282, 283; 282, 286; 283, binary_operator:+; 283, 284; 283, 285; 284, identifier:row; 285, integer:1; 286, identifier:i; 287, break_statement; 288, if_statement; 288, 289; 288, 292; 289, comparison_operator:is; 289, 290; 289, 291; 290, identifier:next_row; 291, None; 292, block; 292, 293; 293, return_statement; 294, expression_statement; 294, 295; 295, assignment; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:self; 298, identifier:current_event_row; 299, identifier:next_row; 300, expression_statement; 300, 301; 301, call; 301, 302; 301, 305; 302, attribute; 302, 303; 302, 304; 303, identifier:notes; 304, identifier:go_to_marker; 305, argument_list; 305, 306; 305, 307; 305, 308; 306, identifier:next_row; 307, integer:0; 308, string:'annot'; 309, expression_statement; 309, 310; 310, call; 310, 311; 310, 316; 311, attribute; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:notes; 314, identifier:idx_annot_list; 315, identifier:setCurrentCell; 316, argument_list; 316, 317; 316, 318; 317, identifier:next_row; 318, integer:0
def next_event(self, delete=False): """Go to next event.""" if delete: msg = "Delete this event? This cannot be undone." msgbox = QMessageBox(QMessageBox.Question, 'Delete event', msg) msgbox.setStandardButtons(QMessageBox.Yes | QMessageBox.No) msgbox.setDefaultButton(QMessageBox.Yes) response = msgbox.exec_() if response == QMessageBox.No: return event_sel = self.event_sel if event_sel is None: return notes = self.parent.notes if not self.current_event_row: row = notes.find_row(event_sel.marker.x(), event_sel.marker.x() + event_sel.marker.width()) else: row = self.current_event_row same_type = self.action['next_of_same_type'].isChecked() if same_type: target = notes.idx_annot_list.item(row, 2).text() if delete: notes.delete_row() msg = 'Deleted event from {} to {}.'.format(event_sel.marker.x(), event_sel.marker.x() + event_sel.marker.width()) self.parent.statusBar().showMessage(msg) row -= 1 if row + 1 == notes.idx_annot_list.rowCount(): return if not same_type: next_row = row + 1 else: next_row = None types = notes.idx_annot_list.property('name')[row + 1:] for i, ty in enumerate(types): if ty == target: next_row = row + 1 + i break if next_row is None: return self.current_event_row = next_row notes.go_to_marker(next_row, 0, 'annot') notes.idx_annot_list.setCurrentCell(next_row, 0)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:montage; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:data; 5, default_parameter; 5, 6; 5, 7; 6, identifier:ref_chan; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:ref_to_avg; 10, False; 11, default_parameter; 11, 12; 11, 13; 12, identifier:bipolar; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:method; 16, string:'average'; 17, block; 17, 18; 17, 20; 17, 34; 17, 68; 17, 78; 17, 153; 17, 332; 18, expression_statement; 18, 19; 19, comment; 20, if_statement; 20, 21; 20, 26; 21, boolean_operator:and; 21, 22; 21, 23; 22, identifier:ref_to_avg; 23, comparison_operator:is; 23, 24; 23, 25; 24, identifier:ref_chan; 25, None; 26, block; 26, 27; 27, raise_statement; 27, 28; 28, call; 28, 29; 28, 30; 29, identifier:TypeError; 30, argument_list; 30, 31; 31, concatenated_string; 31, 32; 31, 33; 32, string:'You cannot specify reference to the average and '; 33, string:'the channels to use as reference'; 34, if_statement; 34, 35; 34, 38; 35, comparison_operator:is; 35, 36; 35, 37; 36, identifier:ref_chan; 37, None; 38, block; 38, 39; 39, if_statement; 39, 40; 39, 62; 40, parenthesized_expression; 40, 41; 41, boolean_operator:or; 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:ref_chan; 47, tuple; 47, 48; 47, 49; 48, identifier:list; 49, identifier:tuple; 50, not_operator; 50, 51; 51, call; 51, 52; 51, 53; 52, identifier:all; 53, generator_expression; 53, 54; 53, 59; 54, call; 54, 55; 54, 56; 55, identifier:isinstance; 56, argument_list; 56, 57; 56, 58; 57, identifier:x; 58, identifier:str; 59, for_in_clause; 59, 60; 59, 61; 60, identifier:x; 61, identifier:ref_chan; 62, block; 62, 63; 63, raise_statement; 63, 64; 64, call; 64, 65; 64, 66; 65, identifier:TypeError; 66, argument_list; 66, 67; 67, string:'chan should be a list of strings'; 68, if_statement; 68, 69; 68, 72; 69, comparison_operator:is; 69, 70; 69, 71; 70, identifier:ref_chan; 71, None; 72, block; 72, 73; 72, 77; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:ref_chan; 76, list:[]; 77, comment; 78, if_statement; 78, 79; 78, 80; 79, identifier:bipolar; 80, block; 80, 81; 80, 94; 80, 103; 80, 113; 80, 121; 80, 135; 80, 145; 81, if_statement; 81, 82; 81, 88; 82, not_operator; 82, 83; 83, subscript; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:data; 86, identifier:attr; 87, string:'chan'; 88, block; 88, 89; 89, raise_statement; 89, 90; 90, call; 90, 91; 90, 92; 91, identifier:ValueError; 92, argument_list; 92, 93; 93, string:'Data should have Chan information in attr'; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 97; 96, identifier:_assert_equal_channels; 97, argument_list; 97, 98; 98, subscript; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:data; 101, identifier:axis; 102, string:'chan'; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:chan_in_data; 106, subscript; 106, 107; 106, 112; 107, subscript; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:data; 110, identifier:axis; 111, string:'chan'; 112, integer:0; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:chan; 116, subscript; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:data; 119, identifier:attr; 120, string:'chan'; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:chan; 124, call; 124, 125; 124, 126; 125, identifier:chan; 126, argument_list; 126, 127; 127, lambda; 127, 128; 127, 130; 128, lambda_parameters; 128, 129; 129, identifier:x; 130, comparison_operator:in; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:x; 133, identifier:label; 134, identifier:chan_in_data; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 140; 137, pattern_list; 137, 138; 137, 139; 138, identifier:chan; 139, identifier:trans; 140, call; 140, 141; 140, 142; 141, identifier:create_bipolar_chan; 142, argument_list; 142, 143; 142, 144; 143, identifier:chan; 144, identifier:bipolar; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 152; 147, subscript; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:data; 150, identifier:attr; 151, string:'chan'; 152, identifier:chan; 153, if_statement; 153, 154; 153, 159; 153, 326; 154, boolean_operator:or; 154, 155; 154, 158; 155, boolean_operator:or; 155, 156; 155, 157; 156, identifier:ref_to_avg; 157, identifier:ref_chan; 158, identifier:bipolar; 159, block; 159, 160; 159, 168; 159, 177; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:mdata; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:data; 166, identifier:_copy; 167, argument_list; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:idx_chan; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:mdata; 174, identifier:index_of; 175, argument_list; 175, 176; 176, string:'chan'; 177, for_statement; 177, 178; 177, 179; 177, 188; 178, identifier:i; 179, call; 179, 180; 179, 181; 180, identifier:range; 181, argument_list; 181, 182; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:mdata; 185, identifier:number_of; 186, argument_list; 186, 187; 187, string:'trial'; 188, block; 188, 189; 189, if_statement; 189, 190; 189, 193; 189, 268; 190, boolean_operator:or; 190, 191; 190, 192; 191, identifier:ref_to_avg; 192, identifier:ref_chan; 193, block; 193, 194; 193, 207; 193, 219; 194, if_statement; 194, 195; 194, 196; 195, identifier:ref_to_avg; 196, block; 196, 197; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:ref_chan; 200, subscript; 200, 201; 200, 206; 201, subscript; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:data; 204, identifier:axis; 205, string:'chan'; 206, identifier:i; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:ref_data; 210, call; 210, 211; 210, 212; 211, identifier:data; 212, argument_list; 212, 213; 212, 216; 213, keyword_argument; 213, 214; 213, 215; 214, identifier:trial; 215, identifier:i; 216, keyword_argument; 216, 217; 216, 218; 217, identifier:chan; 218, identifier:ref_chan; 219, if_statement; 219, 220; 219, 223; 219, 246; 220, comparison_operator:==; 220, 221; 220, 222; 221, identifier:method; 222, string:'average'; 223, block; 223, 224; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 231; 226, subscript; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:mdata; 229, identifier:data; 230, identifier:i; 231, parenthesized_expression; 231, 232; 232, binary_operator:-; 232, 233; 232, 239; 233, call; 233, 234; 233, 235; 234, identifier:data; 235, argument_list; 235, 236; 236, keyword_argument; 236, 237; 236, 238; 237, identifier:trial; 238, identifier:i; 239, call; 239, 240; 239, 241; 240, identifier:mean; 241, argument_list; 241, 242; 241, 243; 242, identifier:ref_data; 243, keyword_argument; 243, 244; 243, 245; 244, identifier:axis; 245, identifier:idx_chan; 246, elif_clause; 246, 247; 246, 250; 247, comparison_operator:==; 247, 248; 247, 249; 248, identifier:method; 249, string:'regression'; 250, block; 250, 251; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 258; 253, subscript; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:mdata; 256, identifier:data; 257, identifier:i; 258, call; 258, 259; 258, 260; 259, identifier:compute_average_regress; 260, argument_list; 260, 261; 260, 267; 261, call; 261, 262; 261, 263; 262, identifier:data; 263, argument_list; 263, 264; 264, keyword_argument; 264, 265; 264, 266; 265, identifier:trial; 266, identifier:i; 267, identifier:idx_chan; 268, elif_clause; 268, 269; 268, 270; 269, identifier:bipolar; 270, block; 270, 271; 270, 289; 270, 306; 271, if_statement; 271, 272; 271, 281; 272, not_operator; 272, 273; 273, comparison_operator:==; 273, 274; 273, 280; 274, call; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:data; 277, identifier:index_of; 278, argument_list; 278, 279; 279, string:'chan'; 280, integer:0; 281, block; 281, 282; 282, raise_statement; 282, 283; 283, call; 283, 284; 283, 285; 284, identifier:ValueError; 285, argument_list; 285, 286; 286, concatenated_string; 286, 287; 286, 288; 287, string:'For matrix multiplication to work, '; 288, string:'the first dimension should be chan'; 289, expression_statement; 289, 290; 290, assignment; 290, 291; 290, 296; 291, subscript; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, identifier:mdata; 294, identifier:data; 295, identifier:i; 296, call; 296, 297; 296, 298; 297, identifier:dot; 298, argument_list; 298, 299; 298, 300; 299, identifier:trans; 300, call; 300, 301; 300, 302; 301, identifier:data; 302, argument_list; 302, 303; 303, keyword_argument; 303, 304; 303, 305; 304, identifier:trial; 305, identifier:i; 306, expression_statement; 306, 307; 307, assignment; 307, 308; 307, 315; 308, subscript; 308, 309; 308, 314; 309, subscript; 309, 310; 309, 313; 310, attribute; 310, 311; 310, 312; 311, identifier:mdata; 312, identifier:axis; 313, string:'chan'; 314, identifier:i; 315, call; 315, 316; 315, 317; 316, identifier:asarray; 317, argument_list; 317, 318; 317, 323; 318, call; 318, 319; 318, 322; 319, attribute; 319, 320; 319, 321; 320, identifier:chan; 321, identifier:return_label; 322, argument_list; 323, keyword_argument; 323, 324; 323, 325; 324, identifier:dtype; 325, string:'U'; 326, else_clause; 326, 327; 327, block; 327, 328; 328, expression_statement; 328, 329; 329, assignment; 329, 330; 329, 331; 330, identifier:mdata; 331, identifier:data; 332, return_statement; 332, 333; 333, identifier:mdata
def montage(data, ref_chan=None, ref_to_avg=False, bipolar=None, method='average'): """Apply linear transformation to the channels. Parameters ---------- data : instance of DataRaw the data to filter ref_chan : list of str list of channels used as reference ref_to_avg : bool if re-reference to average or not bipolar : float distance in mm to consider two channels as neighbors and then compute the bipolar montage between them. method : str 'average' or 'regression'. 'average' takes the average across the channels selected as reference (it can be all) and subtract it from each channel. 'regression' keeps the residuals after regressing out the mean across channels. Returns ------- filtered_data : instance of DataRaw filtered data Notes ----- If you don't change anything, it returns the same instance of data. """ if ref_to_avg and ref_chan is not None: raise TypeError('You cannot specify reference to the average and ' 'the channels to use as reference') if ref_chan is not None: if (not isinstance(ref_chan, (list, tuple)) or not all(isinstance(x, str) for x in ref_chan)): raise TypeError('chan should be a list of strings') if ref_chan is None: ref_chan = [] # TODO: check bool for ref_chan if bipolar: if not data.attr['chan']: raise ValueError('Data should have Chan information in attr') _assert_equal_channels(data.axis['chan']) chan_in_data = data.axis['chan'][0] chan = data.attr['chan'] chan = chan(lambda x: x.label in chan_in_data) chan, trans = create_bipolar_chan(chan, bipolar) data.attr['chan'] = chan if ref_to_avg or ref_chan or bipolar: mdata = data._copy() idx_chan = mdata.index_of('chan') for i in range(mdata.number_of('trial')): if ref_to_avg or ref_chan: if ref_to_avg: ref_chan = data.axis['chan'][i] ref_data = data(trial=i, chan=ref_chan) if method == 'average': mdata.data[i] = (data(trial=i) - mean(ref_data, axis=idx_chan)) elif method == 'regression': mdata.data[i] = compute_average_regress(data(trial=i), idx_chan) elif bipolar: if not data.index_of('chan') == 0: raise ValueError('For matrix multiplication to work, ' 'the first dimension should be chan') mdata.data[i] = dot(trans, data(trial=i)) mdata.axis['chan'][i] = asarray(chan.return_label(), dtype='U') else: mdata = data return mdata
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:remove_artf_evts; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:times; 5, identifier:annot; 6, default_parameter; 6, 7; 6, 8; 7, identifier:chan; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:min_dur; 11, float:0.1; 12, block; 12, 13; 12, 15; 12, 19; 12, 27; 12, 37; 12, 46; 12, 47; 12, 69; 12, 240; 13, expression_statement; 13, 14; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:new_times; 18, identifier:times; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:beg; 22, subscript; 22, 23; 22, 26; 23, subscript; 23, 24; 23, 25; 24, identifier:times; 25, integer:0; 26, integer:0; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:end; 30, subscript; 30, 31; 30, 35; 31, subscript; 31, 32; 31, 33; 32, identifier:times; 33, unary_operator:-; 33, 34; 34, integer:1; 35, unary_operator:-; 35, 36; 36, integer:1; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:chan; 40, conditional_expression:if; 40, 41; 40, 44; 40, 45; 41, tuple; 41, 42; 41, 43; 42, identifier:chan; 43, string:''; 44, identifier:chan; 45, None; 46, comment; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:artefact; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:annot; 53, identifier:get_events; 54, argument_list; 54, 55; 54, 58; 54, 63; 54, 66; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:name; 57, string:'Artefact'; 58, keyword_argument; 58, 59; 58, 60; 59, identifier:time; 60, tuple; 60, 61; 60, 62; 61, identifier:beg; 62, identifier:end; 63, keyword_argument; 63, 64; 63, 65; 64, identifier:chan; 65, identifier:chan; 66, keyword_argument; 66, 67; 66, 68; 67, identifier:qual; 68, string:'Good'; 69, if_statement; 69, 70; 69, 71; 70, identifier:artefact; 71, block; 71, 72; 71, 76; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:new_times; 75, list:[]; 76, for_statement; 76, 77; 76, 78; 76, 79; 77, identifier:seg; 78, identifier:times; 79, block; 79, 80; 79, 84; 79, 88; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:reject; 83, False; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:new_seg; 87, True; 88, while_statement; 88, 89; 88, 92; 89, comparison_operator:is; 89, 90; 89, 91; 90, identifier:new_seg; 91, False; 92, block; 92, 93; 92, 105; 92, 111; 92, 218; 93, if_statement; 93, 94; 93, 100; 94, comparison_operator:is; 94, 95; 94, 99; 95, call; 95, 96; 95, 97; 96, identifier:type; 97, argument_list; 97, 98; 98, identifier:new_seg; 99, identifier:tuple; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:seg; 104, identifier:new_seg; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:end; 108, subscript; 108, 109; 108, 110; 109, identifier:seg; 110, integer:1; 111, for_statement; 111, 112; 111, 113; 111, 114; 112, identifier:artf; 113, identifier:artefact; 114, block; 114, 115; 114, 141; 114, 154; 114, 167; 114, 214; 115, if_statement; 115, 116; 115, 131; 116, boolean_operator:and; 116, 117; 116, 124; 117, comparison_operator:<=; 117, 118; 117, 121; 118, subscript; 118, 119; 118, 120; 119, identifier:artf; 120, string:'start'; 121, subscript; 121, 122; 121, 123; 122, identifier:seg; 123, integer:0; 124, comparison_operator:<=; 124, 125; 124, 128; 125, subscript; 125, 126; 125, 127; 126, identifier:seg; 127, integer:1; 128, subscript; 128, 129; 128, 130; 129, identifier:artf; 130, string:'end'; 131, block; 131, 132; 131, 136; 131, 140; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:reject; 135, True; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:new_seg; 139, False; 140, break_statement; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:a_starts_in_s; 144, comparison_operator:<=; 144, 145; 144, 148; 144, 151; 145, subscript; 145, 146; 145, 147; 146, identifier:seg; 147, integer:0; 148, subscript; 148, 149; 148, 150; 149, identifier:artf; 150, string:'start'; 151, subscript; 151, 152; 151, 153; 152, identifier:seg; 153, integer:1; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:a_ends_in_s; 157, comparison_operator:<=; 157, 158; 157, 161; 157, 164; 158, subscript; 158, 159; 158, 160; 159, identifier:seg; 160, integer:0; 161, subscript; 161, 162; 161, 163; 162, identifier:artf; 163, string:'end'; 164, subscript; 164, 165; 164, 166; 165, identifier:seg; 166, integer:1; 167, if_statement; 167, 168; 167, 172; 167, 183; 168, boolean_operator:and; 168, 169; 168, 170; 169, identifier:a_ends_in_s; 170, not_operator; 170, 171; 171, identifier:a_starts_in_s; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:seg; 176, expression_list; 176, 177; 176, 180; 177, subscript; 177, 178; 177, 179; 178, identifier:artf; 179, string:'end'; 180, subscript; 180, 181; 180, 182; 181, identifier:seg; 182, integer:1; 183, elif_clause; 183, 184; 183, 185; 184, identifier:a_starts_in_s; 185, block; 185, 186; 185, 196; 185, 213; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:seg; 189, expression_list; 189, 190; 189, 193; 190, subscript; 190, 191; 190, 192; 191, identifier:seg; 192, integer:0; 193, subscript; 193, 194; 193, 195; 194, identifier:artf; 195, string:'start'; 196, if_statement; 196, 197; 196, 198; 196, 207; 197, identifier:a_ends_in_s; 198, block; 198, 199; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:new_seg; 202, expression_list; 202, 203; 202, 206; 203, subscript; 203, 204; 203, 205; 204, identifier:artf; 205, string:'end'; 206, identifier:end; 207, else_clause; 207, 208; 208, block; 208, 209; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:new_seg; 212, False; 213, break_statement; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:new_seg; 217, False; 218, if_statement; 218, 219; 218, 232; 219, boolean_operator:and; 219, 220; 219, 223; 220, comparison_operator:is; 220, 221; 220, 222; 221, identifier:reject; 222, False; 223, comparison_operator:>=; 223, 224; 223, 231; 224, binary_operator:-; 224, 225; 224, 228; 225, subscript; 225, 226; 225, 227; 226, identifier:seg; 227, integer:1; 228, subscript; 228, 229; 228, 230; 229, identifier:seg; 230, integer:0; 231, identifier:min_dur; 232, block; 232, 233; 233, expression_statement; 233, 234; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:new_times; 237, identifier:append; 238, argument_list; 238, 239; 239, identifier:seg; 240, return_statement; 240, 241; 241, identifier:new_times
def remove_artf_evts(times, annot, chan=None, min_dur=0.1): """Correct times to remove events marked 'Artefact'. Parameters ---------- times : list of tuple of float the start and end times of each segment annot : instance of Annotations the annotation file containing events and epochs chan : str, optional full name of channel on which artefacts were marked. Channel format is 'chan_name (group_name)'. If None, artefacts from any channel will be removed. min_dur : float resulting segments, after concatenation, are rejected if shorter than this duration Returns ------- list of tuple of float the new start and end times of each segment, with artefact periods taken out """ new_times = times beg = times[0][0] end = times[-1][-1] chan = (chan, '') if chan else None # '' is for channel-global artefacts artefact = annot.get_events(name='Artefact', time=(beg, end), chan=chan, qual='Good') if artefact: new_times = [] for seg in times: reject = False new_seg = True while new_seg is not False: if type(new_seg) is tuple: seg = new_seg end = seg[1] for artf in artefact: if artf['start'] <= seg[0] and seg[1] <= artf['end']: reject = True new_seg = False break a_starts_in_s = seg[0] <= artf['start'] <= seg[1] a_ends_in_s = seg[0] <= artf['end'] <= seg[1] if a_ends_in_s and not a_starts_in_s: seg = artf['end'], seg[1] elif a_starts_in_s: seg = seg[0], artf['start'] if a_ends_in_s: new_seg = artf['end'], end else: new_seg = False break new_seg = False if reject is False and seg[1] - seg[0] >= min_dur: new_times.append(seg) return new_times
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:_build_app_dict; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:site; 5, identifier:request; 6, default_parameter; 6, 7; 6, 8; 7, identifier:label; 8, None; 9, block; 9, 10; 9, 12; 9, 16; 9, 53; 9, 261; 9, 271; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:app_dict; 15, dictionary; 16, if_statement; 16, 17; 16, 18; 16, 45; 17, identifier:label; 18, block; 18, 19; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:models; 22, dictionary_comprehension; 22, 23; 22, 26; 22, 37; 23, pair; 23, 24; 23, 25; 24, identifier:m; 25, identifier:m_a; 26, for_in_clause; 26, 27; 26, 30; 27, pattern_list; 27, 28; 27, 29; 28, identifier:m; 29, identifier:m_a; 30, call; 30, 31; 30, 36; 31, attribute; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:site; 34, identifier:_registry; 35, identifier:items; 36, argument_list; 37, if_clause; 37, 38; 38, comparison_operator:==; 38, 39; 38, 44; 39, attribute; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:m; 42, identifier:_meta; 43, identifier:app_label; 44, identifier:label; 45, else_clause; 45, 46; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:models; 50, attribute; 50, 51; 50, 52; 51, identifier:site; 52, identifier:_registry; 53, for_statement; 53, 54; 53, 57; 53, 62; 54, pattern_list; 54, 55; 54, 56; 55, identifier:model; 56, identifier:model_admin; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:models; 60, identifier:items; 61, argument_list; 62, block; 62, 63; 62, 71; 62, 80; 62, 85; 62, 94; 62, 95; 62, 96; 62, 106; 62, 116; 62, 140; 62, 170; 62, 200; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:app_label; 66, attribute; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:model; 69, identifier:_meta; 70, identifier:app_label; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:has_module_perms; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:model_admin; 77, identifier:has_module_permission; 78, argument_list; 78, 79; 79, identifier:request; 80, if_statement; 80, 81; 80, 83; 81, not_operator; 81, 82; 82, identifier:has_module_perms; 83, block; 83, 84; 84, continue_statement; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:perms; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:model_admin; 91, identifier:get_model_perms; 92, argument_list; 92, 93; 93, identifier:request; 94, comment; 95, comment; 96, if_statement; 96, 97; 96, 104; 97, comparison_operator:not; 97, 98; 97, 99; 98, True; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:perms; 102, identifier:values; 103, argument_list; 104, block; 104, 105; 105, continue_statement; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:info; 109, tuple; 109, 110; 109, 111; 110, identifier:app_label; 111, attribute; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:model; 114, identifier:_meta; 115, identifier:model_name; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:model_dict; 119, dictionary; 119, 120; 119, 130; 119, 137; 120, pair; 120, 121; 120, 122; 121, string:'name'; 122, call; 122, 123; 122, 124; 123, identifier:capfirst; 124, argument_list; 124, 125; 125, attribute; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:model; 128, identifier:_meta; 129, identifier:verbose_name_plural; 130, pair; 130, 131; 130, 132; 131, string:'object_name'; 132, attribute; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:model; 135, identifier:_meta; 136, identifier:object_name; 137, pair; 137, 138; 137, 139; 138, string:'perms'; 139, identifier:perms; 140, if_statement; 140, 141; 140, 147; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:perms; 144, identifier:get; 145, argument_list; 145, 146; 146, string:'change'; 147, block; 147, 148; 148, try_statement; 148, 149; 148, 166; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 155; 152, subscript; 152, 153; 152, 154; 153, identifier:model_dict; 154, string:'admin_url'; 155, call; 155, 156; 155, 157; 156, identifier:reverse; 157, argument_list; 157, 158; 157, 161; 158, binary_operator:%; 158, 159; 158, 160; 159, string:'admin:%s_%s_changelist'; 160, identifier:info; 161, keyword_argument; 161, 162; 161, 163; 162, identifier:current_app; 163, attribute; 163, 164; 163, 165; 164, identifier:site; 165, identifier:name; 166, except_clause; 166, 167; 166, 168; 167, identifier:NoReverseMatch; 168, block; 168, 169; 169, pass_statement; 170, if_statement; 170, 171; 170, 177; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:perms; 174, identifier:get; 175, argument_list; 175, 176; 176, string:'add'; 177, block; 177, 178; 178, try_statement; 178, 179; 178, 196; 179, block; 179, 180; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 185; 182, subscript; 182, 183; 182, 184; 183, identifier:model_dict; 184, string:'add_url'; 185, call; 185, 186; 185, 187; 186, identifier:reverse; 187, argument_list; 187, 188; 187, 191; 188, binary_operator:%; 188, 189; 188, 190; 189, string:'admin:%s_%s_add'; 190, identifier:info; 191, keyword_argument; 191, 192; 191, 193; 192, identifier:current_app; 193, attribute; 193, 194; 193, 195; 194, identifier:site; 195, identifier:name; 196, except_clause; 196, 197; 196, 198; 197, identifier:NoReverseMatch; 198, block; 198, 199; 199, pass_statement; 200, if_statement; 200, 201; 200, 204; 200, 216; 201, comparison_operator:in; 201, 202; 201, 203; 202, identifier:app_label; 203, identifier:app_dict; 204, block; 204, 205; 205, expression_statement; 205, 206; 206, call; 206, 207; 206, 214; 207, attribute; 207, 208; 207, 213; 208, subscript; 208, 209; 208, 212; 209, subscript; 209, 210; 209, 211; 210, identifier:app_dict; 211, identifier:app_label; 212, string:'models'; 213, identifier:append; 214, argument_list; 214, 215; 215, identifier:model_dict; 216, else_clause; 216, 217; 217, block; 217, 218; 218, expression_statement; 218, 219; 219, assignment; 219, 220; 219, 223; 220, subscript; 220, 221; 220, 222; 221, identifier:app_dict; 222, identifier:app_label; 223, dictionary; 223, 224; 223, 234; 223, 237; 223, 254; 223, 257; 224, pair; 224, 225; 224, 226; 225, string:'name'; 226, attribute; 226, 227; 226, 233; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:apps; 230, identifier:get_app_config; 231, argument_list; 231, 232; 232, identifier:app_label; 233, identifier:verbose_name; 234, pair; 234, 235; 234, 236; 235, string:'app_label'; 236, identifier:app_label; 237, pair; 237, 238; 237, 239; 238, string:'app_url'; 239, call; 239, 240; 239, 241; 240, identifier:reverse; 241, argument_list; 241, 242; 241, 243; 241, 249; 242, string:'admin:app_list'; 243, keyword_argument; 243, 244; 243, 245; 244, identifier:kwargs; 245, dictionary; 245, 246; 246, pair; 246, 247; 246, 248; 247, string:'app_label'; 248, identifier:app_label; 249, keyword_argument; 249, 250; 249, 251; 250, identifier:current_app; 251, attribute; 251, 252; 251, 253; 252, identifier:site; 253, identifier:name; 254, pair; 254, 255; 254, 256; 255, string:'has_module_perms'; 256, identifier:has_module_perms; 257, pair; 257, 258; 257, 259; 258, string:'models'; 259, list:[model_dict]; 259, 260; 260, identifier:model_dict; 261, if_statement; 261, 262; 261, 263; 262, identifier:label; 263, block; 263, 264; 264, return_statement; 264, 265; 265, call; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, identifier:app_dict; 268, identifier:get; 269, argument_list; 269, 270; 270, identifier:label; 271, return_statement; 271, 272; 272, identifier:app_dict
def _build_app_dict(site, request, label=None): """ Builds the app dictionary. Takes an optional label parameters to filter models of a specific app. """ app_dict = {} if label: models = { m: m_a for m, m_a in site._registry.items() if m._meta.app_label == label } else: models = site._registry for model, model_admin in models.items(): app_label = model._meta.app_label has_module_perms = model_admin.has_module_permission(request) if not has_module_perms: continue perms = model_admin.get_model_perms(request) # Check whether user has any perm for this module. # If so, add the module to the model_list. if True not in perms.values(): continue info = (app_label, model._meta.model_name) model_dict = { 'name': capfirst(model._meta.verbose_name_plural), 'object_name': model._meta.object_name, 'perms': perms, } if perms.get('change'): try: model_dict['admin_url'] = reverse('admin:%s_%s_changelist' % info, current_app=site.name) except NoReverseMatch: pass if perms.get('add'): try: model_dict['add_url'] = reverse('admin:%s_%s_add' % info, current_app=site.name) except NoReverseMatch: pass if app_label in app_dict: app_dict[app_label]['models'].append(model_dict) else: app_dict[app_label] = { 'name': apps.get_app_config(app_label).verbose_name, 'app_label': app_label, 'app_url': reverse( 'admin:app_list', kwargs={'app_label': app_label}, current_app=site.name, ), 'has_module_perms': has_module_perms, 'models': [model_dict], } if label: return app_dict.get(label) return app_dict
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:get_app_list; 3, parameters; 3, 4; 3, 5; 4, identifier:site; 5, identifier:request; 6, block; 6, 7; 6, 9; 6, 17; 6, 18; 6, 41; 6, 42; 6, 62; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:app_dict; 12, call; 12, 13; 12, 14; 13, identifier:_build_app_dict; 14, argument_list; 14, 15; 14, 16; 15, identifier:site; 16, identifier:request; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:app_list; 21, call; 21, 22; 21, 23; 22, identifier:sorted; 23, argument_list; 23, 24; 23, 29; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:app_dict; 27, identifier:values; 28, argument_list; 29, keyword_argument; 29, 30; 29, 31; 30, identifier:key; 31, lambda; 31, 32; 31, 34; 32, lambda_parameters; 32, 33; 33, identifier:x; 34, call; 34, 35; 34, 40; 35, attribute; 35, 36; 35, 39; 36, subscript; 36, 37; 36, 38; 37, identifier:x; 38, string:'name'; 39, identifier:lower; 40, argument_list; 41, comment; 42, for_statement; 42, 43; 42, 44; 42, 45; 43, identifier:app; 44, identifier:app_list; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, call; 47, 48; 47, 53; 48, attribute; 48, 49; 48, 52; 49, subscript; 49, 50; 49, 51; 50, identifier:app; 51, string:'models'; 52, identifier:sort; 53, argument_list; 53, 54; 54, keyword_argument; 54, 55; 54, 56; 55, identifier:key; 56, lambda; 56, 57; 56, 59; 57, lambda_parameters; 57, 58; 58, identifier:x; 59, subscript; 59, 60; 59, 61; 60, identifier:x; 61, string:'name'; 62, return_statement; 62, 63; 63, identifier:app_list
def get_app_list(site, request): """ Returns a sorted list of all the installed apps that have been registered in this site. """ app_dict = _build_app_dict(site, request) # Sort the apps alphabetically. app_list = sorted(app_dict.values(), key=lambda x: x['name'].lower()) # Sort the models alphabetically within each app. for app in app_list: app['models'].sort(key=lambda x: x['name']) return app_list
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:format_query_results; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:r; 6, identifier:prop_nr; 7, block; 7, 8; 7, 10; 7, 19; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:prop_dt; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:get_prop_datatype; 17, argument_list; 17, 18; 18, identifier:prop_nr; 19, for_statement; 19, 20; 19, 21; 19, 22; 20, identifier:i; 21, identifier:r; 22, block; 22, 23; 22, 56; 22, 57; 22, 58; 22, 59; 22, 110; 22, 111; 22, 112; 22, 187; 22, 188; 22, 249; 22, 250; 23, for_statement; 23, 24; 23, 25; 23, 31; 24, identifier:value; 25, set; 25, 26; 25, 27; 25, 28; 25, 29; 25, 30; 26, string:'item'; 27, string:'sid'; 28, string:'pq'; 29, string:'pr'; 30, string:'ref'; 31, block; 31, 32; 32, if_statement; 32, 33; 32, 36; 32, 37; 33, comparison_operator:in; 33, 34; 33, 35; 34, identifier:value; 35, identifier:i; 36, comment; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 43; 40, subscript; 40, 41; 40, 42; 41, identifier:i; 42, identifier:value; 43, subscript; 43, 44; 43, 54; 44, call; 44, 45; 44, 52; 45, attribute; 45, 46; 45, 51; 46, subscript; 46, 47; 46, 50; 47, subscript; 47, 48; 47, 49; 48, identifier:i; 49, identifier:value; 50, string:'value'; 51, identifier:split; 52, argument_list; 52, 53; 53, string:'/'; 54, unary_operator:-; 54, 55; 55, integer:1; 56, comment; 57, comment; 58, comment; 59, for_statement; 59, 60; 59, 61; 59, 65; 60, identifier:value; 61, set; 61, 62; 61, 63; 61, 64; 62, string:'v'; 63, string:'qval'; 64, string:'rval'; 65, block; 65, 66; 66, if_statement; 66, 67; 66, 70; 67, comparison_operator:in; 67, 68; 67, 69; 68, identifier:value; 69, identifier:i; 70, block; 70, 71; 71, if_statement; 71, 72; 71, 94; 71, 95; 72, boolean_operator:and; 72, 73; 72, 83; 73, comparison_operator:==; 73, 74; 73, 82; 74, call; 74, 75; 74, 80; 75, attribute; 75, 76; 75, 79; 76, subscript; 76, 77; 76, 78; 77, identifier:i; 78, identifier:value; 79, identifier:get; 80, argument_list; 80, 81; 81, string:"datatype"; 82, string:'http://www.w3.org/2001/XMLSchema#dateTime'; 83, not_operator; 83, 84; 83, 85; 84, line_continuation:\; 85, comparison_operator:in; 85, 86; 85, 93; 86, subscript; 86, 87; 86, 92; 87, subscript; 87, 88; 87, 91; 88, subscript; 88, 89; 88, 90; 89, identifier:i; 90, identifier:value; 91, string:'value'; 92, integer:0; 93, string:'+-'; 94, comment; 95, block; 95, 96; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 103; 98, subscript; 98, 99; 98, 102; 99, subscript; 99, 100; 99, 101; 100, identifier:i; 101, identifier:value; 102, string:'value'; 103, binary_operator:+; 103, 104; 103, 105; 104, string:'+'; 105, subscript; 105, 106; 105, 109; 106, subscript; 106, 107; 106, 108; 107, identifier:i; 108, identifier:value; 109, string:'value'; 110, comment; 111, comment; 112, if_statement; 112, 113; 112, 116; 113, comparison_operator:in; 113, 114; 113, 115; 114, string:'v'; 115, identifier:i; 116, block; 116, 117; 116, 160; 116, 161; 116, 162; 117, if_statement; 117, 118; 117, 129; 117, 148; 118, boolean_operator:and; 118, 119; 118, 126; 119, comparison_operator:==; 119, 120; 119, 125; 120, subscript; 120, 121; 120, 124; 121, subscript; 121, 122; 121, 123; 122, identifier:i; 123, string:'v'; 124, string:'type'; 125, string:'uri'; 126, comparison_operator:==; 126, 127; 126, 128; 127, identifier:prop_dt; 128, string:'wikibase-item'; 129, block; 129, 130; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 135; 132, subscript; 132, 133; 132, 134; 133, identifier:i; 134, string:'v'; 135, subscript; 135, 136; 135, 146; 136, call; 136, 137; 136, 144; 137, attribute; 137, 138; 137, 143; 138, subscript; 138, 139; 138, 142; 139, subscript; 139, 140; 139, 141; 140, identifier:i; 141, string:'v'; 142, string:'value'; 143, identifier:split; 144, argument_list; 144, 145; 145, string:'/'; 146, unary_operator:-; 146, 147; 147, integer:1; 148, else_clause; 148, 149; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 155; 152, subscript; 152, 153; 152, 154; 153, identifier:i; 154, string:'v'; 155, subscript; 155, 156; 155, 159; 156, subscript; 156, 157; 156, 158; 157, identifier:i; 158, string:'v'; 159, string:'value'; 160, comment; 161, comment; 162, if_statement; 162, 163; 162, 171; 163, comparison_operator:is; 163, 164; 163, 170; 164, call; 164, 165; 164, 166; 165, identifier:type; 166, argument_list; 166, 167; 167, subscript; 167, 168; 167, 169; 168, identifier:i; 169, string:'v'; 170, identifier:dict; 171, block; 171, 172; 172, expression_statement; 172, 173; 173, call; 173, 174; 173, 183; 174, attribute; 174, 175; 174, 182; 175, subscript; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:self; 178, identifier:rev_lookup; 179, subscript; 179, 180; 179, 181; 180, identifier:i; 181, string:'v'; 182, identifier:add; 183, argument_list; 183, 184; 184, subscript; 184, 185; 184, 186; 185, identifier:i; 186, string:'item'; 187, comment; 188, if_statement; 188, 189; 188, 192; 189, comparison_operator:in; 189, 190; 189, 191; 190, string:'qval'; 191, identifier:i; 192, block; 192, 193; 192, 206; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:qual_prop_dt; 196, call; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:self; 199, identifier:get_prop_datatype; 200, argument_list; 200, 201; 201, keyword_argument; 201, 202; 201, 203; 202, identifier:prop_nr; 203, subscript; 203, 204; 203, 205; 204, identifier:i; 205, string:'pq'; 206, if_statement; 206, 207; 206, 218; 206, 237; 207, boolean_operator:and; 207, 208; 207, 215; 208, comparison_operator:==; 208, 209; 208, 214; 209, subscript; 209, 210; 209, 213; 210, subscript; 210, 211; 210, 212; 211, identifier:i; 212, string:'qval'; 213, string:'type'; 214, string:'uri'; 215, comparison_operator:==; 215, 216; 215, 217; 216, identifier:qual_prop_dt; 217, string:'wikibase-item'; 218, block; 218, 219; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 224; 221, subscript; 221, 222; 221, 223; 222, identifier:i; 223, string:'qval'; 224, subscript; 224, 225; 224, 235; 225, call; 225, 226; 225, 233; 226, attribute; 226, 227; 226, 232; 227, subscript; 227, 228; 227, 231; 228, subscript; 228, 229; 228, 230; 229, identifier:i; 230, string:'qval'; 231, string:'value'; 232, identifier:split; 233, argument_list; 233, 234; 234, string:'/'; 235, unary_operator:-; 235, 236; 236, integer:1; 237, else_clause; 237, 238; 238, block; 238, 239; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 244; 241, subscript; 241, 242; 241, 243; 242, identifier:i; 243, string:'qval'; 244, subscript; 244, 245; 244, 248; 245, subscript; 245, 246; 245, 247; 246, identifier:i; 247, string:'qval'; 248, string:'value'; 249, comment; 250, if_statement; 250, 251; 250, 254; 251, comparison_operator:in; 251, 252; 251, 253; 252, string:'rval'; 253, identifier:i; 254, block; 254, 255; 254, 268; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 258; 257, identifier:ref_prop_dt; 258, call; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:self; 261, identifier:get_prop_datatype; 262, argument_list; 262, 263; 263, keyword_argument; 263, 264; 263, 265; 264, identifier:prop_nr; 265, subscript; 265, 266; 265, 267; 266, identifier:i; 267, string:'pr'; 268, if_statement; 268, 269; 268, 280; 268, 299; 269, boolean_operator:and; 269, 270; 269, 277; 270, comparison_operator:==; 270, 271; 270, 276; 271, subscript; 271, 272; 271, 275; 272, subscript; 272, 273; 272, 274; 273, identifier:i; 274, string:'rval'; 275, string:'type'; 276, string:'uri'; 277, comparison_operator:==; 277, 278; 277, 279; 278, identifier:ref_prop_dt; 279, string:'wikibase-item'; 280, block; 280, 281; 281, expression_statement; 281, 282; 282, assignment; 282, 283; 282, 286; 283, subscript; 283, 284; 283, 285; 284, identifier:i; 285, string:'rval'; 286, subscript; 286, 287; 286, 297; 287, call; 287, 288; 287, 295; 288, attribute; 288, 289; 288, 294; 289, subscript; 289, 290; 289, 293; 290, subscript; 290, 291; 290, 292; 291, identifier:i; 292, string:'rval'; 293, string:'value'; 294, identifier:split; 295, argument_list; 295, 296; 296, string:'/'; 297, unary_operator:-; 297, 298; 298, integer:1; 299, else_clause; 299, 300; 300, block; 300, 301; 301, expression_statement; 301, 302; 302, assignment; 302, 303; 302, 306; 303, subscript; 303, 304; 303, 305; 304, identifier:i; 305, string:'rval'; 306, subscript; 306, 307; 306, 310; 307, subscript; 307, 308; 307, 309; 308, identifier:i; 309, string:'rval'; 310, string:'value'
def format_query_results(self, r, prop_nr): """ `r` is the results of the sparql query in _query_data and is modified in place `prop_nr` is needed to get the property datatype to determine how to format the value `r` is a list of dicts. The keys are: item: the subject. the item this statement is on v: the object. The value for this statement sid: statement ID pq: qualifier property qval: qualifier value ref: reference ID pr: reference property rval: reference value """ prop_dt = self.get_prop_datatype(prop_nr) for i in r: for value in {'item', 'sid', 'pq', 'pr', 'ref'}: if value in i: # these are always URIs for the local wikibase i[value] = i[value]['value'].split('/')[-1] # make sure datetimes are formatted correctly. # the correct format is '+%Y-%m-%dT%H:%M:%SZ', but is sometimes missing the plus?? # some difference between RDF and xsd:dateTime that I don't understand for value in {'v', 'qval', 'rval'}: if value in i: if i[value].get("datatype") == 'http://www.w3.org/2001/XMLSchema#dateTime' and not \ i[value]['value'][0] in '+-': # if it is a dateTime and doesn't start with plus or minus, add a plus i[value]['value'] = '+' + i[value]['value'] # these three ({'v', 'qval', 'rval'}) are values that can be any data type # strip off the URI if they are wikibase-items if 'v' in i: if i['v']['type'] == 'uri' and prop_dt == 'wikibase-item': i['v'] = i['v']['value'].split('/')[-1] else: i['v'] = i['v']['value'] # Note: no-value and some-value don't actually show up in the results here # see for example: select * where { wd:Q7207 p:P40 ?c . ?c ?d ?e } if type(i['v']) is not dict: self.rev_lookup[i['v']].add(i['item']) # handle qualifier value if 'qval' in i: qual_prop_dt = self.get_prop_datatype(prop_nr=i['pq']) if i['qval']['type'] == 'uri' and qual_prop_dt == 'wikibase-item': i['qval'] = i['qval']['value'].split('/')[-1] else: i['qval'] = i['qval']['value'] # handle reference value if 'rval' in i: ref_prop_dt = self.get_prop_datatype(prop_nr=i['pr']) if i['rval']['type'] == 'uri' and ref_prop_dt == 'wikibase-item': i['rval'] = i['rval']['value'].split('/')[-1] else: i['rval'] = i['rval']['value']
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:times; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:p; 5, identifier:mint; 6, default_parameter; 6, 7; 6, 8; 7, identifier:maxt; 8, None; 9, block; 9, 10; 9, 12; 9, 19; 9, 164; 10, expression_statement; 10, 11; 11, string:'''Repeat a parser between `mint` and `maxt` times. DO AS MUCH MATCH AS IT CAN. Return a list of values.'''; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:maxt; 15, conditional_expression:if; 15, 16; 15, 17; 15, 18; 16, identifier:maxt; 17, identifier:maxt; 18, identifier:mint; 19, decorated_definition; 19, 20; 19, 22; 20, decorator; 20, 21; 21, identifier:Parser; 22, function_definition; 22, 23; 22, 24; 22, 27; 23, function_name:times_parser; 24, parameters; 24, 25; 24, 26; 25, identifier:text; 26, identifier:index; 27, block; 27, 28; 27, 44; 27, 162; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 34; 30, pattern_list; 30, 31; 30, 32; 30, 33; 31, identifier:cnt; 32, identifier:values; 33, identifier:res; 34, expression_list; 34, 35; 34, 36; 34, 43; 35, integer:0; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:Value; 39, identifier:success; 40, argument_list; 40, 41; 40, 42; 41, identifier:index; 42, list:[]; 43, None; 44, while_statement; 44, 45; 44, 48; 45, comparison_operator:<; 45, 46; 45, 47; 46, identifier:cnt; 47, identifier:maxt; 48, block; 48, 49; 48, 57; 48, 107; 48, 114; 48, 115; 48, 116; 48, 117; 48, 118; 48, 119; 48, 120; 48, 121; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:res; 52, call; 52, 53; 52, 54; 53, identifier:p; 54, argument_list; 54, 55; 54, 56; 55, identifier:text; 56, identifier:index; 57, if_statement; 57, 58; 57, 61; 57, 94; 58, attribute; 58, 59; 58, 60; 59, identifier:res; 60, identifier:status; 61, block; 61, 62; 61, 82; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:values; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:values; 68, identifier:aggregate; 69, argument_list; 69, 70; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:Value; 73, identifier:success; 74, argument_list; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:res; 77, identifier:index; 78, list:[res.value]; 78, 79; 79, attribute; 79, 80; 79, 81; 80, identifier:res; 81, identifier:value; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 87; 84, pattern_list; 84, 85; 84, 86; 85, identifier:index; 86, identifier:cnt; 87, expression_list; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:res; 90, identifier:index; 91, binary_operator:+; 91, 92; 91, 93; 92, identifier:cnt; 93, integer:1; 94, else_clause; 94, 95; 95, block; 95, 96; 96, if_statement; 96, 97; 96, 100; 96, 102; 97, comparison_operator:>=; 97, 98; 97, 99; 98, identifier:cnt; 99, identifier:mint; 100, block; 100, 101; 101, break_statement; 102, else_clause; 102, 103; 103, block; 103, 104; 103, 106; 104, return_statement; 104, 105; 105, identifier:res; 106, comment; 107, if_statement; 107, 108; 107, 111; 107, 112; 108, comparison_operator:>=; 108, 109; 108, 110; 109, identifier:cnt; 110, identifier:maxt; 111, comment; 112, block; 112, 113; 113, break_statement; 114, comment; 115, comment; 116, comment; 117, comment; 118, comment; 119, comment; 120, comment; 121, if_statement; 121, 122; 121, 128; 122, comparison_operator:>=; 122, 123; 122, 124; 123, identifier:index; 124, call; 124, 125; 124, 126; 125, identifier:len; 126, argument_list; 126, 127; 127, identifier:text; 128, block; 128, 129; 129, if_statement; 129, 130; 129, 133; 129, 136; 130, comparison_operator:>=; 130, 131; 130, 132; 131, identifier:cnt; 132, identifier:mint; 133, block; 133, 134; 133, 135; 134, break_statement; 135, comment; 136, else_clause; 136, 137; 137, block; 137, 138; 137, 146; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:r; 141, call; 141, 142; 141, 143; 142, identifier:p; 143, argument_list; 143, 144; 143, 145; 144, identifier:text; 145, identifier:index; 146, if_statement; 146, 147; 146, 152; 146, 153; 147, comparison_operator:!=; 147, 148; 147, 149; 148, identifier:index; 149, attribute; 149, 150; 149, 151; 150, identifier:r; 151, identifier:index; 152, comment; 153, block; 153, 154; 154, return_statement; 154, 155; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:Value; 158, identifier:failure; 159, argument_list; 159, 160; 159, 161; 160, identifier:index; 161, string:"already meets the end, no enough text"; 162, return_statement; 162, 163; 163, identifier:values; 164, return_statement; 164, 165; 165, identifier:times_parser
def times(p, mint, maxt=None): '''Repeat a parser between `mint` and `maxt` times. DO AS MUCH MATCH AS IT CAN. Return a list of values.''' maxt = maxt if maxt else mint @Parser def times_parser(text, index): cnt, values, res = 0, Value.success(index, []), None while cnt < maxt: res = p(text, index) if res.status: values = values.aggregate( Value.success(res.index, [res.value])) index, cnt = res.index, cnt + 1 else: if cnt >= mint: break else: return res # failed, throw exception. if cnt >= maxt: # finish. break # If we don't have any remaining text to start next loop, we need break. # # We cannot put the `index < len(text)` in where because some parser can # success even when we have no any text. We also need to detect if the # parser consume no text. # # See: #28 if index >= len(text): if cnt >= mint: break # we already have decent result to return else: r = p(text, index) if index != r.index: # report error when the parser cannot success with no text return Value.failure(index, "already meets the end, no enough text") return values return times_parser
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:separated; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 4, identifier:p; 5, identifier:sep; 6, identifier:mint; 7, default_parameter; 7, 8; 7, 9; 8, identifier:maxt; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:end; 12, None; 13, block; 13, 14; 13, 16; 13, 23; 13, 216; 14, expression_statement; 14, 15; 15, string:'''Repeat a parser `p` separated by `s` between `mint` and `maxt` times. When `end` is None, a trailing separator is optional. When `end` is True, a trailing separator is required. When `end` is False, a trailing separator is not allowed. MATCHES AS MUCH AS POSSIBLE. Return list of values returned by `p`.'''; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:maxt; 19, conditional_expression:if; 19, 20; 19, 21; 19, 22; 20, identifier:maxt; 21, identifier:maxt; 22, identifier:mint; 23, decorated_definition; 23, 24; 23, 26; 24, decorator; 24, 25; 25, identifier:Parser; 26, function_definition; 26, 27; 26, 28; 26, 31; 27, function_name:sep_parser; 28, parameters; 28, 29; 28, 30; 29, identifier:text; 30, identifier:index; 31, block; 31, 32; 31, 48; 31, 214; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 38; 34, pattern_list; 34, 35; 34, 36; 34, 37; 35, identifier:cnt; 36, identifier:values; 37, identifier:res; 38, expression_list; 38, 39; 38, 40; 38, 47; 39, integer:0; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:Value; 43, identifier:success; 44, argument_list; 44, 45; 44, 46; 45, identifier:index; 46, list:[]; 47, None; 48, while_statement; 48, 49; 48, 52; 49, comparison_operator:<; 49, 50; 49, 51; 50, identifier:cnt; 51, identifier:maxt; 52, block; 52, 53; 52, 109; 52, 117; 52, 165; 52, 208; 53, if_statement; 53, 54; 53, 63; 54, boolean_operator:and; 54, 55; 54, 60; 55, comparison_operator:in; 55, 56; 55, 57; 56, identifier:end; 57, list:[False, None]; 57, 58; 57, 59; 58, False; 59, None; 60, comparison_operator:>; 60, 61; 60, 62; 61, identifier:cnt; 62, integer:0; 63, block; 63, 64; 63, 72; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:res; 67, call; 67, 68; 67, 69; 68, identifier:sep; 69, argument_list; 69, 70; 69, 71; 70, identifier:text; 71, identifier:index; 72, if_statement; 72, 73; 72, 76; 72, 77; 72, 98; 72, 106; 73, attribute; 73, 74; 73, 75; 74, identifier:res; 75, identifier:status; 76, comment; 77, block; 77, 78; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 83; 80, pattern_list; 80, 81; 80, 82; 81, identifier:index; 82, identifier:values; 83, expression_list; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:res; 86, identifier:index; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:Value; 90, identifier:success; 91, argument_list; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:res; 94, identifier:index; 95, attribute; 95, 96; 95, 97; 96, identifier:values; 97, identifier:value; 98, elif_clause; 98, 99; 98, 102; 99, comparison_operator:<; 99, 100; 99, 101; 100, identifier:cnt; 101, identifier:mint; 102, block; 102, 103; 102, 105; 103, return_statement; 103, 104; 104, identifier:res; 105, comment; 106, else_clause; 106, 107; 107, block; 107, 108; 108, break_statement; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:res; 112, call; 112, 113; 112, 114; 113, identifier:p; 114, argument_list; 114, 115; 114, 116; 115, identifier:text; 116, identifier:index; 117, if_statement; 117, 118; 117, 121; 117, 154; 117, 160; 118, attribute; 118, 119; 118, 120; 119, identifier:res; 120, identifier:status; 121, block; 121, 122; 121, 142; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:values; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:values; 128, identifier:aggregate; 129, argument_list; 129, 130; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:Value; 133, identifier:success; 134, argument_list; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:res; 137, identifier:index; 138, list:[res.value]; 138, 139; 139, attribute; 139, 140; 139, 141; 140, identifier:res; 141, identifier:value; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 147; 144, pattern_list; 144, 145; 144, 146; 145, identifier:index; 146, identifier:cnt; 147, expression_list; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:res; 150, identifier:index; 151, binary_operator:+; 151, 152; 151, 153; 152, identifier:cnt; 153, integer:1; 154, elif_clause; 154, 155; 154, 158; 155, comparison_operator:>=; 155, 156; 155, 157; 156, identifier:cnt; 157, identifier:mint; 158, block; 158, 159; 159, break_statement; 160, else_clause; 160, 161; 161, block; 161, 162; 161, 164; 162, return_statement; 162, 163; 163, identifier:res; 164, comment; 165, if_statement; 165, 166; 165, 169; 166, comparison_operator:is; 166, 167; 166, 168; 167, identifier:end; 168, True; 169, block; 169, 170; 169, 178; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:res; 173, call; 173, 174; 173, 175; 174, identifier:sep; 175, argument_list; 175, 176; 175, 177; 176, identifier:text; 177, identifier:index; 178, if_statement; 178, 179; 178, 182; 178, 203; 179, attribute; 179, 180; 179, 181; 180, identifier:res; 181, identifier:status; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 188; 185, pattern_list; 185, 186; 185, 187; 186, identifier:index; 187, identifier:values; 188, expression_list; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:res; 191, identifier:index; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:Value; 195, identifier:success; 196, argument_list; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:res; 199, identifier:index; 200, attribute; 200, 201; 200, 202; 201, identifier:values; 202, identifier:value; 203, else_clause; 203, 204; 204, block; 204, 205; 204, 207; 205, return_statement; 205, 206; 206, identifier:res; 207, comment; 208, if_statement; 208, 209; 208, 212; 209, comparison_operator:>=; 209, 210; 209, 211; 210, identifier:cnt; 211, identifier:maxt; 212, block; 212, 213; 213, break_statement; 214, return_statement; 214, 215; 215, identifier:values; 216, return_statement; 216, 217; 217, identifier:sep_parser
def separated(p, sep, mint, maxt=None, end=None): '''Repeat a parser `p` separated by `s` between `mint` and `maxt` times. When `end` is None, a trailing separator is optional. When `end` is True, a trailing separator is required. When `end` is False, a trailing separator is not allowed. MATCHES AS MUCH AS POSSIBLE. Return list of values returned by `p`.''' maxt = maxt if maxt else mint @Parser def sep_parser(text, index): cnt, values, res = 0, Value.success(index, []), None while cnt < maxt: if end in [False, None] and cnt > 0: res = sep(text, index) if res.status: # `sep` found, consume it (advance index) index, values = res.index, Value.success( res.index, values.value) elif cnt < mint: return res # error: need more elemnts, but no `sep` found. else: break res = p(text, index) if res.status: values = values.aggregate( Value.success(res.index, [res.value])) index, cnt = res.index, cnt + 1 elif cnt >= mint: break else: return res # error: need more elements, but no `p` found. if end is True: res = sep(text, index) if res.status: index, values = res.index, Value.success( res.index, values.value) else: return res # error: trailing `sep` not found if cnt >= maxt: break return values return sep_parser
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:route; 3, parameters; 3, 4; 4, list_splat_pattern; 4, 5; 5, identifier:args; 6, block; 6, 7; 6, 9; 6, 57; 6, 196; 6, 201; 7, expression_statement; 7, 8; 8, comment; 9, function_definition; 9, 10; 9, 11; 9, 13; 10, function_name:_validate_route; 11, parameters; 11, 12; 12, identifier:route; 13, block; 13, 14; 13, 31; 14, if_statement; 14, 15; 14, 23; 15, not_operator; 15, 16; 16, call; 16, 17; 16, 18; 17, identifier:isinstance; 18, argument_list; 18, 19; 18, 20; 19, identifier:route; 20, attribute; 20, 21; 20, 22; 21, identifier:six; 22, identifier:string_types; 23, block; 23, 24; 24, raise_statement; 24, 25; 25, call; 25, 26; 25, 27; 26, identifier:TypeError; 27, argument_list; 27, 28; 28, binary_operator:%; 28, 29; 28, 30; 29, string:'%s must be a string'; 30, identifier:route; 31, if_statement; 31, 32; 31, 46; 32, boolean_operator:or; 32, 33; 32, 38; 33, comparison_operator:in; 33, 34; 33, 35; 34, identifier:route; 35, tuple; 35, 36; 35, 37; 36, string:'.'; 37, string:'..'; 38, not_operator; 38, 39; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:re; 42, identifier:match; 43, argument_list; 43, 44; 43, 45; 44, string:'^[0-9a-zA-Z-_$\(\)\.~!,;:*+@=]+$'; 45, identifier:route; 46, block; 46, 47; 47, raise_statement; 47, 48; 48, call; 48, 49; 48, 50; 49, identifier:ValueError; 50, argument_list; 50, 51; 51, binary_operator:%; 51, 52; 51, 56; 52, concatenated_string; 52, 53; 52, 54; 52, 55; 53, string:'%s must be a valid path segment. Keep in mind '; 54, string:'that path segments should not contain path separators '; 55, string:'(e.g., /) '; 56, identifier:route; 57, if_statement; 57, 58; 57, 64; 57, 65; 57, 66; 57, 67; 57, 68; 57, 69; 57, 133; 57, 187; 58, comparison_operator:==; 58, 59; 58, 63; 59, call; 59, 60; 59, 61; 60, identifier:len; 61, argument_list; 61, 62; 62, identifier:args; 63, integer:2; 64, comment; 65, comment; 66, comment; 67, comment; 68, comment; 69, block; 69, 70; 69, 76; 69, 88; 69, 102; 69, 112; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 75; 72, pattern_list; 72, 73; 72, 74; 73, identifier:route; 74, identifier:handler; 75, identifier:args; 76, if_statement; 76, 77; 76, 81; 77, call; 77, 78; 77, 79; 78, identifier:ismethod; 79, argument_list; 79, 80; 80, identifier:handler; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:handler; 85, attribute; 85, 86; 85, 87; 86, identifier:handler; 87, identifier:__func__; 88, if_statement; 88, 89; 88, 94; 89, not_operator; 89, 90; 90, call; 90, 91; 90, 92; 91, identifier:iscontroller; 92, argument_list; 92, 93; 93, identifier:handler; 94, block; 94, 95; 95, raise_statement; 95, 96; 96, call; 96, 97; 96, 98; 97, identifier:TypeError; 98, argument_list; 98, 99; 99, binary_operator:%; 99, 100; 99, 101; 100, string:'%s must be a callable decorated with @pecan.expose'; 101, identifier:handler; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 108; 104, pattern_list; 104, 105; 104, 106; 104, 107; 105, identifier:obj; 106, identifier:attr; 107, identifier:value; 108, expression_list; 108, 109; 108, 110; 108, 111; 109, identifier:handler; 110, string:'custom_route'; 111, identifier:route; 112, if_statement; 112, 113; 112, 121; 113, comparison_operator:in; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:handler; 116, identifier:__name__; 117, tuple; 117, 118; 117, 119; 117, 120; 118, string:'_lookup'; 119, string:'_default'; 120, string:'_route'; 121, block; 121, 122; 122, raise_statement; 122, 123; 123, call; 123, 124; 123, 125; 124, identifier:ValueError; 125, argument_list; 125, 126; 126, binary_operator:%; 126, 127; 126, 130; 127, concatenated_string; 127, 128; 127, 129; 128, string:'%s is a special method in pecan and cannot be used in '; 129, string:'combination with custom path segments.'; 130, attribute; 130, 131; 130, 132; 131, identifier:handler; 132, identifier:__name__; 133, elif_clause; 133, 134; 133, 140; 133, 141; 133, 142; 134, comparison_operator:==; 134, 135; 134, 139; 135, call; 135, 136; 135, 137; 136, identifier:len; 137, argument_list; 137, 138; 138, identifier:args; 139, integer:3; 140, comment; 141, comment; 142, block; 142, 143; 142, 150; 142, 157; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 149; 145, pattern_list; 145, 146; 145, 147; 145, 148; 146, identifier:_; 147, identifier:route; 148, identifier:handler; 149, identifier:args; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 156; 152, pattern_list; 152, 153; 152, 154; 152, 155; 153, identifier:obj; 154, identifier:attr; 155, identifier:value; 156, identifier:args; 157, if_statement; 157, 158; 157, 163; 158, call; 158, 159; 158, 160; 159, identifier:hasattr; 160, argument_list; 160, 161; 160, 162; 161, identifier:obj; 162, identifier:attr; 163, block; 163, 164; 164, raise_statement; 164, 165; 165, call; 165, 166; 165, 167; 166, identifier:RuntimeError; 167, argument_list; 167, 168; 168, parenthesized_expression; 168, 169; 169, binary_operator:%; 169, 170; 169, 173; 170, concatenated_string; 170, 171; 170, 172; 171, string:"%(module)s.%(class)s already has an "; 172, string:"existing attribute named \"%(route)s\"."; 173, dictionary; 173, 174; 173, 179; 173, 184; 174, pair; 174, 175; 174, 176; 175, string:'module'; 176, attribute; 176, 177; 176, 178; 177, identifier:obj; 178, identifier:__module__; 179, pair; 179, 180; 179, 181; 180, string:'class'; 181, attribute; 181, 182; 181, 183; 182, identifier:obj; 183, identifier:__name__; 184, pair; 184, 185; 184, 186; 185, string:'route'; 186, identifier:attr; 187, else_clause; 187, 188; 188, block; 188, 189; 189, raise_statement; 189, 190; 190, call; 190, 191; 190, 192; 191, identifier:TypeError; 192, argument_list; 192, 193; 193, concatenated_string; 193, 194; 193, 195; 194, string:'pecan.route should be called in the format '; 195, string:'route(ParentController, "path-segment", SubController())'; 196, expression_statement; 196, 197; 197, call; 197, 198; 197, 199; 198, identifier:_validate_route; 199, argument_list; 199, 200; 200, identifier:route; 201, expression_statement; 201, 202; 202, call; 202, 203; 202, 204; 203, identifier:setattr; 204, argument_list; 204, 205; 204, 206; 204, 207; 205, identifier:obj; 206, identifier:attr; 207, identifier:value
def route(*args): """ This function is used to define an explicit route for a path segment. You generally only want to use this in situations where your desired path segment is not a valid Python variable/function name. For example, if you wanted to be able to route to: /path/with-dashes/ ...the following is invalid Python syntax:: class Controller(object): with-dashes = SubController() ...so you would instead define the route explicitly:: class Controller(object): pass pecan.route(Controller, 'with-dashes', SubController()) """ def _validate_route(route): if not isinstance(route, six.string_types): raise TypeError('%s must be a string' % route) if route in ('.', '..') or not re.match( '^[0-9a-zA-Z-_$\(\)\.~!,;:*+@=]+$', route ): raise ValueError( '%s must be a valid path segment. Keep in mind ' 'that path segments should not contain path separators ' '(e.g., /) ' % route ) if len(args) == 2: # The handler in this situation is a @pecan.expose'd callable, # and is generally only used by the @expose() decorator itself. # # This sets a special attribute, `custom_route` on the callable, which # pecan's routing logic knows how to make use of (as a special case) route, handler = args if ismethod(handler): handler = handler.__func__ if not iscontroller(handler): raise TypeError( '%s must be a callable decorated with @pecan.expose' % handler ) obj, attr, value = handler, 'custom_route', route if handler.__name__ in ('_lookup', '_default', '_route'): raise ValueError( '%s is a special method in pecan and cannot be used in ' 'combination with custom path segments.' % handler.__name__ ) elif len(args) == 3: # This is really just a setattr on the parent controller (with some # additional validation for the path segment itself) _, route, handler = args obj, attr, value = args if hasattr(obj, attr): raise RuntimeError( ( "%(module)s.%(class)s already has an " "existing attribute named \"%(route)s\"." % { 'module': obj.__module__, 'class': obj.__name__, 'route': attr } ), ) else: raise TypeError( 'pecan.route should be called in the format ' 'route(ParentController, "path-segment", SubController())' ) _validate_route(route) setattr(obj, attr, value)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:find_object; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:obj; 5, identifier:remainder; 6, identifier:notfound_handlers; 7, identifier:request; 8, block; 8, 9; 8, 11; 8, 15; 9, expression_statement; 9, 10; 10, string:''' 'Walks' the url path in search of an action for which a controller is implemented and returns that controller object along with what's left of the remainder. '''; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:prev_obj; 14, None; 15, while_statement; 15, 16; 15, 17; 16, True; 17, block; 17, 18; 17, 25; 17, 45; 17, 50; 17, 83; 17, 84; 17, 90; 17, 155; 17, 164; 17, 180; 17, 189; 17, 205; 17, 214; 17, 288; 17, 294; 17, 298; 17, 302; 17, 306; 17, 324; 17, 325; 17, 326; 17, 327; 17, 328; 18, if_statement; 18, 19; 18, 22; 19, comparison_operator:is; 19, 20; 19, 21; 20, identifier:obj; 21, None; 22, block; 22, 23; 23, raise_statement; 23, 24; 24, identifier:PecanNotFound; 25, if_statement; 25, 26; 25, 30; 26, call; 26, 27; 26, 28; 27, identifier:iscontroller; 28, argument_list; 28, 29; 29, identifier:obj; 30, block; 30, 31; 31, if_statement; 31, 32; 31, 40; 32, comparison_operator:is; 32, 33; 32, 39; 33, call; 33, 34; 33, 35; 34, identifier:getattr; 35, argument_list; 35, 36; 35, 37; 35, 38; 36, identifier:obj; 37, string:'custom_route'; 38, None; 39, None; 40, block; 40, 41; 41, return_statement; 41, 42; 42, expression_list; 42, 43; 42, 44; 43, identifier:obj; 44, identifier:remainder; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 48; 47, identifier:_detect_custom_path_segments; 48, argument_list; 48, 49; 49, identifier:obj; 50, if_statement; 50, 51; 50, 52; 51, identifier:remainder; 52, block; 52, 53; 52, 68; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:custom_route; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:__custom_routes__; 59, identifier:get; 60, argument_list; 60, 61; 61, tuple; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:obj; 64, identifier:__class__; 65, subscript; 65, 66; 65, 67; 66, identifier:remainder; 67, integer:0; 68, if_statement; 68, 69; 68, 70; 69, identifier:custom_route; 70, block; 70, 71; 71, return_statement; 71, 72; 72, expression_list; 72, 73; 72, 78; 73, call; 73, 74; 73, 75; 74, identifier:getattr; 75, argument_list; 75, 76; 75, 77; 76, identifier:obj; 77, identifier:custom_route; 78, subscript; 78, 79; 78, 80; 79, identifier:remainder; 80, slice; 80, 81; 80, 82; 81, integer:1; 82, colon; 83, comment; 84, expression_statement; 84, 85; 85, call; 85, 86; 85, 87; 86, identifier:cross_boundary; 87, argument_list; 87, 88; 87, 89; 88, identifier:prev_obj; 89, identifier:obj; 90, try_statement; 90, 91; 90, 130; 91, block; 91, 92; 91, 106; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 97; 94, pattern_list; 94, 95; 94, 96; 95, identifier:next_obj; 96, identifier:rest; 97, expression_list; 97, 98; 97, 101; 98, subscript; 98, 99; 98, 100; 99, identifier:remainder; 100, integer:0; 101, subscript; 101, 102; 101, 103; 102, identifier:remainder; 103, slice; 103, 104; 103, 105; 104, integer:1; 105, colon; 106, if_statement; 106, 107; 106, 110; 107, comparison_operator:==; 107, 108; 107, 109; 108, identifier:next_obj; 109, string:''; 110, block; 110, 111; 110, 120; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:index; 114, call; 114, 115; 114, 116; 115, identifier:getattr; 116, argument_list; 116, 117; 116, 118; 116, 119; 117, identifier:obj; 118, string:'index'; 119, None; 120, if_statement; 120, 121; 120, 125; 121, call; 121, 122; 121, 123; 122, identifier:iscontroller; 123, argument_list; 123, 124; 124, identifier:index; 125, block; 125, 126; 126, return_statement; 126, 127; 127, expression_list; 127, 128; 127, 129; 128, identifier:index; 129, identifier:rest; 130, except_clause; 130, 131; 130, 132; 130, 133; 131, identifier:IndexError; 132, comment; 133, block; 133, 134; 133, 143; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:index; 137, call; 137, 138; 137, 139; 138, identifier:getattr; 139, argument_list; 139, 140; 139, 141; 139, 142; 140, identifier:obj; 141, string:'index'; 142, None; 143, if_statement; 143, 144; 143, 148; 144, call; 144, 145; 144, 146; 145, identifier:iscontroller; 146, argument_list; 146, 147; 147, identifier:index; 148, block; 148, 149; 149, raise_statement; 149, 150; 150, call; 150, 151; 150, 152; 151, identifier:NonCanonicalPath; 152, argument_list; 152, 153; 152, 154; 153, identifier:index; 154, list:[]; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:default; 158, call; 158, 159; 158, 160; 159, identifier:getattr; 160, argument_list; 160, 161; 160, 162; 160, 163; 161, identifier:obj; 162, string:'_default'; 163, None; 164, if_statement; 164, 165; 164, 169; 165, call; 165, 166; 165, 167; 166, identifier:iscontroller; 167, argument_list; 167, 168; 168, identifier:default; 169, block; 169, 170; 170, expression_statement; 170, 171; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:notfound_handlers; 174, identifier:append; 175, argument_list; 175, 176; 176, tuple; 176, 177; 176, 178; 176, 179; 177, string:'_default'; 178, identifier:default; 179, identifier:remainder; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:lookup; 183, call; 183, 184; 183, 185; 184, identifier:getattr; 185, argument_list; 185, 186; 185, 187; 185, 188; 186, identifier:obj; 187, string:'_lookup'; 188, None; 189, if_statement; 189, 190; 189, 194; 190, call; 190, 191; 190, 192; 191, identifier:iscontroller; 192, argument_list; 192, 193; 193, identifier:lookup; 194, block; 194, 195; 195, expression_statement; 195, 196; 196, call; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:notfound_handlers; 199, identifier:append; 200, argument_list; 200, 201; 201, tuple; 201, 202; 201, 203; 201, 204; 202, string:'_lookup'; 203, identifier:lookup; 204, identifier:remainder; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:route; 208, call; 208, 209; 208, 210; 209, identifier:getattr; 210, argument_list; 210, 211; 210, 212; 210, 213; 211, identifier:obj; 212, string:'_route'; 213, None; 214, if_statement; 214, 215; 214, 219; 215, call; 215, 216; 215, 217; 216, identifier:iscontroller; 217, argument_list; 217, 218; 218, identifier:route; 219, block; 219, 220; 219, 278; 219, 284; 220, if_statement; 220, 221; 220, 232; 220, 266; 221, comparison_operator:==; 221, 222; 221, 231; 222, call; 222, 223; 222, 224; 223, identifier:len; 224, argument_list; 224, 225; 225, attribute; 225, 226; 225, 230; 226, call; 226, 227; 226, 228; 227, identifier:getargspec; 228, argument_list; 228, 229; 229, identifier:route; 230, identifier:args; 231, integer:2; 232, block; 232, 233; 232, 257; 233, expression_statement; 233, 234; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:warnings; 237, identifier:warn; 238, argument_list; 238, 239; 238, 256; 239, parenthesized_expression; 239, 240; 240, binary_operator:%; 240, 241; 240, 245; 241, concatenated_string; 241, 242; 241, 243; 241, 244; 242, string:"The function signature for %s.%s._route is changing "; 243, string:"in the next version of pecan.\nPlease update to: "; 244, string:"`def _route(self, args, request)`."; 245, tuple; 245, 246; 245, 251; 246, attribute; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:obj; 249, identifier:__class__; 250, identifier:__module__; 251, attribute; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:obj; 254, identifier:__class__; 255, identifier:__name__; 256, identifier:DeprecationWarning; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 262; 259, pattern_list; 259, 260; 259, 261; 260, identifier:next_obj; 261, identifier:next_remainder; 262, call; 262, 263; 262, 264; 263, identifier:route; 264, argument_list; 264, 265; 265, identifier:remainder; 266, else_clause; 266, 267; 267, block; 267, 268; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 273; 270, pattern_list; 270, 271; 270, 272; 271, identifier:next_obj; 272, identifier:next_remainder; 273, call; 273, 274; 273, 275; 274, identifier:route; 275, argument_list; 275, 276; 275, 277; 276, identifier:remainder; 277, identifier:request; 278, expression_statement; 278, 279; 279, call; 279, 280; 279, 281; 280, identifier:cross_boundary; 281, argument_list; 281, 282; 281, 283; 282, identifier:route; 283, identifier:next_obj; 284, return_statement; 284, 285; 285, expression_list; 285, 286; 285, 287; 286, identifier:next_obj; 287, identifier:next_remainder; 288, if_statement; 288, 289; 288, 291; 289, not_operator; 289, 290; 290, identifier:remainder; 291, block; 291, 292; 292, raise_statement; 292, 293; 293, identifier:PecanNotFound; 294, expression_statement; 294, 295; 295, assignment; 295, 296; 295, 297; 296, identifier:prev_remainder; 297, identifier:remainder; 298, expression_statement; 298, 299; 299, assignment; 299, 300; 299, 301; 300, identifier:prev_obj; 301, identifier:obj; 302, expression_statement; 302, 303; 303, assignment; 303, 304; 303, 305; 304, identifier:remainder; 305, identifier:rest; 306, try_statement; 306, 307; 306, 317; 307, block; 307, 308; 308, expression_statement; 308, 309; 309, assignment; 309, 310; 309, 311; 310, identifier:obj; 311, call; 311, 312; 311, 313; 312, identifier:getattr; 313, argument_list; 313, 314; 313, 315; 313, 316; 314, identifier:obj; 315, identifier:next_obj; 316, None; 317, except_clause; 317, 318; 317, 319; 318, identifier:UnicodeEncodeError; 319, block; 319, 320; 320, expression_statement; 320, 321; 321, assignment; 321, 322; 321, 323; 322, identifier:obj; 323, None; 324, comment; 325, comment; 326, comment; 327, comment; 328, if_statement; 328, 329; 328, 340; 329, boolean_operator:and; 329, 330; 329, 335; 330, boolean_operator:and; 330, 331; 330, 333; 331, not_operator; 331, 332; 332, identifier:obj; 333, not_operator; 333, 334; 334, identifier:notfound_handlers; 335, call; 335, 336; 335, 337; 336, identifier:hasattr; 337, argument_list; 337, 338; 337, 339; 338, identifier:prev_obj; 339, string:'index'; 340, block; 340, 341; 341, if_statement; 341, 342; 341, 358; 342, comparison_operator:in; 342, 343; 342, 346; 343, attribute; 343, 344; 343, 345; 344, identifier:request; 345, identifier:method; 346, call; 346, 347; 346, 355; 347, attribute; 347, 348; 347, 354; 348, call; 348, 349; 348, 350; 349, identifier:_cfg; 350, argument_list; 350, 351; 351, attribute; 351, 352; 351, 353; 352, identifier:prev_obj; 353, identifier:index; 354, identifier:get; 355, argument_list; 355, 356; 355, 357; 356, string:'generic_handlers'; 357, dictionary; 358, block; 358, 359; 359, return_statement; 359, 360; 360, expression_list; 360, 361; 360, 364; 361, attribute; 361, 362; 361, 363; 362, identifier:prev_obj; 363, identifier:index; 364, identifier:prev_remainder
def find_object(obj, remainder, notfound_handlers, request): ''' 'Walks' the url path in search of an action for which a controller is implemented and returns that controller object along with what's left of the remainder. ''' prev_obj = None while True: if obj is None: raise PecanNotFound if iscontroller(obj): if getattr(obj, 'custom_route', None) is None: return obj, remainder _detect_custom_path_segments(obj) if remainder: custom_route = __custom_routes__.get((obj.__class__, remainder[0])) if custom_route: return getattr(obj, custom_route), remainder[1:] # are we traversing to another controller cross_boundary(prev_obj, obj) try: next_obj, rest = remainder[0], remainder[1:] if next_obj == '': index = getattr(obj, 'index', None) if iscontroller(index): return index, rest except IndexError: # the URL has hit an index method without a trailing slash index = getattr(obj, 'index', None) if iscontroller(index): raise NonCanonicalPath(index, []) default = getattr(obj, '_default', None) if iscontroller(default): notfound_handlers.append(('_default', default, remainder)) lookup = getattr(obj, '_lookup', None) if iscontroller(lookup): notfound_handlers.append(('_lookup', lookup, remainder)) route = getattr(obj, '_route', None) if iscontroller(route): if len(getargspec(route).args) == 2: warnings.warn( ( "The function signature for %s.%s._route is changing " "in the next version of pecan.\nPlease update to: " "`def _route(self, args, request)`." % ( obj.__class__.__module__, obj.__class__.__name__ ) ), DeprecationWarning ) next_obj, next_remainder = route(remainder) else: next_obj, next_remainder = route(remainder, request) cross_boundary(route, next_obj) return next_obj, next_remainder if not remainder: raise PecanNotFound prev_remainder = remainder prev_obj = obj remainder = rest try: obj = getattr(obj, next_obj, None) except UnicodeEncodeError: obj = None # Last-ditch effort: if there's not a matching subcontroller, no # `_default`, no `_lookup`, and no `_route`, look to see if there's # an `index` that has a generic method defined for the current request # method. if not obj and not notfound_handlers and hasattr(prev_obj, 'index'): if request.method in _cfg(prev_obj.index).get('generic_handlers', {}): return prev_obj.index, prev_remainder
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:make_app; 3, parameters; 3, 4; 3, 5; 4, identifier:root; 5, dictionary_splat_pattern; 5, 6; 6, identifier:kw; 7, block; 7, 8; 7, 10; 7, 11; 7, 21; 7, 31; 7, 98; 7, 99; 7, 108; 7, 109; 7, 119; 7, 129; 7, 130; 7, 147; 7, 162; 7, 163; 7, 174; 7, 175; 7, 185; 7, 263; 7, 287; 8, expression_statement; 8, 9; 9, string:''' Utility for creating the Pecan application object. This function should generally be called from the ``setup_app`` function in your project's ``app.py`` file. :param root: A string representing a root controller object (e.g., "myapp.controller.root.RootController") :param static_root: The relative path to a directory containing static files. Serving static files is only enabled when debug mode is set. :param debug: A flag to enable debug mode. This enables the debug middleware and serving static files. :param wrap_app: A function or middleware class to wrap the Pecan app. This must either be a wsgi middleware class or a function that returns a wsgi application. This wrapper is applied first before wrapping the application in other middlewares such as Pecan's debug middleware. This should be used if you want to use middleware to perform authentication or intercept all requests before they are routed to the root controller. :param logging: A dictionary used to configure logging. This uses ``logging.config.dictConfig``. All other keyword arguments are passed in to the Pecan app constructor. :returns: a ``Pecan`` object. '''; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:logging; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:kw; 17, identifier:get; 18, argument_list; 18, 19; 18, 20; 19, string:'logging'; 20, dictionary; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:debug; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:kw; 27, identifier:get; 28, argument_list; 28, 29; 28, 30; 29, string:'debug'; 30, False; 31, if_statement; 31, 32; 31, 33; 32, identifier:logging; 33, block; 33, 34; 33, 67; 33, 82; 33, 93; 34, if_statement; 34, 35; 34, 36; 35, identifier:debug; 36, block; 36, 37; 37, try_statement; 37, 38; 37, 39; 37, 40; 37, 41; 37, 42; 37, 43; 37, 62; 38, comment; 39, comment; 40, comment; 41, comment; 42, comment; 43, block; 43, 44; 43, 49; 43, 54; 44, import_from_statement; 44, 45; 44, 47; 45, dotted_name; 45, 46; 46, identifier:logging; 47, dotted_name; 47, 48; 48, identifier:captureWarnings; 49, expression_statement; 49, 50; 50, call; 50, 51; 50, 52; 51, identifier:captureWarnings; 52, argument_list; 52, 53; 53, True; 54, expression_statement; 54, 55; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:warnings; 58, identifier:simplefilter; 59, argument_list; 59, 60; 59, 61; 60, string:"default"; 61, identifier:DeprecationWarning; 62, except_clause; 62, 63; 62, 64; 62, 65; 63, identifier:ImportError; 64, comment; 65, block; 65, 66; 66, pass_statement; 67, if_statement; 67, 68; 67, 73; 68, call; 68, 69; 68, 70; 69, identifier:isinstance; 70, argument_list; 70, 71; 70, 72; 71, identifier:logging; 72, identifier:Config; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:logging; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:logging; 80, identifier:to_dict; 81, argument_list; 82, if_statement; 82, 83; 82, 86; 83, comparison_operator:not; 83, 84; 83, 85; 84, string:'version'; 85, identifier:logging; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 92; 89, subscript; 89, 90; 89, 91; 90, identifier:logging; 91, string:'version'; 92, integer:1; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 96; 95, identifier:load_logging_config; 96, argument_list; 96, 97; 97, identifier:logging; 98, comment; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:app; 102, call; 102, 103; 102, 104; 103, identifier:Pecan; 104, argument_list; 104, 105; 104, 106; 105, identifier:root; 106, dictionary_splat; 106, 107; 107, identifier:kw; 108, comment; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:wrap_app; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:kw; 115, identifier:get; 116, argument_list; 116, 117; 116, 118; 117, string:'wrap_app'; 118, None; 119, if_statement; 119, 120; 119, 121; 120, identifier:wrap_app; 121, block; 121, 122; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:app; 125, call; 125, 126; 125, 127; 126, identifier:wrap_app; 127, argument_list; 127, 128; 128, identifier:app; 129, comment; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:errors; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:kw; 136, identifier:get; 137, argument_list; 137, 138; 137, 139; 138, string:'errors'; 139, call; 139, 140; 139, 141; 140, identifier:getattr; 141, argument_list; 141, 142; 141, 145; 141, 146; 142, attribute; 142, 143; 142, 144; 143, identifier:conf; 144, identifier:app; 145, string:'errors'; 146, dictionary; 147, if_statement; 147, 148; 147, 149; 148, identifier:errors; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:app; 153, call; 153, 154; 153, 159; 154, attribute; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:middleware; 157, identifier:errordocument; 158, identifier:ErrorDocumentMiddleware; 159, argument_list; 159, 160; 159, 161; 160, identifier:app; 161, identifier:errors; 162, comment; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:app; 166, call; 166, 167; 166, 172; 167, attribute; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:middleware; 170, identifier:recursive; 171, identifier:RecursiveMiddleware; 172, argument_list; 172, 173; 173, identifier:app; 174, comment; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:static_root; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:kw; 181, identifier:get; 182, argument_list; 182, 183; 182, 184; 183, string:'static_root'; 184, None; 185, if_statement; 185, 186; 185, 187; 185, 252; 186, identifier:debug; 187, block; 187, 188; 187, 197; 187, 227; 187, 236; 187, 237; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:debug_kwargs; 191, call; 191, 192; 191, 193; 192, identifier:getattr; 193, argument_list; 193, 194; 193, 195; 193, 196; 194, identifier:conf; 195, string:'debug'; 196, dictionary; 197, expression_statement; 197, 198; 198, call; 198, 199; 198, 208; 199, attribute; 199, 200; 199, 207; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:debug_kwargs; 203, identifier:setdefault; 204, argument_list; 204, 205; 204, 206; 205, string:'context_injectors'; 206, list:[]; 207, identifier:append; 208, argument_list; 208, 209; 209, lambda; 209, 210; 209, 212; 210, lambda_parameters; 210, 211; 211, identifier:environ; 212, dictionary; 212, 213; 213, pair; 213, 214; 213, 215; 214, string:'request'; 215, call; 215, 216; 215, 225; 216, attribute; 216, 217; 216, 224; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:environ; 220, identifier:get; 221, argument_list; 221, 222; 221, 223; 222, string:'pecan.locals'; 223, dictionary; 224, identifier:get; 225, argument_list; 225, 226; 226, string:'request'; 227, expression_statement; 227, 228; 228, assignment; 228, 229; 228, 230; 229, identifier:app; 230, call; 230, 231; 230, 232; 231, identifier:DebugMiddleware; 232, argument_list; 232, 233; 232, 234; 233, identifier:app; 234, dictionary_splat; 234, 235; 235, identifier:debug_kwargs; 236, comment; 237, if_statement; 237, 238; 237, 239; 238, identifier:static_root; 239, block; 239, 240; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:app; 243, call; 243, 244; 243, 249; 244, attribute; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:middleware; 247, identifier:static; 248, identifier:StaticFileMiddleware; 249, argument_list; 249, 250; 249, 251; 250, identifier:app; 251, identifier:static_root; 252, elif_clause; 252, 253; 252, 254; 253, identifier:static_root; 254, block; 254, 255; 255, expression_statement; 255, 256; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:warnings; 259, identifier:warn; 260, argument_list; 260, 261; 260, 262; 261, string:"`static_root` is only used when `debug` is True, ignoring"; 262, identifier:RuntimeWarning; 263, if_statement; 263, 264; 263, 269; 264, call; 264, 265; 264, 266; 265, identifier:hasattr; 266, argument_list; 266, 267; 266, 268; 267, identifier:conf; 268, string:'requestviewer'; 269, block; 269, 270; 270, expression_statement; 270, 271; 271, call; 271, 272; 271, 275; 272, attribute; 272, 273; 272, 274; 273, identifier:warnings; 274, identifier:warn; 275, argument_list; 275, 276; 275, 286; 276, call; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, string:''; 279, identifier:join; 280, argument_list; 280, 281; 281, list:[ "`pecan.conf.requestviewer` is deprecated. To apply the ", "`RequestViewerHook` to your application, add it to ", "`pecan.conf.app.hooks` or manually in your project's `app.py` ", "file."]; 281, 282; 281, 283; 281, 284; 281, 285; 282, string:"`pecan.conf.requestviewer` is deprecated. To apply the "; 283, string:"`RequestViewerHook` to your application, add it to "; 284, string:"`pecan.conf.app.hooks` or manually in your project's `app.py` "; 285, string:"file."; 286, identifier:DeprecationWarning; 287, return_statement; 287, 288; 288, identifier:app
def make_app(root, **kw): ''' Utility for creating the Pecan application object. This function should generally be called from the ``setup_app`` function in your project's ``app.py`` file. :param root: A string representing a root controller object (e.g., "myapp.controller.root.RootController") :param static_root: The relative path to a directory containing static files. Serving static files is only enabled when debug mode is set. :param debug: A flag to enable debug mode. This enables the debug middleware and serving static files. :param wrap_app: A function or middleware class to wrap the Pecan app. This must either be a wsgi middleware class or a function that returns a wsgi application. This wrapper is applied first before wrapping the application in other middlewares such as Pecan's debug middleware. This should be used if you want to use middleware to perform authentication or intercept all requests before they are routed to the root controller. :param logging: A dictionary used to configure logging. This uses ``logging.config.dictConfig``. All other keyword arguments are passed in to the Pecan app constructor. :returns: a ``Pecan`` object. ''' # Pass logging configuration (if it exists) on to the Python logging module logging = kw.get('logging', {}) debug = kw.get('debug', False) if logging: if debug: try: # # By default, Python 2.7+ silences DeprecationWarnings. # However, if conf.app.debug is True, we should probably ensure # that users see these types of warnings. # from logging import captureWarnings captureWarnings(True) warnings.simplefilter("default", DeprecationWarning) except ImportError: # No captureWarnings on Python 2.6, DeprecationWarnings are on pass if isinstance(logging, Config): logging = logging.to_dict() if 'version' not in logging: logging['version'] = 1 load_logging_config(logging) # Instantiate the WSGI app by passing **kw onward app = Pecan(root, **kw) # Optionally wrap the app in another WSGI app wrap_app = kw.get('wrap_app', None) if wrap_app: app = wrap_app(app) # Configuration for serving custom error messages errors = kw.get('errors', getattr(conf.app, 'errors', {})) if errors: app = middleware.errordocument.ErrorDocumentMiddleware(app, errors) # Included for internal redirect support app = middleware.recursive.RecursiveMiddleware(app) # When in debug mode, load exception debugging middleware static_root = kw.get('static_root', None) if debug: debug_kwargs = getattr(conf, 'debug', {}) debug_kwargs.setdefault('context_injectors', []).append( lambda environ: { 'request': environ.get('pecan.locals', {}).get('request') } ) app = DebugMiddleware( app, **debug_kwargs ) # Support for serving static files (for development convenience) if static_root: app = middleware.static.StaticFileMiddleware(app, static_root) elif static_root: warnings.warn( "`static_root` is only used when `debug` is True, ignoring", RuntimeWarning ) if hasattr(conf, 'requestviewer'): warnings.warn(''.join([ "`pecan.conf.requestviewer` is deprecated. To apply the ", "`RequestViewerHook` to your application, add it to ", "`pecan.conf.app.hooks` or manually in your project's `app.py` ", "file."]), DeprecationWarning ) return app
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 22; 2, function_name:redirect; 3, parameters; 3, 4; 3, 7; 3, 10; 3, 13; 3, 16; 3, 19; 4, default_parameter; 4, 5; 4, 6; 5, identifier:location; 6, None; 7, default_parameter; 7, 8; 7, 9; 8, identifier:internal; 9, False; 10, default_parameter; 10, 11; 10, 12; 11, identifier:code; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:headers; 15, dictionary; 16, default_parameter; 16, 17; 16, 18; 17, identifier:add_slash; 18, False; 19, default_parameter; 19, 20; 19, 21; 20, identifier:request; 21, None; 22, block; 22, 23; 22, 25; 22, 33; 22, 110; 22, 118; 22, 146; 22, 155; 23, expression_statement; 23, 24; 24, string:''' Perform a redirect, either internal or external. An internal redirect performs the redirect server-side, while the external redirect utilizes an HTTP 302 status code. :param location: The HTTP location to redirect to. :param internal: A boolean indicating whether the redirect should be internal. :param code: The HTTP status code to use for the redirect. Defaults to 302. :param headers: Any HTTP headers to send with the response, as a dictionary. :param request: The :class:`pecan.Request` instance to use. '''; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:request; 28, boolean_operator:or; 28, 29; 28, 30; 29, identifier:request; 30, attribute; 30, 31; 30, 32; 31, identifier:state; 32, identifier:request; 33, if_statement; 33, 34; 33, 35; 34, identifier:add_slash; 35, block; 35, 36; 35, 86; 35, 101; 36, if_statement; 36, 37; 36, 40; 36, 75; 37, comparison_operator:is; 37, 38; 37, 39; 38, identifier:location; 39, None; 40, block; 40, 41; 40, 55; 40, 69; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:split_url; 44, call; 44, 45; 44, 46; 45, identifier:list; 46, argument_list; 46, 47; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:urlparse; 50, identifier:urlsplit; 51, argument_list; 51, 52; 52, attribute; 52, 53; 52, 54; 53, identifier:request; 54, identifier:url; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:new_proto; 58, call; 58, 59; 58, 64; 59, attribute; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:request; 62, identifier:environ; 63, identifier:get; 64, argument_list; 64, 65; 64, 66; 65, string:'HTTP_X_FORWARDED_PROTO'; 66, subscript; 66, 67; 66, 68; 67, identifier:split_url; 68, integer:0; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 74; 71, subscript; 71, 72; 71, 73; 72, identifier:split_url; 73, integer:0; 74, identifier:new_proto; 75, else_clause; 75, 76; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:split_url; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:urlparse; 83, identifier:urlsplit; 84, argument_list; 84, 85; 85, identifier:location; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 91; 88, subscript; 88, 89; 88, 90; 89, identifier:split_url; 90, integer:2; 91, binary_operator:+; 91, 92; 91, 100; 92, call; 92, 93; 92, 98; 93, attribute; 93, 94; 93, 97; 94, subscript; 94, 95; 94, 96; 95, identifier:split_url; 96, integer:2; 97, identifier:rstrip; 98, argument_list; 98, 99; 99, string:'/'; 100, string:'/'; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:location; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:urlparse; 107, identifier:urlunsplit; 108, argument_list; 108, 109; 109, identifier:split_url; 110, if_statement; 110, 111; 110, 113; 111, not_operator; 111, 112; 112, identifier:headers; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:headers; 117, dictionary; 118, if_statement; 118, 119; 118, 120; 119, identifier:internal; 120, block; 120, 121; 120, 131; 120, 141; 121, if_statement; 121, 122; 121, 125; 122, comparison_operator:is; 122, 123; 122, 124; 123, identifier:code; 124, None; 125, block; 125, 126; 126, raise_statement; 126, 127; 127, call; 127, 128; 127, 129; 128, identifier:ValueError; 129, argument_list; 129, 130; 130, string:'Cannot specify a code for internal redirects'; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 138; 133, subscript; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:request; 136, identifier:environ; 137, string:'pecan.recursive.context'; 138, attribute; 138, 139; 138, 140; 139, identifier:request; 140, identifier:context; 141, raise_statement; 141, 142; 142, call; 142, 143; 142, 144; 143, identifier:ForwardRequestException; 144, argument_list; 144, 145; 145, identifier:location; 146, if_statement; 146, 147; 146, 150; 147, comparison_operator:is; 147, 148; 147, 149; 148, identifier:code; 149, None; 150, block; 150, 151; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:code; 154, integer:302; 155, raise_statement; 155, 156; 156, call; 156, 157; 156, 162; 157, subscript; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:exc; 160, identifier:status_map; 161, identifier:code; 162, argument_list; 162, 163; 162, 166; 163, keyword_argument; 163, 164; 163, 165; 164, identifier:location; 165, identifier:location; 166, keyword_argument; 166, 167; 166, 168; 167, identifier:headers; 168, identifier:headers
def redirect(location=None, internal=False, code=None, headers={}, add_slash=False, request=None): ''' Perform a redirect, either internal or external. An internal redirect performs the redirect server-side, while the external redirect utilizes an HTTP 302 status code. :param location: The HTTP location to redirect to. :param internal: A boolean indicating whether the redirect should be internal. :param code: The HTTP status code to use for the redirect. Defaults to 302. :param headers: Any HTTP headers to send with the response, as a dictionary. :param request: The :class:`pecan.Request` instance to use. ''' request = request or state.request if add_slash: if location is None: split_url = list(urlparse.urlsplit(request.url)) new_proto = request.environ.get( 'HTTP_X_FORWARDED_PROTO', split_url[0] ) split_url[0] = new_proto else: split_url = urlparse.urlsplit(location) split_url[2] = split_url[2].rstrip('/') + '/' location = urlparse.urlunsplit(split_url) if not headers: headers = {} if internal: if code is not None: raise ValueError('Cannot specify a code for internal redirects') request.environ['pecan.recursive.context'] = request.context raise ForwardRequestException(location) if code is None: code = 302 raise exc.status_map[code](location=location, headers=headers)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:get_args; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 4, identifier:self; 5, identifier:state; 6, identifier:all_params; 7, identifier:remainder; 8, identifier:argspec; 9, identifier:im_self; 10, block; 10, 11; 10, 13; 10, 17; 10, 21; 10, 27; 10, 36; 10, 54; 10, 62; 10, 72; 10, 84; 10, 85; 10, 105; 10, 106; 10, 147; 10, 148; 10, 175; 10, 176; 10, 214; 10, 215; 10, 253; 10, 254; 10, 283; 11, expression_statement; 11, 12; 12, string:''' Determines the arguments for a controller based upon parameters passed the argument specification for the controller. '''; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:args; 16, list:[]; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:varargs; 20, list:[]; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:kwargs; 24, call; 24, 25; 24, 26; 25, identifier:dict; 26, argument_list; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:valid_args; 30, subscript; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:argspec; 33, identifier:args; 34, slice; 34, 35; 35, colon; 36, if_statement; 36, 37; 36, 45; 37, boolean_operator:or; 37, 38; 37, 44; 38, call; 38, 39; 38, 40; 39, identifier:ismethod; 40, argument_list; 40, 41; 41, attribute; 41, 42; 41, 43; 42, identifier:state; 43, identifier:controller; 44, identifier:im_self; 45, block; 45, 46; 45, 53; 46, expression_statement; 46, 47; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:valid_args; 50, identifier:pop; 51, argument_list; 51, 52; 52, integer:0; 53, comment; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:pecan_state; 57, attribute; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:state; 60, identifier:request; 61, identifier:pecan; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:remainder; 65, list_comprehension; 65, 66; 65, 67; 65, 70; 66, identifier:x; 67, for_in_clause; 67, 68; 67, 69; 68, identifier:x; 69, identifier:remainder; 70, if_clause; 70, 71; 71, identifier:x; 72, if_statement; 72, 73; 72, 76; 73, comparison_operator:is; 73, 74; 73, 75; 74, identifier:im_self; 75, None; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:args; 81, identifier:append; 82, argument_list; 82, 83; 83, identifier:im_self; 84, comment; 85, if_statement; 85, 86; 85, 89; 86, comparison_operator:in; 86, 87; 86, 88; 87, string:'routing_args'; 88, identifier:pecan_state; 89, block; 89, 90; 89, 101; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:remainder; 93, binary_operator:+; 93, 94; 93, 97; 94, subscript; 94, 95; 94, 96; 95, identifier:pecan_state; 96, string:'routing_args'; 97, call; 97, 98; 97, 99; 98, identifier:list; 99, argument_list; 99, 100; 100, identifier:remainder; 101, delete_statement; 101, 102; 102, subscript; 102, 103; 102, 104; 103, identifier:pecan_state; 104, string:'routing_args'; 105, comment; 106, if_statement; 106, 107; 106, 110; 107, boolean_operator:and; 107, 108; 107, 109; 108, identifier:valid_args; 109, identifier:remainder; 110, block; 110, 111; 110, 125; 110, 136; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:args; 115, identifier:extend; 116, argument_list; 116, 117; 117, subscript; 117, 118; 117, 119; 118, identifier:remainder; 119, slice; 119, 120; 119, 121; 120, colon; 121, call; 121, 122; 121, 123; 122, identifier:len; 123, argument_list; 123, 124; 124, identifier:valid_args; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:remainder; 128, subscript; 128, 129; 128, 130; 129, identifier:remainder; 130, slice; 130, 131; 130, 135; 131, call; 131, 132; 131, 133; 132, identifier:len; 133, argument_list; 133, 134; 134, identifier:valid_args; 135, colon; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:valid_args; 139, subscript; 139, 140; 139, 141; 140, identifier:valid_args; 141, slice; 141, 142; 141, 146; 142, call; 142, 143; 142, 144; 143, identifier:len; 144, argument_list; 144, 145; 145, identifier:args; 146, colon; 147, comment; 148, if_statement; 148, 149; 148, 156; 149, list_comprehension; 149, 150; 149, 151; 149, 154; 150, identifier:i; 151, for_in_clause; 151, 152; 151, 153; 152, identifier:i; 153, identifier:remainder; 154, if_clause; 154, 155; 155, identifier:i; 156, block; 156, 157; 156, 168; 157, if_statement; 157, 158; 157, 162; 158, not_operator; 158, 159; 159, subscript; 159, 160; 159, 161; 160, identifier:argspec; 161, integer:1; 162, block; 162, 163; 163, expression_statement; 163, 164; 164, call; 164, 165; 164, 166; 165, identifier:abort; 166, argument_list; 166, 167; 167, integer:404; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:varargs; 172, identifier:extend; 173, argument_list; 173, 174; 174, identifier:remainder; 175, comment; 176, if_statement; 176, 177; 176, 180; 176, 206; 177, subscript; 177, 178; 177, 179; 178, identifier:argspec; 179, integer:3; 180, block; 180, 181; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:defaults; 184, call; 184, 185; 184, 186; 185, identifier:dict; 186, argument_list; 186, 187; 187, call; 187, 188; 187, 189; 188, identifier:izip; 189, argument_list; 189, 190; 189, 203; 190, subscript; 190, 191; 190, 194; 191, subscript; 191, 192; 191, 193; 192, identifier:argspec; 193, integer:0; 194, slice; 194, 195; 194, 202; 195, unary_operator:-; 195, 196; 196, call; 196, 197; 196, 198; 197, identifier:len; 198, argument_list; 198, 199; 199, subscript; 199, 200; 199, 201; 200, identifier:argspec; 201, integer:3; 202, colon; 203, subscript; 203, 204; 203, 205; 204, identifier:argspec; 205, integer:3; 206, else_clause; 206, 207; 207, block; 207, 208; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 211; 210, identifier:defaults; 211, call; 211, 212; 211, 213; 212, identifier:dict; 213, argument_list; 214, comment; 215, for_statement; 215, 216; 215, 217; 215, 218; 216, identifier:name; 217, identifier:valid_args; 218, block; 218, 219; 219, if_statement; 219, 220; 219, 223; 219, 236; 219, 250; 220, comparison_operator:in; 220, 221; 220, 222; 221, identifier:name; 222, identifier:all_params; 223, block; 223, 224; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:args; 228, identifier:append; 229, argument_list; 229, 230; 230, call; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:all_params; 233, identifier:pop; 234, argument_list; 234, 235; 235, identifier:name; 236, elif_clause; 236, 237; 236, 240; 237, comparison_operator:in; 237, 238; 237, 239; 238, identifier:name; 239, identifier:defaults; 240, block; 240, 241; 241, expression_statement; 241, 242; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:args; 245, identifier:append; 246, argument_list; 246, 247; 247, subscript; 247, 248; 247, 249; 248, identifier:defaults; 249, identifier:name; 250, else_clause; 250, 251; 251, block; 251, 252; 252, break_statement; 253, comment; 254, if_statement; 254, 255; 254, 258; 255, subscript; 255, 256; 255, 257; 256, identifier:argspec; 257, integer:2; 258, block; 258, 259; 259, for_statement; 259, 260; 259, 263; 259, 269; 260, pattern_list; 260, 261; 260, 262; 261, identifier:name; 262, identifier:value; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:six; 266, identifier:iteritems; 267, argument_list; 267, 268; 268, identifier:all_params; 269, block; 269, 270; 270, if_statement; 270, 271; 270, 276; 271, comparison_operator:not; 271, 272; 271, 273; 272, identifier:name; 273, subscript; 273, 274; 273, 275; 274, identifier:argspec; 275, integer:0; 276, block; 276, 277; 277, expression_statement; 277, 278; 278, assignment; 278, 279; 278, 282; 279, subscript; 279, 280; 279, 281; 280, identifier:kwargs; 281, identifier:name; 282, identifier:value; 283, return_statement; 283, 284; 284, expression_list; 284, 285; 284, 286; 284, 287; 285, identifier:args; 286, identifier:varargs; 287, identifier:kwargs
def get_args(self, state, all_params, remainder, argspec, im_self): ''' Determines the arguments for a controller based upon parameters passed the argument specification for the controller. ''' args = [] varargs = [] kwargs = dict() valid_args = argspec.args[:] if ismethod(state.controller) or im_self: valid_args.pop(0) # pop off `self` pecan_state = state.request.pecan remainder = [x for x in remainder if x] if im_self is not None: args.append(im_self) # grab the routing args from nested REST controllers if 'routing_args' in pecan_state: remainder = pecan_state['routing_args'] + list(remainder) del pecan_state['routing_args'] # handle positional arguments if valid_args and remainder: args.extend(remainder[:len(valid_args)]) remainder = remainder[len(valid_args):] valid_args = valid_args[len(args):] # handle wildcard arguments if [i for i in remainder if i]: if not argspec[1]: abort(404) varargs.extend(remainder) # get the default positional arguments if argspec[3]: defaults = dict(izip(argspec[0][-len(argspec[3]):], argspec[3])) else: defaults = dict() # handle positional GET/POST params for name in valid_args: if name in all_params: args.append(all_params.pop(name)) elif name in defaults: args.append(defaults[name]) else: break # handle wildcard GET/POST params if argspec[2]: for name, value in six.iteritems(all_params): if name not in argspec[0]: kwargs[name] = value return args, varargs, kwargs
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:_route; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:args; 6, default_parameter; 6, 7; 6, 8; 7, identifier:request; 8, None; 9, block; 9, 10; 9, 12; 9, 22; 9, 23; 9, 41; 9, 42; 9, 60; 9, 61; 9, 71; 9, 76; 9, 77; 9, 90; 9, 281; 9, 282; 10, expression_statement; 10, 11; 11, string:''' Routes a request to the appropriate controller and returns its result. Performs a bit of validation - refuses to route delete and put actions via a GET request). '''; 12, if_statement; 12, 13; 12, 16; 13, comparison_operator:is; 13, 14; 13, 15; 14, identifier:request; 15, None; 16, block; 16, 17; 17, import_from_statement; 17, 18; 17, 20; 18, dotted_name; 18, 19; 19, identifier:pecan; 20, dotted_name; 20, 21; 21, identifier:request; 22, comment; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:method; 26, call; 26, 27; 26, 40; 27, attribute; 27, 28; 27, 39; 28, call; 28, 29; 28, 34; 29, attribute; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:request; 32, identifier:params; 33, identifier:get; 34, argument_list; 34, 35; 34, 36; 35, string:'_method'; 36, attribute; 36, 37; 36, 38; 37, identifier:request; 38, identifier:method; 39, identifier:lower; 40, argument_list; 41, comment; 42, if_statement; 42, 43; 42, 54; 43, boolean_operator:and; 43, 44; 43, 49; 44, comparison_operator:==; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:request; 47, identifier:method; 48, string:'GET'; 49, comparison_operator:in; 49, 50; 49, 51; 50, identifier:method; 51, tuple; 51, 52; 51, 53; 52, string:'delete'; 53, string:'put'; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, call; 56, 57; 56, 58; 57, identifier:abort; 58, argument_list; 58, 59; 59, integer:405; 60, comment; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:result; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:self; 67, identifier:_find_sub_controllers; 68, argument_list; 68, 69; 68, 70; 69, identifier:args; 70, identifier:request; 71, if_statement; 71, 72; 71, 73; 72, identifier:result; 73, block; 73, 74; 74, return_statement; 74, 75; 75, identifier:result; 76, comment; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:handler; 80, call; 80, 81; 80, 82; 81, identifier:getattr; 82, argument_list; 82, 83; 82, 84; 82, 87; 83, identifier:self; 84, binary_operator:%; 84, 85; 84, 86; 85, string:'_handle_%s'; 86, identifier:method; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:_handle_unknown_method; 90, try_statement; 90, 91; 90, 170; 91, block; 91, 92; 91, 124; 91, 125; 91, 126; 91, 127; 91, 128; 91, 129; 91, 140; 91, 147; 92, if_statement; 92, 93; 92, 104; 92, 113; 93, comparison_operator:==; 93, 94; 93, 103; 94, call; 94, 95; 94, 96; 95, identifier:len; 96, argument_list; 96, 97; 97, attribute; 97, 98; 97, 102; 98, call; 98, 99; 98, 100; 99, identifier:getargspec; 100, argument_list; 100, 101; 101, identifier:handler; 102, identifier:args; 103, integer:3; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:result; 108, call; 108, 109; 108, 110; 109, identifier:handler; 110, argument_list; 110, 111; 110, 112; 111, identifier:method; 112, identifier:args; 113, else_clause; 113, 114; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:result; 118, call; 118, 119; 118, 120; 119, identifier:handler; 120, argument_list; 120, 121; 120, 122; 120, 123; 121, identifier:method; 122, identifier:args; 123, identifier:request; 124, comment; 125, comment; 126, comment; 127, comment; 128, comment; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:argspec; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:self; 135, identifier:_get_args_for_controller; 136, argument_list; 136, 137; 137, subscript; 137, 138; 137, 139; 138, identifier:result; 139, integer:0; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:num_args; 143, call; 143, 144; 143, 145; 144, identifier:len; 145, argument_list; 145, 146; 146, identifier:argspec; 147, if_statement; 147, 148; 147, 154; 148, comparison_operator:<; 148, 149; 148, 150; 149, identifier:num_args; 150, call; 150, 151; 150, 152; 151, identifier:len; 152, argument_list; 152, 153; 153, identifier:args; 154, block; 154, 155; 154, 165; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:_lookup_result; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:self; 161, identifier:_handle_lookup; 162, argument_list; 162, 163; 162, 164; 163, identifier:args; 164, identifier:request; 165, if_statement; 165, 166; 165, 167; 166, identifier:_lookup_result; 167, block; 167, 168; 168, return_statement; 168, 169; 169, identifier:_lookup_result; 170, except_clause; 170, 171; 170, 184; 170, 185; 170, 186; 170, 187; 170, 188; 171, as_pattern; 171, 172; 171, 182; 172, tuple; 172, 173; 172, 176; 172, 179; 173, attribute; 173, 174; 173, 175; 174, identifier:exc; 175, identifier:HTTPClientError; 176, attribute; 176, 177; 176, 178; 177, identifier:exc; 178, identifier:HTTPNotFound; 179, attribute; 179, 180; 179, 181; 180, identifier:exc; 181, identifier:HTTPMethodNotAllowed; 182, as_pattern_target; 182, 183; 183, identifier:e; 184, comment; 185, comment; 186, comment; 187, comment; 188, block; 188, 189; 188, 199; 188, 204; 188, 205; 188, 280; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:_lookup_result; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:self; 195, identifier:_handle_lookup; 196, argument_list; 196, 197; 196, 198; 197, identifier:args; 198, identifier:request; 199, if_statement; 199, 200; 199, 201; 200, identifier:_lookup_result; 201, block; 201, 202; 202, return_statement; 202, 203; 203, identifier:_lookup_result; 204, comment; 205, if_statement; 205, 206; 205, 213; 206, call; 206, 207; 206, 208; 207, identifier:isinstance; 208, argument_list; 208, 209; 208, 210; 209, identifier:e; 210, attribute; 210, 211; 210, 212; 211, identifier:exc; 212, identifier:HTTPMethodNotAllowed; 213, block; 213, 214; 213, 269; 214, function_definition; 214, 215; 214, 216; 214, 217; 215, function_name:method_iter; 216, parameters; 217, block; 217, 218; 217, 240; 218, for_statement; 218, 219; 218, 220; 218, 227; 219, identifier:func; 220, tuple; 220, 221; 220, 222; 220, 223; 220, 224; 220, 225; 220, 226; 221, string:'get'; 222, string:'get_one'; 223, string:'get_all'; 224, string:'new'; 225, string:'edit'; 226, string:'get_delete'; 227, block; 227, 228; 228, if_statement; 228, 229; 228, 235; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:self; 232, identifier:_find_controller; 233, argument_list; 233, 234; 234, identifier:func; 235, block; 235, 236; 235, 239; 236, expression_statement; 236, 237; 237, yield; 237, 238; 238, string:'GET'; 239, break_statement; 240, for_statement; 240, 241; 240, 242; 240, 249; 241, identifier:method; 242, tuple; 242, 243; 242, 244; 242, 245; 242, 246; 242, 247; 242, 248; 243, string:'HEAD'; 244, string:'POST'; 245, string:'PUT'; 246, string:'DELETE'; 247, string:'TRACE'; 248, string:'PATCH'; 249, block; 249, 250; 249, 258; 250, expression_statement; 250, 251; 251, assignment; 251, 252; 251, 253; 252, identifier:func; 253, call; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:method; 256, identifier:lower; 257, argument_list; 258, if_statement; 258, 259; 258, 265; 259, call; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:self; 262, identifier:_find_controller; 263, argument_list; 263, 264; 264, identifier:func; 265, block; 265, 266; 266, expression_statement; 266, 267; 267, yield; 267, 268; 268, identifier:method; 269, expression_statement; 269, 270; 270, assignment; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:e; 273, identifier:allow; 274, call; 274, 275; 274, 276; 275, identifier:sorted; 276, argument_list; 276, 277; 277, call; 277, 278; 277, 279; 278, identifier:method_iter; 279, argument_list; 280, raise_statement; 281, comment; 282, return_statement; 282, 283; 283, identifier:result
def _route(self, args, request=None): ''' Routes a request to the appropriate controller and returns its result. Performs a bit of validation - refuses to route delete and put actions via a GET request). ''' if request is None: from pecan import request # convention uses "_method" to handle browser-unsupported methods method = request.params.get('_method', request.method).lower() # make sure DELETE/PUT requests don't use GET if request.method == 'GET' and method in ('delete', 'put'): abort(405) # check for nested controllers result = self._find_sub_controllers(args, request) if result: return result # handle the request handler = getattr( self, '_handle_%s' % method, self._handle_unknown_method ) try: if len(getargspec(handler).args) == 3: result = handler(method, args) else: result = handler(method, args, request) # # If the signature of the handler does not match the number # of remaining positional arguments, attempt to handle # a _lookup method (if it exists) # argspec = self._get_args_for_controller(result[0]) num_args = len(argspec) if num_args < len(args): _lookup_result = self._handle_lookup(args, request) if _lookup_result: return _lookup_result except (exc.HTTPClientError, exc.HTTPNotFound, exc.HTTPMethodNotAllowed) as e: # # If the matching handler results in a 400, 404, or 405, attempt to # handle a _lookup method (if it exists) # _lookup_result = self._handle_lookup(args, request) if _lookup_result: return _lookup_result # Build a correct Allow: header if isinstance(e, exc.HTTPMethodNotAllowed): def method_iter(): for func in ('get', 'get_one', 'get_all', 'new', 'edit', 'get_delete'): if self._find_controller(func): yield 'GET' break for method in ('HEAD', 'POST', 'PUT', 'DELETE', 'TRACE', 'PATCH'): func = method.lower() if self._find_controller(func): yield method e.allow = sorted(method_iter()) raise # return the result return result
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:_handle_get; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:method; 6, identifier:remainder; 7, default_parameter; 7, 8; 7, 9; 8, identifier:request; 9, None; 10, block; 10, 11; 10, 13; 10, 27; 10, 28; 10, 83; 10, 90; 10, 91; 10, 129; 10, 140; 10, 145; 10, 156; 10, 176; 10, 177; 10, 187; 10, 203; 11, expression_statement; 11, 12; 12, string:''' Routes ``GET`` actions to the appropriate controller. '''; 13, if_statement; 13, 14; 13, 17; 14, comparison_operator:is; 14, 15; 14, 16; 15, identifier:request; 16, None; 17, block; 17, 18; 18, expression_statement; 18, 19; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:_raise_method_deprecation_warning; 23, argument_list; 23, 24; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:_handle_get; 27, comment; 28, if_statement; 28, 29; 28, 36; 29, boolean_operator:or; 29, 30; 29, 32; 30, not_operator; 30, 31; 31, identifier:remainder; 32, comparison_operator:==; 32, 33; 32, 34; 33, identifier:remainder; 34, list:['']; 34, 35; 35, string:''; 36, block; 36, 37; 36, 52; 36, 62; 36, 78; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:remainder; 40, call; 40, 41; 40, 42; 41, identifier:list; 42, argument_list; 42, 43; 43, call; 43, 44; 43, 49; 44, attribute; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:six; 47, identifier:moves; 48, identifier:filter; 49, argument_list; 49, 50; 49, 51; 50, identifier:bool; 51, identifier:remainder; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:controller; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:_find_controller; 59, argument_list; 59, 60; 59, 61; 60, string:'get_all'; 61, string:'get'; 62, if_statement; 62, 63; 62, 64; 63, identifier:controller; 64, block; 64, 65; 64, 74; 65, expression_statement; 65, 66; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:self; 69, identifier:_handle_bad_rest_arguments; 70, argument_list; 70, 71; 70, 72; 70, 73; 71, identifier:controller; 72, identifier:remainder; 73, identifier:request; 74, return_statement; 74, 75; 75, expression_list; 75, 76; 75, 77; 76, identifier:controller; 77, list:[]; 78, expression_statement; 78, 79; 79, call; 79, 80; 79, 81; 80, identifier:abort; 81, argument_list; 81, 82; 82, integer:405; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:method_name; 86, subscript; 86, 87; 86, 88; 87, identifier:remainder; 88, unary_operator:-; 88, 89; 89, integer:1; 90, comment; 91, if_statement; 91, 92; 91, 98; 92, comparison_operator:in; 92, 93; 92, 94; 93, identifier:method_name; 94, tuple; 94, 95; 94, 96; 94, 97; 95, string:'new'; 96, string:'edit'; 97, string:'delete'; 98, block; 98, 99; 98, 108; 98, 117; 99, if_statement; 99, 100; 99, 103; 100, comparison_operator:==; 100, 101; 100, 102; 101, identifier:method_name; 102, string:'delete'; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:method_name; 107, string:'get_delete'; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:controller; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:self; 114, identifier:_find_controller; 115, argument_list; 115, 116; 116, identifier:method_name; 117, if_statement; 117, 118; 117, 119; 118, identifier:controller; 119, block; 119, 120; 120, return_statement; 120, 121; 121, expression_list; 121, 122; 121, 123; 122, identifier:controller; 123, subscript; 123, 124; 123, 125; 124, identifier:remainder; 125, slice; 125, 126; 125, 127; 126, colon; 127, unary_operator:-; 127, 128; 128, integer:1; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:match; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:self; 135, identifier:_handle_custom_action; 136, argument_list; 136, 137; 136, 138; 136, 139; 137, identifier:method; 138, identifier:remainder; 139, identifier:request; 140, if_statement; 140, 141; 140, 142; 141, identifier:match; 142, block; 142, 143; 143, return_statement; 143, 144; 144, identifier:match; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:controller; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:self; 151, identifier:_lookup_child; 152, argument_list; 152, 153; 153, subscript; 153, 154; 153, 155; 154, identifier:remainder; 155, integer:0; 156, if_statement; 156, 157; 156, 164; 157, boolean_operator:and; 157, 158; 157, 159; 158, identifier:controller; 159, not_operator; 159, 160; 160, call; 160, 161; 160, 162; 161, identifier:ismethod; 162, argument_list; 162, 163; 163, identifier:controller; 164, block; 164, 165; 165, return_statement; 165, 166; 166, call; 166, 167; 166, 168; 167, identifier:lookup_controller; 168, argument_list; 168, 169; 168, 170; 168, 175; 169, identifier:controller; 170, subscript; 170, 171; 170, 172; 171, identifier:remainder; 172, slice; 172, 173; 172, 174; 173, integer:1; 174, colon; 175, identifier:request; 176, comment; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:controller; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:self; 183, identifier:_find_controller; 184, argument_list; 184, 185; 184, 186; 185, string:'get_one'; 186, string:'get'; 187, if_statement; 187, 188; 187, 189; 188, identifier:controller; 189, block; 189, 190; 189, 199; 190, expression_statement; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:self; 194, identifier:_handle_bad_rest_arguments; 195, argument_list; 195, 196; 195, 197; 195, 198; 196, identifier:controller; 197, identifier:remainder; 198, identifier:request; 199, return_statement; 199, 200; 200, expression_list; 200, 201; 200, 202; 201, identifier:controller; 202, identifier:remainder; 203, expression_statement; 203, 204; 204, call; 204, 205; 204, 206; 205, identifier:abort; 206, argument_list; 206, 207; 207, integer:405
def _handle_get(self, method, remainder, request=None): ''' Routes ``GET`` actions to the appropriate controller. ''' if request is None: self._raise_method_deprecation_warning(self._handle_get) # route to a get_all or get if no additional parts are available if not remainder or remainder == ['']: remainder = list(six.moves.filter(bool, remainder)) controller = self._find_controller('get_all', 'get') if controller: self._handle_bad_rest_arguments(controller, remainder, request) return controller, [] abort(405) method_name = remainder[-1] # check for new/edit/delete GET requests if method_name in ('new', 'edit', 'delete'): if method_name == 'delete': method_name = 'get_delete' controller = self._find_controller(method_name) if controller: return controller, remainder[:-1] match = self._handle_custom_action(method, remainder, request) if match: return match controller = self._lookup_child(remainder[0]) if controller and not ismethod(controller): return lookup_controller(controller, remainder[1:], request) # finally, check for the regular get_one/get requests controller = self._find_controller('get_one', 'get') if controller: self._handle_bad_rest_arguments(controller, remainder, request) return controller, remainder abort(405)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:_handle_delete; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:method; 6, identifier:remainder; 7, default_parameter; 7, 8; 7, 9; 8, identifier:request; 9, None; 10, block; 10, 11; 10, 13; 10, 27; 10, 77; 10, 78; 10, 88; 10, 95; 10, 96; 10, 97; 10, 140; 11, expression_statement; 11, 12; 12, string:''' Routes ``DELETE`` actions to the appropriate controller. '''; 13, if_statement; 13, 14; 13, 17; 14, comparison_operator:is; 14, 15; 14, 16; 15, identifier:request; 16, None; 17, block; 17, 18; 18, expression_statement; 18, 19; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:_raise_method_deprecation_warning; 23, argument_list; 23, 24; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:_handle_delete; 27, if_statement; 27, 28; 27, 29; 28, identifier:remainder; 29, block; 29, 30; 29, 41; 29, 46; 29, 57; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:match; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:_handle_custom_action; 37, argument_list; 37, 38; 37, 39; 37, 40; 38, identifier:method; 39, identifier:remainder; 40, identifier:request; 41, if_statement; 41, 42; 41, 43; 42, identifier:match; 43, block; 43, 44; 44, return_statement; 44, 45; 45, identifier:match; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:controller; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:_lookup_child; 53, argument_list; 53, 54; 54, subscript; 54, 55; 54, 56; 55, identifier:remainder; 56, integer:0; 57, if_statement; 57, 58; 57, 65; 58, boolean_operator:and; 58, 59; 58, 60; 59, identifier:controller; 60, not_operator; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:ismethod; 63, argument_list; 63, 64; 64, identifier:controller; 65, block; 65, 66; 66, return_statement; 66, 67; 67, call; 67, 68; 67, 69; 68, identifier:lookup_controller; 69, argument_list; 69, 70; 69, 71; 69, 76; 70, identifier:controller; 71, subscript; 71, 72; 71, 73; 72, identifier:remainder; 73, slice; 73, 74; 73, 75; 74, integer:1; 75, colon; 76, identifier:request; 77, comment; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:controller; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:_find_controller; 85, argument_list; 85, 86; 85, 87; 86, string:'post_delete'; 87, string:'delete'; 88, if_statement; 88, 89; 88, 90; 89, identifier:controller; 90, block; 90, 91; 91, return_statement; 91, 92; 92, expression_list; 92, 93; 92, 94; 93, identifier:controller; 94, identifier:remainder; 95, comment; 96, comment; 97, if_statement; 97, 98; 97, 99; 98, identifier:remainder; 99, block; 99, 100; 99, 115; 99, 126; 100, if_statement; 100, 101; 100, 109; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:_find_controller; 105, argument_list; 105, 106; 106, subscript; 106, 107; 106, 108; 107, identifier:remainder; 108, integer:0; 109, block; 109, 110; 110, expression_statement; 110, 111; 111, call; 111, 112; 111, 113; 112, identifier:abort; 113, argument_list; 113, 114; 114, integer:405; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:sub_controller; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:self; 121, identifier:_lookup_child; 122, argument_list; 122, 123; 123, subscript; 123, 124; 123, 125; 124, identifier:remainder; 125, integer:0; 126, if_statement; 126, 127; 126, 128; 127, identifier:sub_controller; 128, block; 128, 129; 129, return_statement; 129, 130; 130, call; 130, 131; 130, 132; 131, identifier:lookup_controller; 132, argument_list; 132, 133; 132, 134; 132, 139; 133, identifier:sub_controller; 134, subscript; 134, 135; 134, 136; 135, identifier:remainder; 136, slice; 136, 137; 136, 138; 137, integer:1; 138, colon; 139, identifier:request; 140, expression_statement; 140, 141; 141, call; 141, 142; 141, 143; 142, identifier:abort; 143, argument_list; 143, 144; 144, integer:405
def _handle_delete(self, method, remainder, request=None): ''' Routes ``DELETE`` actions to the appropriate controller. ''' if request is None: self._raise_method_deprecation_warning(self._handle_delete) if remainder: match = self._handle_custom_action(method, remainder, request) if match: return match controller = self._lookup_child(remainder[0]) if controller and not ismethod(controller): return lookup_controller(controller, remainder[1:], request) # check for post_delete/delete requests first controller = self._find_controller('post_delete', 'delete') if controller: return controller, remainder # if no controller exists, try routing to a sub-controller; note that # since this is a DELETE verb, any local exposes are 405'd if remainder: if self._find_controller(remainder[0]): abort(405) sub_controller = self._lookup_child(remainder[0]) if sub_controller: return lookup_controller(sub_controller, remainder[1:], request) abort(405)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 4; 2, function_name:gunicorn_run; 3, parameters; 4, block; 4, 5; 4, 7; 4, 55; 4, 233; 5, expression_statement; 5, 6; 6, comment; 7, try_statement; 7, 8; 7, 16; 8, block; 8, 9; 9, import_from_statement; 9, 10; 9, 14; 10, dotted_name; 10, 11; 10, 12; 10, 13; 11, identifier:gunicorn; 12, identifier:app; 13, identifier:wsgiapp; 14, dotted_name; 14, 15; 15, identifier:WSGIApplication; 16, except_clause; 16, 17; 16, 21; 17, as_pattern; 17, 18; 17, 19; 18, identifier:ImportError; 19, as_pattern_target; 19, 20; 20, identifier:exc; 21, block; 21, 22; 21, 28; 21, 37; 21, 41; 21, 54; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:args; 25, attribute; 25, 26; 25, 27; 26, identifier:exc; 27, identifier:args; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:arg0; 31, conditional_expression:if; 31, 32; 31, 35; 31, 36; 32, subscript; 32, 33; 32, 34; 33, identifier:args; 34, integer:0; 35, identifier:args; 36, string:''; 37, expression_statement; 37, 38; 38, augmented_assignment:+=; 38, 39; 38, 40; 39, identifier:arg0; 40, string:' (are you sure `gunicorn` is installed?)'; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:exc; 45, identifier:args; 46, binary_operator:+; 46, 47; 46, 49; 47, tuple; 47, 48; 48, identifier:arg0; 49, subscript; 49, 50; 49, 51; 50, identifier:args; 51, slice; 51, 52; 51, 53; 52, integer:1; 53, colon; 54, raise_statement; 55, class_definition; 55, 56; 55, 57; 55, 59; 56, identifier:PecanApplication; 57, argument_list; 57, 58; 58, identifier:WSGIApplication; 59, block; 59, 60; 59, 215; 60, function_definition; 60, 61; 60, 62; 60, 67; 61, function_name:init; 62, parameters; 62, 63; 62, 64; 62, 65; 62, 66; 63, identifier:self; 64, identifier:parser; 65, identifier:opts; 66, identifier:args; 67, block; 67, 68; 67, 83; 67, 110; 67, 125; 67, 149; 67, 157; 67, 167; 67, 168; 67, 172; 67, 213; 68, if_statement; 68, 69; 68, 75; 69, comparison_operator:!=; 69, 70; 69, 74; 70, call; 70, 71; 70, 72; 71, identifier:len; 72, argument_list; 72, 73; 73, identifier:args; 74, integer:1; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:parser; 80, identifier:error; 81, argument_list; 81, 82; 82, string:"No configuration file was specified."; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:self; 87, identifier:cfgfname; 88, call; 88, 89; 88, 94; 89, attribute; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:os; 92, identifier:path; 93, identifier:normpath; 94, argument_list; 94, 95; 95, call; 95, 96; 95, 101; 96, attribute; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:os; 99, identifier:path; 100, identifier:join; 101, argument_list; 101, 102; 101, 107; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:os; 105, identifier:getcwd; 106, argument_list; 107, subscript; 107, 108; 107, 109; 108, identifier:args; 109, integer:0; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:self; 114, identifier:cfgfname; 115, call; 115, 116; 115, 121; 116, attribute; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:os; 119, identifier:path; 120, identifier:abspath; 121, argument_list; 121, 122; 122, attribute; 122, 123; 122, 124; 123, identifier:self; 124, identifier:cfgfname; 125, if_statement; 125, 126; 125, 137; 126, not_operator; 126, 127; 127, call; 127, 128; 127, 133; 128, attribute; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:os; 131, identifier:path; 132, identifier:exists; 133, argument_list; 133, 134; 134, attribute; 134, 135; 134, 136; 135, identifier:self; 136, identifier:cfgfname; 137, block; 137, 138; 138, expression_statement; 138, 139; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:parser; 142, identifier:error; 143, argument_list; 143, 144; 144, binary_operator:%; 144, 145; 144, 146; 145, string:"Config file not found: %s"; 146, attribute; 146, 147; 146, 148; 147, identifier:self; 148, identifier:cfgfname; 149, import_from_statement; 149, 150; 149, 153; 149, 155; 150, dotted_name; 150, 151; 150, 152; 151, identifier:pecan; 152, identifier:configuration; 153, dotted_name; 153, 154; 154, identifier:_runtime_conf; 155, dotted_name; 155, 156; 156, identifier:set_config; 157, expression_statement; 157, 158; 158, call; 158, 159; 158, 160; 159, identifier:set_config; 160, argument_list; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:self; 163, identifier:cfgfname; 164, keyword_argument; 164, 165; 164, 166; 165, identifier:overwrite; 166, True; 167, comment; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:cfg; 171, dictionary; 172, if_statement; 172, 173; 172, 179; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:_runtime_conf; 176, identifier:get; 177, argument_list; 177, 178; 178, string:'server'; 179, block; 179, 180; 179, 186; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:server; 183, subscript; 183, 184; 183, 185; 184, identifier:_runtime_conf; 185, string:'server'; 186, if_statement; 186, 187; 186, 198; 187, boolean_operator:and; 187, 188; 187, 193; 188, call; 188, 189; 188, 190; 189, identifier:hasattr; 190, argument_list; 190, 191; 190, 192; 191, identifier:server; 192, string:'host'; 193, call; 193, 194; 193, 195; 194, identifier:hasattr; 195, argument_list; 195, 196; 195, 197; 196, identifier:server; 197, string:'port'; 198, block; 198, 199; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 204; 201, subscript; 201, 202; 201, 203; 202, identifier:cfg; 203, string:'bind'; 204, binary_operator:%; 204, 205; 204, 206; 205, string:'%s:%s'; 206, tuple; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:server; 209, identifier:host; 210, attribute; 210, 211; 210, 212; 211, identifier:server; 212, identifier:port; 213, return_statement; 213, 214; 214, identifier:cfg; 215, function_definition; 215, 216; 215, 217; 215, 219; 216, function_name:load; 217, parameters; 217, 218; 218, identifier:self; 219, block; 219, 220; 219, 226; 220, import_from_statement; 220, 221; 220, 224; 221, dotted_name; 221, 222; 221, 223; 222, identifier:pecan; 223, identifier:deploy; 224, dotted_name; 224, 225; 225, identifier:deploy; 226, return_statement; 226, 227; 227, call; 227, 228; 227, 229; 228, identifier:deploy; 229, argument_list; 229, 230; 230, attribute; 230, 231; 230, 232; 231, identifier:self; 232, identifier:cfgfname; 233, expression_statement; 233, 234; 234, call; 234, 235; 234, 241; 235, attribute; 235, 236; 235, 240; 236, call; 236, 237; 236, 238; 237, identifier:PecanApplication; 238, argument_list; 238, 239; 239, string:"%(prog)s [OPTIONS] config.py"; 240, identifier:run; 241, argument_list
def gunicorn_run(): """ The ``gunicorn_pecan`` command for launching ``pecan`` applications """ try: from gunicorn.app.wsgiapp import WSGIApplication except ImportError as exc: args = exc.args arg0 = args[0] if args else '' arg0 += ' (are you sure `gunicorn` is installed?)' exc.args = (arg0,) + args[1:] raise class PecanApplication(WSGIApplication): def init(self, parser, opts, args): if len(args) != 1: parser.error("No configuration file was specified.") self.cfgfname = os.path.normpath( os.path.join(os.getcwd(), args[0]) ) self.cfgfname = os.path.abspath(self.cfgfname) if not os.path.exists(self.cfgfname): parser.error("Config file not found: %s" % self.cfgfname) from pecan.configuration import _runtime_conf, set_config set_config(self.cfgfname, overwrite=True) # If available, use the host and port from the pecan config file cfg = {} if _runtime_conf.get('server'): server = _runtime_conf['server'] if hasattr(server, 'host') and hasattr(server, 'port'): cfg['bind'] = '%s:%s' % ( server.host, server.port ) return cfg def load(self): from pecan.deploy import deploy return deploy(self.cfgfname) PecanApplication("%(prog)s [OPTIONS] config.py").run()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:expose; 3, parameters; 3, 4; 3, 7; 3, 10; 3, 13; 4, default_parameter; 4, 5; 4, 6; 5, identifier:template; 6, None; 7, default_parameter; 7, 8; 7, 9; 8, identifier:generic; 9, False; 10, default_parameter; 10, 11; 10, 12; 11, identifier:route; 12, None; 13, dictionary_splat_pattern; 13, 14; 14, identifier:kw; 15, block; 15, 16; 15, 18; 15, 28; 15, 37; 15, 197; 16, expression_statement; 16, 17; 17, string:''' Decorator used to flag controller methods as being "exposed" for access via HTTP, and to configure that access. :param template: The path to a template, relative to the base template directory. Can also be passed a string representing a special or custom renderer, such as ``'json'`` for :ref:`expose_json`. :param content_type: The content-type to use for this template. :param generic: A boolean which flags this as a "generic" controller, which uses generic functions based upon ``functools.singledispatch`` generic functions. Allows you to split a single controller into multiple paths based upon HTTP method. :param route: The name of the path segment to match (excluding separator characters, like `/`). Defaults to the name of the function itself, but this can be used to resolve paths which are not valid Python function names, e.g., if you wanted to route a function to `some-special-path'. '''; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:content_type; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:kw; 24, identifier:get; 25, argument_list; 25, 26; 25, 27; 26, string:'content_type'; 27, string:'text/html'; 28, if_statement; 28, 29; 28, 32; 29, comparison_operator:==; 29, 30; 29, 31; 30, identifier:template; 31, string:'json'; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:content_type; 36, string:'application/json'; 37, function_definition; 37, 38; 37, 39; 37, 41; 37, 42; 38, function_name:decorate; 39, parameters; 39, 40; 40, identifier:f; 41, comment; 42, block; 42, 43; 42, 49; 42, 56; 42, 64; 42, 96; 42, 97; 42, 103; 42, 116; 42, 128; 42, 129; 42, 185; 42, 186; 42, 195; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:f; 47, identifier:exposed; 48, True; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:cfg; 52, call; 52, 53; 52, 54; 53, identifier:_cfg; 54, argument_list; 54, 55; 55, identifier:f; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 61; 58, subscript; 58, 59; 58, 60; 59, identifier:cfg; 60, string:'explicit_content_type'; 61, comparison_operator:in; 61, 62; 61, 63; 62, string:'content_type'; 63, identifier:kw; 64, if_statement; 64, 65; 64, 66; 64, 67; 65, identifier:route; 66, comment; 67, block; 67, 68; 67, 73; 67, 88; 68, import_from_statement; 68, 69; 68, 71; 69, dotted_name; 69, 70; 70, identifier:pecan; 71, dotted_name; 71, 72; 72, identifier:routing; 73, if_statement; 73, 74; 73, 80; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:cfg; 77, identifier:get; 78, argument_list; 78, 79; 79, string:'generic_handler'; 80, block; 80, 81; 81, raise_statement; 81, 82; 82, call; 82, 83; 82, 84; 83, identifier:ValueError; 84, argument_list; 84, 85; 85, concatenated_string; 85, 86; 85, 87; 86, string:'Path segments cannot be overridden for generic '; 87, string:'controllers.'; 88, expression_statement; 88, 89; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:routing; 92, identifier:route; 93, argument_list; 93, 94; 93, 95; 94, identifier:route; 95, identifier:f; 96, comment; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 102; 99, subscript; 99, 100; 99, 101; 100, identifier:cfg; 101, string:'content_type'; 102, identifier:content_type; 103, expression_statement; 103, 104; 104, call; 104, 105; 104, 114; 105, attribute; 105, 106; 105, 113; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:cfg; 109, identifier:setdefault; 110, argument_list; 110, 111; 110, 112; 111, string:'template'; 112, list:[]; 113, identifier:append; 114, argument_list; 114, 115; 115, identifier:template; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 127; 118, subscript; 118, 119; 118, 126; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:cfg; 122, identifier:setdefault; 123, argument_list; 123, 124; 123, 125; 124, string:'content_types'; 125, dictionary; 126, identifier:content_type; 127, identifier:template; 128, comment; 129, if_statement; 129, 130; 129, 131; 130, identifier:generic; 131, block; 131, 132; 131, 153; 131, 159; 131, 170; 131, 176; 132, if_statement; 132, 133; 132, 141; 133, comparison_operator:in; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:f; 136, identifier:__name__; 137, tuple; 137, 138; 137, 139; 137, 140; 138, string:'_default'; 139, string:'_lookup'; 140, string:'_route'; 141, block; 141, 142; 142, raise_statement; 142, 143; 143, call; 143, 144; 143, 145; 144, identifier:ValueError; 145, argument_list; 145, 146; 146, binary_operator:%; 146, 147; 146, 150; 147, concatenated_string; 147, 148; 147, 149; 148, string:'The special method %s cannot be used as a generic '; 149, string:'controller'; 150, attribute; 150, 151; 150, 152; 151, identifier:f; 152, identifier:__name__; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 158; 155, subscript; 155, 156; 155, 157; 156, identifier:cfg; 157, string:'generic'; 158, True; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 164; 161, subscript; 161, 162; 161, 163; 162, identifier:cfg; 163, string:'generic_handlers'; 164, call; 164, 165; 164, 166; 165, identifier:dict; 166, argument_list; 166, 167; 167, keyword_argument; 167, 168; 167, 169; 168, identifier:DEFAULT; 169, identifier:f; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 175; 172, subscript; 172, 173; 172, 174; 173, identifier:cfg; 174, string:'allowed_methods'; 175, list:[]; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:f; 180, identifier:when; 181, call; 181, 182; 181, 183; 182, identifier:when_for; 183, argument_list; 183, 184; 184, identifier:f; 185, comment; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 191; 188, subscript; 188, 189; 188, 190; 189, identifier:cfg; 190, string:'argspec'; 191, call; 191, 192; 191, 193; 192, identifier:getargspec; 193, argument_list; 193, 194; 194, identifier:f; 195, return_statement; 195, 196; 196, identifier:f; 197, return_statement; 197, 198; 198, identifier:decorate
def expose(template=None, generic=False, route=None, **kw): ''' Decorator used to flag controller methods as being "exposed" for access via HTTP, and to configure that access. :param template: The path to a template, relative to the base template directory. Can also be passed a string representing a special or custom renderer, such as ``'json'`` for :ref:`expose_json`. :param content_type: The content-type to use for this template. :param generic: A boolean which flags this as a "generic" controller, which uses generic functions based upon ``functools.singledispatch`` generic functions. Allows you to split a single controller into multiple paths based upon HTTP method. :param route: The name of the path segment to match (excluding separator characters, like `/`). Defaults to the name of the function itself, but this can be used to resolve paths which are not valid Python function names, e.g., if you wanted to route a function to `some-special-path'. ''' content_type = kw.get('content_type', 'text/html') if template == 'json': content_type = 'application/json' def decorate(f): # flag the method as exposed f.exposed = True cfg = _cfg(f) cfg['explicit_content_type'] = 'content_type' in kw if route: # This import is here to avoid a circular import issue from pecan import routing if cfg.get('generic_handler'): raise ValueError( 'Path segments cannot be overridden for generic ' 'controllers.' ) routing.route(route, f) # set a "pecan" attribute, where we will store details cfg['content_type'] = content_type cfg.setdefault('template', []).append(template) cfg.setdefault('content_types', {})[content_type] = template # handle generic controllers if generic: if f.__name__ in ('_default', '_lookup', '_route'): raise ValueError( 'The special method %s cannot be used as a generic ' 'controller' % f.__name__ ) cfg['generic'] = True cfg['generic_handlers'] = dict(DEFAULT=f) cfg['allowed_methods'] = [] f.when = when_for(f) # store the arguments for this controller method cfg['argspec'] = getargspec(f) return f return decorate
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:add_operations_bulk; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:payload; 6, block; 6, 7; 6, 9; 6, 10; 6, 22; 6, 32; 6, 36; 6, 40; 6, 44; 6, 137; 6, 147; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 16; 12, pattern_list; 12, 13; 12, 14; 12, 15; 13, identifier:valid_indices; 14, identifier:_; 15, identifier:_; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:_check_agent_id_bulk; 20, argument_list; 20, 21; 21, identifier:payload; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:valid_payload; 25, list_comprehension; 25, 26; 25, 29; 26, subscript; 26, 27; 26, 28; 27, identifier:payload; 28, identifier:i; 29, for_in_clause; 29, 30; 29, 31; 30, identifier:i; 31, identifier:valid_indices; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:chunked_data; 35, list:[]; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:current_chunk; 39, list:[]; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:current_chunk_size; 43, integer:0; 44, for_statement; 44, 45; 44, 46; 44, 47; 45, identifier:agent; 46, identifier:valid_payload; 47, block; 47, 48; 48, if_statement; 48, 49; 48, 61; 49, parenthesized_expression; 49, 50; 50, boolean_operator:and; 50, 51; 50, 54; 51, subscript; 51, 52; 51, 53; 52, identifier:agent; 53, string:"operations"; 54, call; 54, 55; 54, 56; 55, identifier:isinstance; 56, argument_list; 56, 57; 56, 60; 57, subscript; 57, 58; 57, 59; 58, identifier:agent; 59, string:"operations"; 60, identifier:list; 61, block; 61, 62; 61, 93; 62, if_statement; 62, 63; 62, 77; 63, comparison_operator:>; 63, 64; 63, 72; 64, binary_operator:+; 64, 65; 64, 66; 65, identifier:current_chunk_size; 66, call; 66, 67; 66, 68; 67, identifier:len; 68, argument_list; 68, 69; 69, subscript; 69, 70; 69, 71; 70, identifier:agent; 71, string:"operations"; 72, subscript; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:self; 75, identifier:config; 76, string:"operationsChunksSize"; 77, block; 77, 78; 77, 85; 77, 89; 78, expression_statement; 78, 79; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:chunked_data; 82, identifier:append; 83, argument_list; 83, 84; 84, identifier:current_chunk; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:current_chunk_size; 88, integer:0; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:current_chunk; 92, list:[]; 93, if_statement; 93, 94; 93, 106; 93, 119; 94, comparison_operator:>; 94, 95; 94, 101; 95, call; 95, 96; 95, 97; 96, identifier:len; 97, argument_list; 97, 98; 98, subscript; 98, 99; 98, 100; 99, identifier:agent; 100, string:"operations"; 101, subscript; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:config; 105, string:"operationsChunksSize"; 106, block; 106, 107; 106, 115; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:chunked_data; 111, identifier:append; 112, argument_list; 112, 113; 113, list:[agent]; 113, 114; 114, identifier:agent; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:current_chunk_size; 118, integer:0; 119, else_clause; 119, 120; 120, block; 120, 121; 120, 130; 121, expression_statement; 121, 122; 122, augmented_assignment:+=; 122, 123; 122, 124; 123, identifier:current_chunk_size; 124, call; 124, 125; 124, 126; 125, identifier:len; 126, argument_list; 126, 127; 127, subscript; 127, 128; 127, 129; 128, identifier:agent; 129, string:"operations"; 130, expression_statement; 130, 131; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:current_chunk; 134, identifier:append; 135, argument_list; 135, 136; 136, identifier:agent; 137, if_statement; 137, 138; 137, 139; 138, identifier:current_chunk; 139, block; 139, 140; 140, expression_statement; 140, 141; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:chunked_data; 144, identifier:append; 145, argument_list; 145, 146; 146, identifier:current_chunk; 147, return_statement; 147, 148; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:self; 151, identifier:_add_operations_bulk; 152, argument_list; 152, 153; 153, identifier:chunked_data
def add_operations_bulk(self, payload): """Add operations to a group of agents. :param list payload: contains the informations necessary for the action. It's in the form [{"id": agent_id, "operations": operations}] With id that is an str containing only characters in "a-zA-Z0-9_-" and must be between 1 and 36 characters. It must referenced an existing agent. With operations a list containing dictionnaries that has the form given in the craftai documentation and the configuration of the agent. :return: list of agents containing a message about the added operations. :rtype: list if dict. :raises CraftAiBadRequestError: if all of the ids are invalid or referenced non existing agents or one of the operations is invalid. """ # Check all ids, raise an error if all ids are invalid valid_indices, _, _ = self._check_agent_id_bulk(payload) valid_payload = [payload[i] for i in valid_indices] chunked_data = [] current_chunk = [] current_chunk_size = 0 for agent in valid_payload: if (agent["operations"] and isinstance(agent["operations"], list)): if current_chunk_size + len(agent["operations"]) > self.config["operationsChunksSize"]: chunked_data.append(current_chunk) current_chunk_size = 0 current_chunk = [] if len(agent["operations"]) > self.config["operationsChunksSize"]: chunked_data.append([agent]) current_chunk_size = 0 else: current_chunk_size += len(agent["operations"]) current_chunk.append(agent) if current_chunk: chunked_data.append(current_chunk) return self._add_operations_bulk(chunked_data)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:parse; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:cls; 5, identifier:line; 6, default_parameter; 6, 7; 6, 8; 7, identifier:encoding; 8, attribute; 8, 9; 8, 12; 9, attribute; 9, 10; 9, 11; 10, identifier:pydle; 11, identifier:protocol; 12, identifier:DEFAULT_ENCODING; 13, block; 13, 14; 13, 16; 13, 20; 13, 21; 13, 49; 13, 50; 13, 64; 13, 65; 13, 113; 13, 114; 13, 131; 13, 132; 13, 133; 13, 174; 13, 221; 13, 222; 13, 237; 13, 238; 13, 239; 13, 240; 13, 351; 13, 352; 13, 353; 13, 354; 13, 374; 13, 375; 14, expression_statement; 14, 15; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:valid; 19, True; 20, comment; 21, try_statement; 21, 22; 21, 32; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:message; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:line; 29, identifier:decode; 30, argument_list; 30, 31; 31, identifier:encoding; 32, except_clause; 32, 33; 32, 34; 32, 35; 33, identifier:UnicodeDecodeError; 34, comment; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:message; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:line; 42, identifier:decode; 43, argument_list; 43, 44; 44, attribute; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:pydle; 47, identifier:protocol; 48, identifier:FALLBACK_ENCODING; 49, comment; 50, if_statement; 50, 51; 50, 59; 51, comparison_operator:>; 51, 52; 51, 56; 52, call; 52, 53; 52, 54; 53, identifier:len; 54, argument_list; 54, 55; 55, identifier:message; 56, attribute; 56, 57; 56, 58; 57, identifier:protocol; 58, identifier:MESSAGE_LENGTH_LIMIT; 59, block; 59, 60; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:valid; 63, False; 64, comment; 65, if_statement; 65, 66; 65, 74; 65, 89; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:message; 69, identifier:endswith; 70, argument_list; 70, 71; 71, attribute; 71, 72; 71, 73; 72, identifier:protocol; 73, identifier:LINE_SEPARATOR; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:message; 78, subscript; 78, 79; 78, 80; 79, identifier:message; 80, slice; 80, 81; 80, 82; 81, colon; 82, unary_operator:-; 82, 83; 83, call; 83, 84; 83, 85; 84, identifier:len; 85, argument_list; 85, 86; 86, attribute; 86, 87; 86, 88; 87, identifier:protocol; 88, identifier:LINE_SEPARATOR; 89, elif_clause; 89, 90; 89, 98; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:message; 93, identifier:endswith; 94, argument_list; 94, 95; 95, attribute; 95, 96; 95, 97; 96, identifier:protocol; 97, identifier:MINIMAL_LINE_SEPARATOR; 98, block; 98, 99; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:message; 102, subscript; 102, 103; 102, 104; 103, identifier:message; 104, slice; 104, 105; 104, 106; 105, colon; 106, unary_operator:-; 106, 107; 107, call; 107, 108; 107, 109; 108, identifier:len; 109, argument_list; 109, 110; 110, attribute; 110, 111; 110, 112; 111, identifier:protocol; 112, identifier:MINIMAL_LINE_SEPARATOR; 113, comment; 114, if_statement; 114, 115; 114, 126; 115, call; 115, 116; 115, 117; 116, identifier:any; 117, generator_expression; 117, 118; 117, 121; 118, comparison_operator:in; 118, 119; 118, 120; 119, identifier:ch; 120, identifier:message; 121, for_in_clause; 121, 122; 121, 123; 122, identifier:ch; 123, attribute; 123, 124; 123, 125; 124, identifier:protocol; 125, identifier:FORBIDDEN_CHARACTERS; 126, block; 126, 127; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:valid; 130, False; 131, comment; 132, comment; 133, if_statement; 133, 134; 133, 140; 133, 157; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:message; 137, identifier:startswith; 138, argument_list; 138, 139; 139, string:':'; 140, block; 140, 141; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:parts; 144, call; 144, 145; 144, 150; 145, attribute; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:protocol; 148, identifier:ARGUMENT_SEPARATOR; 149, identifier:split; 150, argument_list; 150, 151; 150, 156; 151, subscript; 151, 152; 151, 153; 152, identifier:message; 153, slice; 153, 154; 153, 155; 154, integer:1; 155, colon; 156, integer:2; 157, else_clause; 157, 158; 158, block; 158, 159; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:parts; 162, binary_operator:+; 162, 163; 162, 165; 163, list:[ None ]; 163, 164; 164, None; 165, call; 165, 166; 165, 171; 166, attribute; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:protocol; 169, identifier:ARGUMENT_SEPARATOR; 170, identifier:split; 171, argument_list; 171, 172; 171, 173; 172, identifier:message; 173, integer:1; 174, if_statement; 174, 175; 174, 181; 174, 189; 174, 207; 175, comparison_operator:==; 175, 176; 175, 180; 176, call; 176, 177; 176, 178; 177, identifier:len; 178, argument_list; 178, 179; 179, identifier:parts; 180, integer:3; 181, block; 181, 182; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 188; 184, pattern_list; 184, 185; 184, 186; 184, 187; 185, identifier:source; 186, identifier:command; 187, identifier:raw_params; 188, identifier:parts; 189, elif_clause; 189, 190; 189, 196; 190, comparison_operator:==; 190, 191; 190, 195; 191, call; 191, 192; 191, 193; 192, identifier:len; 193, argument_list; 193, 194; 194, identifier:parts; 195, integer:2; 196, block; 196, 197; 196, 203; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 202; 199, pattern_list; 199, 200; 199, 201; 200, identifier:source; 201, identifier:command; 202, identifier:parts; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:raw_params; 206, string:''; 207, else_clause; 207, 208; 208, block; 208, 209; 209, raise_statement; 209, 210; 210, call; 210, 211; 210, 216; 211, attribute; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:pydle; 214, identifier:protocol; 215, identifier:ProtocolViolation; 216, argument_list; 216, 217; 216, 218; 217, string:'Improper IRC message format: not enough elements.'; 218, keyword_argument; 218, 219; 218, 220; 219, identifier:message; 220, identifier:message; 221, comment; 222, if_statement; 222, 223; 222, 232; 223, not_operator; 223, 224; 224, call; 224, 225; 224, 230; 225, attribute; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:protocol; 228, identifier:COMMAND_PATTERN; 229, identifier:match; 230, argument_list; 230, 231; 231, identifier:command; 232, block; 232, 233; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 236; 235, identifier:valid; 236, False; 237, comment; 238, comment; 239, comment; 240, if_statement; 240, 241; 240, 249; 240, 264; 240, 265; 240, 329; 240, 330; 240, 344; 240, 345; 241, call; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:raw_params; 244, identifier:startswith; 245, argument_list; 245, 246; 246, attribute; 246, 247; 246, 248; 247, identifier:protocol; 248, identifier:TRAILING_PREFIX; 249, block; 249, 250; 250, expression_statement; 250, 251; 251, assignment; 251, 252; 251, 253; 252, identifier:params; 253, list:[ raw_params[len(protocol.TRAILING_PREFIX):] ]; 253, 254; 254, subscript; 254, 255; 254, 256; 255, identifier:raw_params; 256, slice; 256, 257; 256, 263; 257, call; 257, 258; 257, 259; 258, identifier:len; 259, argument_list; 259, 260; 260, attribute; 260, 261; 260, 262; 261, identifier:protocol; 262, identifier:TRAILING_PREFIX; 263, colon; 264, comment; 265, elif_clause; 265, 266; 265, 273; 266, comparison_operator:in; 266, 267; 266, 272; 267, binary_operator:+; 267, 268; 267, 269; 268, string:' '; 269, attribute; 269, 270; 269, 271; 270, identifier:protocol; 271, identifier:TRAILING_PREFIX; 272, identifier:raw_params; 273, block; 273, 274; 273, 287; 273, 288; 273, 308; 273, 309; 274, expression_statement; 274, 275; 275, assignment; 275, 276; 275, 277; 276, identifier:index; 277, call; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:raw_params; 280, identifier:find; 281, argument_list; 281, 282; 282, binary_operator:+; 282, 283; 282, 284; 283, string:' '; 284, attribute; 284, 285; 284, 286; 285, identifier:protocol; 286, identifier:TRAILING_PREFIX; 287, comment; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 291; 290, identifier:params; 291, call; 291, 292; 291, 297; 292, attribute; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:protocol; 295, identifier:ARGUMENT_SEPARATOR; 296, identifier:split; 297, argument_list; 297, 298; 298, call; 298, 299; 298, 306; 299, attribute; 299, 300; 299, 305; 300, subscript; 300, 301; 300, 302; 301, identifier:raw_params; 302, slice; 302, 303; 302, 304; 303, colon; 304, identifier:index; 305, identifier:rstrip; 306, argument_list; 306, 307; 307, string:' '; 308, comment; 309, expression_statement; 309, 310; 310, call; 310, 311; 310, 314; 311, attribute; 311, 312; 311, 313; 312, identifier:params; 313, identifier:append; 314, argument_list; 314, 315; 315, subscript; 315, 316; 315, 317; 316, identifier:raw_params; 317, slice; 317, 318; 317, 328; 318, binary_operator:+; 318, 319; 318, 327; 319, binary_operator:+; 319, 320; 319, 321; 320, identifier:index; 321, call; 321, 322; 321, 323; 322, identifier:len; 323, argument_list; 323, 324; 324, attribute; 324, 325; 324, 326; 325, identifier:protocol; 326, identifier:TRAILING_PREFIX; 327, integer:1; 328, colon; 329, comment; 330, elif_clause; 330, 331; 330, 332; 331, identifier:raw_params; 332, block; 332, 333; 333, expression_statement; 333, 334; 334, assignment; 334, 335; 334, 336; 335, identifier:params; 336, call; 336, 337; 336, 342; 337, attribute; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, identifier:protocol; 340, identifier:ARGUMENT_SEPARATOR; 341, identifier:split; 342, argument_list; 342, 343; 343, identifier:raw_params; 344, comment; 345, else_clause; 345, 346; 346, block; 346, 347; 347, expression_statement; 347, 348; 348, assignment; 348, 349; 348, 350; 349, identifier:params; 350, list:[]; 351, comment; 352, comment; 353, comment; 354, try_statement; 354, 355; 354, 363; 355, block; 355, 356; 356, expression_statement; 356, 357; 357, assignment; 357, 358; 357, 359; 358, identifier:command; 359, call; 359, 360; 359, 361; 360, identifier:int; 361, argument_list; 361, 362; 362, identifier:command; 363, except_clause; 363, 364; 363, 365; 364, identifier:ValueError; 365, block; 365, 366; 366, expression_statement; 366, 367; 367, assignment; 367, 368; 367, 369; 368, identifier:command; 369, call; 369, 370; 369, 373; 370, attribute; 370, 371; 370, 372; 371, identifier:command; 372, identifier:upper; 373, argument_list; 374, comment; 375, return_statement; 375, 376; 376, call; 376, 377; 376, 378; 377, identifier:RFC1459Message; 378, argument_list; 378, 379; 378, 380; 378, 381; 378, 384; 378, 387; 379, identifier:command; 380, identifier:params; 381, keyword_argument; 381, 382; 381, 383; 382, identifier:source; 383, identifier:source; 384, keyword_argument; 384, 385; 384, 386; 385, identifier:_valid; 386, identifier:valid; 387, keyword_argument; 387, 388; 387, 389; 388, identifier:_raw; 389, identifier:message
def parse(cls, line, encoding=pydle.protocol.DEFAULT_ENCODING): """ Parse given line into IRC message structure. Returns a Message. """ valid = True # Decode message. try: message = line.decode(encoding) except UnicodeDecodeError: # Try our fallback encoding. message = line.decode(pydle.protocol.FALLBACK_ENCODING) # Sanity check for message length. if len(message) > protocol.MESSAGE_LENGTH_LIMIT: valid = False # Strip message separator. if message.endswith(protocol.LINE_SEPARATOR): message = message[:-len(protocol.LINE_SEPARATOR)] elif message.endswith(protocol.MINIMAL_LINE_SEPARATOR): message = message[:-len(protocol.MINIMAL_LINE_SEPARATOR)] # Sanity check for forbidden characters. if any(ch in message for ch in protocol.FORBIDDEN_CHARACTERS): valid = False # Extract message sections. # Format: (:source)? command parameter* if message.startswith(':'): parts = protocol.ARGUMENT_SEPARATOR.split(message[1:], 2) else: parts = [ None ] + protocol.ARGUMENT_SEPARATOR.split(message, 1) if len(parts) == 3: source, command, raw_params = parts elif len(parts) == 2: source, command = parts raw_params = '' else: raise pydle.protocol.ProtocolViolation('Improper IRC message format: not enough elements.', message=message) # Sanity check for command. if not protocol.COMMAND_PATTERN.match(command): valid = False # Extract parameters properly. # Format: (word|:sentence)* # Only parameter is a 'trailing' sentence. if raw_params.startswith(protocol.TRAILING_PREFIX): params = [ raw_params[len(protocol.TRAILING_PREFIX):] ] # We have a sentence in our parameters. elif ' ' + protocol.TRAILING_PREFIX in raw_params: index = raw_params.find(' ' + protocol.TRAILING_PREFIX) # Get all single-word parameters. params = protocol.ARGUMENT_SEPARATOR.split(raw_params[:index].rstrip(' ')) # Extract last parameter as sentence params.append(raw_params[index + len(protocol.TRAILING_PREFIX) + 1:]) # We have some parameters, but no sentences. elif raw_params: params = protocol.ARGUMENT_SEPARATOR.split(raw_params) # No parameters. else: params = [] # Commands can be either [a-zA-Z]+ or [0-9]+. # In the former case, force it to uppercase. # In the latter case (a numeric command), try to represent it as such. try: command = int(command) except ValueError: command = command.upper() # Return parsed message. return RFC1459Message(command, params, source=source, _valid=valid, _raw=message)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:construct; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:force; 7, False; 8, block; 8, 9; 8, 11; 8, 12; 8, 21; 8, 58; 8, 66; 8, 67; 8, 77; 8, 149; 8, 150; 8, 167; 8, 168; 8, 210; 8, 211; 8, 217; 8, 257; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:command; 15, call; 15, 16; 15, 17; 16, identifier:str; 17, argument_list; 17, 18; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:command; 21, if_statement; 21, 22; 21, 34; 22, boolean_operator:and; 22, 23; 22, 32; 23, not_operator; 23, 24; 24, call; 24, 25; 24, 30; 25, attribute; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:protocol; 28, identifier:COMMAND_PATTERN; 29, identifier:match; 30, argument_list; 30, 31; 31, identifier:command; 32, not_operator; 32, 33; 33, identifier:force; 34, block; 34, 35; 35, raise_statement; 35, 36; 36, call; 36, 37; 36, 42; 37, attribute; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:pydle; 40, identifier:protocol; 41, identifier:ProtocolViolation; 42, argument_list; 42, 43; 42, 55; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, string:'The constructed command does not follow the command pattern ({pat})'; 46, identifier:format; 47, argument_list; 47, 48; 48, keyword_argument; 48, 49; 48, 50; 49, identifier:pat; 50, attribute; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:protocol; 53, identifier:COMMAND_PATTERN; 54, identifier:pattern; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:message; 57, identifier:command; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:message; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:command; 64, identifier:upper; 65, argument_list; 66, comment; 67, if_statement; 67, 68; 67, 72; 68, not_operator; 68, 69; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:params; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, augmented_assignment:+=; 74, 75; 74, 76; 75, identifier:message; 76, string:' '; 77, for_statement; 77, 78; 77, 81; 77, 87; 77, 88; 78, pattern_list; 78, 79; 78, 80; 79, identifier:idx; 80, identifier:param; 81, call; 81, 82; 81, 83; 82, identifier:enumerate; 83, argument_list; 83, 84; 84, attribute; 84, 85; 84, 86; 85, identifier:self; 86, identifier:params; 87, comment; 88, block; 88, 89; 89, if_statement; 89, 90; 89, 102; 89, 140; 89, 141; 90, boolean_operator:or; 90, 91; 90, 97; 91, boolean_operator:or; 91, 92; 91, 94; 92, not_operator; 92, 93; 93, identifier:param; 94, comparison_operator:in; 94, 95; 94, 96; 95, string:' '; 96, identifier:param; 97, comparison_operator:==; 97, 98; 97, 101; 98, subscript; 98, 99; 98, 100; 99, identifier:param; 100, integer:0; 101, string:':'; 102, block; 102, 103; 102, 130; 103, if_statement; 103, 104; 103, 117; 104, boolean_operator:and; 104, 105; 104, 115; 105, comparison_operator:<; 105, 106; 105, 109; 106, binary_operator:+; 106, 107; 106, 108; 107, identifier:idx; 108, integer:1; 109, call; 109, 110; 109, 111; 110, identifier:len; 111, argument_list; 111, 112; 112, attribute; 112, 113; 112, 114; 113, identifier:self; 114, identifier:params; 115, not_operator; 115, 116; 116, identifier:force; 117, block; 117, 118; 118, raise_statement; 118, 119; 119, call; 119, 120; 119, 125; 120, attribute; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:pydle; 123, identifier:protocol; 124, identifier:ProtocolViolation; 125, argument_list; 125, 126; 125, 127; 126, string:'Only the final parameter of an IRC message can be trailing and thus contain spaces, or start with a colon.'; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:message; 129, identifier:param; 130, expression_statement; 130, 131; 131, augmented_assignment:+=; 131, 132; 131, 133; 132, identifier:message; 133, binary_operator:+; 133, 134; 133, 139; 134, binary_operator:+; 134, 135; 134, 136; 135, string:' '; 136, attribute; 136, 137; 136, 138; 137, identifier:protocol; 138, identifier:TRAILING_PREFIX; 139, identifier:param; 140, comment; 141, else_clause; 141, 142; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, augmented_assignment:+=; 144, 145; 144, 146; 145, identifier:message; 146, binary_operator:+; 146, 147; 146, 148; 147, string:' '; 148, identifier:param; 149, comment; 150, if_statement; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:self; 153, identifier:source; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:message; 158, binary_operator:+; 158, 159; 158, 166; 159, binary_operator:+; 159, 160; 159, 165; 160, binary_operator:+; 160, 161; 160, 162; 161, string:':'; 162, attribute; 162, 163; 162, 164; 163, identifier:self; 164, identifier:source; 165, string:' '; 166, identifier:message; 167, comment; 168, if_statement; 168, 169; 168, 183; 169, boolean_operator:and; 169, 170; 169, 181; 170, call; 170, 171; 170, 172; 171, identifier:any; 172, generator_expression; 172, 173; 172, 176; 173, comparison_operator:in; 173, 174; 173, 175; 174, identifier:ch; 175, identifier:message; 176, for_in_clause; 176, 177; 176, 178; 177, identifier:ch; 178, attribute; 178, 179; 178, 180; 179, identifier:protocol; 180, identifier:FORBIDDEN_CHARACTERS; 181, not_operator; 181, 182; 182, identifier:force; 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:pydle; 189, identifier:protocol; 190, identifier:ProtocolViolation; 191, argument_list; 191, 192; 191, 207; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, string:'The constructed message contains forbidden characters ({chs}).'; 195, identifier:format; 196, argument_list; 196, 197; 197, keyword_argument; 197, 198; 197, 199; 198, identifier:chs; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, string:', '; 202, identifier:join; 203, argument_list; 203, 204; 204, attribute; 204, 205; 204, 206; 205, identifier:protocol; 206, identifier:FORBIDDEN_CHARACTERS; 207, keyword_argument; 207, 208; 207, 209; 208, identifier:message; 209, identifier:message; 210, comment; 211, expression_statement; 211, 212; 212, augmented_assignment:+=; 212, 213; 212, 214; 213, identifier:message; 214, attribute; 214, 215; 214, 216; 215, identifier:protocol; 216, identifier:LINE_SEPARATOR; 217, if_statement; 217, 218; 217, 229; 218, boolean_operator:and; 218, 219; 218, 227; 219, comparison_operator:>; 219, 220; 219, 224; 220, call; 220, 221; 220, 222; 221, identifier:len; 222, argument_list; 222, 223; 223, identifier:message; 224, attribute; 224, 225; 224, 226; 225, identifier:protocol; 226, identifier:MESSAGE_LENGTH_LIMIT; 227, not_operator; 227, 228; 228, identifier:force; 229, block; 229, 230; 230, raise_statement; 230, 231; 231, call; 231, 232; 231, 237; 232, attribute; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:pydle; 235, identifier:protocol; 236, identifier:ProtocolViolation; 237, argument_list; 237, 238; 237, 254; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, string:'The constructed message is too long. ({len} > {maxlen})'; 241, identifier:format; 242, argument_list; 242, 243; 242, 249; 243, keyword_argument; 243, 244; 243, 245; 244, identifier:len; 245, call; 245, 246; 245, 247; 246, identifier:len; 247, argument_list; 247, 248; 248, identifier:message; 249, keyword_argument; 249, 250; 249, 251; 250, identifier:maxlen; 251, attribute; 251, 252; 251, 253; 252, identifier:protocol; 253, identifier:MESSAGE_LENGTH_LIMIT; 254, keyword_argument; 254, 255; 254, 256; 255, identifier:message; 256, identifier:message; 257, return_statement; 257, 258; 258, identifier:message
def construct(self, force=False): """ Construct a raw IRC message. """ # Sanity check for command. command = str(self.command) if not protocol.COMMAND_PATTERN.match(command) and not force: raise pydle.protocol.ProtocolViolation('The constructed command does not follow the command pattern ({pat})'.format(pat=protocol.COMMAND_PATTERN.pattern), message=command) message = command.upper() # Add parameters. if not self.params: message += ' ' for idx, param in enumerate(self.params): # Trailing parameter? if not param or ' ' in param or param[0] == ':': if idx + 1 < len(self.params) and not force: raise pydle.protocol.ProtocolViolation('Only the final parameter of an IRC message can be trailing and thus contain spaces, or start with a colon.', message=param) message += ' ' + protocol.TRAILING_PREFIX + param # Regular parameter. else: message += ' ' + param # Prepend source. if self.source: message = ':' + self.source + ' ' + message # Sanity check for characters. if any(ch in message for ch in protocol.FORBIDDEN_CHARACTERS) and not force: raise pydle.protocol.ProtocolViolation('The constructed message contains forbidden characters ({chs}).'.format(chs=', '.join(protocol.FORBIDDEN_CHARACTERS)), message=message) # Sanity check for length. message += protocol.LINE_SEPARATOR if len(message) > protocol.MESSAGE_LENGTH_LIMIT and not force: raise pydle.protocol.ProtocolViolation('The constructed message is too long. ({len} > {maxlen})'.format(len=len(message), maxlen=protocol.MESSAGE_LENGTH_LIMIT), message=message) return message
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:split; 3, parameters; 3, 4; 4, identifier:url; 5, block; 5, 6; 5, 8; 5, 20; 5, 29; 5, 38; 5, 52; 5, 98; 5, 106; 5, 115; 5, 124; 5, 133; 5, 283; 5, 316; 5, 331; 5, 345; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:scheme; 11, assignment; 11, 12; 11, 13; 12, identifier:netloc; 13, assignment; 13, 14; 13, 15; 14, identifier:path; 15, assignment; 15, 16; 15, 17; 16, identifier:query; 17, assignment; 17, 18; 17, 19; 18, identifier:fragment; 19, string:''; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:ip6_start; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:url; 26, identifier:find; 27, argument_list; 27, 28; 28, string:'['; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:scheme_end; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:url; 35, identifier:find; 36, argument_list; 36, 37; 37, string:':'; 38, if_statement; 38, 39; 38, 46; 39, boolean_operator:and; 39, 40; 39, 43; 40, comparison_operator:>; 40, 41; 40, 42; 41, identifier:ip6_start; 42, integer:0; 43, comparison_operator:<; 43, 44; 43, 45; 44, identifier:ip6_start; 45, identifier:scheme_end; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:scheme_end; 50, unary_operator:-; 50, 51; 51, integer:1; 52, if_statement; 52, 53; 52, 56; 53, comparison_operator:>; 53, 54; 53, 55; 54, identifier:scheme_end; 55, integer:0; 56, block; 56, 57; 57, for_statement; 57, 58; 57, 59; 57, 64; 57, 71; 58, identifier:c; 59, subscript; 59, 60; 59, 61; 60, identifier:url; 61, slice; 61, 62; 61, 63; 62, colon; 63, identifier:scheme_end; 64, block; 64, 65; 65, if_statement; 65, 66; 65, 69; 66, comparison_operator:not; 66, 67; 66, 68; 67, identifier:c; 68, identifier:SCHEME_CHARS; 69, block; 69, 70; 70, break_statement; 71, else_clause; 71, 72; 72, block; 72, 73; 72, 85; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:scheme; 76, call; 76, 77; 76, 84; 77, attribute; 77, 78; 77, 83; 78, subscript; 78, 79; 78, 80; 79, identifier:url; 80, slice; 80, 81; 80, 82; 81, colon; 82, identifier:scheme_end; 83, identifier:lower; 84, argument_list; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:rest; 88, call; 88, 89; 88, 96; 89, attribute; 89, 90; 89, 95; 90, subscript; 90, 91; 90, 92; 91, identifier:url; 92, slice; 92, 93; 92, 94; 93, identifier:scheme_end; 94, colon; 95, identifier:lstrip; 96, argument_list; 96, 97; 97, string:':/'; 98, if_statement; 98, 99; 98, 101; 99, not_operator; 99, 100; 100, identifier:scheme; 101, block; 101, 102; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:rest; 105, identifier:url; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:l_path; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:rest; 112, identifier:find; 113, argument_list; 113, 114; 114, string:'/'; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:l_query; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:rest; 121, identifier:find; 122, argument_list; 122, 123; 123, string:'?'; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:l_frag; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:rest; 130, identifier:find; 131, argument_list; 131, 132; 132, string:'#'; 133, if_statement; 133, 134; 133, 137; 133, 249; 134, comparison_operator:>; 134, 135; 134, 136; 135, identifier:l_path; 136, integer:0; 137, block; 137, 138; 138, if_statement; 138, 139; 138, 146; 138, 168; 138, 209; 138, 231; 139, boolean_operator:and; 139, 140; 139, 143; 140, comparison_operator:>; 140, 141; 140, 142; 141, identifier:l_query; 142, integer:0; 143, comparison_operator:>; 143, 144; 143, 145; 144, identifier:l_frag; 145, integer:0; 146, block; 146, 147; 146, 155; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:netloc; 150, subscript; 150, 151; 150, 152; 151, identifier:rest; 152, slice; 152, 153; 152, 154; 153, colon; 154, identifier:l_path; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:path; 158, subscript; 158, 159; 158, 160; 159, identifier:rest; 160, slice; 160, 161; 160, 162; 160, 163; 161, identifier:l_path; 162, colon; 163, call; 163, 164; 163, 165; 164, identifier:min; 165, argument_list; 165, 166; 165, 167; 166, identifier:l_query; 167, identifier:l_frag; 168, elif_clause; 168, 169; 168, 172; 169, comparison_operator:>; 169, 170; 169, 171; 170, identifier:l_query; 171, integer:0; 172, block; 172, 173; 173, if_statement; 173, 174; 173, 177; 173, 195; 174, comparison_operator:>; 174, 175; 174, 176; 175, identifier:l_query; 176, identifier:l_path; 177, block; 177, 178; 177, 186; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:netloc; 181, subscript; 181, 182; 181, 183; 182, identifier:rest; 183, slice; 183, 184; 183, 185; 184, colon; 185, identifier:l_path; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:path; 189, subscript; 189, 190; 189, 191; 190, identifier:rest; 191, slice; 191, 192; 191, 193; 191, 194; 192, identifier:l_path; 193, colon; 194, identifier:l_query; 195, else_clause; 195, 196; 196, block; 196, 197; 196, 205; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:netloc; 200, subscript; 200, 201; 200, 202; 201, identifier:rest; 202, slice; 202, 203; 202, 204; 203, colon; 204, identifier:l_query; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:path; 208, string:''; 209, elif_clause; 209, 210; 209, 213; 210, comparison_operator:>; 210, 211; 210, 212; 211, identifier:l_frag; 212, integer:0; 213, block; 213, 214; 213, 222; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:netloc; 217, subscript; 217, 218; 217, 219; 218, identifier:rest; 219, slice; 219, 220; 219, 221; 220, colon; 221, identifier:l_path; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:path; 225, subscript; 225, 226; 225, 227; 226, identifier:rest; 227, slice; 227, 228; 227, 229; 227, 230; 228, identifier:l_path; 229, colon; 230, identifier:l_frag; 231, else_clause; 231, 232; 232, block; 232, 233; 232, 241; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 236; 235, identifier:netloc; 236, subscript; 236, 237; 236, 238; 237, identifier:rest; 238, slice; 238, 239; 238, 240; 239, colon; 240, identifier:l_path; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 244; 243, identifier:path; 244, subscript; 244, 245; 244, 246; 245, identifier:rest; 246, slice; 246, 247; 246, 248; 247, identifier:l_path; 248, colon; 249, else_clause; 249, 250; 250, block; 250, 251; 251, if_statement; 251, 252; 251, 255; 251, 264; 251, 277; 252, comparison_operator:>; 252, 253; 252, 254; 253, identifier:l_query; 254, integer:0; 255, block; 255, 256; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 259; 258, identifier:netloc; 259, subscript; 259, 260; 259, 261; 260, identifier:rest; 261, slice; 261, 262; 261, 263; 262, colon; 263, identifier:l_query; 264, elif_clause; 264, 265; 264, 268; 265, comparison_operator:>; 265, 266; 265, 267; 266, identifier:l_frag; 267, integer:0; 268, block; 268, 269; 269, expression_statement; 269, 270; 270, assignment; 270, 271; 270, 272; 271, identifier:netloc; 272, subscript; 272, 273; 272, 274; 273, identifier:rest; 274, slice; 274, 275; 274, 276; 275, colon; 276, identifier:l_frag; 277, else_clause; 277, 278; 278, block; 278, 279; 279, expression_statement; 279, 280; 280, assignment; 280, 281; 280, 282; 281, identifier:netloc; 282, identifier:rest; 283, if_statement; 283, 284; 283, 287; 284, comparison_operator:>; 284, 285; 284, 286; 285, identifier:l_query; 286, integer:0; 287, block; 287, 288; 288, if_statement; 288, 289; 288, 292; 288, 304; 289, comparison_operator:>; 289, 290; 289, 291; 290, identifier:l_frag; 291, integer:0; 292, block; 292, 293; 293, expression_statement; 293, 294; 294, assignment; 294, 295; 294, 296; 295, identifier:query; 296, subscript; 296, 297; 296, 298; 297, identifier:rest; 298, slice; 298, 299; 298, 302; 298, 303; 299, binary_operator:+; 299, 300; 299, 301; 300, identifier:l_query; 301, integer:1; 302, colon; 303, identifier:l_frag; 304, else_clause; 304, 305; 305, block; 305, 306; 306, expression_statement; 306, 307; 307, assignment; 307, 308; 307, 309; 308, identifier:query; 309, subscript; 309, 310; 309, 311; 310, identifier:rest; 311, slice; 311, 312; 311, 315; 312, binary_operator:+; 312, 313; 312, 314; 313, identifier:l_query; 314, integer:1; 315, colon; 316, if_statement; 316, 317; 316, 320; 317, comparison_operator:>; 317, 318; 317, 319; 318, identifier:l_frag; 319, integer:0; 320, block; 320, 321; 321, expression_statement; 321, 322; 322, assignment; 322, 323; 322, 324; 323, identifier:fragment; 324, subscript; 324, 325; 324, 326; 325, identifier:rest; 326, slice; 326, 327; 326, 330; 327, binary_operator:+; 327, 328; 327, 329; 328, identifier:l_frag; 329, integer:1; 330, colon; 331, if_statement; 331, 332; 331, 334; 332, not_operator; 332, 333; 333, identifier:scheme; 334, block; 334, 335; 334, 341; 335, expression_statement; 335, 336; 336, assignment; 336, 337; 336, 338; 337, identifier:path; 338, binary_operator:+; 338, 339; 338, 340; 339, identifier:netloc; 340, identifier:path; 341, expression_statement; 341, 342; 342, assignment; 342, 343; 342, 344; 343, identifier:netloc; 344, string:''; 345, return_statement; 345, 346; 346, call; 346, 347; 346, 348; 347, identifier:SplitResult; 348, argument_list; 348, 349; 348, 350; 348, 351; 348, 352; 348, 353; 349, identifier:scheme; 350, identifier:netloc; 351, identifier:path; 352, identifier:query; 353, identifier:fragment
def split(url): """Split URL into scheme, netloc, path, query and fragment. >>> split('http://www.example.com/abc?x=1&y=2#foo') SplitResult(scheme='http', netloc='www.example.com', path='/abc', query='x=1&y=2', fragment='foo') """ scheme = netloc = path = query = fragment = '' ip6_start = url.find('[') scheme_end = url.find(':') if ip6_start > 0 and ip6_start < scheme_end: scheme_end = -1 if scheme_end > 0: for c in url[:scheme_end]: if c not in SCHEME_CHARS: break else: scheme = url[:scheme_end].lower() rest = url[scheme_end:].lstrip(':/') if not scheme: rest = url l_path = rest.find('/') l_query = rest.find('?') l_frag = rest.find('#') if l_path > 0: if l_query > 0 and l_frag > 0: netloc = rest[:l_path] path = rest[l_path:min(l_query, l_frag)] elif l_query > 0: if l_query > l_path: netloc = rest[:l_path] path = rest[l_path:l_query] else: netloc = rest[:l_query] path = '' elif l_frag > 0: netloc = rest[:l_path] path = rest[l_path:l_frag] else: netloc = rest[:l_path] path = rest[l_path:] else: if l_query > 0: netloc = rest[:l_query] elif l_frag > 0: netloc = rest[:l_frag] else: netloc = rest if l_query > 0: if l_frag > 0: query = rest[l_query+1:l_frag] else: query = rest[l_query+1:] if l_frag > 0: fragment = rest[l_frag+1:] if not scheme: path = netloc + path netloc = '' return SplitResult(scheme, netloc, path, query, fragment)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:issue_cmd; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:cmd; 6, identifier:value; 7, default_parameter; 7, 8; 7, 9; 8, identifier:retry; 9, integer:3; 10, block; 10, 11; 10, 13; 11, expression_statement; 11, 12; 12, comment; 13, with_statement; 13, 14; 13, 19; 14, with_clause; 14, 15; 15, with_item; 15, 16; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:_cmd_lock; 19, block; 19, 20; 19, 35; 19, 62; 19, 71; 19, 91; 19, 117; 19, 159; 19, 348; 20, if_statement; 20, 21; 20, 25; 21, not_operator; 21, 22; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:connected; 25, block; 25, 26; 25, 34; 26, expression_statement; 26, 27; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:_LOGGER; 30, identifier:debug; 31, argument_list; 31, 32; 31, 33; 32, string:"Serial transport closed, not sending command %s"; 33, identifier:cmd; 34, return_statement; 35, while_statement; 35, 36; 35, 44; 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:self; 41, identifier:_cmdq; 42, identifier:empty; 43, argument_list; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:_LOGGER; 49, identifier:debug; 50, argument_list; 50, 51; 50, 54; 51, concatenated_string; 51, 52; 51, 53; 52, string:"Clearing leftover message from command queue:"; 53, string:" %s"; 54, await; 54, 55; 55, call; 55, 56; 55, 61; 56, attribute; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:_cmdq; 60, identifier:get; 61, argument_list; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:_LOGGER; 66, identifier:debug; 67, argument_list; 67, 68; 67, 69; 67, 70; 68, string:"Sending command: %s with value %s"; 69, identifier:cmd; 70, identifier:value; 71, expression_statement; 71, 72; 72, call; 72, 73; 72, 78; 73, attribute; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:self; 76, identifier:transport; 77, identifier:write; 78, argument_list; 78, 79; 79, call; 79, 80; 79, 89; 80, attribute; 80, 81; 80, 88; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, string:'{}={}\r\n'; 84, identifier:format; 85, argument_list; 85, 86; 85, 87; 86, identifier:cmd; 87, identifier:value; 88, identifier:encode; 89, argument_list; 89, 90; 90, string:'ascii'; 91, if_statement; 91, 92; 91, 95; 91, 106; 92, comparison_operator:==; 92, 93; 92, 94; 93, identifier:cmd; 94, identifier:OTGW_CMD_REPORT; 95, block; 95, 96; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:expect; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, string:r'^{}:\s*([A-Z]{{2}}|{}=[^$]+)$'; 102, identifier:format; 103, argument_list; 103, 104; 103, 105; 104, identifier:cmd; 105, identifier:value; 106, else_clause; 106, 107; 107, block; 107, 108; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:expect; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, string:r'^{}:\s*([^$]+)$'; 114, identifier:format; 115, argument_list; 115, 116; 116, identifier:cmd; 117, function_definition; 117, 118; 117, 119; 117, 121; 118, function_name:send_again; 119, parameters; 119, 120; 120, identifier:err; 121, block; 121, 122; 121, 124; 121, 126; 121, 135; 121, 139; 122, expression_statement; 122, 123; 123, comment; 124, nonlocal_statement; 124, 125; 125, identifier:retry; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:_LOGGER; 130, identifier:warning; 131, argument_list; 131, 132; 131, 133; 131, 134; 132, string:"Command %s failed with %s, retrying..."; 133, identifier:cmd; 134, identifier:err; 135, expression_statement; 135, 136; 136, augmented_assignment:-=; 136, 137; 136, 138; 137, identifier:retry; 138, integer:1; 139, expression_statement; 139, 140; 140, call; 140, 141; 140, 146; 141, attribute; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:self; 144, identifier:transport; 145, identifier:write; 146, argument_list; 146, 147; 147, call; 147, 148; 147, 157; 148, attribute; 148, 149; 148, 156; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, string:'{}={}\r\n'; 152, identifier:format; 153, argument_list; 153, 154; 153, 155; 154, identifier:cmd; 155, identifier:value; 156, identifier:encode; 157, argument_list; 157, 158; 158, string:'ascii'; 159, function_definition; 159, 160; 159, 161; 159, 163; 160, function_name:process; 161, parameters; 161, 162; 162, identifier:msg; 163, block; 163, 164; 163, 166; 163, 175; 163, 197; 163, 230; 163, 240; 163, 313; 163, 334; 163, 342; 164, expression_statement; 164, 165; 165, comment; 166, expression_statement; 166, 167; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:_LOGGER; 170, identifier:debug; 171, argument_list; 171, 172; 171, 173; 171, 174; 172, string:"Got possible response for command %s: %s"; 173, identifier:cmd; 174, identifier:msg; 175, if_statement; 175, 176; 175, 179; 175, 180; 176, comparison_operator:in; 176, 177; 176, 178; 177, identifier:msg; 178, identifier:OTGW_ERRS; 179, comment; 180, block; 180, 181; 180, 190; 180, 196; 181, if_statement; 181, 182; 181, 185; 182, comparison_operator:==; 182, 183; 182, 184; 183, identifier:retry; 184, integer:0; 185, block; 185, 186; 186, raise_statement; 186, 187; 187, subscript; 187, 188; 187, 189; 188, identifier:OTGW_ERRS; 189, identifier:msg; 190, expression_statement; 190, 191; 191, await; 191, 192; 192, call; 192, 193; 192, 194; 193, identifier:send_again; 194, argument_list; 194, 195; 195, identifier:msg; 196, return_statement; 197, if_statement; 197, 198; 197, 205; 197, 206; 198, boolean_operator:and; 198, 199; 198, 202; 199, comparison_operator:==; 199, 200; 199, 201; 200, identifier:cmd; 201, identifier:OTGW_CMD_MODE; 202, comparison_operator:==; 202, 203; 202, 204; 203, identifier:value; 204, string:'R'; 205, comment; 206, block; 206, 207; 206, 228; 207, while_statement; 207, 208; 207, 216; 208, not_operator; 208, 209; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:re; 212, identifier:match; 213, argument_list; 213, 214; 213, 215; 214, string:r'OpenTherm Gateway \d+\.\d+\.\d+'; 215, identifier:msg; 216, block; 216, 217; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:msg; 220, await; 220, 221; 221, call; 221, 222; 221, 227; 222, attribute; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:self; 225, identifier:_cmdq; 226, identifier:get; 227, argument_list; 228, return_statement; 228, 229; 229, True; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:match; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:re; 236, identifier:match; 237, argument_list; 237, 238; 237, 239; 238, identifier:expect; 239, identifier:msg; 240, if_statement; 240, 241; 240, 242; 241, identifier:match; 242, block; 242, 243; 242, 275; 242, 284; 242, 311; 243, if_statement; 243, 244; 243, 252; 243, 253; 244, comparison_operator:in; 244, 245; 244, 251; 245, call; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:match; 248, identifier:group; 249, argument_list; 249, 250; 250, integer:1; 251, identifier:OTGW_ERRS; 252, comment; 253, block; 253, 254; 253, 268; 253, 274; 254, if_statement; 254, 255; 254, 258; 255, comparison_operator:==; 255, 256; 255, 257; 256, identifier:retry; 257, integer:0; 258, block; 258, 259; 259, raise_statement; 259, 260; 260, subscript; 260, 261; 260, 262; 261, identifier:OTGW_ERRS; 262, call; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:match; 265, identifier:group; 266, argument_list; 266, 267; 267, integer:1; 268, expression_statement; 268, 269; 269, await; 269, 270; 270, call; 270, 271; 270, 272; 271, identifier:send_again; 272, argument_list; 272, 273; 273, identifier:msg; 274, return_statement; 275, expression_statement; 275, 276; 276, assignment; 276, 277; 276, 278; 277, identifier:ret; 278, call; 278, 279; 278, 282; 279, attribute; 279, 280; 279, 281; 280, identifier:match; 281, identifier:group; 282, argument_list; 282, 283; 283, integer:1; 284, if_statement; 284, 285; 284, 292; 284, 293; 285, boolean_operator:and; 285, 286; 285, 289; 286, comparison_operator:==; 286, 287; 286, 288; 287, identifier:cmd; 288, identifier:OTGW_CMD_SUMMARY; 289, comparison_operator:==; 289, 290; 289, 291; 290, identifier:ret; 291, string:'1'; 292, comment; 293, block; 293, 294; 293, 305; 294, expression_statement; 294, 295; 295, assignment; 295, 296; 295, 297; 296, identifier:part2; 297, await; 297, 298; 298, call; 298, 299; 298, 304; 299, attribute; 299, 300; 299, 303; 300, attribute; 300, 301; 300, 302; 301, identifier:self; 302, identifier:_cmdq; 303, identifier:get; 304, argument_list; 305, expression_statement; 305, 306; 306, assignment; 306, 307; 306, 308; 307, identifier:ret; 308, list:[ret, part2]; 308, 309; 308, 310; 309, identifier:ret; 310, identifier:part2; 311, return_statement; 311, 312; 312, identifier:ret; 313, if_statement; 313, 314; 313, 321; 314, call; 314, 315; 314, 318; 315, attribute; 315, 316; 315, 317; 316, identifier:re; 317, identifier:match; 318, argument_list; 318, 319; 318, 320; 319, string:r'Error 0[1-4]'; 320, identifier:msg; 321, block; 321, 322; 321, 333; 322, expression_statement; 322, 323; 323, call; 323, 324; 323, 327; 324, attribute; 324, 325; 324, 326; 325, identifier:_LOGGER; 326, identifier:warning; 327, argument_list; 327, 328; 327, 332; 328, concatenated_string; 328, 329; 328, 330; 328, 331; 329, string:"Received %s. If this happens during a "; 330, string:"reset of the gateway it can be safely "; 331, string:"ignored."; 332, identifier:msg; 333, return_statement; 334, expression_statement; 334, 335; 335, call; 335, 336; 335, 339; 336, attribute; 336, 337; 336, 338; 337, identifier:_LOGGER; 338, identifier:warning; 339, argument_list; 339, 340; 339, 341; 340, string:"Unknown message in command queue: %s"; 341, identifier:msg; 342, expression_statement; 342, 343; 343, await; 343, 344; 344, call; 344, 345; 344, 346; 345, identifier:send_again; 346, argument_list; 346, 347; 347, identifier:msg; 348, while_statement; 348, 349; 348, 350; 349, True; 350, block; 350, 351; 350, 362; 350, 370; 351, expression_statement; 351, 352; 352, assignment; 352, 353; 352, 354; 353, identifier:msg; 354, await; 354, 355; 355, call; 355, 356; 355, 361; 356, attribute; 356, 357; 356, 360; 357, attribute; 357, 358; 357, 359; 358, identifier:self; 359, identifier:_cmdq; 360, identifier:get; 361, argument_list; 362, expression_statement; 362, 363; 363, assignment; 363, 364; 363, 365; 364, identifier:ret; 365, await; 365, 366; 366, call; 366, 367; 366, 368; 367, identifier:process; 368, argument_list; 368, 369; 369, identifier:msg; 370, if_statement; 370, 371; 370, 374; 371, comparison_operator:is; 371, 372; 371, 373; 372, identifier:ret; 373, None; 374, block; 374, 375; 375, return_statement; 375, 376; 376, identifier:ret
async def issue_cmd(self, cmd, value, retry=3): """ Issue a command, then await and return the return value. This method is a coroutine """ async with self._cmd_lock: if not self.connected: _LOGGER.debug( "Serial transport closed, not sending command %s", cmd) return while not self._cmdq.empty(): _LOGGER.debug("Clearing leftover message from command queue:" " %s", await self._cmdq.get()) _LOGGER.debug("Sending command: %s with value %s", cmd, value) self.transport.write( '{}={}\r\n'.format(cmd, value).encode('ascii')) if cmd == OTGW_CMD_REPORT: expect = r'^{}:\s*([A-Z]{{2}}|{}=[^$]+)$'.format(cmd, value) else: expect = r'^{}:\s*([^$]+)$'.format(cmd) async def send_again(err): """Resend the command.""" nonlocal retry _LOGGER.warning("Command %s failed with %s, retrying...", cmd, err) retry -= 1 self.transport.write( '{}={}\r\n'.format(cmd, value).encode('ascii')) async def process(msg): """Process a possible response.""" _LOGGER.debug("Got possible response for command %s: %s", cmd, msg) if msg in OTGW_ERRS: # Some errors appear by themselves on one line. if retry == 0: raise OTGW_ERRS[msg] await send_again(msg) return if cmd == OTGW_CMD_MODE and value == 'R': # Device was reset, msg contains build info while not re.match( r'OpenTherm Gateway \d+\.\d+\.\d+', msg): msg = await self._cmdq.get() return True match = re.match(expect, msg) if match: if match.group(1) in OTGW_ERRS: # Some errors are considered a response. if retry == 0: raise OTGW_ERRS[match.group(1)] await send_again(msg) return ret = match.group(1) if cmd == OTGW_CMD_SUMMARY and ret == '1': # Expects a second line part2 = await self._cmdq.get() ret = [ret, part2] return ret if re.match(r'Error 0[1-4]', msg): _LOGGER.warning("Received %s. If this happens during a " "reset of the gateway it can be safely " "ignored.", msg) return _LOGGER.warning("Unknown message in command queue: %s", msg) await send_again(msg) while True: msg = await self._cmdq.get() ret = await process(msg) if ret is not None: return ret
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_reports; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 12; 5, 16; 5, 57; 5, 117; 5, 126; 5, 152; 5, 161; 5, 189; 5, 198; 5, 240; 5, 249; 5, 277; 5, 286; 5, 304; 5, 313; 5, 331; 5, 356; 5, 363; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:cmd; 11, identifier:OTGW_CMD_REPORT; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:reports; 15, dictionary; 16, for_statement; 16, 17; 16, 18; 16, 23; 17, identifier:value; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:OTGW_REPORTS; 21, identifier:keys; 22, argument_list; 23, block; 23, 24; 23, 35; 23, 47; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:ret; 27, await; 27, 28; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:_wait_for_cmd; 32, argument_list; 32, 33; 32, 34; 33, identifier:cmd; 34, identifier:value; 35, if_statement; 35, 36; 35, 39; 36, comparison_operator:is; 36, 37; 36, 38; 37, identifier:ret; 38, None; 39, block; 39, 40; 39, 46; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 45; 42, subscript; 42, 43; 42, 44; 43, identifier:reports; 44, identifier:value; 45, None; 46, continue_statement; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 52; 49, subscript; 49, 50; 49, 51; 50, identifier:reports; 51, identifier:value; 52, subscript; 52, 53; 52, 54; 53, identifier:ret; 54, slice; 54, 55; 54, 56; 55, integer:2; 56, colon; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:status; 60, dictionary; 60, 61; 60, 69; 60, 77; 60, 85; 60, 93; 60, 101; 60, 109; 61, pair; 61, 62; 61, 63; 62, identifier:OTGW_ABOUT; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:reports; 66, identifier:get; 67, argument_list; 67, 68; 68, identifier:OTGW_REPORT_ABOUT; 69, pair; 69, 70; 69, 71; 70, identifier:OTGW_BUILD; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:reports; 74, identifier:get; 75, argument_list; 75, 76; 76, identifier:OTGW_REPORT_BUILDDATE; 77, pair; 77, 78; 77, 79; 78, identifier:OTGW_CLOCKMHZ; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:reports; 82, identifier:get; 83, argument_list; 83, 84; 84, identifier:OTGW_REPORT_CLOCKMHZ; 85, pair; 85, 86; 85, 87; 86, identifier:OTGW_MODE; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:reports; 90, identifier:get; 91, argument_list; 91, 92; 92, identifier:OTGW_REPORT_GW_MODE; 93, pair; 93, 94; 93, 95; 94, identifier:OTGW_SMART_PWR; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:reports; 98, identifier:get; 99, argument_list; 99, 100; 100, identifier:OTGW_REPORT_SMART_PWR; 101, pair; 101, 102; 101, 103; 102, identifier:OTGW_THRM_DETECT; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:reports; 106, identifier:get; 107, argument_list; 107, 108; 108, identifier:OTGW_REPORT_THERMOSTAT_DETECT; 109, pair; 109, 110; 109, 111; 110, identifier:OTGW_DHW_OVRD; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:reports; 114, identifier:get; 115, argument_list; 115, 116; 116, identifier:OTGW_REPORT_DHW_SETTING; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:ovrd_mode; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:reports; 123, identifier:get; 124, argument_list; 124, 125; 125, identifier:OTGW_REPORT_SETPOINT_OVRD; 126, if_statement; 126, 127; 126, 130; 127, comparison_operator:is; 127, 128; 127, 129; 128, identifier:ovrd_mode; 129, None; 130, block; 130, 131; 130, 142; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:ovrd_mode; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:str; 137, identifier:upper; 138, argument_list; 138, 139; 139, subscript; 139, 140; 139, 141; 140, identifier:ovrd_mode; 141, integer:0; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:status; 146, identifier:update; 147, argument_list; 147, 148; 148, dictionary; 148, 149; 149, pair; 149, 150; 149, 151; 150, identifier:OTGW_SETP_OVRD_MODE; 151, identifier:ovrd_mode; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:gpio_funcs; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:reports; 158, identifier:get; 159, argument_list; 159, 160; 160, identifier:OTGW_REPORT_GPIO_FUNCS; 161, if_statement; 161, 162; 161, 165; 162, comparison_operator:is; 162, 163; 162, 164; 163, identifier:gpio_funcs; 164, None; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:status; 170, identifier:update; 171, argument_list; 171, 172; 172, dictionary; 172, 173; 172, 181; 173, pair; 173, 174; 173, 175; 174, identifier:OTGW_GPIO_A; 175, call; 175, 176; 175, 177; 176, identifier:int; 177, argument_list; 177, 178; 178, subscript; 178, 179; 178, 180; 179, identifier:gpio_funcs; 180, integer:0; 181, pair; 181, 182; 181, 183; 182, identifier:OTGW_GPIO_B; 183, call; 183, 184; 183, 185; 184, identifier:int; 185, argument_list; 185, 186; 186, subscript; 186, 187; 186, 188; 187, identifier:gpio_funcs; 188, integer:1; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:led_funcs; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:reports; 195, identifier:get; 196, argument_list; 196, 197; 197, identifier:OTGW_REPORT_LED_FUNCS; 198, if_statement; 198, 199; 198, 202; 199, comparison_operator:is; 199, 200; 199, 201; 200, identifier:led_funcs; 201, None; 202, block; 202, 203; 203, expression_statement; 203, 204; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:status; 207, identifier:update; 208, argument_list; 208, 209; 209, dictionary; 209, 210; 209, 215; 209, 220; 209, 225; 209, 230; 209, 235; 210, pair; 210, 211; 210, 212; 211, identifier:OTGW_LED_A; 212, subscript; 212, 213; 212, 214; 213, identifier:led_funcs; 214, integer:0; 215, pair; 215, 216; 215, 217; 216, identifier:OTGW_LED_B; 217, subscript; 217, 218; 217, 219; 218, identifier:led_funcs; 219, integer:1; 220, pair; 220, 221; 220, 222; 221, identifier:OTGW_LED_C; 222, subscript; 222, 223; 222, 224; 223, identifier:led_funcs; 224, integer:2; 225, pair; 225, 226; 225, 227; 226, identifier:OTGW_LED_D; 227, subscript; 227, 228; 227, 229; 228, identifier:led_funcs; 229, integer:3; 230, pair; 230, 231; 230, 232; 231, identifier:OTGW_LED_E; 232, subscript; 232, 233; 232, 234; 233, identifier:led_funcs; 234, integer:4; 235, pair; 235, 236; 235, 237; 236, identifier:OTGW_LED_F; 237, subscript; 237, 238; 237, 239; 238, identifier:led_funcs; 239, integer:5; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:tweaks; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:reports; 246, identifier:get; 247, argument_list; 247, 248; 248, identifier:OTGW_REPORT_TWEAKS; 249, if_statement; 249, 250; 249, 253; 250, comparison_operator:is; 250, 251; 250, 252; 251, identifier:tweaks; 252, None; 253, block; 253, 254; 254, expression_statement; 254, 255; 255, call; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:status; 258, identifier:update; 259, argument_list; 259, 260; 260, dictionary; 260, 261; 260, 269; 261, pair; 261, 262; 261, 263; 262, identifier:OTGW_IGNORE_TRANSITIONS; 263, call; 263, 264; 263, 265; 264, identifier:int; 265, argument_list; 265, 266; 266, subscript; 266, 267; 266, 268; 267, identifier:tweaks; 268, integer:0; 269, pair; 269, 270; 269, 271; 270, identifier:OTGW_OVRD_HB; 271, call; 271, 272; 271, 273; 272, identifier:int; 273, argument_list; 273, 274; 274, subscript; 274, 275; 274, 276; 275, identifier:tweaks; 276, integer:1; 277, expression_statement; 277, 278; 278, assignment; 278, 279; 278, 280; 279, identifier:sb_temp; 280, call; 280, 281; 280, 284; 281, attribute; 281, 282; 281, 283; 282, identifier:reports; 283, identifier:get; 284, argument_list; 284, 285; 285, identifier:OTGW_REPORT_SETBACK_TEMP; 286, if_statement; 286, 287; 286, 290; 287, comparison_operator:is; 287, 288; 287, 289; 288, identifier:sb_temp; 289, None; 290, block; 290, 291; 291, expression_statement; 291, 292; 292, call; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:status; 295, identifier:update; 296, argument_list; 296, 297; 297, dictionary; 297, 298; 298, pair; 298, 299; 298, 300; 299, identifier:OTGW_SB_TEMP; 300, call; 300, 301; 300, 302; 301, identifier:float; 302, argument_list; 302, 303; 303, identifier:sb_temp; 304, expression_statement; 304, 305; 305, assignment; 305, 306; 305, 307; 306, identifier:vref; 307, call; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:reports; 310, identifier:get; 311, argument_list; 311, 312; 312, identifier:OTGW_REPORT_VREF; 313, if_statement; 313, 314; 313, 317; 314, comparison_operator:is; 314, 315; 314, 316; 315, identifier:vref; 316, None; 317, block; 317, 318; 318, expression_statement; 318, 319; 319, call; 319, 320; 319, 323; 320, attribute; 320, 321; 320, 322; 321, identifier:status; 322, identifier:update; 323, argument_list; 323, 324; 324, dictionary; 324, 325; 325, pair; 325, 326; 325, 327; 326, identifier:OTGW_VREF; 327, call; 327, 328; 327, 329; 328, identifier:int; 329, argument_list; 329, 330; 330, identifier:vref; 331, if_statement; 331, 332; 331, 340; 332, parenthesized_expression; 332, 333; 333, boolean_operator:and; 333, 334; 333, 337; 334, comparison_operator:is; 334, 335; 334, 336; 335, identifier:ovrd_mode; 336, None; 337, comparison_operator:!=; 337, 338; 337, 339; 338, identifier:ovrd_mode; 339, identifier:OTGW_SETP_OVRD_DISABLED; 340, block; 340, 341; 341, expression_statement; 341, 342; 342, assignment; 342, 343; 342, 346; 343, subscript; 343, 344; 343, 345; 344, identifier:status; 345, identifier:DATA_ROOM_SETPOINT_OVRD; 346, call; 346, 347; 346, 348; 347, identifier:float; 348, argument_list; 348, 349; 349, subscript; 349, 350; 349, 353; 350, subscript; 350, 351; 350, 352; 351, identifier:reports; 352, identifier:OTGW_REPORT_SETPOINT_OVRD; 353, slice; 353, 354; 353, 355; 354, integer:1; 355, colon; 356, expression_statement; 356, 357; 357, call; 357, 358; 357, 361; 358, attribute; 358, 359; 358, 360; 359, identifier:self; 360, identifier:_update_status; 361, argument_list; 361, 362; 362, identifier:status; 363, return_statement; 363, 364; 364, call; 364, 365; 364, 366; 365, identifier:dict; 366, argument_list; 366, 367; 367, attribute; 367, 368; 367, 371; 368, attribute; 368, 369; 368, 370; 369, identifier:self; 370, identifier:_protocol; 371, identifier:status
async def get_reports(self): """ Update the pyotgw object with the information from all of the PR commands and return the updated status dict. This method is a coroutine """ cmd = OTGW_CMD_REPORT reports = {} for value in OTGW_REPORTS.keys(): ret = await self._wait_for_cmd(cmd, value) if ret is None: reports[value] = None continue reports[value] = ret[2:] status = { OTGW_ABOUT: reports.get(OTGW_REPORT_ABOUT), OTGW_BUILD: reports.get(OTGW_REPORT_BUILDDATE), OTGW_CLOCKMHZ: reports.get(OTGW_REPORT_CLOCKMHZ), OTGW_MODE: reports.get(OTGW_REPORT_GW_MODE), OTGW_SMART_PWR: reports.get(OTGW_REPORT_SMART_PWR), OTGW_THRM_DETECT: reports.get(OTGW_REPORT_THERMOSTAT_DETECT), OTGW_DHW_OVRD: reports.get(OTGW_REPORT_DHW_SETTING), } ovrd_mode = reports.get(OTGW_REPORT_SETPOINT_OVRD) if ovrd_mode is not None: ovrd_mode = str.upper(ovrd_mode[0]) status.update({OTGW_SETP_OVRD_MODE: ovrd_mode}) gpio_funcs = reports.get(OTGW_REPORT_GPIO_FUNCS) if gpio_funcs is not None: status.update({ OTGW_GPIO_A: int(gpio_funcs[0]), OTGW_GPIO_B: int(gpio_funcs[1]), }) led_funcs = reports.get(OTGW_REPORT_LED_FUNCS) if led_funcs is not None: status.update({ OTGW_LED_A: led_funcs[0], OTGW_LED_B: led_funcs[1], OTGW_LED_C: led_funcs[2], OTGW_LED_D: led_funcs[3], OTGW_LED_E: led_funcs[4], OTGW_LED_F: led_funcs[5], }) tweaks = reports.get(OTGW_REPORT_TWEAKS) if tweaks is not None: status.update({ OTGW_IGNORE_TRANSITIONS: int(tweaks[0]), OTGW_OVRD_HB: int(tweaks[1]), }) sb_temp = reports.get(OTGW_REPORT_SETBACK_TEMP) if sb_temp is not None: status.update({OTGW_SB_TEMP: float(sb_temp)}) vref = reports.get(OTGW_REPORT_VREF) if vref is not None: status.update({OTGW_VREF: int(vref)}) if (ovrd_mode is not None and ovrd_mode != OTGW_SETP_OVRD_DISABLED): status[DATA_ROOM_SETPOINT_OVRD] = float( reports[OTGW_REPORT_SETPOINT_OVRD][1:]) self._update_status(status) return dict(self._protocol.status)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:build; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:self; 5, identifier:client; 6, default_parameter; 6, 7; 6, 8; 7, identifier:nobuild; 8, False; 9, default_parameter; 9, 10; 9, 11; 10, identifier:usecache; 11, True; 12, default_parameter; 12, 13; 12, 14; 13, identifier:pull; 14, False; 15, block; 15, 16; 15, 18; 15, 35; 15, 43; 15, 59; 15, 79; 15, 95; 15, 229; 15, 235; 16, expression_statement; 16, 17; 17, comment; 18, if_statement; 18, 19; 18, 21; 19, not_operator; 19, 20; 20, identifier:nobuild; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:update_source_images; 27, argument_list; 27, 28; 27, 29; 27, 32; 28, identifier:client; 29, keyword_argument; 29, 30; 29, 31; 30, identifier:usecache; 31, identifier:usecache; 32, keyword_argument; 32, 33; 32, 34; 33, identifier:pull; 34, identifier:pull; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:width; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:utils; 41, identifier:get_console_width; 42, argument_list; 43, expression_statement; 43, 44; 44, call; 44, 45; 44, 46; 45, identifier:cprint; 46, argument_list; 46, 47; 46, 52; 46, 55; 47, binary_operator:+; 47, 48; 47, 49; 48, string:'\n'; 49, binary_operator:*; 49, 50; 49, 51; 50, string:'='; 51, identifier:width; 52, keyword_argument; 52, 53; 52, 54; 53, identifier:color; 54, string:'white'; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:attrs; 57, list:['bold']; 57, 58; 58, string:'bold'; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:line; 62, binary_operator:%; 62, 63; 62, 64; 63, string:'STARTING BUILD for "%s" (image definition "%s" from %s)\n'; 64, tuple; 64, 65; 64, 68; 64, 71; 65, attribute; 65, 66; 65, 67; 66, identifier:self; 67, identifier:targetname; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:imagename; 71, attribute; 71, 72; 71, 78; 72, subscript; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:self; 75, identifier:steps; 76, unary_operator:-; 76, 77; 77, integer:1; 78, identifier:sourcefile; 79, expression_statement; 79, 80; 80, call; 80, 81; 80, 82; 81, identifier:cprint; 82, argument_list; 82, 83; 82, 88; 82, 91; 83, call; 83, 84; 83, 85; 84, identifier:_centered; 85, argument_list; 85, 86; 85, 87; 86, identifier:line; 87, identifier:width; 88, keyword_argument; 88, 89; 88, 90; 89, identifier:color; 90, string:'blue'; 91, keyword_argument; 91, 92; 91, 93; 92, identifier:attrs; 93, list:['bold']; 93, 94; 94, string:'bold'; 95, for_statement; 95, 96; 95, 99; 95, 105; 96, pattern_list; 96, 97; 96, 98; 97, identifier:istep; 98, identifier:step; 99, call; 99, 100; 99, 101; 100, identifier:enumerate; 101, argument_list; 101, 102; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:steps; 105, block; 105, 106; 105, 155; 106, expression_statement; 106, 107; 107, call; 107, 108; 107, 109; 108, identifier:print; 109, argument_list; 109, 110; 109, 115; 109, 135; 109, 142; 110, call; 110, 111; 110, 112; 111, identifier:colored; 112, argument_list; 112, 113; 112, 114; 113, string:'* Step'; 114, string:'blue'; 115, call; 115, 116; 115, 117; 116, identifier:colored; 117, argument_list; 117, 118; 117, 130; 117, 131; 118, binary_operator:%; 118, 119; 118, 120; 119, string:'%d/%d'; 120, tuple; 120, 121; 120, 124; 121, binary_operator:+; 121, 122; 121, 123; 122, identifier:istep; 123, integer:1; 124, call; 124, 125; 124, 126; 125, identifier:len; 126, argument_list; 126, 127; 127, attribute; 127, 128; 127, 129; 128, identifier:self; 129, identifier:steps; 130, string:'blue'; 131, keyword_argument; 131, 132; 131, 133; 132, identifier:attrs; 133, list:['bold']; 133, 134; 134, string:'bold'; 135, call; 135, 136; 135, 137; 136, identifier:colored; 137, argument_list; 137, 138; 137, 139; 138, string:'for image'; 139, keyword_argument; 139, 140; 139, 141; 140, identifier:color; 141, string:'blue'; 142, call; 142, 143; 142, 144; 143, identifier:colored; 144, argument_list; 144, 145; 144, 148; 144, 151; 145, attribute; 145, 146; 145, 147; 146, identifier:self; 147, identifier:imagename; 148, keyword_argument; 148, 149; 148, 150; 149, identifier:color; 150, string:'blue'; 151, keyword_argument; 151, 152; 151, 153; 152, identifier:attrs; 153, list:['bold']; 153, 154; 154, string:'bold'; 155, if_statement; 155, 156; 155, 158; 156, not_operator; 156, 157; 157, identifier:nobuild; 158, block; 158, 159; 158, 184; 158, 194; 158, 217; 159, if_statement; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:step; 162, identifier:bust_cache; 163, block; 163, 164; 163, 173; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:stackkey; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:self; 170, identifier:_get_stack_key; 171, argument_list; 171, 172; 172, identifier:istep; 173, if_statement; 173, 174; 173, 177; 174, comparison_operator:in; 174, 175; 174, 176; 175, identifier:stackkey; 176, identifier:_rebuilt; 177, block; 177, 178; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:step; 182, identifier:bust_cache; 183, False; 184, expression_statement; 184, 185; 185, call; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:step; 188, identifier:build; 189, argument_list; 189, 190; 189, 191; 190, identifier:client; 191, keyword_argument; 191, 192; 191, 193; 192, identifier:usecache; 193, identifier:usecache; 194, expression_statement; 194, 195; 195, call; 195, 196; 195, 197; 196, identifier:print; 197, argument_list; 197, 198; 197, 203; 197, 214; 198, call; 198, 199; 198, 200; 199, identifier:colored; 200, argument_list; 200, 201; 200, 202; 201, string:"* Created intermediate image"; 202, string:'green'; 203, call; 203, 204; 203, 205; 204, identifier:colored; 205, argument_list; 205, 206; 205, 209; 205, 210; 206, attribute; 206, 207; 206, 208; 207, identifier:step; 208, identifier:buildname; 209, string:'green'; 210, keyword_argument; 210, 211; 210, 212; 211, identifier:attrs; 212, list:['bold']; 212, 213; 213, string:'bold'; 214, keyword_argument; 214, 215; 214, 216; 215, identifier:end; 216, string:'\n\n'; 217, if_statement; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:step; 220, identifier:bust_cache; 221, block; 221, 222; 222, expression_statement; 222, 223; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:_rebuilt; 226, identifier:add; 227, argument_list; 227, 228; 228, identifier:stackkey; 229, expression_statement; 229, 230; 230, assignment; 230, 231; 230, 232; 231, identifier:finalimage; 232, attribute; 232, 233; 232, 234; 233, identifier:step; 234, identifier:buildname; 235, if_statement; 235, 236; 235, 238; 236, not_operator; 236, 237; 237, identifier:nobuild; 238, block; 238, 239; 238, 247; 238, 267; 238, 283; 239, expression_statement; 239, 240; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:self; 243, identifier:finalizenames; 244, argument_list; 244, 245; 244, 246; 245, identifier:client; 246, identifier:finalimage; 247, expression_statement; 247, 248; 248, assignment; 248, 249; 248, 250; 249, identifier:line; 250, binary_operator:%; 250, 251; 250, 252; 251, string:'FINISHED BUILDING "%s" (image definition "%s" from %s)'; 252, tuple; 252, 253; 252, 256; 252, 259; 253, attribute; 253, 254; 253, 255; 254, identifier:self; 255, identifier:targetname; 256, attribute; 256, 257; 256, 258; 257, identifier:self; 258, identifier:imagename; 259, attribute; 259, 260; 259, 266; 260, subscript; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:self; 263, identifier:steps; 264, unary_operator:-; 264, 265; 265, integer:1; 266, identifier:sourcefile; 267, expression_statement; 267, 268; 268, call; 268, 269; 268, 270; 269, identifier:cprint; 270, argument_list; 270, 271; 270, 276; 270, 279; 271, call; 271, 272; 271, 273; 272, identifier:_centered; 273, argument_list; 273, 274; 273, 275; 274, identifier:line; 275, identifier:width; 276, keyword_argument; 276, 277; 276, 278; 277, identifier:color; 278, string:'green'; 279, keyword_argument; 279, 280; 279, 281; 280, identifier:attrs; 281, list:['bold']; 281, 282; 282, string:'bold'; 283, expression_statement; 283, 284; 284, call; 284, 285; 284, 286; 285, identifier:cprint; 286, argument_list; 286, 287; 286, 290; 286, 293; 286, 297; 287, binary_operator:*; 287, 288; 287, 289; 288, string:'='; 289, identifier:width; 290, keyword_argument; 290, 291; 290, 292; 291, identifier:color; 292, string:'white'; 293, keyword_argument; 293, 294; 293, 295; 294, identifier:attrs; 295, list:['bold']; 295, 296; 296, string:'bold'; 297, keyword_argument; 297, 298; 297, 299; 298, identifier:end; 299, string:'\n\n'
def build(self, client, nobuild=False, usecache=True, pull=False): """ Drives the build of the final image - get the list of steps and execute them. Args: client (docker.Client): docker client object that will build the image nobuild (bool): just create dockerfiles, don't actually build the image usecache (bool): use docker cache, or rebuild everything from scratch? pull (bool): try to pull new versions of repository images? """ if not nobuild: self.update_source_images(client, usecache=usecache, pull=pull) width = utils.get_console_width() cprint('\n' + '='*width, color='white', attrs=['bold']) line = 'STARTING BUILD for "%s" (image definition "%s" from %s)\n' % ( self.targetname, self.imagename, self.steps[-1].sourcefile) cprint(_centered(line, width), color='blue', attrs=['bold']) for istep, step in enumerate(self.steps): print(colored('* Step','blue'), colored('%d/%d' % (istep+1, len(self.steps)), 'blue', attrs=['bold']), colored('for image', color='blue'), colored(self.imagename, color='blue', attrs=['bold'])) if not nobuild: if step.bust_cache: stackkey = self._get_stack_key(istep) if stackkey in _rebuilt: step.bust_cache = False step.build(client, usecache=usecache) print(colored("* Created intermediate image", 'green'), colored(step.buildname, 'green', attrs=['bold']), end='\n\n') if step.bust_cache: _rebuilt.add(stackkey) finalimage = step.buildname if not nobuild: self.finalizenames(client, finalimage) line = 'FINISHED BUILDING "%s" (image definition "%s" from %s)'%( self.targetname, self.imagename, self.steps[-1].sourcefile) cprint(_centered(line, width), color='green', attrs=['bold']) cprint('=' * width, color='white', attrs=['bold'], end='\n\n')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_check_yaml_and_paths; 3, parameters; 3, 4; 3, 5; 4, identifier:ymlfilepath; 5, identifier:yamldefs; 6, block; 6, 7; 6, 9; 6, 20; 6, 31; 6, 49; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:relpath; 12, call; 12, 13; 12, 18; 13, attribute; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:os; 16, identifier:path; 17, identifier:relpath; 18, argument_list; 18, 19; 19, identifier:ymlfilepath; 20, if_statement; 20, 21; 20, 24; 21, comparison_operator:not; 21, 22; 21, 23; 22, string:'/'; 23, identifier:relpath; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:relpath; 28, binary_operator:%; 28, 29; 28, 30; 29, string:'./%s'; 30, identifier:relpath; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:pathroot; 34, call; 34, 35; 34, 40; 35, attribute; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:os; 38, identifier:path; 39, identifier:abspath; 40, argument_list; 40, 41; 41, call; 41, 42; 41, 47; 42, attribute; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:os; 45, identifier:path; 46, identifier:dirname; 47, argument_list; 47, 48; 48, identifier:ymlfilepath; 49, for_statement; 49, 50; 49, 53; 49, 57; 50, pattern_list; 50, 51; 50, 52; 51, identifier:imagename; 52, identifier:defn; 53, call; 53, 54; 53, 55; 54, identifier:iteritems; 55, argument_list; 55, 56; 56, identifier:yamldefs; 57, block; 57, 58; 57, 93; 57, 117; 57, 188; 57, 189; 57, 195; 57, 215; 57, 238; 57, 266; 58, if_statement; 58, 59; 58, 62; 58, 87; 59, comparison_operator:==; 59, 60; 59, 61; 60, identifier:imagename; 61, string:'_SOURCES_'; 62, block; 62, 63; 62, 86; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 68; 65, subscript; 65, 66; 65, 67; 66, identifier:yamldefs; 67, string:'_SOURCES_'; 68, list_comprehension; 68, 69; 68, 81; 69, call; 69, 70; 69, 75; 70, attribute; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:os; 73, identifier:path; 74, identifier:relpath; 75, argument_list; 75, 76; 76, call; 76, 77; 76, 78; 77, identifier:_get_abspath; 78, argument_list; 78, 79; 78, 80; 79, identifier:pathroot; 80, identifier:p; 81, for_in_clause; 81, 82; 81, 83; 82, identifier:p; 83, subscript; 83, 84; 83, 85; 84, identifier:yamldefs; 85, string:'_SOURCES_'; 86, continue_statement; 87, elif_clause; 87, 88; 87, 91; 88, comparison_operator:in; 88, 89; 88, 90; 89, identifier:imagename; 90, identifier:SPECIAL_FIELDS; 91, block; 91, 92; 92, continue_statement; 93, for_statement; 93, 94; 93, 95; 93, 99; 94, identifier:key; 95, tuple; 95, 96; 95, 97; 95, 98; 96, string:'build_directory'; 97, string:'FROM_DOCKERFILE'; 98, string:'ignorefile'; 99, block; 99, 100; 100, if_statement; 100, 101; 100, 104; 101, comparison_operator:in; 101, 102; 101, 103; 102, identifier:key; 103, identifier:defn; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 110; 107, subscript; 107, 108; 107, 109; 108, identifier:defn; 109, identifier:key; 110, call; 110, 111; 110, 112; 111, identifier:_get_abspath; 112, argument_list; 112, 113; 112, 114; 113, identifier:pathroot; 114, subscript; 114, 115; 114, 116; 115, identifier:defn; 116, identifier:key; 117, if_statement; 117, 118; 117, 121; 118, comparison_operator:in; 118, 119; 118, 120; 119, string:'copy_from'; 120, identifier:defn; 121, block; 121, 122; 121, 148; 122, if_statement; 122, 123; 122, 131; 123, not_operator; 123, 124; 124, call; 124, 125; 124, 126; 125, identifier:isinstance; 126, argument_list; 126, 127; 126, 130; 127, subscript; 127, 128; 127, 129; 128, identifier:defn; 129, string:'copy_from'; 130, identifier:dict; 131, block; 131, 132; 132, raise_statement; 132, 133; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:errors; 136, identifier:ParsingFailure; 137, argument_list; 137, 138; 138, binary_operator:%; 138, 139; 138, 145; 139, parenthesized_expression; 139, 140; 140, binary_operator:+; 140, 141; 140, 142; 141, string:'Syntax error in file "%s": \n'; 142, concatenated_string; 142, 143; 142, 144; 143, string:'The "copy_from" field in image definition "%s" is not \n'; 144, string:'a key:value list.'; 145, tuple; 145, 146; 145, 147; 146, identifier:ymlfilepath; 147, identifier:imagename; 148, for_statement; 148, 149; 148, 152; 148, 163; 149, pattern_list; 149, 150; 149, 151; 150, identifier:otherimg; 151, identifier:value; 152, call; 152, 153; 152, 162; 153, attribute; 153, 154; 153, 161; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:defn; 157, identifier:get; 158, argument_list; 158, 159; 158, 160; 159, string:'copy_from'; 160, dictionary; 161, identifier:items; 162, argument_list; 163, block; 163, 164; 164, if_statement; 164, 165; 164, 171; 165, not_operator; 165, 166; 166, call; 166, 167; 166, 168; 167, identifier:isinstance; 168, argument_list; 168, 169; 168, 170; 169, identifier:value; 170, identifier:dict; 171, block; 171, 172; 172, raise_statement; 172, 173; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:errors; 176, identifier:ParsingFailure; 177, argument_list; 177, 178; 178, binary_operator:%; 178, 179; 178, 184; 179, parenthesized_expression; 179, 180; 180, concatenated_string; 180, 181; 180, 182; 180, 183; 181, string:'Syntax error in field:\n'; 182, string:' %s . copy_from . %s\nin file "%s". \n'; 183, string:'All entries must be of the form "sourcepath: destpath"'; 184, tuple; 184, 185; 184, 186; 184, 187; 185, identifier:imagename; 186, identifier:otherimg; 187, identifier:ymlfilepath; 188, comment; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 194; 191, subscript; 191, 192; 191, 193; 192, identifier:defn; 193, string:'_sourcefile'; 194, identifier:relpath; 195, if_statement; 195, 196; 195, 203; 196, boolean_operator:and; 196, 197; 196, 200; 197, comparison_operator:in; 197, 198; 197, 199; 198, string:'ignore'; 199, identifier:defn; 200, comparison_operator:in; 200, 201; 200, 202; 201, string:'ignorefile'; 202, identifier:defn; 203, block; 203, 204; 204, raise_statement; 204, 205; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:errors; 208, identifier:MultipleIgnoreError; 209, argument_list; 209, 210; 210, binary_operator:+; 210, 211; 210, 214; 211, binary_operator:%; 211, 212; 211, 213; 212, string:'Image "%s" has both "ignore" AND "ignorefile" fields.'; 213, identifier:imagename; 214, string:' At most ONE of these should be defined'; 215, if_statement; 215, 216; 215, 228; 216, boolean_operator:and; 216, 217; 216, 220; 217, comparison_operator:in; 217, 218; 217, 219; 218, string:'secret_files'; 219, identifier:defn; 220, not_operator; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:defn; 224, identifier:get; 225, argument_list; 225, 226; 225, 227; 226, string:'squash'; 227, True; 228, block; 228, 229; 229, raise_statement; 229, 230; 230, call; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:errors; 233, identifier:ParsingFailure; 234, argument_list; 234, 235; 235, binary_operator:%; 235, 236; 235, 237; 236, string:"Step '%s' defines secret_files, so 'squash' cannot be set to 'false'"; 237, identifier:imagename; 238, if_statement; 238, 239; 238, 254; 239, boolean_operator:and; 239, 240; 239, 247; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:defn; 243, identifier:get; 244, argument_list; 244, 245; 244, 246; 245, string:'secret_files'; 246, None; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:defn; 250, identifier:get; 251, argument_list; 251, 252; 251, 253; 252, string:'copy_from'; 253, False; 254, block; 254, 255; 255, raise_statement; 255, 256; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:errors; 259, identifier:ParsingFailure; 260, argument_list; 260, 261; 261, binary_operator:%; 261, 262; 261, 265; 262, concatenated_string; 262, 263; 262, 264; 263, string:'`secret_files` currently is not implmemented to handle `copy_from`'; 264, string:' (step %s)'; 265, identifier:imagename; 266, for_statement; 266, 267; 266, 268; 266, 269; 267, identifier:key; 268, identifier:defn; 269, block; 269, 270; 270, if_statement; 270, 271; 270, 274; 271, comparison_operator:not; 271, 272; 271, 273; 272, identifier:key; 273, identifier:RECOGNIZED_KEYS; 274, block; 274, 275; 275, raise_statement; 275, 276; 276, call; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:errors; 279, identifier:UnrecognizedKeyError; 280, argument_list; 280, 281; 281, binary_operator:%; 281, 282; 281, 283; 282, string:'Field "%s" in image "%s" in file "%s" not recognized'; 283, tuple; 283, 284; 283, 285; 283, 286; 284, identifier:key; 285, identifier:imagename; 286, identifier:relpath
def _check_yaml_and_paths(ymlfilepath, yamldefs): """ Checks YAML for errors and resolves all paths """ relpath = os.path.relpath(ymlfilepath) if '/' not in relpath: relpath = './%s' % relpath pathroot = os.path.abspath(os.path.dirname(ymlfilepath)) for imagename, defn in iteritems(yamldefs): if imagename == '_SOURCES_': yamldefs['_SOURCES_'] = [os.path.relpath(_get_abspath(pathroot, p)) for p in yamldefs['_SOURCES_']] continue elif imagename in SPECIAL_FIELDS: continue for key in ('build_directory', 'FROM_DOCKERFILE', 'ignorefile'): if key in defn: defn[key] = _get_abspath(pathroot, defn[key]) if 'copy_from' in defn: if not isinstance(defn['copy_from'], dict): raise errors.ParsingFailure(( 'Syntax error in file "%s": \n' + 'The "copy_from" field in image definition "%s" is not \n' 'a key:value list.') % (ymlfilepath, imagename)) for otherimg, value in defn.get('copy_from', {}).items(): if not isinstance(value, dict): raise errors.ParsingFailure(( 'Syntax error in field:\n' ' %s . copy_from . %s\nin file "%s". \n' 'All entries must be of the form "sourcepath: destpath"')% (imagename, otherimg, ymlfilepath)) # save the file path for logging defn['_sourcefile'] = relpath if 'ignore' in defn and 'ignorefile' in defn: raise errors.MultipleIgnoreError( 'Image "%s" has both "ignore" AND "ignorefile" fields.' % imagename + ' At most ONE of these should be defined') if 'secret_files' in defn and not defn.get('squash', True): raise errors.ParsingFailure( "Step '%s' defines secret_files, so 'squash' cannot be set to 'false'" % imagename) if defn.get('secret_files', None) and defn.get('copy_from', False): raise errors.ParsingFailure( '`secret_files` currently is not implmemented to handle `copy_from`' ' (step %s)' % imagename) for key in defn: if key not in RECOGNIZED_KEYS: raise errors.UnrecognizedKeyError( 'Field "%s" in image "%s" in file "%s" not recognized' % (key, imagename, relpath))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 21; 2, function_name:generate_build; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 3, 16; 3, 19; 4, identifier:self; 5, identifier:image; 6, identifier:targetname; 7, default_parameter; 7, 8; 7, 9; 8, identifier:rebuilds; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:cache_repo; 12, string:''; 13, default_parameter; 13, 14; 13, 15; 14, identifier:cache_tag; 15, string:''; 16, default_parameter; 16, 17; 16, 18; 17, identifier:buildargs; 18, None; 19, dictionary_splat_pattern; 19, 20; 20, identifier:kwargs; 21, block; 21, 22; 21, 24; 21, 33; 21, 55; 21, 69; 21, 96; 21, 100; 21, 104; 21, 110; 21, 128; 21, 315; 21, 337; 22, expression_statement; 22, 23; 23, comment; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:from_image; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:get_external_base_image; 31, argument_list; 31, 32; 32, identifier:image; 33, if_statement; 33, 34; 33, 37; 33, 49; 34, boolean_operator:or; 34, 35; 34, 36; 35, identifier:cache_repo; 36, identifier:cache_tag; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:cache_from; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:utils; 44, identifier:generate_name; 45, argument_list; 45, 46; 45, 47; 45, 48; 46, identifier:image; 47, identifier:cache_repo; 48, identifier:cache_tag; 49, else_clause; 49, 50; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:cache_from; 54, None; 55, if_statement; 55, 56; 55, 59; 56, comparison_operator:is; 56, 57; 56, 58; 57, identifier:from_image; 58, None; 59, block; 59, 60; 60, raise_statement; 60, 61; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:errors; 64, identifier:NoBaseError; 65, argument_list; 65, 66; 66, binary_operator:%; 66, 67; 66, 68; 67, string:"No base image found in %s's dependencies"; 68, identifier:image; 69, if_statement; 69, 70; 69, 75; 69, 86; 70, call; 70, 71; 70, 72; 71, identifier:isinstance; 72, argument_list; 72, 73; 72, 74; 73, identifier:from_image; 74, identifier:ExternalDockerfile; 75, block; 75, 76; 75, 80; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:build_first; 79, identifier:from_image; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:base_image; 83, attribute; 83, 84; 83, 85; 84, identifier:from_image; 85, identifier:tag; 86, else_clause; 86, 87; 87, block; 87, 88; 87, 92; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:base_image; 91, identifier:from_image; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:build_first; 95, None; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:build_steps; 99, list:[]; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:istep; 103, integer:0; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:sourceimages; 107, call; 107, 108; 107, 109; 108, identifier:set; 109, argument_list; 110, if_statement; 110, 111; 110, 114; 110, 119; 111, comparison_operator:is; 111, 112; 111, 113; 112, identifier:rebuilds; 113, None; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:rebuilds; 118, list:[]; 119, else_clause; 119, 120; 120, block; 120, 121; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:rebuilds; 124, call; 124, 125; 124, 126; 125, identifier:set; 126, argument_list; 126, 127; 127, identifier:rebuilds; 128, for_statement; 128, 129; 128, 130; 128, 136; 129, identifier:base_name; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:sort_dependencies; 134, argument_list; 134, 135; 135, identifier:image; 136, block; 136, 137; 136, 141; 136, 149; 136, 163; 136, 180; 136, 221; 136, 225; 136, 229; 137, expression_statement; 137, 138; 138, augmented_assignment:+=; 138, 139; 138, 140; 139, identifier:istep; 140, integer:1; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:buildname; 144, binary_operator:%; 144, 145; 144, 146; 145, string:'dmkbuild_%s_%d'; 146, tuple; 146, 147; 146, 148; 147, identifier:image; 148, identifier:istep; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:secret_files; 152, call; 152, 153; 152, 160; 153, attribute; 153, 154; 153, 159; 154, subscript; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:self; 157, identifier:ymldefs; 158, identifier:base_name; 159, identifier:get; 160, argument_list; 160, 161; 160, 162; 161, string:'secret_files'; 162, None; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:squash; 166, call; 166, 167; 166, 174; 167, attribute; 167, 168; 167, 173; 168, subscript; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:self; 171, identifier:ymldefs; 172, identifier:base_name; 173, identifier:get; 174, argument_list; 174, 175; 174, 176; 175, string:'squash'; 176, call; 176, 177; 176, 178; 177, identifier:bool; 178, argument_list; 178, 179; 179, identifier:secret_files; 180, expression_statement; 180, 181; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:build_steps; 184, identifier:append; 185, argument_list; 185, 186; 186, call; 186, 187; 186, 192; 187, attribute; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:dockermake; 190, identifier:step; 191, identifier:BuildStep; 192, argument_list; 192, 193; 192, 194; 192, 195; 192, 200; 192, 201; 192, 206; 192, 209; 192, 212; 192, 215; 192, 218; 193, identifier:base_name; 194, identifier:base_image; 195, subscript; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:self; 198, identifier:ymldefs; 199, identifier:base_name; 200, identifier:buildname; 201, keyword_argument; 201, 202; 201, 203; 202, identifier:bust_cache; 203, comparison_operator:in; 203, 204; 203, 205; 204, identifier:base_name; 205, identifier:rebuilds; 206, keyword_argument; 206, 207; 206, 208; 207, identifier:build_first; 208, identifier:build_first; 209, keyword_argument; 209, 210; 209, 211; 210, identifier:cache_from; 211, identifier:cache_from; 212, keyword_argument; 212, 213; 212, 214; 213, identifier:buildargs; 214, identifier:buildargs; 215, keyword_argument; 215, 216; 215, 217; 216, identifier:squash; 217, identifier:squash; 218, keyword_argument; 218, 219; 218, 220; 219, identifier:secret_files; 220, identifier:secret_files; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:base_image; 224, identifier:buildname; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:build_first; 228, None; 229, for_statement; 229, 230; 229, 233; 229, 247; 230, pattern_list; 230, 231; 230, 232; 231, identifier:sourceimage; 232, identifier:files; 233, call; 233, 234; 233, 235; 234, identifier:iteritems; 235, argument_list; 235, 236; 236, call; 236, 237; 236, 244; 237, attribute; 237, 238; 237, 243; 238, subscript; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:self; 241, identifier:ymldefs; 242, identifier:base_name; 243, identifier:get; 244, argument_list; 244, 245; 244, 246; 245, string:'copy_from'; 246, dictionary; 247, block; 247, 248; 247, 255; 248, expression_statement; 248, 249; 249, call; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:sourceimages; 252, identifier:add; 253, argument_list; 253, 254; 254, identifier:sourceimage; 255, for_statement; 255, 256; 255, 259; 255, 263; 256, pattern_list; 256, 257; 256, 258; 257, identifier:sourcepath; 258, identifier:destpath; 259, call; 259, 260; 259, 261; 260, identifier:iteritems; 261, argument_list; 261, 262; 262, identifier:files; 263, block; 263, 264; 263, 268; 263, 276; 263, 311; 264, expression_statement; 264, 265; 265, augmented_assignment:+=; 265, 266; 265, 267; 266, identifier:istep; 267, integer:1; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 271; 270, identifier:buildname; 271, binary_operator:%; 271, 272; 271, 273; 272, string:'dmkbuild_%s_%d'; 273, tuple; 273, 274; 273, 275; 274, identifier:image; 275, identifier:istep; 276, expression_statement; 276, 277; 277, call; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:build_steps; 280, identifier:append; 281, argument_list; 281, 282; 282, call; 282, 283; 282, 288; 283, attribute; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:dockermake; 286, identifier:step; 287, identifier:FileCopyStep; 288, argument_list; 288, 289; 288, 290; 288, 291; 288, 292; 288, 293; 288, 294; 288, 299; 288, 300; 288, 305; 288, 308; 289, identifier:sourceimage; 290, identifier:sourcepath; 291, identifier:destpath; 292, identifier:base_name; 293, identifier:base_image; 294, subscript; 294, 295; 294, 298; 295, attribute; 295, 296; 295, 297; 296, identifier:self; 297, identifier:ymldefs; 298, identifier:base_name; 299, identifier:buildname; 300, keyword_argument; 300, 301; 300, 302; 301, identifier:bust_cache; 302, comparison_operator:in; 302, 303; 302, 304; 303, identifier:base_name; 304, identifier:rebuilds; 305, keyword_argument; 305, 306; 305, 307; 306, identifier:build_first; 307, identifier:build_first; 308, keyword_argument; 308, 309; 308, 310; 309, identifier:cache_from; 310, identifier:cache_from; 311, expression_statement; 311, 312; 312, assignment; 312, 313; 312, 314; 313, identifier:base_image; 314, identifier:buildname; 315, expression_statement; 315, 316; 316, assignment; 316, 317; 316, 318; 317, identifier:sourcebuilds; 318, list_comprehension; 318, 319; 318, 334; 319, call; 319, 320; 319, 323; 320, attribute; 320, 321; 320, 322; 321, identifier:self; 322, identifier:generate_build; 323, argument_list; 323, 324; 323, 325; 323, 326; 323, 329; 323, 332; 324, identifier:img; 325, identifier:img; 326, keyword_argument; 326, 327; 326, 328; 327, identifier:cache_repo; 328, identifier:cache_repo; 329, keyword_argument; 329, 330; 329, 331; 330, identifier:cache_tag; 331, identifier:cache_tag; 332, dictionary_splat; 332, 333; 333, identifier:kwargs; 334, for_in_clause; 334, 335; 334, 336; 335, identifier:img; 336, identifier:sourceimages; 337, return_statement; 337, 338; 338, call; 338, 339; 338, 342; 339, attribute; 339, 340; 339, 341; 340, identifier:builds; 341, identifier:BuildTarget; 342, argument_list; 342, 343; 342, 346; 342, 349; 342, 352; 342, 355; 342, 358; 343, keyword_argument; 343, 344; 343, 345; 344, identifier:imagename; 345, identifier:image; 346, keyword_argument; 346, 347; 346, 348; 347, identifier:targetname; 348, identifier:targetname; 349, keyword_argument; 349, 350; 349, 351; 350, identifier:steps; 351, identifier:build_steps; 352, keyword_argument; 352, 353; 352, 354; 353, identifier:sourcebuilds; 354, identifier:sourcebuilds; 355, keyword_argument; 355, 356; 355, 357; 356, identifier:from_image; 357, identifier:from_image; 358, dictionary_splat; 358, 359; 359, identifier:kwargs
def generate_build(self, image, targetname, rebuilds=None, cache_repo='', cache_tag='', buildargs=None, **kwargs): """ Separate the build into a series of one or more intermediate steps. Each specified build directory gets its own step Args: image (str): name of the image as defined in the dockermake.py file targetname (str): name to tag the final built image with rebuilds (List[str]): list of image layers to rebuild (i.e., without docker's cache) cache_repo (str): repository to get images for caches in builds cache_tag (str): tags to use from repository for caches in builds buildargs (dict): build-time dockerfile arugments **kwargs (dict): extra keyword arguments for the BuildTarget object """ from_image = self.get_external_base_image(image) if cache_repo or cache_tag: cache_from = utils.generate_name(image, cache_repo, cache_tag) else: cache_from = None if from_image is None: raise errors.NoBaseError("No base image found in %s's dependencies" % image) if isinstance(from_image, ExternalDockerfile): build_first = from_image base_image = from_image.tag else: base_image = from_image build_first = None build_steps = [] istep = 0 sourceimages = set() if rebuilds is None: rebuilds = [] else: rebuilds = set(rebuilds) for base_name in self.sort_dependencies(image): istep += 1 buildname = 'dmkbuild_%s_%d' % (image, istep) secret_files = self.ymldefs[base_name].get('secret_files', None) squash = self.ymldefs[base_name].get('squash', bool(secret_files)) build_steps.append( dockermake.step.BuildStep( base_name, base_image, self.ymldefs[base_name], buildname, bust_cache=base_name in rebuilds, build_first=build_first, cache_from=cache_from, buildargs=buildargs, squash=squash, secret_files=secret_files)) base_image = buildname build_first = None for sourceimage, files in iteritems(self.ymldefs[base_name].get('copy_from', {})): sourceimages.add(sourceimage) for sourcepath, destpath in iteritems(files): istep += 1 buildname = 'dmkbuild_%s_%d' % (image, istep) build_steps.append( dockermake.step.FileCopyStep( sourceimage, sourcepath, destpath, base_name, base_image, self.ymldefs[base_name], buildname, bust_cache=base_name in rebuilds, build_first=build_first, cache_from=cache_from)) base_image = buildname sourcebuilds = [self.generate_build(img, img, cache_repo=cache_repo, cache_tag=cache_tag, **kwargs) for img in sourceimages] return builds.BuildTarget(imagename=image, targetname=targetname, steps=build_steps, sourcebuilds=sourcebuilds, from_image=from_image, **kwargs)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:sort_dependencies; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:image; 6, default_parameter; 6, 7; 6, 8; 7, identifier:dependencies; 8, None; 9, block; 9, 10; 9, 12; 9, 24; 9, 30; 9, 44; 9, 56; 9, 62; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 16; 13, comparison_operator:is; 13, 14; 13, 15; 14, identifier:dependencies; 15, None; 16, block; 16, 17; 16, 23; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:dependencies; 20, call; 20, 21; 20, 22; 21, identifier:OrderedDict; 22, argument_list; 23, comment; 24, if_statement; 24, 25; 24, 28; 25, comparison_operator:in; 25, 26; 25, 27; 26, identifier:image; 27, identifier:dependencies; 28, block; 28, 29; 29, return_statement; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:requires; 33, call; 33, 34; 33, 41; 34, attribute; 34, 35; 34, 40; 35, subscript; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:ymldefs; 39, identifier:image; 40, identifier:get; 41, argument_list; 41, 42; 41, 43; 42, string:'requires'; 43, list:[]; 44, for_statement; 44, 45; 44, 46; 44, 47; 45, identifier:dep; 46, identifier:requires; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:sort_dependencies; 53, argument_list; 53, 54; 53, 55; 54, identifier:dep; 55, identifier:dependencies; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 61; 58, subscript; 58, 59; 58, 60; 59, identifier:dependencies; 60, identifier:image; 61, None; 62, return_statement; 62, 63; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:dependencies; 66, identifier:keys; 67, argument_list
def sort_dependencies(self, image, dependencies=None): """ Topologically sort the docker commands by their requirements Note: Circular "requires" dependencies are assumed to have already been checked in get_external_base_image, they are not checked here Args: image (str): process this docker image's dependencies dependencies (OrderedDict): running cache of sorted dependencies (ordered dict) Returns: List[str]: list of dependencies a topologically-sorted build order """ if dependencies is None: dependencies = OrderedDict() # using this as an ordered set - not storing any values if image in dependencies: return requires = self.ymldefs[image].get('requires', []) for dep in requires: self.sort_dependencies(dep, dependencies) dependencies[image] = None return dependencies.keys()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:get_external_base_image; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:image; 6, default_parameter; 6, 7; 6, 8; 7, identifier:stack; 8, None; 9, block; 9, 10; 9, 12; 9, 23; 9, 31; 9, 57; 9, 64; 9, 65; 9, 85; 9, 139; 9, 149; 9, 166; 9, 227; 9, 235; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 16; 13, comparison_operator:is; 13, 14; 13, 15; 14, identifier:stack; 15, None; 16, block; 16, 17; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:stack; 20, call; 20, 21; 20, 22; 21, identifier:list; 22, argument_list; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:mydef; 26, subscript; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:ymldefs; 30, identifier:image; 31, if_statement; 31, 32; 31, 35; 32, comparison_operator:in; 32, 33; 32, 34; 33, identifier:image; 34, identifier:stack; 35, block; 35, 36; 35, 43; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:stack; 40, identifier:append; 41, argument_list; 41, 42; 42, identifier:image; 43, raise_statement; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:errors; 47, identifier:CircularDependencyError; 48, argument_list; 48, 49; 49, binary_operator:+; 49, 50; 49, 51; 50, string:'Circular dependency found:\n'; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, string:'->'; 54, identifier:join; 55, argument_list; 55, 56; 56, identifier:stack; 57, expression_statement; 57, 58; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:stack; 61, identifier:append; 62, argument_list; 62, 63; 63, identifier:image; 64, comment; 65, if_statement; 65, 66; 65, 73; 66, boolean_operator:and; 66, 67; 66, 70; 67, comparison_operator:in; 67, 68; 67, 69; 68, string:'FROM'; 69, identifier:mydef; 70, comparison_operator:in; 70, 71; 70, 72; 71, string:'FROM_DOCKERFILE'; 72, identifier:mydef; 73, block; 73, 74; 74, raise_statement; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:errors; 78, identifier:MultipleBaseError; 79, argument_list; 79, 80; 80, binary_operator:+; 80, 81; 80, 84; 81, binary_operator:%; 81, 82; 81, 83; 82, string:'ERROR: Image "%s" has both a "FROM" and a "FROM_DOCKERFILE" field.'; 83, identifier:image; 84, string:' It should have at most ONE of these fields.'; 85, if_statement; 85, 86; 85, 89; 85, 96; 85, 133; 86, comparison_operator:in; 86, 87; 86, 88; 87, string:'FROM'; 88, identifier:mydef; 89, block; 89, 90; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:externalbase; 93, subscript; 93, 94; 93, 95; 94, identifier:mydef; 95, string:'FROM'; 96, elif_clause; 96, 97; 96, 100; 97, comparison_operator:in; 97, 98; 97, 99; 98, string:'FROM_DOCKERFILE'; 99, identifier:mydef; 100, block; 100, 101; 100, 107; 100, 125; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:path; 104, subscript; 104, 105; 104, 106; 105, identifier:mydef; 106, string:'FROM_DOCKERFILE'; 107, if_statement; 107, 108; 107, 113; 108, comparison_operator:not; 108, 109; 108, 110; 109, identifier:path; 110, attribute; 110, 111; 110, 112; 111, identifier:self; 112, identifier:_external_dockerfiles; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 121; 116, subscript; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:_external_dockerfiles; 120, identifier:path; 121, call; 121, 122; 121, 123; 122, identifier:ExternalDockerfile; 123, argument_list; 123, 124; 124, identifier:path; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:externalbase; 128, subscript; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:self; 131, identifier:_external_dockerfiles; 132, identifier:path; 133, else_clause; 133, 134; 134, block; 134, 135; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:externalbase; 138, None; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:requires; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:mydef; 145, identifier:get; 146, argument_list; 146, 147; 146, 148; 147, string:'requires'; 148, list:[]; 149, if_statement; 149, 150; 149, 156; 150, not_operator; 150, 151; 151, call; 151, 152; 151, 153; 152, identifier:isinstance; 153, argument_list; 153, 154; 153, 155; 154, identifier:requires; 155, identifier:list; 156, block; 156, 157; 157, raise_statement; 157, 158; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:errors; 161, identifier:InvalidRequiresList; 162, argument_list; 162, 163; 163, binary_operator:%; 163, 164; 163, 165; 164, string:'Requirements for image "%s" are not a list'; 165, identifier:image; 166, for_statement; 166, 167; 166, 168; 166, 169; 167, identifier:base; 168, identifier:requires; 169, block; 169, 170; 169, 186; 170, try_statement; 170, 171; 170, 182; 171, block; 171, 172; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:otherexternal; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:self; 178, identifier:get_external_base_image; 179, argument_list; 179, 180; 179, 181; 180, identifier:base; 181, identifier:stack; 182, except_clause; 182, 183; 182, 184; 183, identifier:ValueError; 184, block; 184, 185; 185, continue_statement; 186, if_statement; 186, 187; 186, 190; 186, 195; 186, 201; 187, comparison_operator:is; 187, 188; 187, 189; 188, identifier:externalbase; 189, None; 190, block; 190, 191; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 194; 193, identifier:externalbase; 194, identifier:otherexternal; 195, elif_clause; 195, 196; 195, 199; 196, comparison_operator:is; 196, 197; 196, 198; 197, identifier:otherexternal; 198, None; 199, block; 199, 200; 200, continue_statement; 201, elif_clause; 201, 202; 201, 205; 202, comparison_operator:!=; 202, 203; 202, 204; 203, identifier:externalbase; 204, identifier:otherexternal; 205, block; 205, 206; 206, raise_statement; 206, 207; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:errors; 210, identifier:ConflictingBaseError; 211, argument_list; 211, 212; 212, binary_operator:+; 212, 213; 212, 222; 213, binary_operator:+; 213, 214; 213, 217; 214, binary_operator:%; 214, 215; 214, 216; 215, string:'Multiple external dependencies: definition "%s" depends on:\n'; 216, identifier:image; 217, binary_operator:%; 217, 218; 217, 219; 218, string:' %s (FROM: %s), and\n'; 219, tuple; 219, 220; 219, 221; 220, identifier:image; 221, identifier:externalbase; 222, binary_operator:%; 222, 223; 222, 224; 223, string:' %s (FROM: %s).'; 224, tuple; 224, 225; 224, 226; 225, identifier:base; 226, identifier:otherexternal; 227, assert_statement; 227, 228; 228, comparison_operator:==; 228, 229; 228, 234; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:stack; 232, identifier:pop; 233, argument_list; 234, identifier:image; 235, return_statement; 235, 236; 236, identifier:externalbase
def get_external_base_image(self, image, stack=None): """ Makes sure that this image has exactly one unique external base image """ if stack is None: stack = list() mydef = self.ymldefs[image] if image in stack: stack.append(image) raise errors.CircularDependencyError('Circular dependency found:\n' + '->'.join(stack)) stack.append(image) # Deal with FROM and FROM_DOCKERFILE fields if 'FROM' in mydef and 'FROM_DOCKERFILE' in mydef: raise errors.MultipleBaseError( 'ERROR: Image "%s" has both a "FROM" and a "FROM_DOCKERFILE" field.' % image + ' It should have at most ONE of these fields.') if 'FROM' in mydef: externalbase = mydef['FROM'] elif 'FROM_DOCKERFILE' in mydef: path = mydef['FROM_DOCKERFILE'] if path not in self._external_dockerfiles: self._external_dockerfiles[path] = ExternalDockerfile(path) externalbase = self._external_dockerfiles[path] else: externalbase = None requires = mydef.get('requires', []) if not isinstance(requires, list): raise errors.InvalidRequiresList('Requirements for image "%s" are not a list' % image) for base in requires: try: otherexternal = self.get_external_base_image(base, stack) except ValueError: continue if externalbase is None: externalbase = otherexternal elif otherexternal is None: continue elif externalbase != otherexternal: raise errors.ConflictingBaseError( 'Multiple external dependencies: definition "%s" depends on:\n' % image + ' %s (FROM: %s), and\n' % (image, externalbase) + ' %s (FROM: %s).' % (base, otherexternal)) assert stack.pop() == image return externalbase
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_htmlRenderDict; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:pathParts; 5, identifier:statDict; 6, identifier:output; 7, block; 7, 8; 7, 10; 7, 21; 7, 27; 7, 31; 7, 38; 7, 175; 7, 189; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:keys; 13, call; 13, 14; 13, 15; 14, identifier:list; 15, argument_list; 15, 16; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:statDict; 19, identifier:keys; 20, argument_list; 21, expression_statement; 21, 22; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:keys; 25, identifier:sort; 26, argument_list; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:links; 30, list:[]; 31, expression_statement; 31, 32; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:output; 35, identifier:write; 36, argument_list; 36, 37; 37, string:'<div class="level">'; 38, for_statement; 38, 39; 38, 40; 38, 41; 39, identifier:key; 40, identifier:keys; 41, block; 41, 42; 41, 54; 41, 60; 41, 73; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:keyStr; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:cgi; 48, identifier:escape; 49, argument_list; 49, 50; 50, call; 50, 51; 50, 52; 51, identifier:_utf8str; 52, argument_list; 52, 53; 53, identifier:key; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:value; 57, subscript; 57, 58; 57, 59; 58, identifier:statDict; 59, identifier:key; 60, if_statement; 60, 61; 60, 66; 61, call; 61, 62; 61, 63; 62, identifier:hasattr; 63, argument_list; 63, 64; 63, 65; 64, identifier:value; 65, string:'__call__'; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:value; 70, call; 70, 71; 70, 72; 71, identifier:value; 72, argument_list; 73, if_statement; 73, 74; 73, 79; 73, 142; 74, call; 74, 75; 74, 76; 75, identifier:hasattr; 76, argument_list; 76, 77; 76, 78; 77, identifier:value; 78, string:'keys'; 79, block; 79, 80; 79, 87; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:valuePath; 83, binary_operator:+; 83, 84; 83, 85; 84, identifier:pathParts; 85, tuple; 85, 86; 86, identifier:keyStr; 87, if_statement; 87, 88; 87, 101; 87, 124; 88, boolean_operator:and; 88, 89; 88, 96; 89, call; 89, 90; 89, 91; 90, identifier:isinstance; 91, argument_list; 91, 92; 91, 93; 92, identifier:value; 93, attribute; 93, 94; 93, 95; 94, identifier:scales; 95, identifier:StatContainer; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:value; 99, identifier:isCollapsed; 100, argument_list; 101, block; 101, 102; 101, 113; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:link; 105, binary_operator:+; 105, 106; 105, 107; 106, string:'/status/'; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, string:'/'; 110, identifier:join; 111, argument_list; 111, 112; 112, identifier:valuePath; 113, expression_statement; 113, 114; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:links; 117, identifier:append; 118, argument_list; 118, 119; 119, binary_operator:%; 119, 120; 119, 121; 120, string:'<div class="key"><a href="%s">%s</a></div>'; 121, tuple; 121, 122; 121, 123; 122, identifier:link; 123, identifier:keyStr; 124, else_clause; 124, 125; 125, block; 125, 126; 125, 135; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:output; 130, identifier:write; 131, argument_list; 131, 132; 132, binary_operator:%; 132, 133; 132, 134; 133, string:'<div class="key">%s</div>'; 134, identifier:keyStr; 135, expression_statement; 135, 136; 136, call; 136, 137; 136, 138; 137, identifier:_htmlRenderDict; 138, argument_list; 138, 139; 138, 140; 138, 141; 139, identifier:valuePath; 140, identifier:value; 141, identifier:output; 142, else_clause; 142, 143; 143, block; 143, 144; 144, expression_statement; 144, 145; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:output; 148, identifier:write; 149, argument_list; 149, 150; 150, binary_operator:%; 150, 151; 150, 152; 151, string:'<div><span class="key">%s</span> <span class="%s">%s</span></div>'; 152, tuple; 152, 153; 152, 154; 152, 160; 153, identifier:keyStr; 154, attribute; 154, 155; 154, 159; 155, call; 155, 156; 155, 157; 156, identifier:type; 157, argument_list; 157, 158; 158, identifier:value; 159, identifier:__name__; 160, call; 160, 161; 160, 172; 161, attribute; 161, 162; 161, 171; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:cgi; 165, identifier:escape; 166, argument_list; 166, 167; 167, call; 167, 168; 167, 169; 168, identifier:_utf8str; 169, argument_list; 169, 170; 170, identifier:value; 171, identifier:replace; 172, argument_list; 172, 173; 172, 174; 173, string:'\n'; 174, string:'<br/>'; 175, if_statement; 175, 176; 175, 177; 176, identifier:links; 177, block; 177, 178; 178, for_statement; 178, 179; 178, 180; 178, 181; 179, identifier:link; 180, identifier:links; 181, block; 181, 182; 182, expression_statement; 182, 183; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:output; 186, identifier:write; 187, argument_list; 187, 188; 188, identifier:link; 189, expression_statement; 189, 190; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:output; 193, identifier:write; 194, argument_list; 194, 195; 195, string:'</div>'
def _htmlRenderDict(pathParts, statDict, output): """Render a dictionary as a table - recursing as necessary.""" keys = list(statDict.keys()) keys.sort() links = [] output.write('<div class="level">') for key in keys: keyStr = cgi.escape(_utf8str(key)) value = statDict[key] if hasattr(value, '__call__'): value = value() if hasattr(value, 'keys'): valuePath = pathParts + (keyStr,) if isinstance(value, scales.StatContainer) and value.isCollapsed(): link = '/status/' + '/'.join(valuePath) links.append('<div class="key"><a href="%s">%s</a></div>' % (link, keyStr)) else: output.write('<div class="key">%s</div>' % keyStr) _htmlRenderDict(valuePath, value, output) else: output.write('<div><span class="key">%s</span> <span class="%s">%s</span></div>' % (keyStr, type(value).__name__, cgi.escape(_utf8str(value)).replace('\n', '<br/>'))) if links: for link in links: output.write(link) output.write('</div>')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 96; 1, 98; 2, function_name:create_session; 3, parameters; 3, 4; 3, 5; 3, 9; 3, 14; 3, 23; 3, 32; 3, 41; 3, 46; 3, 51; 3, 56; 3, 61; 3, 66; 3, 75; 3, 80; 3, 85; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:kind; 7, type; 7, 8; 8, identifier:SessionKind; 9, typed_default_parameter; 9, 10; 9, 11; 9, 13; 10, identifier:proxy_user; 11, type; 11, 12; 12, identifier:str; 13, None; 14, typed_default_parameter; 14, 15; 14, 16; 14, 22; 15, identifier:jars; 16, type; 16, 17; 17, generic_type; 17, 18; 17, 19; 18, identifier:List; 19, type_parameter; 19, 20; 20, type; 20, 21; 21, identifier:str; 22, None; 23, typed_default_parameter; 23, 24; 23, 25; 23, 31; 24, identifier:py_files; 25, type; 25, 26; 26, generic_type; 26, 27; 26, 28; 27, identifier:List; 28, type_parameter; 28, 29; 29, type; 29, 30; 30, identifier:str; 31, None; 32, typed_default_parameter; 32, 33; 32, 34; 32, 40; 33, identifier:files; 34, type; 34, 35; 35, generic_type; 35, 36; 35, 37; 36, identifier:List; 37, type_parameter; 37, 38; 38, type; 38, 39; 39, identifier:str; 40, None; 41, typed_default_parameter; 41, 42; 41, 43; 41, 45; 42, identifier:driver_memory; 43, type; 43, 44; 44, identifier:str; 45, None; 46, typed_default_parameter; 46, 47; 46, 48; 46, 50; 47, identifier:driver_cores; 48, type; 48, 49; 49, identifier:int; 50, None; 51, typed_default_parameter; 51, 52; 51, 53; 51, 55; 52, identifier:executor_memory; 53, type; 53, 54; 54, identifier:str; 55, None; 56, typed_default_parameter; 56, 57; 56, 58; 56, 60; 57, identifier:executor_cores; 58, type; 58, 59; 59, identifier:int; 60, None; 61, typed_default_parameter; 61, 62; 61, 63; 61, 65; 62, identifier:num_executors; 63, type; 63, 64; 64, identifier:int; 65, None; 66, typed_default_parameter; 66, 67; 66, 68; 66, 74; 67, identifier:archives; 68, type; 68, 69; 69, generic_type; 69, 70; 69, 71; 70, identifier:List; 71, type_parameter; 71, 72; 72, type; 72, 73; 73, identifier:str; 74, None; 75, typed_default_parameter; 75, 76; 75, 77; 75, 79; 76, identifier:queue; 77, type; 77, 78; 78, identifier:str; 79, None; 80, typed_default_parameter; 80, 81; 80, 82; 80, 84; 81, identifier:name; 82, type; 82, 83; 83, identifier:str; 84, None; 85, typed_default_parameter; 85, 86; 85, 87; 85, 95; 86, identifier:spark_conf; 87, type; 87, 88; 88, generic_type; 88, 89; 88, 90; 89, identifier:Dict; 90, type_parameter; 90, 91; 90, 93; 91, type; 91, 92; 92, identifier:str; 93, type; 93, 94; 94, identifier:Any; 95, None; 96, type; 96, 97; 97, identifier:Session; 98, block; 98, 99; 98, 101; 98, 118; 98, 130; 98, 139; 98, 150; 98, 161; 98, 172; 98, 183; 98, 194; 98, 205; 98, 216; 98, 227; 98, 238; 98, 249; 98, 260; 98, 271; 98, 282; 98, 296; 99, expression_statement; 99, 100; 100, comment; 101, if_statement; 101, 102; 101, 107; 101, 112; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:self; 105, identifier:legacy_server; 106, argument_list; 107, block; 107, 108; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:valid_kinds; 111, identifier:VALID_LEGACY_SESSION_KINDS; 112, else_clause; 112, 113; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:valid_kinds; 117, identifier:VALID_SESSION_KINDS; 118, if_statement; 118, 119; 118, 122; 119, comparison_operator:not; 119, 120; 119, 121; 120, identifier:kind; 121, identifier:valid_kinds; 122, block; 122, 123; 123, raise_statement; 123, 124; 124, call; 124, 125; 124, 126; 125, identifier:ValueError; 126, argument_list; 126, 127; 127, concatenated_string; 127, 128; 127, 129; 128, string:f"{kind} is not a valid session kind for a Livy server of "; 129, string:f"this version (should be one of {valid_kinds})"; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:body; 133, dictionary; 133, 134; 134, pair; 134, 135; 134, 136; 135, string:"kind"; 136, attribute; 136, 137; 136, 138; 137, identifier:kind; 138, identifier:value; 139, if_statement; 139, 140; 139, 143; 140, comparison_operator:is; 140, 141; 140, 142; 141, identifier:proxy_user; 142, None; 143, block; 143, 144; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 149; 146, subscript; 146, 147; 146, 148; 147, identifier:body; 148, string:"proxyUser"; 149, identifier:proxy_user; 150, if_statement; 150, 151; 150, 154; 151, comparison_operator:is; 151, 152; 151, 153; 152, identifier:jars; 153, None; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 160; 157, subscript; 157, 158; 157, 159; 158, identifier:body; 159, string:"jars"; 160, identifier:jars; 161, if_statement; 161, 162; 161, 165; 162, comparison_operator:is; 162, 163; 162, 164; 163, identifier:py_files; 164, None; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 171; 168, subscript; 168, 169; 168, 170; 169, identifier:body; 170, string:"pyFiles"; 171, identifier:py_files; 172, if_statement; 172, 173; 172, 176; 173, comparison_operator:is; 173, 174; 173, 175; 174, identifier:files; 175, None; 176, block; 176, 177; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 182; 179, subscript; 179, 180; 179, 181; 180, identifier:body; 181, string:"files"; 182, identifier:files; 183, if_statement; 183, 184; 183, 187; 184, comparison_operator:is; 184, 185; 184, 186; 185, identifier:driver_memory; 186, None; 187, block; 187, 188; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 193; 190, subscript; 190, 191; 190, 192; 191, identifier:body; 192, string:"driverMemory"; 193, identifier:driver_memory; 194, if_statement; 194, 195; 194, 198; 195, comparison_operator:is; 195, 196; 195, 197; 196, identifier:driver_cores; 197, None; 198, block; 198, 199; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 204; 201, subscript; 201, 202; 201, 203; 202, identifier:body; 203, string:"driverCores"; 204, identifier:driver_cores; 205, if_statement; 205, 206; 205, 209; 206, comparison_operator:is; 206, 207; 206, 208; 207, identifier:executor_memory; 208, None; 209, block; 209, 210; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 215; 212, subscript; 212, 213; 212, 214; 213, identifier:body; 214, string:"executorMemory"; 215, identifier:executor_memory; 216, if_statement; 216, 217; 216, 220; 217, comparison_operator:is; 217, 218; 217, 219; 218, identifier:executor_cores; 219, None; 220, block; 220, 221; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 226; 223, subscript; 223, 224; 223, 225; 224, identifier:body; 225, string:"executorCores"; 226, identifier:executor_cores; 227, if_statement; 227, 228; 227, 231; 228, comparison_operator:is; 228, 229; 228, 230; 229, identifier:num_executors; 230, None; 231, block; 231, 232; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 237; 234, subscript; 234, 235; 234, 236; 235, identifier:body; 236, string:"numExecutors"; 237, identifier:num_executors; 238, if_statement; 238, 239; 238, 242; 239, comparison_operator:is; 239, 240; 239, 241; 240, identifier:archives; 241, None; 242, block; 242, 243; 243, expression_statement; 243, 244; 244, assignment; 244, 245; 244, 248; 245, subscript; 245, 246; 245, 247; 246, identifier:body; 247, string:"archives"; 248, identifier:archives; 249, if_statement; 249, 250; 249, 253; 250, comparison_operator:is; 250, 251; 250, 252; 251, identifier:queue; 252, None; 253, block; 253, 254; 254, expression_statement; 254, 255; 255, assignment; 255, 256; 255, 259; 256, subscript; 256, 257; 256, 258; 257, identifier:body; 258, string:"queue"; 259, identifier:queue; 260, if_statement; 260, 261; 260, 264; 261, comparison_operator:is; 261, 262; 261, 263; 262, identifier:name; 263, None; 264, block; 264, 265; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 270; 267, subscript; 267, 268; 267, 269; 268, identifier:body; 269, string:"name"; 270, identifier:name; 271, if_statement; 271, 272; 271, 275; 272, comparison_operator:is; 272, 273; 272, 274; 273, identifier:spark_conf; 274, None; 275, block; 275, 276; 276, expression_statement; 276, 277; 277, assignment; 277, 278; 277, 281; 278, subscript; 278, 279; 278, 280; 279, identifier:body; 280, string:"conf"; 281, identifier:spark_conf; 282, expression_statement; 282, 283; 283, assignment; 283, 284; 283, 285; 284, identifier:data; 285, call; 285, 286; 285, 291; 286, attribute; 286, 287; 286, 290; 287, attribute; 287, 288; 287, 289; 288, identifier:self; 289, identifier:_client; 290, identifier:post; 291, argument_list; 291, 292; 291, 293; 292, string:"/sessions"; 293, keyword_argument; 293, 294; 293, 295; 294, identifier:data; 295, identifier:body; 296, return_statement; 296, 297; 297, call; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:Session; 300, identifier:from_json; 301, argument_list; 301, 302; 302, identifier:data
def create_session( self, kind: SessionKind, proxy_user: str = None, jars: List[str] = None, py_files: List[str] = None, files: List[str] = None, driver_memory: str = None, driver_cores: int = None, executor_memory: str = None, executor_cores: int = None, num_executors: int = None, archives: List[str] = None, queue: str = None, name: str = None, spark_conf: Dict[str, Any] = None, ) -> Session: """Create a new session in Livy. The py_files, files, jars and archives arguments are lists of URLs, e.g. ["s3://bucket/object", "hdfs://path/to/file", ...] and must be reachable by the Spark driver process. If the provided URL has no scheme, it's considered to be relative to the default file system configured in the Livy server. URLs in the py_files argument are copied to a temporary staging area and inserted into Python's sys.path ahead of the standard library paths. This allows you to import .py, .zip and .egg files in Python. URLs for jars, py_files, files and archives arguments are all copied to the same working directory on the Spark cluster. The driver_memory and executor_memory arguments have the same format as JVM memory strings with a size unit suffix ("k", "m", "g" or "t") (e.g. 512m, 2g). See https://spark.apache.org/docs/latest/configuration.html for more information on Spark configuration properties. :param kind: The kind of session to create. :param proxy_user: User to impersonate when starting the session. :param jars: URLs of jars to be used in this session. :param py_files: URLs of Python files to be used in this session. :param files: URLs of files to be used in this session. :param driver_memory: Amount of memory to use for the driver process (e.g. '512m'). :param driver_cores: Number of cores to use for the driver process. :param executor_memory: Amount of memory to use per executor process (e.g. '512m'). :param executor_cores: Number of cores to use for each executor. :param num_executors: Number of executors to launch for this session. :param archives: URLs of archives to be used in this session. :param queue: The name of the YARN queue to which submitted. :param name: The name of this session. :param spark_conf: Spark configuration properties. """ if self.legacy_server(): valid_kinds = VALID_LEGACY_SESSION_KINDS else: valid_kinds = VALID_SESSION_KINDS if kind not in valid_kinds: raise ValueError( f"{kind} is not a valid session kind for a Livy server of " f"this version (should be one of {valid_kinds})" ) body = {"kind": kind.value} if proxy_user is not None: body["proxyUser"] = proxy_user if jars is not None: body["jars"] = jars if py_files is not None: body["pyFiles"] = py_files if files is not None: body["files"] = files if driver_memory is not None: body["driverMemory"] = driver_memory if driver_cores is not None: body["driverCores"] = driver_cores if executor_memory is not None: body["executorMemory"] = executor_memory if executor_cores is not None: body["executorCores"] = executor_cores if num_executors is not None: body["numExecutors"] = num_executors if archives is not None: body["archives"] = archives if queue is not None: body["queue"] = queue if name is not None: body["name"] = name if spark_conf is not None: body["conf"] = spark_conf data = self._client.post("/sessions", data=body) return Session.from_json(data)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 21; 2, function_name:sine_psd; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 4, identifier:data; 5, identifier:delta; 6, default_parameter; 6, 7; 6, 8; 7, identifier:number_of_tapers; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:number_of_iterations; 11, integer:2; 12, default_parameter; 12, 13; 12, 14; 13, identifier:degree_of_smoothing; 14, float:1.0; 15, default_parameter; 15, 16; 15, 17; 16, identifier:statistics; 17, False; 18, default_parameter; 18, 19; 18, 20; 19, identifier:verbose; 20, False; 21, block; 21, 22; 21, 24; 21, 25; 21, 50; 21, 51; 21, 60; 21, 61; 21, 68; 21, 69; 21, 89; 21, 90; 21, 97; 21, 108; 21, 109; 21, 118; 21, 127; 21, 128; 21, 190; 21, 191; 21, 289; 21, 290; 21, 296; 21, 310; 22, expression_statement; 22, 23; 23, comment; 24, comment; 25, if_statement; 25, 26; 25, 29; 25, 44; 26, comparison_operator:is; 26, 27; 26, 28; 27, identifier:verbose; 28, True; 29, block; 29, 30; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:verbose; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:C; 36, identifier:byref; 37, argument_list; 37, 38; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:C; 41, identifier:c_char; 42, argument_list; 42, 43; 43, string:'y'; 44, else_clause; 44, 45; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:verbose; 49, None; 50, comment; 51, if_statement; 51, 52; 51, 55; 52, comparison_operator:is; 52, 53; 52, 54; 53, identifier:number_of_tapers; 54, None; 55, block; 55, 56; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:number_of_tapers; 59, integer:0; 60, comment; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:mt; 64, call; 64, 65; 64, 66; 65, identifier:_MtspecType; 66, argument_list; 66, 67; 67, string:"float32"; 68, comment; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:data; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:np; 75, identifier:require; 76, argument_list; 76, 77; 76, 78; 76, 83; 77, identifier:data; 78, keyword_argument; 78, 79; 78, 80; 79, identifier:dtype; 80, attribute; 80, 81; 80, 82; 81, identifier:mt; 82, identifier:float; 83, keyword_argument; 83, 84; 83, 85; 84, identifier:requirements; 85, list:[mt.order]; 85, 86; 86, attribute; 86, 87; 86, 88; 87, identifier:mt; 88, identifier:order; 89, comment; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:npts; 93, call; 93, 94; 93, 95; 94, identifier:len; 95, argument_list; 95, 96; 96, identifier:data; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:number_of_frequency_bins; 100, binary_operator:+; 100, 101; 100, 107; 101, call; 101, 102; 101, 103; 102, identifier:int; 103, argument_list; 103, 104; 104, binary_operator:/; 104, 105; 104, 106; 105, identifier:npts; 106, integer:2; 107, integer:1; 108, comment; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:frequency_bins; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:mt; 115, identifier:empty; 116, argument_list; 116, 117; 117, identifier:number_of_frequency_bins; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:spectrum; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:mt; 124, identifier:empty; 125, argument_list; 125, 126; 126, identifier:number_of_frequency_bins; 127, comment; 128, if_statement; 128, 129; 128, 132; 128, 133; 128, 134; 128, 182; 129, comparison_operator:is; 129, 130; 129, 131; 130, identifier:statistics; 131, True; 132, comment; 133, comment; 134, block; 134, 135; 134, 152; 134, 171; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:tapers_per_freq_point; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:np; 141, identifier:empty; 142, argument_list; 142, 143; 142, 144; 142, 147; 143, identifier:number_of_frequency_bins; 144, keyword_argument; 144, 145; 144, 146; 145, identifier:dtype; 146, string:'int32'; 147, keyword_argument; 147, 148; 147, 149; 148, identifier:order; 149, attribute; 149, 150; 149, 151; 150, identifier:mt; 151, identifier:order; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 153, 156; 154, identifier:tapers_per_freq_point_p; 155, line_continuation:\; 156, call; 156, 157; 156, 162; 157, attribute; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:tapers_per_freq_point; 160, identifier:ctypes; 161, identifier:data_as; 162, argument_list; 162, 163; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:C; 166, identifier:POINTER; 167, argument_list; 167, 168; 168, attribute; 168, 169; 168, 170; 169, identifier:C; 170, identifier:c_int; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:errors; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:mt; 177, identifier:empty; 178, argument_list; 178, 179; 179, tuple; 179, 180; 179, 181; 180, identifier:number_of_frequency_bins; 181, integer:2; 182, else_clause; 182, 183; 183, block; 183, 184; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:tapers_per_freq_point_p; 187, assignment; 187, 188; 187, 189; 188, identifier:errors; 189, None; 190, comment; 191, expression_statement; 191, 192; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:mtspeclib; 195, identifier:sine_psd_; 196, argument_list; 196, 197; 196, 208; 196, 219; 196, 225; 196, 236; 196, 247; 196, 258; 196, 269; 196, 275; 196, 281; 196, 282; 196, 288; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:C; 200, identifier:byref; 201, argument_list; 201, 202; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:C; 205, identifier:c_int; 206, argument_list; 206, 207; 207, identifier:npts; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:C; 211, identifier:byref; 212, argument_list; 212, 213; 213, call; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:C; 216, identifier:c_float; 217, argument_list; 217, 218; 218, identifier:delta; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:mt; 222, identifier:p; 223, argument_list; 223, 224; 224, identifier:data; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:C; 228, identifier:byref; 229, argument_list; 229, 230; 230, call; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:C; 233, identifier:c_int; 234, argument_list; 234, 235; 235, identifier:number_of_tapers; 236, call; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:C; 239, identifier:byref; 240, argument_list; 240, 241; 241, call; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:C; 244, identifier:c_int; 245, argument_list; 245, 246; 246, identifier:number_of_iterations; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:C; 250, identifier:byref; 251, argument_list; 251, 252; 252, call; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:C; 255, identifier:c_float; 256, argument_list; 256, 257; 257, identifier:degree_of_smoothing; 258, call; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:C; 261, identifier:byref; 262, argument_list; 262, 263; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:C; 266, identifier:c_int; 267, argument_list; 267, 268; 268, identifier:number_of_frequency_bins; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:mt; 272, identifier:p; 273, argument_list; 273, 274; 274, identifier:frequency_bins; 275, call; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:mt; 278, identifier:p; 279, argument_list; 279, 280; 280, identifier:spectrum; 281, identifier:tapers_per_freq_point_p; 282, call; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, identifier:mt; 285, identifier:p; 286, argument_list; 286, 287; 287, identifier:errors; 288, identifier:verbose; 289, comment; 290, expression_statement; 290, 291; 291, assignment; 291, 292; 291, 293; 292, identifier:return_values; 293, list:[spectrum, frequency_bins]; 293, 294; 293, 295; 294, identifier:spectrum; 295, identifier:frequency_bins; 296, if_statement; 296, 297; 296, 300; 297, comparison_operator:is; 297, 298; 297, 299; 298, identifier:statistics; 299, True; 300, block; 300, 301; 301, expression_statement; 301, 302; 302, call; 302, 303; 302, 306; 303, attribute; 303, 304; 303, 305; 304, identifier:return_values; 305, identifier:extend; 306, argument_list; 306, 307; 307, list:[errors, tapers_per_freq_point]; 307, 308; 307, 309; 308, identifier:errors; 309, identifier:tapers_per_freq_point; 310, return_statement; 310, 311; 311, identifier:return_values
def sine_psd(data, delta, number_of_tapers=None, number_of_iterations=2, degree_of_smoothing=1.0, statistics=False, verbose=False): """ Wrapper method for the sine_psd subroutine in the library by German A. Prieto. The subroutine is in charge of estimating the adaptive sine multitaper as in Riedel and Sidorenko (1995). It outputs the power spectral density (PSD). This is done by performing a MSE adaptive estimation. First a pilot spectral estimate is used, and S" is estimated, in order to get te number of tapers to use, using (13) of Riedel and Sidorenko for a min square error spectrum. Unlike the prolate spheroidal multitapers, the sine multitaper adaptive process introduces a variable resolution and error in the frequency domain. Complete error information is contained in the output variables as the corridor of 1-standard-deviation errors, and in the number of tapers used at each frequency. The errors are estimated in the simplest way, from the number of degrees of freedom (two per taper), not by jack-knifing. The frequency resolution is found from K*fN/Nf where fN is the Nyquist frequency and Nf is the number of frequencies estimated. The adaptive process used is as follows. A quadratic fit to the log PSD within an adaptively determined frequency band is used to find an estimate of the local second derivative of the spectrum. This is used in an equation like R & S (13) for the MSE taper number, with the difference that a parabolic weighting is applied with increasing taper order. Because the FFTs of the tapered series can be found by resampling the FFT of the original time series (doubled in length and padded with zeros) only one FFT is required per series, no matter how many tapers are used. This makes the program fast. Compared with the Thomson multitaper programs, this code is not only fast but simple and short. The spectra associated with the sine tapers are weighted before averaging with a parabolically varying weight. The expression for the optimal number of tapers given by R & S must be modified since it gives an unbounded result near points where S" vanishes, which happens at many points in most spectra. This program restricts the rate of growth of the number of tapers so that a neighboring covering interval estimate is never completely contained in the next such interval. This method SHOULD not be used for sharp cutoffs or deep valleys, or small sample sizes. Instead use Thomson multitaper in mtspec in this same library. :param data: :class:`numpy.ndarray` Array with the data. :param delta: float Sample spacing of the data. :param number_of_tapers: integer/None, optional Number of tapers to use. If none is given, the library will perform an adaptive taper estimation with a varying number of tapers for each frequency. Defaults to None. :param number_of_iterations: integer, optional Number of iterations to perform. Values less than 2 will be set to 2. Defaults to 2. :param degree_of_smoothing: float, optional Degree of smoothing. Defaults to 1.0. :param statistics: bool, optional Calculates and returns statistics. See the notes in the docstring for further details. :param verbose: bool, optional Passed to the fortran library. Defaults to False. :return: Returns a list with :class:`numpy.ndarray`. See the note below for details. .. note:: This method will at return at least two arrays: The calculated spectrum and the corresponding frequencies. If statistics is True is will also return (in the given order) (multidimensional) arrays containing the 1-std errors (a simple dof estimate) and the number of tapers used for each frequency point. """ # Verbose mode on or off. if verbose is True: verbose = C.byref(C.c_char('y')) else: verbose = None # Set the number of tapers so it can be read by the library. if number_of_tapers is None: number_of_tapers = 0 # initialize _MtspecType to save some space mt = _MtspecType("float32") # Transform the data to work with the library. data = np.require(data, dtype=mt.float, requirements=[mt.order]) # Some variables necessary to call the library. npts = len(data) number_of_frequency_bins = int(npts / 2) + 1 # Create output arrays. frequency_bins = mt.empty(number_of_frequency_bins) spectrum = mt.empty(number_of_frequency_bins) # Create optional arrays or set to None. if statistics is True: # here an exception, mt sets the type float32, here we need int32 # that is do all the type and POINTER definition once by hand tapers_per_freq_point = np.empty(number_of_frequency_bins, dtype='int32', order=mt.order) tapers_per_freq_point_p = \ tapers_per_freq_point.ctypes.data_as(C.POINTER(C.c_int)) errors = mt.empty((number_of_frequency_bins, 2)) else: tapers_per_freq_point_p = errors = None # Call the library. Fortran passes pointers! mtspeclib.sine_psd_( C.byref(C.c_int(npts)), C.byref(C.c_float(delta)), mt.p(data), C.byref(C.c_int(number_of_tapers)), C.byref(C.c_int(number_of_iterations)), C.byref(C.c_float(degree_of_smoothing)), C.byref(C.c_int(number_of_frequency_bins)), mt.p(frequency_bins), mt.p(spectrum), tapers_per_freq_point_p, mt.p(errors), verbose) # Calculate return values. return_values = [spectrum, frequency_bins] if statistics is True: return_values.extend([errors, tapers_per_freq_point]) return return_values
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 25; 2, function_name:mt_deconvolve; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 3, 16; 3, 19; 3, 22; 4, identifier:data_a; 5, identifier:data_b; 6, identifier:delta; 7, default_parameter; 7, 8; 7, 9; 8, identifier:nfft; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:time_bandwidth; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:number_of_tapers; 15, None; 16, default_parameter; 16, 17; 16, 18; 17, identifier:weights; 18, string:"adaptive"; 19, default_parameter; 19, 20; 19, 21; 20, identifier:demean; 21, True; 22, default_parameter; 22, 23; 22, 24; 23, identifier:fmax; 24, float:0.0; 25, block; 25, 26; 25, 28; 25, 35; 25, 48; 25, 69; 25, 70; 25, 77; 25, 78; 25, 94; 25, 95; 25, 113; 25, 131; 25, 139; 25, 140; 25, 153; 25, 154; 25, 155; 25, 180; 25, 189; 25, 198; 25, 207; 25, 216; 25, 225; 25, 390; 26, expression_statement; 26, 27; 27, comment; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:npts; 31, call; 31, 32; 31, 33; 32, identifier:len; 33, argument_list; 33, 34; 34, identifier:data_a; 35, if_statement; 35, 36; 35, 42; 36, comparison_operator:!=; 36, 37; 36, 41; 37, call; 37, 38; 37, 39; 38, identifier:len; 39, argument_list; 39, 40; 40, identifier:data_b; 41, identifier:npts; 42, block; 42, 43; 43, raise_statement; 43, 44; 44, call; 44, 45; 44, 46; 45, identifier:ValueError; 46, argument_list; 46, 47; 47, string:"Input arrays must have the same length!"; 48, if_statement; 48, 49; 48, 52; 48, 57; 49, comparison_operator:is; 49, 50; 49, 51; 50, identifier:nfft; 51, None; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:nfft; 56, identifier:npts; 57, elif_clause; 57, 58; 57, 61; 58, comparison_operator:<; 58, 59; 58, 60; 59, identifier:nfft; 60, identifier:npts; 61, block; 61, 62; 62, raise_statement; 62, 63; 63, call; 63, 64; 63, 65; 64, identifier:ValueError; 65, argument_list; 65, 66; 66, concatenated_string; 66, 67; 66, 68; 67, string:"nfft must be larger then the number of samples in "; 68, string:"the array."; 69, comment; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:mt; 73, call; 73, 74; 73, 75; 74, identifier:_MtspecType; 75, argument_list; 75, 76; 76, string:"float32"; 77, comment; 78, if_statement; 78, 79; 78, 82; 79, comparison_operator:is; 79, 80; 79, 81; 80, identifier:number_of_tapers; 81, None; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:number_of_tapers; 86, binary_operator:-; 86, 87; 86, 93; 87, call; 87, 88; 87, 89; 88, identifier:int; 89, argument_list; 89, 90; 90, binary_operator:*; 90, 91; 90, 92; 91, integer:2; 92, identifier:time_bandwidth; 93, integer:1; 94, comment; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:data_a; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:np; 101, identifier:require; 102, argument_list; 102, 103; 102, 104; 102, 107; 103, identifier:data_a; 104, attribute; 104, 105; 104, 106; 105, identifier:mt; 106, identifier:float; 107, keyword_argument; 107, 108; 107, 109; 108, identifier:requirements; 109, list:[mt.order]; 109, 110; 110, attribute; 110, 111; 110, 112; 111, identifier:mt; 112, identifier:order; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:data_b; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:np; 119, identifier:require; 120, argument_list; 120, 121; 120, 122; 120, 125; 121, identifier:data_b; 122, attribute; 122, 123; 122, 124; 123, identifier:mt; 124, identifier:float; 125, keyword_argument; 125, 126; 125, 127; 126, identifier:requirements; 127, list:[mt.order]; 127, 128; 128, attribute; 128, 129; 128, 130; 129, identifier:mt; 130, identifier:order; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:nf; 134, binary_operator:+; 134, 135; 134, 138; 135, binary_operator://; 135, 136; 135, 137; 136, identifier:nfft; 137, integer:2; 138, integer:1; 139, comment; 140, if_statement; 140, 141; 140, 142; 140, 147; 141, identifier:demean; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:demean; 146, integer:1; 147, else_clause; 147, 148; 148, block; 148, 149; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:demean; 152, integer:0; 153, comment; 154, comment; 155, if_statement; 155, 156; 155, 159; 155, 164; 155, 173; 156, comparison_operator:==; 156, 157; 156, 158; 157, identifier:weights; 158, string:"constant"; 159, block; 159, 160; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:adaptive; 163, integer:1; 164, elif_clause; 164, 165; 164, 168; 165, comparison_operator:==; 165, 166; 165, 167; 166, identifier:weights; 167, string:"adaptive"; 168, block; 168, 169; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:adaptive; 172, integer:0; 173, else_clause; 173, 174; 174, block; 174, 175; 175, raise_statement; 175, 176; 176, call; 176, 177; 176, 178; 177, identifier:ValueError; 178, argument_list; 178, 179; 179, string:'Weights must be either "adaptive" or "constant".'; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:tfun; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:mt; 186, identifier:empty; 187, argument_list; 187, 188; 188, identifier:nfft; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:freq; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:mt; 195, identifier:empty; 196, argument_list; 196, 197; 197, identifier:nf; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:spec_ratio; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:mt; 204, identifier:empty; 205, argument_list; 205, 206; 206, identifier:nf; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:speci; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:mt; 213, identifier:empty; 214, argument_list; 214, 215; 215, identifier:nf; 216, expression_statement; 216, 217; 217, assignment; 217, 218; 217, 219; 218, identifier:specj; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:mt; 222, identifier:empty; 223, argument_list; 223, 224; 224, identifier:nf; 225, expression_statement; 225, 226; 226, call; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:mtspeclib; 229, identifier:mt_deconv_; 230, argument_list; 230, 231; 230, 245; 230, 259; 230, 273; 230, 279; 230, 285; 230, 299; 230, 313; 230, 327; 230, 338; 230, 344; 230, 350; 230, 356; 230, 362; 230, 368; 230, 379; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:C; 234, identifier:byref; 235, argument_list; 235, 236; 236, call; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:C; 239, identifier:c_int; 240, argument_list; 240, 241; 241, call; 241, 242; 241, 243; 242, identifier:int; 243, argument_list; 243, 244; 244, identifier:npts; 245, call; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:C; 248, identifier:byref; 249, argument_list; 249, 250; 250, call; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:C; 253, identifier:c_int; 254, argument_list; 254, 255; 255, call; 255, 256; 255, 257; 256, identifier:int; 257, argument_list; 257, 258; 258, identifier:nfft; 259, call; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:C; 262, identifier:byref; 263, argument_list; 263, 264; 264, call; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:C; 267, identifier:c_float; 268, argument_list; 268, 269; 269, call; 269, 270; 269, 271; 270, identifier:float; 271, argument_list; 271, 272; 272, identifier:delta; 273, call; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:mt; 276, identifier:p; 277, argument_list; 277, 278; 278, identifier:data_a; 279, call; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:mt; 282, identifier:p; 283, argument_list; 283, 284; 284, identifier:data_b; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:C; 288, identifier:byref; 289, argument_list; 289, 290; 290, call; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:C; 293, identifier:c_float; 294, argument_list; 294, 295; 295, call; 295, 296; 295, 297; 296, identifier:float; 297, argument_list; 297, 298; 298, identifier:time_bandwidth; 299, call; 299, 300; 299, 303; 300, attribute; 300, 301; 300, 302; 301, identifier:C; 302, identifier:byref; 303, argument_list; 303, 304; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:C; 307, identifier:c_int; 308, argument_list; 308, 309; 309, call; 309, 310; 309, 311; 310, identifier:int; 311, argument_list; 311, 312; 312, identifier:number_of_tapers; 313, call; 313, 314; 313, 317; 314, attribute; 314, 315; 314, 316; 315, identifier:C; 316, identifier:byref; 317, argument_list; 317, 318; 318, call; 318, 319; 318, 322; 319, attribute; 319, 320; 319, 321; 320, identifier:C; 321, identifier:c_int; 322, argument_list; 322, 323; 323, call; 323, 324; 323, 325; 324, identifier:int; 325, argument_list; 325, 326; 326, identifier:nf; 327, call; 327, 328; 327, 331; 328, attribute; 328, 329; 328, 330; 329, identifier:C; 330, identifier:byref; 331, argument_list; 331, 332; 332, call; 332, 333; 332, 336; 333, attribute; 333, 334; 333, 335; 334, identifier:C; 335, identifier:c_int; 336, argument_list; 336, 337; 337, identifier:adaptive; 338, call; 338, 339; 338, 342; 339, attribute; 339, 340; 339, 341; 340, identifier:mt; 341, identifier:p; 342, argument_list; 342, 343; 343, identifier:freq; 344, call; 344, 345; 344, 348; 345, attribute; 345, 346; 345, 347; 346, identifier:mt; 347, identifier:p; 348, argument_list; 348, 349; 349, identifier:tfun; 350, call; 350, 351; 350, 354; 351, attribute; 351, 352; 351, 353; 352, identifier:mt; 353, identifier:p; 354, argument_list; 354, 355; 355, identifier:spec_ratio; 356, call; 356, 357; 356, 360; 357, attribute; 357, 358; 357, 359; 358, identifier:mt; 359, identifier:p; 360, argument_list; 360, 361; 361, identifier:speci; 362, call; 362, 363; 362, 366; 363, attribute; 363, 364; 363, 365; 364, identifier:mt; 365, identifier:p; 366, argument_list; 366, 367; 367, identifier:specj; 368, call; 368, 369; 368, 372; 369, attribute; 369, 370; 369, 371; 370, identifier:C; 371, identifier:byref; 372, argument_list; 372, 373; 373, call; 373, 374; 373, 377; 374, attribute; 374, 375; 374, 376; 375, identifier:C; 376, identifier:c_int; 377, argument_list; 377, 378; 378, identifier:demean; 379, call; 379, 380; 379, 383; 380, attribute; 380, 381; 380, 382; 381, identifier:C; 382, identifier:byref; 383, argument_list; 383, 384; 384, call; 384, 385; 384, 388; 385, attribute; 385, 386; 385, 387; 386, identifier:C; 387, identifier:c_float; 388, argument_list; 388, 389; 389, identifier:fmax; 390, return_statement; 390, 391; 391, dictionary; 391, 392; 391, 395; 391, 398; 391, 401; 391, 404; 392, pair; 392, 393; 392, 394; 393, string:"frequencies"; 394, identifier:freq; 395, pair; 395, 396; 395, 397; 396, string:"deconvolved"; 397, identifier:tfun; 398, pair; 398, 399; 398, 400; 399, string:"spectral_ratio"; 400, identifier:spec_ratio; 401, pair; 401, 402; 401, 403; 402, string:"spectrum_a"; 403, identifier:speci; 404, pair; 404, 405; 404, 406; 405, string:"spectrum_b"; 406, identifier:specj
def mt_deconvolve(data_a, data_b, delta, nfft=None, time_bandwidth=None, number_of_tapers=None, weights="adaptive", demean=True, fmax=0.0): """ Deconvolve two time series using multitapers. This uses the eigencoefficients and the weights from the multitaper spectral estimations and more or less follows this paper: .. |br| raw:: html <br /> **Receiver Functions from Multiple-Taper Spectral Correlation Estimates** *Jeffrey Park, Vadim Levin* |br| Bulletin of the Seismological Society of America Dec 2000, 90 (6) 1507-1520 http://dx.doi.org/10.1785/0119990122 :type data_a: :class:`numpy.ndarray` :param data_a: Data for first time series. :type data_b: :class:`numpy.ndarray` :param data_b: Data for second time series. :type delta: float :param delta: Sample spacing of the data. :type nfft: int :param nfft: Number of points for the FFT. If ``nfft == None``, no zero padding will be applied before the FFT. :type time_bandwidth: float :param time_bandwidth: Time-bandwidth product. Common values are 2, 3, 4, and numbers in between. :type number_of_tapers: int :param number_of_tapers: Number of tapers to use. Defaults to ``int(2*time_bandwidth) - 1``. This is maximum senseful amount. More tapers will have no great influence on the final spectrum but increase the calculation time. Use fewer tapers for a faster calculation. :type weights: str :param weights: ``"adaptive"`` or ``"constant"`` weights. :type deman: bool :param demean: Force the complex TF to be demeaned. :type fmax: float :param fmax: Maximum frequency for lowpass cosine filter. Set this to zero to not have a filter. :return: Returns a dictionary with 5 :class:`numpy.ndarray`'s. See the note below. .. note:: Returns a dictionary with five arrays: * ``"deconvolved"``: Deconvolved time series. * ``"spectrum_a"``: Spectrum of the first time series. * ``"spectrum_b"``: Spectrum of the second time series. * ``"spectral_ratio"``: The ratio of both spectra. * ``"frequencies"``: The used frequency bins for the spectra. """ npts = len(data_a) if len(data_b) != npts: raise ValueError("Input arrays must have the same length!") if nfft is None: nfft = npts elif nfft < npts: raise ValueError("nfft must be larger then the number of samples in " "the array.") # Deconvolution utilizes the 32bit version. mt = _MtspecType("float32") # Use the optimal number of tapers in case no number is specified. if number_of_tapers is None: number_of_tapers = int(2 * time_bandwidth) - 1 # Transform the data to work with the library. data_a = np.require(data_a, mt.float, requirements=[mt.order]) data_b = np.require(data_b, mt.float, requirements=[mt.order]) nf = nfft // 2 + 1 # Internally uses integers if demean: demean = 1 else: demean = 0 # iad = 0 are adaptive, iad = 1 are constant weight - this is # counter intuitive. if weights == "constant": adaptive = 1 elif weights == "adaptive": adaptive = 0 else: raise ValueError('Weights must be either "adaptive" or "constant".') tfun = mt.empty(nfft) freq = mt.empty(nf) spec_ratio = mt.empty(nf) speci = mt.empty(nf) specj = mt.empty(nf) mtspeclib.mt_deconv_( C.byref(C.c_int(int(npts))), C.byref(C.c_int(int(nfft))), C.byref(C.c_float(float(delta))), mt.p(data_a), mt.p(data_b), C.byref(C.c_float(float(time_bandwidth))), C.byref(C.c_int(int(number_of_tapers))), C.byref(C.c_int(int(nf))), C.byref(C.c_int(adaptive)), mt.p(freq), mt.p(tfun), mt.p(spec_ratio), mt.p(speci), mt.p(specj), C.byref(C.c_int(demean)), C.byref(C.c_float(fmax))) return { "frequencies": freq, "deconvolved": tfun, "spectral_ratio": spec_ratio, "spectrum_a": speci, "spectrum_b": specj }
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:create_server; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:loop; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:as_coroutine; 10, False; 11, default_parameter; 11, 12; 11, 13; 12, identifier:protocol_factory; 13, None; 14, dictionary_splat_pattern; 14, 15; 15, identifier:server_config; 16, block; 16, 17; 16, 19; 16, 35; 16, 52; 16, 69; 17, expression_statement; 17, 18; 18, comment; 19, if_statement; 19, 20; 19, 23; 20, comparison_operator:is; 20, 21; 20, 22; 21, identifier:loop; 22, None; 23, block; 23, 24; 23, 27; 24, import_statement; 24, 25; 25, dotted_name; 25, 26; 26, identifier:asyncio; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:loop; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:asyncio; 33, identifier:get_event_loop; 34, argument_list; 35, if_statement; 35, 36; 35, 39; 36, comparison_operator:is; 36, 37; 36, 38; 37, identifier:protocol_factory; 38, None; 39, block; 39, 40; 39, 46; 40, import_from_statement; 40, 41; 40, 44; 41, dotted_name; 41, 42; 41, 43; 42, identifier:growler; 43, identifier:aio; 44, dotted_name; 44, 45; 45, identifier:GrowlerHTTPProtocol; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:protocol_factory; 49, attribute; 49, 50; 49, 51; 50, identifier:GrowlerHTTPProtocol; 51, identifier:get_factory; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:create_server; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:loop; 58, identifier:create_server; 59, argument_list; 59, 60; 59, 67; 60, call; 60, 61; 60, 62; 61, identifier:protocol_factory; 62, argument_list; 62, 63; 62, 64; 63, identifier:self; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:loop; 66, identifier:loop; 67, dictionary_splat; 67, 68; 68, identifier:server_config; 69, if_statement; 69, 70; 69, 71; 69, 74; 70, identifier:as_coroutine; 71, block; 71, 72; 72, return_statement; 72, 73; 73, identifier:create_server; 74, else_clause; 74, 75; 75, block; 75, 76; 76, return_statement; 76, 77; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:loop; 80, identifier:run_until_complete; 81, argument_list; 81, 82; 82, identifier:create_server
def create_server(self, loop=None, as_coroutine=False, protocol_factory=None, **server_config): """ Helper function which constructs a listening server, using the default growler.http.protocol.Protocol which responds to this app. This function exists only to remove boilerplate code for starting up a growler app when using asyncio. Args: as_coroutine (bool): If True, this function does not wait for the server to be created, and only returns the coroutine generator object returned by loop.create_server. This mode should be used when already inside an async function. The default mode is to call :method:`run_until_complete` on the loop paramter, blocking until the server is created and added to the event loop. server_config (mixed): These keyword arguments parameters are passed directly to the BaseEventLoop.create_server function. Consult their documentation for details. loop (BaseEventLoop): This is the asyncio event loop used to provide the underlying `create_server` method, and, if as_coroutine is False, will block until the server is created. protocol_factory (callable): Function returning an asyncio protocol object (or more specifically, a `growler.aio.GrowlerProtocol` object) to be called upon client connection. The default is the :class:`GrowlerHttpProtocol` factory function. **server_config (mixed): These keyword arguments parameters are passed directly to the BaseEventLoop.create_server function. Consult their documentation for details. Returns: asyncio.Server: The result of asyncio.BaseEventLoop.create_server which has been passed to the event loop and setup with the provided parameters. This is returned if gen_coroutine is False (default). asyncio.coroutine: An asyncio.coroutine which will produce the asyncio.Server from the provided configuration parameters. This is returned if gen_coroutine is True. """ if loop is None: import asyncio loop = asyncio.get_event_loop() if protocol_factory is None: from growler.aio import GrowlerHTTPProtocol protocol_factory = GrowlerHTTPProtocol.get_factory create_server = loop.create_server( protocol_factory(self, loop=loop), **server_config ) if as_coroutine: return create_server else: return loop.run_until_complete(create_server)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:event_emitter; 3, parameters; 3, 4; 3, 7; 3, 8; 4, default_parameter; 4, 5; 4, 6; 5, identifier:cls_; 6, None; 7, keyword_separator; 8, default_parameter; 8, 9; 8, 10; 9, identifier:events; 10, tuple; 10, 11; 11, string:'*'; 12, block; 12, 13; 12, 15; 12, 16; 12, 17; 12, 27; 12, 28; 12, 29; 12, 30; 12, 42; 12, 161; 13, expression_statement; 13, 14; 14, comment; 15, comment; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:event_dict; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:dict; 23, identifier:fromkeys; 24, argument_list; 24, 25; 24, 26; 25, identifier:events; 26, list:[]; 27, comment; 28, comment; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:allow_any_eventname; 33, comparison_operator:==; 33, 34; 33, 41; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:event_dict; 37, identifier:pop; 38, argument_list; 38, 39; 38, 40; 39, string:'*'; 40, False; 41, list:[]; 42, function_definition; 42, 43; 42, 44; 42, 46; 43, function_name:_event_emitter; 44, parameters; 44, 45; 45, identifier:cls; 46, block; 46, 47; 46, 118; 46, 147; 46, 153; 46, 159; 47, function_definition; 47, 48; 47, 49; 47, 53; 48, function_name:on; 49, parameters; 49, 50; 49, 51; 49, 52; 50, identifier:self; 51, identifier:name; 52, identifier:callback; 53, block; 53, 54; 53, 56; 53, 76; 53, 116; 54, expression_statement; 54, 55; 55, comment; 56, if_statement; 56, 57; 56, 68; 57, not_operator; 57, 58; 58, parenthesized_expression; 58, 59; 59, boolean_operator:or; 59, 60; 59, 64; 60, call; 60, 61; 60, 62; 61, identifier:callable; 62, argument_list; 62, 63; 63, identifier:callback; 64, call; 64, 65; 64, 66; 65, identifier:isawaitable; 66, argument_list; 66, 67; 67, identifier:callback; 68, block; 68, 69; 69, raise_statement; 69, 70; 70, call; 70, 71; 70, 72; 71, identifier:ValueError; 72, argument_list; 72, 73; 73, binary_operator:%; 73, 74; 73, 75; 74, string:"Callback not callable: %r"; 75, identifier:callback; 76, try_statement; 76, 77; 76, 87; 77, block; 77, 78; 78, expression_statement; 78, 79; 79, call; 79, 80; 79, 85; 80, attribute; 80, 81; 80, 84; 81, subscript; 81, 82; 81, 83; 82, identifier:event_dict; 83, identifier:name; 84, identifier:append; 85, argument_list; 85, 86; 86, identifier:callback; 87, except_clause; 87, 88; 87, 89; 88, identifier:KeyError; 89, block; 89, 90; 90, if_statement; 90, 91; 90, 92; 90, 100; 91, identifier:allow_any_eventname; 92, block; 92, 93; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 98; 95, subscript; 95, 96; 95, 97; 96, identifier:event_dict; 97, identifier:name; 98, list:[callback]; 98, 99; 99, identifier:callback; 100, else_clause; 100, 101; 101, block; 101, 102; 101, 111; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:msg; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, string:"Event Emitter has no event {!r}"; 108, identifier:format; 109, argument_list; 109, 110; 110, identifier:name; 111, raise_statement; 111, 112; 112, call; 112, 113; 112, 114; 113, identifier:KeyError; 114, argument_list; 114, 115; 115, identifier:msg; 116, return_statement; 116, 117; 117, identifier:self; 118, function_definition; 118, 119; 118, 120; 118, 123; 119, function_name:emit; 120, parameters; 120, 121; 120, 122; 121, identifier:self; 122, identifier:name; 123, block; 123, 124; 123, 126; 124, expression_statement; 124, 125; 125, comment; 126, for_statement; 126, 127; 126, 128; 126, 131; 127, identifier:cb; 128, subscript; 128, 129; 128, 130; 129, identifier:event_dict; 130, identifier:name; 131, block; 131, 132; 132, if_statement; 132, 133; 132, 137; 132, 141; 133, call; 133, 134; 133, 135; 134, identifier:isawaitable; 135, argument_list; 135, 136; 136, identifier:cb; 137, block; 137, 138; 138, expression_statement; 138, 139; 139, await; 139, 140; 140, identifier:cb; 141, else_clause; 141, 142; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, call; 144, 145; 144, 146; 145, identifier:cb; 146, argument_list; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:cls; 151, identifier:on; 152, identifier:on; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:cls; 157, identifier:emit; 158, identifier:emit; 159, return_statement; 159, 160; 160, identifier:cls; 161, if_statement; 161, 162; 161, 165; 161, 168; 162, comparison_operator:is; 162, 163; 162, 164; 163, identifier:cls_; 164, None; 165, block; 165, 166; 166, return_statement; 166, 167; 167, identifier:_event_emitter; 168, else_clause; 168, 169; 169, block; 169, 170; 170, return_statement; 170, 171; 171, call; 171, 172; 171, 173; 172, identifier:_event_emitter; 173, argument_list; 173, 174; 174, identifier:cls_
def event_emitter(cls_=None, *, events=('*', )): """ A class-decorator which will add the specified events and the methods 'on' and 'emit' to the class. """ # create a dictionary from items in the 'events' parameter and with empty # lists as values event_dict = dict.fromkeys(events, []) # if '*' was in the events tuple - then pop it out of the event_dict # and store the fact that we may allow any event name to be added to the # event emitter. allow_any_eventname = event_dict.pop('*', False) == [] def _event_emitter(cls): def on(self, name, callback): """ Add a callback to the event named 'name'. Returns the object for chained 'on' calls. """ if not (callable(callback) or isawaitable(callback)): raise ValueError("Callback not callable: %r" % callback) try: event_dict[name].append(callback) except KeyError: if allow_any_eventname: event_dict[name] = [callback] else: msg = "Event Emitter has no event {!r}".format(name) raise KeyError(msg) return self async def emit(self, name): """ Coroutine which executes each of the callbacks added to the event identified by 'name' """ for cb in event_dict[name]: if isawaitable(cb): await cb else: cb() cls.on = on cls.emit = emit return cls if cls_ is None: return _event_emitter else: return _event_emitter(cls_)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_build_production_name; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:glyph; 6, block; 6, 7; 6, 9; 6, 10; 6, 35; 6, 36; 6, 42; 6, 60; 6, 61; 6, 73; 6, 108; 6, 109; 6, 121; 6, 163; 6, 243; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, if_statement; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:_postscriptNames; 14, block; 14, 15; 14, 28; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:production_name; 18, call; 18, 19; 18, 24; 19, attribute; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:_postscriptNames; 23, identifier:get; 24, argument_list; 24, 25; 25, attribute; 25, 26; 25, 27; 26, identifier:glyph; 27, identifier:name; 28, return_statement; 28, 29; 29, conditional_expression:if; 29, 30; 29, 31; 29, 32; 30, identifier:production_name; 31, identifier:production_name; 32, attribute; 32, 33; 32, 34; 33, identifier:glyph; 34, identifier:name; 35, comment; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:unicode_val; 39, attribute; 39, 40; 39, 41; 40, identifier:glyph; 41, identifier:unicode; 42, if_statement; 42, 43; 42, 48; 43, comparison_operator:is; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:glyph; 46, identifier:unicode; 47, None; 48, block; 48, 49; 49, return_statement; 49, 50; 50, binary_operator:%; 50, 51; 50, 52; 51, string:'%s%04X'; 52, tuple; 52, 53; 52, 59; 53, conditional_expression:if; 53, 54; 53, 55; 53, 58; 54, string:'u'; 55, comparison_operator:>; 55, 56; 55, 57; 56, identifier:unicode_val; 57, integer:0xffff; 58, string:'uni'; 59, identifier:unicode_val; 60, comment; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:parts; 64, call; 64, 65; 64, 70; 65, attribute; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:glyph; 68, identifier:name; 69, identifier:rsplit; 70, argument_list; 70, 71; 70, 72; 71, string:'.'; 72, integer:1; 73, if_statement; 73, 74; 73, 88; 74, boolean_operator:and; 74, 75; 74, 81; 75, comparison_operator:==; 75, 76; 75, 80; 76, call; 76, 77; 76, 78; 77, identifier:len; 78, argument_list; 78, 79; 79, identifier:parts; 80, integer:2; 81, comparison_operator:in; 81, 82; 81, 85; 82, subscript; 82, 83; 82, 84; 83, identifier:parts; 84, integer:0; 85, attribute; 85, 86; 85, 87; 86, identifier:self; 87, identifier:glyphSet; 88, block; 88, 89; 89, return_statement; 89, 90; 90, binary_operator:%; 90, 91; 90, 92; 91, string:'%s.%s'; 92, tuple; 92, 93; 92, 105; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:self; 96, identifier:_build_production_name; 97, argument_list; 97, 98; 98, subscript; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:glyphSet; 102, subscript; 102, 103; 102, 104; 103, identifier:parts; 104, integer:0; 105, subscript; 105, 106; 105, 107; 106, identifier:parts; 107, integer:1; 108, comment; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:parts; 112, call; 112, 113; 112, 118; 113, attribute; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:glyph; 116, identifier:name; 117, identifier:split; 118, argument_list; 118, 119; 118, 120; 119, string:'.'; 120, integer:1; 121, if_statement; 121, 122; 121, 128; 121, 150; 122, comparison_operator:==; 122, 123; 122, 127; 123, call; 123, 124; 123, 125; 124, identifier:len; 125, argument_list; 125, 126; 126, identifier:parts; 127, integer:2; 128, block; 128, 129; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:liga_parts; 132, list_comprehension; 132, 133; 132, 140; 133, binary_operator:%; 133, 134; 133, 135; 134, string:'%s.%s'; 135, tuple; 135, 136; 135, 137; 136, identifier:n; 137, subscript; 137, 138; 137, 139; 138, identifier:parts; 139, integer:1; 140, for_in_clause; 140, 141; 140, 142; 141, identifier:n; 142, call; 142, 143; 142, 148; 143, attribute; 143, 144; 143, 147; 144, subscript; 144, 145; 144, 146; 145, identifier:parts; 146, integer:0; 147, identifier:split; 148, argument_list; 148, 149; 149, string:'_'; 150, else_clause; 150, 151; 151, block; 151, 152; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:liga_parts; 155, call; 155, 156; 155, 161; 156, attribute; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:glyph; 159, identifier:name; 160, identifier:split; 161, argument_list; 161, 162; 162, string:'_'; 163, if_statement; 163, 164; 163, 182; 164, boolean_operator:and; 164, 165; 164, 171; 165, comparison_operator:>; 165, 166; 165, 170; 166, call; 166, 167; 166, 168; 167, identifier:len; 168, argument_list; 168, 169; 169, identifier:liga_parts; 170, integer:1; 171, call; 171, 172; 171, 173; 172, identifier:all; 173, generator_expression; 173, 174; 173, 179; 174, comparison_operator:in; 174, 175; 174, 176; 175, identifier:n; 176, attribute; 176, 177; 176, 178; 177, identifier:self; 178, identifier:glyphSet; 179, for_in_clause; 179, 180; 179, 181; 180, identifier:n; 181, identifier:liga_parts; 182, block; 182, 183; 182, 197; 182, 224; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:unicode_vals; 186, list_comprehension; 186, 187; 186, 194; 187, attribute; 187, 188; 187, 193; 188, subscript; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:self; 191, identifier:glyphSet; 192, identifier:n; 193, identifier:unicode; 194, for_in_clause; 194, 195; 194, 196; 195, identifier:n; 196, identifier:liga_parts; 197, if_statement; 197, 198; 197, 209; 198, call; 198, 199; 198, 200; 199, identifier:all; 200, generator_expression; 200, 201; 200, 206; 201, boolean_operator:and; 201, 202; 201, 203; 202, identifier:v; 203, comparison_operator:<=; 203, 204; 203, 205; 204, identifier:v; 205, integer:0xffff; 206, for_in_clause; 206, 207; 206, 208; 207, identifier:v; 208, identifier:unicode_vals; 209, block; 209, 210; 210, return_statement; 210, 211; 211, binary_operator:+; 211, 212; 211, 213; 212, string:'uni'; 213, call; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, string:''; 216, identifier:join; 217, generator_expression; 217, 218; 217, 221; 218, binary_operator:%; 218, 219; 218, 220; 219, string:'%04X'; 220, identifier:v; 221, for_in_clause; 221, 222; 221, 223; 222, identifier:v; 223, identifier:unicode_vals; 224, return_statement; 224, 225; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, string:'_'; 228, identifier:join; 229, generator_expression; 229, 230; 229, 240; 230, call; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:self; 233, identifier:_build_production_name; 234, argument_list; 234, 235; 235, subscript; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:self; 238, identifier:glyphSet; 239, identifier:n; 240, for_in_clause; 240, 241; 240, 242; 241, identifier:n; 242, identifier:liga_parts; 243, return_statement; 243, 244; 244, attribute; 244, 245; 244, 246; 245, identifier:glyph; 246, identifier:name
def _build_production_name(self, glyph): """Build a production name for a single glyph.""" # use PostScript names from UFO lib if available if self._postscriptNames: production_name = self._postscriptNames.get(glyph.name) return production_name if production_name else glyph.name # use name derived from unicode value unicode_val = glyph.unicode if glyph.unicode is not None: return '%s%04X' % ( 'u' if unicode_val > 0xffff else 'uni', unicode_val) # use production name + last (non-script) suffix if possible parts = glyph.name.rsplit('.', 1) if len(parts) == 2 and parts[0] in self.glyphSet: return '%s.%s' % ( self._build_production_name(self.glyphSet[parts[0]]), parts[1]) # use ligature name, making sure to look up components with suffixes parts = glyph.name.split('.', 1) if len(parts) == 2: liga_parts = ['%s.%s' % (n, parts[1]) for n in parts[0].split('_')] else: liga_parts = glyph.name.split('_') if len(liga_parts) > 1 and all(n in self.glyphSet for n in liga_parts): unicode_vals = [self.glyphSet[n].unicode for n in liga_parts] if all(v and v <= 0xffff for v in unicode_vals): return 'uni' + ''.join('%04X' % v for v in unicode_vals) return '_'.join( self._build_production_name(self.glyphSet[n]) for n in liga_parts) return glyph.name
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:makeOfficialGlyphOrder; 3, parameters; 3, 4; 3, 5; 4, identifier:font; 5, default_parameter; 5, 6; 5, 7; 6, identifier:glyphOrder; 7, None; 8, block; 8, 9; 8, 11; 8, 25; 8, 36; 8, 40; 8, 59; 8, 83; 8, 93; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 15; 12, comparison_operator:is; 12, 13; 12, 14; 13, identifier:glyphOrder; 14, None; 15, block; 15, 16; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:glyphOrder; 19, call; 19, 20; 19, 21; 20, identifier:getattr; 21, argument_list; 21, 22; 21, 23; 21, 24; 22, identifier:font; 23, string:"glyphOrder"; 24, tuple; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:names; 28, call; 28, 29; 28, 30; 29, identifier:set; 30, argument_list; 30, 31; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:font; 34, identifier:keys; 35, argument_list; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:order; 39, list:[]; 40, if_statement; 40, 41; 40, 44; 41, comparison_operator:in; 41, 42; 41, 43; 42, string:".notdef"; 43, identifier:names; 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:names; 49, identifier:remove; 50, argument_list; 50, 51; 51, string:".notdef"; 52, expression_statement; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:order; 56, identifier:append; 57, argument_list; 57, 58; 58, string:".notdef"; 59, for_statement; 59, 60; 59, 61; 59, 62; 60, identifier:name; 61, identifier:glyphOrder; 62, block; 62, 63; 62, 69; 62, 76; 63, if_statement; 63, 64; 63, 67; 64, comparison_operator:not; 64, 65; 64, 66; 65, identifier:name; 66, identifier:names; 67, block; 67, 68; 68, continue_statement; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:names; 73, identifier:remove; 74, argument_list; 74, 75; 75, identifier:name; 76, expression_statement; 76, 77; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:order; 80, identifier:append; 81, argument_list; 81, 82; 82, identifier:name; 83, expression_statement; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:order; 87, identifier:extend; 88, argument_list; 88, 89; 89, call; 89, 90; 89, 91; 90, identifier:sorted; 91, argument_list; 91, 92; 92, identifier:names; 93, return_statement; 93, 94; 94, identifier:order
def makeOfficialGlyphOrder(font, glyphOrder=None): """ Make the final glyph order for 'font'. If glyphOrder is None, try getting the font.glyphOrder list. If not explicit glyphOrder is defined, sort glyphs alphabetically. If ".notdef" glyph is present in the font, force this to always be the first glyph (at index 0). """ if glyphOrder is None: glyphOrder = getattr(font, "glyphOrder", ()) names = set(font.keys()) order = [] if ".notdef" in names: names.remove(".notdef") order.append(".notdef") for name in glyphOrder: if name not in names: continue names.remove(name) order.append(name) order.extend(sorted(names)) return order
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:from_layer; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:cls; 5, identifier:font; 6, default_parameter; 6, 7; 6, 8; 7, identifier:layerName; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:copy; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:skipExportGlyphs; 14, None; 15, block; 15, 16; 15, 18; 15, 41; 15, 89; 15, 90; 15, 91; 15, 197; 15, 210; 16, expression_statement; 16, 17; 17, comment; 18, if_statement; 18, 19; 18, 22; 18, 31; 19, comparison_operator:is; 19, 20; 19, 21; 20, identifier:layerName; 21, None; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:layer; 26, subscript; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:font; 29, identifier:layers; 30, identifier:layerName; 31, else_clause; 31, 32; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:layer; 36, attribute; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:font; 39, identifier:layers; 40, identifier:defaultLayer; 41, if_statement; 41, 42; 41, 43; 41, 65; 42, identifier:copy; 43, block; 43, 44; 43, 54; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:self; 47, call; 47, 48; 47, 49; 48, identifier:_copyLayer; 49, argument_list; 49, 50; 49, 51; 50, identifier:layer; 51, keyword_argument; 51, 52; 51, 53; 52, identifier:obj_type; 53, identifier:cls; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:lib; 59, call; 59, 60; 59, 61; 60, identifier:deepcopy; 61, argument_list; 61, 62; 62, attribute; 62, 63; 62, 64; 63, identifier:layer; 64, identifier:lib; 65, else_clause; 65, 66; 66, block; 66, 67; 66, 81; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:self; 70, call; 70, 71; 70, 72; 71, identifier:cls; 72, generator_expression; 72, 73; 72, 78; 73, tuple; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:g; 76, identifier:name; 77, identifier:g; 78, for_in_clause; 78, 79; 78, 80; 79, identifier:g; 80, identifier:layer; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:self; 85, identifier:lib; 86, attribute; 86, 87; 86, 88; 87, identifier:layer; 88, identifier:lib; 89, comment; 90, comment; 91, if_statement; 91, 92; 91, 93; 92, identifier:skipExportGlyphs; 93, block; 93, 94; 93, 183; 93, 184; 94, for_statement; 94, 95; 94, 96; 94, 101; 95, identifier:glyph; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:self; 99, identifier:values; 100, argument_list; 101, block; 101, 102; 102, if_statement; 102, 103; 102, 116; 103, call; 103, 104; 103, 105; 104, identifier:any; 105, generator_expression; 105, 106; 105, 111; 106, comparison_operator:in; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:c; 109, identifier:baseGlyph; 110, identifier:skipExportGlyphs; 111, for_in_clause; 111, 112; 111, 113; 112, identifier:c; 113, attribute; 113, 114; 113, 115; 114, identifier:glyph; 115, identifier:components; 116, block; 116, 117; 116, 128; 117, expression_statement; 117, 118; 118, call; 118, 119; 118, 120; 119, identifier:deepCopyContours; 120, argument_list; 120, 121; 120, 122; 120, 123; 120, 124; 120, 127; 121, identifier:self; 122, identifier:glyph; 123, identifier:glyph; 124, call; 124, 125; 124, 126; 125, identifier:Transform; 126, argument_list; 127, identifier:skipExportGlyphs; 128, if_statement; 128, 129; 128, 134; 128, 135; 128, 159; 129, call; 129, 130; 129, 131; 130, identifier:hasattr; 131, argument_list; 131, 132; 131, 133; 132, identifier:glyph; 133, string:"removeComponent"; 134, comment; 135, block; 135, 136; 136, for_statement; 136, 137; 136, 138; 136, 151; 137, identifier:c; 138, list_comprehension; 138, 139; 138, 140; 138, 145; 139, identifier:component; 140, for_in_clause; 140, 141; 140, 142; 141, identifier:component; 142, attribute; 142, 143; 142, 144; 143, identifier:glyph; 144, identifier:components; 145, if_clause; 145, 146; 146, comparison_operator:in; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:component; 149, identifier:baseGlyph; 150, identifier:skipExportGlyphs; 151, block; 151, 152; 152, expression_statement; 152, 153; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:glyph; 156, identifier:removeComponent; 157, argument_list; 157, 158; 158, identifier:c; 159, else_clause; 159, 160; 159, 161; 160, comment; 161, block; 161, 162; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 170; 164, subscript; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:glyph; 167, identifier:components; 168, slice; 168, 169; 169, colon; 170, list_comprehension; 170, 171; 170, 172; 170, 177; 171, identifier:c; 172, for_in_clause; 172, 173; 172, 174; 173, identifier:c; 174, attribute; 174, 175; 174, 176; 175, identifier:glyph; 176, identifier:components; 177, if_clause; 177, 178; 178, comparison_operator:not; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:c; 181, identifier:baseGlyph; 182, identifier:skipExportGlyphs; 183, comment; 184, for_statement; 184, 185; 184, 186; 184, 187; 185, identifier:glyph_name; 186, identifier:skipExportGlyphs; 187, block; 187, 188; 188, if_statement; 188, 189; 188, 192; 189, comparison_operator:in; 189, 190; 189, 191; 190, identifier:glyph_name; 191, identifier:self; 192, block; 192, 193; 193, delete_statement; 193, 194; 194, subscript; 194, 195; 194, 196; 195, identifier:self; 196, identifier:glyph_name; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:self; 201, identifier:name; 202, conditional_expression:if; 202, 203; 202, 206; 202, 209; 203, attribute; 203, 204; 203, 205; 204, identifier:layer; 205, identifier:name; 206, comparison_operator:is; 206, 207; 206, 208; 207, identifier:layerName; 208, None; 209, None; 210, return_statement; 210, 211; 211, identifier:self
def from_layer(cls, font, layerName=None, copy=False, skipExportGlyphs=None): """Return a mapping of glyph names to glyph objects from `font`.""" if layerName is not None: layer = font.layers[layerName] else: layer = font.layers.defaultLayer if copy: self = _copyLayer(layer, obj_type=cls) self.lib = deepcopy(layer.lib) else: self = cls((g.name, g) for g in layer) self.lib = layer.lib # If any glyphs in the skipExportGlyphs list are used as components, decompose # them in the containing glyphs... if skipExportGlyphs: for glyph in self.values(): if any(c.baseGlyph in skipExportGlyphs for c in glyph.components): deepCopyContours(self, glyph, glyph, Transform(), skipExportGlyphs) if hasattr(glyph, "removeComponent"): # defcon for c in [ component for component in glyph.components if component.baseGlyph in skipExportGlyphs ]: glyph.removeComponent(c) else: # ufoLib2 glyph.components[:] = [ c for c in glyph.components if c.baseGlyph not in skipExportGlyphs ] # ... and then remove them from the glyph set, if even present. for glyph_name in skipExportGlyphs: if glyph_name in self: del self[glyph_name] self.name = layer.name if layerName is not None else None return self
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 38; 2, function_name:compileInterpolatableTTFs; 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:ufos; 5, default_parameter; 5, 6; 5, 7; 6, identifier:preProcessorClass; 7, identifier:TTFInterpolatablePreProcessor; 8, default_parameter; 8, 9; 8, 10; 9, identifier:outlineCompilerClass; 10, identifier:OutlineTTFCompiler; 11, default_parameter; 11, 12; 11, 13; 12, identifier:featureCompilerClass; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:featureWriters; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:glyphOrder; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:useProductionNames; 22, None; 23, default_parameter; 23, 24; 23, 25; 24, identifier:cubicConversionError; 25, None; 26, default_parameter; 26, 27; 26, 28; 27, identifier:reverseDirection; 28, True; 29, default_parameter; 29, 30; 29, 31; 30, identifier:inplace; 31, False; 32, default_parameter; 32, 33; 32, 34; 33, identifier:layerNames; 34, None; 35, default_parameter; 35, 36; 35, 37; 36, identifier:skipExportGlyphs; 37, None; 38, block; 38, 39; 38, 41; 38, 47; 38, 62; 38, 72; 38, 102; 38, 109; 38, 131; 38, 139; 39, expression_statement; 39, 40; 40, comment; 41, import_from_statement; 41, 42; 41, 45; 42, dotted_name; 42, 43; 42, 44; 43, identifier:ufo2ft; 44, identifier:util; 45, dotted_name; 45, 46; 46, identifier:_LazyFontName; 47, if_statement; 47, 48; 47, 51; 48, comparison_operator:is; 48, 49; 48, 50; 49, identifier:layerNames; 50, None; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:layerNames; 55, binary_operator:*; 55, 56; 55, 58; 56, list:[None]; 56, 57; 57, None; 58, call; 58, 59; 58, 60; 59, identifier:len; 60, argument_list; 60, 61; 61, identifier:ufos; 62, assert_statement; 62, 63; 63, comparison_operator:==; 63, 64; 63, 68; 64, call; 64, 65; 64, 66; 65, identifier:len; 66, argument_list; 66, 67; 67, identifier:ufos; 68, call; 68, 69; 68, 70; 69, identifier:len; 70, argument_list; 70, 71; 71, identifier:layerNames; 72, if_statement; 72, 73; 72, 76; 73, comparison_operator:is; 73, 74; 73, 75; 74, identifier:skipExportGlyphs; 75, None; 76, block; 76, 77; 76, 83; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:skipExportGlyphs; 80, call; 80, 81; 80, 82; 81, identifier:set; 82, argument_list; 83, for_statement; 83, 84; 83, 85; 83, 86; 84, identifier:ufo; 85, identifier:ufos; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:skipExportGlyphs; 91, identifier:update; 92, argument_list; 92, 93; 93, call; 93, 94; 93, 99; 94, attribute; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:ufo; 97, identifier:lib; 98, identifier:get; 99, argument_list; 99, 100; 99, 101; 100, string:"public.skipExportGlyphs"; 101, list:[]; 102, expression_statement; 102, 103; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:logger; 106, identifier:info; 107, argument_list; 107, 108; 108, string:"Pre-processing glyphs"; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:preProcessor; 112, call; 112, 113; 112, 114; 113, identifier:preProcessorClass; 114, argument_list; 114, 115; 114, 116; 114, 119; 114, 122; 114, 125; 114, 128; 115, identifier:ufos; 116, keyword_argument; 116, 117; 116, 118; 117, identifier:inplace; 118, identifier:inplace; 119, keyword_argument; 119, 120; 119, 121; 120, identifier:conversionError; 121, identifier:cubicConversionError; 122, keyword_argument; 122, 123; 122, 124; 123, identifier:reverseDirection; 124, identifier:reverseDirection; 125, keyword_argument; 125, 126; 125, 127; 126, identifier:layerNames; 127, identifier:layerNames; 128, keyword_argument; 128, 129; 128, 130; 129, identifier:skipExportGlyphs; 130, identifier:skipExportGlyphs; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:glyphSets; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:preProcessor; 137, identifier:process; 138, argument_list; 139, for_statement; 139, 140; 139, 144; 139, 150; 140, pattern_list; 140, 141; 140, 142; 140, 143; 141, identifier:ufo; 142, identifier:glyphSet; 143, identifier:layerName; 144, call; 144, 145; 144, 146; 145, identifier:zip; 146, argument_list; 146, 147; 146, 148; 146, 149; 147, identifier:ufos; 148, identifier:glyphSets; 149, identifier:layerNames; 150, block; 150, 151; 150, 158; 150, 182; 150, 201; 150, 209; 150, 210; 150, 211; 150, 231; 150, 242; 150, 251; 150, 281; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:fontName; 154, call; 154, 155; 154, 156; 155, identifier:_LazyFontName; 156, argument_list; 156, 157; 157, identifier:ufo; 158, if_statement; 158, 159; 158, 162; 158, 172; 159, comparison_operator:is; 159, 160; 159, 161; 160, identifier:layerName; 161, None; 162, block; 162, 163; 163, expression_statement; 163, 164; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:logger; 167, identifier:info; 168, argument_list; 168, 169; 168, 170; 168, 171; 169, string:"Building OpenType tables for %s-%s"; 170, identifier:fontName; 171, identifier:layerName; 172, else_clause; 172, 173; 173, block; 173, 174; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:logger; 178, identifier:info; 179, argument_list; 179, 180; 179, 181; 180, string:"Building OpenType tables for %s"; 181, identifier:fontName; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:outlineCompiler; 185, call; 185, 186; 185, 187; 186, identifier:outlineCompilerClass; 187, argument_list; 187, 188; 187, 189; 187, 192; 187, 195; 188, identifier:ufo; 189, keyword_argument; 189, 190; 189, 191; 190, identifier:glyphSet; 191, identifier:glyphSet; 192, keyword_argument; 192, 193; 192, 194; 193, identifier:glyphOrder; 194, identifier:glyphOrder; 195, keyword_argument; 195, 196; 195, 197; 196, identifier:tables; 197, conditional_expression:if; 197, 198; 197, 199; 197, 200; 198, identifier:SPARSE_TTF_MASTER_TABLES; 199, identifier:layerName; 200, None; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 204; 203, identifier:ttf; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:outlineCompiler; 207, identifier:compile; 208, argument_list; 209, comment; 210, comment; 211, if_statement; 211, 212; 211, 215; 212, comparison_operator:is; 212, 213; 212, 214; 213, identifier:layerName; 214, None; 215, block; 215, 216; 216, expression_statement; 216, 217; 217, call; 217, 218; 217, 219; 218, identifier:compileFeatures; 219, argument_list; 219, 220; 219, 221; 219, 222; 219, 225; 219, 228; 220, identifier:ufo; 221, identifier:ttf; 222, keyword_argument; 222, 223; 222, 224; 223, identifier:glyphSet; 224, identifier:glyphSet; 225, keyword_argument; 225, 226; 225, 227; 226, identifier:featureWriters; 227, identifier:featureWriters; 228, keyword_argument; 228, 229; 228, 230; 229, identifier:featureCompilerClass; 230, identifier:featureCompilerClass; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:postProcessor; 234, call; 234, 235; 234, 236; 235, identifier:PostProcessor; 236, argument_list; 236, 237; 236, 238; 236, 239; 237, identifier:ttf; 238, identifier:ufo; 239, keyword_argument; 239, 240; 239, 241; 240, identifier:glyphSet; 241, identifier:glyphSet; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 245; 244, identifier:ttf; 245, call; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:postProcessor; 248, identifier:process; 249, argument_list; 249, 250; 250, identifier:useProductionNames; 251, if_statement; 251, 252; 251, 255; 251, 256; 251, 257; 251, 258; 251, 259; 251, 260; 251, 261; 251, 262; 252, comparison_operator:is; 252, 253; 252, 254; 253, identifier:layerName; 254, None; 255, comment; 256, comment; 257, comment; 258, comment; 259, comment; 260, comment; 261, comment; 262, block; 262, 263; 262, 272; 263, expression_statement; 263, 264; 264, assignment; 264, 265; 264, 270; 265, attribute; 265, 266; 265, 269; 266, subscript; 266, 267; 266, 268; 267, identifier:ttf; 268, string:"post"; 269, identifier:underlinePosition; 270, unary_operator:-; 270, 271; 271, integer:0x8000; 272, expression_statement; 272, 273; 273, assignment; 273, 274; 273, 279; 274, attribute; 274, 275; 274, 278; 275, subscript; 275, 276; 275, 277; 276, identifier:ttf; 277, string:"post"; 278, identifier:underlineThickness; 279, unary_operator:-; 279, 280; 280, integer:0x8000; 281, expression_statement; 281, 282; 282, yield; 282, 283; 283, identifier:ttf
def compileInterpolatableTTFs( ufos, preProcessorClass=TTFInterpolatablePreProcessor, outlineCompilerClass=OutlineTTFCompiler, featureCompilerClass=None, featureWriters=None, glyphOrder=None, useProductionNames=None, cubicConversionError=None, reverseDirection=True, inplace=False, layerNames=None, skipExportGlyphs=None, ): """Create FontTools TrueType fonts from a list of UFOs with interpolatable outlines. Cubic curves are converted compatibly to quadratic curves using the Cu2Qu conversion algorithm. Return an iterator object that yields a TTFont instance for each UFO. *layerNames* refers to the layer names to use glyphs from in the order of the UFOs in *ufos*. By default, this is a list of `[None]` times the number of UFOs, i.e. using the default layer from all the UFOs. When the layerName is not None for a given UFO, the corresponding TTFont object will contain only a minimum set of tables ("head", "hmtx", "glyf", "loca", "maxp", "post" and "vmtx"), and no OpenType layout tables. *skipExportGlyphs* is a list or set of glyph names to not be exported to the final font. If these glyphs are used as components in any other glyph, those components get decomposed. If the parameter is not passed in, the union of all UFO's "public.skipExportGlyphs" lib keys will be used. If they don't exist, all glyphs are exported. UFO groups and kerning will be pruned of skipped glyphs. """ from ufo2ft.util import _LazyFontName if layerNames is None: layerNames = [None] * len(ufos) assert len(ufos) == len(layerNames) if skipExportGlyphs is None: skipExportGlyphs = set() for ufo in ufos: skipExportGlyphs.update(ufo.lib.get("public.skipExportGlyphs", [])) logger.info("Pre-processing glyphs") preProcessor = preProcessorClass( ufos, inplace=inplace, conversionError=cubicConversionError, reverseDirection=reverseDirection, layerNames=layerNames, skipExportGlyphs=skipExportGlyphs, ) glyphSets = preProcessor.process() for ufo, glyphSet, layerName in zip(ufos, glyphSets, layerNames): fontName = _LazyFontName(ufo) if layerName is not None: logger.info("Building OpenType tables for %s-%s", fontName, layerName) else: logger.info("Building OpenType tables for %s", fontName) outlineCompiler = outlineCompilerClass( ufo, glyphSet=glyphSet, glyphOrder=glyphOrder, tables=SPARSE_TTF_MASTER_TABLES if layerName else None, ) ttf = outlineCompiler.compile() # Only the default layer is likely to have all glyphs used in feature # code. if layerName is None: compileFeatures( ufo, ttf, glyphSet=glyphSet, featureWriters=featureWriters, featureCompilerClass=featureCompilerClass, ) postProcessor = PostProcessor(ttf, ufo, glyphSet=glyphSet) ttf = postProcessor.process(useProductionNames) if layerName is not None: # for sparse masters (i.e. containing only a subset of the glyphs), we # need to include the post table in order to store glyph names, so that # fontTools.varLib can interpolate glyphs with same name across masters. # However we want to prevent the underlinePosition/underlineThickness # fields in such sparse masters to be included when computing the deltas # for the MVAR table. Thus, we set them to this unlikely, limit value # (-36768) which is a signal varLib should ignore them when building MVAR. ttf["post"].underlinePosition = -0x8000 ttf["post"].underlineThickness = -0x8000 yield ttf
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_propagate_glyph_anchors; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:glyphSet; 5, identifier:composite; 6, identifier:processed; 7, block; 7, 8; 7, 10; 7, 18; 7, 27; 7, 34; 7, 38; 7, 42; 7, 48; 7, 52; 7, 145; 7, 227; 7, 260; 7, 271; 7, 272; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 16; 11, comparison_operator:in; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:composite; 14, identifier:name; 15, identifier:processed; 16, block; 16, 17; 17, return_statement; 18, expression_statement; 18, 19; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:processed; 22, identifier:add; 23, argument_list; 23, 24; 24, attribute; 24, 25; 24, 26; 25, identifier:composite; 26, identifier:name; 27, if_statement; 27, 28; 27, 32; 28, not_operator; 28, 29; 29, attribute; 29, 30; 29, 31; 30, identifier:composite; 31, identifier:components; 32, block; 32, 33; 33, return_statement; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:base_components; 37, list:[]; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:mark_components; 41, list:[]; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:anchor_names; 45, call; 45, 46; 45, 47; 46, identifier:set; 47, argument_list; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:to_add; 51, dictionary; 52, for_statement; 52, 53; 52, 54; 52, 57; 53, identifier:component; 54, attribute; 54, 55; 54, 56; 55, identifier:composite; 56, identifier:components; 57, block; 57, 58; 58, try_statement; 58, 59; 58, 68; 58, 90; 59, block; 59, 60; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:glyph; 63, subscript; 63, 64; 63, 65; 64, identifier:glyphSet; 65, attribute; 65, 66; 65, 67; 66, identifier:component; 67, identifier:baseGlyph; 68, except_clause; 68, 69; 68, 70; 69, identifier:KeyError; 70, block; 70, 71; 71, expression_statement; 71, 72; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:logger; 75, identifier:warning; 76, argument_list; 76, 77; 77, call; 77, 78; 77, 83; 78, attribute; 78, 79; 78, 82; 79, concatenated_string; 79, 80; 79, 81; 80, string:'Anchors not propagated for inexistent component {} '; 81, string:'in glyph {}'; 82, identifier:format; 83, argument_list; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:component; 86, identifier:baseGlyph; 87, attribute; 87, 88; 87, 89; 88, identifier:composite; 89, identifier:name; 90, else_clause; 90, 91; 91, block; 91, 92; 91, 99; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 95; 94, identifier:_propagate_glyph_anchors; 95, argument_list; 95, 96; 95, 97; 95, 98; 96, identifier:glyphSet; 97, identifier:glyph; 98, identifier:processed; 99, if_statement; 99, 100; 99, 116; 99, 124; 100, call; 100, 101; 100, 102; 101, identifier:any; 102, generator_expression; 102, 103; 102, 111; 103, call; 103, 104; 103, 109; 104, attribute; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:a; 107, identifier:name; 108, identifier:startswith; 109, argument_list; 109, 110; 110, string:'_'; 111, for_in_clause; 111, 112; 111, 113; 112, identifier:a; 113, attribute; 113, 114; 113, 115; 114, identifier:glyph; 115, identifier:anchors; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:mark_components; 121, identifier:append; 122, argument_list; 122, 123; 123, identifier:component; 124, else_clause; 124, 125; 125, block; 125, 126; 125, 133; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:base_components; 130, identifier:append; 131, argument_list; 131, 132; 132, identifier:component; 133, expression_statement; 133, 134; 134, augmented_assignment:|=; 134, 135; 134, 136; 135, identifier:anchor_names; 136, set_comprehension; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:a; 139, identifier:name; 140, for_in_clause; 140, 141; 140, 142; 141, identifier:a; 142, attribute; 142, 143; 142, 144; 143, identifier:glyph; 144, identifier:anchors; 145, if_statement; 145, 146; 145, 155; 145, 156; 145, 157; 145, 158; 146, boolean_operator:and; 146, 147; 146, 151; 147, boolean_operator:and; 147, 148; 147, 149; 148, identifier:mark_components; 149, not_operator; 149, 150; 150, identifier:base_components; 151, call; 151, 152; 151, 153; 152, identifier:_is_ligature_mark; 153, argument_list; 153, 154; 154, identifier:composite; 155, comment; 156, comment; 157, comment; 158, block; 158, 159; 158, 193; 158, 200; 158, 207; 158, 215; 159, try_statement; 159, 160; 159, 169; 160, block; 160, 161; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:component; 164, call; 164, 165; 164, 166; 165, identifier:_component_closest_to_origin; 166, argument_list; 166, 167; 166, 168; 167, identifier:mark_components; 168, identifier:glyphSet; 169, except_clause; 169, 170; 169, 174; 170, as_pattern; 170, 171; 170, 172; 171, identifier:Exception; 172, as_pattern_target; 172, 173; 173, identifier:e; 174, block; 174, 175; 175, raise_statement; 175, 176; 176, call; 176, 177; 176, 178; 177, identifier:Exception; 178, argument_list; 178, 179; 179, call; 179, 180; 179, 185; 180, attribute; 180, 181; 180, 184; 181, concatenated_string; 181, 182; 181, 183; 182, string:"Error while determining which component of composite "; 183, string:"'{}' is the lowest: {}"; 184, identifier:format; 185, argument_list; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:composite; 188, identifier:name; 189, call; 189, 190; 189, 191; 190, identifier:str; 191, argument_list; 191, 192; 192, identifier:e; 193, expression_statement; 193, 194; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:mark_components; 197, identifier:remove; 198, argument_list; 198, 199; 199, identifier:component; 200, expression_statement; 200, 201; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:base_components; 204, identifier:append; 205, argument_list; 205, 206; 206, identifier:component; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:glyph; 210, subscript; 210, 211; 210, 212; 211, identifier:glyphSet; 212, attribute; 212, 213; 212, 214; 213, identifier:component; 214, identifier:baseGlyph; 215, expression_statement; 215, 216; 216, augmented_assignment:|=; 216, 217; 216, 218; 217, identifier:anchor_names; 218, set_comprehension; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:a; 221, identifier:name; 222, for_in_clause; 222, 223; 222, 224; 223, identifier:a; 224, attribute; 224, 225; 224, 226; 225, identifier:glyph; 226, identifier:anchors; 227, for_statement; 227, 228; 227, 229; 227, 230; 227, 231; 227, 232; 228, identifier:anchor_name; 229, identifier:anchor_names; 230, comment; 231, comment; 232, block; 232, 233; 233, if_statement; 233, 234; 233, 251; 234, not_operator; 234, 235; 235, call; 235, 236; 235, 237; 236, identifier:any; 237, generator_expression; 237, 238; 237, 246; 238, call; 238, 239; 238, 244; 239, attribute; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:a; 242, identifier:name; 243, identifier:startswith; 244, argument_list; 244, 245; 245, identifier:anchor_name; 246, for_in_clause; 246, 247; 246, 248; 247, identifier:a; 248, attribute; 248, 249; 248, 250; 249, identifier:composite; 250, identifier:anchors; 251, block; 251, 252; 252, expression_statement; 252, 253; 253, call; 253, 254; 253, 255; 254, identifier:_get_anchor_data; 255, argument_list; 255, 256; 255, 257; 255, 258; 255, 259; 256, identifier:to_add; 257, identifier:glyphSet; 258, identifier:base_components; 259, identifier:anchor_name; 260, for_statement; 260, 261; 260, 262; 260, 263; 261, identifier:component; 262, identifier:mark_components; 263, block; 263, 264; 264, expression_statement; 264, 265; 265, call; 265, 266; 265, 267; 266, identifier:_adjust_anchors; 267, argument_list; 267, 268; 267, 269; 267, 270; 268, identifier:to_add; 269, identifier:glyphSet; 270, identifier:component; 271, comment; 272, for_statement; 272, 273; 272, 278; 272, 286; 273, pattern_list; 273, 274; 273, 275; 274, identifier:name; 275, tuple_pattern; 275, 276; 275, 277; 276, identifier:x; 277, identifier:y; 278, call; 278, 279; 278, 280; 279, identifier:sorted; 280, argument_list; 280, 281; 281, call; 281, 282; 281, 285; 282, attribute; 282, 283; 282, 284; 283, identifier:to_add; 284, identifier:items; 285, argument_list; 286, block; 286, 287; 286, 300; 287, expression_statement; 287, 288; 288, assignment; 288, 289; 288, 290; 289, identifier:anchor_dict; 290, dictionary; 290, 291; 290, 294; 290, 297; 291, pair; 291, 292; 291, 293; 292, string:'name'; 293, identifier:name; 294, pair; 294, 295; 294, 296; 295, string:'x'; 296, identifier:x; 297, pair; 297, 298; 297, 299; 298, string:'y'; 299, identifier:y; 300, try_statement; 300, 301; 300, 309; 301, block; 301, 302; 302, expression_statement; 302, 303; 303, call; 303, 304; 303, 307; 304, attribute; 304, 305; 304, 306; 305, identifier:composite; 306, identifier:appendAnchor; 307, argument_list; 307, 308; 308, identifier:anchor_dict; 309, except_clause; 309, 310; 309, 311; 309, 312; 309, 313; 310, identifier:TypeError; 311, comment; 312, comment; 313, block; 313, 314; 314, expression_statement; 314, 315; 315, call; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:composite; 318, identifier:appendAnchor; 319, argument_list; 319, 320; 319, 321; 320, identifier:name; 321, tuple; 321, 322; 321, 323; 322, identifier:x; 323, identifier:y
def _propagate_glyph_anchors(glyphSet, composite, processed): """ Propagate anchors from base glyphs to a given composite glyph, and to all composite glyphs used in between. """ if composite.name in processed: return processed.add(composite.name) if not composite.components: return base_components = [] mark_components = [] anchor_names = set() to_add = {} for component in composite.components: try: glyph = glyphSet[component.baseGlyph] except KeyError: logger.warning( 'Anchors not propagated for inexistent component {} ' 'in glyph {}'.format(component.baseGlyph, composite.name)) else: _propagate_glyph_anchors(glyphSet, glyph, processed) if any(a.name.startswith('_') for a in glyph.anchors): mark_components.append(component) else: base_components.append(component) anchor_names |= {a.name for a in glyph.anchors} if mark_components and not base_components and _is_ligature_mark(composite): # The composite is a mark that is composed of other marks (E.g. # "circumflexcomb_tildecomb"). Promote the mark that is positioned closest # to the origin to a base. try: component = _component_closest_to_origin(mark_components, glyphSet) except Exception as e: raise Exception( "Error while determining which component of composite " "'{}' is the lowest: {}".format(composite.name, str(e)) ) mark_components.remove(component) base_components.append(component) glyph = glyphSet[component.baseGlyph] anchor_names |= {a.name for a in glyph.anchors} for anchor_name in anchor_names: # don't add if composite glyph already contains this anchor OR any # associated ligature anchors (e.g. "top_1, top_2" for "top") if not any(a.name.startswith(anchor_name) for a in composite.anchors): _get_anchor_data(to_add, glyphSet, base_components, anchor_name) for component in mark_components: _adjust_anchors(to_add, glyphSet, component) # we sort propagated anchors to append in a deterministic order for name, (x, y) in sorted(to_add.items()): anchor_dict = {'name': name, 'x': x, 'y': y} try: composite.appendAnchor(anchor_dict) except TypeError: # pragma: no cover # fontParts API composite.appendAnchor(name, (x, y))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:setupTable_name; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 16; 5, 22; 5, 35; 5, 41; 5, 42; 5, 104; 5, 105; 5, 115; 5, 129; 5, 139; 5, 149; 5, 157; 5, 284; 5, 285; 5, 298; 5, 311; 5, 312; 5, 328; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 14; 9, comparison_operator:not; 9, 10; 9, 11; 10, string:"name"; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:tables; 14, block; 14, 15; 15, return_statement; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:font; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:ufo; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 29; 24, subscript; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:otf; 28, string:"name"; 29, assignment; 29, 30; 29, 31; 30, identifier:name; 31, call; 31, 32; 31, 33; 32, identifier:newTable; 33, argument_list; 33, 34; 34, string:"name"; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:name; 39, identifier:names; 40, list:[]; 41, comment; 42, for_statement; 42, 43; 42, 44; 42, 51; 43, identifier:nameRecord; 44, call; 44, 45; 44, 46; 45, identifier:getAttrWithFallback; 46, argument_list; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:font; 49, identifier:info; 50, string:"openTypeNameRecords"; 51, block; 51, 52; 51, 58; 51, 64; 51, 70; 51, 76; 51, 77; 51, 78; 51, 79; 51, 80; 51, 81; 51, 93; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:nameId; 55, subscript; 55, 56; 55, 57; 56, identifier:nameRecord; 57, string:"nameID"; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:platformId; 61, subscript; 61, 62; 61, 63; 62, identifier:nameRecord; 63, string:"platformID"; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:platEncId; 67, subscript; 67, 68; 67, 69; 68, identifier:nameRecord; 69, string:"encodingID"; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:langId; 73, subscript; 73, 74; 73, 75; 74, identifier:nameRecord; 75, string:"languageID"; 76, comment; 77, comment; 78, comment; 79, comment; 80, comment; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:nameVal; 84, call; 84, 85; 84, 86; 85, identifier:tounicode; 86, argument_list; 86, 87; 86, 90; 87, subscript; 87, 88; 87, 89; 88, identifier:nameRecord; 89, string:"string"; 90, keyword_argument; 90, 91; 90, 92; 91, identifier:encoding; 92, string:'ascii'; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:name; 97, identifier:setName; 98, argument_list; 98, 99; 98, 100; 98, 101; 98, 102; 98, 103; 99, identifier:nameVal; 100, identifier:nameId; 101, identifier:platformId; 102, identifier:platEncId; 103, identifier:langId; 104, comment; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:familyName; 108, call; 108, 109; 108, 110; 109, identifier:getAttrWithFallback; 110, argument_list; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:font; 113, identifier:info; 114, string:"styleMapFamilyName"; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:styleName; 118, call; 118, 119; 118, 128; 119, attribute; 119, 120; 119, 127; 120, call; 120, 121; 120, 122; 121, identifier:getAttrWithFallback; 122, argument_list; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:font; 125, identifier:info; 126, string:"styleMapStyleName"; 127, identifier:title; 128, argument_list; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:preferredFamilyName; 132, call; 132, 133; 132, 134; 133, identifier:getAttrWithFallback; 134, argument_list; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:font; 137, identifier:info; 138, string:"openTypeNamePreferredFamilyName"; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:preferredSubfamilyName; 142, call; 142, 143; 142, 144; 143, identifier:getAttrWithFallback; 144, argument_list; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:font; 147, identifier:info; 148, string:"openTypeNamePreferredSubfamilyName"; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:fullName; 152, binary_operator:%; 152, 153; 152, 154; 153, string:"%s %s"; 154, tuple; 154, 155; 154, 156; 155, identifier:preferredFamilyName; 156, identifier:preferredSubfamilyName; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:nameVals; 160, dictionary; 160, 161; 160, 170; 160, 173; 160, 176; 160, 185; 160, 188; 160, 197; 160, 206; 160, 215; 160, 224; 160, 233; 160, 242; 160, 251; 160, 260; 160, 269; 160, 278; 160, 281; 161, pair; 161, 162; 161, 163; 162, integer:0; 163, call; 163, 164; 163, 165; 164, identifier:getAttrWithFallback; 165, argument_list; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:font; 168, identifier:info; 169, string:"copyright"; 170, pair; 170, 171; 170, 172; 171, integer:1; 172, identifier:familyName; 173, pair; 173, 174; 173, 175; 174, integer:2; 175, identifier:styleName; 176, pair; 176, 177; 176, 178; 177, integer:3; 178, call; 178, 179; 178, 180; 179, identifier:getAttrWithFallback; 180, argument_list; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:font; 183, identifier:info; 184, string:"openTypeNameUniqueID"; 185, pair; 185, 186; 185, 187; 186, integer:4; 187, identifier:fullName; 188, pair; 188, 189; 188, 190; 189, integer:5; 190, call; 190, 191; 190, 192; 191, identifier:getAttrWithFallback; 192, argument_list; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:font; 195, identifier:info; 196, string:"openTypeNameVersion"; 197, pair; 197, 198; 197, 199; 198, integer:6; 199, call; 199, 200; 199, 201; 200, identifier:getAttrWithFallback; 201, argument_list; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:font; 204, identifier:info; 205, string:"postscriptFontName"; 206, pair; 206, 207; 206, 208; 207, integer:7; 208, call; 208, 209; 208, 210; 209, identifier:getAttrWithFallback; 210, argument_list; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:font; 213, identifier:info; 214, string:"trademark"; 215, pair; 215, 216; 215, 217; 216, integer:8; 217, call; 217, 218; 217, 219; 218, identifier:getAttrWithFallback; 219, argument_list; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:font; 222, identifier:info; 223, string:"openTypeNameManufacturer"; 224, pair; 224, 225; 224, 226; 225, integer:9; 226, call; 226, 227; 226, 228; 227, identifier:getAttrWithFallback; 228, argument_list; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:font; 231, identifier:info; 232, string:"openTypeNameDesigner"; 233, pair; 233, 234; 233, 235; 234, integer:10; 235, call; 235, 236; 235, 237; 236, identifier:getAttrWithFallback; 237, argument_list; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:font; 240, identifier:info; 241, string:"openTypeNameDescription"; 242, pair; 242, 243; 242, 244; 243, integer:11; 244, call; 244, 245; 244, 246; 245, identifier:getAttrWithFallback; 246, argument_list; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:font; 249, identifier:info; 250, string:"openTypeNameManufacturerURL"; 251, pair; 251, 252; 251, 253; 252, integer:12; 253, call; 253, 254; 253, 255; 254, identifier:getAttrWithFallback; 255, argument_list; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:font; 258, identifier:info; 259, string:"openTypeNameDesignerURL"; 260, pair; 260, 261; 260, 262; 261, integer:13; 262, call; 262, 263; 262, 264; 263, identifier:getAttrWithFallback; 264, argument_list; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:font; 267, identifier:info; 268, string:"openTypeNameLicense"; 269, pair; 269, 270; 269, 271; 270, integer:14; 271, call; 271, 272; 271, 273; 272, identifier:getAttrWithFallback; 273, argument_list; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:font; 276, identifier:info; 277, string:"openTypeNameLicenseURL"; 278, pair; 278, 279; 278, 280; 279, integer:16; 280, identifier:preferredFamilyName; 281, pair; 281, 282; 281, 283; 282, integer:17; 283, identifier:preferredSubfamilyName; 284, comment; 285, if_statement; 285, 286; 285, 293; 286, comparison_operator:==; 286, 287; 286, 290; 287, subscript; 287, 288; 287, 289; 288, identifier:nameVals; 289, integer:1; 290, subscript; 290, 291; 290, 292; 291, identifier:nameVals; 292, integer:16; 293, block; 293, 294; 294, delete_statement; 294, 295; 295, subscript; 295, 296; 295, 297; 296, identifier:nameVals; 297, integer:16; 298, if_statement; 298, 299; 298, 306; 299, comparison_operator:==; 299, 300; 299, 303; 300, subscript; 300, 301; 300, 302; 301, identifier:nameVals; 302, integer:2; 303, subscript; 303, 304; 303, 305; 304, identifier:nameVals; 305, integer:17; 306, block; 306, 307; 307, delete_statement; 307, 308; 308, subscript; 308, 309; 308, 310; 309, identifier:nameVals; 310, integer:17; 311, comment; 312, if_statement; 312, 313; 312, 316; 313, subscript; 313, 314; 313, 315; 314, identifier:nameVals; 315, integer:6; 316, block; 316, 317; 317, expression_statement; 317, 318; 318, assignment; 318, 319; 318, 322; 319, subscript; 319, 320; 319, 321; 320, identifier:nameVals; 321, integer:6; 322, call; 322, 323; 322, 324; 323, identifier:normalizeStringForPostscript; 324, argument_list; 324, 325; 325, subscript; 325, 326; 325, 327; 326, identifier:nameVals; 327, integer:6; 328, for_statement; 328, 329; 328, 330; 328, 338; 329, identifier:nameId; 330, call; 330, 331; 330, 332; 331, identifier:sorted; 332, argument_list; 332, 333; 333, call; 333, 334; 333, 337; 334, attribute; 334, 335; 334, 336; 335, identifier:nameVals; 336, identifier:keys; 337, argument_list; 338, block; 338, 339; 338, 345; 338, 350; 338, 360; 338, 364; 338, 374; 338, 378; 338, 379; 338, 391; 339, expression_statement; 339, 340; 340, assignment; 340, 341; 340, 342; 341, identifier:nameVal; 342, subscript; 342, 343; 342, 344; 343, identifier:nameVals; 344, identifier:nameId; 345, if_statement; 345, 346; 345, 348; 346, not_operator; 346, 347; 347, identifier:nameVal; 348, block; 348, 349; 349, continue_statement; 350, expression_statement; 350, 351; 351, assignment; 351, 352; 351, 353; 352, identifier:nameVal; 353, call; 353, 354; 353, 355; 354, identifier:tounicode; 355, argument_list; 355, 356; 355, 357; 356, identifier:nameVal; 357, keyword_argument; 357, 358; 357, 359; 358, identifier:encoding; 359, string:'ascii'; 360, expression_statement; 360, 361; 361, assignment; 361, 362; 361, 363; 362, identifier:platformId; 363, integer:3; 364, expression_statement; 364, 365; 365, assignment; 365, 366; 365, 367; 366, identifier:platEncId; 367, conditional_expression:if; 367, 368; 367, 369; 367, 373; 368, integer:10; 369, call; 369, 370; 369, 371; 370, identifier:_isNonBMP; 371, argument_list; 371, 372; 372, identifier:nameVal; 373, integer:1; 374, expression_statement; 374, 375; 375, assignment; 375, 376; 375, 377; 376, identifier:langId; 377, integer:0x409; 378, comment; 379, if_statement; 379, 380; 379, 389; 380, call; 380, 381; 380, 384; 381, attribute; 381, 382; 381, 383; 382, identifier:name; 383, identifier:getName; 384, argument_list; 384, 385; 384, 386; 384, 387; 384, 388; 385, identifier:nameId; 386, identifier:platformId; 387, identifier:platEncId; 388, identifier:langId; 389, block; 389, 390; 390, continue_statement; 391, expression_statement; 391, 392; 392, call; 392, 393; 392, 396; 393, attribute; 393, 394; 393, 395; 394, identifier:name; 395, identifier:setName; 396, argument_list; 396, 397; 396, 398; 396, 399; 396, 400; 396, 401; 397, identifier:nameVal; 398, identifier:nameId; 399, identifier:platformId; 400, identifier:platEncId; 401, identifier:langId
def setupTable_name(self): """ Make the name table. **This should not be called externally.** Subclasses may override or supplement this method to handle the table creation in a different way if desired. """ if "name" not in self.tables: return font = self.ufo self.otf["name"] = name = newTable("name") name.names = [] # Set name records from font.info.openTypeNameRecords for nameRecord in getAttrWithFallback( font.info, "openTypeNameRecords"): nameId = nameRecord["nameID"] platformId = nameRecord["platformID"] platEncId = nameRecord["encodingID"] langId = nameRecord["languageID"] # on Python 2, plistLib (used by ufoLib) returns unicode strings # only when plist data contain non-ascii characters, and returns # ascii-encoded bytes when it can. On the other hand, fontTools's # name table `setName` method wants unicode strings, so we must # decode them first nameVal = tounicode(nameRecord["string"], encoding='ascii') name.setName(nameVal, nameId, platformId, platEncId, langId) # Build name records familyName = getAttrWithFallback(font.info, "styleMapFamilyName") styleName = getAttrWithFallback(font.info, "styleMapStyleName").title() preferredFamilyName = getAttrWithFallback( font.info, "openTypeNamePreferredFamilyName") preferredSubfamilyName = getAttrWithFallback( font.info, "openTypeNamePreferredSubfamilyName") fullName = "%s %s" % (preferredFamilyName, preferredSubfamilyName) nameVals = { 0: getAttrWithFallback(font.info, "copyright"), 1: familyName, 2: styleName, 3: getAttrWithFallback(font.info, "openTypeNameUniqueID"), 4: fullName, 5: getAttrWithFallback(font.info, "openTypeNameVersion"), 6: getAttrWithFallback(font.info, "postscriptFontName"), 7: getAttrWithFallback(font.info, "trademark"), 8: getAttrWithFallback(font.info, "openTypeNameManufacturer"), 9: getAttrWithFallback(font.info, "openTypeNameDesigner"), 10: getAttrWithFallback(font.info, "openTypeNameDescription"), 11: getAttrWithFallback(font.info, "openTypeNameManufacturerURL"), 12: getAttrWithFallback(font.info, "openTypeNameDesignerURL"), 13: getAttrWithFallback(font.info, "openTypeNameLicense"), 14: getAttrWithFallback(font.info, "openTypeNameLicenseURL"), 16: preferredFamilyName, 17: preferredSubfamilyName, } # don't add typographic names if they are the same as the legacy ones if nameVals[1] == nameVals[16]: del nameVals[16] if nameVals[2] == nameVals[17]: del nameVals[17] # postscript font name if nameVals[6]: nameVals[6] = normalizeStringForPostscript(nameVals[6]) for nameId in sorted(nameVals.keys()): nameVal = nameVals[nameId] if not nameVal: continue nameVal = tounicode(nameVal, encoding='ascii') platformId = 3 platEncId = 10 if _isNonBMP(nameVal) else 1 langId = 0x409 # Set built name record if not set yet if name.getName(nameId, platformId, platEncId, langId): continue name.setName(nameVal, nameId, platformId, platEncId, langId)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:setupTable_cmap; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 16; 5, 24; 5, 48; 5, 86; 5, 87; 5, 94; 5, 100; 5, 106; 5, 112; 5, 118; 5, 119; 5, 126; 5, 132; 5, 138; 5, 144; 5, 150; 5, 151; 5, 164; 5, 170; 5, 178; 5, 179; 5, 180; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 14; 9, comparison_operator:not; 9, 10; 9, 11; 10, string:"cmap"; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:tables; 14, block; 14, 15; 15, return_statement; 16, import_from_statement; 16, 17; 16, 22; 17, dotted_name; 17, 18; 17, 19; 17, 20; 17, 21; 18, identifier:fontTools; 19, identifier:ttLib; 20, identifier:tables; 21, identifier:_c_m_a_p; 22, dotted_name; 22, 23; 23, identifier:cmap_format_4; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:nonBMP; 27, call; 27, 28; 27, 29; 28, identifier:dict; 29, generator_expression; 29, 30; 29, 33; 29, 44; 30, tuple; 30, 31; 30, 32; 31, identifier:k; 32, identifier:v; 33, for_in_clause; 33, 34; 33, 37; 34, pattern_list; 34, 35; 34, 36; 35, identifier:k; 36, identifier:v; 37, call; 37, 38; 37, 43; 38, attribute; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:unicodeToGlyphNameMapping; 42, identifier:items; 43, argument_list; 44, if_clause; 44, 45; 45, comparison_operator:>; 45, 46; 45, 47; 46, identifier:k; 47, integer:65535; 48, if_statement; 48, 49; 48, 50; 48, 75; 49, identifier:nonBMP; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:mapping; 54, call; 54, 55; 54, 56; 55, identifier:dict; 56, generator_expression; 56, 57; 56, 60; 56, 71; 57, tuple; 57, 58; 57, 59; 58, identifier:k; 59, identifier:v; 60, for_in_clause; 60, 61; 60, 64; 61, pattern_list; 61, 62; 61, 63; 62, identifier:k; 63, identifier:v; 64, call; 64, 65; 64, 70; 65, attribute; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:unicodeToGlyphNameMapping; 69, identifier:items; 70, argument_list; 71, if_clause; 71, 72; 72, comparison_operator:<=; 72, 73; 72, 74; 73, identifier:k; 74, integer:65535; 75, else_clause; 75, 76; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:mapping; 80, call; 80, 81; 80, 82; 81, identifier:dict; 82, argument_list; 82, 83; 83, attribute; 83, 84; 83, 85; 84, identifier:self; 85, identifier:unicodeToGlyphNameMapping; 86, comment; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:cmap4_0_3; 90, call; 90, 91; 90, 92; 91, identifier:cmap_format_4; 92, argument_list; 92, 93; 93, integer:4; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:cmap4_0_3; 98, identifier:platformID; 99, integer:0; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:cmap4_0_3; 104, identifier:platEncID; 105, integer:3; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:cmap4_0_3; 110, identifier:language; 111, integer:0; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:cmap4_0_3; 116, identifier:cmap; 117, identifier:mapping; 118, comment; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:cmap4_3_1; 122, call; 122, 123; 122, 124; 123, identifier:cmap_format_4; 124, argument_list; 124, 125; 125, integer:4; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:cmap4_3_1; 130, identifier:platformID; 131, integer:3; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:cmap4_3_1; 136, identifier:platEncID; 137, integer:1; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:cmap4_3_1; 142, identifier:language; 143, integer:0; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:cmap4_3_1; 148, identifier:cmap; 149, identifier:mapping; 150, comment; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 158; 153, subscript; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:otf; 157, string:"cmap"; 158, assignment; 158, 159; 158, 160; 159, identifier:cmap; 160, call; 160, 161; 160, 162; 161, identifier:newTable; 162, argument_list; 162, 163; 163, string:"cmap"; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:cmap; 168, identifier:tableVersion; 169, integer:0; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:cmap; 174, identifier:tables; 175, list:[cmap4_0_3, cmap4_3_1]; 175, 176; 175, 177; 176, identifier:cmap4_0_3; 177, identifier:cmap4_3_1; 178, comment; 179, comment; 180, if_statement; 180, 181; 180, 182; 181, identifier:nonBMP; 182, block; 182, 183; 182, 191; 182, 198; 182, 199; 182, 206; 182, 212; 182, 218; 182, 224; 182, 230; 182, 231; 182, 238; 182, 244; 182, 250; 182, 256; 182, 262; 182, 263; 183, import_from_statement; 183, 184; 183, 189; 184, dotted_name; 184, 185; 184, 186; 184, 187; 184, 188; 185, identifier:fontTools; 186, identifier:ttLib; 187, identifier:tables; 188, identifier:_c_m_a_p; 189, dotted_name; 189, 190; 190, identifier:cmap_format_12; 191, expression_statement; 191, 192; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:nonBMP; 195, identifier:update; 196, argument_list; 196, 197; 197, identifier:mapping; 198, comment; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:cmap12_0_4; 202, call; 202, 203; 202, 204; 203, identifier:cmap_format_12; 204, argument_list; 204, 205; 205, integer:12; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:cmap12_0_4; 210, identifier:platformID; 211, integer:0; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:cmap12_0_4; 216, identifier:platEncID; 217, integer:4; 218, expression_statement; 218, 219; 219, assignment; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:cmap12_0_4; 222, identifier:language; 223, integer:0; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:cmap12_0_4; 228, identifier:cmap; 229, identifier:nonBMP; 230, comment; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:cmap12_3_10; 234, call; 234, 235; 234, 236; 235, identifier:cmap_format_12; 236, argument_list; 236, 237; 237, integer:12; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:cmap12_3_10; 242, identifier:platformID; 243, integer:3; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:cmap12_3_10; 248, identifier:platEncID; 249, integer:10; 250, expression_statement; 250, 251; 251, assignment; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:cmap12_3_10; 254, identifier:language; 255, integer:0; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:cmap12_3_10; 260, identifier:cmap; 261, identifier:nonBMP; 262, comment; 263, expression_statement; 263, 264; 264, assignment; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:cmap; 267, identifier:tables; 268, list:[cmap4_0_3, cmap4_3_1, cmap12_0_4, cmap12_3_10]; 268, 269; 268, 270; 268, 271; 268, 272; 269, identifier:cmap4_0_3; 270, identifier:cmap4_3_1; 271, identifier:cmap12_0_4; 272, identifier:cmap12_3_10
def setupTable_cmap(self): """ Make the cmap table. **This should not be called externally.** Subclasses may override or supplement this method to handle the table creation in a different way if desired. """ if "cmap" not in self.tables: return from fontTools.ttLib.tables._c_m_a_p import cmap_format_4 nonBMP = dict((k,v) for k,v in self.unicodeToGlyphNameMapping.items() if k > 65535) if nonBMP: mapping = dict((k,v) for k,v in self.unicodeToGlyphNameMapping.items() if k <= 65535) else: mapping = dict(self.unicodeToGlyphNameMapping) # mac cmap4_0_3 = cmap_format_4(4) cmap4_0_3.platformID = 0 cmap4_0_3.platEncID = 3 cmap4_0_3.language = 0 cmap4_0_3.cmap = mapping # windows cmap4_3_1 = cmap_format_4(4) cmap4_3_1.platformID = 3 cmap4_3_1.platEncID = 1 cmap4_3_1.language = 0 cmap4_3_1.cmap = mapping # store self.otf["cmap"] = cmap = newTable("cmap") cmap.tableVersion = 0 cmap.tables = [cmap4_0_3, cmap4_3_1] # If we have glyphs outside Unicode BMP, we must set another # subtable that can hold longer codepoints for them. if nonBMP: from fontTools.ttLib.tables._c_m_a_p import cmap_format_12 nonBMP.update(mapping) # mac cmap12_0_4 = cmap_format_12(12) cmap12_0_4.platformID = 0 cmap12_0_4.platEncID = 4 cmap12_0_4.language = 0 cmap12_0_4.cmap = nonBMP # windows cmap12_3_10 = cmap_format_12(12) cmap12_3_10.platformID = 3 cmap12_3_10.platEncID = 10 cmap12_3_10.language = 0 cmap12_3_10.cmap = nonBMP # update tables registry cmap.tables = [cmap4_0_3, cmap4_3_1, cmap12_0_4, cmap12_3_10]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_setupTable_hhea_or_vhea; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:tag; 6, block; 6, 7; 6, 9; 6, 17; 6, 32; 6, 45; 6, 60; 6, 66; 6, 83; 6, 84; 6, 85; 6, 86; 6, 87; 6, 88; 6, 89; 6, 90; 6, 100; 6, 119; 6, 153; 6, 179; 6, 180; 6, 184; 6, 185; 6, 189; 6, 190; 6, 194; 6, 195; 6, 199; 6, 311; 6, 330; 6, 349; 6, 368; 6, 387; 6, 408; 6, 421; 6, 427; 6, 428; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 15; 10, comparison_operator:not; 10, 11; 10, 12; 11, identifier:tag; 12, attribute; 12, 13; 12, 14; 13, identifier:self; 14, identifier:tables; 15, block; 15, 16; 16, return_statement; 17, if_statement; 17, 18; 17, 21; 17, 26; 18, comparison_operator:==; 18, 19; 18, 20; 19, identifier:tag; 20, string:"hhea"; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:isHhea; 25, True; 26, else_clause; 26, 27; 27, block; 27, 28; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:isHhea; 31, False; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 39; 34, subscript; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:otf; 38, identifier:tag; 39, assignment; 39, 40; 39, 41; 40, identifier:table; 41, call; 41, 42; 41, 43; 42, identifier:newTable; 43, argument_list; 43, 44; 44, identifier:tag; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:mtxTable; 48, call; 48, 49; 48, 54; 49, attribute; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:otf; 53, identifier:get; 54, argument_list; 54, 55; 55, binary_operator:+; 55, 56; 55, 59; 56, subscript; 56, 57; 56, 58; 57, identifier:tag; 58, integer:0; 59, string:"mtx"; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:font; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:ufo; 66, if_statement; 66, 67; 66, 68; 66, 75; 67, identifier:isHhea; 68, block; 68, 69; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:table; 73, identifier:tableVersion; 74, integer:0x00010000; 75, else_clause; 75, 76; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:table; 81, identifier:tableVersion; 82, integer:0x00011000; 83, comment; 84, comment; 85, comment; 86, comment; 87, comment; 88, comment; 89, comment; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:commonPrefix; 93, binary_operator:%; 93, 94; 93, 95; 94, string:"openType%s"; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:tag; 98, identifier:title; 99, argument_list; 100, if_statement; 100, 101; 100, 102; 100, 107; 101, identifier:isHhea; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:metricsPrefix; 106, identifier:commonPrefix; 107, else_clause; 107, 108; 108, block; 108, 109; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:metricsPrefix; 112, binary_operator:%; 112, 113; 112, 114; 113, string:"openType%sVertTypo"; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:tag; 117, identifier:title; 118, argument_list; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:metricsDict; 122, dictionary; 122, 123; 122, 128; 122, 133; 122, 138; 122, 143; 122, 148; 123, pair; 123, 124; 123, 125; 124, string:"ascent"; 125, binary_operator:%; 125, 126; 125, 127; 126, string:"%sAscender"; 127, identifier:metricsPrefix; 128, pair; 128, 129; 128, 130; 129, string:"descent"; 130, binary_operator:%; 130, 131; 130, 132; 131, string:"%sDescender"; 132, identifier:metricsPrefix; 133, pair; 133, 134; 133, 135; 134, string:"lineGap"; 135, binary_operator:%; 135, 136; 135, 137; 136, string:"%sLineGap"; 137, identifier:metricsPrefix; 138, pair; 138, 139; 138, 140; 139, string:"caretSlopeRise"; 140, binary_operator:%; 140, 141; 140, 142; 141, string:"%sCaretSlopeRise"; 142, identifier:commonPrefix; 143, pair; 143, 144; 143, 145; 144, string:"caretSlopeRun"; 145, binary_operator:%; 145, 146; 145, 147; 146, string:"%sCaretSlopeRun"; 147, identifier:commonPrefix; 148, pair; 148, 149; 148, 150; 149, string:"caretOffset"; 150, binary_operator:%; 150, 151; 150, 152; 151, string:"%sCaretOffset"; 152, identifier:commonPrefix; 153, for_statement; 153, 154; 153, 157; 153, 162; 154, pattern_list; 154, 155; 154, 156; 155, identifier:otfName; 156, identifier:ufoName; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:metricsDict; 160, identifier:items; 161, argument_list; 162, block; 162, 163; 163, expression_statement; 163, 164; 164, call; 164, 165; 164, 166; 165, identifier:setattr; 166, argument_list; 166, 167; 166, 168; 166, 169; 167, identifier:table; 168, identifier:otfName; 169, call; 169, 170; 169, 171; 170, identifier:otRound; 171, argument_list; 171, 172; 172, call; 172, 173; 172, 174; 173, identifier:getAttrWithFallback; 174, argument_list; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:font; 177, identifier:info; 178, identifier:ufoName; 179, comment; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:advances; 183, list:[]; 184, comment; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:firstSideBearings; 188, list:[]; 189, comment; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:secondSideBearings; 193, list:[]; 194, comment; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:extents; 198, list:[]; 199, if_statement; 199, 200; 199, 203; 200, comparison_operator:is; 200, 201; 200, 202; 201, identifier:mtxTable; 202, None; 203, block; 203, 204; 204, for_statement; 204, 205; 204, 206; 204, 209; 205, identifier:glyphName; 206, attribute; 206, 207; 206, 208; 207, identifier:self; 208, identifier:allGlyphs; 209, block; 209, 210; 209, 218; 209, 225; 209, 233; 209, 239; 209, 282; 209, 290; 209, 297; 209, 304; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 215; 212, pattern_list; 212, 213; 212, 214; 213, identifier:advance; 214, identifier:firstSideBearing; 215, subscript; 215, 216; 215, 217; 216, identifier:mtxTable; 217, identifier:glyphName; 218, expression_statement; 218, 219; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:advances; 222, identifier:append; 223, argument_list; 223, 224; 224, identifier:advance; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:bounds; 228, subscript; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:self; 231, identifier:glyphBoundingBoxes; 232, identifier:glyphName; 233, if_statement; 233, 234; 233, 237; 234, comparison_operator:is; 234, 235; 234, 236; 235, identifier:bounds; 236, None; 237, block; 237, 238; 238, continue_statement; 239, if_statement; 239, 240; 239, 241; 239, 261; 240, identifier:isHhea; 241, block; 241, 242; 241, 253; 241, 254; 241, 255; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 245; 244, identifier:boundsAdvance; 245, parenthesized_expression; 245, 246; 246, binary_operator:-; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:bounds; 249, identifier:xMax; 250, attribute; 250, 251; 250, 252; 251, identifier:bounds; 252, identifier:xMin; 253, comment; 254, comment; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 258; 257, identifier:extent; 258, binary_operator:+; 258, 259; 258, 260; 259, identifier:firstSideBearing; 260, identifier:boundsAdvance; 261, else_clause; 261, 262; 262, block; 262, 263; 262, 274; 262, 275; 262, 276; 263, expression_statement; 263, 264; 264, assignment; 264, 265; 264, 266; 265, identifier:boundsAdvance; 266, parenthesized_expression; 266, 267; 267, binary_operator:-; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:bounds; 270, identifier:yMax; 271, attribute; 271, 272; 271, 273; 272, identifier:bounds; 273, identifier:yMin; 274, comment; 275, comment; 276, expression_statement; 276, 277; 277, assignment; 277, 278; 277, 279; 278, identifier:extent; 279, binary_operator:+; 279, 280; 279, 281; 280, identifier:firstSideBearing; 281, identifier:boundsAdvance; 282, expression_statement; 282, 283; 283, assignment; 283, 284; 283, 285; 284, identifier:secondSideBearing; 285, binary_operator:-; 285, 286; 285, 289; 286, binary_operator:-; 286, 287; 286, 288; 287, identifier:advance; 288, identifier:firstSideBearing; 289, identifier:boundsAdvance; 290, expression_statement; 290, 291; 291, call; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, identifier:firstSideBearings; 294, identifier:append; 295, argument_list; 295, 296; 296, identifier:firstSideBearing; 297, expression_statement; 297, 298; 298, call; 298, 299; 298, 302; 299, attribute; 299, 300; 299, 301; 300, identifier:secondSideBearings; 301, identifier:append; 302, argument_list; 302, 303; 303, identifier:secondSideBearing; 304, expression_statement; 304, 305; 305, call; 305, 306; 305, 309; 306, attribute; 306, 307; 306, 308; 307, identifier:extents; 308, identifier:append; 309, argument_list; 309, 310; 310, identifier:extent; 311, expression_statement; 311, 312; 312, call; 312, 313; 312, 314; 313, identifier:setattr; 314, argument_list; 314, 315; 314, 316; 314, 323; 315, identifier:table; 316, binary_operator:%; 316, 317; 316, 318; 317, string:"advance%sMax"; 318, parenthesized_expression; 318, 319; 319, conditional_expression:if; 319, 320; 319, 321; 319, 322; 320, string:"Width"; 321, identifier:isHhea; 322, string:"Height"; 323, conditional_expression:if; 323, 324; 323, 328; 323, 329; 324, call; 324, 325; 324, 326; 325, identifier:max; 326, argument_list; 326, 327; 327, identifier:advances; 328, identifier:advances; 329, integer:0; 330, expression_statement; 330, 331; 331, call; 331, 332; 331, 333; 332, identifier:setattr; 333, argument_list; 333, 334; 333, 335; 333, 342; 334, identifier:table; 335, binary_operator:%; 335, 336; 335, 337; 336, string:"min%sSideBearing"; 337, parenthesized_expression; 337, 338; 338, conditional_expression:if; 338, 339; 338, 340; 338, 341; 339, string:"Left"; 340, identifier:isHhea; 341, string:"Top"; 342, conditional_expression:if; 342, 343; 342, 347; 342, 348; 343, call; 343, 344; 343, 345; 344, identifier:min; 345, argument_list; 345, 346; 346, identifier:firstSideBearings; 347, identifier:firstSideBearings; 348, integer:0; 349, expression_statement; 349, 350; 350, call; 350, 351; 350, 352; 351, identifier:setattr; 352, argument_list; 352, 353; 352, 354; 352, 361; 353, identifier:table; 354, binary_operator:%; 354, 355; 354, 356; 355, string:"min%sSideBearing"; 356, parenthesized_expression; 356, 357; 357, conditional_expression:if; 357, 358; 357, 359; 357, 360; 358, string:"Right"; 359, identifier:isHhea; 360, string:"Bottom"; 361, conditional_expression:if; 361, 362; 361, 366; 361, 367; 362, call; 362, 363; 362, 364; 363, identifier:min; 364, argument_list; 364, 365; 365, identifier:secondSideBearings; 366, identifier:secondSideBearings; 367, integer:0; 368, expression_statement; 368, 369; 369, call; 369, 370; 369, 371; 370, identifier:setattr; 371, argument_list; 371, 372; 371, 373; 371, 380; 372, identifier:table; 373, binary_operator:%; 373, 374; 373, 375; 374, string:"%sMaxExtent"; 375, parenthesized_expression; 375, 376; 376, conditional_expression:if; 376, 377; 376, 378; 376, 379; 377, string:"x"; 378, identifier:isHhea; 379, string:"y"; 380, conditional_expression:if; 380, 381; 380, 385; 380, 386; 381, call; 381, 382; 381, 383; 382, identifier:max; 383, argument_list; 383, 384; 384, identifier:extents; 385, identifier:extents; 386, integer:0; 387, if_statement; 387, 388; 387, 389; 387, 397; 388, identifier:isHhea; 389, block; 389, 390; 390, expression_statement; 390, 391; 391, assignment; 391, 392; 391, 393; 392, identifier:reserved; 393, call; 393, 394; 393, 395; 394, identifier:range; 395, argument_list; 395, 396; 396, integer:4; 397, else_clause; 397, 398; 397, 399; 398, comment; 399, block; 399, 400; 400, expression_statement; 400, 401; 401, assignment; 401, 402; 401, 403; 402, identifier:reserved; 403, call; 403, 404; 403, 405; 404, identifier:range; 405, argument_list; 405, 406; 405, 407; 406, integer:1; 407, integer:5; 408, for_statement; 408, 409; 408, 410; 408, 411; 409, identifier:i; 410, identifier:reserved; 411, block; 411, 412; 412, expression_statement; 412, 413; 413, call; 413, 414; 413, 415; 414, identifier:setattr; 415, argument_list; 415, 416; 415, 417; 415, 420; 416, identifier:table; 417, binary_operator:%; 417, 418; 417, 419; 418, string:"reserved%i"; 419, identifier:i; 420, integer:0; 421, expression_statement; 421, 422; 422, assignment; 422, 423; 422, 426; 423, attribute; 423, 424; 423, 425; 424, identifier:table; 425, identifier:metricDataFormat; 426, integer:0; 427, comment; 428, expression_statement; 428, 429; 429, call; 429, 430; 429, 431; 430, identifier:setattr; 431, argument_list; 431, 432; 431, 433; 431, 440; 432, identifier:table; 433, binary_operator:%; 433, 434; 433, 435; 434, string:"numberOf%sMetrics"; 435, parenthesized_expression; 435, 436; 436, conditional_expression:if; 436, 437; 436, 438; 436, 439; 437, string:"H"; 438, identifier:isHhea; 439, string:"V"; 440, call; 440, 441; 440, 442; 441, identifier:len; 442, argument_list; 442, 443; 443, attribute; 443, 444; 443, 445; 444, identifier:self; 445, identifier:allGlyphs
def _setupTable_hhea_or_vhea(self, tag): """ Make the hhea table or the vhea table. This assume the hmtx or the vmtx were respectively made first. """ if tag not in self.tables: return if tag == "hhea": isHhea = True else: isHhea = False self.otf[tag] = table = newTable(tag) mtxTable = self.otf.get(tag[0] + "mtx") font = self.ufo if isHhea: table.tableVersion = 0x00010000 else: table.tableVersion = 0x00011000 # Vertical metrics in hhea, horizontal metrics in vhea # and caret info. # The hhea metrics names are formed as: # "openType" + tag.title() + "Ascender", etc. # While vhea metrics names are formed as: # "openType" + tag.title() + "VertTypo" + "Ascender", etc. # Caret info names only differ by tag.title(). commonPrefix = "openType%s" % tag.title() if isHhea: metricsPrefix = commonPrefix else: metricsPrefix = "openType%sVertTypo" % tag.title() metricsDict = { "ascent": "%sAscender" % metricsPrefix, "descent": "%sDescender" % metricsPrefix, "lineGap": "%sLineGap" % metricsPrefix, "caretSlopeRise": "%sCaretSlopeRise" % commonPrefix, "caretSlopeRun": "%sCaretSlopeRun" % commonPrefix, "caretOffset": "%sCaretOffset" % commonPrefix, } for otfName, ufoName in metricsDict.items(): setattr(table, otfName, otRound(getAttrWithFallback(font.info, ufoName))) # Horizontal metrics in hhea, vertical metrics in vhea advances = [] # width in hhea, height in vhea firstSideBearings = [] # left in hhea, top in vhea secondSideBearings = [] # right in hhea, bottom in vhea extents = [] if mtxTable is not None: for glyphName in self.allGlyphs: advance, firstSideBearing = mtxTable[glyphName] advances.append(advance) bounds = self.glyphBoundingBoxes[glyphName] if bounds is None: continue if isHhea: boundsAdvance = (bounds.xMax - bounds.xMin) # equation from the hhea spec for calculating xMaxExtent: # Max(lsb + (xMax - xMin)) extent = firstSideBearing + boundsAdvance else: boundsAdvance = (bounds.yMax - bounds.yMin) # equation from the vhea spec for calculating yMaxExtent: # Max(tsb + (yMax - yMin)). extent = firstSideBearing + boundsAdvance secondSideBearing = advance - firstSideBearing - boundsAdvance firstSideBearings.append(firstSideBearing) secondSideBearings.append(secondSideBearing) extents.append(extent) setattr(table, "advance%sMax" % ("Width" if isHhea else "Height"), max(advances) if advances else 0) setattr(table, "min%sSideBearing" % ("Left" if isHhea else "Top"), min(firstSideBearings) if firstSideBearings else 0) setattr(table, "min%sSideBearing" % ("Right" if isHhea else "Bottom"), min(secondSideBearings) if secondSideBearings else 0) setattr(table, "%sMaxExtent" % ("x" if isHhea else "y"), max(extents) if extents else 0) if isHhea: reserved = range(4) else: # vhea.reserved0 is caretOffset for legacy reasons reserved = range(1, 5) for i in reserved: setattr(table, "reserved%i" % i, 0) table.metricDataFormat = 0 # glyph count setattr(table, "numberOf%sMetrics" % ("H" if isHhea else "V"), len(self.allGlyphs))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:dir; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:methods; 7, True; 8, default_parameter; 8, 9; 8, 10; 9, identifier:showall; 10, True; 11, block; 11, 12; 11, 14; 11, 17; 11, 18; 11, 54; 11, 59; 11, 66; 11, 68; 11, 69; 12, expression_statement; 12, 13; 13, comment; 14, import_statement; 14, 15; 15, dotted_name; 15, 16; 16, identifier:inspect; 17, comment; 18, for_statement; 18, 19; 18, 22; 18, 32; 19, pattern_list; 19, 20; 19, 21; 20, identifier:k; 21, identifier:v; 22, call; 22, 23; 22, 24; 23, identifier:sorted; 24, argument_list; 24, 25; 25, call; 25, 26; 25, 31; 26, attribute; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:__dict__; 30, identifier:items; 31, argument_list; 32, block; 32, 33; 32, 42; 33, if_statement; 33, 34; 33, 40; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:k; 37, identifier:startswith; 38, argument_list; 38, 39; 39, string:"_"; 40, block; 40, 41; 41, continue_statement; 42, print_statement; 42, 43; 42, 52; 42, 53; 43, call; 43, 44; 43, 45; 44, identifier:makeminlength; 45, argument_list; 45, 46; 45, 49; 46, binary_operator:+; 46, 47; 46, 48; 47, string:"."; 48, identifier:k; 49, attribute; 49, 50; 49, 51; 50, identifier:being; 51, identifier:linelen; 52, string:"\t"; 53, identifier:v; 54, if_statement; 54, 55; 54, 57; 55, not_operator; 55, 56; 56, identifier:methods; 57, block; 57, 58; 58, return_statement; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:entmethods; 62, call; 62, 63; 62, 64; 63, identifier:dir; 64, argument_list; 64, 65; 65, identifier:entity; 66, expression_statement; 66, 67; 67, identifier:print; 68, comment; 69, for_statement; 69, 70; 69, 71; 69, 105; 70, identifier:x; 71, list_comprehension; 71, 72; 71, 73; 71, 79; 72, identifier:x; 73, for_in_clause; 73, 74; 73, 75; 74, identifier:x; 75, call; 75, 76; 75, 77; 76, identifier:dir; 77, argument_list; 77, 78; 78, identifier:self; 79, if_clause; 79, 80; 80, boolean_operator:and; 80, 81; 80, 98; 81, parenthesized_expression; 81, 82; 82, comparison_operator:in; 82, 83; 82, 90; 83, binary_operator:+; 83, 84; 83, 85; 84, string:"bound method "; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:self; 88, identifier:classname; 89, argument_list; 90, call; 90, 91; 90, 92; 91, identifier:str; 92, argument_list; 92, 93; 93, call; 93, 94; 93, 95; 94, identifier:getattr; 95, argument_list; 95, 96; 95, 97; 96, identifier:self; 97, identifier:x; 98, not_operator; 98, 99; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:x; 102, identifier:startswith; 103, argument_list; 103, 104; 104, string:"_"; 105, block; 105, 106; 105, 117; 105, 125; 105, 126; 105, 127; 105, 128; 105, 134; 105, 142; 105, 143; 105, 144; 105, 145; 105, 146; 105, 147; 105, 148; 105, 155; 105, 186; 105, 204; 106, if_statement; 106, 107; 106, 115; 107, boolean_operator:and; 107, 108; 107, 111; 108, parenthesized_expression; 108, 109; 109, not_operator; 109, 110; 110, identifier:showall; 111, parenthesized_expression; 111, 112; 112, comparison_operator:in; 112, 113; 112, 114; 113, identifier:x; 114, identifier:entmethods; 115, block; 115, 116; 116, continue_statement; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:attr; 120, call; 120, 121; 120, 122; 121, identifier:getattr; 122, argument_list; 122, 123; 122, 124; 123, identifier:self; 124, identifier:x; 125, comment; 126, comment; 127, comment; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:doc; 131, attribute; 131, 132; 131, 133; 132, identifier:attr; 133, identifier:__doc__; 134, if_statement; 134, 135; 134, 137; 135, not_operator; 135, 136; 136, identifier:doc; 137, block; 137, 138; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:doc; 141, string:""; 142, comment; 143, comment; 144, comment; 145, comment; 146, comment; 147, comment; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:y; 151, call; 151, 152; 151, 153; 152, identifier:describe_func; 153, argument_list; 153, 154; 154, identifier:attr; 155, if_statement; 155, 156; 155, 158; 155, 163; 156, not_operator; 156, 157; 157, identifier:y; 158, block; 158, 159; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:y; 162, string:""; 163, else_clause; 163, 164; 164, block; 164, 165; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:y; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, string:", "; 171, identifier:join; 172, generator_expression; 172, 173; 172, 181; 173, binary_operator:+; 173, 174; 173, 177; 174, binary_operator:+; 174, 175; 174, 176; 175, identifier:a; 176, string:"="; 177, call; 177, 178; 177, 179; 178, identifier:str; 179, argument_list; 179, 180; 180, identifier:b; 181, for_in_clause; 181, 182; 181, 185; 182, tuple_pattern; 182, 183; 182, 184; 183, identifier:a; 184, identifier:b; 185, identifier:y; 186, print_statement; 186, 187; 186, 202; 186, 203; 187, call; 187, 188; 187, 189; 188, identifier:makeminlength; 189, argument_list; 189, 190; 189, 199; 190, binary_operator:+; 190, 191; 190, 198; 191, binary_operator:+; 191, 192; 191, 197; 192, binary_operator:+; 192, 193; 192, 196; 193, binary_operator:+; 193, 194; 193, 195; 194, string:"."; 195, identifier:x; 196, string:"("; 197, identifier:y; 198, string:")"; 199, attribute; 199, 200; 199, 201; 200, identifier:being; 201, identifier:linelen; 202, string:"\t"; 203, identifier:doc; 204, if_statement; 204, 205; 204, 206; 205, identifier:showall; 206, block; 206, 207; 207, expression_statement; 207, 208; 208, identifier:print
def dir(self,methods=True,showall=True): """Show this object's attributes and methods.""" import inspect #print "[attributes]" for k,v in sorted(self.__dict__.items()): if k.startswith("_"): continue print makeminlength("."+k,being.linelen),"\t",v if not methods: return entmethods=dir(entity) print #print "[methods]" for x in [x for x in dir(self) if ("bound method "+self.classname() in str(getattr(self,x))) and not x.startswith("_")]: if (not showall) and (x in entmethods): continue attr=getattr(self,x) #print attr.__dict__ #print dir(attr) #doc=inspect.getdoc(attr) doc = attr.__doc__ if not doc: doc="" #else: # docsplit=[z for z in doc.replace("\r","\n").split("\n") if z] # if len(docsplit)>1: # doc = docsplit[0] + "\n" + makeminlength(" ",being.linelen) + "\n".join( makeminlength(" ",being.linelen)+"\t"+z for z in docsplit[1:]) # else: # doc = docsplit[0] y=describe_func(attr) if not y: y="" else: y=", ".join(a+"="+str(b) for (a,b) in y) print makeminlength("."+x+"("+y+")",being.linelen),"\t", doc if showall: print
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:tree; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:offset; 7, integer:0; 8, default_parameter; 8, 9; 8, 10; 9, identifier:prefix_inherited; 10, string:""; 11, default_parameter; 11, 12; 11, 13; 12, identifier:nofeatsplease; 13, list:['Phoneme']; 13, 14; 14, string:'Phoneme'; 15, block; 15, 16; 15, 18; 15, 22; 15, 26; 15, 336; 16, expression_statement; 16, 17; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:tree; 21, string:""; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:numchild; 25, integer:0; 26, for_statement; 26, 27; 26, 28; 26, 31; 27, identifier:child; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:children; 31, block; 31, 32; 31, 49; 31, 53; 31, 61; 31, 88; 31, 115; 31, 119; 31, 123; 31, 135; 31, 139; 31, 143; 31, 160; 31, 173; 31, 190; 31, 320; 31, 324; 32, if_statement; 32, 33; 32, 42; 33, comparison_operator:==; 33, 34; 33, 38; 34, call; 34, 35; 34, 36; 35, identifier:type; 36, argument_list; 36, 37; 37, identifier:child; 38, call; 38, 39; 38, 40; 39, identifier:type; 40, argument_list; 40, 41; 41, list:[]; 42, block; 42, 43; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:child; 46, subscript; 46, 47; 46, 48; 47, identifier:child; 48, integer:0; 49, expression_statement; 49, 50; 50, augmented_assignment:+=; 50, 51; 50, 52; 51, identifier:numchild; 52, integer:1; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:classname; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:child; 59, identifier:classname; 60, argument_list; 61, if_statement; 61, 62; 61, 65; 61, 70; 61, 79; 62, comparison_operator:==; 62, 63; 62, 64; 63, identifier:classname; 64, string:"Word"; 65, block; 65, 66; 66, expression_statement; 66, 67; 67, augmented_assignment:+=; 67, 68; 67, 69; 68, identifier:tree; 69, string:"\n\n"; 70, elif_clause; 70, 71; 70, 74; 71, comparison_operator:==; 71, 72; 71, 73; 72, identifier:classname; 73, string:"Line"; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, augmented_assignment:+=; 76, 77; 76, 78; 77, identifier:tree; 78, string:"\n\n\n"; 79, elif_clause; 79, 80; 79, 83; 80, comparison_operator:==; 80, 81; 80, 82; 81, identifier:classname; 82, string:"Stanza"; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, augmented_assignment:+=; 85, 86; 85, 87; 86, identifier:tree; 87, string:"\n\n\n\n"; 88, if_statement; 88, 89; 88, 92; 89, comparison_operator:!=; 89, 90; 89, 91; 90, identifier:offset; 91, integer:0; 92, block; 92, 93; 92, 97; 92, 109; 92, 110; 92, 111; 93, expression_statement; 93, 94; 94, augmented_assignment:+=; 94, 95; 94, 96; 95, identifier:tree; 96, string:"\n"; 97, for_statement; 97, 98; 97, 99; 97, 104; 98, identifier:i; 99, call; 99, 100; 99, 101; 100, identifier:range; 101, argument_list; 101, 102; 101, 103; 102, integer:0; 103, identifier:offset; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, augmented_assignment:+=; 106, 107; 106, 108; 107, identifier:tree; 108, string:" "; 109, comment; 110, comment; 111, expression_statement; 111, 112; 112, augmented_assignment:+=; 112, 113; 112, 114; 113, identifier:tree; 114, string:"|"; 115, expression_statement; 115, 116; 116, augmented_assignment:+=; 116, 117; 116, 118; 117, identifier:tree; 118, string:"\n"; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:newline; 122, string:""; 123, for_statement; 123, 124; 123, 125; 123, 130; 124, identifier:i; 125, call; 125, 126; 125, 127; 126, identifier:range; 127, argument_list; 127, 128; 127, 129; 128, integer:0; 129, identifier:offset; 130, block; 130, 131; 131, expression_statement; 131, 132; 132, augmented_assignment:+=; 132, 133; 132, 134; 133, identifier:newline; 134, string:" "; 135, expression_statement; 135, 136; 136, augmented_assignment:+=; 136, 137; 136, 138; 137, identifier:newline; 138, string:"|"; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:cname; 142, string:""; 143, for_statement; 143, 144; 143, 145; 143, 146; 144, identifier:letter; 145, identifier:classname; 146, block; 146, 147; 147, if_statement; 147, 148; 147, 155; 148, comparison_operator:==; 148, 149; 148, 150; 149, identifier:letter; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:letter; 153, identifier:upper; 154, argument_list; 155, block; 155, 156; 156, expression_statement; 156, 157; 157, augmented_assignment:+=; 157, 158; 157, 159; 158, identifier:cname; 159, identifier:letter; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:prefix; 163, binary_operator:+; 163, 164; 163, 172; 164, binary_operator:+; 164, 165; 164, 168; 165, binary_operator:+; 165, 166; 165, 167; 166, identifier:prefix_inherited; 167, identifier:cname; 168, call; 168, 169; 168, 170; 169, identifier:str; 170, argument_list; 170, 171; 171, identifier:numchild; 172, string:"."; 173, expression_statement; 173, 174; 174, augmented_assignment:+=; 174, 175; 174, 176; 175, identifier:newline; 176, binary_operator:+; 176, 177; 176, 189; 177, binary_operator:+; 177, 178; 177, 188; 178, binary_operator:+; 178, 179; 178, 187; 179, binary_operator:+; 179, 180; 179, 181; 180, string:"-----| ("; 181, subscript; 181, 182; 181, 183; 182, identifier:prefix; 183, slice; 183, 184; 183, 185; 184, colon; 185, unary_operator:-; 185, 186; 186, integer:1; 187, string:") <"; 188, identifier:classname; 189, string:">"; 190, if_statement; 190, 191; 190, 196; 190, 201; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:child; 194, identifier:isBroken; 195, argument_list; 196, block; 196, 197; 197, expression_statement; 197, 198; 198, augmented_assignment:+=; 198, 199; 198, 200; 199, identifier:newline; 200, string:"<<broken>>"; 201, else_clause; 201, 202; 202, block; 202, 203; 202, 212; 202, 248; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:string; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:self; 209, identifier:u2s; 210, argument_list; 210, 211; 211, identifier:child; 212, if_statement; 212, 213; 212, 218; 212, 235; 213, parenthesized_expression; 213, 214; 214, not_operator; 214, 215; 215, comparison_operator:in; 215, 216; 215, 217; 216, string:"<"; 217, identifier:string; 218, block; 218, 219; 218, 227; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 222; 221, identifier:newline; 222, call; 222, 223; 222, 224; 223, identifier:makeminlength; 224, argument_list; 224, 225; 224, 226; 225, identifier:newline; 226, integer:99; 227, expression_statement; 227, 228; 228, augmented_assignment:+=; 228, 229; 228, 230; 229, identifier:newline; 230, binary_operator:+; 230, 231; 230, 234; 231, binary_operator:+; 231, 232; 231, 233; 232, string:"["; 233, identifier:string; 234, string:"]"; 235, elif_clause; 235, 236; 235, 241; 236, comparison_operator:!=; 236, 237; 236, 240; 237, subscript; 237, 238; 237, 239; 238, identifier:string; 239, integer:0; 240, string:"<"; 241, block; 241, 242; 242, expression_statement; 242, 243; 243, augmented_assignment:+=; 243, 244; 243, 245; 244, identifier:newline; 245, binary_operator:+; 245, 246; 245, 247; 246, string:"\t"; 247, identifier:string; 248, if_statement; 248, 249; 248, 255; 249, call; 249, 250; 249, 251; 250, identifier:len; 251, argument_list; 251, 252; 252, attribute; 252, 253; 252, 254; 253, identifier:child; 254, identifier:feats; 255, block; 255, 256; 256, if_statement; 256, 257; 256, 266; 257, parenthesized_expression; 257, 258; 258, not_operator; 258, 259; 259, comparison_operator:in; 259, 260; 259, 265; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:child; 263, identifier:classname; 264, argument_list; 265, identifier:nofeatsplease; 266, block; 266, 267; 267, for_statement; 267, 268; 267, 271; 267, 281; 268, pattern_list; 268, 269; 268, 270; 269, identifier:k; 270, identifier:v; 271, call; 271, 272; 271, 273; 272, identifier:sorted; 273, argument_list; 273, 274; 274, call; 274, 275; 274, 280; 275, attribute; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:child; 278, identifier:feats; 279, identifier:items; 280, argument_list; 281, block; 281, 282; 281, 288; 281, 292; 281, 306; 281, 310; 282, if_statement; 282, 283; 282, 286; 283, comparison_operator:==; 283, 284; 283, 285; 284, identifier:v; 285, None; 286, block; 286, 287; 287, continue_statement; 288, expression_statement; 288, 289; 289, augmented_assignment:+=; 289, 290; 289, 291; 290, identifier:newline; 291, string:"\n"; 292, for_statement; 292, 293; 292, 294; 292, 301; 293, identifier:i; 294, call; 294, 295; 294, 296; 295, identifier:range; 296, argument_list; 296, 297; 296, 298; 297, integer:0; 298, binary_operator:+; 298, 299; 298, 300; 299, identifier:offset; 300, integer:1; 301, block; 301, 302; 302, expression_statement; 302, 303; 303, augmented_assignment:+=; 303, 304; 303, 305; 304, identifier:newline; 305, string:" "; 306, expression_statement; 306, 307; 307, augmented_assignment:+=; 307, 308; 307, 309; 308, identifier:newline; 309, string:"| "; 310, expression_statement; 310, 311; 311, augmented_assignment:+=; 311, 312; 311, 313; 312, identifier:newline; 313, call; 313, 314; 313, 317; 314, attribute; 314, 315; 314, 316; 315, identifier:self; 316, identifier:showFeat; 317, argument_list; 317, 318; 317, 319; 318, identifier:k; 319, identifier:v; 320, expression_statement; 320, 321; 321, augmented_assignment:+=; 321, 322; 321, 323; 322, identifier:tree; 323, identifier:newline; 324, expression_statement; 324, 325; 325, augmented_assignment:+=; 325, 326; 325, 327; 326, identifier:tree; 327, call; 327, 328; 327, 331; 328, attribute; 328, 329; 328, 330; 329, identifier:child; 330, identifier:tree; 331, argument_list; 331, 332; 331, 335; 332, binary_operator:+; 332, 333; 332, 334; 333, identifier:offset; 334, integer:1; 335, identifier:prefix; 336, return_statement; 336, 337; 337, identifier:tree
def tree(self,offset=0,prefix_inherited="",nofeatsplease=['Phoneme']): """Print a tree-structure of this object's phonological representation.""" tree = "" numchild=0 for child in self.children: if type(child)==type([]): child=child[0] numchild+=1 classname=child.classname() if classname=="Word": tree+="\n\n" elif classname=="Line": tree+="\n\n\n" elif classname=="Stanza": tree+="\n\n\n\n" if offset!=0: tree+="\n" for i in range(0,offset): tree+=" " #if not len(child.feats): # tree+=" " tree+="|" tree+="\n" newline="" for i in range(0,offset): newline+=" " newline+="|" cname="" for letter in classname: if letter==letter.upper(): cname+=letter prefix=prefix_inherited+cname+str(numchild) + "." newline+="-----| ("+prefix[:-1]+") <"+classname+">" if child.isBroken(): newline+="<<broken>>" else: string=self.u2s(child) if (not "<" in string): newline=makeminlength(newline,99) newline+="["+string+"]" elif string[0]!="<": newline+="\t"+string if len(child.feats): if (not child.classname() in nofeatsplease): for k,v in sorted(child.feats.items()): if v==None: continue newline+="\n" for i in range(0,offset+1): newline+=" " newline+="| " newline+=self.showFeat(k,v) tree+=newline tree+=child.tree(offset+1,prefix) return tree
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:stats_positions; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:meter; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:all_parses; 10, False; 11, block; 11, 12; 11, 14; 11, 16; 11, 42; 11, 46; 11, 134; 11, 302; 11, 314; 11, 333; 11, 334; 11, 370; 11, 381; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:parses; 19, conditional_expression:if; 19, 20; 19, 28; 19, 29; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:allParses; 24, argument_list; 24, 25; 25, keyword_argument; 25, 26; 25, 27; 26, identifier:meter; 27, identifier:meter; 28, identifier:all_parses; 29, list_comprehension; 29, 30; 29, 32; 30, list:[parse]; 30, 31; 31, identifier:parse; 32, for_in_clause; 32, 33; 32, 34; 33, identifier:parse; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:bestParses; 38, argument_list; 38, 39; 39, keyword_argument; 39, 40; 39, 41; 40, identifier:meter; 41, identifier:meter; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:dx; 45, dictionary; 46, for_statement; 46, 47; 46, 48; 46, 49; 47, identifier:parselist; 48, identifier:parses; 49, block; 49, 50; 50, for_statement; 50, 51; 50, 52; 50, 53; 51, identifier:parse; 52, identifier:parselist; 53, block; 53, 54; 53, 59; 53, 63; 54, if_statement; 54, 55; 54, 57; 55, not_operator; 55, 56; 56, identifier:parse; 57, block; 57, 58; 58, continue_statement; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:slot_i; 62, integer:0; 63, for_statement; 63, 64; 63, 65; 63, 68; 64, identifier:pos; 65, attribute; 65, 66; 65, 67; 66, identifier:parse; 67, identifier:positions; 68, block; 68, 69; 69, for_statement; 69, 70; 69, 71; 69, 74; 70, identifier:slot; 71, attribute; 71, 72; 71, 73; 72, identifier:pos; 73, identifier:slots; 74, block; 74, 75; 74, 79; 74, 92; 75, expression_statement; 75, 76; 76, augmented_assignment:+=; 76, 77; 76, 78; 77, identifier:slot_i; 78, integer:1; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:feat_dicts; 82, list:[slot.feats, pos.constraintScores, pos.feats]; 82, 83; 82, 86; 82, 89; 83, attribute; 83, 84; 83, 85; 84, identifier:slot; 85, identifier:feats; 86, attribute; 86, 87; 86, 88; 87, identifier:pos; 88, identifier:constraintScores; 89, attribute; 89, 90; 89, 91; 90, identifier:pos; 91, identifier:feats; 92, for_statement; 92, 93; 92, 94; 92, 95; 93, identifier:feat_dict; 94, identifier:feat_dicts; 95, block; 95, 96; 96, for_statement; 96, 97; 96, 100; 96, 105; 97, pattern_list; 97, 98; 97, 99; 98, identifier:k; 99, identifier:v; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:feat_dict; 103, identifier:items; 104, argument_list; 105, block; 105, 106; 105, 115; 105, 127; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:dk; 109, tuple; 109, 110; 109, 111; 110, identifier:slot_i; 111, call; 111, 112; 111, 113; 112, identifier:str; 113, argument_list; 113, 114; 114, identifier:k; 115, if_statement; 115, 116; 115, 120; 116, not_operator; 116, 117; 117, comparison_operator:in; 117, 118; 117, 119; 118, identifier:dk; 119, identifier:dx; 120, block; 120, 121; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 126; 123, subscript; 123, 124; 123, 125; 124, identifier:dx; 125, identifier:dk; 126, list:[]; 127, expression_statement; 127, 128; 128, augmented_assignment:+=; 128, 129; 128, 132; 129, subscript; 129, 130; 129, 131; 130, identifier:dx; 131, identifier:dk; 132, list:[v]; 132, 133; 133, identifier:v; 134, function_definition; 134, 135; 134, 136; 134, 137; 135, function_name:_writegen; 136, parameters; 137, block; 137, 138; 138, for_statement; 138, 139; 138, 144; 138, 152; 139, tuple_pattern; 139, 140; 139, 143; 140, tuple_pattern; 140, 141; 140, 142; 141, identifier:slot_i; 142, identifier:k; 143, identifier:l; 144, call; 144, 145; 144, 146; 145, identifier:sorted; 146, argument_list; 146, 147; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:dx; 150, identifier:items; 151, argument_list; 152, block; 152, 153; 152, 157; 152, 225; 152, 226; 152, 227; 152, 232; 152, 247; 152, 254; 152, 261; 152, 262; 152, 263; 152, 287; 152, 298; 152, 299; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:l2; 156, list:[]; 157, for_statement; 157, 158; 157, 159; 157, 160; 158, identifier:x; 159, identifier:l; 160, block; 160, 161; 160, 211; 160, 220; 161, if_statement; 161, 162; 161, 168; 161, 176; 161, 191; 161, 202; 162, comparison_operator:==; 162, 163; 162, 167; 163, call; 163, 164; 163, 165; 164, identifier:type; 165, argument_list; 165, 166; 166, identifier:x; 167, identifier:bool; 168, block; 168, 169; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:x; 172, conditional_expression:if; 172, 173; 172, 174; 172, 175; 173, integer:1; 174, identifier:x; 175, integer:0; 176, elif_clause; 176, 177; 176, 186; 177, comparison_operator:==; 177, 178; 177, 182; 178, call; 178, 179; 178, 180; 179, identifier:type; 180, argument_list; 180, 181; 181, identifier:x; 182, call; 182, 183; 182, 184; 183, identifier:type; 184, argument_list; 184, 185; 185, None; 186, block; 186, 187; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:x; 190, integer:0; 191, elif_clause; 191, 192; 191, 200; 192, comparison_operator:in; 192, 193; 192, 197; 193, call; 193, 194; 193, 195; 194, identifier:type; 195, argument_list; 195, 196; 196, identifier:x; 197, list:[str,unicode]; 197, 198; 197, 199; 198, identifier:str; 199, identifier:unicode; 200, block; 200, 201; 201, continue_statement; 202, else_clause; 202, 203; 203, block; 203, 204; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:x; 207, call; 207, 208; 207, 209; 208, identifier:float; 209, argument_list; 209, 210; 210, identifier:x; 211, if_statement; 211, 212; 211, 215; 212, comparison_operator:>; 212, 213; 212, 214; 213, identifier:x; 214, integer:1; 215, block; 215, 216; 216, expression_statement; 216, 217; 217, assignment; 217, 218; 217, 219; 218, identifier:x; 219, integer:1; 220, expression_statement; 220, 221; 221, augmented_assignment:+=; 221, 222; 221, 223; 222, identifier:l2; 223, list:[x]; 223, 224; 224, identifier:x; 225, comment; 226, comment; 227, if_statement; 227, 228; 227, 230; 228, not_operator; 228, 229; 229, identifier:l2; 230, block; 230, 231; 231, continue_statement; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 235; 234, identifier:avg; 235, binary_operator:/; 235, 236; 235, 240; 236, call; 236, 237; 236, 238; 237, identifier:sum; 238, argument_list; 238, 239; 239, identifier:l2; 240, call; 240, 241; 240, 242; 241, identifier:float; 242, argument_list; 242, 243; 243, call; 243, 244; 243, 245; 244, identifier:len; 245, argument_list; 245, 246; 246, identifier:l2; 247, expression_statement; 247, 248; 248, assignment; 248, 249; 248, 250; 249, identifier:count; 250, call; 250, 251; 250, 252; 251, identifier:sum; 252, argument_list; 252, 253; 253, identifier:l2; 254, expression_statement; 254, 255; 255, assignment; 255, 256; 255, 257; 256, identifier:chances; 257, call; 257, 258; 257, 259; 258, identifier:len; 259, argument_list; 259, 260; 260, identifier:l2; 261, comment; 262, comment; 263, expression_statement; 263, 264; 264, assignment; 264, 265; 264, 266; 265, identifier:odx; 266, dictionary; 266, 267; 266, 270; 266, 273; 266, 276; 266, 279; 266, 282; 267, pair; 267, 268; 267, 269; 268, string:'slot_num'; 269, identifier:slot_i; 270, pair; 270, 271; 270, 272; 271, string:'statistic'; 272, identifier:k; 273, pair; 273, 274; 273, 275; 274, string:'average'; 275, identifier:avg; 276, pair; 276, 277; 276, 278; 277, string:'count'; 278, identifier:count; 279, pair; 279, 280; 279, 281; 280, string:'chances'; 281, identifier:chances; 282, pair; 282, 283; 282, 284; 283, string:'text'; 284, attribute; 284, 285; 284, 286; 285, identifier:self; 286, identifier:name; 287, expression_statement; 287, 288; 288, assignment; 288, 289; 288, 292; 289, subscript; 289, 290; 289, 291; 290, identifier:odx; 291, string:'header'; 292, list:['slot_num', 'statistic','count','chances','average']; 292, 293; 292, 294; 292, 295; 292, 296; 292, 297; 293, string:'slot_num'; 294, string:'statistic'; 295, string:'count'; 296, string:'chances'; 297, string:'average'; 298, comment; 299, expression_statement; 299, 300; 300, yield; 300, 301; 301, identifier:odx; 302, expression_statement; 302, 303; 303, assignment; 303, 304; 303, 305; 304, identifier:name; 305, call; 305, 306; 305, 311; 306, attribute; 306, 307; 306, 310; 307, attribute; 307, 308; 307, 309; 308, identifier:self; 309, identifier:name; 310, identifier:replace; 311, argument_list; 311, 312; 311, 313; 312, string:'.txt'; 313, string:''; 314, expression_statement; 314, 315; 315, assignment; 315, 316; 315, 317; 316, identifier:ofn; 317, call; 317, 318; 317, 323; 318, attribute; 318, 319; 318, 322; 319, attribute; 319, 320; 319, 321; 320, identifier:os; 321, identifier:path; 322, identifier:join; 323, argument_list; 323, 324; 323, 327; 323, 328; 323, 329; 323, 330; 324, attribute; 324, 325; 324, 326; 325, identifier:self; 326, identifier:dir_results; 327, string:'stats'; 328, string:'texts'; 329, identifier:name; 330, binary_operator:+; 330, 331; 330, 332; 331, identifier:name; 332, string:'.positions.csv'; 333, comment; 334, if_statement; 334, 335; 334, 353; 335, not_operator; 335, 336; 336, call; 336, 337; 336, 342; 337, attribute; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, identifier:os; 340, identifier:path; 341, identifier:exists; 342, argument_list; 342, 343; 343, subscript; 343, 344; 343, 352; 344, call; 344, 345; 344, 350; 345, attribute; 345, 346; 345, 349; 346, attribute; 346, 347; 346, 348; 347, identifier:os; 348, identifier:path; 349, identifier:split; 350, argument_list; 350, 351; 351, identifier:ofn; 352, integer:0; 353, block; 353, 354; 354, expression_statement; 354, 355; 355, call; 355, 356; 355, 359; 356, attribute; 356, 357; 356, 358; 357, identifier:os; 358, identifier:makedirs; 359, argument_list; 359, 360; 360, subscript; 360, 361; 360, 369; 361, call; 361, 362; 361, 367; 362, attribute; 362, 363; 362, 366; 363, attribute; 363, 364; 363, 365; 364, identifier:os; 365, identifier:path; 366, identifier:split; 367, argument_list; 367, 368; 368, identifier:ofn; 369, integer:0; 370, for_statement; 370, 371; 370, 372; 370, 377; 371, identifier:dx; 372, call; 372, 373; 372, 374; 373, identifier:writegengen; 374, argument_list; 374, 375; 374, 376; 375, identifier:ofn; 376, identifier:_writegen; 377, block; 377, 378; 378, expression_statement; 378, 379; 379, yield; 379, 380; 380, identifier:dx; 381, print_statement; 381, 382; 381, 383; 382, string:'>> saved:'; 383, identifier:ofn
def stats_positions(self,meter=None,all_parses=False): """Produce statistics from the parser""" """Positions All feats of slots All constraint violations """ parses = self.allParses(meter=meter) if all_parses else [[parse] for parse in self.bestParses(meter=meter)] dx={} for parselist in parses: for parse in parselist: if not parse: continue slot_i=0 for pos in parse.positions: for slot in pos.slots: slot_i+=1 feat_dicts = [slot.feats, pos.constraintScores, pos.feats] for feat_dict in feat_dicts: for k,v in feat_dict.items(): dk = (slot_i,str(k)) if not dk in dx: dx[dk]=[] dx[dk]+=[v] def _writegen(): for ((slot_i,k),l) in sorted(dx.items()): l2=[] for x in l: if type(x)==bool: x=1 if x else 0 elif type(x)==type(None): x=0 elif type(x) in [str,unicode]: continue else: x=float(x) if x>1: x=1 l2+=[x] #print k, l2 #try: if not l2: continue avg=sum(l2) / float(len(l2)) count=sum(l2) chances=len(l2) #except TypeError: # continue odx={'slot_num':slot_i, 'statistic':k, 'average':avg, 'count':count, 'chances':chances, 'text':self.name} odx['header']=['slot_num', 'statistic','count','chances','average'] #print odx yield odx name=self.name.replace('.txt','') ofn=os.path.join(self.dir_results, 'stats','texts',name, name+'.positions.csv') #print ofn if not os.path.exists(os.path.split(ofn)[0]): os.makedirs(os.path.split(ofn)[0]) for dx in writegengen(ofn, _writegen): yield dx print '>> saved:',ofn
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:iparse; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:meter; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:num_processes; 10, integer:1; 11, default_parameter; 11, 12; 11, 13; 12, identifier:arbiter; 13, string:'Line'; 14, default_parameter; 14, 15; 14, 16; 15, identifier:line_lim; 16, None; 17, block; 17, 18; 17, 20; 17, 31; 17, 36; 17, 45; 17, 46; 17, 56; 17, 66; 17, 76; 17, 86; 17, 94; 17, 102; 17, 109; 17, 113; 17, 122; 17, 134; 17, 146; 17, 147; 17, 167; 17, 168; 17, 180; 17, 187; 17, 188; 17, 196; 17, 208; 17, 345; 18, expression_statement; 18, 19; 19, comment; 20, import_from_statement; 20, 21; 20, 23; 20, 25; 20, 27; 20, 29; 21, dotted_name; 21, 22; 22, identifier:Meter; 23, dotted_name; 23, 24; 24, identifier:Meter; 25, dotted_name; 25, 26; 26, identifier:genDefault; 27, dotted_name; 27, 28; 28, identifier:parse_ent; 29, dotted_name; 29, 30; 30, identifier:parse_ent_mp; 31, import_statement; 31, 32; 32, aliased_import; 32, 33; 32, 35; 33, dotted_name; 33, 34; 34, identifier:multiprocessing; 35, identifier:mp; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:meter; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:get_meter; 43, argument_list; 43, 44; 44, identifier:meter; 45, comment; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 55; 48, subscript; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:__parses; 52, attribute; 52, 53; 52, 54; 53, identifier:meter; 54, identifier:id; 55, list:[]; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 65; 58, subscript; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:__bestparses; 62, attribute; 62, 63; 62, 64; 63, identifier:meter; 64, identifier:id; 65, list:[]; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 75; 68, subscript; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:__boundParses; 72, attribute; 72, 73; 72, 74; 73, identifier:meter; 74, identifier:id; 75, list:[]; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 85; 78, subscript; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:self; 81, identifier:__parsed_ents; 82, attribute; 82, 83; 82, 84; 83, identifier:meter; 84, identifier:id; 85, list:[]; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:lines; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:self; 92, identifier:lines; 93, argument_list; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:lines; 97, subscript; 97, 98; 97, 99; 98, identifier:lines; 99, slice; 99, 100; 99, 101; 100, colon; 101, identifier:line_lim; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:numlines; 105, call; 105, 106; 105, 107; 106, identifier:len; 107, argument_list; 107, 108; 108, identifier:lines; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:init; 112, identifier:self; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:ents; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:ents; 120, argument_list; 120, 121; 121, identifier:arbiter; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:smax; 125, call; 125, 126; 125, 131; 126, attribute; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:self; 129, identifier:config; 130, identifier:get; 131, argument_list; 131, 132; 131, 133; 132, string:'line_maxsylls'; 133, integer:100; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:smin; 137, call; 137, 138; 137, 143; 138, attribute; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:self; 141, identifier:config; 142, identifier:get; 143, argument_list; 143, 144; 143, 145; 144, string:'line_minsylls'; 145, integer:0; 146, comment; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:ents; 150, list_comprehension; 150, 151; 150, 152; 150, 155; 151, identifier:e; 152, for_in_clause; 152, 153; 152, 154; 153, identifier:e; 154, identifier:ents; 155, if_clause; 155, 156; 156, boolean_operator:and; 156, 157; 156, 162; 157, comparison_operator:>=; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:e; 160, identifier:num_syll; 161, identifier:smin; 162, comparison_operator:<=; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:e; 165, identifier:num_syll; 166, identifier:smax; 167, comment; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:self; 172, identifier:scansion_prepare; 173, argument_list; 173, 174; 173, 177; 174, keyword_argument; 174, 175; 174, 176; 175, identifier:meter; 176, identifier:meter; 177, keyword_argument; 177, 178; 177, 179; 178, identifier:conscious; 179, True; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:numents; 183, call; 183, 184; 183, 185; 184, identifier:len; 185, argument_list; 185, 186; 186, identifier:ents; 187, comment; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:toprint; 191, subscript; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:self; 194, identifier:config; 195, string:'print_to_screen'; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 199; 198, identifier:objects; 199, list_comprehension; 199, 200; 199, 205; 200, tuple; 200, 201; 200, 202; 200, 203; 200, 204; 201, identifier:ent; 202, identifier:meter; 203, identifier:init; 204, False; 205, for_in_clause; 205, 206; 205, 207; 206, identifier:ent; 207, identifier:ents; 208, if_statement; 208, 209; 208, 212; 208, 256; 209, comparison_operator:>; 209, 210; 209, 211; 210, identifier:num_processes; 211, integer:1; 212, block; 212, 213; 212, 215; 212, 224; 212, 239; 213, print_statement; 213, 214; 214, string:'!! MULTIPROCESSING PARSING IS NOT WORKING YET !!'; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:pool; 218, call; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:mp; 221, identifier:Pool; 222, argument_list; 222, 223; 223, identifier:num_processes; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:jobs; 227, list_comprehension; 227, 228; 227, 236; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:pool; 231, identifier:apply_async; 232, argument_list; 232, 233; 232, 234; 233, identifier:parse_ent_mp; 234, tuple; 234, 235; 235, identifier:x; 236, for_in_clause; 236, 237; 236, 238; 237, identifier:x; 238, identifier:objects; 239, for_statement; 239, 240; 239, 241; 239, 242; 240, identifier:j; 241, identifier:jobs; 242, block; 242, 243; 242, 249; 243, print_statement; 243, 244; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:j; 247, identifier:get; 248, argument_list; 249, expression_statement; 249, 250; 250, yield; 250, 251; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:j; 254, identifier:get; 255, argument_list; 256, else_clause; 256, 257; 257, block; 257, 258; 257, 266; 257, 270; 257, 271; 258, expression_statement; 258, 259; 259, assignment; 259, 260; 259, 261; 260, identifier:now; 261, call; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:time; 264, identifier:time; 265, argument_list; 266, expression_statement; 266, 267; 267, assignment; 267, 268; 267, 269; 268, identifier:clock_snum; 269, integer:0; 270, comment; 271, for_statement; 271, 272; 271, 275; 271, 279; 272, pattern_list; 272, 273; 272, 274; 273, identifier:ei; 274, identifier:objectx; 275, call; 275, 276; 275, 277; 276, identifier:enumerate; 277, argument_list; 277, 278; 278, identifier:objects; 279, block; 279, 280; 279, 286; 279, 339; 280, expression_statement; 280, 281; 281, augmented_assignment:+=; 281, 282; 281, 283; 282, identifier:clock_snum; 283, attribute; 283, 284; 283, 285; 284, identifier:ent; 285, identifier:num_syll; 286, if_statement; 286, 287; 286, 293; 287, boolean_operator:and; 287, 288; 287, 289; 288, identifier:ei; 289, not_operator; 289, 290; 290, binary_operator:%; 290, 291; 290, 292; 291, identifier:ei; 292, integer:100; 293, block; 293, 294; 293, 302; 293, 331; 293, 335; 294, expression_statement; 294, 295; 295, assignment; 295, 296; 295, 297; 296, identifier:nownow; 297, call; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:time; 300, identifier:time; 301, argument_list; 302, if_statement; 302, 303; 302, 308; 303, subscript; 303, 304; 303, 307; 304, attribute; 304, 305; 304, 306; 305, identifier:self; 306, identifier:config; 307, string:'print_to_screen'; 308, block; 308, 309; 309, print_statement; 309, 310; 309, 311; 309, 312; 309, 313; 309, 314; 309, 315; 309, 316; 309, 329; 309, 330; 310, string:'>> parsing line #'; 311, identifier:ei; 312, string:'of'; 313, identifier:numents; 314, string:'lines'; 315, string:'['; 316, call; 316, 317; 316, 318; 317, identifier:round; 318, argument_list; 318, 319; 318, 328; 319, call; 319, 320; 319, 321; 320, identifier:float; 321, argument_list; 321, 322; 322, binary_operator:/; 322, 323; 322, 324; 323, identifier:clock_snum; 324, parenthesized_expression; 324, 325; 325, binary_operator:-; 325, 326; 325, 327; 326, identifier:nownow; 327, identifier:now; 328, integer:2; 329, string:'syllables/second'; 330, string:']'; 331, expression_statement; 331, 332; 332, assignment; 332, 333; 332, 334; 333, identifier:now; 334, identifier:nownow; 335, expression_statement; 335, 336; 336, assignment; 336, 337; 336, 338; 337, identifier:clock_snum; 338, integer:0; 339, expression_statement; 339, 340; 340, yield; 340, 341; 341, call; 341, 342; 341, 343; 342, identifier:parse_ent_mp; 343, argument_list; 343, 344; 344, identifier:objectx; 345, if_statement; 345, 346; 345, 351; 346, subscript; 346, 347; 346, 350; 347, attribute; 347, 348; 347, 349; 348, identifier:self; 349, identifier:config; 350, string:'print_to_screen'; 351, block; 351, 352; 352, print_statement; 352, 353; 352, 354; 352, 361; 353, string:'>> parsing complete in:'; 354, binary_operator:-; 354, 355; 354, 360; 355, call; 355, 356; 355, 359; 356, attribute; 356, 357; 356, 358; 357, identifier:time; 358, identifier:time; 359, argument_list; 360, identifier:now; 361, string:'seconds'
def iparse(self,meter=None,num_processes=1,arbiter='Line',line_lim=None): """Parse this text metrically, yielding it line by line.""" from Meter import Meter,genDefault,parse_ent,parse_ent_mp import multiprocessing as mp meter=self.get_meter(meter) # set internal attributes self.__parses[meter.id]=[] self.__bestparses[meter.id]=[] self.__boundParses[meter.id]=[] self.__parsed_ents[meter.id]=[] lines = self.lines() lines=lines[:line_lim] numlines = len(lines) init=self ents=self.ents(arbiter) smax=self.config.get('line_maxsylls',100) smin=self.config.get('line_minsylls',0) #print '>> # of lines to parse:',len(ents) ents = [e for e in ents if e.num_syll >= smin and e.num_syll<=smax] #print '>> # of lines to parse after applying min/max line settings:',len(ents) self.scansion_prepare(meter=meter,conscious=True) numents=len(ents) #pool=mp.Pool(1) toprint=self.config['print_to_screen'] objects = [(ent,meter,init,False) for ent in ents] if num_processes>1: print '!! MULTIPROCESSING PARSING IS NOT WORKING YET !!' pool = mp.Pool(num_processes) jobs = [pool.apply_async(parse_ent_mp,(x,)) for x in objects] for j in jobs: print j.get() yield j.get() else: now=time.time() clock_snum=0 #for ei,ent in enumerate(pool.imap(parse_ent_mp,objects)): for ei,objectx in enumerate(objects): clock_snum+=ent.num_syll if ei and not ei%100: nownow=time.time() if self.config['print_to_screen']: print '>> parsing line #',ei,'of',numents,'lines','[',round(float(clock_snum/(nownow-now)),2),'syllables/second',']' now=nownow clock_snum=0 yield parse_ent_mp(objectx) if self.config['print_to_screen']: print '>> parsing complete in:',time.time()-now,'seconds'
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:from_representation; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:representation; 6, block; 6, 7; 6, 9; 6, 13; 6, 17; 6, 173; 6, 207; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:object_dict; 12, dictionary; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:failed; 16, dictionary; 17, for_statement; 17, 18; 17, 21; 17, 28; 18, pattern_list; 18, 19; 18, 20; 19, identifier:name; 20, identifier:field; 21, call; 21, 22; 21, 27; 22, attribute; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:fields; 26, identifier:items; 27, argument_list; 28, block; 28, 29; 28, 35; 29, if_statement; 29, 30; 29, 33; 30, comparison_operator:not; 30, 31; 30, 32; 31, identifier:name; 32, identifier:representation; 33, block; 33, 34; 34, continue_statement; 35, try_statement; 35, 36; 35, 158; 36, block; 36, 37; 36, 61; 36, 69; 36, 75; 37, if_statement; 37, 38; 37, 55; 38, parenthesized_expression; 38, 39; 38, 40; 38, 41; 39, comment; 40, comment; 41, boolean_operator:and; 41, 42; 41, 52; 42, not_operator; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:isinstance; 45, argument_list; 45, 46; 45, 49; 46, subscript; 46, 47; 46, 48; 47, identifier:representation; 48, identifier:name; 49, tuple; 49, 50; 49, 51; 50, identifier:list; 51, identifier:tuple; 52, attribute; 52, 53; 52, 54; 53, identifier:field; 54, identifier:many; 55, block; 55, 56; 56, raise_statement; 56, 57; 57, call; 57, 58; 57, 59; 58, identifier:ValueError; 59, argument_list; 59, 60; 60, string:"field should be sequence"; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:source; 64, call; 64, 65; 64, 66; 65, identifier:_source; 66, argument_list; 66, 67; 66, 68; 67, identifier:name; 68, identifier:field; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:value; 72, subscript; 72, 73; 72, 74; 73, identifier:representation; 74, identifier:name; 75, if_statement; 75, 76; 75, 79; 75, 123; 76, attribute; 76, 77; 76, 78; 77, identifier:field; 78, identifier:many; 79, block; 79, 80; 80, if_statement; 80, 81; 80, 85; 80, 101; 81, not_operator; 81, 82; 82, attribute; 82, 83; 82, 84; 83, identifier:field; 84, identifier:allow_null; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 91; 88, subscript; 88, 89; 88, 90; 89, identifier:object_dict; 90, identifier:source; 91, list_comprehension; 91, 92; 91, 98; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:field; 95, identifier:from_representation; 96, argument_list; 96, 97; 97, identifier:single_value; 98, for_in_clause; 98, 99; 98, 100; 99, identifier:single_value; 100, identifier:value; 101, else_clause; 101, 102; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 108; 105, subscript; 105, 106; 105, 107; 106, identifier:object_dict; 107, identifier:source; 108, list_comprehension; 108, 109; 108, 120; 109, conditional_expression:if; 109, 110; 109, 116; 109, 119; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:field; 113, identifier:from_representation; 114, argument_list; 114, 115; 115, identifier:single_value; 116, comparison_operator:is; 116, 117; 116, 118; 117, identifier:single_value; 118, None; 119, None; 120, for_in_clause; 120, 121; 120, 122; 121, identifier:single_value; 122, identifier:value; 123, else_clause; 123, 124; 124, block; 124, 125; 125, if_statement; 125, 126; 125, 130; 125, 142; 126, not_operator; 126, 127; 127, attribute; 127, 128; 127, 129; 128, identifier:field; 129, identifier:allow_null; 130, block; 130, 131; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 136; 133, subscript; 133, 134; 133, 135; 134, identifier:object_dict; 135, identifier:source; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:field; 139, identifier:from_representation; 140, argument_list; 140, 141; 141, identifier:value; 142, else_clause; 142, 143; 143, block; 143, 144; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 149; 146, subscript; 146, 147; 146, 148; 147, identifier:object_dict; 148, identifier:source; 149, conditional_expression:if; 149, 150; 149, 156; 149, 157; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:field; 153, identifier:from_representation; 154, argument_list; 154, 155; 155, identifier:value; 156, identifier:value; 157, None; 158, except_clause; 158, 159; 158, 163; 159, as_pattern; 159, 160; 159, 161; 160, identifier:ValueError; 161, as_pattern_target; 161, 162; 162, identifier:err; 163, block; 163, 164; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 169; 166, subscript; 166, 167; 166, 168; 167, identifier:failed; 168, identifier:name; 169, call; 169, 170; 169, 171; 170, identifier:str; 171, argument_list; 171, 172; 172, identifier:err; 173, if_statement; 173, 174; 173, 175; 173, 176; 173, 177; 174, identifier:failed; 175, comment; 176, comment; 177, block; 177, 178; 178, try_statement; 178, 179; 178, 194; 179, block; 179, 180; 179, 187; 179, 188; 179, 189; 179, 193; 180, expression_statement; 180, 181; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:self; 184, identifier:validate; 185, argument_list; 185, 186; 186, identifier:object_dict; 187, comment; 188, comment; 189, raise_statement; 189, 190; 190, call; 190, 191; 190, 192; 191, identifier:DeserializationError; 192, argument_list; 193, comment; 194, except_clause; 194, 195; 194, 199; 195, as_pattern; 195, 196; 195, 197; 196, identifier:DeserializationError; 197, as_pattern_target; 197, 198; 198, identifier:err; 199, block; 199, 200; 199, 206; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:err; 204, identifier:failed; 205, identifier:failed; 206, raise_statement; 207, return_statement; 207, 208; 208, identifier:object_dict
def from_representation(self, representation): """Convert given representation dict into internal object. Internal object is simply a dictionary of values with respect to field sources. This does not check if all required fields exist or values are valid in terms of value validation (see: :meth:`BaseField.validate()`) but still requires all of passed representation values to be well formed representation (success call to ``field.from_representation``). In case of malformed representation it will run additional validation only to provide a full detailed exception about all that might be wrong with provided representation. Args: representation (dict): dictionary with field representation values Raises: DeserializationError: when at least one representation field is not formed as expected by field object. Information about additional forbidden/missing/invalid fields is provided as well. """ object_dict = {} failed = {} for name, field in self.fields.items(): if name not in representation: continue try: if ( # note: we cannot check for any sequence or iterable # because of strings and nested dicts. not isinstance(representation[name], (list, tuple)) and field.many ): raise ValueError("field should be sequence") source = _source(name, field) value = representation[name] if field.many: if not field.allow_null: object_dict[source] = [ field.from_representation(single_value) for single_value in value ] else: object_dict[source] = [ field.from_representation(single_value) if single_value is not None else None for single_value in value ] else: if not field.allow_null: object_dict[source] = field.from_representation(value) else: object_dict[source] = field.from_representation( value) if value else None except ValueError as err: failed[name] = str(err) if failed: # if failed to parse we eagerly perform validation so full # information about what is wrong will be returned try: self.validate(object_dict) # note: this exception can be reached with partial==True # since do not support partial updates yet this has 'no cover' raise DeserializationError() # pragma: no cover except DeserializationError as err: err.failed = failed raise return object_dict
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:require_params; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:req; 6, block; 6, 7; 6, 9; 6, 13; 6, 208; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:params; 12, dictionary; 13, for_statement; 13, 14; 13, 17; 13, 24; 14, pattern_list; 14, 15; 14, 16; 15, identifier:name; 16, identifier:param; 17, call; 17, 18; 17, 23; 18, attribute; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:params; 22, identifier:items; 23, argument_list; 24, block; 24, 25; 25, if_statement; 25, 26; 25, 35; 25, 36; 25, 37; 25, 38; 25, 82; 26, boolean_operator:and; 26, 27; 26, 32; 27, comparison_operator:not; 27, 28; 27, 29; 28, identifier:name; 29, attribute; 29, 30; 29, 31; 30, identifier:req; 31, identifier:params; 32, attribute; 32, 33; 32, 34; 33, identifier:param; 34, identifier:required; 35, comment; 36, comment; 37, comment; 38, block; 38, 39; 38, 70; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:missing; 42, binary_operator:-; 42, 43; 42, 60; 43, call; 43, 44; 43, 45; 44, identifier:set; 45, generator_expression; 45, 46; 45, 47; 45, 52; 46, identifier:p; 47, for_in_clause; 47, 48; 47, 49; 48, identifier:p; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:params; 52, if_clause; 52, 53; 53, attribute; 53, 54; 53, 59; 54, subscript; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:params; 58, identifier:p; 59, identifier:required; 60, call; 60, 61; 60, 62; 61, identifier:set; 62, argument_list; 62, 63; 63, call; 63, 64; 63, 69; 64, attribute; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:req; 67, identifier:params; 68, identifier:keys; 69, argument_list; 70, raise_statement; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:errors; 74, identifier:HTTPMissingParam; 75, argument_list; 75, 76; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, string:", "; 79, identifier:join; 80, argument_list; 80, 81; 81, identifier:missing; 82, elif_clause; 82, 83; 82, 92; 82, 93; 82, 94; 82, 95; 82, 96; 82, 97; 82, 98; 82, 99; 83, boolean_operator:or; 83, 84; 83, 89; 84, comparison_operator:in; 84, 85; 84, 86; 85, identifier:name; 86, attribute; 86, 87; 86, 88; 87, identifier:req; 88, identifier:params; 89, attribute; 89, 90; 89, 91; 90, identifier:param; 91, identifier:default; 92, comment; 93, comment; 94, comment; 95, comment; 96, comment; 97, comment; 98, comment; 99, block; 99, 100; 100, try_statement; 100, 101; 100, 175; 100, 190; 101, block; 101, 102; 102, if_statement; 102, 103; 102, 106; 102, 107; 102, 148; 103, attribute; 103, 104; 103, 105; 104, identifier:param; 105, identifier:many; 106, comment; 107, block; 107, 108; 107, 137; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:values; 111, boolean_operator:or; 111, 112; 111, 124; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:req; 115, identifier:get_param_as_list; 116, argument_list; 116, 117; 116, 118; 116, 119; 116, 120; 116, 121; 117, comment; 118, comment; 119, comment; 120, identifier:name; 121, attribute; 121, 122; 121, 123; 122, identifier:param; 123, identifier:validated_value; 124, list:[ param.default and param.validated_value(param.default) ]; 124, 125; 125, boolean_operator:and; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:param; 128, identifier:default; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:param; 132, identifier:validated_value; 133, argument_list; 133, 134; 134, attribute; 134, 135; 134, 136; 135, identifier:param; 136, identifier:default; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 142; 139, subscript; 139, 140; 139, 141; 140, identifier:params; 141, identifier:name; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:param; 145, identifier:container; 146, argument_list; 146, 147; 147, identifier:values; 148, else_clause; 148, 149; 148, 150; 148, 151; 148, 152; 148, 153; 149, comment; 150, comment; 151, comment; 152, comment; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 159; 156, subscript; 156, 157; 156, 158; 157, identifier:params; 158, identifier:name; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:param; 162, identifier:validated_value; 163, argument_list; 163, 164; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:req; 167, identifier:get_param; 168, argument_list; 168, 169; 168, 170; 169, identifier:name; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:default; 172, attribute; 172, 173; 172, 174; 173, identifier:param; 174, identifier:default; 175, except_clause; 175, 176; 175, 180; 175, 181; 175, 182; 176, as_pattern; 176, 177; 176, 178; 177, identifier:ValidationError; 178, as_pattern_target; 178, 179; 179, identifier:err; 180, comment; 181, comment; 182, block; 182, 183; 183, raise_statement; 183, 184; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:err; 187, identifier:as_invalid_param; 188, argument_list; 188, 189; 189, identifier:name; 190, except_clause; 190, 191; 190, 195; 190, 196; 191, as_pattern; 191, 192; 191, 193; 192, identifier:ValueError; 193, as_pattern_target; 193, 194; 194, identifier:err; 195, comment; 196, block; 196, 197; 197, raise_statement; 197, 198; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:errors; 201, identifier:HTTPInvalidParam; 202, argument_list; 202, 203; 202, 207; 203, call; 203, 204; 203, 205; 204, identifier:str; 205, argument_list; 205, 206; 206, identifier:err; 207, identifier:name; 208, return_statement; 208, 209; 209, identifier:params
def require_params(self, req): """Require all defined parameters from request query string. Raises ``falcon.errors.HTTPMissingParam`` exception if any of required parameters is missing and ``falcon.errors.HTTPInvalidParam`` if any of parameters could not be understood (wrong format). Args: req (falcon.Request): request object """ params = {} for name, param in self.params.items(): if name not in req.params and param.required: # we could simply raise with this single param or use get_param # with required=True parameter but for client convenience # we prefer to list all missing params that are required missing = set( p for p in self.params if self.params[p].required ) - set(req.params.keys()) raise errors.HTTPMissingParam(", ".join(missing)) elif name in req.params or param.default: # Note: lack of key in req.params means it was not specified # so unless there is default value it will not be included in # output params dict. # This way we have explicit information that param was # not specified. Using None would not be as good because param # class can also return None from `.value()` method as a valid # translated value. try: if param.many: # params with "many" enabled need special care values = req.get_param_as_list( # note: falcon allows to pass value handler using # `transform` param so we do not need to # iterate through list manually name, param.validated_value ) or [ param.default and param.validated_value(param.default) ] params[name] = param.container(values) else: # note that if many==False and query parameter # occurs multiple times in qs then it is # **unspecified** which one will be used. See: # http://falcon.readthedocs.org/en/latest/api/request_and_response.html#falcon.Request.get_param # noqa params[name] = param.validated_value( req.get_param(name, default=param.default) ) except ValidationError as err: # ValidationError allows to easily translate itself to # to falcon's HTTPInvalidParam (Bad Request HTTP response) raise err.as_invalid_param(name) except ValueError as err: # Other parsing issues are expected to raise ValueError raise errors.HTTPInvalidParam(str(err), name) return params
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:celf; 5, identifier:s; 6, block; 6, 7; 6, 9; 6, 283; 6, 284; 6, 285; 6, 294; 6, 301; 7, expression_statement; 7, 8; 8, string:"generates an Introspection tree from the given XML string description."; 9, function_definition; 9, 10; 9, 11; 9, 15; 10, function_name:from_string_elts; 11, parameters; 11, 12; 11, 13; 11, 14; 12, identifier:celf; 13, identifier:attrs; 14, identifier:tree; 15, block; 15, 16; 15, 30; 15, 59; 15, 63; 15, 210; 15, 211; 15, 275; 15, 276; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:elts; 19, call; 19, 20; 19, 21; 20, identifier:dict; 21, generator_expression; 21, 22; 21, 27; 22, tuple; 22, 23; 22, 24; 23, identifier:k; 24, subscript; 24, 25; 24, 26; 25, identifier:attrs; 26, identifier:k; 27, for_in_clause; 27, 28; 27, 29; 28, identifier:k; 29, identifier:attrs; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:child_tags; 33, call; 33, 34; 33, 35; 33, 36; 34, identifier:dict; 35, line_continuation:\; 36, generator_expression; 36, 37; 36, 42; 37, tuple; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:childclass; 40, identifier:tag_name; 41, identifier:childclass; 42, for_in_clause; 42, 43; 42, 44; 43, identifier:childclass; 44, binary_operator:+; 44, 45; 44, 55; 45, call; 45, 46; 45, 47; 46, identifier:tuple; 47, argument_list; 47, 48; 48, call; 48, 49; 48, 54; 49, attribute; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:celf; 52, identifier:tag_elts; 53, identifier:values; 54, argument_list; 55, tuple; 55, 56; 56, attribute; 56, 57; 56, 58; 57, identifier:Introspection; 58, identifier:Annotation; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:children; 62, list:[]; 63, for_statement; 63, 64; 63, 65; 63, 66; 64, identifier:child; 65, identifier:tree; 66, block; 66, 67; 66, 83; 66, 84; 66, 92; 66, 96; 66, 161; 66, 162; 66, 197; 66, 198; 67, if_statement; 67, 68; 67, 73; 68, comparison_operator:not; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:child; 71, identifier:tag; 72, identifier:child_tags; 73, block; 73, 74; 74, raise_statement; 74, 75; 75, call; 75, 76; 75, 77; 76, identifier:KeyError; 77, argument_list; 77, 78; 78, binary_operator:%; 78, 79; 78, 80; 79, string:"unrecognized tag %s"; 80, attribute; 80, 81; 80, 82; 81, identifier:child; 82, identifier:tag; 83, comment; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:childclass; 87, subscript; 87, 88; 87, 89; 88, identifier:child_tags; 89, attribute; 89, 90; 89, 91; 90, identifier:child; 91, identifier:tag; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:childattrs; 95, dictionary; 96, for_statement; 96, 97; 96, 98; 96, 101; 97, identifier:attrname; 98, attribute; 98, 99; 98, 100; 99, identifier:childclass; 100, identifier:tag_attrs; 101, block; 101, 102; 101, 160; 102, if_statement; 102, 103; 102, 114; 102, 129; 103, boolean_operator:and; 103, 104; 103, 109; 104, call; 104, 105; 104, 106; 105, identifier:hasattr; 106, argument_list; 106, 107; 106, 108; 107, identifier:childclass; 108, string:"tag_attrs_optional"; 109, comparison_operator:in; 109, 110; 109, 111; 110, identifier:attrname; 111, attribute; 111, 112; 111, 113; 112, identifier:childclass; 113, identifier:tag_attrs_optional; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 120; 117, subscript; 117, 118; 117, 119; 118, identifier:childattrs; 119, identifier:attrname; 120, call; 120, 121; 120, 126; 121, attribute; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:child; 124, identifier:attrib; 125, identifier:get; 126, argument_list; 126, 127; 126, 128; 127, identifier:attrname; 128, None; 129, else_clause; 129, 130; 130, block; 130, 131; 130, 149; 130, 150; 131, if_statement; 131, 132; 131, 137; 132, comparison_operator:not; 132, 133; 132, 134; 133, identifier:attrname; 134, attribute; 134, 135; 134, 136; 135, identifier:child; 136, identifier:attrib; 137, block; 137, 138; 138, raise_statement; 138, 139; 139, call; 139, 140; 139, 141; 140, identifier:ValueError; 141, argument_list; 141, 142; 142, binary_operator:%; 142, 143; 142, 144; 143, string:"missing %s attribute for %s tag"; 144, tuple; 144, 145; 144, 146; 145, identifier:attrname; 146, attribute; 146, 147; 146, 148; 147, identifier:child; 148, identifier:tag; 149, comment; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 155; 152, subscript; 152, 153; 152, 154; 153, identifier:childattrs; 154, identifier:attrname; 155, subscript; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:child; 158, identifier:attrib; 159, identifier:attrname; 160, comment; 161, comment; 162, if_statement; 162, 163; 162, 168; 163, call; 163, 164; 163, 165; 164, identifier:hasattr; 165, argument_list; 165, 166; 165, 167; 166, identifier:childclass; 167, string:"attr_convert"; 168, block; 168, 169; 168, 196; 169, for_statement; 169, 170; 169, 171; 169, 174; 170, identifier:attr; 171, attribute; 171, 172; 171, 173; 172, identifier:childclass; 173, identifier:attr_convert; 174, block; 174, 175; 174, 195; 175, if_statement; 175, 176; 175, 179; 176, comparison_operator:in; 176, 177; 176, 178; 177, identifier:attr; 178, identifier:childattrs; 179, block; 179, 180; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 185; 182, subscript; 182, 183; 182, 184; 183, identifier:childattrs; 184, identifier:attr; 185, call; 185, 186; 185, 191; 186, subscript; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:childclass; 189, identifier:attr_convert; 190, identifier:attr; 191, argument_list; 191, 192; 192, subscript; 192, 193; 192, 194; 193, identifier:childattrs; 194, identifier:attr; 195, comment; 196, comment; 197, comment; 198, expression_statement; 198, 199; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:children; 202, identifier:append; 203, argument_list; 203, 204; 204, call; 204, 205; 204, 206; 205, identifier:from_string_elts; 206, argument_list; 206, 207; 206, 208; 206, 209; 207, identifier:childclass; 208, identifier:childattrs; 209, identifier:child; 210, comment; 211, for_statement; 211, 212; 211, 215; 211, 240; 212, pattern_list; 212, 213; 212, 214; 213, identifier:child_tag; 214, identifier:childclass; 215, binary_operator:+; 215, 216; 215, 226; 216, call; 216, 217; 216, 218; 217, identifier:tuple; 218, argument_list; 218, 219; 219, call; 219, 220; 219, 225; 220, attribute; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:celf; 223, identifier:tag_elts; 224, identifier:items; 225, argument_list; 226, subscript; 226, 227; 226, 235; 227, tuple; 227, 228; 227, 229; 228, tuple; 229, tuple; 229, 230; 230, tuple; 230, 231; 230, 232; 231, string:"annotations"; 232, attribute; 232, 233; 232, 234; 233, identifier:Introspection; 234, identifier:Annotation; 235, comparison_operator:!=; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:tree; 238, identifier:tag; 239, string:"annotation"; 240, block; 240, 241; 240, 274; 241, for_statement; 241, 242; 241, 243; 241, 244; 242, identifier:child; 243, identifier:children; 244, block; 244, 245; 244, 273; 245, if_statement; 245, 246; 245, 251; 246, call; 246, 247; 246, 248; 247, identifier:isinstance; 248, argument_list; 248, 249; 248, 250; 249, identifier:child; 250, identifier:childclass; 251, block; 251, 252; 251, 263; 251, 264; 252, if_statement; 252, 253; 252, 256; 253, comparison_operator:not; 253, 254; 253, 255; 254, identifier:child_tag; 255, identifier:elts; 256, block; 256, 257; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 262; 259, subscript; 259, 260; 259, 261; 260, identifier:elts; 261, identifier:child_tag; 262, list:[]; 263, comment; 264, expression_statement; 264, 265; 265, call; 265, 266; 265, 271; 266, attribute; 266, 267; 266, 270; 267, subscript; 267, 268; 267, 269; 268, identifier:elts; 269, identifier:child_tag; 270, identifier:append; 271, argument_list; 271, 272; 272, identifier:child; 273, comment; 274, comment; 275, comment; 276, return_statement; 276, 277; 276, 278; 277, line_continuation:\; 278, call; 278, 279; 278, 280; 279, identifier:celf; 280, argument_list; 280, 281; 281, dictionary_splat; 281, 282; 282, identifier:elts; 283, comment; 284, comment; 285, expression_statement; 285, 286; 286, assignment; 286, 287; 286, 288; 287, identifier:tree; 288, call; 288, 289; 288, 292; 289, attribute; 289, 290; 289, 291; 290, identifier:XMLElementTree; 291, identifier:fromstring; 292, argument_list; 292, 293; 293, identifier:s; 294, assert_statement; 294, 295; 294, 300; 295, comparison_operator:==; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:tree; 298, identifier:tag; 299, string:"node"; 300, string:"root of introspection tree must be <node> tag"; 301, return_statement; 301, 302; 301, 303; 302, line_continuation:\; 303, call; 303, 304; 303, 305; 304, identifier:from_string_elts; 305, argument_list; 305, 306; 305, 307; 305, 308; 306, identifier:Introspection; 307, dictionary; 308, identifier:tree
def parse(celf, s) : "generates an Introspection tree from the given XML string description." def from_string_elts(celf, attrs, tree) : elts = dict((k, attrs[k]) for k in attrs) child_tags = dict \ ( (childclass.tag_name, childclass) for childclass in tuple(celf.tag_elts.values()) + (Introspection.Annotation,) ) children = [] for child in tree : if child.tag not in child_tags : raise KeyError("unrecognized tag %s" % child.tag) #end if childclass = child_tags[child.tag] childattrs = {} for attrname in childclass.tag_attrs : if hasattr(childclass, "tag_attrs_optional") and attrname in childclass.tag_attrs_optional : childattrs[attrname] = child.attrib.get(attrname, None) else : if attrname not in child.attrib : raise ValueError("missing %s attribute for %s tag" % (attrname, child.tag)) #end if childattrs[attrname] = child.attrib[attrname] #end if #end for if hasattr(childclass, "attr_convert") : for attr in childclass.attr_convert : if attr in childattrs : childattrs[attr] = childclass.attr_convert[attr](childattrs[attr]) #end if #end for #end if children.append(from_string_elts(childclass, childattrs, child)) #end for for child_tag, childclass in tuple(celf.tag_elts.items()) + ((), (("annotations", Introspection.Annotation),))[tree.tag != "annotation"] : for child in children : if isinstance(child, childclass) : if child_tag not in elts : elts[child_tag] = [] #end if elts[child_tag].append(child) #end if #end for #end for return \ celf(**elts) #end from_string_elts #begin parse tree = XMLElementTree.fromstring(s) assert tree.tag == "node", "root of introspection tree must be <node> tag" return \ from_string_elts(Introspection, {}, tree)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:unparse; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:indent_step; 7, integer:4; 8, default_parameter; 8, 9; 8, 10; 9, identifier:max_linelen; 10, integer:72; 11, block; 11, 12; 11, 14; 11, 22; 11, 362; 11, 363; 11, 364; 11, 373; 11, 380; 11, 401; 11, 402; 11, 409; 11, 421; 11, 422; 11, 434; 11, 435; 11, 442; 12, expression_statement; 12, 13; 13, string:"returns an XML string description of this Introspection tree."; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:out; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:io; 20, identifier:StringIO; 21, argument_list; 22, function_definition; 22, 23; 22, 24; 22, 27; 23, function_name:to_string; 24, parameters; 24, 25; 24, 26; 25, identifier:obj; 26, identifier:indent; 27, block; 27, 28; 27, 34; 27, 38; 27, 127; 27, 128; 27, 172; 27, 185; 27, 289; 27, 290; 27, 301; 27, 302; 27, 309; 27, 361; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:tag_name; 31, attribute; 31, 32; 31, 33; 32, identifier:obj; 33, identifier:tag_name; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:attrs; 37, list:[]; 38, for_statement; 38, 39; 38, 40; 38, 43; 39, identifier:attrname; 40, attribute; 40, 41; 40, 42; 41, identifier:obj; 42, identifier:tag_attrs; 43, block; 43, 44; 43, 52; 43, 126; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:attr; 47, call; 47, 48; 47, 49; 48, identifier:getattr; 49, argument_list; 49, 50; 49, 51; 50, identifier:obj; 51, identifier:attrname; 52, if_statement; 52, 53; 52, 56; 53, comparison_operator:!=; 53, 54; 53, 55; 54, identifier:attr; 55, None; 56, block; 56, 57; 56, 111; 56, 112; 57, if_statement; 57, 58; 57, 65; 57, 72; 57, 86; 58, call; 58, 59; 58, 60; 59, identifier:isinstance; 60, argument_list; 60, 61; 60, 62; 61, identifier:attr; 62, attribute; 62, 63; 62, 64; 63, identifier:enum; 64, identifier:Enum; 65, block; 65, 66; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:attr; 69, attribute; 69, 70; 69, 71; 70, identifier:attr; 71, identifier:value; 72, elif_clause; 72, 73; 72, 78; 73, call; 73, 74; 73, 75; 74, identifier:isinstance; 75, argument_list; 75, 76; 75, 77; 76, identifier:attr; 77, identifier:Type; 78, block; 78, 79; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:attr; 82, call; 82, 83; 82, 84; 83, identifier:unparse_signature; 84, argument_list; 84, 85; 85, identifier:attr; 86, elif_clause; 86, 87; 86, 93; 87, not_operator; 87, 88; 88, call; 88, 89; 88, 90; 89, identifier:isinstance; 90, argument_list; 90, 91; 90, 92; 91, identifier:attr; 92, identifier:str; 93, block; 93, 94; 94, raise_statement; 94, 95; 95, call; 95, 96; 95, 97; 96, identifier:TypeError; 97, argument_list; 97, 98; 98, binary_operator:%; 98, 99; 98, 100; 99, string:"unexpected attribute type %s for %s"; 100, tuple; 100, 101; 100, 107; 101, attribute; 101, 102; 101, 106; 102, call; 102, 103; 102, 104; 103, identifier:type; 104, argument_list; 104, 105; 105, identifier:attr; 106, identifier:__name__; 107, call; 107, 108; 107, 109; 108, identifier:repr; 109, argument_list; 109, 110; 110, identifier:attr; 111, comment; 112, expression_statement; 112, 113; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:attrs; 116, identifier:append; 117, argument_list; 117, 118; 118, binary_operator:%; 118, 119; 118, 120; 119, string:"%s=%s"; 120, tuple; 120, 121; 120, 122; 121, identifier:attrname; 122, call; 122, 123; 122, 124; 123, identifier:quote_xml_attr; 124, argument_list; 124, 125; 125, identifier:attr; 126, comment; 127, comment; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 129, 132; 130, identifier:has_elts; 131, line_continuation:\; 132, parenthesized_expression; 132, 133; 133, comparison_operator:!=; 133, 134; 133, 171; 134, call; 134, 135; 134, 136; 135, identifier:sum; 136, generator_expression; 136, 137; 136, 145; 137, call; 137, 138; 137, 139; 138, identifier:len; 139, argument_list; 139, 140; 140, call; 140, 141; 140, 142; 141, identifier:getattr; 142, argument_list; 142, 143; 142, 144; 143, identifier:obj; 144, identifier:attrname; 145, for_in_clause; 145, 146; 145, 147; 146, identifier:attrname; 147, binary_operator:+; 147, 148; 147, 158; 148, call; 148, 149; 148, 150; 149, identifier:tuple; 150, argument_list; 150, 151; 151, call; 151, 152; 151, 157; 152, attribute; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:obj; 155, identifier:tag_elts; 156, identifier:keys; 157, argument_list; 158, subscript; 158, 159; 158, 163; 159, tuple; 159, 160; 159, 161; 160, tuple; 161, tuple; 161, 162; 162, string:"annotations"; 163, not_operator; 163, 164; 164, call; 164, 165; 164, 166; 165, identifier:isinstance; 166, argument_list; 166, 167; 166, 168; 167, identifier:obj; 168, attribute; 168, 169; 168, 170; 169, identifier:Introspection; 170, identifier:Annotation; 171, integer:0; 172, expression_statement; 172, 173; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:out; 176, identifier:write; 177, argument_list; 177, 178; 178, binary_operator:+; 178, 179; 178, 184; 179, binary_operator:+; 179, 180; 179, 183; 180, binary_operator:*; 180, 181; 180, 182; 181, string:" "; 182, identifier:indent; 183, string:"<"; 184, identifier:tag_name; 185, if_statement; 185, 186; 185, 220; 185, 268; 186, parenthesized_expression; 186, 187; 187, boolean_operator:and; 187, 188; 187, 191; 188, comparison_operator:!=; 188, 189; 188, 190; 189, identifier:max_linelen; 190, None; 191, comparison_operator:>; 191, 192; 191, 219; 192, binary_operator:+; 192, 193; 192, 215; 193, binary_operator:+; 193, 194; 193, 214; 194, binary_operator:+; 194, 195; 194, 201; 195, binary_operator:+; 195, 196; 195, 197; 196, identifier:indent; 197, call; 197, 198; 197, 199; 198, identifier:len; 199, argument_list; 199, 200; 200, identifier:tag_name; 201, call; 201, 202; 201, 203; 202, identifier:sum; 203, generator_expression; 203, 204; 203, 211; 204, parenthesized_expression; 204, 205; 205, binary_operator:+; 205, 206; 205, 210; 206, call; 206, 207; 206, 208; 207, identifier:len; 208, argument_list; 208, 209; 209, identifier:s; 210, integer:1; 211, for_in_clause; 211, 212; 211, 213; 212, identifier:s; 213, identifier:attrs; 214, integer:2; 215, call; 215, 216; 215, 217; 216, identifier:int; 217, argument_list; 217, 218; 218, identifier:has_elts; 219, identifier:max_linelen; 220, block; 220, 221; 220, 228; 220, 258; 220, 259; 221, expression_statement; 221, 222; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:out; 225, identifier:write; 226, argument_list; 226, 227; 227, string:"\n"; 228, for_statement; 228, 229; 228, 230; 228, 231; 229, identifier:attr; 230, identifier:attrs; 231, block; 231, 232; 231, 244; 231, 251; 232, expression_statement; 232, 233; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:out; 236, identifier:write; 237, argument_list; 237, 238; 238, binary_operator:*; 238, 239; 238, 240; 239, string:" "; 240, parenthesized_expression; 240, 241; 241, binary_operator:+; 241, 242; 241, 243; 242, identifier:indent; 243, identifier:indent_step; 244, expression_statement; 244, 245; 245, call; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:out; 248, identifier:write; 249, argument_list; 249, 250; 250, identifier:attr; 251, expression_statement; 251, 252; 252, call; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:out; 255, identifier:write; 256, argument_list; 256, 257; 257, string:"\n"; 258, comment; 259, expression_statement; 259, 260; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:out; 263, identifier:write; 264, argument_list; 264, 265; 265, binary_operator:*; 265, 266; 265, 267; 266, string:" "; 267, identifier:indent; 268, else_clause; 268, 269; 269, block; 269, 270; 269, 288; 270, for_statement; 270, 271; 270, 272; 270, 273; 271, identifier:attr; 272, identifier:attrs; 273, block; 273, 274; 273, 281; 274, expression_statement; 274, 275; 275, call; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:out; 278, identifier:write; 279, argument_list; 279, 280; 280, string:" "; 281, expression_statement; 281, 282; 282, call; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, identifier:out; 285, identifier:write; 286, argument_list; 286, 287; 287, identifier:attr; 288, comment; 289, comment; 290, if_statement; 290, 291; 290, 293; 291, not_operator; 291, 292; 292, identifier:has_elts; 293, block; 293, 294; 294, expression_statement; 294, 295; 295, call; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:out; 298, identifier:write; 299, argument_list; 299, 300; 300, string:"/"; 301, comment; 302, expression_statement; 302, 303; 303, call; 303, 304; 303, 307; 304, attribute; 304, 305; 304, 306; 305, identifier:out; 306, identifier:write; 307, argument_list; 307, 308; 308, string:">\n"; 309, if_statement; 309, 310; 309, 311; 310, identifier:has_elts; 311, block; 311, 312; 311, 345; 311, 346; 312, for_statement; 312, 313; 312, 314; 312, 327; 313, identifier:attrname; 314, binary_operator:+; 314, 315; 314, 325; 315, call; 315, 316; 315, 317; 316, identifier:sorted; 317, argument_list; 317, 318; 318, call; 318, 319; 318, 324; 319, attribute; 319, 320; 319, 323; 320, attribute; 320, 321; 320, 322; 321, identifier:obj; 322, identifier:tag_elts; 323, identifier:keys; 324, argument_list; 325, list:["annotations"]; 325, 326; 326, string:"annotations"; 327, block; 327, 328; 327, 344; 328, for_statement; 328, 329; 328, 330; 328, 335; 329, identifier:elt; 330, call; 330, 331; 330, 332; 331, identifier:getattr; 332, argument_list; 332, 333; 332, 334; 333, identifier:obj; 334, identifier:attrname; 335, block; 335, 336; 336, expression_statement; 336, 337; 337, call; 337, 338; 337, 339; 338, identifier:to_string; 339, argument_list; 339, 340; 339, 341; 340, identifier:elt; 341, binary_operator:+; 341, 342; 341, 343; 342, identifier:indent; 343, identifier:indent_step; 344, comment; 345, comment; 346, expression_statement; 346, 347; 347, call; 347, 348; 347, 351; 348, attribute; 348, 349; 348, 350; 349, identifier:out; 350, identifier:write; 351, argument_list; 351, 352; 352, binary_operator:+; 352, 353; 352, 360; 353, binary_operator:+; 353, 354; 353, 359; 354, binary_operator:+; 354, 355; 354, 358; 355, binary_operator:*; 355, 356; 355, 357; 356, string:" "; 357, identifier:indent; 358, string:"</"; 359, identifier:tag_name; 360, string:">\n"; 361, comment; 362, comment; 363, comment; 364, expression_statement; 364, 365; 365, call; 365, 366; 365, 369; 366, attribute; 366, 367; 366, 368; 367, identifier:out; 368, identifier:write; 369, argument_list; 369, 370; 370, attribute; 370, 371; 370, 372; 371, identifier:DBUS; 372, identifier:INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE; 373, expression_statement; 373, 374; 374, call; 374, 375; 374, 378; 375, attribute; 375, 376; 375, 377; 376, identifier:out; 377, identifier:write; 378, argument_list; 378, 379; 379, string:"<node"; 380, if_statement; 380, 381; 380, 386; 381, comparison_operator:!=; 381, 382; 381, 385; 382, attribute; 382, 383; 382, 384; 383, identifier:self; 384, identifier:name; 385, None; 386, block; 386, 387; 387, expression_statement; 387, 388; 388, call; 388, 389; 388, 392; 389, attribute; 389, 390; 389, 391; 390, identifier:out; 391, identifier:write; 392, argument_list; 392, 393; 393, binary_operator:%; 393, 394; 393, 395; 394, string:" name=%s"; 395, call; 395, 396; 395, 397; 396, identifier:quote_xml_attr; 397, argument_list; 397, 398; 398, attribute; 398, 399; 398, 400; 399, identifier:self; 400, identifier:name; 401, comment; 402, expression_statement; 402, 403; 403, call; 403, 404; 403, 407; 404, attribute; 404, 405; 404, 406; 405, identifier:out; 406, identifier:write; 407, argument_list; 407, 408; 408, string:">\n"; 409, for_statement; 409, 410; 409, 411; 409, 414; 410, identifier:elt; 411, attribute; 411, 412; 411, 413; 412, identifier:self; 413, identifier:interfaces; 414, block; 414, 415; 415, expression_statement; 415, 416; 416, call; 416, 417; 416, 418; 417, identifier:to_string; 418, argument_list; 418, 419; 418, 420; 419, identifier:elt; 420, identifier:indent_step; 421, comment; 422, for_statement; 422, 423; 422, 424; 422, 427; 423, identifier:elt; 424, attribute; 424, 425; 424, 426; 425, identifier:self; 426, identifier:nodes; 427, block; 427, 428; 428, expression_statement; 428, 429; 429, call; 429, 430; 429, 431; 430, identifier:to_string; 431, argument_list; 431, 432; 431, 433; 432, identifier:elt; 433, identifier:indent_step; 434, comment; 435, expression_statement; 435, 436; 436, call; 436, 437; 436, 440; 437, attribute; 437, 438; 437, 439; 438, identifier:out; 439, identifier:write; 440, argument_list; 440, 441; 441, string:"</node>\n"; 442, return_statement; 442, 443; 442, 444; 443, line_continuation:\; 444, call; 444, 445; 444, 448; 445, attribute; 445, 446; 445, 447; 446, identifier:out; 447, identifier:getvalue; 448, argument_list
def unparse(self, indent_step = 4, max_linelen = 72) : "returns an XML string description of this Introspection tree." out = io.StringIO() def to_string(obj, indent) : tag_name = obj.tag_name attrs = [] for attrname in obj.tag_attrs : attr = getattr(obj, attrname) if attr != None : if isinstance(attr, enum.Enum) : attr = attr.value elif isinstance(attr, Type) : attr = unparse_signature(attr) elif not isinstance(attr, str) : raise TypeError("unexpected attribute type %s for %s" % (type(attr).__name__, repr(attr))) #end if attrs.append("%s=%s" % (attrname, quote_xml_attr(attr))) #end if #end for has_elts = \ ( sum ( len(getattr(obj, attrname)) for attrname in tuple(obj.tag_elts.keys()) + ((), ("annotations",)) [not isinstance(obj, Introspection.Annotation)] ) != 0 ) out.write(" " * indent + "<" + tag_name) if ( max_linelen != None and indent + len(tag_name) + sum((len(s) + 1) for s in attrs) + 2 + int(has_elts) > max_linelen ) : out.write("\n") for attr in attrs : out.write(" " * (indent + indent_step)) out.write(attr) out.write("\n") #end for out.write(" " * indent) else : for attr in attrs : out.write(" ") out.write(attr) #end for #end if if not has_elts : out.write("/") #end if out.write(">\n") if has_elts : for attrname in sorted(obj.tag_elts.keys()) + ["annotations"] : for elt in getattr(obj, attrname) : to_string(elt, indent + indent_step) #end for #end for out.write(" " * indent + "</" + tag_name + ">\n") #end if #end to_string #begin unparse out.write(DBUS.INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE) out.write("<node") if self.name != None : out.write(" name=%s" % quote_xml_attr(self.name)) #end if out.write(">\n") for elt in self.interfaces : to_string(elt, indent_step) #end for for elt in self.nodes : to_string(elt, indent_step) #end for out.write("</node>\n") return \ out.getvalue()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:AddMethod; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 4, identifier:self; 5, identifier:interface; 6, identifier:name; 7, identifier:in_sig; 8, identifier:out_sig; 9, identifier:code; 10, block; 10, 11; 10, 13; 10, 23; 10, 35; 10, 36; 10, 37; 10, 38; 10, 39; 10, 62; 10, 63; 10, 64; 10, 81; 10, 90; 10, 96; 10, 114; 10, 115; 10, 116; 10, 132; 11, expression_statement; 11, 12; 12, string:'''Add a method to this object interface: D-Bus interface to add this to. For convenience you can specify '' here to add the method to the object's main interface (as specified on construction). name: Name of the method in_sig: Signature of input arguments; for example "ias" for a method that takes an int32 and a string array as arguments; see http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-signatures out_sig: Signature of output arguments; for example "s" for a method that returns a string; use '' for methods that do not return anything. code: Python 3 code to run in the method call; you have access to the arguments through the "args" list, and can set the return value by assigning a value to the "ret" variable. You can also read the global "objects" variable, which is a dictionary mapping object paths to DBusMockObject instances. For keeping state across method calls, you are free to use normal Python members of the "self" object, which will be persistent for the whole mock's life time. E. g. you can have a method with "self.my_state = True", and another method that returns it with "ret = self.my_state". When specifying '', the method will not do anything (except logging) and return None. '''; 13, if_statement; 13, 14; 13, 16; 14, not_operator; 14, 15; 15, identifier:interface; 16, block; 16, 17; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:interface; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:interface; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:n_args; 26, call; 26, 27; 26, 28; 27, identifier:len; 28, argument_list; 28, 29; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:dbus; 32, identifier:Signature; 33, argument_list; 33, 34; 34, identifier:in_sig; 35, comment; 36, comment; 37, comment; 38, comment; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:method; 42, lambda; 42, 43; 42, 49; 43, lambda_parameters; 43, 44; 43, 45; 43, 47; 44, identifier:self; 45, list_splat_pattern; 45, 46; 46, identifier:args; 47, dictionary_splat_pattern; 47, 48; 48, identifier:kwargs; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:DBusMockObject; 52, identifier:mock_method; 53, argument_list; 53, 54; 53, 55; 53, 56; 53, 57; 53, 58; 53, 60; 54, identifier:self; 55, identifier:interface; 56, identifier:name; 57, identifier:in_sig; 58, list_splat; 58, 59; 59, identifier:args; 60, dictionary_splat; 60, 61; 61, identifier:kwargs; 62, comment; 63, comment; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:dbus_method; 67, call; 67, 68; 67, 79; 68, call; 68, 69; 68, 74; 69, attribute; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:dbus; 72, identifier:service; 73, identifier:method; 74, argument_list; 74, 75; 74, 76; 75, identifier:interface; 76, keyword_argument; 76, 77; 76, 78; 77, identifier:out_signature; 78, identifier:out_sig; 79, argument_list; 79, 80; 80, identifier:method; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:dbus_method; 85, identifier:__name__; 86, call; 86, 87; 86, 88; 87, identifier:str; 88, argument_list; 88, 89; 89, identifier:name; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:dbus_method; 94, identifier:_dbus_in_signature; 95, identifier:in_sig; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:dbus_method; 100, identifier:_dbus_args; 101, list_comprehension; 101, 102; 101, 105; 102, binary_operator:%; 102, 103; 102, 104; 103, string:'arg%i'; 104, identifier:i; 105, for_in_clause; 105, 106; 105, 107; 106, identifier:i; 107, call; 107, 108; 107, 109; 108, identifier:range; 109, argument_list; 109, 110; 109, 111; 110, integer:1; 111, binary_operator:+; 111, 112; 111, 113; 112, identifier:n_args; 113, integer:1; 114, comment; 115, comment; 116, if_statement; 116, 117; 116, 122; 117, comparison_operator:==; 117, 118; 117, 119; 118, identifier:interface; 119, attribute; 119, 120; 119, 121; 120, identifier:self; 121, identifier:interface; 122, block; 122, 123; 123, expression_statement; 123, 124; 124, call; 124, 125; 124, 126; 125, identifier:setattr; 126, argument_list; 126, 127; 126, 130; 126, 131; 127, attribute; 127, 128; 127, 129; 128, identifier:self; 129, identifier:__class__; 130, identifier:name; 131, identifier:dbus_method; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 148; 134, subscript; 134, 135; 134, 144; 135, call; 135, 136; 135, 141; 136, attribute; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:self; 139, identifier:methods; 140, identifier:setdefault; 141, argument_list; 141, 142; 141, 143; 142, identifier:interface; 143, dictionary; 144, call; 144, 145; 144, 146; 145, identifier:str; 146, argument_list; 146, 147; 147, identifier:name; 148, tuple; 148, 149; 148, 150; 148, 151; 148, 152; 149, identifier:in_sig; 150, identifier:out_sig; 151, identifier:code; 152, identifier:dbus_method
def AddMethod(self, interface, name, in_sig, out_sig, code): '''Add a method to this object interface: D-Bus interface to add this to. For convenience you can specify '' here to add the method to the object's main interface (as specified on construction). name: Name of the method in_sig: Signature of input arguments; for example "ias" for a method that takes an int32 and a string array as arguments; see http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-signatures out_sig: Signature of output arguments; for example "s" for a method that returns a string; use '' for methods that do not return anything. code: Python 3 code to run in the method call; you have access to the arguments through the "args" list, and can set the return value by assigning a value to the "ret" variable. You can also read the global "objects" variable, which is a dictionary mapping object paths to DBusMockObject instances. For keeping state across method calls, you are free to use normal Python members of the "self" object, which will be persistent for the whole mock's life time. E. g. you can have a method with "self.my_state = True", and another method that returns it with "ret = self.my_state". When specifying '', the method will not do anything (except logging) and return None. ''' if not interface: interface = self.interface n_args = len(dbus.Signature(in_sig)) # we need to have separate methods for dbus-python, so clone # mock_method(); using message_keyword with this dynamic approach fails # because inspect cannot handle those, so pass on interface and method # name as first positional arguments method = lambda self, *args, **kwargs: DBusMockObject.mock_method( self, interface, name, in_sig, *args, **kwargs) # we cannot specify in_signature here, as that trips over a consistency # check in dbus-python; we need to set it manually instead dbus_method = dbus.service.method(interface, out_signature=out_sig)(method) dbus_method.__name__ = str(name) dbus_method._dbus_in_signature = in_sig dbus_method._dbus_args = ['arg%i' % i for i in range(1, n_args + 1)] # for convenience, add mocked methods on the primary interface as # callable methods if interface == self.interface: setattr(self.__class__, name, dbus_method) self.methods.setdefault(interface, {})[str(name)] = (in_sig, out_sig, code, dbus_method)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:AddTemplate; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:template; 6, identifier:parameters; 7, block; 7, 8; 7, 10; 7, 44; 7, 45; 7, 62; 7, 63; 7, 137; 7, 146; 7, 154; 7, 155; 7, 156; 7, 162; 8, expression_statement; 8, 9; 9, string:'''Load a template into the mock. python-dbusmock ships a set of standard mocks for common system services such as UPower and NetworkManager. With these the actual tests become a lot simpler, as they only have to set up the particular properties for the tests, and not the skeleton of common properties, interfaces, and methods. template: Name of the template to load or the full path to a *.py file for custom templates. See "pydoc dbusmock.templates" for a list of available templates from python-dbusmock package, and "pydoc dbusmock.templates.NAME" for documentation about template NAME. parameters: A parameter (string) → value (variant) map, for parameterizing templates. Each template can define their own, see documentation of that particular template for details. '''; 10, try_statement; 10, 11; 10, 19; 11, block; 11, 12; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:module; 15, call; 15, 16; 15, 17; 16, identifier:load_module; 17, argument_list; 17, 18; 18, identifier:template; 19, except_clause; 19, 20; 19, 24; 20, as_pattern; 20, 21; 20, 22; 21, identifier:ImportError; 22, as_pattern_target; 22, 23; 23, identifier:e; 24, block; 24, 25; 25, raise_statement; 25, 26; 26, call; 26, 27; 26, 32; 27, attribute; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:dbus; 30, identifier:exceptions; 31, identifier:DBusException; 32, argument_list; 32, 33; 32, 41; 33, binary_operator:%; 33, 34; 33, 35; 34, string:'Cannot add template %s: %s'; 35, tuple; 35, 36; 35, 37; 36, identifier:template; 37, call; 37, 38; 37, 39; 38, identifier:str; 39, argument_list; 39, 40; 40, identifier:e; 41, keyword_argument; 41, 42; 41, 43; 42, identifier:name; 43, string:'org.freedesktop.DBus.Mock.TemplateError'; 44, comment; 45, if_statement; 45, 46; 45, 55; 46, boolean_operator:and; 46, 47; 46, 52; 47, call; 47, 48; 47, 49; 48, identifier:hasattr; 49, argument_list; 49, 50; 49, 51; 50, identifier:module; 51, string:'IS_OBJECT_MANAGER'; 52, attribute; 52, 53; 52, 54; 53, identifier:module; 54, identifier:IS_OBJECT_MANAGER; 55, block; 55, 56; 56, expression_statement; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:_set_up_object_manager; 61, argument_list; 62, comment; 63, for_statement; 63, 64; 63, 65; 63, 69; 64, identifier:symbol; 65, call; 65, 66; 65, 67; 66, identifier:dir; 67, argument_list; 67, 68; 68, identifier:module; 69, block; 69, 70; 69, 78; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:fn; 73, call; 73, 74; 73, 75; 74, identifier:getattr; 75, argument_list; 75, 76; 75, 77; 76, identifier:module; 77, identifier:symbol; 78, if_statement; 78, 79; 78, 99; 78, 100; 79, parenthesized_expression; 79, 80; 80, boolean_operator:and; 80, 81; 80, 87; 81, comparison_operator:in; 81, 82; 81, 83; 82, string:'_dbus_interface'; 83, call; 83, 84; 83, 85; 84, identifier:dir; 85, argument_list; 85, 86; 86, identifier:fn; 87, parenthesized_expression; 87, 88; 88, boolean_operator:or; 88, 89; 88, 95; 89, comparison_operator:not; 89, 90; 89, 91; 90, string:'_dbus_is_signal'; 91, call; 91, 92; 91, 93; 92, identifier:dir; 93, argument_list; 93, 94; 94, identifier:fn; 95, not_operator; 95, 96; 96, attribute; 96, 97; 96, 98; 97, identifier:fn; 98, identifier:_dbus_is_signal; 99, comment; 100, block; 100, 101; 100, 110; 101, expression_statement; 101, 102; 102, call; 102, 103; 102, 104; 103, identifier:setattr; 104, argument_list; 104, 105; 104, 108; 104, 109; 105, attribute; 105, 106; 105, 107; 106, identifier:self; 107, identifier:__class__; 108, identifier:symbol; 109, identifier:fn; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 128; 112, subscript; 112, 113; 112, 124; 113, call; 113, 114; 113, 119; 114, attribute; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:methods; 118, identifier:setdefault; 119, argument_list; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:fn; 122, identifier:_dbus_interface; 123, dictionary; 124, call; 124, 125; 124, 126; 125, identifier:str; 126, argument_list; 126, 127; 127, identifier:symbol; 128, tuple; 128, 129; 128, 132; 128, 135; 128, 136; 129, attribute; 129, 130; 129, 131; 130, identifier:fn; 131, identifier:_dbus_in_signature; 132, attribute; 132, 133; 132, 134; 133, identifier:fn; 134, identifier:_dbus_out_signature; 135, string:''; 136, identifier:fn; 137, if_statement; 137, 138; 137, 141; 138, comparison_operator:is; 138, 139; 138, 140; 139, identifier:parameters; 140, None; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:parameters; 145, dictionary; 146, expression_statement; 146, 147; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:module; 150, identifier:load; 151, argument_list; 151, 152; 151, 153; 152, identifier:self; 153, identifier:parameters; 154, comment; 155, comment; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:self; 160, identifier:_template; 161, identifier:template; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:self; 166, identifier:_template_parameters; 167, identifier:parameters
def AddTemplate(self, template, parameters): '''Load a template into the mock. python-dbusmock ships a set of standard mocks for common system services such as UPower and NetworkManager. With these the actual tests become a lot simpler, as they only have to set up the particular properties for the tests, and not the skeleton of common properties, interfaces, and methods. template: Name of the template to load or the full path to a *.py file for custom templates. See "pydoc dbusmock.templates" for a list of available templates from python-dbusmock package, and "pydoc dbusmock.templates.NAME" for documentation about template NAME. parameters: A parameter (string) → value (variant) map, for parameterizing templates. Each template can define their own, see documentation of that particular template for details. ''' try: module = load_module(template) except ImportError as e: raise dbus.exceptions.DBusException('Cannot add template %s: %s' % (template, str(e)), name='org.freedesktop.DBus.Mock.TemplateError') # If the template specifies this is an ObjectManager, set that up if hasattr(module, 'IS_OBJECT_MANAGER') and module.IS_OBJECT_MANAGER: self._set_up_object_manager() # pick out all D-Bus service methods and add them to our interface for symbol in dir(module): fn = getattr(module, symbol) if ('_dbus_interface' in dir(fn) and ('_dbus_is_signal' not in dir(fn) or not fn._dbus_is_signal)): # for dbus-python compatibility, add methods as callables setattr(self.__class__, symbol, fn) self.methods.setdefault(fn._dbus_interface, {})[str(symbol)] = ( fn._dbus_in_signature, fn._dbus_out_signature, '', fn ) if parameters is None: parameters = {} module.load(self, parameters) # save the given template and parameters for re-instantiation on # Reset() self._template = template self._template_parameters = parameters
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:format_args; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:args; 6, block; 6, 7; 6, 9; 6, 197; 6, 201; 6, 219; 6, 228; 7, expression_statement; 7, 8; 8, string:'''Format a D-Bus argument tuple into an appropriate logging string.'''; 9, function_definition; 9, 10; 9, 11; 9, 13; 10, function_name:format_arg; 11, parameters; 11, 12; 12, identifier:a; 13, block; 13, 14; 13, 31; 13, 48; 13, 66; 13, 82; 13, 110; 13, 135; 13, 191; 13, 192; 14, if_statement; 14, 15; 14, 22; 15, call; 15, 16; 15, 17; 16, identifier:isinstance; 17, argument_list; 17, 18; 17, 19; 18, identifier:a; 19, attribute; 19, 20; 19, 21; 20, identifier:dbus; 21, identifier:Boolean; 22, block; 22, 23; 23, return_statement; 23, 24; 24, call; 24, 25; 24, 26; 25, identifier:str; 26, argument_list; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:bool; 29, argument_list; 29, 30; 30, identifier:a; 31, if_statement; 31, 32; 31, 39; 32, call; 32, 33; 32, 34; 33, identifier:isinstance; 34, argument_list; 34, 35; 34, 36; 35, identifier:a; 36, attribute; 36, 37; 36, 38; 37, identifier:dbus; 38, identifier:Byte; 39, block; 39, 40; 40, return_statement; 40, 41; 41, call; 41, 42; 41, 43; 42, identifier:str; 43, argument_list; 43, 44; 44, call; 44, 45; 44, 46; 45, identifier:int; 46, argument_list; 46, 47; 47, identifier:a; 48, if_statement; 48, 49; 48, 60; 49, boolean_operator:or; 49, 50; 49, 55; 50, call; 50, 51; 50, 52; 51, identifier:isinstance; 52, argument_list; 52, 53; 52, 54; 53, identifier:a; 54, identifier:int; 55, call; 55, 56; 55, 57; 56, identifier:isinstance; 57, argument_list; 57, 58; 57, 59; 58, identifier:a; 59, identifier:long; 60, block; 60, 61; 61, return_statement; 61, 62; 62, call; 62, 63; 62, 64; 63, identifier:str; 64, argument_list; 64, 65; 65, identifier:a; 66, if_statement; 66, 67; 66, 72; 67, call; 67, 68; 67, 69; 68, identifier:isinstance; 69, argument_list; 69, 70; 69, 71; 70, identifier:a; 71, identifier:str; 72, block; 72, 73; 73, return_statement; 73, 74; 74, binary_operator:+; 74, 75; 74, 81; 75, binary_operator:+; 75, 76; 75, 77; 76, string:'"'; 77, call; 77, 78; 77, 79; 78, identifier:str; 79, argument_list; 79, 80; 80, identifier:a; 81, string:'"'; 82, if_statement; 82, 83; 82, 88; 82, 89; 83, call; 83, 84; 83, 85; 84, identifier:isinstance; 85, argument_list; 85, 86; 85, 87; 86, identifier:a; 87, identifier:unicode; 88, comment; 89, block; 89, 90; 90, return_statement; 90, 91; 91, binary_operator:+; 91, 92; 91, 109; 92, binary_operator:+; 92, 93; 92, 94; 93, string:'"'; 94, subscript; 94, 95; 94, 104; 95, call; 95, 96; 95, 97; 96, identifier:repr; 97, argument_list; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:a; 101, identifier:encode; 102, argument_list; 102, 103; 103, string:'UTF-8'; 104, slice; 104, 105; 104, 106; 104, 107; 105, integer:1; 106, colon; 107, unary_operator:-; 107, 108; 108, integer:1; 109, string:'"'; 110, if_statement; 110, 111; 110, 116; 111, call; 111, 112; 111, 113; 112, identifier:isinstance; 113, argument_list; 113, 114; 113, 115; 114, identifier:a; 115, identifier:list; 116, block; 116, 117; 117, return_statement; 117, 118; 118, binary_operator:+; 118, 119; 118, 134; 119, binary_operator:+; 119, 120; 119, 121; 120, string:'['; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, string:', '; 124, identifier:join; 125, argument_list; 125, 126; 126, list_comprehension; 126, 127; 126, 131; 127, call; 127, 128; 127, 129; 128, identifier:format_arg; 129, argument_list; 129, 130; 130, identifier:x; 131, for_in_clause; 131, 132; 131, 133; 132, identifier:x; 133, identifier:a; 134, string:']'; 135, if_statement; 135, 136; 135, 141; 136, call; 136, 137; 136, 138; 137, identifier:isinstance; 138, argument_list; 138, 139; 138, 140; 139, identifier:a; 140, identifier:dict; 141, block; 141, 142; 141, 146; 141, 150; 141, 187; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:fmta; 145, string:'{'; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:first; 149, True; 150, for_statement; 150, 151; 150, 154; 150, 159; 151, pattern_list; 151, 152; 151, 153; 152, identifier:k; 153, identifier:v; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:a; 157, identifier:items; 158, argument_list; 159, block; 159, 160; 159, 173; 160, if_statement; 160, 161; 160, 162; 160, 167; 161, identifier:first; 162, block; 162, 163; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:first; 166, False; 167, else_clause; 167, 168; 168, block; 168, 169; 169, expression_statement; 169, 170; 170, augmented_assignment:+=; 170, 171; 170, 172; 171, identifier:fmta; 172, string:', '; 173, expression_statement; 173, 174; 174, augmented_assignment:+=; 174, 175; 174, 176; 175, identifier:fmta; 176, binary_operator:+; 176, 177; 176, 183; 177, binary_operator:+; 177, 178; 177, 182; 178, call; 178, 179; 178, 180; 179, identifier:format_arg; 180, argument_list; 180, 181; 181, identifier:k; 182, string:': '; 183, call; 183, 184; 183, 185; 184, identifier:format_arg; 185, argument_list; 185, 186; 186, identifier:v; 187, return_statement; 187, 188; 188, binary_operator:+; 188, 189; 188, 190; 189, identifier:fmta; 190, string:'}'; 191, comment; 192, return_statement; 192, 193; 193, call; 193, 194; 193, 195; 194, identifier:repr; 195, argument_list; 195, 196; 196, identifier:a; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:s; 200, string:''; 201, for_statement; 201, 202; 201, 203; 201, 204; 202, identifier:a; 203, identifier:args; 204, block; 204, 205; 204, 212; 205, if_statement; 205, 206; 205, 207; 206, identifier:s; 207, block; 207, 208; 208, expression_statement; 208, 209; 209, augmented_assignment:+=; 209, 210; 209, 211; 210, identifier:s; 211, string:' '; 212, expression_statement; 212, 213; 213, augmented_assignment:+=; 213, 214; 213, 215; 214, identifier:s; 215, call; 215, 216; 215, 217; 216, identifier:format_arg; 217, argument_list; 217, 218; 218, identifier:a; 219, if_statement; 219, 220; 219, 221; 220, identifier:s; 221, block; 221, 222; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:s; 225, binary_operator:+; 225, 226; 225, 227; 226, string:' '; 227, identifier:s; 228, return_statement; 228, 229; 229, identifier:s
def format_args(self, args): '''Format a D-Bus argument tuple into an appropriate logging string.''' def format_arg(a): if isinstance(a, dbus.Boolean): return str(bool(a)) if isinstance(a, dbus.Byte): return str(int(a)) if isinstance(a, int) or isinstance(a, long): return str(a) if isinstance(a, str): return '"' + str(a) + '"' if isinstance(a, unicode): # Python 2 only return '"' + repr(a.encode('UTF-8'))[1:-1] + '"' if isinstance(a, list): return '[' + ', '.join([format_arg(x) for x in a]) + ']' if isinstance(a, dict): fmta = '{' first = True for k, v in a.items(): if first: first = False else: fmta += ', ' fmta += format_arg(k) + ': ' + format_arg(v) return fmta + '}' # fallback return repr(a) s = '' for a in args: if s: s += ' ' s += format_arg(a) if s: s = ' ' + s return s
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:Introspect; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:object_path; 6, identifier:connection; 7, block; 7, 8; 7, 10; 7, 11; 7, 27; 7, 35; 7, 43; 7, 79; 7, 87; 7, 102; 7, 111; 7, 213; 7, 233; 7, 234; 7, 242; 8, expression_statement; 8, 9; 9, string:'''Return XML description of this object's interfaces, methods and signals. This wraps dbus-python's Introspect() method to include the dynamic methods and properties. '''; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:cls; 14, binary_operator:+; 14, 15; 14, 22; 15, binary_operator:+; 15, 16; 15, 21; 16, attribute; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:__class__; 20, identifier:__module__; 21, string:'.'; 22, attribute; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:__class__; 26, identifier:__name__; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:orig_interfaces; 30, subscript; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:_dbus_class_table; 34, identifier:cls; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:mock_interfaces; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:orig_interfaces; 41, identifier:copy; 42, argument_list; 43, for_statement; 43, 44; 43, 47; 43, 54; 44, pattern_list; 44, 45; 44, 46; 45, identifier:interface; 46, identifier:methods; 47, call; 47, 48; 47, 53; 48, attribute; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:methods; 52, identifier:items; 53, argument_list; 54, block; 54, 55; 55, for_statement; 55, 56; 55, 57; 55, 58; 56, identifier:method; 57, identifier:methods; 58, block; 58, 59; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 70; 61, subscript; 61, 62; 61, 69; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:mock_interfaces; 65, identifier:setdefault; 66, argument_list; 66, 67; 66, 68; 67, identifier:interface; 68, dictionary; 69, identifier:method; 70, subscript; 70, 71; 70, 78; 71, subscript; 71, 72; 71, 77; 72, subscript; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:self; 75, identifier:methods; 76, identifier:interface; 77, identifier:method; 78, integer:3; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 86; 81, subscript; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:_dbus_class_table; 85, identifier:cls; 86, identifier:mock_interfaces; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:xml; 90, call; 90, 91; 90, 98; 91, attribute; 91, 92; 91, 97; 92, attribute; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:dbus; 95, identifier:service; 96, identifier:Object; 97, identifier:Introspect; 98, argument_list; 98, 99; 98, 100; 98, 101; 99, identifier:self; 100, identifier:object_path; 101, identifier:connection; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:tree; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:ElementTree; 108, identifier:fromstring; 109, argument_list; 109, 110; 110, identifier:xml; 111, for_statement; 111, 112; 111, 113; 111, 116; 111, 117; 111, 118; 111, 119; 112, identifier:name; 113, attribute; 113, 114; 113, 115; 114, identifier:self; 115, identifier:props; 116, comment; 117, comment; 118, comment; 119, block; 119, 120; 119, 131; 119, 156; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:interface; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:tree; 126, identifier:find; 127, argument_list; 127, 128; 128, binary_operator:%; 128, 129; 128, 130; 129, string:".//interface[@name='%s']"; 130, identifier:name; 131, if_statement; 131, 132; 131, 135; 132, comparison_operator:is; 132, 133; 132, 134; 133, identifier:interface; 134, None; 135, block; 135, 136; 135, 149; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:interface; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:ElementTree; 142, identifier:Element; 143, argument_list; 143, 144; 143, 145; 144, string:"interface"; 145, dictionary; 145, 146; 146, pair; 146, 147; 146, 148; 147, string:"name"; 148, identifier:name; 149, expression_statement; 149, 150; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:tree; 153, identifier:append; 154, argument_list; 154, 155; 155, identifier:interface; 156, for_statement; 156, 157; 156, 160; 156, 169; 157, pattern_list; 157, 158; 157, 159; 158, identifier:prop; 159, identifier:val; 160, call; 160, 161; 160, 168; 161, attribute; 161, 162; 161, 167; 162, subscript; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:self; 165, identifier:props; 166, identifier:name; 167, identifier:items; 168, argument_list; 169, block; 169, 170; 169, 177; 169, 206; 170, if_statement; 170, 171; 170, 174; 170, 175; 171, comparison_operator:is; 171, 172; 171, 173; 172, identifier:val; 173, None; 174, comment; 175, block; 175, 176; 176, continue_statement; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:elem; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:ElementTree; 183, identifier:Element; 184, argument_list; 184, 185; 184, 186; 185, string:"property"; 186, dictionary; 186, 187; 186, 190; 186, 191; 186, 203; 187, pair; 187, 188; 187, 189; 188, string:"name"; 189, identifier:prop; 190, comment; 191, pair; 191, 192; 191, 193; 192, string:"type"; 193, call; 193, 194; 193, 201; 194, attribute; 194, 195; 194, 200; 195, attribute; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:dbus; 198, identifier:lowlevel; 199, identifier:Message; 200, identifier:guess_signature; 201, argument_list; 201, 202; 202, identifier:val; 203, pair; 203, 204; 203, 205; 204, string:"access"; 205, string:"readwrite"; 206, expression_statement; 206, 207; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:interface; 210, identifier:append; 211, argument_list; 211, 212; 212, identifier:elem; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 216; 215, identifier:xml; 216, call; 216, 217; 216, 231; 217, attribute; 217, 218; 217, 230; 218, call; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:ElementTree; 221, identifier:tostring; 222, argument_list; 222, 223; 222, 224; 222, 227; 223, identifier:tree; 224, keyword_argument; 224, 225; 224, 226; 225, identifier:encoding; 226, string:'utf8'; 227, keyword_argument; 227, 228; 227, 229; 228, identifier:method; 229, string:'xml'; 230, identifier:decode; 231, argument_list; 231, 232; 232, string:'utf8'; 233, comment; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 241; 236, subscript; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:self; 239, identifier:_dbus_class_table; 240, identifier:cls; 241, identifier:orig_interfaces; 242, return_statement; 242, 243; 243, identifier:xml
def Introspect(self, object_path, connection): '''Return XML description of this object's interfaces, methods and signals. This wraps dbus-python's Introspect() method to include the dynamic methods and properties. ''' # temporarily add our dynamic methods cls = self.__class__.__module__ + '.' + self.__class__.__name__ orig_interfaces = self._dbus_class_table[cls] mock_interfaces = orig_interfaces.copy() for interface, methods in self.methods.items(): for method in methods: mock_interfaces.setdefault(interface, {})[method] = self.methods[interface][method][3] self._dbus_class_table[cls] = mock_interfaces xml = dbus.service.Object.Introspect(self, object_path, connection) tree = ElementTree.fromstring(xml) for name in self.props: # We might have properties for new interfaces we don't know about # yet. Try to find an existing <interface> node named after our # interface to append to, and create one if we can't. interface = tree.find(".//interface[@name='%s']" % name) if interface is None: interface = ElementTree.Element("interface", {"name": name}) tree.append(interface) for prop, val in self.props[name].items(): if val is None: # can't guess type from None, skip continue elem = ElementTree.Element("property", { "name": prop, # We don't store the signature anywhere, so guess it. "type": dbus.lowlevel.Message.guess_signature(val), "access": "readwrite"}) interface.append(elem) xml = ElementTree.tostring(tree, encoding='utf8', method='xml').decode('utf8') # restore original class table self._dbus_class_table[cls] = orig_interfaces return xml
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:argument; 3, parameters; 3, 4; 3, 6; 3, 9; 4, list_splat_pattern; 4, 5; 5, identifier:param_decls; 6, default_parameter; 6, 7; 6, 8; 7, identifier:cls; 8, None; 9, dictionary_splat_pattern; 9, 10; 10, identifier:attrs; 11, block; 11, 12; 11, 14; 12, expression_statement; 12, 13; 13, comment; 14, return_statement; 14, 15; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:click; 18, identifier:argument; 19, argument_list; 19, 20; 19, 22; 19, 27; 20, list_splat; 20, 21; 21, identifier:param_decls; 22, keyword_argument; 22, 23; 22, 24; 23, identifier:cls; 24, boolean_operator:or; 24, 25; 24, 26; 25, identifier:cls; 26, identifier:Argument; 27, dictionary_splat; 27, 28; 28, identifier:attrs
def argument(*param_decls, cls=None, **attrs): """ Arguments are positional parameters to a command. They generally provide fewer features than options but can have infinite ``nargs`` and are required by default. :param param_decls: the parameter declarations for this option or argument. This is a list of flags or argument names. :param type: the type that should be used. Either a :class:`ParamType` or a Python type. The later is converted into the former automatically if supported. :param required: controls if this is optional or not. :param default: the default value if omitted. This can also be a callable, in which case it's invoked when the default is needed without any arguments. :param callback: a callback that should be executed after the parameter was matched. This is called as ``fn(ctx, param, value)`` and needs to return the value. Before Click 2.0, the signature was ``(ctx, value)``. :param nargs: the number of arguments to match. If not ``1`` the return value is a tuple instead of single value. The default for nargs is ``1`` (except if the type is a tuple, then it's the arity of the tuple). :param metavar: how the value is represented in the help page. :param expose_value: if this is `True` then the value is passed onwards to the command callback and stored on the context, otherwise it's skipped. :param is_eager: eager values are processed before non eager ones. This should not be set for arguments or it will inverse the order of processing. :param envvar: a string or list of strings that are environment variables that should be checked. :param help: the help string. :param hidden: hide this option from help outputs. Default is True, unless help is given. """ return click.argument(*param_decls, cls=cls or Argument, **attrs)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:option; 3, parameters; 3, 4; 3, 6; 3, 9; 4, list_splat_pattern; 4, 5; 5, identifier:param_decls; 6, default_parameter; 6, 7; 6, 8; 7, identifier:cls; 8, None; 9, dictionary_splat_pattern; 9, 10; 10, identifier:attrs; 11, block; 11, 12; 11, 14; 12, expression_statement; 12, 13; 13, comment; 14, return_statement; 14, 15; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:click; 18, identifier:option; 19, argument_list; 19, 20; 19, 22; 19, 27; 20, list_splat; 20, 21; 21, identifier:param_decls; 22, keyword_argument; 22, 23; 22, 24; 23, identifier:cls; 24, boolean_operator:or; 24, 25; 24, 26; 25, identifier:cls; 26, identifier:Option; 27, dictionary_splat; 27, 28; 28, identifier:attrs
def option(*param_decls, cls=None, **attrs): """ Options are usually optional values on the command line and have some extra features that arguments don't have. :param param_decls: the parameter declarations for this option or argument. This is a list of flags or argument names. :param show_default: controls if the default value should be shown on the help page. Normally, defaults are not shown. :param prompt: if set to `True` or a non empty string then the user will be prompted for input if not set. If set to `True` the prompt will be the option name capitalized. :param confirmation_prompt: if set then the value will need to be confirmed if it was prompted for. :param hide_input: if this is `True` then the input on the prompt will be hidden from the user. This is useful for password input. :param is_flag: forces this option to act as a flag. The default is auto detection. :param flag_value: which value should be used for this flag if it's enabled. This is set to a boolean automatically if the option string contains a slash to mark two options. :param multiple: if this is set to `True` then the argument is accepted multiple times and recorded. This is similar to ``nargs`` in how it works but supports arbitrary number of arguments. :param count: this flag makes an option increment an integer. :param allow_from_autoenv: if this is enabled then the value of this parameter will be pulled from an environment variable in case a prefix is defined on the context. :param help: the help string. """ return click.option(*param_decls, cls=cls or Option, **attrs)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 1, 14; 2, function_name:add_to_loader_class; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:cls; 5, default_parameter; 5, 6; 5, 7; 6, identifier:loader_class; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:tag; 10, None; 11, dictionary_splat_pattern; 11, 12; 12, identifier:kwargs; 13, comment; 14, block; 14, 15; 14, 17; 14, 26; 14, 34; 14, 44; 14, 58; 14, 66; 14, 104; 15, expression_statement; 15, 16; 16, comment; 17, if_statement; 17, 18; 17, 21; 18, comparison_operator:is; 18, 19; 18, 20; 19, identifier:tag; 20, None; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:tag; 25, string:''; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:tag; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:tag; 32, identifier:strip; 33, argument_list; 34, if_statement; 34, 35; 34, 37; 35, not_operator; 35, 36; 36, identifier:tag; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:tag; 41, attribute; 41, 42; 41, 43; 42, identifier:cls; 43, identifier:DEFAULT_TAG_NAME; 44, if_statement; 44, 45; 44, 52; 45, not_operator; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:tag; 49, identifier:startswith; 50, argument_list; 50, 51; 51, string:'!'; 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:'`tag` argument should start with character "!"'; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:instance; 61, call; 61, 62; 61, 63; 62, identifier:cls; 63, argument_list; 63, 64; 64, dictionary_splat; 64, 65; 65, identifier:kwargs; 66, if_statement; 66, 67; 66, 70; 66, 93; 67, comparison_operator:is; 67, 68; 67, 69; 68, identifier:loader_class; 69, None; 70, block; 70, 71; 71, if_statement; 71, 72; 71, 73; 71, 83; 72, identifier:FullLoader; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:yaml; 78, identifier:add_constructor; 79, argument_list; 79, 80; 79, 81; 79, 82; 80, identifier:tag; 81, identifier:instance; 82, identifier:FullLoader; 83, else_clause; 83, 84; 84, block; 84, 85; 85, expression_statement; 85, 86; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:yaml; 89, identifier:add_constructor; 90, argument_list; 90, 91; 90, 92; 91, identifier:tag; 92, identifier:instance; 93, else_clause; 93, 94; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:yaml; 99, identifier:add_constructor; 100, argument_list; 100, 101; 100, 102; 100, 103; 101, identifier:tag; 102, identifier:instance; 103, identifier:loader_class; 104, return_statement; 104, 105; 105, identifier:instance
def add_to_loader_class(cls, loader_class=None, tag=None, **kwargs): # type: (type(yaml.Loader), str, **str)-> YamlIncludeConstructor """ Create an instance of the constructor, and add it to the YAML `Loader` class :param loader_class: The `Loader` class add constructor to. .. attention:: This parameter **SHOULD** be a **class type**, **NOT** object. It's one of following: - :class:`yaml.BaseLoader` - :class:`yaml.UnSafeLoader` - :class:`yaml.SafeLoader` - :class:`yaml.Loader` - :class:`yaml.FullLoader` - :class:`yaml.CBaseLoader` - :class:`yaml.CUnSafeLoader` - :class:`yaml.CSafeLoader` - :class:`yaml.CLoader` - :class:`yaml.CFullLoader` :default: ``None``: - When :mod:`pyyaml` 3.*: Add to PyYAML's default `Loader` - When :mod:`pyyaml` 5.*: Add to `FullLoader` :type loader_class: type :param str tag: Tag's name of the include constructor. :default: ``""``: Use :attr:`DEFAULT_TAG_NAME` as tag name. :param kwargs: Arguments passed to construct function :return: New created object :rtype: YamlIncludeConstructor """ if tag is None: tag = '' tag = tag.strip() if not tag: tag = cls.DEFAULT_TAG_NAME if not tag.startswith('!'): raise ValueError('`tag` argument should start with character "!"') instance = cls(**kwargs) if loader_class is None: if FullLoader: yaml.add_constructor(tag, instance, FullLoader) else: yaml.add_constructor(tag, instance) else: yaml.add_constructor(tag, instance, loader_class) return instance
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 26; 1, 28; 2, function_name:print_table; 3, parameters; 3, 4; 3, 8; 3, 12; 3, 21; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:column_names; 6, type; 6, 7; 7, identifier:IterableOfStrings; 8, typed_parameter; 8, 9; 8, 10; 9, identifier:rows; 10, type; 10, 11; 11, identifier:IterableOfTuples; 12, typed_default_parameter; 12, 13; 12, 14; 12, 20; 13, identifier:column_alignments; 14, type; 14, 15; 15, generic_type; 15, 16; 15, 17; 16, identifier:Optional; 17, type_parameter; 17, 18; 18, type; 18, 19; 19, identifier:IterableOfStrings; 20, None; 21, typed_default_parameter; 21, 22; 21, 23; 21, 25; 22, identifier:primary_column_idx; 23, type; 23, 24; 24, identifier:int; 25, integer:0; 26, type; 26, 27; 27, None; 28, block; 28, 29; 28, 31; 28, 35; 28, 39; 28, 43; 28, 56; 28, 75; 28, 85; 28, 107; 28, 147; 28, 210; 28, 211; 29, expression_statement; 29, 30; 30, comment; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:header_template; 34, string:''; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:row_template; 38, string:''; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:table_width; 42, integer:0; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:type_formatters; 46, dictionary; 46, 47; 46, 50; 46, 53; 47, pair; 47, 48; 47, 49; 48, identifier:int; 49, string:'d'; 50, pair; 50, 51; 50, 52; 51, identifier:float; 52, string:'f'; 53, pair; 53, 54; 53, 55; 54, identifier:str; 55, string:'s'; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:types; 59, list_comprehension; 59, 60; 59, 70; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:type_formatters; 63, identifier:get; 64, argument_list; 64, 65; 64, 69; 65, call; 65, 66; 65, 67; 66, identifier:type; 67, argument_list; 67, 68; 68, identifier:x; 69, string:'r'; 70, for_in_clause; 70, 71; 70, 72; 71, identifier:x; 72, subscript; 72, 73; 72, 74; 73, identifier:rows; 74, integer:0; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:alignments; 78, dictionary; 78, 79; 78, 82; 79, pair; 79, 80; 79, 81; 80, identifier:int; 81, string:'>'; 82, pair; 82, 83; 82, 84; 83, identifier:float; 84, string:'>'; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:column_alignments; 88, parenthesized_expression; 88, 89; 89, boolean_operator:or; 89, 90; 89, 91; 90, identifier:column_alignments; 91, list_comprehension; 91, 92; 91, 102; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:alignments; 95, identifier:get; 96, argument_list; 96, 97; 96, 101; 97, call; 97, 98; 97, 99; 98, identifier:type; 99, argument_list; 99, 100; 100, identifier:x; 101, string:'<'; 102, for_in_clause; 102, 103; 102, 104; 103, identifier:x; 104, subscript; 104, 105; 104, 106; 105, identifier:rows; 106, integer:0; 107, function_definition; 107, 108; 107, 109; 107, 111; 108, function_name:get_column_width; 109, parameters; 109, 110; 110, identifier:idx; 111, block; 111, 112; 111, 121; 111, 139; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:header_length; 115, call; 115, 116; 115, 117; 116, identifier:len; 117, argument_list; 117, 118; 118, subscript; 118, 119; 118, 120; 119, identifier:column_names; 120, identifier:idx; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:content_length; 124, call; 124, 125; 124, 126; 125, identifier:max; 126, generator_expression; 126, 127; 126, 136; 127, call; 127, 128; 127, 129; 128, identifier:len; 129, argument_list; 129, 130; 130, call; 130, 131; 130, 132; 131, identifier:str; 132, argument_list; 132, 133; 133, subscript; 133, 134; 133, 135; 134, identifier:row; 135, identifier:idx; 136, for_in_clause; 136, 137; 136, 138; 137, identifier:row; 138, identifier:rows; 139, return_statement; 139, 140; 140, parenthesized_expression; 140, 141; 141, conditional_expression:if; 141, 142; 141, 143; 141, 146; 142, identifier:content_length; 143, comparison_operator:>; 143, 144; 143, 145; 144, identifier:content_length; 145, identifier:header_length; 146, identifier:header_length; 147, for_statement; 147, 148; 147, 149; 147, 157; 148, identifier:i; 149, call; 149, 150; 149, 151; 150, identifier:range; 151, argument_list; 151, 152; 151, 153; 152, integer:0; 153, call; 153, 154; 153, 155; 154, identifier:len; 155, argument_list; 155, 156; 156, identifier:column_names; 157, block; 157, 158; 157, 165; 157, 169; 157, 173; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:col_width; 161, call; 161, 162; 161, 163; 162, identifier:get_column_width; 163, argument_list; 163, 164; 164, identifier:i; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:header_col_template; 168, string:f'{{:{col_width}}}'; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:col_template; 172, string:f'{{:{column_alignments[i]}{col_width}{types[i]}}}'; 173, if_statement; 173, 174; 173, 177; 173, 190; 174, comparison_operator:==; 174, 175; 174, 176; 175, identifier:i; 176, integer:0; 177, block; 177, 178; 177, 182; 177, 186; 178, expression_statement; 178, 179; 179, augmented_assignment:+=; 179, 180; 179, 181; 180, identifier:header_template; 181, identifier:header_col_template; 182, expression_statement; 182, 183; 183, augmented_assignment:+=; 183, 184; 183, 185; 184, identifier:row_template; 185, identifier:col_template; 186, expression_statement; 186, 187; 187, augmented_assignment:+=; 187, 188; 187, 189; 188, identifier:table_width; 189, identifier:col_width; 190, else_clause; 190, 191; 191, block; 191, 192; 191, 198; 191, 204; 192, expression_statement; 192, 193; 193, augmented_assignment:+=; 193, 194; 193, 195; 194, identifier:header_template; 195, binary_operator:+; 195, 196; 195, 197; 196, string:' '; 197, identifier:header_col_template; 198, expression_statement; 198, 199; 199, augmented_assignment:+=; 199, 200; 199, 201; 200, identifier:row_template; 201, binary_operator:+; 201, 202; 201, 203; 202, string:' '; 203, identifier:col_template; 204, expression_statement; 204, 205; 205, augmented_assignment:+=; 205, 206; 205, 207; 206, identifier:table_width; 207, binary_operator:+; 207, 208; 207, 209; 208, integer:2; 209, identifier:col_width; 210, comment; 211, if_statement; 211, 212; 211, 217; 211, 270; 211, 271; 212, comparison_operator:<; 212, 213; 212, 214; 213, identifier:table_width; 214, call; 214, 215; 214, 216; 215, identifier:get_terminal_width; 216, argument_list; 217, block; 217, 218; 217, 231; 217, 240; 218, expression_statement; 218, 219; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:click; 222, identifier:echo; 223, argument_list; 223, 224; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:header_template; 227, identifier:format; 228, argument_list; 228, 229; 229, list_splat; 229, 230; 230, identifier:column_names; 231, expression_statement; 231, 232; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:click; 235, identifier:echo; 236, argument_list; 236, 237; 237, binary_operator:*; 237, 238; 237, 239; 238, string:'-'; 239, identifier:table_width; 240, for_statement; 240, 241; 240, 242; 240, 243; 241, identifier:row; 242, identifier:rows; 243, block; 243, 244; 244, try_statement; 244, 245; 244, 259; 245, block; 245, 246; 246, expression_statement; 246, 247; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:click; 250, identifier:echo; 251, argument_list; 251, 252; 252, call; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:row_template; 255, identifier:format; 256, argument_list; 256, 257; 257, list_splat; 257, 258; 258, identifier:row; 259, except_clause; 259, 260; 259, 264; 260, as_pattern; 260, 261; 260, 262; 261, identifier:TypeError; 262, as_pattern_target; 262, 263; 263, identifier:e; 264, block; 264, 265; 265, raise_statement; 265, 266; 266, call; 266, 267; 266, 268; 267, identifier:TypeError; 268, argument_list; 268, 269; 269, string:f'{e}: {row!r}'; 270, comment; 271, else_clause; 271, 272; 272, block; 272, 273; 272, 288; 272, 307; 273, expression_statement; 273, 274; 274, assignment; 274, 275; 274, 276; 275, identifier:max_label_width; 276, call; 276, 277; 276, 278; 277, identifier:max; 278, argument_list; 278, 279; 279, list_splat; 279, 280; 280, list_comprehension; 280, 281; 280, 285; 281, call; 281, 282; 281, 283; 282, identifier:len; 283, argument_list; 283, 284; 284, identifier:label; 285, for_in_clause; 285, 286; 285, 287; 286, identifier:label; 287, identifier:column_names; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 291; 290, identifier:non_primary_columns; 291, list_comprehension; 291, 292; 291, 295; 291, 303; 292, tuple; 292, 293; 292, 294; 293, identifier:i; 294, identifier:col; 295, for_in_clause; 295, 296; 295, 299; 296, pattern_list; 296, 297; 296, 298; 297, identifier:i; 298, identifier:col; 299, call; 299, 300; 299, 301; 300, identifier:enumerate; 301, argument_list; 301, 302; 302, identifier:column_names; 303, if_clause; 303, 304; 304, comparison_operator:!=; 304, 305; 304, 306; 305, identifier:i; 306, identifier:primary_column_idx; 307, for_statement; 307, 308; 307, 309; 307, 310; 308, identifier:row; 309, identifier:rows; 310, block; 310, 311; 310, 317; 310, 321; 310, 338; 310, 363; 311, expression_statement; 311, 312; 312, assignment; 312, 313; 312, 314; 313, identifier:type_; 314, subscript; 314, 315; 314, 316; 315, identifier:types; 316, identifier:primary_column_idx; 317, expression_statement; 317, 318; 318, assignment; 318, 319; 318, 320; 319, identifier:row_template; 320, string:f'{{:>{max_label_width}s}}: {{:{type_}}}'; 321, expression_statement; 321, 322; 322, call; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:click; 325, identifier:echo; 326, argument_list; 326, 327; 327, call; 327, 328; 327, 331; 328, attribute; 328, 329; 328, 330; 329, identifier:row_template; 330, identifier:format; 331, argument_list; 331, 332; 331, 335; 332, subscript; 332, 333; 332, 334; 333, identifier:column_names; 334, identifier:primary_column_idx; 335, subscript; 335, 336; 335, 337; 336, identifier:row; 337, identifier:primary_column_idx; 338, for_statement; 338, 339; 338, 342; 338, 343; 339, pattern_list; 339, 340; 339, 341; 340, identifier:i; 341, identifier:label; 342, identifier:non_primary_columns; 343, block; 343, 344; 343, 348; 344, expression_statement; 344, 345; 345, assignment; 345, 346; 345, 347; 346, identifier:row_template; 347, string:f'{{:>{max_label_width}s}}: {{:{types[i]}}}'; 348, expression_statement; 348, 349; 349, call; 349, 350; 349, 353; 350, attribute; 350, 351; 350, 352; 351, identifier:click; 352, identifier:echo; 353, argument_list; 353, 354; 354, call; 354, 355; 354, 358; 355, attribute; 355, 356; 355, 357; 356, identifier:row_template; 357, identifier:format; 358, argument_list; 358, 359; 358, 360; 359, identifier:label; 360, subscript; 360, 361; 360, 362; 361, identifier:row; 362, identifier:i; 363, expression_statement; 363, 364; 364, call; 364, 365; 364, 368; 365, attribute; 365, 366; 365, 367; 366, identifier:click; 367, identifier:echo; 368, argument_list
def print_table(column_names: IterableOfStrings, rows: IterableOfTuples, column_alignments: Optional[IterableOfStrings] = None, primary_column_idx: int = 0, ) -> None: """ Prints a table of information to the console. Automatically determines if the console is wide enough, and if not, displays the information in list form. :param column_names: The heading labels :param rows: A list of lists :param column_alignments: An optional list of strings, using either '<' or '>' to specify left or right alignment respectively :param primary_column_idx: Used when displaying information in list form, to determine which label should be the top-most one. Defaults to the first label in ``column_names``. """ header_template = '' row_template = '' table_width = 0 type_formatters = {int: 'd', float: 'f', str: 's'} types = [type_formatters.get(type(x), 'r') for x in rows[0]] alignments = {int: '>', float: '>'} column_alignments = (column_alignments or [alignments.get(type(x), '<') for x in rows[0]]) def get_column_width(idx): header_length = len(column_names[idx]) content_length = max(len(str(row[idx])) for row in rows) return (content_length if content_length > header_length else header_length) for i in range(0, len(column_names)): col_width = get_column_width(i) header_col_template = f'{{:{col_width}}}' col_template = f'{{:{column_alignments[i]}{col_width}{types[i]}}}' if i == 0: header_template += header_col_template row_template += col_template table_width += col_width else: header_template += ' ' + header_col_template row_template += ' ' + col_template table_width += 2 + col_width # check if we can format the table horizontally if table_width < get_terminal_width(): click.echo(header_template.format(*column_names)) click.echo('-' * table_width) for row in rows: try: click.echo(row_template.format(*row)) except TypeError as e: raise TypeError(f'{e}: {row!r}') # otherwise format it vertically else: max_label_width = max(*[len(label) for label in column_names]) non_primary_columns = [(i, col) for i, col in enumerate(column_names) if i != primary_column_idx] for row in rows: type_ = types[primary_column_idx] row_template = f'{{:>{max_label_width}s}}: {{:{type_}}}' click.echo(row_template.format(column_names[primary_column_idx], row[primary_column_idx])) for i, label in non_primary_columns: row_template = f'{{:>{max_label_width}s}}: {{:{types[i]}}}' click.echo(row_template.format(label, row[i])) click.echo()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 26; 2, function_name:model_fields; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 4, identifier:model; 5, default_parameter; 5, 6; 5, 7; 6, identifier:db_session; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:only; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:exclude; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:field_args; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:converter; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:exclude_pk; 22, False; 23, default_parameter; 23, 24; 23, 25; 24, identifier:exclude_fk; 25, False; 26, block; 26, 27; 26, 29; 26, 37; 26, 45; 26, 51; 26, 55; 26, 104; 26, 105; 26, 141; 26, 145; 26, 180; 27, expression_statement; 27, 28; 28, comment; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:mapper; 32, attribute; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:model; 35, identifier:_sa_class_manager; 36, identifier:mapper; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:converter; 40, boolean_operator:or; 40, 41; 40, 42; 41, identifier:converter; 42, call; 42, 43; 42, 44; 43, identifier:_ModelConverter; 44, argument_list; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:field_args; 48, boolean_operator:or; 48, 49; 48, 50; 49, identifier:field_args; 50, dictionary; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:properties; 54, list:[]; 55, for_statement; 55, 56; 55, 57; 55, 60; 56, identifier:prop; 57, attribute; 57, 58; 57, 59; 58, identifier:mapper; 59, identifier:iterate_properties; 60, block; 60, 61; 60, 93; 61, if_statement; 61, 62; 61, 68; 62, call; 62, 63; 62, 64; 63, identifier:getattr; 64, argument_list; 64, 65; 64, 66; 64, 67; 65, identifier:prop; 66, string:'columns'; 67, None; 68, block; 68, 69; 69, if_statement; 69, 70; 69, 79; 69, 81; 70, boolean_operator:and; 70, 71; 70, 72; 71, identifier:exclude_fk; 72, attribute; 72, 73; 72, 78; 73, subscript; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:prop; 76, identifier:columns; 77, integer:0; 78, identifier:foreign_keys; 79, block; 79, 80; 80, continue_statement; 81, elif_clause; 81, 82; 81, 91; 82, boolean_operator:and; 82, 83; 82, 84; 83, identifier:exclude_pk; 84, attribute; 84, 85; 84, 90; 85, subscript; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:prop; 88, identifier:columns; 89, integer:0; 90, identifier:primary_key; 91, block; 91, 92; 92, continue_statement; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:properties; 97, identifier:append; 98, argument_list; 98, 99; 99, tuple; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:prop; 102, identifier:key; 103, identifier:prop; 104, comment; 105, if_statement; 105, 106; 105, 109; 105, 124; 106, comparison_operator:is; 106, 107; 106, 108; 107, identifier:only; 108, None; 109, block; 109, 110; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:properties; 113, generator_expression; 113, 114; 113, 115; 113, 118; 114, identifier:x; 115, for_in_clause; 115, 116; 115, 117; 116, identifier:x; 117, identifier:properties; 118, if_clause; 118, 119; 119, comparison_operator:in; 119, 120; 119, 123; 120, subscript; 120, 121; 120, 122; 121, identifier:x; 122, integer:0; 123, identifier:only; 124, elif_clause; 124, 125; 124, 126; 125, identifier:exclude; 126, block; 126, 127; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:properties; 130, generator_expression; 130, 131; 130, 132; 130, 135; 131, identifier:x; 132, for_in_clause; 132, 133; 132, 134; 133, identifier:x; 134, identifier:properties; 135, if_clause; 135, 136; 136, comparison_operator:not; 136, 137; 136, 140; 137, subscript; 137, 138; 137, 139; 138, identifier:x; 139, integer:0; 140, identifier:exclude; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:field_dict; 144, dictionary; 145, for_statement; 145, 146; 145, 149; 145, 150; 146, pattern_list; 146, 147; 146, 148; 147, identifier:name; 148, identifier:prop; 149, identifier:properties; 150, block; 150, 151; 150, 169; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:field; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:converter; 157, identifier:convert; 158, argument_list; 158, 159; 158, 160; 158, 161; 158, 162; 158, 168; 159, identifier:model; 160, identifier:mapper; 161, identifier:prop; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:field_args; 165, identifier:get; 166, argument_list; 166, 167; 167, identifier:name; 168, identifier:db_session; 169, if_statement; 169, 170; 169, 173; 170, comparison_operator:is; 170, 171; 170, 172; 171, identifier:field; 172, None; 173, block; 173, 174; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 179; 176, subscript; 176, 177; 176, 178; 177, identifier:field_dict; 178, identifier:name; 179, identifier:field; 180, return_statement; 180, 181; 181, identifier:field_dict
def model_fields(model, db_session=None, only=None, exclude=None, field_args=None, converter=None, exclude_pk=False, exclude_fk=False): """ Generate a dictionary of fields for a given SQLAlchemy model. See `model_form` docstring for description of parameters. """ mapper = model._sa_class_manager.mapper converter = converter or _ModelConverter() field_args = field_args or {} properties = [] for prop in mapper.iterate_properties: if getattr(prop, 'columns', None): if exclude_fk and prop.columns[0].foreign_keys: continue elif exclude_pk and prop.columns[0].primary_key: continue properties.append((prop.key, prop)) # the following if condition is modified: if only is not None: properties = (x for x in properties if x[0] in only) elif exclude: properties = (x for x in properties if x[0] not in exclude) field_dict = {} for name, prop in properties: field = converter.convert( model, mapper, prop, field_args.get(name), db_session ) if field is not None: field_dict[name] = field return field_dict
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:singularize; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:word; 5, default_parameter; 5, 6; 5, 7; 6, identifier:pos; 7, identifier:NOUN; 8, default_parameter; 8, 9; 8, 10; 9, identifier:custom; 10, None; 11, block; 11, 12; 11, 14; 11, 25; 11, 26; 11, 76; 11, 77; 11, 97; 11, 105; 11, 119; 11, 133; 11, 154; 11, 181; 11, 253; 12, expression_statement; 12, 13; 13, comment; 14, if_statement; 14, 15; 14, 20; 15, boolean_operator:and; 15, 16; 15, 17; 16, identifier:custom; 17, comparison_operator:in; 17, 18; 17, 19; 18, identifier:word; 19, identifier:custom; 20, block; 20, 21; 21, return_statement; 21, 22; 22, subscript; 22, 23; 22, 24; 23, identifier:custom; 24, identifier:word; 25, comment; 26, if_statement; 26, 27; 26, 30; 27, comparison_operator:in; 27, 28; 27, 29; 28, string:"-"; 29, identifier:word; 30, block; 30, 31; 30, 40; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:w; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:word; 37, identifier:split; 38, argument_list; 38, 39; 39, string:"-"; 40, if_statement; 40, 41; 40, 53; 41, boolean_operator:and; 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:w; 47, integer:1; 48, comparison_operator:in; 48, 49; 48, 52; 49, subscript; 49, 50; 49, 51; 50, identifier:w; 51, integer:1; 52, identifier:plural_prepositions; 53, block; 53, 54; 54, return_statement; 54, 55; 55, binary_operator:+; 55, 56; 55, 66; 56, binary_operator:+; 56, 57; 56, 65; 57, call; 57, 58; 57, 59; 58, identifier:singularize; 59, argument_list; 59, 60; 59, 63; 59, 64; 60, subscript; 60, 61; 60, 62; 61, identifier:w; 62, integer:0; 63, identifier:pos; 64, identifier:custom; 65, string:"-"; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, string:"-"; 69, identifier:join; 70, argument_list; 70, 71; 71, subscript; 71, 72; 71, 73; 72, identifier:w; 73, slice; 73, 74; 73, 75; 74, integer:1; 75, colon; 76, comment; 77, if_statement; 77, 78; 77, 84; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:word; 81, identifier:endswith; 82, argument_list; 82, 83; 83, string:"'"; 84, block; 84, 85; 85, return_statement; 85, 86; 86, binary_operator:+; 86, 87; 86, 96; 87, call; 87, 88; 87, 89; 88, identifier:singularize; 89, argument_list; 89, 90; 90, subscript; 90, 91; 90, 92; 91, identifier:word; 92, slice; 92, 93; 92, 94; 93, colon; 94, unary_operator:-; 94, 95; 95, integer:1; 96, string:"'s"; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:w; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:word; 103, identifier:lower; 104, argument_list; 105, for_statement; 105, 106; 105, 107; 105, 108; 106, identifier:x; 107, identifier:singular_uninflected; 108, block; 108, 109; 109, if_statement; 109, 110; 109, 116; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:x; 113, identifier:endswith; 114, argument_list; 114, 115; 115, identifier:w; 116, block; 116, 117; 117, return_statement; 117, 118; 118, identifier:word; 119, for_statement; 119, 120; 119, 121; 119, 122; 120, identifier:x; 121, identifier:singular_uncountable; 122, block; 122, 123; 123, if_statement; 123, 124; 123, 130; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:x; 127, identifier:endswith; 128, argument_list; 128, 129; 129, identifier:w; 130, block; 130, 131; 131, return_statement; 131, 132; 132, identifier:word; 133, for_statement; 133, 134; 133, 135; 133, 136; 134, identifier:x; 135, identifier:singular_ie; 136, block; 136, 137; 137, if_statement; 137, 138; 137, 146; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:w; 141, identifier:endswith; 142, argument_list; 142, 143; 143, binary_operator:+; 143, 144; 143, 145; 144, identifier:x; 145, string:"s"; 146, block; 146, 147; 147, return_statement; 147, 148; 148, subscript; 148, 149; 148, 150; 149, identifier:w; 150, slice; 150, 151; 150, 152; 151, colon; 152, unary_operator:-; 152, 153; 153, integer:1; 154, for_statement; 154, 155; 154, 156; 154, 157; 155, identifier:x; 156, identifier:singular_irregular; 157, block; 157, 158; 158, if_statement; 158, 159; 158, 165; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:w; 162, identifier:endswith; 163, argument_list; 163, 164; 164, identifier:x; 165, block; 165, 166; 166, return_statement; 166, 167; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:re; 170, identifier:sub; 171, argument_list; 171, 172; 171, 177; 171, 180; 172, binary_operator:+; 172, 173; 172, 176; 173, binary_operator:+; 173, 174; 173, 175; 174, string:'(?i)'; 175, identifier:x; 176, string:'$'; 177, subscript; 177, 178; 177, 179; 178, identifier:singular_irregular; 179, identifier:x; 180, identifier:word; 181, for_statement; 181, 182; 181, 185; 181, 186; 182, pattern_list; 182, 183; 182, 184; 183, identifier:suffix; 184, identifier:inflection; 185, identifier:singular_rules; 186, block; 186, 187; 186, 196; 186, 208; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:m; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:suffix; 193, identifier:search; 194, argument_list; 194, 195; 195, identifier:word; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 199; 198, identifier:g; 199, boolean_operator:or; 199, 200; 199, 207; 200, boolean_operator:and; 200, 201; 200, 202; 201, identifier:m; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:m; 205, identifier:groups; 206, argument_list; 207, list:[]; 208, if_statement; 208, 209; 208, 210; 209, identifier:m; 210, block; 210, 211; 210, 245; 211, for_statement; 211, 212; 211, 213; 211, 220; 212, identifier:k; 213, call; 213, 214; 213, 215; 214, identifier:range; 215, argument_list; 215, 216; 216, call; 216, 217; 216, 218; 217, identifier:len; 218, argument_list; 218, 219; 219, identifier:g; 220, block; 220, 221; 221, if_statement; 221, 222; 221, 227; 222, comparison_operator:is; 222, 223; 222, 226; 223, subscript; 223, 224; 223, 225; 224, identifier:g; 225, identifier:k; 226, None; 227, block; 227, 228; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:inflection; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:inflection; 234, identifier:replace; 235, argument_list; 235, 236; 235, 244; 236, binary_operator:+; 236, 237; 236, 238; 237, string:'\\'; 238, call; 238, 239; 238, 240; 239, identifier:str; 240, argument_list; 240, 241; 241, binary_operator:+; 241, 242; 241, 243; 242, identifier:k; 243, integer:1; 244, string:''; 245, return_statement; 245, 246; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:suffix; 249, identifier:sub; 250, argument_list; 250, 251; 250, 252; 251, identifier:inflection; 252, identifier:word; 253, return_statement; 253, 254; 254, identifier:word
def singularize(word, pos=NOUN, custom=None): """ Returns the singular of a given word. """ if custom and word in custom: return custom[word] # Recurse compound words (e.g. mothers-in-law). if "-" in word: w = word.split("-") if len(w) > 1 and w[1] in plural_prepositions: return singularize(w[0], pos, custom) + "-" + "-".join(w[1:]) # dogs' => dog's if word.endswith("'"): return singularize(word[:-1]) + "'s" w = word.lower() for x in singular_uninflected: if x.endswith(w): return word for x in singular_uncountable: if x.endswith(w): return word for x in singular_ie: if w.endswith(x + "s"): return w[:-1] for x in singular_irregular: if w.endswith(x): return re.sub('(?i)' + x + '$', singular_irregular[x], word) for suffix, inflection in singular_rules: m = suffix.search(word) g = m and m.groups() or [] if m: for k in range(len(g)): if g[k] is None: inflection = inflection.replace('\\' + str(k + 1), '') return suffix.sub(inflection, word) return word
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 37; 1, 39; 2, function_name:login_user; 3, parameters; 3, 4; 3, 5; 3, 9; 3, 18; 3, 27; 3, 32; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:user; 7, type; 7, 8; 8, identifier:User; 9, typed_default_parameter; 9, 10; 9, 11; 9, 17; 10, identifier:remember; 11, type; 11, 12; 12, generic_type; 12, 13; 12, 14; 13, identifier:Optional; 14, type_parameter; 14, 15; 15, type; 15, 16; 16, identifier:bool; 17, None; 18, typed_default_parameter; 18, 19; 18, 20; 18, 26; 19, identifier:duration; 20, type; 20, 21; 21, generic_type; 21, 22; 21, 23; 22, identifier:Optional; 23, type_parameter; 23, 24; 24, type; 24, 25; 25, identifier:timedelta; 26, None; 27, typed_default_parameter; 27, 28; 27, 29; 27, 31; 28, identifier:force; 29, type; 29, 30; 30, identifier:bool; 31, False; 32, typed_default_parameter; 32, 33; 32, 34; 32, 36; 33, identifier:fresh; 34, type; 34, 35; 35, identifier:bool; 36, True; 37, type; 37, 38; 38, identifier:bool; 39, block; 39, 40; 39, 42; 39, 102; 39, 116; 39, 122; 39, 134; 39, 147; 39, 188; 39, 196; 39, 210; 39, 229; 40, expression_statement; 40, 41; 41, comment; 42, if_statement; 42, 43; 42, 45; 43, not_operator; 43, 44; 44, identifier:force; 45, block; 45, 46; 45, 60; 45, 88; 46, if_statement; 46, 47; 46, 51; 47, not_operator; 47, 48; 48, attribute; 48, 49; 48, 50; 49, identifier:user; 50, identifier:active; 51, block; 51, 52; 52, raise_statement; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:AuthenticationError; 55, argument_list; 55, 56; 56, call; 56, 57; 56, 58; 57, identifier:_; 58, argument_list; 58, 59; 59, string:'flask_unchained.bundles.security:error.disabled_account'; 60, if_statement; 60, 61; 60, 79; 61, parenthesized_expression; 61, 62; 62, boolean_operator:and; 62, 63; 62, 73; 63, boolean_operator:and; 63, 64; 63, 68; 64, not_operator; 64, 65; 65, attribute; 65, 66; 65, 67; 66, identifier:user; 67, identifier:confirmed_at; 68, attribute; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:security; 72, identifier:confirmable; 73, not_operator; 73, 74; 74, attribute; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:security; 78, identifier:login_without_confirmation; 79, block; 79, 80; 80, raise_statement; 80, 81; 81, call; 81, 82; 81, 83; 82, identifier:AuthenticationError; 83, argument_list; 83, 84; 84, call; 84, 85; 84, 86; 85, identifier:_; 86, argument_list; 86, 87; 87, string:'flask_unchained.bundles.security:error.confirmation_required'; 88, if_statement; 88, 89; 88, 93; 89, not_operator; 89, 90; 90, attribute; 90, 91; 90, 92; 91, identifier:user; 92, identifier:password; 93, block; 93, 94; 94, raise_statement; 94, 95; 95, call; 95, 96; 95, 97; 96, identifier:AuthenticationError; 97, argument_list; 97, 98; 98, call; 98, 99; 98, 100; 99, identifier:_; 100, argument_list; 100, 101; 101, string:'flask_unchained.bundles.security:error.password_not_set'; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 107; 104, subscript; 104, 105; 104, 106; 105, identifier:session; 106, string:'user_id'; 107, call; 107, 108; 107, 109; 108, identifier:getattr; 109, argument_list; 109, 110; 109, 111; 110, identifier:user; 111, attribute; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:user; 114, identifier:Meta; 115, identifier:pk; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 121; 118, subscript; 118, 119; 118, 120; 119, identifier:session; 120, string:'_fresh'; 121, identifier:fresh; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 127; 124, subscript; 124, 125; 124, 126; 125, identifier:session; 126, string:'_id'; 127, call; 127, 128; 127, 133; 128, attribute; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:app; 131, identifier:login_manager; 132, identifier:_session_identifier_generator; 133, argument_list; 134, if_statement; 134, 135; 134, 138; 135, comparison_operator:is; 135, 136; 135, 137; 136, identifier:remember; 137, None; 138, block; 138, 139; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:remember; 142, attribute; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:app; 145, identifier:config; 146, identifier:SECURITY_DEFAULT_REMEMBER_ME; 147, if_statement; 147, 148; 147, 149; 148, identifier:remember; 149, block; 149, 150; 149, 156; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 155; 152, subscript; 152, 153; 152, 154; 153, identifier:session; 154, string:'remember'; 155, string:'set'; 156, if_statement; 156, 157; 156, 160; 157, comparison_operator:is; 157, 158; 157, 159; 158, identifier:duration; 159, None; 160, block; 160, 161; 161, try_statement; 161, 162; 161, 173; 162, block; 162, 163; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 168; 165, subscript; 165, 166; 165, 167; 166, identifier:session; 167, string:'remember_seconds'; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:duration; 171, identifier:total_seconds; 172, argument_list; 173, except_clause; 173, 174; 173, 175; 174, identifier:AttributeError; 175, block; 175, 176; 176, raise_statement; 176, 177; 177, call; 177, 178; 177, 179; 178, identifier:Exception; 179, argument_list; 179, 180; 180, call; 180, 181; 180, 186; 181, attribute; 181, 182; 181, 185; 182, concatenated_string; 182, 183; 182, 184; 183, string:'duration must be a datetime.timedelta, '; 184, string:'instead got: {0}'; 185, identifier:format; 186, argument_list; 186, 187; 187, identifier:duration; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 195; 190, attribute; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:_request_ctx_stack; 193, identifier:top; 194, identifier:user; 195, identifier:user; 196, expression_statement; 196, 197; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:user_logged_in; 200, identifier:send; 201, argument_list; 201, 202; 201, 207; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:app; 205, identifier:_get_current_object; 206, argument_list; 207, keyword_argument; 207, 208; 207, 209; 208, identifier:user; 209, identifier:user; 210, expression_statement; 210, 211; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:identity_changed; 214, identifier:send; 215, argument_list; 215, 216; 215, 221; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:app; 219, identifier:_get_current_object; 220, argument_list; 221, keyword_argument; 221, 222; 221, 223; 222, identifier:identity; 223, call; 223, 224; 223, 225; 224, identifier:Identity; 225, argument_list; 225, 226; 226, attribute; 226, 227; 226, 228; 227, identifier:user; 228, identifier:id; 229, return_statement; 229, 230; 230, True
def login_user(self, user: User, remember: Optional[bool] = None, duration: Optional[timedelta] = None, force: bool = False, fresh: bool = True, ) -> bool: """ Logs a user in. You should pass the actual user object to this. If the user's `active` property is ``False``, they will not be logged in unless `force` is ``True``. This will return ``True`` if the log in attempt succeeds, and ``False`` if it fails (i.e. because the user is inactive). :param user: The user object to log in. :type user: object :param remember: Whether to remember the user after their session expires. Defaults to ``False``. :type remember: bool :param duration: The amount of time before the remember cookie expires. If ``None`` the value set in the settings is used. Defaults to ``None``. :type duration: :class:`datetime.timedelta` :param force: If the user is inactive, setting this to ``True`` will log them in regardless. Defaults to ``False``. :type force: bool :param fresh: setting this to ``False`` will log in the user with a session marked as not "fresh". Defaults to ``True``. :type fresh: bool """ if not force: if not user.active: raise AuthenticationError( _('flask_unchained.bundles.security:error.disabled_account')) if (not user.confirmed_at and self.security.confirmable and not self.security.login_without_confirmation): raise AuthenticationError( _('flask_unchained.bundles.security:error.confirmation_required')) if not user.password: raise AuthenticationError( _('flask_unchained.bundles.security:error.password_not_set')) session['user_id'] = getattr(user, user.Meta.pk) session['_fresh'] = fresh session['_id'] = app.login_manager._session_identifier_generator() if remember is None: remember = app.config.SECURITY_DEFAULT_REMEMBER_ME if remember: session['remember'] = 'set' if duration is not None: try: session['remember_seconds'] = duration.total_seconds() except AttributeError: raise Exception('duration must be a datetime.timedelta, ' 'instead got: {0}'.format(duration)) _request_ctx_stack.top.user = user user_logged_in.send(app._get_current_object(), user=user) identity_changed.send(app._get_current_object(), identity=Identity(user.id)) return True
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 70; 1, 78; 2, function_name:url_for; 3, parameters; 3, 4; 3, 8; 3, 17; 3, 32; 3, 41; 3, 50; 3, 59; 3, 68; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:endpoint_or_url_or_config_key; 6, type; 6, 7; 7, identifier:str; 8, typed_default_parameter; 8, 9; 8, 10; 8, 16; 9, identifier:_anchor; 10, type; 10, 11; 11, generic_type; 11, 12; 11, 13; 12, identifier:Optional; 13, type_parameter; 13, 14; 14, type; 14, 15; 15, identifier:str; 16, None; 17, typed_default_parameter; 17, 18; 17, 19; 17, 31; 18, identifier:_cls; 19, type; 19, 20; 20, generic_type; 20, 21; 20, 22; 21, identifier:Optional; 22, type_parameter; 22, 23; 23, type; 23, 24; 24, generic_type; 24, 25; 24, 26; 25, identifier:Union; 26, type_parameter; 26, 27; 26, 29; 27, type; 27, 28; 28, identifier:object; 29, type; 29, 30; 30, identifier:type; 31, None; 32, typed_default_parameter; 32, 33; 32, 34; 32, 40; 33, identifier:_external; 34, type; 34, 35; 35, generic_type; 35, 36; 35, 37; 36, identifier:Optional; 37, type_parameter; 37, 38; 38, type; 38, 39; 39, identifier:bool; 40, False; 41, typed_default_parameter; 41, 42; 41, 43; 41, 49; 42, identifier:_external_host; 43, type; 43, 44; 44, generic_type; 44, 45; 44, 46; 45, identifier:Optional; 46, type_parameter; 46, 47; 47, type; 47, 48; 48, identifier:str; 49, None; 50, typed_default_parameter; 50, 51; 50, 52; 50, 58; 51, identifier:_method; 52, type; 52, 53; 53, generic_type; 53, 54; 53, 55; 54, identifier:Optional; 55, type_parameter; 55, 56; 56, type; 56, 57; 57, identifier:str; 58, None; 59, typed_default_parameter; 59, 60; 59, 61; 59, 67; 60, identifier:_scheme; 61, type; 61, 62; 62, generic_type; 62, 63; 62, 64; 63, identifier:Optional; 64, type_parameter; 64, 65; 65, type; 65, 66; 66, identifier:str; 67, None; 68, dictionary_splat_pattern; 68, 69; 69, identifier:values; 70, type; 70, 71; 71, generic_type; 71, 72; 71, 73; 72, identifier:Union; 73, type_parameter; 73, 74; 73, 76; 74, type; 74, 75; 75, identifier:str; 76, type; 76, 77; 77, None; 78, block; 78, 79; 78, 81; 78, 85; 78, 86; 78, 106; 78, 121; 78, 122; 78, 132; 78, 155; 78, 156; 78, 203; 78, 204; 79, expression_statement; 79, 80; 80, comment; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:what; 84, identifier:endpoint_or_url_or_config_key; 85, comment; 86, if_statement; 86, 87; 86, 94; 87, boolean_operator:and; 87, 88; 87, 89; 88, identifier:what; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:what; 92, identifier:isupper; 93, argument_list; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:what; 98, call; 98, 99; 98, 104; 99, attribute; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:current_app; 102, identifier:config; 103, identifier:get; 104, argument_list; 104, 105; 105, identifier:what; 106, if_statement; 106, 107; 106, 112; 107, call; 107, 108; 107, 109; 108, identifier:isinstance; 109, argument_list; 109, 110; 109, 111; 110, identifier:what; 111, identifier:LocalProxy; 112, block; 112, 113; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:what; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:what; 119, identifier:_get_current_object; 120, argument_list; 121, comment; 122, if_statement; 122, 123; 122, 129; 123, boolean_operator:or; 123, 124; 123, 126; 124, not_operator; 124, 125; 125, identifier:what; 126, comparison_operator:in; 126, 127; 126, 128; 127, string:'/'; 128, identifier:what; 129, block; 129, 130; 130, return_statement; 130, 131; 131, identifier:what; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:flask_url_for_kwargs; 135, call; 135, 136; 135, 137; 136, identifier:dict; 137, argument_list; 137, 138; 137, 141; 137, 144; 137, 147; 137, 150; 137, 153; 138, keyword_argument; 138, 139; 138, 140; 139, identifier:_anchor; 140, identifier:_anchor; 141, keyword_argument; 141, 142; 141, 143; 142, identifier:_external; 143, identifier:_external; 144, keyword_argument; 144, 145; 144, 146; 145, identifier:_external_host; 146, identifier:_external_host; 147, keyword_argument; 147, 148; 147, 149; 148, identifier:_method; 149, identifier:_method; 150, keyword_argument; 150, 151; 150, 152; 151, identifier:_scheme; 152, identifier:_scheme; 153, dictionary_splat; 153, 154; 154, identifier:values; 155, comment; 156, if_statement; 156, 157; 156, 162; 157, boolean_operator:and; 157, 158; 157, 159; 158, identifier:_cls; 159, comparison_operator:not; 159, 160; 159, 161; 160, string:'.'; 161, identifier:what; 162, block; 162, 163; 162, 171; 162, 180; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:controller_routes; 166, call; 166, 167; 166, 168; 167, identifier:getattr; 168, argument_list; 168, 169; 168, 170; 169, identifier:_cls; 170, identifier:CONTROLLER_ROUTES_ATTR; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:method_routes; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:controller_routes; 177, identifier:get; 178, argument_list; 178, 179; 179, identifier:what; 180, try_statement; 180, 181; 180, 193; 181, block; 181, 182; 182, return_statement; 182, 183; 183, call; 183, 184; 183, 185; 184, identifier:_url_for; 185, argument_list; 185, 186; 185, 191; 186, attribute; 186, 187; 186, 190; 187, subscript; 187, 188; 187, 189; 188, identifier:method_routes; 189, integer:0; 190, identifier:endpoint; 191, dictionary_splat; 191, 192; 192, identifier:flask_url_for_kwargs; 193, except_clause; 193, 194; 193, 201; 194, tuple; 194, 195; 194, 196; 194, 197; 194, 198; 194, 199; 194, 200; 195, identifier:BuildError; 196, comment; 197, identifier:IndexError; 198, comment; 199, identifier:TypeError; 200, comment; 201, block; 201, 202; 202, pass_statement; 203, comment; 204, return_statement; 204, 205; 205, call; 205, 206; 205, 207; 206, identifier:_url_for; 207, argument_list; 207, 208; 207, 209; 208, identifier:what; 209, dictionary_splat; 209, 210; 210, identifier:flask_url_for_kwargs
def url_for(endpoint_or_url_or_config_key: str, _anchor: Optional[str] = None, _cls: Optional[Union[object, type]] = None, _external: Optional[bool] = False, _external_host: Optional[str] = None, _method: Optional[str] = None, _scheme: Optional[str] = None, **values, ) -> Union[str, None]: """ An improved version of flask's url_for function :param endpoint_or_url_or_config_key: what to lookup. it can be an endpoint name, an app config key, or an already-formed url. if _cls is specified, it also accepts a method name. :param values: the variable arguments of the URL rule :param _anchor: if provided this is added as anchor to the URL. :param _cls: if specified, can also pass a method name as the first argument :param _external: if set to ``True``, an absolute URL is generated. Server address can be changed via ``SERVER_NAME`` configuration variable which defaults to `localhost`. :param _external_host: if specified, the host of an external server to generate urls for (eg https://example.com or localhost:8888) :param _method: if provided this explicitly specifies an HTTP method. :param _scheme: a string specifying the desired URL scheme. The `_external` parameter must be set to ``True`` or a :exc:`ValueError` is raised. The default behavior uses the same scheme as the current request, or ``PREFERRED_URL_SCHEME`` from the :ref:`app configuration <config>` if no request context is available. As of Werkzeug 0.10, this also can be set to an empty string to build protocol-relative URLs. """ what = endpoint_or_url_or_config_key # if what is a config key if what and what.isupper(): what = current_app.config.get(what) if isinstance(what, LocalProxy): what = what._get_current_object() # if we already have a url (or an invalid value, eg None) if not what or '/' in what: return what flask_url_for_kwargs = dict(_anchor=_anchor, _external=_external, _external_host=_external_host, _method=_method, _scheme=_scheme, **values) # check if it's a class method name, and try that endpoint if _cls and '.' not in what: controller_routes = getattr(_cls, CONTROLLER_ROUTES_ATTR) method_routes = controller_routes.get(what) try: return _url_for(method_routes[0].endpoint, **flask_url_for_kwargs) except ( BuildError, # url not found IndexError, # method_routes[0] is out-of-range TypeError, # method_routes is None ): pass # what must be an endpoint return _url_for(what, **flask_url_for_kwargs)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 93; 1, 95; 2, function_name:redirect; 3, parameters; 3, 4; 3, 13; 3, 22; 3, 31; 3, 40; 3, 55; 3, 64; 3, 73; 3, 82; 3, 91; 4, typed_default_parameter; 4, 5; 4, 6; 4, 12; 5, identifier:where; 6, type; 6, 7; 7, generic_type; 7, 8; 7, 9; 8, identifier:Optional; 9, type_parameter; 9, 10; 10, type; 10, 11; 11, identifier:str; 12, None; 13, typed_default_parameter; 13, 14; 13, 15; 13, 21; 14, identifier:default; 15, type; 15, 16; 16, generic_type; 16, 17; 16, 18; 17, identifier:Optional; 18, type_parameter; 18, 19; 19, type; 19, 20; 20, identifier:str; 21, None; 22, typed_default_parameter; 22, 23; 22, 24; 22, 30; 23, identifier:override; 24, type; 24, 25; 25, generic_type; 25, 26; 25, 27; 26, identifier:Optional; 27, type_parameter; 27, 28; 28, type; 28, 29; 29, identifier:str; 30, None; 31, typed_default_parameter; 31, 32; 31, 33; 31, 39; 32, identifier:_anchor; 33, type; 33, 34; 34, generic_type; 34, 35; 34, 36; 35, identifier:Optional; 36, type_parameter; 36, 37; 37, type; 37, 38; 38, identifier:str; 39, None; 40, typed_default_parameter; 40, 41; 40, 42; 40, 54; 41, identifier:_cls; 42, type; 42, 43; 43, generic_type; 43, 44; 43, 45; 44, identifier:Optional; 45, type_parameter; 45, 46; 46, type; 46, 47; 47, generic_type; 47, 48; 47, 49; 48, identifier:Union; 49, type_parameter; 49, 50; 49, 52; 50, type; 50, 51; 51, identifier:object; 52, type; 52, 53; 53, identifier:type; 54, None; 55, typed_default_parameter; 55, 56; 55, 57; 55, 63; 56, identifier:_external; 57, type; 57, 58; 58, generic_type; 58, 59; 58, 60; 59, identifier:Optional; 60, type_parameter; 60, 61; 61, type; 61, 62; 62, identifier:bool; 63, False; 64, typed_default_parameter; 64, 65; 64, 66; 64, 72; 65, identifier:_external_host; 66, type; 66, 67; 67, generic_type; 67, 68; 67, 69; 68, identifier:Optional; 69, type_parameter; 69, 70; 70, type; 70, 71; 71, identifier:str; 72, None; 73, typed_default_parameter; 73, 74; 73, 75; 73, 81; 74, identifier:_method; 75, type; 75, 76; 76, generic_type; 76, 77; 76, 78; 77, identifier:Optional; 78, type_parameter; 78, 79; 79, type; 79, 80; 80, identifier:str; 81, None; 82, typed_default_parameter; 82, 83; 82, 84; 82, 90; 83, identifier:_scheme; 84, type; 84, 85; 85, generic_type; 85, 86; 85, 87; 86, identifier:Optional; 87, type_parameter; 87, 88; 88, type; 88, 89; 89, identifier:str; 90, None; 91, dictionary_splat_pattern; 91, 92; 92, identifier:values; 93, type; 93, 94; 94, identifier:Response; 95, block; 95, 96; 95, 98; 95, 121; 95, 151; 95, 169; 95, 187; 95, 206; 95, 222; 96, expression_statement; 96, 97; 97, comment; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:flask_url_for_kwargs; 101, call; 101, 102; 101, 103; 102, identifier:dict; 103, argument_list; 103, 104; 103, 107; 103, 110; 103, 113; 103, 116; 103, 119; 104, keyword_argument; 104, 105; 104, 106; 105, identifier:_anchor; 106, identifier:_anchor; 107, keyword_argument; 107, 108; 107, 109; 108, identifier:_external; 109, identifier:_external; 110, keyword_argument; 110, 111; 110, 112; 111, identifier:_external_host; 112, identifier:_external_host; 113, keyword_argument; 113, 114; 113, 115; 114, identifier:_method; 115, identifier:_method; 116, keyword_argument; 116, 117; 116, 118; 117, identifier:_scheme; 118, identifier:_scheme; 119, dictionary_splat; 119, 120; 120, identifier:values; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:urls; 124, list:[url_for(request.args.get('next'), **flask_url_for_kwargs), url_for(request.form.get('next'), **flask_url_for_kwargs)]; 124, 125; 124, 138; 125, call; 125, 126; 125, 127; 126, identifier:url_for; 127, argument_list; 127, 128; 127, 136; 128, call; 128, 129; 128, 134; 129, attribute; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:request; 132, identifier:args; 133, identifier:get; 134, argument_list; 134, 135; 135, string:'next'; 136, dictionary_splat; 136, 137; 137, identifier:flask_url_for_kwargs; 138, call; 138, 139; 138, 140; 139, identifier:url_for; 140, argument_list; 140, 141; 140, 149; 141, call; 141, 142; 141, 147; 142, attribute; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:request; 145, identifier:form; 146, identifier:get; 147, argument_list; 147, 148; 148, string:'next'; 149, dictionary_splat; 149, 150; 150, identifier:flask_url_for_kwargs; 151, if_statement; 151, 152; 151, 153; 152, identifier:where; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:urls; 158, identifier:append; 159, argument_list; 159, 160; 160, call; 160, 161; 160, 162; 161, identifier:url_for; 162, argument_list; 162, 163; 162, 164; 162, 167; 163, identifier:where; 164, keyword_argument; 164, 165; 164, 166; 165, identifier:_cls; 166, identifier:_cls; 167, dictionary_splat; 167, 168; 168, identifier:flask_url_for_kwargs; 169, if_statement; 169, 170; 169, 171; 170, identifier:default; 171, block; 171, 172; 172, expression_statement; 172, 173; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:urls; 176, identifier:append; 177, argument_list; 177, 178; 178, call; 178, 179; 178, 180; 179, identifier:url_for; 180, argument_list; 180, 181; 180, 182; 180, 185; 181, identifier:default; 182, keyword_argument; 182, 183; 182, 184; 183, identifier:_cls; 184, identifier:_cls; 185, dictionary_splat; 185, 186; 186, identifier:flask_url_for_kwargs; 187, if_statement; 187, 188; 187, 189; 188, identifier:override; 189, block; 189, 190; 190, expression_statement; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:urls; 194, identifier:insert; 195, argument_list; 195, 196; 195, 197; 196, integer:0; 197, call; 197, 198; 197, 199; 198, identifier:url_for; 199, argument_list; 199, 200; 199, 201; 199, 204; 200, identifier:override; 201, keyword_argument; 201, 202; 201, 203; 202, identifier:_cls; 203, identifier:_cls; 204, dictionary_splat; 204, 205; 205, identifier:flask_url_for_kwargs; 206, for_statement; 206, 207; 206, 208; 206, 209; 207, identifier:url; 208, identifier:urls; 209, block; 209, 210; 210, if_statement; 210, 211; 210, 216; 211, call; 211, 212; 211, 213; 212, identifier:_validate_redirect_url; 213, argument_list; 213, 214; 213, 215; 214, identifier:url; 215, identifier:_external_host; 216, block; 216, 217; 217, return_statement; 217, 218; 218, call; 218, 219; 218, 220; 219, identifier:flask_redirect; 220, argument_list; 220, 221; 221, identifier:url; 222, return_statement; 222, 223; 223, call; 223, 224; 223, 225; 224, identifier:flask_redirect; 225, argument_list; 225, 226; 226, string:'/'
def redirect(where: Optional[str] = None, default: Optional[str] = None, override: Optional[str] = None, _anchor: Optional[str] = None, _cls: Optional[Union[object, type]] = None, _external: Optional[bool] = False, _external_host: Optional[str] = None, _method: Optional[str] = None, _scheme: Optional[str] = None, **values, ) -> Response: """ An improved version of flask's redirect function :param where: A URL, endpoint, or config key name to redirect to :param default: A URL, endpoint, or config key name to redirect to if ``where`` is invalid :param override: explicitly redirect to a URL, endpoint, or config key name (takes precedence over the ``next`` value in query strings or forms) :param values: the variable arguments of the URL rule :param _anchor: if provided this is added as anchor to the URL. :param _cls: if specified, allows a method name to be passed to where, default, and/or override :param _external: if set to ``True``, an absolute URL is generated. Server address can be changed via ``SERVER_NAME`` configuration variable which defaults to `localhost`. :param _external_host: if specified, the host of an external server to generate urls for (eg https://example.com or localhost:8888) :param _method: if provided this explicitly specifies an HTTP method. :param _scheme: a string specifying the desired URL scheme. The `_external` parameter must be set to ``True`` or a :exc:`ValueError` is raised. The default behavior uses the same scheme as the current request, or ``PREFERRED_URL_SCHEME`` from the :ref:`app configuration <config>` if no request context is available. As of Werkzeug 0.10, this also can be set to an empty string to build protocol-relative URLs. """ flask_url_for_kwargs = dict(_anchor=_anchor, _external=_external, _external_host=_external_host, _method=_method, _scheme=_scheme, **values) urls = [url_for(request.args.get('next'), **flask_url_for_kwargs), url_for(request.form.get('next'), **flask_url_for_kwargs)] if where: urls.append(url_for(where, _cls=_cls, **flask_url_for_kwargs)) if default: urls.append(url_for(default, _cls=_cls, **flask_url_for_kwargs)) if override: urls.insert(0, url_for(override, _cls=_cls, **flask_url_for_kwargs)) for url in urls: if _validate_redirect_url(url, _external_host): return flask_redirect(url) return flask_redirect('/')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:auth_required; 3, parameters; 3, 4; 3, 7; 4, default_parameter; 4, 5; 4, 6; 5, identifier:decorated_fn; 6, None; 7, dictionary_splat_pattern; 7, 8; 8, identifier:role_rules; 9, block; 9, 10; 9, 12; 9, 16; 9, 20; 9, 78; 9, 123; 9, 136; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:required_roles; 15, list:[]; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:one_of_roles; 19, list:[]; 20, if_statement; 20, 21; 20, 29; 21, not_operator; 21, 22; 22, parenthesized_expression; 22, 23; 23, boolean_operator:and; 23, 24; 23, 25; 24, identifier:decorated_fn; 25, call; 25, 26; 25, 27; 26, identifier:callable; 27, argument_list; 27, 28; 28, identifier:decorated_fn; 29, block; 29, 30; 29, 67; 30, if_statement; 30, 31; 30, 38; 30, 44; 30, 56; 31, boolean_operator:and; 31, 32; 31, 35; 32, comparison_operator:in; 32, 33; 32, 34; 33, string:'role'; 34, identifier:role_rules; 35, comparison_operator:in; 35, 36; 35, 37; 36, string:'roles'; 37, identifier:role_rules; 38, block; 38, 39; 39, raise_statement; 39, 40; 40, call; 40, 41; 40, 42; 41, identifier:RuntimeError; 42, argument_list; 42, 43; 43, string:'specify only one of `role` or `roles` kwargs'; 44, elif_clause; 44, 45; 44, 48; 45, comparison_operator:in; 45, 46; 45, 47; 46, string:'role'; 47, identifier:role_rules; 48, block; 48, 49; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:required_roles; 52, list:[role_rules['role']]; 52, 53; 53, subscript; 53, 54; 53, 55; 54, identifier:role_rules; 55, string:'role'; 56, elif_clause; 56, 57; 56, 60; 57, comparison_operator:in; 57, 58; 57, 59; 58, string:'roles'; 59, identifier:role_rules; 60, block; 60, 61; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:required_roles; 64, subscript; 64, 65; 64, 66; 65, identifier:role_rules; 66, string:'roles'; 67, if_statement; 67, 68; 67, 71; 68, comparison_operator:in; 68, 69; 68, 70; 69, string:'one_of'; 70, identifier:role_rules; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:one_of_roles; 75, subscript; 75, 76; 75, 77; 76, identifier:role_rules; 77, string:'one_of'; 78, function_definition; 78, 79; 78, 80; 78, 82; 79, function_name:wrapper; 80, parameters; 80, 81; 81, identifier:fn; 82, block; 82, 83; 82, 121; 83, decorated_definition; 83, 84; 83, 89; 83, 93; 83, 99; 83, 105; 84, decorator; 84, 85; 85, call; 85, 86; 85, 87; 86, identifier:wraps; 87, argument_list; 87, 88; 88, identifier:fn; 89, decorator; 89, 90; 90, call; 90, 91; 90, 92; 91, identifier:_auth_required; 92, argument_list; 93, decorator; 93, 94; 94, call; 94, 95; 94, 96; 95, identifier:roles_required; 96, argument_list; 96, 97; 97, list_splat; 97, 98; 98, identifier:required_roles; 99, decorator; 99, 100; 100, call; 100, 101; 100, 102; 101, identifier:roles_accepted; 102, argument_list; 102, 103; 103, list_splat; 103, 104; 104, identifier:one_of_roles; 105, function_definition; 105, 106; 105, 107; 105, 112; 106, function_name:decorated; 107, parameters; 107, 108; 107, 110; 108, list_splat_pattern; 108, 109; 109, identifier:args; 110, dictionary_splat_pattern; 110, 111; 111, identifier:kwargs; 112, block; 112, 113; 113, return_statement; 113, 114; 114, call; 114, 115; 114, 116; 115, identifier:fn; 116, argument_list; 116, 117; 116, 119; 117, list_splat; 117, 118; 118, identifier:args; 119, dictionary_splat; 119, 120; 120, identifier:kwargs; 121, return_statement; 121, 122; 122, identifier:decorated; 123, if_statement; 123, 124; 123, 130; 124, boolean_operator:and; 124, 125; 124, 126; 125, identifier:decorated_fn; 126, call; 126, 127; 126, 128; 127, identifier:callable; 128, argument_list; 128, 129; 129, identifier:decorated_fn; 130, block; 130, 131; 131, return_statement; 131, 132; 132, call; 132, 133; 132, 134; 133, identifier:wrapper; 134, argument_list; 134, 135; 135, identifier:decorated_fn; 136, return_statement; 136, 137; 137, identifier:wrapper
def auth_required(decorated_fn=None, **role_rules): """ Decorator for requiring an authenticated user, optionally with roles. Roles are passed as keyword arguments, like so:: @auth_required(role='REQUIRE_THIS_ONE_ROLE') @auth_required(roles=['REQUIRE', 'ALL', 'OF', 'THESE', 'ROLES']) @auth_required(one_of=['EITHER_THIS_ROLE', 'OR_THIS_ONE']) One of role or roles kwargs can also be combined with one_of:: @auth_required(role='REQUIRED', one_of=['THIS', 'OR_THIS']) Aborts with ``HTTP 401: Unauthorized`` if no user is logged in, or ``HTTP 403: Forbidden`` if any of the specified role checks fail. """ required_roles = [] one_of_roles = [] if not (decorated_fn and callable(decorated_fn)): if 'role' in role_rules and 'roles' in role_rules: raise RuntimeError('specify only one of `role` or `roles` kwargs') elif 'role' in role_rules: required_roles = [role_rules['role']] elif 'roles' in role_rules: required_roles = role_rules['roles'] if 'one_of' in role_rules: one_of_roles = role_rules['one_of'] def wrapper(fn): @wraps(fn) @_auth_required() @roles_required(*required_roles) @roles_accepted(*one_of_roles) def decorated(*args, **kwargs): return fn(*args, **kwargs) return decorated if decorated_fn and callable(decorated_fn): return wrapper(decorated_fn) return wrapper
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:build_tree; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 14; 5, 26; 5, 351; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 12; 9, attribute; 9, 10; 9, 11; 10, identifier:self; 11, identifier:built; 12, block; 12, 13; 13, return_statement; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:doc_root; 19, call; 19, 20; 19, 25; 20, attribute; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:root; 24, identifier:element; 25, argument_list; 26, for_statement; 26, 27; 26, 28; 26, 33; 27, identifier:key; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:sorted_fields; 32, argument_list; 33, block; 33, 34; 33, 42; 33, 50; 34, if_statement; 34, 35; 34, 40; 35, comparison_operator:not; 35, 36; 35, 37; 36, identifier:key; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:_fields; 40, block; 40, 41; 41, continue_statement; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:field; 45, subscript; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:_fields; 49, identifier:key; 50, if_statement; 50, 51; 50, 56; 51, comparison_operator:!=; 51, 52; 51, 53; 52, identifier:field; 53, attribute; 53, 54; 53, 55; 54, identifier:self; 55, identifier:root; 56, block; 56, 57; 57, if_statement; 57, 58; 57, 63; 57, 101; 57, 228; 57, 285; 58, call; 58, 59; 58, 60; 59, identifier:isinstance; 60, argument_list; 60, 61; 60, 62; 61, identifier:field; 62, identifier:XmlModel; 63, block; 63, 64; 63, 70; 63, 90; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:field; 68, identifier:build_tree; 69, argument_list; 70, if_statement; 70, 71; 70, 88; 71, parenthesized_expression; 71, 72; 72, boolean_operator:and; 72, 73; 72, 80; 73, boolean_operator:and; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:self; 76, identifier:drop_empty; 77, attribute; 77, 78; 77, 79; 78, identifier:field; 79, identifier:drop_empty; 80, comparison_operator:==; 80, 81; 80, 87; 81, call; 81, 82; 81, 83; 82, identifier:len; 83, argument_list; 83, 84; 84, attribute; 84, 85; 84, 86; 85, identifier:field; 86, identifier:doc_root; 87, integer:0; 88, block; 88, 89; 89, continue_statement; 90, expression_statement; 90, 91; 91, call; 91, 92; 91, 97; 92, attribute; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:self; 95, identifier:doc_root; 96, identifier:append; 97, argument_list; 97, 98; 98, attribute; 98, 99; 98, 100; 99, identifier:field; 100, identifier:doc_root; 101, elif_clause; 101, 102; 101, 107; 101, 108; 101, 109; 102, call; 102, 103; 102, 104; 103, identifier:isinstance; 104, argument_list; 104, 105; 104, 106; 105, identifier:field; 106, identifier:list; 107, comment; 108, comment; 109, block; 109, 110; 110, for_statement; 110, 111; 110, 112; 110, 113; 111, identifier:item; 112, identifier:field; 113, block; 113, 114; 113, 224; 114, if_statement; 114, 115; 114, 120; 114, 151; 114, 190; 115, call; 115, 116; 115, 117; 116, identifier:isinstance; 117, argument_list; 117, 118; 117, 119; 118, identifier:item; 119, identifier:XmlField; 120, block; 120, 121; 120, 129; 120, 142; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:ele; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:item; 127, identifier:element; 128, argument_list; 129, if_statement; 129, 130; 129, 140; 130, boolean_operator:and; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:drop_empty; 134, comparison_operator:==; 134, 135; 134, 139; 135, call; 135, 136; 135, 137; 136, identifier:len; 137, argument_list; 137, 138; 138, identifier:ele; 139, integer:0; 140, block; 140, 141; 141, continue_statement; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 149; 144, attribute; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:self; 147, identifier:doc_root; 148, identifier:append; 149, argument_list; 149, 150; 150, identifier:ele; 151, elif_clause; 151, 152; 151, 157; 152, call; 152, 153; 152, 154; 153, identifier:isinstance; 154, argument_list; 154, 155; 154, 156; 155, identifier:item; 156, identifier:XmlModel; 157, block; 157, 158; 157, 164; 157, 179; 158, expression_statement; 158, 159; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:item; 162, identifier:build_tree; 163, argument_list; 164, if_statement; 164, 165; 164, 177; 165, boolean_operator:and; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:self; 168, identifier:drop_empty; 169, comparison_operator:==; 169, 170; 169, 176; 170, call; 170, 171; 170, 172; 171, identifier:len; 172, argument_list; 172, 173; 173, attribute; 173, 174; 173, 175; 174, identifier:item; 175, identifier:doc_root; 176, integer:0; 177, block; 177, 178; 178, continue_statement; 179, expression_statement; 179, 180; 180, call; 180, 181; 180, 186; 181, attribute; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:self; 184, identifier:doc_root; 185, identifier:append; 186, argument_list; 186, 187; 187, attribute; 187, 188; 187, 189; 188, identifier:item; 189, identifier:doc_root; 190, elif_clause; 190, 191; 190, 202; 191, call; 191, 192; 191, 193; 192, identifier:isinstance; 193, argument_list; 193, 194; 193, 195; 194, identifier:item; 195, tuple; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:six; 198, identifier:text_type; 199, attribute; 199, 200; 199, 201; 200, identifier:six; 201, identifier:string_types; 202, block; 202, 203; 202, 215; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:ele; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:etree; 209, identifier:fromstring; 210, argument_list; 210, 211; 211, call; 211, 212; 211, 213; 212, identifier:clean_xml; 213, argument_list; 213, 214; 214, identifier:item; 215, expression_statement; 215, 216; 216, call; 216, 217; 216, 222; 217, attribute; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:self; 220, identifier:doc_root; 221, identifier:append; 222, argument_list; 222, 223; 223, identifier:ele; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:item; 227, None; 228, elif_clause; 228, 229; 228, 245; 229, comparison_operator:==; 229, 230; 229, 240; 230, parenthesized_expression; 230, 231; 231, boolean_operator:or; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:field; 234, identifier:parent; 235, attribute; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:self; 238, identifier:root; 239, identifier:name; 240, attribute; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:self; 243, identifier:root; 244, identifier:name; 245, block; 245, 246; 245, 254; 245, 272; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:ele; 249, call; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:field; 252, identifier:element; 253, argument_list; 254, if_statement; 254, 255; 254, 270; 255, boolean_operator:and; 255, 256; 255, 266; 256, boolean_operator:and; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:self; 259, identifier:drop_empty; 260, comparison_operator:==; 260, 261; 260, 265; 261, call; 261, 262; 261, 263; 262, identifier:len; 263, argument_list; 263, 264; 264, identifier:ele; 265, integer:0; 266, not_operator; 266, 267; 267, attribute; 267, 268; 267, 269; 268, identifier:ele; 269, identifier:text; 270, block; 270, 271; 271, continue_statement; 272, expression_statement; 272, 273; 273, assignment; 273, 274; 273, 275; 274, identifier:ele; 275, call; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:field; 278, identifier:element; 279, argument_list; 279, 280; 280, keyword_argument; 280, 281; 280, 282; 281, identifier:parent; 282, attribute; 282, 283; 282, 284; 283, identifier:self; 284, identifier:doc_root; 285, else_clause; 285, 286; 286, block; 286, 287; 286, 306; 286, 349; 286, 350; 287, expression_statement; 287, 288; 288, assignment; 288, 289; 288, 290; 289, identifier:nodes; 290, list_comprehension; 290, 291; 290, 292; 291, identifier:n; 292, for_in_clause; 292, 293; 292, 294; 293, identifier:n; 294, call; 294, 295; 294, 300; 295, attribute; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:self; 298, identifier:doc_root; 299, identifier:iterdescendants; 300, argument_list; 300, 301; 301, keyword_argument; 301, 302; 301, 303; 302, identifier:tag; 303, attribute; 303, 304; 303, 305; 304, identifier:field; 305, identifier:parent; 306, if_statement; 306, 307; 306, 308; 307, identifier:nodes; 308, block; 308, 309; 308, 317; 308, 336; 309, expression_statement; 309, 310; 310, assignment; 310, 311; 310, 312; 311, identifier:ele; 312, call; 312, 313; 312, 316; 313, attribute; 313, 314; 313, 315; 314, identifier:field; 315, identifier:element; 316, argument_list; 317, if_statement; 317, 318; 317, 334; 318, parenthesized_expression; 318, 319; 319, boolean_operator:and; 319, 320; 319, 330; 320, boolean_operator:and; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:self; 323, identifier:drop_empty; 324, comparison_operator:==; 324, 325; 324, 329; 325, call; 325, 326; 325, 327; 326, identifier:len; 327, argument_list; 327, 328; 328, identifier:ele; 329, integer:0; 330, not_operator; 330, 331; 331, attribute; 331, 332; 331, 333; 332, identifier:ele; 333, identifier:text; 334, block; 334, 335; 335, continue_statement; 336, expression_statement; 336, 337; 337, assignment; 337, 338; 337, 339; 338, identifier:ele; 339, call; 339, 340; 339, 343; 340, attribute; 340, 341; 340, 342; 341, identifier:field; 342, identifier:element; 343, argument_list; 343, 344; 344, keyword_argument; 344, 345; 344, 346; 345, identifier:parent; 346, subscript; 346, 347; 346, 348; 347, identifier:nodes; 348, integer:0; 349, comment; 350, comment; 351, expression_statement; 351, 352; 352, assignment; 352, 353; 352, 356; 353, attribute; 353, 354; 353, 355; 354, identifier:self; 355, identifier:built; 356, True
def build_tree(self): """Bulids the tree with all the fields converted to Elements """ if self.built: return self.doc_root = self.root.element() for key in self.sorted_fields(): if key not in self._fields: continue field = self._fields[key] if field != self.root: if isinstance(field, XmlModel): field.build_tree() if (self.drop_empty and field.drop_empty and len(field.doc_root) == 0): continue self.doc_root.append(field.doc_root) elif isinstance(field, list): # we just allow XmlFields and XmlModels # Also xml as str for memory management for item in field: if isinstance(item, XmlField): ele = item.element() if self.drop_empty and len(ele) == 0: continue self.doc_root.append(ele) elif isinstance(item, XmlModel): item.build_tree() if self.drop_empty and len(item.doc_root) == 0: continue self.doc_root.append(item.doc_root) elif isinstance(item, (six.text_type, six.string_types)): ele = etree.fromstring(clean_xml(item)) self.doc_root.append(ele) item = None elif (field.parent or self.root.name) == self.root.name: ele = field.element() if self.drop_empty and len(ele) == 0 and not ele.text: continue ele = field.element(parent=self.doc_root) else: nodes = [n for n in self.doc_root.iterdescendants( tag=field.parent)] if nodes: ele = field.element() if (self.drop_empty and len(ele) == 0 and not ele.text): continue ele = field.element(parent=nodes[0]) #else: # raise RuntimeError("No parent found!") self.built = True