コード例 #1
0
 def map_input_symbols(self, symbol_map):
     safe_sub_map = itemmap(tupfun(safe_symbol, safe_symbol), symbol_map)
     symbol_map_ = fallback(safe_sub_map.__getitem__,
                            identity,
                            exception_type=KeyError)
     return tuple(map(symbol_map_, self.inputs))
コード例 #2
0
def map_question_keys(question: dict):
    return pipe(
        question,
        itemmap(vcall(lambda k, v: (question_key_map.get(k, k), v))),
    )
コード例 #3
0
def lookup_mac(mac):
    return _.pipe(
        requests.get(f'http://macvendors.co/api/{mac}'),
        __.maybe_json(default={},
        _.get('result', default={}),
        lambda d: {'mac':mac, 'info': d},
    )

@_.curry
def mac_conv(split_char, mac): 
    return _.pipe(
        mac.split(split_char),
        _.map(lambda b: int(b, 16)),
        _.map(hex),
        _.map(lambda h: h[2:]),
        _.map(lambda h: h.zfill(2)),
        ':'.join,
        lookup_mac,
    )
        
win_mac_conv = mac_conv('-')
macos_mac_conv = mac_conv(':')

# ----------------------------------------------------------------------
# ARP
# ----------------------------------------------------------------------

arp_output_macos = _.partial(getoutput, 'arp -a')
arp_macos_re = re.compile(
    fr'^(?P<name>[?.\w-]*)\s+\((?P<ip>{ip_re})\) at (?P<mac>.*?) on .*$'
)

arp_output_macos = _.partial(getoutput, 'arp -a')
arp_win_re = re.compile(
    fr'^\s+(?P<ip>{ip_re})\s+(?P<mac>.*?)\s+\w+\s*$'
)

def get_arp_data(arp_output, regex, mac_conv):
    return _.pipe(
        arp_output.splitlines(),
        _.map(regex.match),
        _.filter(None),
        _.map(__.call('groupdict')),
        larc.parallel.thread_map(
            lambda d: _merge(d, mac_conv(d['mac'])),
            max_workers=5,
        ),
        tuple,
    )

    def get_arp(arp_output_f, regex, mac_conv):
        def arp(*args):
            return _.pipe(
                arp_output_f(*args),
                lambda output: get_arp_data(output, regex, mac_conv),
            )
            retur arp

get_arp_macos = get_arp(arp_output_macos, arp_macos_re, macos_mac_conv)
get_arp_win = get_arp(arp_output_win, arp_win_re, win_mac_conv)

# -----------------------------------------------------------------------
# ICMP ping
# -----------------------------------------------------------------------

@_.curry
def re_map(regex, map_d, content):
    match = regex.search(content)
    if match:
        d = match.groupict()
        return _.merge(d, _.pipe(
            map_d,
            _.itemmap(__.vcall(lambda key, func: (
                key, func(d[key])
            ))),
        ))

    return {}

ping_re_macos = {
    'tick': re_map(re.compile(
        fr'\d+ bytes from (?P<ip>{ip_re}): icmp_seq=\d+' ttl=\d+'
        fr' time=(?P<ms>\d+(?:\.\d+)?) ms'
    ), {'ms': float}),
}    'totals': re_map(re.compile(
    r'(?P<sent>\d+) packets transmitted,'
    r' (?P<received>\d+) packets received,'
    r' (?P<lost>\d+(?:\.\d+))% packet loss'
), {'sent': int, 'received ': int, 'lost': float}),
'stats': re_map(re.compile(
    fr'round-trip' min/avg/max/stddev ='
    fr' (?P<min>{float_re})/'
    fr'(?P<avg>{float_re})/'
    fr'(?P<max>{float_re})/'
    fr'(?P<std>{float_re}) ms'
), {'min': float, 'avg': float, 'max': float, 'std': float}),
}
コード例 #4
0
def map_answer_keys(answer: dict):
    return pipe(
        answer,
        itemmap(vcall(lambda k, v: (answer_key_map.get(k, k), v))),
    )