if layers:<EOL><INDENT>ctor = payload_type(self.type)[<NUM_LIT:0>]<EOL>if ctor:<EOL><INDENT>ctor = ctor<EOL>payload = self.payload<EOL>self.payload = ctor(payload, layers - <NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>

Given an Ethernet frame, determine the appropriate sub-protocol;
If layers is greater than zerol determine the type of the payload
and load the appropriate type of network packet. It is expected
that the payload be a hexified string. The layers argument determines
how many layers to descend while parsing the packet.


pack = None<EOL>try:<EOL><INDENT>pack = WiHelper.get_wifi_packet(frame, no_rtap)<EOL><DEDENT>except Exception as e:<EOL><INDENT>logging.exception(e)<EOL><DEDENT>return pack<EOL>

calls wifi packet discriminator and constructor.
    :frame: ctypes.Structure
    :no_rtap: Bool
    :return: packet object in success
    :return: int
        -1 on known error
    :return: int
        -2 on unknown error


_, packet = WiHelper._strip_rtap(frame)<EOL>frame_control = struct.unpack('<STR_LIT>', packet[:<NUM_LIT:2>])<EOL>cat = (frame_control[<NUM_LIT:0>] >> <NUM_LIT:2>) & <NUM_LIT><EOL>s_type = frame_control[<NUM_LIT:0>] >> <NUM_LIT:4><EOL>if cat not in _CATEGORIES_.keys():<EOL><INDENT>logging.warning("<STR_LIT>" % (cat))<EOL>return Unknown(frame, no_rtap)<EOL><DEDENT>if s_type not in _SUBTYPES_[cat].keys():<EOL><INDENT>logging.warning("<STR_LIT>" % (s_type, _CATEGORIES_[cat]))<EOL>return Unknown(frame, no_rtap)<EOL><DEDENT>if cat == <NUM_LIT:0>:<EOL><INDENT>if s_type == <NUM_LIT:4>:<EOL><INDENT>return ProbeReq(frame, no_rtap)<EOL><DEDENT>elif s_type == <NUM_LIT:5>:<EOL><INDENT>return ProbeResp(frame, no_rtap)<EOL><DEDENT>elif s_type == <NUM_LIT:8>:<EOL><INDENT>return Beacon(frame, no_rtap)<EOL><DEDENT>else:<EOL><INDENT>return Management(frame, no_rtap)<EOL><DEDENT><DEDENT>elif cat == <NUM_LIT:1>:<EOL><INDENT>if s_type == <NUM_LIT:11>:<EOL><INDENT>return RTS(frame, no_rtap)<EOL><DEDENT>elif s_type == <NUM_LIT:12>:<EOL><INDENT>return CTS(frame, no_rtap)<EOL><DEDENT>elif s_type == <NUM_LIT:9>:<EOL><INDENT>return BACK(frame, no_rtap)<EOL><DEDENT>else:<EOL><INDENT>return Control(frame, no_rtap)<EOL><DEDENT><DEDENT>elif cat == <NUM_LIT:2>:<EOL><INDENT>if s_type == <NUM_LIT:8>:<EOL><INDENT>return QosData(frame, no_rtap, parse_amsdu=True)<EOL><DEDENT>else:<EOL><INDENT>return Data(frame, no_rtap)<EOL><DEDENT><DEDENT>

Discriminates Wi-Fi packet and creates
        packet object.
        :frame: ctypes.Structure
        :no_rtap: Bool
        :return: obj
            Wi-Fi packet


rtap_len = WiHelper.__get_rtap_len(frame)<EOL>rtap = frame[:rtap_len]<EOL>packet = frame[rtap_len:]<EOL>return rtap, packet<EOL>

strip injected radiotap header.
        :return: ctypes.Structure
            radiotap header
        :return: ctypes.Structure
            actual layer 2 Wi-Fi payload


r_len = struct.unpack('<STR_LIT:H>', frame[<NUM_LIT:2>:<NUM_LIT:4>])<EOL>return r_len[<NUM_LIT:0>]<EOL>

parse length of radiotap header.
        :packet: ctypes.structure
        :return: int


super(Radiotap, self).__init__()<EOL>self._raw = {}   <EOL>self._bits = {}  <EOL>idx = <NUM_LIT:0><EOL>self._rtap = rtap_bytes<EOL>self.vers = Radiotap.strip_vers(self._rtap[idx:idx + <NUM_LIT:1>])<EOL>idx += <NUM_LIT:1><EOL>self.pad = Radiotap.strip_pad(self._rtap[idx:idx + <NUM_LIT:1>])<EOL>idx += <NUM_LIT:1><EOL>self.len = Radiotap.strip_len(self._rtap[idx:idx + <NUM_LIT:2>])<EOL>idx += <NUM_LIT:2><EOL>self.present, self.present_bits = Radiotap.strip_present(self._rtap[idx:idx + <NUM_LIT:4>])<EOL>idx += <NUM_LIT:4><EOL>if self.present.tsft:  <EOL><INDENT>idx, self.mactime = self.strip_tsft(idx)<EOL><DEDENT>if self.present.flags:  <EOL><INDENT>idx, self.flags = self.strip_flags(idx)<EOL><DEDENT>if self.present.rate:  <EOL><INDENT>idx, self.rate = self.strip_rate(idx)<EOL><DEDENT>if self.present.channel:  <EOL><INDENT>idx, self.chan = self.strip_chan(idx)<EOL><DEDENT>if self.present.fhss:  <EOL><INDENT>idx, self.fhss = self.strip_fhss(idx)<EOL><DEDENT>if self.present.dbm_antsignal:  <EOL><INDENT>idx, self.dbm_antsignal = self.strip_dbm_antsignal(idx)<EOL><DEDENT>if self.present.dbm_antnoise:  <EOL><INDENT>idx, self.dbm_antnoise = self.strip_dbm_antnoise(idx)<EOL><DEDENT>if self.present.lock_quality:  <EOL><INDENT>idx, self.lock_quality = self.strip_lock_quality(idx)<EOL><DEDENT>if self.present.tx_attenuation:  <EOL><INDENT>idx, self.tx_attenuation = self.strip_tx_attenuation(idx)<EOL><DEDENT>if self.present.db_tx_attenuation:  <EOL><INDENT>idx, self.db_tx_attenuation = self.strip_db_tx_attenuation(idx)<EOL><DEDENT>if self.present.dbm_tx_power:  <EOL><INDENT>idx, self.dbm_tx_power = self.strip_dbm_tx_power(idx)<EOL><DEDENT>if self.present.antenna:  <EOL><INDENT>idx, self.antenna = self.strip_antenna(idx)<EOL><DEDENT>if self.present.db_antsignal:  <EOL><INDENT>idx, self.db_antsignal = self.strip_db_antsignal(idx)<EOL><DEDENT>if self.present.db_antnoise:  <EOL><INDENT>idx, self.db_antnoise = self.strip_db_antnoise(idx)<EOL><DEDENT>if self.present.rxflags:  <EOL><INDENT>idx, self.rxflags = self.strip_rx_flags(idx)<EOL><DEDENT>if self.present.txflags:  <EOL><INDENT>idx, self.txflags = self.strip_tx_flags(idx)<EOL><DEDENT>if self.present.rts_retries:  <EOL><INDENT>idx, self.rts_retries = self.strip_rts_retries(idx)<EOL><DEDENT>if self.present.data_retries:  <EOL><INDENT>idx, self.data_retries = self.strip_data_retries(idx)<EOL><DEDENT>if self.present.xchannel:  <EOL><INDENT>idx, self.xchannel = self.strip_xchannel(idx)<EOL><DEDENT>if self.present.mcs:  <EOL><INDENT>idx, self.mcs = self.strip_mcs(idx)<EOL><DEDENT>if self.present.ampdu:  <EOL><INDENT>idx, self.ampdu = self.strip_ampdu(idx)<EOL><DEDENT>if self.present.vht:  <EOL><INDENT>idx, self.vht = self.strip_vht(idx)<EOL><DEDENT>self.prot_type = self.extract_protocol()<EOL>

Constructor method.
        :rtap_bytes: ctypes.Structure


return struct.unpack('<STR_LIT:B>', payload)[<NUM_LIT:0>]<EOL>

strip(1 byte) radiotap.version
        :payload: ctypes.Structure
        :return: int


strip(1 byte) radiotap.pad
        :payload: ctypes.Structure
        :return: int


return struct.unpack('<STR_LIT:H>', payload)[<NUM_LIT:0>]<EOL>

strip(2 byte) radiotap.length
        :payload: ctypes.Structure
        :return: int


present = collections.namedtuple(<EOL>'<STR_LIT>', ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'])<EOL>val = struct.unpack('<STR_LIT>', payload)[<NUM_LIT:0>]<EOL>bits = format(val, '<STR_LIT>')[::-<NUM_LIT:1>]<EOL>present.tsft = int(bits[<NUM_LIT:0>])               <EOL>present.flags = int(bits[<NUM_LIT:1>])              <EOL>present.rate = int(bits[<NUM_LIT:2>])               <EOL>present.channel = int(bits[<NUM_LIT:3>])            <EOL>present.fhss = int(bits[<NUM_LIT:4>])               <EOL>present.dbm_antsignal = int(bits[<NUM_LIT:5>])      <EOL>present.dbm_antnoise = int(bits[<NUM_LIT:6>])       <EOL>present.lock_quality = int(bits[<NUM_LIT:7>])       <EOL>present.tx_attenuation = int(bits[<NUM_LIT:8>])     <EOL>present.db_tx_attenuation = int(bits[<NUM_LIT:9>])  <EOL>present.dbm_tx_power = int(bits[<NUM_LIT:10>])      <EOL>present.antenna = int(bits[<NUM_LIT:11>])           <EOL>present.db_antsignal = int(bits[<NUM_LIT:12>])      <EOL>present.db_antnoise = int(bits[<NUM_LIT>])       <EOL>present.rxflags = int(bits[<NUM_LIT>])           <EOL>present.txflags = int(bits[<NUM_LIT:15>])           <EOL>present.rts_retries = int(bits[<NUM_LIT:16>])       <EOL>present.data_retries = int(bits[<NUM_LIT>])      <EOL>present.xchannel = int(bits[<NUM_LIT>])          <EOL>present.mcs = int(bits[<NUM_LIT>])               <EOL>present.ampdu = int(bits[<NUM_LIT:20>])             <EOL>present.vht = int(bits[<NUM_LIT>])               <EOL>present.rtap_ns = int(bits[<NUM_LIT>])           <EOL>present.ven_ns = int(bits[<NUM_LIT:30>])            <EOL>present.ext = int(bits[<NUM_LIT>])               <EOL>return present, bits<EOL>

strip(4 byte) radiotap.present. Those are flags that
        identify existence of incoming radiotap meta-data.
        :idx: int
        :return: str
        :return: namedtuple


idx = Radiotap.align(idx, <NUM_LIT:8>)<EOL>mactime, = struct.unpack_from('<STR_LIT>', self._rtap, idx)<EOL>return idx + <NUM_LIT:8>, mactime<EOL>

strip(8 byte) radiotap.mactime
        :idx: int
        :return: int
            idx
        :return: int
            mactime


flags = collections.namedtuple(<EOL>'<STR_LIT>', ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>'])<EOL>val, = struct.unpack_from('<STR_LIT>', self._rtap, idx)<EOL>bits = format(val, '<STR_LIT>')[::-<NUM_LIT:1>]<EOL>flags.cfp = int(bits[<NUM_LIT:0>])<EOL>flags.preamble = int(bits[<NUM_LIT:1>])<EOL>flags.wep = int(bits[<NUM_LIT:2>])<EOL>flags.fragmentation = int(bits[<NUM_LIT:3>])<EOL>flags.fcs = int(bits[<NUM_LIT:4>])<EOL>flags.datapad = int(bits[<NUM_LIT:5>])<EOL>flags.badfcs = int(bits[<NUM_LIT:6>])<EOL>flags.shortgi = int(bits[<NUM_LIT:7>])<EOL>return idx + <NUM_LIT:1>, flags<EOL>

strip(1 byte) radiotap.flags
        :idx: int
        :return: int
            idx
        :return: collections.namedtuple


val, = struct.unpack_from('<STR_LIT>', self._rtap, idx)<EOL>rate_unit = float(<NUM_LIT:1>) / <NUM_LIT:2>    <EOL>return idx + <NUM_LIT:1>, rate_unit * val<EOL>

strip(1 byte) radiotap.datarate
        note that, unit of this field is originally 0.5 Mbps
        :idx: int
        :return: int
            idx
        :return: double
            rate in terms of Mbps


chan = collections.namedtuple(<EOL>'<STR_LIT>', ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>'])<EOL>idx = Radiotap.align(idx, <NUM_LIT:2>)<EOL>freq, flags, = struct.unpack_from('<STR_LIT>', self._rtap, idx)<EOL>chan.freq = freq<EOL>bits = format(flags, '<STR_LIT>')[::-<NUM_LIT:1>]<EOL>chan.turbo = int(bits[<NUM_LIT:4>])<EOL>chan.cck = int(bits[<NUM_LIT:5>])<EOL>chan.ofdm = int(bits[<NUM_LIT:6>])<EOL>chan.two_g = int(bits[<NUM_LIT:7>])<EOL>chan.five_g = int(bits[<NUM_LIT:8>])<EOL>chan.passive = int(bits[<NUM_LIT:9>])<EOL>chan.dynamic = int(bits[<NUM_LIT:10>])<EOL>chan.gfsk = int(bits[<NUM_LIT:11>])<EOL>chan.gsm = int(bits[<NUM_LIT:12>])<EOL>chan.static_turbo = int(bits[<NUM_LIT>])<EOL>chan.half_rate = int(bits[<NUM_LIT>])<EOL>chan.quarter_rate = int(bits[<NUM_LIT:15>])<EOL>return idx + <NUM_LIT:4>, chan<EOL>

strip(2 byte) radiotap.channel.flags
        :idx: int
        :return: int
            idx
        :return: collections.namedtuple


fhss = collections.namedtuple('<STR_LIT>', ['<STR_LIT>', '<STR_LIT>'])<EOL>fhss.hopset, fhss.pattern, = struct.unpack_from('<STR_LIT>', self._rtap, idx)<EOL>return idx + <NUM_LIT:2>, fhss<EOL>

strip (2 byte) radiotap.fhss.hopset(1 byte) and
        radiotap.fhss.pattern(1 byte)
        :idx: int
        :return: int
            idx
        :return: collections.namedtuple


dbm_antsignal, = struct.unpack_from('<STR_LIT>', self._rtap, idx)<EOL>return idx + <NUM_LIT:1>, dbm_antsignal<EOL>

strip(1 byte) radiotap.dbm.ant_signal
        :idx: int
        :return: int
            idx
        :return: int


dbm_antnoise, = struct.unpack_from('<STR_LIT>', self._rtap, idx)<EOL>return idx + <NUM_LIT:1>, dbm_antnoise<EOL>

strip(1 byte) radiotap.dbm_antnoise
        :idx: int
        :return: int
            idx
        :return: int


idx = Radiotap.align(idx, <NUM_LIT:2>)<EOL>lock_quality, = struct.unpack_from('<STR_LIT>', self._rtap, idx)<EOL>return idx + <NUM_LIT:2>, lock_quality<EOL>

strip(2 byte) lock quality
        :idx: int
        :return: int
            idx
        :return: int


idx = Radiotap.align(idx, <NUM_LIT:2>)<EOL>tx_attenuation, = struct.unpack_from('<STR_LIT>', self._rtap, idx)<EOL>return idx + <NUM_LIT:2>, tx_attenuation<EOL>

strip(1 byte) tx_attenuation
        :idx: int
        :return: int
            idx
        :return: int


idx = Radiotap.align(idx, <NUM_LIT:2>)<EOL>db_tx_attenuation, = struct.unpack_from('<STR_LIT>', self._rtap, idx)<EOL>return idx + <NUM_LIT:2>, db_tx_attenuation<EOL>

strip(1 byte) db_tx_attenuation
        :return: int
            idx
        :return: int


idx = Radiotap.align(idx, <NUM_LIT:1>)<EOL>dbm_tx_power, = struct.unpack_from('<STR_LIT>', self._rtap, idx)<EOL>return idx + <NUM_LIT:1>, dbm_tx_power<EOL>

strip(1 byte) dbm_tx_power
        :return: int
            idx
        :return: int


antenna, = struct.unpack_from('<STR_LIT>', self._rtap, idx)<EOL>return idx + <NUM_LIT:1>, antenna<EOL>

strip(1 byte) radiotap.antenna
        :return: int
            idx
        :return: int


db_antsignal, = struct.unpack_from('<STR_LIT>', self._rtap, idx)<EOL>return idx + <NUM_LIT:1>, db_antsignal<EOL>

strip(1 byte) radiotap.db_antsignal
        :return: int
            idx
        :return: int


db_antnoise, = struct.unpack_from('<STR_LIT>', self._rtap, idx)<EOL>return idx + <NUM_LIT:1>, db_antnoise<EOL>

strip(1 byte) radiotap.db_antnoise
        :return: int
            idx
        :return: int


rx_flags = collections.namedtuple('<STR_LIT>', ['<STR_LIT>', '<STR_LIT>'])<EOL>idx = Radiotap.align(idx, <NUM_LIT:2>)<EOL>flags, = struct.unpack_from('<STR_LIT>', self._rtap, idx)<EOL>flag_bits = format(flags, '<STR_LIT>')[::-<NUM_LIT:1>]<EOL>rx_flags.reserved = int(flag_bits[<NUM_LIT:0>])<EOL>rx_flags.badplcp = int(flag_bits[<NUM_LIT:1>])<EOL>return idx + <NUM_LIT:2>, rx_flags<EOL>

strip(2 byte) radiotap.rxflags
        :idx: int
        :return: int
            idx
        :return: collections.namedtuple


idx = Radiotap.align(idx, <NUM_LIT:2>)<EOL>tx_flags, = struct.unpack_from('<STR_LIT>', self._rtap, idx)<EOL>return idx + <NUM_LIT:1>, tx_flags<EOL>

strip(1 byte) tx_flags
        :idx: int
        :return: int
            idx
        :return: int


rts_retries, = struct.unpack_from('<STR_LIT>', self._rtap, idx)<EOL>return idx + <NUM_LIT:1>, rts_retries<EOL>

strip(1 byte) rts_retries
        :idx: int
        :return: int
            idx
        :return: int


data_retries, = struct.unpack_from('<STR_LIT>', self._rtap, idx)<EOL>return idx + <NUM_LIT:1>, data_retries<EOL>

strip(1 byte) data_retries
        :idx: int
        :return: int
            idx
        :return: int


xchannel = collections.namedtuple(<EOL>'<STR_LIT>', ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'])<EOL>flags = collections.namedtuple(<EOL>'<STR_LIT>', ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>'])<EOL>idx = Radiotap.align(idx, <NUM_LIT:2>)<EOL>flag_val, freq, channel, max_power = struct.unpack_from('<STR_LIT>', self._rtap, idx)<EOL>xchannel.freq = freq<EOL>xchannel.channel = channel<EOL>xchannel.max_power = max_power<EOL>bits = format(flag_val, '<STR_LIT>')[::-<NUM_LIT:1>]<EOL>flags.turbo = int(bits[<NUM_LIT:4>])<EOL>flags.cck = int(bits[<NUM_LIT:5>])<EOL>flags.ofdm = int(bits[<NUM_LIT:6>])<EOL>flags.two_g = int(bits[<NUM_LIT:7>])<EOL>flags.five_g = int(bits[<NUM_LIT:8>])<EOL>flags.passive = int(bits[<NUM_LIT:9>])<EOL>flags.dynamic = int(bits[<NUM_LIT:10>])<EOL>flags.gfsk = int(bits[<NUM_LIT:11>])<EOL>flags.gsm = int(bits[<NUM_LIT:12>])<EOL>flags.sturbo = int(bits[<NUM_LIT>])<EOL>flags.half = int(bits[<NUM_LIT>])<EOL>flags.quarter = int(bits[<NUM_LIT:15>])<EOL>flags.ht_20 = int(bits[<NUM_LIT:16>])<EOL>flags.ht_40u = int(bits[<NUM_LIT>])<EOL>flags.ht_40d = int(bits[<NUM_LIT>])<EOL>xchannel.flags = flags<EOL>return idx + <NUM_LIT:8>, xchannel<EOL>

strip(7 bytes) radiotap.xchannel.channel(1 byte),
        radiotap.xchannel.freq(2 bytes) and radiotap.xchannel.flags(4 bytes)
        :idx: int
        :return: int
            idx
        :return: collections.namedtuple


mcs = collections.namedtuple(<EOL>'<STR_LIT>', ['<STR_LIT>', '<STR_LIT:index>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>'])<EOL>idx = Radiotap.align(idx, <NUM_LIT:1>)<EOL>known, flags, index = struct.unpack_from('<STR_LIT>', self._rtap, idx)<EOL>bits = format(flags, '<STR_LIT>')[::-<NUM_LIT:1>]<EOL>mcs.known = known               <EOL>mcs.index = index               <EOL>mcs.have_bw = int(bits[<NUM_LIT:0>])      <EOL>mcs.have_mcs = int(bits[<NUM_LIT:1>])     <EOL>mcs.have_gi = int(bits[<NUM_LIT:2>])      <EOL>mcs.have_format = int(bits[<NUM_LIT:3>])  <EOL>mcs.have_fec = int(bits[<NUM_LIT:4>])     <EOL>mcs.have_stbc = int(bits[<NUM_LIT:5>])    <EOL>mcs.have_ness = int(bits[<NUM_LIT:6>])    <EOL>mcs.ness_bit1 = int(bits[<NUM_LIT:7>])    <EOL>return idx + <NUM_LIT:3>, mcs<EOL>

strip(3 byte) radiotap.mcs which contains 802.11n bandwidth,
        mcs(modulation and coding scheme) and stbc(space time block coding)
        information.
        :idx: int
        :return: int
            idx
        :return: collections.namedtuple


ampdu = collections.namedtuple(<EOL>'<STR_LIT>', ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'])<EOL>flags = collections.namedtuple(<EOL>'<STR_LIT>', ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>'])<EOL>idx = Radiotap.align(idx, <NUM_LIT:4>)<EOL>refnum, flag_vals, crc_val, reserved = struct.unpack_from('<STR_LIT>', self._rtap, idx)<EOL>ampdu.flags = flags<EOL>ampdu.reference = refnum<EOL>ampdu.crc_val = crc_val<EOL>ampdu.reserved = reserved<EOL>bits = format(flag_vals, '<STR_LIT>')[::-<NUM_LIT:1>]<EOL>ampdu.flags.report_zerolen = int(bits[<NUM_LIT:0>])<EOL>ampdu.flags.is_zerolen = int(bits[<NUM_LIT:1>])<EOL>ampdu.flags.lastknown = int(bits[<NUM_LIT:2>])<EOL>ampdu.flags.last = int(bits[<NUM_LIT:3>])<EOL>ampdu.flags.delim_crc_error = int(bits[<NUM_LIT:4>])<EOL>return idx + <NUM_LIT:8>, ampdu<EOL>

strip(8 byte) radiotap.ampdu
        :idx: int
        :return: int
            idx
        :return: collections.namedtuple


vht = collections.namedtuple(<EOL>'<STR_LIT>', ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'])<EOL>user = collections.namedtuple('<STR_LIT:user>', ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>'])<EOL>idx = Radiotap.align(idx, <NUM_LIT:2>)<EOL>known, flags, bw = struct.unpack_from('<STR_LIT>', self._rtap, idx)<EOL>mcs_nss_0, mcs_nss_1, mcs_nss_2, mcs_nss_3 = struct.unpack_from('<STR_LIT>', self._rtap, idx + <NUM_LIT:4>)<EOL>coding, group_id, partial_id = struct.unpack_from('<STR_LIT>', self._rtap, idx + <NUM_LIT:8>)<EOL>known_bits = format(known, '<STR_LIT>')[::-<NUM_LIT:1>]<EOL>vht.known_bits = known_bits<EOL>vht.have_stbc = int(known_bits[<NUM_LIT:0>])         <EOL>vht.have_txop_ps = int(known_bits[<NUM_LIT:1>])      <EOL>vht.have_gi = int(known_bits[<NUM_LIT:2>])           <EOL>vht.have_sgi_nsym_da = int(known_bits[<NUM_LIT:3>])  <EOL>vht.have_ldpc_extra = int(known_bits[<NUM_LIT:4>])   <EOL>vht.have_beamformed = int(known_bits[<NUM_LIT:5>])   <EOL>vht.have_bw = int(known_bits[<NUM_LIT:6>])           <EOL>vht.have_gid = int(known_bits[<NUM_LIT:7>])          <EOL>vht.have_paid = int(known_bits[<NUM_LIT:8>])         <EOL>flag_bits = format(flags, '<STR_LIT>')[::-<NUM_LIT:1>]<EOL>vht.flag_bits = flag_bits<EOL>vht.stbc = int(flag_bits[<NUM_LIT:0>])<EOL>vht.txop_ps = int(flag_bits[<NUM_LIT:1>])<EOL>vht.gi = int(flag_bits[<NUM_LIT:2>])<EOL>vht.sgi_nysm_da = int(flag_bits[<NUM_LIT:3>])<EOL>vht.ldpc_extra = int(flag_bits[<NUM_LIT:4>])<EOL>vht.beamformed = int(flag_bits[<NUM_LIT:5>])<EOL>vht.group_id = group_id<EOL>vht.partial_id = partial_id<EOL>vht.bw = bw<EOL>vht.user_0 = user(None, None, None)<EOL>vht.user_1 = user(None, None, None)<EOL>vht.user_2 = user(None, None, None)<EOL>vht.user_3 = user(None, None, None)<EOL>for (i, mcs_nss) in enumerate([mcs_nss_0, mcs_nss_1, mcs_nss_2, mcs_nss_3]):<EOL><INDENT>if mcs_nss:<EOL><INDENT>nss = mcs_nss & <NUM_LIT> >> <NUM_LIT:4><EOL>mcs = (mcs_nss & <NUM_LIT>) >> <NUM_LIT:4><EOL>coding = (coding & <NUM_LIT:2>**i) >> i<EOL>if i == <NUM_LIT:0>:<EOL><INDENT>vht.user_0 = user(nss, mcs, coding)<EOL><DEDENT>elif i == <NUM_LIT:1>:<EOL><INDENT>vht.user_1 = user(nss, mcs, coding)<EOL><DEDENT>elif i == <NUM_LIT:2>:<EOL><INDENT>vht.user_2 = user(nss, mcs, coding)<EOL><DEDENT>elif i == <NUM_LIT:3>:<EOL><INDENT>vht.user_3 = user(nss, mcs, coding)<EOL><DEDENT><DEDENT><DEDENT>return idx + <NUM_LIT:12>, vht<EOL>

strip(12 byte) radiotap.vht
        :idx: int
        :return: int
            idx
        :return: collections.namedtuple


if self.present.mcs:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>if self.present.vht:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>if self.present.channel and hasattr(self, '<STR_LIT>'):<EOL><INDENT>if self.chan.five_g:<EOL><INDENT>if self.chan.ofdm:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT><DEDENT>elif self.chan.two_g:<EOL><INDENT>if self.chan.cck:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>elif self.chan.ofdm or self.chan.dynamic:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT><DEDENT><DEDENT>return '<STR_LIT:None>'<EOL>

extract 802.11 protocol from radiotap.channel.flags
        :return: str
            protocol name
            one of below in success
            [.11a, .11b, .11g, .11n, .11ac]
            None in fail


return (val + align - <NUM_LIT:1>) & ~(align - <NUM_LIT:1>)<EOL>


def __init__(self, frame, no_rtap=False):

super(Wifi, self).__init__()<EOL>self._raw = {}<EOL>if not no_rtap:<EOL><INDENT>rtap_bytes, self._packet = WiHelper._strip_rtap(frame)<EOL>self.radiotap = Radiotap(rtap_bytes)<EOL><DEDENT>else:<EOL><INDENT>self._packet = frame<EOL>self.radiotap = None<EOL><DEDENT>f_cntrl = struct.unpack('<STR_LIT>', self._packet[:<NUM_LIT:2>])  <EOL>flags = f_cntrl[<NUM_LIT:1>]<EOL>self.vers = f_cntrl[<NUM_LIT:0>] & <NUM_LIT><EOL>self.category = (f_cntrl[<NUM_LIT:0>] >> <NUM_LIT:2>) & <NUM_LIT><EOL>self.subtype = f_cntrl[<NUM_LIT:0>] >> <NUM_LIT:4><EOL>flag_bits = format(flags, '<STR_LIT>')[::-<NUM_LIT:1>]<EOL>self.to_ds = int(flag_bits[<NUM_LIT:0>])<EOL>self.from_ds = int(flag_bits[<NUM_LIT:1>])<EOL>self.ds = b'<STR_LIT>'.join([(flag_bits[<NUM_LIT:0>]).encode('<STR_LIT:ascii>'),<EOL>(flag_bits[<NUM_LIT:1>]).encode('<STR_LIT:ascii>')])<EOL>self.frag = int(flag_bits[<NUM_LIT:2>])<EOL>self.retry = int(flag_bits[<NUM_LIT:3>])<EOL>self.power_mgmt = int(flag_bits[<NUM_LIT:4>])<EOL>self.more_data = int(flag_bits[<NUM_LIT:5>])<EOL>self.wep = int(flag_bits[<NUM_LIT:6>])<EOL>self.order = int(flag_bits[<NUM_LIT:7>])<EOL>self.duration = struct.unpack('<STR_LIT:H>', self._packet[<NUM_LIT:2>:<NUM_LIT:4>])[<NUM_LIT:0>]  <EOL>self.name = None<EOL>if self.category == <NUM_LIT:0>:<EOL><INDENT>if self.subtype in _SUBTYPES_[<NUM_LIT:0>].keys():<EOL><INDENT>self.name = _SUBTYPES_[<NUM_LIT:0>][self.subtype].encode('<STR_LIT:ascii>')<EOL><DEDENT><DEDENT>elif self.category == <NUM_LIT:1>:<EOL><INDENT>if self.subtype in _SUBTYPES_[<NUM_LIT:1>].keys():<EOL><INDENT>self.name = _SUBTYPES_[<NUM_LIT:1>][self.subtype].encode('<STR_LIT:ascii>')<EOL><DEDENT><DEDENT>elif self.category == <NUM_LIT:2>:<EOL><INDENT>if self.subtype in _SUBTYPES_[<NUM_LIT:2>].keys():<EOL><INDENT>self.name = _SUBTYPES_[<NUM_LIT:2>][self.subtype].encode('<STR_LIT:ascii>')<EOL><DEDENT><DEDENT>

Constructor method.
        Parse common headers of all Wi-Fi frames.
        :frame: ctypes.Structure


keys, exist, out = None, {}, None<EOL>if isinstance(fields, str):<EOL><INDENT>fields = [fields]<EOL><DEDENT>elif not isinstance(fields, list):<EOL><INDENT>logging.error('<STR_LIT>')<EOL>return None<EOL><DEDENT>out = dict.fromkeys(fields)<EOL>if hasattr(self, '<STR_LIT>'):<EOL><INDENT>exist.update(self._shark_)<EOL><DEDENT>if hasattr(self, '<STR_LIT>'):<EOL><INDENT>exist.update(self._s_shark_)<EOL><DEDENT>if hasattr(self.radiotap, '<STR_LIT>'):<EOL><INDENT>exist.update(self.radiotap._r_shark_)<EOL><DEDENT>keys = exist.keys()<EOL>for elem in fields:<EOL><INDENT>if elem in keys:<EOL><INDENT>obj_field, tmp = exist[elem], None<EOL>try:<EOL><INDENT>tmp = operator.attrgetter(obj_field)(self)<EOL><DEDENT>except AttributeError:<EOL><INDENT>tmp = None<EOL><DEDENT>if not tmp:<EOL><INDENT>try:<EOL><INDENT>tmp = operator.attrgetter(obj_field)(self.radiotap)<EOL><DEDENT>except AttributeError:<EOL><INDENT>tmp = None<EOL><DEDENT><DEDENT>out[elem] = tmp<EOL><DEDENT><DEDENT>return out<EOL>

get parameters via wireshark syntax.
        out = x.get_shark_field('wlan.fc.type')
        out = x.get_shark_field(['wlan.fc.type', 'wlan.seq'])
        :fields: str or str[]
        :return: dict
            out[fields[0]] = val[0] or None
            out[fields[1]] = val[1] or None ...


mac_addr = bytearray(mac_addr)<EOL>mac = b'<STR_LIT::>'.join([('<STR_LIT>' % o).encode('<STR_LIT:ascii>') for o in mac_addr])<EOL>return mac<EOL>

converts bytes to mac addr format
        :mac_addr: ctypes.structure
        :return: str
            mac addr in format
            11:22:33:aa:bb:cc


return hex(self.category * <NUM_LIT:16> + self.subtype)<EOL>

wlan.fc.type_subtype hex representation
        :return: str


qos_idx, seq_idx = <NUM_LIT:0>, <NUM_LIT:0><EOL>sa, ta, ra, da, bssid = None, None, None, None, None<EOL>if self.to_ds == <NUM_LIT:1> and self.from_ds == <NUM_LIT:1>:<EOL><INDENT>(ra, ta, da) = struct.unpack('<STR_LIT>', self._packet[<NUM_LIT:4>:<NUM_LIT>])<EOL>sa = struct.unpack('<STR_LIT>', self._packet[<NUM_LIT>:<NUM_LIT:30>])[<NUM_LIT:0>]<EOL>qos_idx = <NUM_LIT:30><EOL>seq_idx = <NUM_LIT><EOL><DEDENT>elif self.to_ds == <NUM_LIT:0> and self.from_ds == <NUM_LIT:1>:<EOL><INDENT>(ra, ta, sa) = struct.unpack('<STR_LIT>', self._packet[<NUM_LIT:4>:<NUM_LIT>])<EOL>qos_idx = <NUM_LIT><EOL>seq_idx = <NUM_LIT><EOL><DEDENT>elif self.to_ds == <NUM_LIT:1> and self.from_ds == <NUM_LIT:0>:<EOL><INDENT>(ra, ta, da) = struct.unpack('<STR_LIT>', self._packet[<NUM_LIT:4>:<NUM_LIT>])<EOL>qos_idx = <NUM_LIT><EOL>seq_idx = <NUM_LIT><EOL><DEDENT>elif self.to_ds == <NUM_LIT:0> and self.from_ds == <NUM_LIT:0>:<EOL><INDENT>(ra, ta, bssid) = struct.unpack('<STR_LIT>', self._packet[<NUM_LIT:4>:<NUM_LIT>])<EOL>qos_idx = <NUM_LIT><EOL>seq_idx = <NUM_LIT><EOL><DEDENT>if ta is not None:<EOL><INDENT>ta = Wifi.get_mac_addr(ta)<EOL><DEDENT>if ra is not None:<EOL><INDENT>ra = Wifi.get_mac_addr(ra)<EOL><DEDENT>if sa is not None:<EOL><INDENT>sa = Wifi.get_mac_addr(sa)<EOL><DEDENT>if da is not None:<EOL><INDENT>da = Wifi.get_mac_addr(da)<EOL><DEDENT>if bssid is not None:<EOL><INDENT>bssid = Wifi.get_mac_addr(bssid)<EOL><DEDENT>return seq_idx, qos_idx, sa, ta, ra, da, bssid<EOL>

strip mac address(each 6 byte) information.
        (wlan.ta, wlan.ra, wlan.sa, wlan.da)
        (transmitter, receiver, source, destination)
        :return: int
            index of sequence control
        :return: int
            index after mac addresses
        :return: str
            source address (sa)
        :return: str
            transmitter address (ta)
        :return: str
            receiver address (ra)
        :return: str
            destination address (da)
        :return: str
            basic service sed identifier (bssid)


seq_cntrl = struct.unpack('<STR_LIT:H>', self._packet[idx:idx + <NUM_LIT:2>])[<NUM_LIT:0>]<EOL>seq_num = seq_cntrl >> <NUM_LIT:4><EOL>frag_num = seq_cntrl & <NUM_LIT><EOL>return seq_num, frag_num<EOL>

strip(2 byte) wlan.seq(12 bit) and wlan.fram(4 bit)
        number information.
        :seq_cntrl: ctypes.Structure
        :return: int
            sequence number
        :return: int
            fragment number


out_str = '<STR_LIT>'<EOL>all_fields = []<EOL>if hasattr(self, '<STR_LIT>'):<EOL><INDENT>all_fields += self._fields_<EOL><DEDENT>if hasattr(self, '<STR_LIT>'):<EOL><INDENT>all_fields += self._sfields_<EOL><DEDENT>if all_fields:<EOL><INDENT>for elem in all_fields:<EOL><INDENT>key = elem[<NUM_LIT:0>]<EOL>try:<EOL><INDENT>val = operator.attrgetter(key)(self)<EOL><DEDENT>except Exception:<EOL><INDENT>val = None<EOL><DEDENT>if isinstance(val, list):<EOL><INDENT>if val:<EOL><INDENT>out_str += "<STR_LIT>".format(key, type(val[<NUM_LIT:0>]))<EOL><DEDENT>else:<EOL><INDENT>out_str += "<STR_LIT>".format(str(key))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>out_str += "<STR_LIT>".format(str(key), str(val))<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>logging.error('<STR_LIT>')<EOL>return None<EOL><DEDENT>if show_rfields and hasattr(self.radiotap, '<STR_LIT>'):<EOL><INDENT>for elem in self.radiotap._rfields_:<EOL><INDENT>key = elem[<NUM_LIT:0>]<EOL>try:<EOL><INDENT>val = operator.attrgetter(key)(self.radiotap)<EOL><DEDENT>except Exception:<EOL><INDENT>val = None<EOL><DEDENT>if val is not None:<EOL><INDENT>out_str += "<STR_LIT>".format(key, val)<EOL><DEDENT><DEDENT><DEDENT>return out_str<EOL>

:show_rfields: bool
    whether to show radiotap fields too.


Wifi.__init__(self, frame, no_rtap)<EOL>

Constructor method.
        :packet: ctypes.Structure
        :no_rtap: Bool
            shall parse radiotap headers


def __init__(self, frame, no_rtap=False, parse_amsdu=True):

Data.__init__(self, frame, no_rtap)<EOL>idx = <NUM_LIT:0><EOL>self.sa = self.ta = self.ra = self.da = None<EOL>self.seq_num = self.frag_num = None<EOL>self.qos_pri = self.qos_bit = self.qos_ack = None<EOL>self.ccmp_extiv = None<EOL>self.payload = []<EOL>seq_idx, qos_idx, self.sa, self.ta, self.ra, self.da, _ = self.strip_mac_addrs()<EOL>self.seq_num, self.frag_num = self.strip_seq_cntrl(seq_idx)<EOL>idx = qos_idx<EOL>incr, self.qos_pri, self.qos_bit, self.qos_ack, self.amsdupresent =self.strip_qos_cntrl(idx, self.radiotap.prot_type)<EOL>idx += incr<EOL>if self.wep == <NUM_LIT:1>:<EOL><INDENT>incr, self.ccmp_extiv = self.strip_ccmp(idx)<EOL>idx += incr<EOL><DEDENT>if parse_amsdu:<EOL><INDENT>if self.amsdupresent != <NUM_LIT:0> and self.wep == <NUM_LIT:0>:<EOL><INDENT>while idx < len(self._packet):<EOL><INDENT>msdu, offset = self.strip_msdu(idx)<EOL>self.payload.append(msdu)<EOL>idx += offset<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if self.wep == <NUM_LIT:0>:<EOL><INDENT>msdu = {}<EOL>offset, llc = self.strip_llc(idx)<EOL>msdu['<STR_LIT>'] = llc<EOL>msdu['<STR_LIT>'] = self._packet[idx + offset:]<EOL>self.payload.append(msdu)<EOL><DEDENT>else:<EOL><INDENT>self.payload.append({'<STR_LIT>': self._packet[idx:]})<EOL><DEDENT><DEDENT><DEDENT>

Constructor method.
        :frame: ctypes.Structure
        :parse_amsdu: Bool
            shall parse aggregated mac service data unit


def strip_qos_cntrl(self, idx, prot_type):

qos_cntrl, = struct.unpack('<STR_LIT:H>', self._packet[idx:idx + <NUM_LIT:2>])<EOL>qos_cntrl_bits = format(qos_cntrl, '<STR_LIT>')[::-<NUM_LIT:1>]<EOL>qos_pri = qos_cntrl & <NUM_LIT><EOL>qos_bit = int(qos_cntrl_bits[<NUM_LIT:5>])<EOL>qos_ack = int(qos_cntrl_bits[<NUM_LIT:6>:<NUM_LIT:8>], <NUM_LIT:2>)<EOL>amsdupresent = <NUM_LIT:0><EOL>if prot_type == '<STR_LIT>':<EOL><INDENT>amsdupresent = int(qos_cntrl_bits[<NUM_LIT:7>])<EOL><DEDENT>return <NUM_LIT:2>, qos_pri, qos_bit, qos_ack, amsdupresent<EOL>

strip(2 byte) wlan.qos
        :idx: int
        :prot_type: string
            802.11 protocol type(.11ac, .11a, .11n, etc)
        :return: int
            number of processed bytes
        :return: int
            qos priority
        :return: int
            qos bit
        :return: int
            qos acknowledgement
        :return: int
            amsdupresent(aggregated mac service data unit)


ccmp_extiv = None<EOL>if len(self._packet[idx:]) >= <NUM_LIT:8>:<EOL><INDENT>raw_bytes = self._packet[idx:idx + <NUM_LIT:8>]<EOL>ccmp_extiv, = struct.unpack_from('<STR_LIT>', raw_bytes, <NUM_LIT:0>)<EOL><DEDENT>return <NUM_LIT:8>, ccmp_extiv<EOL>

strip(8 byte) wlan.ccmp.extiv
        CCMP Extended Initialization Vector
        :return: int
            number of processed bytes
        :return: ctypes.raw
            ccmp vector


<EOL>padding = <NUM_LIT:0><EOL>len_payload = <NUM_LIT:0><EOL>msdu = {<EOL>'<STR_LIT>': {},<EOL>'<STR_LIT>': None,<EOL>'<STR_LIT>': None,<EOL>'<STR_LIT>': None,<EOL>'<STR_LIT>': <NUM_LIT:0><EOL>}<EOL>(da_mac, sa_mac) = struct.unpack('<STR_LIT>', self._packet[idx:idx + <NUM_LIT:12>])<EOL>msdu['<STR_LIT>'] = Wifi.get_mac_addr(da_mac)<EOL>msdu['<STR_LIT>'] = Wifi.get_mac_addr(sa_mac)<EOL>idx += <NUM_LIT:12><EOL>msdu['<STR_LIT>'] = struct.unpack('<STR_LIT>', self._packet[idx:idx + <NUM_LIT:2>])[<NUM_LIT:0>]<EOL>idx += <NUM_LIT:2><EOL>offset, msdu['<STR_LIT>'] = self.strip_llc(idx)<EOL>idx += offset<EOL>len_payload = msdu['<STR_LIT>'] - offset<EOL>msdu['<STR_LIT>'] = self._packet[idx:idx + len_payload]<EOL>padding = <NUM_LIT:4> - (len_payload % <NUM_LIT:4>)<EOL>return msdu, msdu['<STR_LIT>'] + padding + <NUM_LIT:12><EOL>

strip single mac servis data unit(msdu)
        see -> https://mrncciew.com/2014/11/01/cwap-802-11-data-frame-aggregation/
        :idx: int
        :return: dict
            msdu
        :return: int
            number of processed bytes


llc = {}<EOL>snap = <NUM_LIT><EOL>llc_dsap = struct.unpack('<STR_LIT:B>', self._packet[idx:idx + <NUM_LIT:1>])[<NUM_LIT:0>]<EOL>llc['<STR_LIT>'] = llc_dsap >> <NUM_LIT:1><EOL>llc['<STR_LIT>'] = llc_dsap & <NUM_LIT><EOL>idx += <NUM_LIT:1><EOL>llc_ssap = struct.unpack('<STR_LIT:B>', self._packet[idx:idx + <NUM_LIT:1>])[<NUM_LIT:0>]<EOL>llc['<STR_LIT>'] = llc_ssap >> <NUM_LIT:1><EOL>llc['<STR_LIT>'] = llc_ssap & <NUM_LIT><EOL>idx += <NUM_LIT:1><EOL>if llc_dsap == snap and llc_ssap == snap:<EOL><INDENT>llc_control = struct.unpack('<STR_LIT:B>', self._packet[idx:idx + <NUM_LIT:1>])[<NUM_LIT:0>]<EOL>llc['<STR_LIT>'] = llc_control >> <NUM_LIT:2><EOL>llc['<STR_LIT>'] = llc_control & <NUM_LIT><EOL>idx += <NUM_LIT:1><EOL>llc['<STR_LIT>'] = self._packet[idx:idx + <NUM_LIT:3>]<EOL>idx += <NUM_LIT:3><EOL>llc['<STR_LIT:type>'] = self._packet[idx:idx + <NUM_LIT:2>]<EOL>return <NUM_LIT:8>, llc<EOL><DEDENT>else:<EOL><INDENT>return <NUM_LIT:4>, llc<EOL><DEDENT>

strip(4 or 8 byte) logical link control headers
        :return: int
            number of processed bytes
        :return: dict
            llc information
        see -> http://www.wildpackets.com/resources/compendium/ethernet/frame_snap_iee8023
        ABBRVS.
        ssap: source service access point
        dsap: destination service access point
        SNAP(Subnetwork Acess Protocol)


Wifi.__init__(self, frame, no_rtap)<EOL>self.tagged_params = []<EOL>self._raw_tagged_params = None<EOL>self.timestamp = None<EOL>self.interval = None<EOL>self.fixed_capabils = None<EOL>

Constructor Method
        :frame: ctypes.Structure
        :subtype: int


fcs_len = <NUM_LIT:4>  <EOL>idx = <NUM_LIT:0><EOL>tagged_params = []<EOL>while idx < len(raw_tagged_params) - fcs_len:<EOL><INDENT>tag_num, tag_len = struct.unpack('<STR_LIT>', raw_tagged_params[idx:idx + <NUM_LIT:2>])<EOL>idx += <NUM_LIT:2><EOL>if len(raw_tagged_params) >= idx + tag_len:<EOL><INDENT>param = {}<EOL>param['<STR_LIT>'], param['<STR_LIT>'] = tag_num, tag_len<EOL>payload = raw_tagged_params[idx:idx + tag_len]<EOL>if tag_num in MNGMT_TAGS:<EOL><INDENT>param['<STR_LIT:name>'] = MNGMT_TAGS[tag_num]<EOL>if MNGMT_TAGS[tag_num] == '<STR_LIT>':<EOL><INDENT>param['<STR_LIT>'] = Management.parse_vendor_ie(payload)<EOL><DEDENT>else:<EOL><INDENT>param['<STR_LIT>'] = payload<EOL><DEDENT><DEDENT>else:<EOL><INDENT>param['<STR_LIT:name>'] = None<EOL><DEDENT>tagged_params.append(param)<EOL>idx += tag_len<EOL><DEDENT>else:<EOL><INDENT>logging.warning('<STR_LIT>')<EOL>log_msg = '<STR_LIT>'<EOL>log_msg = log_msg.format(p_idx=idx + tag_len,<EOL>p_len=len(raw_tagged_params))<EOL>logging.warning(log_msg)<EOL>return <NUM_LIT:1>, tagged_params<EOL><DEDENT><DEDENT>return <NUM_LIT:0>, tagged_params<EOL>

strip tagged information elements wlan_mgt.tag
        which has generic type-length-value structure
        [type, length, value]
        type(1 byte), length(1 byte), value(varies)
        [wlan_mgt.tag.number, wlan_mgt.tag.length, payload]
        structured fields.
        :return: dict[]
            list of tagged params
        :return: int
            0 in succ, 1 for


if len(payload) != <NUM_LIT:2>:<EOL><INDENT>return None<EOL><DEDENT>capabils = {}<EOL>fix_cap = struct.unpack('<STR_LIT:H>', payload)[<NUM_LIT:0>]<EOL>cap_bits = format(fix_cap, '<STR_LIT>')[::-<NUM_LIT:1>]<EOL>capabils['<STR_LIT>'] = int(cap_bits[<NUM_LIT:0>])             <EOL>capabils['<STR_LIT>'] = int(cap_bits[<NUM_LIT:1>])            <EOL>capabils['<STR_LIT>'] = int(cap_bits[<NUM_LIT:4>])            <EOL>capabils['<STR_LIT>'] = int(cap_bits[<NUM_LIT:5>])  <EOL>capabils['<STR_LIT>'] = int(cap_bits[<NUM_LIT:6>])            <EOL>capabils['<STR_LIT>'] = int(cap_bits[<NUM_LIT:7>])    <EOL>capabils['<STR_LIT>'] = int(cap_bits[<NUM_LIT:8>])        <EOL>capabils['<STR_LIT>'] = int(cap_bits[<NUM_LIT:10>])     <EOL>capabils['<STR_LIT>'] = int(cap_bits[<NUM_LIT:11>])           <EOL>capabils['<STR_LIT>'] = int(cap_bits[<NUM_LIT:12>])     <EOL>capabils['<STR_LIT>'] = int(cap_bits[<NUM_LIT>])       <EOL>capabils['<STR_LIT>'] = int(cap_bits[<NUM_LIT>])       <EOL>capabils['<STR_LIT>'] = int(cap_bits[<NUM_LIT:15>])       <EOL>return capabils<EOL>

strip(2 byte) wlan_mgt.fixed.capabilities
        :payload: ctypes.structure
            2 byte
        :return: dict
            None in error


output = {}<EOL>oui = struct.unpack('<STR_LIT>', payload[<NUM_LIT:0>:<NUM_LIT:3>])<EOL>oui = b'<STR_LIT:->'.join([('<STR_LIT>' % o).encode('<STR_LIT:ascii>') for o in oui])<EOL>oui_type = struct.unpack('<STR_LIT:B>', payload[<NUM_LIT:3>:<NUM_LIT:4>])[<NUM_LIT:0>]<EOL>oui_data = payload[<NUM_LIT:4>:]<EOL>output['<STR_LIT>'] = oui.upper()<EOL>output['<STR_LIT>'] = oui_type<EOL>output['<STR_LIT>'] = oui_data<EOL>return output<EOL>

parse vendor specific information element
        oui -> organizationally unique identifier
        first 3 bytes of mac addresses
        see:https://www.wireshark.org/tools/oui-lookup.html
        strip wlan_mgt.tag.oui(3 bytes),
        wlan_mgt.tag.vendor.oui.type(1 byte)
        wlan_mgt.tag.vendor.data (varies)
        :payload: ctypes.structure
        :return: dict
            {'oui':00-11-22, 'oui_type':1, 'oui_data':ctypes.structure}


if len(payload) != <NUM_LIT:8>:<EOL><INDENT>return None<EOL><DEDENT>timestamp = struct.unpack('<STR_LIT>', payload)[<NUM_LIT:0>]<EOL>return timestamp<EOL>

strip wlan_mgt.fixed.timestamp(8 bytes)
        :payload: ctypes.structure
        :return: int
            None on error


if len(payload) != <NUM_LIT:2>:<EOL><INDENT>return None<EOL><DEDENT>interval = struct.unpack('<STR_LIT:H>', payload)[<NUM_LIT:0>]<EOL>return interval<EOL>

strip wlan_mgt.fixed.beacoN(2 bytes)
        beacon interval
        :payload: ctypes.structure
        :return: int
            None on error


if len(payload) != <NUM_LIT:12>:<EOL><INDENT>return None, None, None<EOL><DEDENT>idx = <NUM_LIT:0><EOL>timestamp = Management.get_timestamp(payload[idx:idx + <NUM_LIT:8>])<EOL>idx += <NUM_LIT:8><EOL>interval = Management.get_interval(payload[idx:idx + <NUM_LIT:2>])<EOL>idx += <NUM_LIT:2><EOL>capabils = Management.get_fixed_capabils(payload[idx:idx + <NUM_LIT:2>])<EOL>return timestamp, interval, capabils<EOL>

strip(12 byte) wlan_mgt.fixed.all
        :payload: ctypes.structure
        :return: int
            timestamp
        :return: int
            beacon interval
        :return: dict
            capabilities


if len(mac_block) != <NUM_LIT:8>:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>if '<STR_LIT::>' in mac_block:<EOL><INDENT>if len(mac_block.split('<STR_LIT::>')) != <NUM_LIT:3>:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT><DEDENT>elif '<STR_LIT:->' in mac_block:<EOL><INDENT>if len(mac_block.split('<STR_LIT:->')) != <NUM_LIT:3>:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT><DEDENT>return <NUM_LIT:1><EOL>

checks whether mac block is in format of
        00-11-22 or 00:11:22.
        :return: int


self.ess = capabils['<STR_LIT>']<EOL>self.ibss = capabils['<STR_LIT>']<EOL>self.priv = capabils['<STR_LIT>']<EOL>self.short_preamble = capabils['<STR_LIT>']<EOL>self.pbcc = capabils['<STR_LIT>']<EOL>self.chan_agility = capabils['<STR_LIT>']<EOL>self.spec_man = capabils['<STR_LIT>']<EOL>self.short_slot = capabils['<STR_LIT>']<EOL>self.apsd = capabils['<STR_LIT>']<EOL>self.radio_meas = capabils['<STR_LIT>']<EOL>self.dss_ofdm = capabils['<STR_LIT>']<EOL>self.del_back = capabils['<STR_LIT>']<EOL>self.imm_back = capabils['<STR_LIT>']<EOL>

set keys of capabils into fields of object
        :capabils: dict


def get_vendor_ies(self, mac_block=None, oui_type=None):

vendor_ies = []<EOL>if mac_block is not None:<EOL><INDENT>if Management.is_valid_mac_oui(mac_block):<EOL><INDENT>mac_block = mac_block.upper()<EOL>if '<STR_LIT::>' in mac_block:<EOL><INDENT>mac_block.replace('<STR_LIT::>', '<STR_LIT:->')<EOL><DEDENT><DEDENT>else:<EOL><INDENT>logging.warning("<STR_LIT>")<EOL>return None<EOL><DEDENT><DEDENT>for elem in self.tagged_params:<EOL><INDENT>tag_num = elem['<STR_LIT>']<EOL>if MNGMT_TAGS[tag_num] == '<STR_LIT>':<EOL><INDENT>if mac_block is None:<EOL><INDENT>vendor_ies.append(elem)<EOL><DEDENT>elif elem['<STR_LIT>']['<STR_LIT>'] == mac_block.encode('<STR_LIT:ascii>'):<EOL><INDENT>if oui_type is None:<EOL><INDENT>vendor_ies.append(elem)<EOL><DEDENT>elif elem['<STR_LIT>']['<STR_LIT>'] == oui_type:<EOL><INDENT>vendor_ies.append(elem)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return vendor_ies<EOL>

vendor information element querying
        :mac_block: str
            first 3 bytes of mac addresses in format of
            00-11-22 or 00:11:22 or 001122
        :oui_type: int
            vendors ie type
        :return: int
            is valid mac_block  format
            -1 is unknown
        :return: dict[]
            list of oui information elements
            -1 on error (invalid v


Management.__init__(self, frame, no_rtap)<EOL>idx = <NUM_LIT:0><EOL>self.timestamp = self.interval = None<EOL>self.ta = self.ra = self.bssid = None<EOL>self.seq_num = self.frag_num = None<EOL>self.ess = self.ibss = None<EOL>self.privacy = None<EOL>self.priv = self.short_preamble = self.pbcc = self.chan_agility = None<EOL>self.spec_man = self.short_slot = self.apsd = self.radio_meas = None<EOL>self.dss_ofdm = self.del_back = self.imm_back = None<EOL>seq_idx, _, _, self.ta, self.ra, _, self.bssid = self.strip_mac_addrs()<EOL>idx = seq_idx<EOL>self.seq_num, self.frag_num = self.strip_seq_cntrl(idx)<EOL>idx += <NUM_LIT:2><EOL>payload = self._packet[idx:idx + <NUM_LIT:12>]<EOL>timestamp, interval, fixed_capabils = self.strip_fixed_params(payload)<EOL>if all([timestamp, interval, fixed_capabils]):<EOL><INDENT>self.timestamp = timestamp<EOL>self.interval = interval<EOL>self.set_fixed_capabils(fixed_capabils)<EOL>idx += <NUM_LIT:12><EOL><DEDENT>else:<EOL><INDENT>logging.warning("<STR_LIT>")<EOL>return<EOL><DEDENT>if idx < len(self._packet):<EOL><INDENT>self._raw_tagged_params = self._packet[idx:]<EOL>is_out_bound, tagged_params = self.parse_tagged_params(self._raw_tagged_params)<EOL>if len(tagged_params):<EOL><INDENT>self.tagged_params = tagged_params<EOL><DEDENT>if is_out_bound:<EOL><INDENT>logging.warning("<STR_LIT>")<EOL><DEDENT><DEDENT>

Constructor method.
        :frame: ctypes.Structure


Wifi.__init__(self, frame, no_rtap)<EOL>


Control.__init__(self, frame, no_rtap)<EOL>(ra_mac, ta_mac) = struct.unpack('<STR_LIT>', self._packet[<NUM_LIT:4>:<NUM_LIT:16>])<EOL>self.ra = Wifi.get_mac_addr(ra_mac)<EOL>self.ta = Wifi.get_mac_addr(ta_mac)<EOL>


Control.__init__(self, frame, no_rtap)<EOL>ra_mac = struct.unpack('<STR_LIT>', self._packet[<NUM_LIT:4>:<NUM_LIT:10>])[<NUM_LIT:0>]<EOL>self.ra = Wifi.get_mac_addr(ra_mac)<EOL>


Control.__init__(self, frame, no_rtap)<EOL>(ra_mac, ta_mac) = struct.unpack('<STR_LIT>', self._packet[<NUM_LIT:4>:<NUM_LIT:16>])<EOL>self.ra = self.ta = None<EOL>self.ackpolicy = self.multitid = None<EOL>self.ssc_frag = self.ssc_seq = None<EOL>self.bitmap_str = None<EOL>self.acked_seqs = []<EOL>self.ra = Wifi.get_mac_addr(ra_mac)<EOL>self.ta = Wifi.get_mac_addr(ta_mac)<EOL>idx = <NUM_LIT:16><EOL>payload = self._packet[idx:idx + <NUM_LIT:2>]<EOL>self.ackpolicy, self.multitid = BACK.strip_cntrl(payload)<EOL>idx += <NUM_LIT:2><EOL>payload = self._packet[idx:idx + <NUM_LIT:2>]<EOL>self.ssc_seq, self.ssc_frag = BACK.strip_ssc(payload)<EOL>idx += <NUM_LIT:2><EOL>payload = self._packet[idx:idx + <NUM_LIT:8>]<EOL>self.bitmap_str = BACK.strip_bitmap_str(payload)<EOL>idx += <NUM_LIT:8><EOL>self.acked_seqs = BACK.extract_acked_seqs(self.bitmap_str, self.ssc_seq)<EOL>


out = super(BACK, self).get_shark_field(fields)<EOL>out.update({'<STR_LIT>': self.acked_seqs,<EOL>'<STR_LIT>': self.bitmap_str})<EOL>return out<EOL>


cntrl = struct.unpack('<STR_LIT:H>', payload)[<NUM_LIT:0>]<EOL>cntrl_bits = format(cntrl, '<STR_LIT>')[::-<NUM_LIT:1>]<EOL>ackpolicy = int(cntrl_bits[<NUM_LIT:0>])<EOL>multitid = int(cntrl_bits[<NUM_LIT:1>])<EOL>return ackpolicy, multitid<EOL>

strip(2 byte) wlan.ba.control
        :payload: ctypes.structure
        :return: int
            multitid (tid: traffic indicator)
        :return: int
            ackpolicy


ssc = struct.unpack('<STR_LIT:H>', payload)[<NUM_LIT:0>]<EOL>ssc_seq = ssc >> <NUM_LIT:4><EOL>ssc_frag = ssc & <NUM_LIT><EOL>return ssc_seq, ssc_frag<EOL>

strip(2 byte) wlan_mgt.fixed.ssc
        :payload: ctypes.structure
        :return: int
            ssc_seq (starting sequence control sequence)
        :return: int
            ssc_frag (starting sequence control fragment number)


bitmap = struct.unpack('<STR_LIT>', payload)<EOL>bitmap_str = '<STR_LIT>'<EOL>for elem in bitmap:<EOL><INDENT>bitmap_str += format(elem, '<STR_LIT>')[::-<NUM_LIT:1>]<EOL><DEDENT>return bitmap_str<EOL>

strip(8 byte) wlan.ba.bm
        :payload: ctypes.structure
        :return: str
            bitmap


acked_seqs = []<EOL>for idx, val in enumerate(bitmap):<EOL><INDENT>if int(val) == <NUM_LIT:1>:<EOL><INDENT>seq = (ssc_seq + idx) % <NUM_LIT><EOL>acked_seqs.append(seq)<EOL><DEDENT><DEDENT>return acked_seqs<EOL>

extracts acknowledged sequences from bitmap and
        starting sequence number.
        :bitmap: str
        :ssc_seq: int
        :return: int[]
            acknowledged sequence numbers


def parse_require(self, env, keys, defaults={}):

for k in keys:<EOL><INDENT>env[k] = getattr(self.options, k) or env.get(k, None)<EOL>if env[k] is None:<EOL><INDENT>self.error("<STR_LIT>"<EOL>"<STR_LIT>" % (k, env))<EOL><DEDENT>pass<EOL><DEDENT>for k, v in defaults.items():<EOL><INDENT>env.setdefault(k, v)<EOL><DEDENT>return env<EOL>

check and get require config
:param dict env: user config node
:param list keys: check keys
    .. note::

        option and key name must be same.

:param dict defaults: default value for keys
:return: dict.env with verified.

.. exception::

    will raise `ValueError` when some key missed.


from cliez.conf import COMPONENT_ROOT<EOL>root = COMPONENT_ROOT<EOL>if root is None:<EOL><INDENT>sys.stderr.write("<STR_LIT>")<EOL>sys.exit(<NUM_LIT:2>)<EOL>pass<EOL><DEDENT>if not os.path.exists(root):<EOL><INDENT>sys.stderr.write(<EOL>"<STR_LIT>")<EOL>sys.exit(<NUM_LIT:2>)<EOL>pass<EOL><DEDENT>try:<EOL><INDENT>path = os.listdir(os.path.join(root, '<STR_LIT>'))<EOL>return [f[:-<NUM_LIT:3>] for f in path if<EOL>f.endswith('<STR_LIT>') and f != '<STR_LIT>']<EOL><DEDENT>except FileNotFoundError:<EOL><INDENT>return []<EOL><DEDENT>

Get sub-command list

.. note::

    Don't use logger handle this function errors.

    Because the error should be a code error,not runtime error.

:return: `list` matched sub-parser


def append_arguments(klass, sub_parsers, default_epilog, general_arguments):

entry_name = hump_to_underscore(klass.__name__).replace(<EOL>'<STR_LIT>',<EOL>'<STR_LIT>')<EOL>epilog = default_epilog if default_epilogelse '<STR_LIT>''<STR_LIT>'<EOL>sub_parser = sub_parsers.add_parser(entry_name, help=klass.__doc__,<EOL>epilog=epilog)<EOL>sub_parser.description = klass.add_arguments.__doc__<EOL>if hasattr(klass, '<STR_LIT>'):<EOL><INDENT>slot_args = klass.add_slot_args() or []<EOL>for v in slot_args:<EOL><INDENT>sub_parser.add_argument(*v[<NUM_LIT:0>], **v[<NUM_LIT:1>])<EOL><DEDENT>sub_parser.description = klass.add_slot_args.__doc__<EOL>pass<EOL><DEDENT>user_arguments = klass.add_arguments() or []<EOL>for v in user_arguments:<EOL><INDENT>sub_parser.add_argument(*v[<NUM_LIT:0>], **v[<NUM_LIT:1>])<EOL><DEDENT>if not klass.exclude_global_option:<EOL><INDENT>for v in general_arguments:<EOL><INDENT>sub_parser.add_argument(*v[<NUM_LIT:0>], **v[<NUM_LIT:1>])<EOL><DEDENT><DEDENT>return sub_parser<EOL>

Add class options to argparser options.

:param cliez.component.Component klass: subclass of Component
:param Namespace sub_parsers:
:param str default_epilog: default_epilog
:param list general_arguments: global options, defined by user
:return: Namespace subparser


def parse(parser, argv=None, settings_key='<STR_LIT>', no_args_func=None):

argv = argv or sys.argv<EOL>commands = command_list()<EOL>if type(argv) not in [list, tuple]:<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>if len(argv) >= <NUM_LIT:2> and argv[<NUM_LIT:1>] in commands:<EOL><INDENT>sub_parsers = parser.add_subparsers()<EOL>class_name = argv[<NUM_LIT:1>].capitalize() + '<STR_LIT>'<EOL>from cliez.conf import (COMPONENT_ROOT,<EOL>LOGGING_CONFIG,<EOL>EPILOG,<EOL>GENERAL_ARGUMENTS)<EOL>sys.path.insert(<NUM_LIT:0>, os.path.dirname(COMPONENT_ROOT))<EOL>mod = importlib.import_module(<EOL>'<STR_LIT>'.format(os.path.basename(COMPONENT_ROOT),<EOL>argv[<NUM_LIT:1>]))<EOL>klass = getattr(mod, class_name)<EOL>sub_parser = append_arguments(klass, sub_parsers, EPILOG,<EOL>GENERAL_ARGUMENTS)<EOL>options = parser.parse_args(argv[<NUM_LIT:1>:])<EOL>settings = Settings.bind(<EOL>getattr(options, settings_key)<EOL>) if settings_key and hasattr(options, settings_key) else None<EOL>obj = klass(parser, sub_parser, options, settings)<EOL>logger_level = logging.CRITICAL<EOL>if hasattr(options, '<STR_LIT>'):<EOL><INDENT>if options.verbose == <NUM_LIT:1>:<EOL><INDENT>logger_level = logging.ERROR<EOL><DEDENT>elif options.verbose == <NUM_LIT:2>:<EOL><INDENT>logger_level = logging.WARNING<EOL><DEDENT>elif options.verbose == <NUM_LIT:3>:<EOL><INDENT>logger_level = logging.INFO<EOL>obj.logger.setLevel(logging.INFO)<EOL><DEDENT>pass<EOL><DEDENT>if hasattr(options, '<STR_LIT>') and options.debug:<EOL><INDENT>logger_level = logging.DEBUG<EOL>try:<EOL><INDENT>import http.client as http_client<EOL>http_client.HTTPConnection.debuglevel = <NUM_LIT:1><EOL><DEDENT>except Exception:<EOL><INDENT>pass<EOL><DEDENT>pass<EOL><DEDENT>loggers = LOGGING_CONFIG['<STR_LIT>']<EOL>for k, v in loggers.items():<EOL><INDENT>v.setdefault('<STR_LIT>', logger_level)<EOL>if logger_level in [logging.INFO, logging.DEBUG]:<EOL><INDENT>v['<STR_LIT>'] = ['<STR_LIT>']<EOL><DEDENT>pass<EOL><DEDENT>logging_config.dictConfig(LOGGING_CONFIG)<EOL>obj.run(options)<EOL>return obj<EOL><DEDENT>if not parser.description and len(commands):<EOL><INDENT>sub_parsers = parser.add_subparsers()<EOL>[sub_parsers.add_parser(v) for v in commands]<EOL>pass<EOL><DEDENT>pass<EOL>options = parser.parse_args(argv[<NUM_LIT:1>:])<EOL>if no_args_func and callable(no_args_func):<EOL><INDENT>return no_args_func(options)<EOL><DEDENT>else:<EOL><INDENT>parser._print_message("<STR_LIT>")<EOL><DEDENT>pass<EOL>

parser cliez app

:param argparse.ArgumentParser parser: an instance
    of argparse.ArgumentParser
:param argv: argument list,default is `sys.argv`
:type argv: list or tuple

:param str settings: settings option name,
    default is settings.

:param object no_args_func: a callable object.if no sub-parser matched,
    parser will call it.

:return:  an instance of `cliez.component.Component` or its subclass


def __init__(self, parser=None, sub_parser=None, options=None,<EOL>settings=None,<EOL>*args, **kwargs):

self.parser = parser<EOL>self.sub_parser = sub_parser<EOL>self.options = options<EOL>self.settings = settings<EOL>if not self.logger_name:<EOL><INDENT>self.logger_name = '<STR_LIT>'.format(<EOL>self.__class__.__name__.lower().replace('<STR_LIT>', '<STR_LIT>'))<EOL><DEDENT>self.logger = logging.getLogger(self.logger_name)<EOL>pass<EOL>

Don't overwrite this method.
In most case,you should custom  `run()` method


def print_message(self, message, fh=None):

return self.parser._print_message(message + "<STR_LIT:\n>", fh)<EOL>

print message on screen

:param str message:
:param file fh: file handle,default is None
:return: None


tags = ['<STR_LIT:\\>', '<STR_LIT:|>', '<STR_LIT:/>', '<STR_LIT:->']<EOL>for i in range(wait):<EOL><INDENT>time.sleep(<NUM_LIT>)<EOL>sys.stdout.write("<STR_LIT>" % {<EOL>'<STR_LIT:message>': message,<EOL>'<STR_LIT>': tags[i % <NUM_LIT:4>]<EOL>})<EOL>sys.stdout.flush()<EOL>pass<EOL><DEDENT>sys.stdout.write("<STR_LIT>" % message)<EOL>sys.stdout.flush()<EOL>pass<EOL>

print loading message on screen

.. note::

    loading message only write to `sys.stdout`

:param int wait: seconds to wait
:param str message: message to print
:return: None


def warn_message(self, message, fh=None, prefix="<STR_LIT>", suffix="<STR_LIT>"):

msg = prefix + message + suffix<EOL>fh = fh or sys.stdout<EOL>if fh is sys.stdout:<EOL><INDENT>termcolor.cprint(msg, color="<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>fh.write(msg)<EOL><DEDENT>pass<EOL>

print warn type message,
if file handle is `sys.stdout`, print color message

:param str message: message to print
:param file fh: file handle,default is `sys.stdout`
:param str prefix: message prefix,default is `[warn]`
:param str suffix: message suffix ,default is `...`
:return: None


return self.warn_message(*args, **kwargs)<EOL>


def error_message(self, message, fh=None, prefix="<STR_LIT>",<EOL>suffix="<STR_LIT>"):

msg = prefix + message + suffix<EOL>fh = fh or sys.stderr<EOL>if fh is sys.stderr:<EOL><INDENT>termcolor.cprint(msg, color="<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>fh.write(msg)<EOL><DEDENT>pass<EOL>

print error type message
if file handle is `sys.stderr`, print color message

:param str message: message to print
:param file fh: file handle, default is `sys.stdout`
:param str prefix: message prefix,default is `[error]`
:param str suffix: message suffix ,default is '...'
:return: None


def error(self, message=None, exit_code=<NUM_LIT:2>):

return self.parser.exit(exit_code, message)<EOL>

print error message and exit.
this method call `argparser.exit` to exit.

:param str message: message to print
:param int exit_code: exit code,default is 2

:return: None


try:<EOL><INDENT>if self.options.dry_run:<EOL><INDENT>def fake_system(cmd):<EOL><INDENT>self.print_message(cmd)<EOL>return fake_code<EOL><DEDENT>return fake_system(cmd)<EOL><DEDENT><DEDENT>except AttributeError:<EOL><INDENT>self.logger.warnning("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL>pass<EOL><DEDENT>return os.system(cmd)<EOL>

a built-in wrapper make dry-run easier.
you should use this instead use `os.system`

.. note::

    to use it,you need add '--dry-run' option in
    your argparser options

:param str cmd: command to execute
:param bool fake_code: only display command
    when is True,default is False
:return:


if root:<EOL><INDENT>full_path = root + '<STR_LIT:/>' + path.strip('<STR_LIT:/>')<EOL><DEDENT>else:<EOL><INDENT>full_path = path<EOL><DEDENT>buf = '<STR_LIT>'<EOL>try:<EOL><INDENT>buf = open(full_path).read()<EOL><DEDENT>except IOError:<EOL><INDENT>pkg, path = full_path.split('<STR_LIT:/>', <NUM_LIT:1>)<EOL>try:<EOL><INDENT>import pkg_resources<EOL>buf = pkg_resources.resource_string(pkg, path)<EOL>if type(buf) != str:<EOL><INDENT>buf = buf.decode('<STR_LIT:utf-8>')<EOL>pass<EOL><DEDENT><DEDENT>except AttributeError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>return buf<EOL>

.. warning::

    Experiment feature.

    BE CAREFUL! WE MAY REMOVE THIS FEATURE!

load resource file which in package.
this method is used to load file easier in different environment.

e.g:

consume we have a file named `resource.io` in package `cliez.conf`,
and we want to load it.
the easiest way may like this:

.. code-block:: python

    open('../conf/resource.io').read()

An obvious problem is `..` is relative path.
it will cause an error.

`load_resource` is designed for solve this problem.

The following code are equivalent:

.. code-block:: python

    a = Component()
    a.load_resource('resource.io', root='cliez/base')
    a.load_resource('base/resource.io', root='cliez')
    a.load_resource('/base/resource.io', root='cliez')
    a.load_resource('cliez/base/resource.io')
    a.load_resource(__file__.rsplit('/', 2)[0] +
    '/cliez/base/resource.io')

.. note::

    The document charset *MUST BE* utf-8

:param str path: file path
:param str root: root path

:return: str


desc = '<STR_LIT>'<EOL>try:<EOL><INDENT>desc = Component.load_resource(name, root=root)<EOL><DEDENT>except (IOError, ImportError):<EOL><INDENT>pass<EOL><DEDENT>return desc<EOL>

.. warning::

    Experiment feature.

    BE CAREFUL! WE MAY REMOVE THIS FEATURE!

Load resource file as description,
if resource file not exist,will return empty string.

:param str path: name resource path
:param str root: same as `load_resource` root
:return: `str`


pass<EOL>


return [<EOL>(('<STR_LIT>',), dict(help='<STR_LIT>')),<EOL>(('<STR_LIT:name>',), dict(nargs='<STR_LIT:?>', default='<STR_LIT>', help='<STR_LIT>')),<EOL>(('<STR_LIT>',),<EOL>dict(action='<STR_LIT:store_true>', help='<STR_LIT>')),<EOL>(('<STR_LIT>',),<EOL>dict(action='<STR_LIT:store_true>', help='<STR_LIT>')),<EOL>]<EOL>pass<EOL>

Create project.

By default cliez find github first,
if not found,then try to search bitbucket

if user define `--local` option. search local path first.

if user define `--bitbucket`, search bitbucket first,
then search github.


gitconfig_path = os.path.expanduser('<STR_LIT>')<EOL>if os.path.exists(gitconfig_path):<EOL><INDENT>parser = Parser()<EOL>parser.read(gitconfig_path)<EOL>parser.sections()<EOL>return parser<EOL><DEDENT>pass<EOL>

try use gitconfig info.
author,email etc.


def render(self, match_string, new_string):

current_dir = self.options.dir<EOL>if os.path.expanduser(current_dir) in ['<STR_LIT:/>', os.path.expanduser("<STR_LIT>")]:<EOL><INDENT>self.error("<STR_LIT>", -<NUM_LIT:1>)<EOL>pass<EOL><DEDENT>def match_directory(path):<EOL><INDENT>"""<STR_LIT>"""<EOL>skip = False<EOL>for include_dir in ['<STR_LIT>' % s for s in<EOL>self.exclude_directories]:<EOL><INDENT>if path.find(include_dir) > -<NUM_LIT:1>:<EOL><INDENT>skip = True<EOL>break<EOL><DEDENT>pass<EOL><DEDENT>return skip<EOL><DEDENT>for v in os.walk(current_dir):<EOL><INDENT>if os.path.basename(v[<NUM_LIT:0>]) in self.exclude_directories:<EOL><INDENT>continue<EOL><DEDENT>if match_directory(v[<NUM_LIT:0>]):<EOL><INDENT>continue<EOL><DEDENT>for base_name in v[<NUM_LIT:2>]:<EOL><INDENT>file_name = os.path.join(v[<NUM_LIT:0>], base_name)<EOL>try:<EOL><INDENT>with open(file_name, '<STR_LIT:r>') as fh:<EOL><INDENT>buffer = fh.read()<EOL>buffer = buffer.replace(match_string, new_string)<EOL>pass<EOL><DEDENT>with open(file_name, '<STR_LIT:w>') as fh:<EOL><INDENT>fh.write(buffer)<EOL>pass<EOL><DEDENT><DEDENT>except UnicodeDecodeError:<EOL><INDENT>continue<EOL><DEDENT>pass<EOL><DEDENT>pass<EOL><DEDENT>redo_directories = []<EOL>redo_files = []<EOL>for v in os.walk(current_dir):<EOL><INDENT>if os.path.basename(v[<NUM_LIT:0>]) in self.exclude_directories:<EOL><INDENT>continue<EOL><DEDENT>if match_directory(v[<NUM_LIT:0>]):<EOL><INDENT>continue<EOL><DEDENT>for sub_dir in v[<NUM_LIT:1>]:<EOL><INDENT>if match_string in sub_dir:<EOL><INDENT>redo_directories.append(os.path.join(v[<NUM_LIT:0>], sub_dir))<EOL>pass<EOL><DEDENT><DEDENT>for f in v[<NUM_LIT:2>]:<EOL><INDENT>if match_string in f:<EOL><INDENT>redo_files.append(os.path.join(v[<NUM_LIT:0>], f))<EOL>pass<EOL><DEDENT><DEDENT>pass<EOL><DEDENT>redo_directories.reverse()<EOL>redo_files.reverse()<EOL>for v in redo_files:<EOL><INDENT>dir_name = os.path.dirname(v)<EOL>file_name = os.path.basename(v)<EOL>shutil.move(v, os.path.join(<EOL>dir_name,<EOL>file_name.replace(match_string, new_string)))<EOL>pass<EOL><DEDENT>for v in redo_directories:<EOL><INDENT>shutil.move(v, v.replace(match_string, new_string))<EOL>pass<EOL><DEDENT>pass<EOL>

render template string to user string
:param str match_string: template string,syntax: '___VAR___'
:param str new_string: user string
:return:


return [<EOL>(('<STR_LIT>',), dict(action='<STR_LIT:store_true>', help='<STR_LIT>')),<EOL>(('<STR_LIT>', '<STR_LIT>'),<EOL>dict(nargs='<STR_LIT:+>', help='<STR_LIT>')),<EOL>(('<STR_LIT>',),<EOL>dict(action='<STR_LIT:store_true>', help='<STR_LIT>')),<EOL>]<EOL>


self.logger.debug("<STR_LIT>")<EOL>depends = ['<STR_LIT>']<EOL>nil_tools = []<EOL>self.logger.info("<STR_LIT>", depends)<EOL>for v in depends:<EOL><INDENT>real_path = shutil.which(v)<EOL>if real_path:<EOL><INDENT>self.print_message("<STR_LIT>"<EOL>"<STR_LIT>".format(v,<EOL>real_path,<EOL>termcolor.colored(<EOL>'<STR_LIT>',<EOL>color='<STR_LIT>')))<EOL><DEDENT>else:<EOL><INDENT>nil_tools.append(v)<EOL>self.error_message(<EOL>'<STR_LIT>'.format(v, '<STR_LIT>'), prefix='<STR_LIT>',<EOL>suffix='<STR_LIT>')<EOL><DEDENT>pass<EOL><DEDENT>if nil_tools:<EOL><INDENT>self.print_message('<STR_LIT>')<EOL>self.error("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>self.print_message("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>self.logger.debug("<STR_LIT>")<EOL>pass<EOL><DEDENT>pass<EOL>

.. todo::

    check network connection

:param Namespace options: parse result from argparse
:return:


return [<EOL>(('<STR_LIT>',), dict(action='<STR_LIT:store_true>', help='<STR_LIT>')),<EOL>(('<STR_LIT>', '<STR_LIT>'), dict(action='<STR_LIT:count>')),<EOL>]<EOL>


def include_file(filename, global_vars=None, local_vars=None):

if global_vars is None:<EOL><INDENT>global_vars = sys._getframe(<NUM_LIT:1>).f_globals<EOL><DEDENT>if local_vars is None:<EOL><INDENT>local_vars = sys._getframe(<NUM_LIT:1>).f_locals<EOL><DEDENT>with open(filename, '<STR_LIT:r>') as f:<EOL><INDENT>code = compile(f.read(), os.path.basename(filename), '<STR_LIT>')<EOL>exec(code, global_vars, local_vars)<EOL>pass<EOL><DEDENT>

.. deprecated 2.1::
    Don't use this any more.

    It's not pythonic.

include file like php include.

include is very useful when we need to split large config file


new_name = '<STR_LIT>'<EOL>pos = <NUM_LIT:0><EOL>for c in name:<EOL><INDENT>if pos == <NUM_LIT:0>:<EOL><INDENT>new_name = c.lower()<EOL><DEDENT>elif <NUM_LIT> <= ord(c) <= <NUM_LIT>:<EOL><INDENT>new_name += '<STR_LIT:_>' + c.lower()<EOL>pass<EOL><DEDENT>else:<EOL><INDENT>new_name += c<EOL><DEDENT>pos += <NUM_LIT:1><EOL>pass<EOL><DEDENT>return new_name<EOL>

Convert Hump style to underscore

:param name: Hump Character
:return: str


if path:<EOL><INDENT>Settings.bind(path, with_path=with_path)<EOL><DEDENT>return Settings._wrapped<EOL>

Get or set `Settings._wrapped`

:param str path: a python module file,
    if user set it,write config to `Settings._wrapped`
:param str with_path: search path
:return: A instance of `Settings`


if with_path:<EOL><INDENT>if os.path.isdir(with_path):<EOL><INDENT>sys.path.insert(<NUM_LIT:0>, with_path)<EOL><DEDENT>else:<EOL><INDENT>sys.path.insert(<NUM_LIT:0>, with_path.rsplit('<STR_LIT:/>', <NUM_LIT:2>)[<NUM_LIT:0>])<EOL><DEDENT>pass<EOL><DEDENT>mod = importlib.import_module(mod_path)<EOL>settings = Settings()<EOL>for v in dir(mod):<EOL><INDENT>if v[<NUM_LIT:0>] == '<STR_LIT:_>' or type(getattr(mod, v)).__name__ == '<STR_LIT>':<EOL><INDENT>continue<EOL><DEDENT>setattr(settings, v, getattr(mod, v))<EOL>pass<EOL><DEDENT>Settings._path = mod_path<EOL>Settings._wrapped = settings<EOL>return settings<EOL>

bind user variable to `_wrapped`

.. note::

    you don't need call this method by yourself.

    program will call it in  `cliez.parser.parse`

.. expection::

    if path is not correct,will cause an `ImportError`

:param str mod_path: module path, *use dot style,'mod.mod1'*
:param str with_path: add path to `sys.path`,
    if path is file,use its parent.
:return: A instance of `Settings`


def signal_handle(signum, frame):<EOL><INDENT>self.error("<STR_LIT>")<EOL>sys.exit(-<NUM_LIT:1>)<EOL>pass<EOL><DEDENT>signal.signal(signal.SIGINT, signal_handle)<EOL>pass<EOL>

设置信号处理

默认直接中断,考虑到一些场景用户需要等待线程结束.
可在此自定义操作

:return:


if self.options.exclusive_mode:<EOL><INDENT>import psutil<EOL>current_pid = os.getpid()<EOL>current = psutil.Process(current_pid).cmdline()<EOL>for pid in psutil.pids():<EOL><INDENT>p = psutil.Process(pid)<EOL>try:<EOL><INDENT>if current_pid != pid and current == p.cmdline():<EOL><INDENT>self.error_message(<EOL>"<STR_LIT>".format(p.pid))<EOL>sys.exit(-<NUM_LIT:1>)<EOL>pass<EOL><DEDENT><DEDENT>except psutil.ZombieProcess:<EOL><INDENT>pass<EOL><DEDENT>except psutil.AccessDenied:<EOL><INDENT>pass<EOL><DEDENT>pass<EOL><DEDENT>pass<EOL><DEDENT>pass<EOL>

检查是否是独占模式

参数顺序必须一致,也就是说如果参数顺序不一致,则判定为是两个不同的进程
这么设计是考虑到:

- 一般而言,排他模式的服务启动都是crontab等脚本来完成的,不存在顺序变更的可能
- 这在调试的时候可以帮助我们不需要结束原有进程就可以继续调试

:return:


self.set_signal()<EOL>self.check_exclusive_mode()<EOL>slot = self.Handle(self)<EOL>i = <NUM_LIT:0><EOL>while i < options.threads:<EOL><INDENT>t = threading.Thread(target=self.worker, args=[slot])<EOL>if options.once is True or options.no_daemon is True:<EOL><INDENT>t.daemon = False<EOL><DEDENT>else:<EOL><INDENT>t.daemon = True<EOL><DEDENT>t.start()<EOL>i += <NUM_LIT:1><EOL><DEDENT>if options.once is False:<EOL><INDENT>while True:<EOL><INDENT>if threading.active_count() > <NUM_LIT:1>:<EOL><INDENT>sleep(<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>if threading.current_thread().name == "<STR_LIT>":<EOL><INDENT>sys.exit(<NUM_LIT:0>)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>pass<EOL>

In general, you don't need to overwrite this method.

:param options:
:return:


if filters is None:<EOL><INDENT>filters = [cls(self) for cls in self.MESSAGE_FILTERS]<EOL><DEDENT>url = await self._get_socket_url()<EOL>logger.debug('<STR_LIT>', url)<EOL>async with ws_connect(url) as socket:<EOL><INDENT>first_msg = await socket.receive()<EOL>self._validate_first_message(first_msg)<EOL>self.socket = socket<EOL>async for message in socket:<EOL><INDENT>if message.tp == MsgType.text:<EOL><INDENT>await self.handle_message(message, filters)<EOL><DEDENT>elif message.tp in (MsgType.closed, MsgType.error):<EOL><INDENT>if not socket.closed:<EOL><INDENT>await socket.close()<EOL><DEDENT>self.socket = None<EOL>break<EOL><DEDENT><DEDENT><DEDENT>logger.info('<STR_LIT>')<EOL>

Join the real-time messaging service.

        Arguments:
          filters (:py:class:`dict`, optional): Dictionary mapping
            message filters to the functions they should dispatch to.
            Use a :py:class:`collections.OrderedDict` if precedence is
            important; only one filter, the first match, will be
            applied to each message.


async def handle_message(self, message, filters):

data = self._unpack_message(message)<EOL>logger.debug(data)<EOL>if data.get('<STR_LIT:type>') == '<STR_LIT:error>':<EOL><INDENT>raise SlackApiError(<EOL>data.get('<STR_LIT:error>', {}).get('<STR_LIT>', str(data))<EOL>)<EOL><DEDENT>elif self.message_is_to_me(data):<EOL><INDENT>text = data['<STR_LIT:text>'][len(self.address_as):].strip()<EOL>if text == '<STR_LIT>':<EOL><INDENT>return self._respond(<EOL>channel=data['<STR_LIT>'],<EOL>text=self._instruction_list(filters),<EOL>)<EOL><DEDENT>elif text == '<STR_LIT:version>':<EOL><INDENT>return self._respond(<EOL>channel=data['<STR_LIT>'],<EOL>text=self.VERSION,<EOL>)<EOL><DEDENT><DEDENT>for _filter in filters:<EOL><INDENT>if _filter.matches(data):<EOL><INDENT>logger.debug('<STR_LIT>')<EOL>async for response in _filter:<EOL><INDENT>self._respond(channel=data['<STR_LIT>'], text=response)<EOL><DEDENT><DEDENT><DEDENT>

Handle an incoming message appropriately.

        Arguments:
          message (:py:class:`aiohttp.websocket.Message`): The incoming
            message to handle.
          filters (:py:class:`list`): The filters to apply to incoming
            messages.


return (data.get('<STR_LIT:type>') == '<STR_LIT:message>' and<EOL>self.full_name in data.get('<STR_LIT:text>', '<STR_LIT>'))<EOL>


return (data.get('<STR_LIT:type>') == '<STR_LIT:message>' and<EOL>data.get('<STR_LIT:text>', '<STR_LIT>').startswith(self.address_as))<EOL>


@classmethod<EOL><INDENT>async def from_api_token(cls, token=None, api_cls=SlackBotApi):<DEDENT>

api = api_cls.from_env() if token is None else api_cls(api_token=token)<EOL>data = await api.execute_method(cls.API_AUTH_ENDPOINT)<EOL>return cls(data['<STR_LIT>'], data['<STR_LIT:user>'], api)<EOL>

Create a new instance from the API token.

        Arguments:
          token (:py:class:`str`, optional): The bot's API token
            (defaults to ``None``, which means looking in the
            environment).
          api_cls (:py:class:`type`, optional): The class to create
            as the ``api`` argument for API access (defaults to
            :py:class:`aslack.slack_api.SlackBotApi`).

        Returns:
          :py:class:`SlackBot`: The new instance.


def _format_message(self, channel, text):

payload = {'<STR_LIT:type>': '<STR_LIT:message>', '<STR_LIT:id>': next(self._msg_ids)}<EOL>payload.update(channel=channel, text=text)<EOL>return json.dumps(payload)<EOL>

Format an outgoing message for transmission.

        Note:
          Adds the message type (``'message'``) and incremental ID.

        Arguments:
          channel (:py:class:`str`): The channel to send to.
          text (:py:class:`str`): The message text to send.

        Returns:
          :py:class:`str`: The JSON string of the message.


data = await self.api.execute_method(<EOL>self.RTM_START_ENDPOINT,<EOL>simple_latest=True,<EOL>no_unreads=True,<EOL>)<EOL>return data['<STR_LIT:url>']<EOL>

Get the WebSocket URL for the RTM session.

        Warning:
          The URL expires if the session is not joined within 30
          seconds of the API call to the start endpoint.

        Returns:
          :py:class:`str`: The socket URL.


return '<STR_LIT>'.join([<EOL>self.INSTRUCTIONS.strip(),<EOL>'<STR_LIT>',<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(self.user),<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(self.user),<EOL>] + [filter.description() for filter in filters])<EOL>

Generates the instructions for a bot and its filters.

        Note:
          The guidance for each filter is generated by combining the
          docstrings of the predicate filter and resulting dispatch
          function with a single space between. The class's
          :py:attr:`INSTRUCTIONS` and the default help command are
          added.

        Arguments:
          filters (:py:class:`list`): The filters to apply to incoming
            messages.

        Returns:
          :py:class:`str`: The bot's instructions.


result = self._format_message(channel, text)<EOL>if result is not None:<EOL><INDENT>logger.info(<EOL>'<STR_LIT>',<EOL>truncate(result, max_len=<NUM_LIT:50>),<EOL>)<EOL><DEDENT>self.socket.send_str(result)<EOL>

Respond to a message on the current socket.

        Args:
          channel (:py:class:`str`): The channel to send to.
          text (:py:class:`str`): The message text to send.


return json.loads(msg.data)<EOL>

Unpack the data from the message.

        Arguments:
          msg (:py:class:`aiohttp.websocket.Message`): The message to
            unpack.

        Returns:
          :py:class:`dict`: The loaded data.

        Raises:
          :py:class:`AttributeError`: If there is no data attribute.
          :py:class:`json.JSONDecodeError`: If the data isn't valid
            JSON.


data = cls._unpack_message(msg)<EOL>logger.debug(data)<EOL>if data != cls.RTM_HANDSHAKE:<EOL><INDENT>raise SlackApiError('<STR_LIT>'.format(data))<EOL><DEDENT>logger.info('<STR_LIT>')<EOL>

Check the first message matches the expected handshake.

        Note:
          The handshake is provided as :py:attr:`RTM_HANDSHAKE`.

        Arguments:
          msg (:py:class:`aiohttp.Message`): The message to validate.

        Raises:
          :py:class:`SlackApiError`: If the data doesn't match the
            expected handshake.


if self._description is None:<EOL><INDENT>text = '<STR_LIT:\n>'.join(self.__doc__.splitlines()[<NUM_LIT:1>:]).strip()<EOL>lines = []<EOL>for line in map(str.strip, text.splitlines()):<EOL><INDENT>if line and lines:<EOL><INDENT>lines[-<NUM_LIT:1>] = '<STR_LIT:U+0020>'.join((lines[-<NUM_LIT:1>], line))<EOL><DEDENT>elif line:<EOL><INDENT>lines.append(line)<EOL><DEDENT>else:<EOL><INDENT>lines.append('<STR_LIT>')<EOL><DEDENT><DEDENT>self._description = '<STR_LIT:\n>'.join(lines)<EOL><DEDENT>return self._description<EOL>

A user-friendly description of the handler.

        Returns:
          :py:class:`str`: The handler's description.


self.text = data.get('<STR_LIT:text>')<EOL>return True<EOL>

Whether the handler should handle the current message.

        Args:
          data: The data representing the current message.

        Returns:
          :py:class:`bool`: Whether it should be handled.


def api_subclass_factory(name, docstring, remove_methods, base=SlackApi):

methods = deepcopy(base.API_METHODS)<EOL>for parent, to_remove in remove_methods.items():<EOL><INDENT>if to_remove is ALL:<EOL><INDENT>del methods[parent]<EOL><DEDENT>else:<EOL><INDENT>for method in to_remove:<EOL><INDENT>del methods[parent][method]<EOL><DEDENT><DEDENT><DEDENT>return type(name, (base,), dict(API_METHODS=methods, __doc__=docstring))<EOL>

Create an API subclass with fewer methods than its base class.

    Arguments:
      name (:py:class:`str`): The name of the new class.
      docstring (:py:class:`str`): The docstring for the new class.
      remove_methods (:py:class:`dict`): The methods to remove from
        the base class's :py:attr:`API_METHODS` for the subclass. The
        key is the name of the root method (e.g. ``'auth'`` for
        ``'auth.test'``, the value is either a tuple of child method
        names (e.g. ``('test',)``) or, if all children should be
        removed, the special value :py:const:`ALL`.
      base (:py:class:`type`, optional): The base class (defaults to
        :py:class:`SlackApi`).

    Returns:
      :py:class:`type`: The new subclass.

    Raises:
      :py:class:`KeyError`: If the method wasn't in the superclass.


async def execute_method(self, method, **params):

url = self.url_builder(method, url_params=params)<EOL>logger.info('<STR_LIT>', method)<EOL>response = await aiohttp.get(url)<EOL>logger.info('<STR_LIT>', response.status)<EOL>if response.status == <NUM_LIT:200>:<EOL><INDENT>json = await response.json()<EOL>logger.debug('<STR_LIT>', json)<EOL>if json.get('<STR_LIT>'):<EOL><INDENT>return json<EOL><DEDENT>raise SlackApiError(json['<STR_LIT:error>'])<EOL><DEDENT>else:<EOL><INDENT>raise_for_status(response)<EOL><DEDENT>

Execute a specified Slack Web API method.

        Arguments:
          method (:py:class:`str`): The name of the method.
          **params (:py:class:`dict`): Any additional parameters
            required.

        Returns:
          :py:class:`dict`: The JSON data from the response.

        Raises:
          :py:class:`aiohttp.web_exceptions.HTTPException`: If the HTTP
            request returns a code other than 200 (OK).
          SlackApiError: If the Slack API is reached but the response
           contains an error message.


methods = cls.API_METHODS<EOL>for key in method.split('<STR_LIT:.>'):<EOL><INDENT>methods = methods.get(key)<EOL>if methods is None:<EOL><INDENT>break<EOL><DEDENT><DEDENT>if isinstance(methods, str):<EOL><INDENT>logger.debug('<STR_LIT>', method, methods)<EOL>return True<EOL><DEDENT>return False<EOL>

Whether a given method exists in the known API.

        Arguments:
          method (:py:class:`str`): The name of the method.

        Returns:
          :py:class:`bool`: Whether the method is in the known API.


return {}<EOL>

Get the headers for the service requests.

        Returns:
          :py:class:`dict`: The header mapping.


def url_builder(self, endpoint, *, root=None, params=None, url_params=None):

if root is None:<EOL><INDENT>root = self.ROOT<EOL><DEDENT>scheme, netloc, path, _, _ = urlsplit(root)<EOL>return urlunsplit((<EOL>scheme,<EOL>netloc,<EOL>urljoin(path, endpoint),<EOL>urlencode(url_params or {}),<EOL>'<STR_LIT>',<EOL>)).format(**params or {})<EOL>

Create a URL for the specified endpoint.

        Arguments:
          endpoint (:py:class:`str`): The API endpoint to access.
          root: (:py:class:`str`, optional): The root URL for the
            service API.
          params: (:py:class:`dict`, optional): The values for format
            into the created URL (defaults to ``None``).
          url_params: (:py:class:`dict`, optional): Parameters to add
            to the end of the URL (defaults to ``None``).

        Returns:
          :py:class:`str`: The resulting URL.


token = getenv(cls.TOKEN_ENV_VAR)<EOL>if token is None:<EOL><INDENT>msg = '<STR_LIT>'.format(cls.TOKEN_ENV_VAR)<EOL>raise ValueError(msg)<EOL><DEDENT>return cls(api_token=token)<EOL>

Create a service instance from an environment variable.


def url_builder(self, endpoint, params=None, url_params=None):

if url_params is None:<EOL><INDENT>url_params = OrderedDict()<EOL><DEDENT>url_params[self.AUTH_PARAM] = self.api_token<EOL>return super().url_builder(<EOL>endpoint,<EOL>params=params,<EOL>url_params=url_params,<EOL>)<EOL>


for err_name in web_exceptions.__all__:<EOL><INDENT>err = getattr(web_exceptions, err_name)<EOL>if err.status_code == response.status:<EOL><INDENT>payload = dict(<EOL>headers=response.headers,<EOL>reason=response.reason,<EOL>)<EOL>if issubclass(err, web_exceptions._HTTPMove):  <EOL><INDENT>raise err(response.headers['<STR_LIT>'], **payload)<EOL><DEDENT>raise err(**payload)<EOL><DEDENT><DEDENT>

Raise an appropriate error for a given response.

    Arguments:
      response (:py:class:`aiohttp.ClientResponse`): The API response.

    Raises:
      :py:class:`aiohttp.web_exceptions.HTTPException`: The appropriate
        error for the response's status.


def truncate(text, max_len=<NUM_LIT>, end='<STR_LIT>'):

if len(text) <= max_len:<EOL><INDENT>return text<EOL><DEDENT>return text[:max_len].rsplit('<STR_LIT:U+0020>', maxsplit=<NUM_LIT:1>)[<NUM_LIT:0>] + end<EOL>

Truncate the supplied text for display.

    Arguments:
      text (:py:class:`str`): The text to truncate.
      max_len (:py:class:`int`, optional): The maximum length of the
        text before truncation (defaults to 350 characters).
      end (:py:class:`str`, optional): The ending to use to show that
        the text was truncated (defaults to ``'...'``).

    Returns:
      :py:class:`str`: The truncated text.


def connect(self, axis0, n0_index, source_angle, axis1, n1_index, target_angle, **kwargs):

n0    = axis0.nodes[n0_index]<EOL>n1    = axis1.nodes[n1_index]<EOL>pth  = self.dwg.path(d="<STR_LIT>" % (n0.x, n0.y), fill='<STR_LIT:none>', **kwargs) <EOL>alfa = axis0.angle() + radians(source_angle)<EOL>length = sqrt( ((n0.x - axis0.start[<NUM_LIT:0>])**<NUM_LIT:2>) + ((n0.y-axis0.start[<NUM_LIT:1>])**<NUM_LIT:2>)) <EOL>x = axis0.start[<NUM_LIT:0>] + length * cos(alfa);<EOL>y = axis0.start[<NUM_LIT:1>] + length * sin(alfa);<EOL>pth.push("<STR_LIT>" % (x, y)) <EOL>alfa = axis1.angle() + radians(target_angle)<EOL>length = sqrt( ((n1.x - axis1.start[<NUM_LIT:0>])**<NUM_LIT:2>) + ((n1.y-axis1.start[<NUM_LIT:1>])**<NUM_LIT:2>)) <EOL>x = axis1.start[<NUM_LIT:0>] + length * cos(alfa);<EOL>y = axis1.start[<NUM_LIT:1>] + length * sin(alfa);<EOL>pth.push("<STR_LIT>" % (x, y))   <EOL>pth.push("<STR_LIT>" % (n1.x, n1.y)) <EOL>self.dwg.add(pth)<EOL>

Draw edges as Bézier curves.

        Start and end points map to the coordinates of the given nodes
        which in turn are set when adding nodes to an axis with the
        Axis.add_node() method, by using the placement information of
        the axis and a specified offset from its start point.

        Control points are set at the same distance from the start (or end)
        point of an axis as their corresponding nodes, but along an invisible
        axis that shares its origin but diverges by a given angle.

        Parameters
        ----------
        axis0        : source Axis object
        n0_index     : key of source node in nodes dictionary of axis0
        source_angle : angle of departure for invisible axis that diverges from axis0 and holds first control points
        axis1        : target Axis object
        n1_index     : key of target node in nodes dictionary of axis1
        target_angle : angle of departure for invisible axis that diverges from axis1 and holds second control points
        kwargs       : extra SVG attributes for path element, optional
                       Set or change attributes using key=value


def __init__( self, start=(<NUM_LIT:0>,<NUM_LIT:0>), end=(<NUM_LIT:0>,<NUM_LIT:0>), **kwargs):

self.start = start<EOL>self.end   = end<EOL>self.nodes = {}<EOL>self.dwg   = svgwrite.Drawing()<EOL>self.attrs = kwargs<EOL>

Initialize Axis object with start, end positions and optional SVG attributes

        Parameters
        ----------
        start  : ( x, y ) start point of the axis
        end    : (x1, y1) end point of the axis
        kwargs : extra SVG attributes for line element, optional
                 Set or change attributes using key=value

        Example
        -------
        >>> axis0 = Axis( (150, 200), # start
                          (150, 0),   # end
                          stroke="black", stroke_width=1.5) # pass SVG attributes of axes


<EOL>width  = self.end[<NUM_LIT:0>] - self.start[<NUM_LIT:0>]<EOL>height = self.end[<NUM_LIT:1>] - self.start[<NUM_LIT:1>]        <EOL>node.x = self.start[<NUM_LIT:0>] + (width * offset)<EOL>node.y = self.start[<NUM_LIT:1>] + (height * offset)<EOL>self.nodes[node.ID] = node<EOL>

Add a Node object to nodes dictionary, calculating its coordinates using offset

        Parameters
        ----------
        node   : a Node object
        offset : float
                 number between 0 and 1 that sets the distance
                 from the start point at which the node will be placed


self.ID = ID<EOL>self.x = <NUM_LIT:0><EOL>self.y = <NUM_LIT:0><EOL>self.dwg   = svgwrite.Drawing()<EOL>

Parameters
----------
ID: a unique key for the nodes dict of an axis.


def notify(self, message, title="<STR_LIT>", **kwargs):

logger.info(<EOL>"<STR_LIT>".format(title, message))<EOL>try:<EOL><INDENT>data = {<EOL>'<STR_LIT>': self._token,<EOL>'<STR_LIT:user>': self._user,<EOL>'<STR_LIT:title>': title,<EOL>'<STR_LIT:message>': message,<EOL>}<EOL>data.update(kwargs)<EOL>payload = []<EOL>for (k, v) in data.items():<EOL><INDENT>if isinstance(v, str_type):<EOL><INDENT>payload.append((k, v.encode("<STR_LIT:utf-8>")))<EOL><DEDENT>else:<EOL><INDENT>payload.append((k, v))<EOL><DEDENT><DEDENT>headers = {"<STR_LIT>": "<STR_LIT>",}<EOL>conn = HTTPSConnection("<STR_LIT>")<EOL>params = urlencode(payload)<EOL>conn.request("<STR_LIT:POST>", "<STR_LIT>", params, headers)<EOL>rsp = conn.getresponse()<EOL>if rsp.status != <NUM_LIT:200>:<EOL><INDENT>raise PushoverException("<STR_LIT>".format(rsp.status))<EOL><DEDENT>conn.close()<EOL><DEDENT>except Exception as e:<EOL><INDENT>raise PushoverException("<STR_LIT>".format(e))<EOL><DEDENT>

priority (-2 -1 0 1 2)
sound (bike,bugle,cashregister,classical,cosmic,falling,gamelan,
       incoming,intermission,magic,mechanical,pianobar,siren,spacealarm,
       tugboat,alien,climb,persistent,echo,updown,none)


try:<EOL><INDENT>return os.path.join(sys._MEIPASS, relative_paths[-<NUM_LIT:1>])<EOL><DEDENT>except Exception:<EOL><INDENT>for prefix in prefixes:<EOL><INDENT>for relative_path in relative_paths:<EOL><INDENT>path = os.path.join(prefix, relative_path)<EOL>if os.path.exists(path):<EOL><INDENT>return path<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT><DEDENT>

Taken from https://github.com/tokland/youtube-upload/blob/master/youtube_upload/main.py
Get the first existing filename of relative_path seeking on prefixes directories.


if any(not x for x in (self._ename.value, self._p1.value, self._p2.value, self._file.value)):<EOL><INDENT>print("<STR_LIT>")<EOL>return<EOL><DEDENT>self.__p1chars = []<EOL>self.__p2chars = []<EOL>options = Namespace()<EOL>self.__history.append(self.__save_form())<EOL>options.ename = self._ename.value<EOL>if self._ename_min.value:<EOL><INDENT>options.ename_min = self._ename_min.value<EOL><DEDENT>else:<EOL><INDENT>options.ename_min = options.ename<EOL><DEDENT>options.pID = self._pID.value<EOL>options.mtype = self._mtype.value<EOL>options.mmid = options.mtype<EOL>options.p1 = self._p1.value<EOL>options.p2 = self._p2.value<EOL>options.p1char = self._p1char.value<EOL>options.p2char = self._p2char.value<EOL>options.bracket = self._bracket.value<EOL>isadir = os.path.isdir(self._file.value)<EOL>if isadir:<EOL><INDENT>options.file = max([os.path.join(self._file.value, f) for f in os.listdir(self._file.value) if os.path.isfile(os.path.join(self._file.value, f))], key=os.path.getmtime)<EOL><DEDENT>else:<EOL><INDENT>options.file = self._file.value<EOL><DEDENT>options.tags = self._tags.value<EOL>options.msuffix = self._msuffix.value<EOL>options.mprefix = self._mprefix.value<EOL>options.privacy = self._privacy.value<EOL>options.descrip = self._description.value<EOL>options.titleformat = self._titleformat.value<EOL>if self._p1sponsor.value:<EOL><INDENT>options.p1 = "<STR_LIT>".join((self._p1sponsor.value, options.p1))<EOL><DEDENT>if self._p2sponsor.value:<EOL><INDENT>options.p2 = "<STR_LIT>".join((self._p2sponsor.value, options.p2))<EOL><DEDENT>options.ignore = False<EOL>self.__reset_match(False, isadir)<EOL>self.__add_to_qview(options)<EOL>self._queueref.append(options)<EOL>if consts.firstrun:<EOL><INDENT>thr = threading.Thread(target=self.__worker)<EOL>thr.daemon = True<EOL>thr.start()<EOL>consts.firstrun = False<EOL><DEDENT>


if not values:<EOL><INDENT>return values<EOL><DEDENT>values = sorted(values)<EOL>first_word = next(<EOL>(cnt for cnt, val in enumerate(values)<EOL>if val and not val[<NUM_LIT:0>].isdigit()),<EOL>None<EOL>)<EOL>if first_word is None:<EOL><INDENT>return values<EOL><DEDENT>words = values[first_word:]<EOL>numbers = values[:first_word]<EOL>return words + numbers<EOL>

Sort values, but put numbers after alphabetically sorted words.

This function is here to make outputs diff-compatible with Aleph.

Example::
    >>> sorted(["b", "1", "a"])
    ['1', 'a', 'b']
    >>> resorted(["b", "1", "a"])
    ['a', 'b', '1']

Args:
    values (iterable): any iterable object/list/tuple/whatever.

Returns:
    list of sorted values, but with numbers after words


def __init__(self, xml=None, resort=True):

self.leader = None<EOL>self.oai_marc = False<EOL>self.controlfields = OrderedDict()<EOL>self.datafields = OrderedDict()<EOL>self.valid_i_chars = set(list("<STR_LIT>"))<EOL>self.resorted = tools.resorted if resort else lambda x: x<EOL>if hasattr(xml, "<STR_LIT>"):<EOL><INDENT>xml = xml.read()<EOL><DEDENT>if xml is not None:<EOL><INDENT>self._original_xml = xml<EOL>self._parse_string(xml)<EOL><DEDENT>

Constructor.

Args:
    xml (str/file, default None): XML to be parsed. May be file-like
        object.
    resort (bool, default True): Sort the output alphabetically?


if not isinstance(xml, HTMLElement):<EOL><INDENT>xml = dhtmlparser.parseString(str(xml))<EOL><DEDENT>record = xml.find("<STR_LIT>")<EOL>if not record:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>record = record[<NUM_LIT:0>]<EOL>self.oai_marc = len(record.find("<STR_LIT>")) > <NUM_LIT:0><EOL>if not self.oai_marc:<EOL><INDENT>leader = record.find("<STR_LIT>")<EOL>if len(leader) >= <NUM_LIT:1>:<EOL><INDENT>self.leader = leader[<NUM_LIT:0>].getContent()<EOL><DEDENT><DEDENT>if self.oai_marc:<EOL><INDENT>self._parse_control_fields(record.find("<STR_LIT>"), "<STR_LIT:id>")<EOL>self._parse_data_fields(record.find("<STR_LIT>"), "<STR_LIT:id>", "<STR_LIT:label>")<EOL><DEDENT>else:<EOL><INDENT>self._parse_control_fields(record.find("<STR_LIT>"), "<STR_LIT>")<EOL>self._parse_data_fields(record.find("<STR_LIT>"), "<STR_LIT>", "<STR_LIT:code>")<EOL><DEDENT>if self.oai_marc and "<STR_LIT>" in self.controlfields:<EOL><INDENT>self.leader = self.controlfields["<STR_LIT>"]<EOL><DEDENT>

Parse MARC XML document to dicts, which are contained in
self.controlfields and self.datafields.

Args:
    xml (str or HTMLElement): input data

Also detect if this is oai marc format or not (see elf.oai_marc).


def _parse_control_fields(self, fields, tag_id="<STR_LIT>"):

for field in fields:<EOL><INDENT>params = field.params<EOL>if tag_id not in params:<EOL><INDENT>continue<EOL><DEDENT>self.controlfields[params[tag_id]] = field.getContent().strip()<EOL><DEDENT>

Parse control fields.

Args:
    fields (list): list of HTMLElements
    tag_id (str):  parameter name, which holds the information, about
                   field name this is normally "tag", but in case of
                   oai_marc "id".


def _parse_data_fields(self, fields, tag_id="<STR_LIT>", sub_id="<STR_LIT:code>"):

for field in fields:<EOL><INDENT>params = field.params<EOL>if tag_id not in params:<EOL><INDENT>continue<EOL><DEDENT>field_repr = OrderedDict([<EOL>[self.i1_name, params.get(self.i1_name, "<STR_LIT:U+0020>")],<EOL>[self.i2_name, params.get(self.i2_name, "<STR_LIT:U+0020>")],<EOL>])<EOL>for subfield in field.find("<STR_LIT>"):<EOL><INDENT>if sub_id not in subfield.params:<EOL><INDENT>continue<EOL><DEDENT>content = MARCSubrecord(<EOL>val=subfield.getContent().strip(),<EOL>i1=field_repr[self.i1_name],<EOL>i2=field_repr[self.i2_name],<EOL>other_subfields=field_repr<EOL>)<EOL>code = subfield.params[sub_id]<EOL>if code in field_repr:<EOL><INDENT>field_repr[code].append(content)<EOL><DEDENT>else:<EOL><INDENT>field_repr[code] = [content]<EOL><DEDENT><DEDENT>tag = params[tag_id]<EOL>if tag in self.datafields:<EOL><INDENT>self.datafields[tag].append(field_repr)<EOL><DEDENT>else:<EOL><INDENT>self.datafields[tag] = [field_repr]<EOL><DEDENT><DEDENT>

Parse data fields.

Args:
    fields (list): of HTMLElements
    tag_id (str): parameter name, which holds the information, about
                  field name this is normally "tag", but in case of
                  oai_marc "id"
    sub_id (str): id of parameter, which holds informations about
                  subfield name this is normally "code" but in case of
                  oai_marc "label"


if len(name) != <NUM_LIT:3>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>self.controlfields[name] = value<EOL>

Add new control field `value` with under `name` into control field
dictionary :attr:`controlfields`.


def add_data_field(self, name, i1, i2, subfields_dict):

if i1 not in self.valid_i_chars:<EOL><INDENT>raise ValueError("<STR_LIT>" + i1 + "<STR_LIT>")<EOL><DEDENT>if i2 not in self.valid_i_chars:<EOL><INDENT>raise ValueError("<STR_LIT>" + i2 + "<STR_LIT>")<EOL><DEDENT>if len(name) != <NUM_LIT:3>:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if not subfields_dict:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if not isinstance(subfields_dict, dict):<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>subrecords = []<EOL>for key, val in subfields_dict.items():<EOL><INDENT>if len(key) > <NUM_LIT:1>:<EOL><INDENT>raise KeyError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if not isinstance(val, list):<EOL><INDENT>val = [val]<EOL><DEDENT>subfields = map(<EOL>lambda x: MARCSubrecord(x, i1, i2, None),<EOL>val<EOL>)<EOL>subfields_dict[key] = subfields<EOL>subrecords.extend(subfields)<EOL><DEDENT>subfields_dict[self.i1_name] = i1<EOL>subfields_dict[self.i2_name] = i2<EOL>if name in self.datafields:<EOL><INDENT>self.datafields[name].append(subfields_dict)<EOL><DEDENT>else:<EOL><INDENT>self.datafields[name] = [subfields_dict]<EOL><DEDENT>other_subfields = self.datafields[name]<EOL>for record in subrecords:<EOL><INDENT>record.other_subfields = other_subfields<EOL><DEDENT>

Add new datafield into :attr:`datafields` and take care of OAI MARC
differencies.

Args:
    name (str): Name of datafield.
    i1 (char): Value of i1/ind1 parameter.
    i2 (char): Value of i2/ind2 parameter.
    subfields_dict (dict): Dictionary containing subfields (as list).

`subfields_dict` is expected to be in this format::

    {
        "field_id": ["subfield data",],
        ...
        "z": ["X0456b"]
    }

Warning:
    For your own good, use OrderedDict for `subfields_dict`, or
    constructor's `resort` parameter set to ``True`` (it is by
    default).

Warning:
    ``field_id`` can be only one character long!


if num not in (<NUM_LIT:1>, <NUM_LIT:2>):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if is_oai is None:<EOL><INDENT>is_oai = self.oai_marc<EOL><DEDENT>i_name = "<STR_LIT>" if not is_oai else "<STR_LIT:i>"<EOL>return i_name + str(num)<EOL>

This method is used mainly internally, but it can be handy if you work
with with raw MARC XML object and not using getters.

Args:
    num (int): Which indicator you need (1/2).
    is_oai (bool/None): If None, :attr:`.oai_marc` is
           used.

Returns:
    str: current name of ``i1``/``ind1`` parameter based on \
         :attr:`oai_marc` property.


return self.get_i_name(<NUM_LIT:1>)<EOL>

Property getter / alias for ``self.get_i_name(1)``.


return self.get_i_name(<NUM_LIT:2>)<EOL>

Property getter / alias for ``self.get_i_name(2)``.


def get_ctl_field(self, controlfield, alt=None):

if not alt:<EOL><INDENT>return self.controlfields[controlfield]<EOL><DEDENT>return self.controlfields.get(controlfield, alt)<EOL>

Method wrapper over :attr:`.controlfields` dictionary.

Args:
    controlfield (str): Name of the controlfield.
    alt (object, default None): Alternative value of the `controlfield`
        when `controlfield` couldn't be found.

Returns:
    str: record from given `controlfield`


def getDataRecords(self, datafield, subfield, throw_exceptions=True):

return self.get_subfields(<EOL>datafield=datafield,<EOL>subfield=subfield,<EOL>exception=throw_exceptions<EOL>)<EOL>

.. deprecated::
    Use :func:`get_subfields` instead.


def get_subfields(self, datafield, subfield, i1=None, i2=None,<EOL>exception=False):

if len(datafield) != <NUM_LIT:3>:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if len(subfield) != <NUM_LIT:1>:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if datafield not in self.datafields:<EOL><INDENT>if exception:<EOL><INDENT>raise KeyError(datafield + "<STR_LIT>")<EOL><DEDENT>return []<EOL><DEDENT>output = []<EOL>for datafield in self.datafields[datafield]:<EOL><INDENT>if subfield not in datafield:<EOL><INDENT>continue<EOL><DEDENT>for sfield in datafield[subfield]:<EOL><INDENT>if i1 and sfield.i1 != i1:<EOL><INDENT>continue<EOL><DEDENT>if i2 and sfield.i2 != i2:<EOL><INDENT>continue<EOL><DEDENT>output.append(sfield)<EOL><DEDENT><DEDENT>if not output and exception:<EOL><INDENT>raise KeyError(subfield + "<STR_LIT>")<EOL><DEDENT>return output<EOL>

Return content of given `subfield` in `datafield`.

Args:
    datafield (str): Section name (for example "001", "100", "700").
    subfield (str):  Subfield name (for example "a", "1", etc..).
    i1 (str, default None): Optional i1/ind1 parameter value, which
       will be used for search.
    i2 (str, default None): Optional i2/ind2 parameter value, which
       will be used for search.
    exception (bool): If ``True``, :exc:`~exceptions.KeyError` is
              raised when method couldn't found given `datafield` /
              `subfield`. If ``False``, blank array ``[]`` is returned.

Returns:
    list: of :class:`.MARCSubrecord`.

Raises:
    KeyError: If the subfield or datafield couldn't be found.

Note:
    MARCSubrecord is practically same thing as string, but has defined
    :meth:`.MARCSubrecord.i1` and :attr:`.MARCSubrecord.i2`
    methods.

    You may need to be able to get this, because MARC XML depends on
    i/ind parameters from time to time (names of authors for example).


<EOL>if hasattr(xml, "<STR_LIT>"):<EOL><INDENT>xml = xml.read()<EOL><DEDENT>dom = None<EOL>try:<EOL><INDENT>dom = dhtmlparser.parseString(xml)<EOL><DEDENT>except UnicodeError:<EOL><INDENT>dom = dhtmlparser.parseString(xml.encode("<STR_LIT:utf-8>"))<EOL><DEDENT>for record_xml in dom.findB("<STR_LIT>"):<EOL><INDENT>yield MARCXMLRecord(record_xml)<EOL><DEDENT>

Iterate over all ``<record>`` tags in `xml`.

Args:
    xml (str/file): Input string with XML. UTF-8 is prefered encoding,
                    unicode should be ok.

Yields:
    MARCXMLRecord: For each corresponding ``<record>``.


def _undefined_pattern(value, fn, undefined):

if fn(value):<EOL><INDENT>return undefined<EOL><DEDENT>return value<EOL>

If ``fn(value) == True``, return `undefined`, else `value`.


def _parse_corporations(self, datafield, subfield, roles=["<STR_LIT>"]):

if len(datafield) != <NUM_LIT:3>:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if len(subfield) != <NUM_LIT:1>:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>parsed_corporations = []<EOL>for corporation in self.get_subfields(datafield, subfield):<EOL><INDENT>other_subfields = corporation.other_subfields<EOL>if "<STR_LIT:4>" in other_subfields and roles != ["<STR_LIT>"]:<EOL><INDENT>corp_roles = other_subfields["<STR_LIT:4>"]  <EOL>relevant = any(map(lambda role: role in roles, corp_roles))<EOL>if not relevant:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>name = "<STR_LIT>"<EOL>place = "<STR_LIT>"<EOL>date = "<STR_LIT>"<EOL>name = corporation<EOL>if "<STR_LIT:c>" in other_subfields:<EOL><INDENT>place = "<STR_LIT:U+002C>".join(other_subfields["<STR_LIT:c>"])<EOL><DEDENT>if "<STR_LIT:d>" in other_subfields:<EOL><INDENT>date = "<STR_LIT:U+002C>".join(other_subfields["<STR_LIT:d>"])<EOL><DEDENT>parsed_corporations.append(Corporation(name, place, date))<EOL><DEDENT>return parsed_corporations<EOL>

Parse informations about corporations from given field identified
by `datafield` parameter.

Args:
    datafield (str): MARC field ID ("``110``", "``610``", etc..)
    subfield (str):  MARC subfield ID with name, which is typically
                     stored in "``a``" subfield.
    roles (str): specify which roles you need. Set to ``["any"]`` for
                 any role, ``["dst"]`` for distributors, etc.. For
                 details, see
                 http://www.loc.gov/marc/relators/relaterm.html

Returns:
    list: :class:`Corporation` objects.